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"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/bind.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/lock.h"
17 #include "jni/UsbMidiDeviceFactoryAndroid_jni.h"
18 #include "media/midi/usb_midi_device_android.h"
24 typedef UsbMidiDevice::Factory::Callback Callback
;
28 UsbMidiDeviceFactoryAndroid::UsbMidiDeviceFactoryAndroid() : delegate_(NULL
) {}
30 UsbMidiDeviceFactoryAndroid::~UsbMidiDeviceFactoryAndroid() {
31 JNIEnv
* env
= base::android::AttachCurrentThread();
32 if (!raw_factory_
.is_null())
33 Java_UsbMidiDeviceFactoryAndroid_close(
34 env
, raw_factory_
.obj(), base::android::GetApplicationContext());
37 void UsbMidiDeviceFactoryAndroid::EnumerateDevices(
38 UsbMidiDeviceDelegate
* delegate
,
41 JNIEnv
* env
= base::android::AttachCurrentThread();
42 uintptr_t pointer
= reinterpret_cast<uintptr_t>(this);
43 raw_factory_
.Reset(Java_UsbMidiDeviceFactoryAndroid_create(
44 env
, base::android::GetApplicationContext(), pointer
));
49 if (Java_UsbMidiDeviceFactoryAndroid_enumerateDevices(
50 env
, raw_factory_
.obj(), base::android::GetApplicationContext())) {
51 // Asynchronous operation.
54 // No devices are found.
55 ScopedVector
<UsbMidiDevice
> devices
;
56 callback
.Run(true, &devices
);
59 // Called from the Java world.
60 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceRequestDone(
63 jobjectArray devices
) {
64 size_t size
= env
->GetArrayLength(devices
);
65 ScopedVector
<UsbMidiDevice
> devices_to_pass
;
66 for (size_t i
= 0; i
< size
; ++i
) {
67 UsbMidiDeviceAndroid::ObjectRef
raw_device(
68 env
, env
->GetObjectArrayElement(devices
, i
));
69 devices_to_pass
.push_back(new UsbMidiDeviceAndroid(raw_device
, delegate_
));
72 callback_
.Run(true, &devices_to_pass
);
75 // Called from the Java world.
76 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceAttached(
80 UsbMidiDeviceAndroid::ObjectRef
raw_device(env
, device
);
81 delegate_
->OnDeviceAttached(
82 scoped_ptr
<UsbMidiDevice
>(
83 new UsbMidiDeviceAndroid(raw_device
, delegate_
)));
86 // Called from the Java world.
87 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceDetached(
91 delegate_
->OnDeviceDetached(index
);
94 bool UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory(JNIEnv
* env
) {
95 return RegisterNativesImpl(env
);