This source file includes following definitions.
- JNINamespace
- create
- enumerateDevices
- onRequestDone
- close
- nativeOnUsbMidiDeviceRequestDone
package org.chromium.media;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@JNINamespace("media")
class UsbMidiDeviceFactoryAndroid {
private UsbManager mUsbManager;
private BroadcastReceiver mReceiver;
private final List<UsbMidiDeviceAndroid> mDevices = new ArrayList<UsbMidiDeviceAndroid>();
private Set<UsbDevice> mRequestedDevices;
private long mNativePointer;
private static final String ACTION_USB_PERMISSION =
"org.chromium.media.USB_PERMISSION";
UsbMidiDeviceFactoryAndroid(long nativePointer) {
mNativePointer = nativePointer;
}
@CalledByNative
static UsbMidiDeviceFactoryAndroid create(long nativePointer) {
return new UsbMidiDeviceFactoryAndroid(nativePointer);
}
@CalledByNative
boolean enumerateDevices(Context context) {
mUsbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
Map<String, UsbDevice> devices = mUsbManager.getDeviceList();
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 0, new Intent(ACTION_USB_PERMISSION), 0);
mRequestedDevices = new HashSet<UsbDevice>();
for (UsbDevice device : devices.values()) {
boolean found = false;
for (int i = 0; i < device.getInterfaceCount() && !found; ++i) {
UsbInterface iface = device.getInterface(i);
if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO &&
iface.getInterfaceSubclass() == UsbMidiDeviceAndroid.MIDI_SUBCLASS) {
found = true;
}
}
if (found) {
mUsbManager.requestPermission(device, pendingIntent);
mRequestedDevices.add(device);
}
}
if (mRequestedDevices.isEmpty()) {
return false;
}
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
onRequestDone(context, intent);
}
}
};
context.registerReceiver(mReceiver, filter);
return true;
}
private void onRequestDone(Context context, Intent intent) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (!mRequestedDevices.contains(device)) {
return;
}
mRequestedDevices.remove(device);
if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
device = null;
}
if (device != null) {
mDevices.add(new UsbMidiDeviceAndroid(mUsbManager, device));
}
if (mRequestedDevices.isEmpty()) {
context.unregisterReceiver(mReceiver);
if (mNativePointer != 0) {
nativeOnUsbMidiDeviceRequestDone(mNativePointer, mDevices.toArray());
}
}
}
@CalledByNative
void close() {
mNativePointer = 0;
}
private static native void nativeOnUsbMidiDeviceRequestDone(
long nativeUsbMidiDeviceFactoryAndroid, Object[] devices);
}