OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.midi; | 5 package org.chromium.midi; |
6 | 6 |
7 import android.app.PendingIntent; | 7 import android.app.PendingIntent; |
8 import android.content.BroadcastReceiver; | 8 import android.content.BroadcastReceiver; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.content.IntentFilter; | 11 import android.content.IntentFilter; |
12 import android.hardware.usb.UsbConstants; | 12 import android.hardware.usb.UsbConstants; |
13 import android.hardware.usb.UsbDevice; | 13 import android.hardware.usb.UsbDevice; |
14 import android.hardware.usb.UsbInterface; | 14 import android.hardware.usb.UsbInterface; |
15 import android.hardware.usb.UsbManager; | 15 import android.hardware.usb.UsbManager; |
16 import android.os.Parcelable; | 16 import android.os.Parcelable; |
17 | 17 |
| 18 import org.chromium.base.ContextUtils; |
18 import org.chromium.base.annotations.CalledByNative; | 19 import org.chromium.base.annotations.CalledByNative; |
19 import org.chromium.base.annotations.JNINamespace; | 20 import org.chromium.base.annotations.JNINamespace; |
20 | 21 |
21 import java.util.ArrayList; | 22 import java.util.ArrayList; |
22 import java.util.HashSet; | 23 import java.util.HashSet; |
23 import java.util.List; | 24 import java.util.List; |
24 import java.util.Map; | 25 import java.util.Map; |
25 import java.util.Set; | 26 import java.util.Set; |
26 | 27 |
27 /** | 28 /** |
(...skipping 29 matching lines...) Expand all Loading... |
57 | 58 |
58 /** | 59 /** |
59 * The identifier of this factory. | 60 * The identifier of this factory. |
60 */ | 61 */ |
61 private long mNativePointer; | 62 private long mNativePointer; |
62 | 63 |
63 private static final String ACTION_USB_PERMISSION = "org.chromium.midi.USB_P
ERMISSION"; | 64 private static final String ACTION_USB_PERMISSION = "org.chromium.midi.USB_P
ERMISSION"; |
64 | 65 |
65 /** | 66 /** |
66 * Constructs a UsbMidiDeviceAndroid. | 67 * Constructs a UsbMidiDeviceAndroid. |
67 * @param context | |
68 * @param nativePointer The native pointer to which the created factory is a
ssociated. | 68 * @param nativePointer The native pointer to which the created factory is a
ssociated. |
69 */ | 69 */ |
70 UsbMidiDeviceFactoryAndroid(Context context, long nativePointer) { | 70 UsbMidiDeviceFactoryAndroid(long nativePointer) { |
71 mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE)
; | 71 mUsbManager = (UsbManager) ContextUtils.getApplicationContext().getSyste
mService( |
| 72 Context.USB_SERVICE); |
72 mNativePointer = nativePointer; | 73 mNativePointer = nativePointer; |
73 mReceiver = new BroadcastReceiver() { | 74 mReceiver = new BroadcastReceiver() { |
74 @Override | 75 @Override |
75 public void onReceive(Context context, Intent intent) { | 76 public void onReceive(Context context, Intent intent) { |
76 Parcelable extra = intent.getParcelableExtra(UsbManager.EXTRA_DE
VICE); | 77 Parcelable extra = intent.getParcelableExtra(UsbManager.EXTRA_DE
VICE); |
77 if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getActio
n())) { | 78 if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getActio
n())) { |
78 requestDevicePermissionIfNecessary(context, (UsbDevice) extr
a); | 79 requestDevicePermissionIfNecessary((UsbDevice) extra); |
79 } | 80 } |
80 if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getActio
n())) { | 81 if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getActio
n())) { |
81 onUsbDeviceDetached((UsbDevice) extra); | 82 onUsbDeviceDetached((UsbDevice) extra); |
82 } | 83 } |
83 if (ACTION_USB_PERMISSION.equals(intent.getAction())) { | 84 if (ACTION_USB_PERMISSION.equals(intent.getAction())) { |
84 onUsbDevicePermissionRequestDone(context, intent); | 85 onUsbDevicePermissionRequestDone(context, intent); |
85 } | 86 } |
86 } | 87 } |
87 }; | 88 }; |
88 IntentFilter filter = new IntentFilter(); | 89 IntentFilter filter = new IntentFilter(); |
89 filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); | 90 filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
90 filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); | 91 filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); |
91 filter.addAction(ACTION_USB_PERMISSION); | 92 filter.addAction(ACTION_USB_PERMISSION); |
92 context.registerReceiver(mReceiver, filter); | 93 ContextUtils.getApplicationContext().registerReceiver(mReceiver, filter)
; |
93 mRequestedDevices = new HashSet<UsbDevice>(); | 94 mRequestedDevices = new HashSet<UsbDevice>(); |
94 } | 95 } |
95 | 96 |
96 /** | 97 /** |
97 * Constructs a UsbMidiDeviceAndroid. | 98 * Constructs a UsbMidiDeviceAndroid. |
98 * @param context | |
99 * @param nativePointer The native pointer to which the created factory is a
ssociated. | 99 * @param nativePointer The native pointer to which the created factory is a
ssociated. |
100 */ | 100 */ |
101 @CalledByNative | 101 @CalledByNative |
102 static UsbMidiDeviceFactoryAndroid create(Context context, long nativePointe
r) { | 102 static UsbMidiDeviceFactoryAndroid create(long nativePointer) { |
103 return new UsbMidiDeviceFactoryAndroid(context, nativePointer); | 103 return new UsbMidiDeviceFactoryAndroid(nativePointer); |
104 } | 104 } |
105 | 105 |
106 /** | 106 /** |
107 * Enumerates USB-MIDI devices. | 107 * Enumerates USB-MIDI devices. |
108 * If there are devices having USB-MIDI interfaces, this function requests p
ermission for | 108 * If there are devices having USB-MIDI interfaces, this function requests p
ermission for |
109 * accessing the device to the user. | 109 * accessing the device to the user. |
110 * When the permission request is accepted or rejected, nativeOnUsbMidiDevi
ceRequestDone | 110 * When the permission request is accepted or rejected, nativeOnUsbMidiDevi
ceRequestDone |
111 * will be called. | 111 * will be called. |
112 * | 112 * |
113 * If there are no USB-MIDI interfaces, this function returns false. | 113 * If there are no USB-MIDI interfaces, this function returns false. |
114 * @param context | |
115 * @return true if some permission requests are in progress. | 114 * @return true if some permission requests are in progress. |
116 */ | 115 */ |
117 @CalledByNative | 116 @CalledByNative |
118 boolean enumerateDevices(Context context) { | 117 boolean enumerateDevices() { |
119 assert !mIsEnumeratingDevices; | 118 assert !mIsEnumeratingDevices; |
120 mIsEnumeratingDevices = true; | 119 mIsEnumeratingDevices = true; |
121 Map<String, UsbDevice> devices = mUsbManager.getDeviceList(); | 120 Map<String, UsbDevice> devices = mUsbManager.getDeviceList(); |
122 if (devices.isEmpty()) { | 121 if (devices.isEmpty()) { |
123 // No USB-MIDI devices are found. | 122 // No USB-MIDI devices are found. |
124 mIsEnumeratingDevices = false; | 123 mIsEnumeratingDevices = false; |
125 return false; | 124 return false; |
126 } | 125 } |
127 for (UsbDevice device : devices.values()) { | 126 for (UsbDevice device : devices.values()) { |
128 requestDevicePermissionIfNecessary(context, device); | 127 requestDevicePermissionIfNecessary(device); |
129 } | 128 } |
130 return !mRequestedDevices.isEmpty(); | 129 return !mRequestedDevices.isEmpty(); |
131 } | 130 } |
132 | 131 |
133 /** | 132 /** |
134 * Request a device access permission if there is a MIDI interface in the de
vice. | 133 * Request a device access permission if there is a MIDI interface in the de
vice. |
135 * | 134 * |
136 * @param context | |
137 * @param device a USB device | 135 * @param device a USB device |
138 */ | 136 */ |
139 private void requestDevicePermissionIfNecessary(Context context, UsbDevice d
evice) { | 137 private void requestDevicePermissionIfNecessary(UsbDevice device) { |
140 for (UsbDevice d : mRequestedDevices) { | 138 for (UsbDevice d : mRequestedDevices) { |
141 if (d.getDeviceId() == device.getDeviceId()) { | 139 if (d.getDeviceId() == device.getDeviceId()) { |
142 // It is already requested. | 140 // It is already requested. |
143 return; | 141 return; |
144 } | 142 } |
145 } | 143 } |
146 | 144 |
147 for (int i = 0; i < device.getInterfaceCount(); ++i) { | 145 for (int i = 0; i < device.getInterfaceCount(); ++i) { |
148 UsbInterface iface = device.getInterface(i); | 146 UsbInterface iface = device.getInterface(i); |
149 if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO | 147 if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO |
150 && iface.getInterfaceSubclass() == UsbMidiDeviceAndroid.MIDI
_SUBCLASS) { | 148 && iface.getInterfaceSubclass() == UsbMidiDeviceAndroid.MIDI
_SUBCLASS) { |
151 // There is at least one interface supporting MIDI. | 149 // There is at least one interface supporting MIDI. |
152 mUsbManager.requestPermission( | 150 mUsbManager.requestPermission(device, |
153 device, PendingIntent.getBroadcast( | 151 PendingIntent.getBroadcast(ContextUtils.getApplicationCo
ntext(), 0, |
154 context, 0, new Intent(ACTION_USB_PERMIS
SION), 0)); | 152 new Intent(ACTION_USB_PERMISSION), 0)); |
155 mRequestedDevices.add(device); | 153 mRequestedDevices.add(device); |
156 break; | 154 break; |
157 } | 155 } |
158 } | 156 } |
159 } | 157 } |
160 | 158 |
161 /** | 159 /** |
162 * Called when a USB device is detached. | 160 * Called when a USB device is detached. |
163 * | 161 * |
164 * @param device a USB device | 162 * @param device a USB device |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 if (mIsEnumeratingDevices) { | 238 if (mIsEnumeratingDevices) { |
241 nativeOnUsbMidiDeviceRequestDone(mNativePointer, mDevices.toArray())
; | 239 nativeOnUsbMidiDeviceRequestDone(mNativePointer, mDevices.toArray())
; |
242 mIsEnumeratingDevices = false; | 240 mIsEnumeratingDevices = false; |
243 } else if (midiDevice != null) { | 241 } else if (midiDevice != null) { |
244 nativeOnUsbMidiDeviceAttached(mNativePointer, midiDevice); | 242 nativeOnUsbMidiDeviceAttached(mNativePointer, midiDevice); |
245 } | 243 } |
246 } | 244 } |
247 | 245 |
248 /** | 246 /** |
249 * Disconnects the native object. | 247 * Disconnects the native object. |
250 * @param context | |
251 */ | 248 */ |
252 @CalledByNative | 249 @CalledByNative |
253 void close(Context context) { | 250 void close() { |
254 mNativePointer = 0; | 251 mNativePointer = 0; |
255 context.unregisterReceiver(mReceiver); | 252 ContextUtils.getApplicationContext().unregisterReceiver(mReceiver); |
256 } | 253 } |
257 | 254 |
258 private static native void nativeOnUsbMidiDeviceRequestDone( | 255 private static native void nativeOnUsbMidiDeviceRequestDone( |
259 long nativeUsbMidiDeviceFactoryAndroid, Object[] devices); | 256 long nativeUsbMidiDeviceFactoryAndroid, Object[] devices); |
260 private static native void nativeOnUsbMidiDeviceAttached( | 257 private static native void nativeOnUsbMidiDeviceAttached( |
261 long nativeUsbMidiDeviceFactoryAndroid, Object device); | 258 long nativeUsbMidiDeviceFactoryAndroid, Object device); |
262 private static native void nativeOnUsbMidiDeviceDetached( | 259 private static native void nativeOnUsbMidiDeviceDetached( |
263 long nativeUsbMidiDeviceFactoryAndroid, int index); | 260 long nativeUsbMidiDeviceFactoryAndroid, int index); |
264 } | 261 } |
OLD | NEW |