1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/midi/usb_midi_device_factory_android.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/synchronization/lock.h"
13 #include "jni/UsbMidiDeviceFactoryAndroid_jni.h"
14 #include "media/midi/usb_midi_device_android.h"
21 typedef UsbMidiDevice::Factory::Callback Callback
;
25 UsbMidiDeviceFactoryAndroid::UsbMidiDeviceFactoryAndroid() : delegate_(NULL
) {}
27 UsbMidiDeviceFactoryAndroid::~UsbMidiDeviceFactoryAndroid() {
28 JNIEnv
* env
= base::android::AttachCurrentThread();
29 if (!raw_factory_
.is_null())
30 Java_UsbMidiDeviceFactoryAndroid_close(
31 env
, raw_factory_
.obj(), base::android::GetApplicationContext());
34 void UsbMidiDeviceFactoryAndroid::EnumerateDevices(
35 UsbMidiDeviceDelegate
* delegate
,
38 JNIEnv
* env
= base::android::AttachCurrentThread();
39 uintptr_t pointer
= reinterpret_cast<uintptr_t>(this);
40 raw_factory_
.Reset(Java_UsbMidiDeviceFactoryAndroid_create(
41 env
, base::android::GetApplicationContext(), pointer
));
46 if (Java_UsbMidiDeviceFactoryAndroid_enumerateDevices(
47 env
, raw_factory_
.obj(), base::android::GetApplicationContext())) {
48 // Asynchronous operation.
51 // No devices are found.
52 ScopedVector
<UsbMidiDevice
> devices
;
53 callback
.Run(true, &devices
);
56 // Called from the Java world.
57 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceRequestDone(
60 jobjectArray devices
) {
61 size_t size
= env
->GetArrayLength(devices
);
62 ScopedVector
<UsbMidiDevice
> devices_to_pass
;
63 for (size_t i
= 0; i
< size
; ++i
) {
64 UsbMidiDeviceAndroid::ObjectRef
raw_device(
65 env
, env
->GetObjectArrayElement(devices
, i
));
66 devices_to_pass
.push_back(new UsbMidiDeviceAndroid(raw_device
, delegate_
));
69 callback_
.Run(true, &devices_to_pass
);
72 // Called from the Java world.
73 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceAttached(
77 UsbMidiDeviceAndroid::ObjectRef
raw_device(env
, device
);
78 delegate_
->OnDeviceAttached(
79 scoped_ptr
<UsbMidiDevice
>(
80 new UsbMidiDeviceAndroid(raw_device
, delegate_
)));
83 // Called from the Java world.
84 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceDetached(
88 delegate_
->OnDeviceDetached(index
);
91 bool UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory(JNIEnv
* env
) {
92 return RegisterNativesImpl(env
);