Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 22 | import android.hardware.IUsbManager; |
| 23 | import android.hardware.UsbConstants; |
| 24 | import android.hardware.UsbDevice; |
| 25 | import android.hardware.UsbEndpoint; |
| 26 | import android.hardware.UsbInterface; |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 27 | import android.hardware.UsbManager; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 28 | import android.net.Uri; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 29 | import android.os.Bundle; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 32 | import android.os.Parcelable; |
| 33 | import android.os.ParcelFileDescriptor; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 34 | import android.os.UEventObserver; |
| 35 | import android.provider.Settings; |
| 36 | import android.util.Log; |
| 37 | import android.util.Slog; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.io.FileNotFoundException; |
| 41 | import java.io.FileReader; |
| 42 | import java.util.ArrayList; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 43 | import java.util.HashMap; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 44 | |
| 45 | /** |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 46 | * <p>UsbService monitors for changes to USB state. |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 47 | */ |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 48 | class UsbService extends IUsbManager.Stub { |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 49 | private static final String TAG = UsbService.class.getSimpleName(); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 50 | private static final boolean LOG = false; |
| 51 | |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 52 | private static final String USB_CONNECTED_MATCH = |
| 53 | "DEVPATH=/devices/virtual/switch/usb_connected"; |
| 54 | private static final String USB_CONFIGURATION_MATCH = |
| 55 | "DEVPATH=/devices/virtual/switch/usb_configuration"; |
| 56 | private static final String USB_FUNCTIONS_MATCH = |
| 57 | "DEVPATH=/devices/virtual/usb_composite/"; |
| 58 | private static final String USB_CONNECTED_PATH = |
| 59 | "/sys/class/switch/usb_connected/state"; |
| 60 | private static final String USB_CONFIGURATION_PATH = |
| 61 | "/sys/class/switch/usb_configuration/state"; |
| 62 | private static final String USB_COMPOSITE_CLASS_PATH = |
| 63 | "/sys/class/usb_composite"; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 64 | |
| 65 | private static final int MSG_UPDATE = 0; |
| 66 | |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 67 | // Delay for debouncing USB disconnects. |
| 68 | // We often get rapid connect/disconnect events when enabling USB functions, |
| 69 | // which need debouncing. |
| 70 | private static final int UPDATE_DELAY = 1000; |
| 71 | |
| 72 | // current connected and configuration state |
| 73 | private int mConnected; |
| 74 | private int mConfiguration; |
| 75 | |
| 76 | // last broadcasted connected and configuration state |
| 77 | private int mLastConnected = -1; |
| 78 | private int mLastConfiguration = -1; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 79 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 80 | // lists of enabled and disabled USB functions (for USB device mode) |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 81 | private final ArrayList<String> mEnabledFunctions = new ArrayList<String>(); |
| 82 | private final ArrayList<String> mDisabledFunctions = new ArrayList<String>(); |
| 83 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 84 | private final HashMap<String,UsbDevice> mDevices = new HashMap<String,UsbDevice>(); |
| 85 | |
Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 86 | // USB busses to exclude from USB host support |
| 87 | private final String[] mHostBlacklist; |
| 88 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 89 | private boolean mSystemReady; |
| 90 | |
| 91 | private final Context mContext; |
| 92 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 93 | private final UEventObserver mUEventObserver = new UEventObserver() { |
| 94 | @Override |
| 95 | public void onUEvent(UEventObserver.UEvent event) { |
| 96 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 97 | Slog.v(TAG, "USB UEVENT: " + event.toString()); |
| 98 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 99 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 100 | synchronized (this) { |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 101 | String name = event.get("SWITCH_NAME"); |
| 102 | String state = event.get("SWITCH_STATE"); |
| 103 | if (name != null && state != null) { |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 104 | try { |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 105 | int intState = Integer.parseInt(state); |
| 106 | if ("usb_connected".equals(name)) { |
| 107 | mConnected = intState; |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 108 | // trigger an Intent broadcast |
| 109 | if (mSystemReady) { |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 110 | // debounce disconnects |
| 111 | update(mConnected == 0); |
| 112 | } |
| 113 | } else if ("usb_configuration".equals(name)) { |
| 114 | mConfiguration = intState; |
| 115 | // trigger an Intent broadcast |
| 116 | if (mSystemReady) { |
| 117 | update(mConnected == 0); |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 118 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 119 | } |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 120 | } catch (NumberFormatException e) { |
| 121 | Slog.e(TAG, "Could not parse switch state from event " + event); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 122 | } |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 123 | } else { |
| 124 | String function = event.get("FUNCTION"); |
| 125 | String enabledStr = event.get("ENABLED"); |
| 126 | if (function != null && enabledStr != null) { |
| 127 | // Note: we do not broadcast a change when a function is enabled or disabled. |
| 128 | // We just record the state change for the next broadcast. |
| 129 | boolean enabled = "1".equals(enabledStr); |
| 130 | if (enabled) { |
| 131 | if (!mEnabledFunctions.contains(function)) { |
| 132 | mEnabledFunctions.add(function); |
| 133 | } |
| 134 | mDisabledFunctions.remove(function); |
| 135 | } else { |
| 136 | if (!mDisabledFunctions.contains(function)) { |
| 137 | mDisabledFunctions.add(function); |
| 138 | } |
| 139 | mEnabledFunctions.remove(function); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 140 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | public UsbService(Context context) { |
| 148 | mContext = context; |
Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 149 | mHostBlacklist = context.getResources().getStringArray( |
| 150 | com.android.internal.R.array.config_usbHostBlacklist); |
| 151 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 152 | init(); // set initial status |
| 153 | |
David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 154 | if (mConfiguration >= 0) { |
| 155 | mUEventObserver.startObserving(USB_CONNECTED_MATCH); |
| 156 | mUEventObserver.startObserving(USB_CONFIGURATION_MATCH); |
| 157 | mUEventObserver.startObserving(USB_FUNCTIONS_MATCH); |
| 158 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 159 | } |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 160 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 161 | private final void init() { |
| 162 | char[] buffer = new char[1024]; |
| 163 | |
David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 164 | mConfiguration = -1; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 165 | try { |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 166 | FileReader file = new FileReader(USB_CONNECTED_PATH); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 167 | int len = file.read(buffer, 0, 1024); |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 168 | file.close(); |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 169 | mConnected = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 170 | |
| 171 | file = new FileReader(USB_CONFIGURATION_PATH); |
| 172 | len = file.read(buffer, 0, 1024); |
| 173 | file.close(); |
| 174 | mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim()); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 175 | |
| 176 | } catch (FileNotFoundException e) { |
David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 177 | Slog.i(TAG, "This kernel does not have USB configuration switch support"); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 178 | } catch (Exception e) { |
| 179 | Slog.e(TAG, "" , e); |
| 180 | } |
David 'Digit' Turner | 49db853 | 2011-01-17 00:19:37 +0100 | [diff] [blame] | 181 | if (mConfiguration < 0) |
| 182 | return; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 183 | |
| 184 | try { |
| 185 | File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles(); |
| 186 | for (int i = 0; i < files.length; i++) { |
| 187 | File file = new File(files[i], "enable"); |
| 188 | FileReader reader = new FileReader(file); |
| 189 | int len = reader.read(buffer, 0, 1024); |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 190 | reader.close(); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 191 | int value = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 192 | String functionName = files[i].getName(); |
| 193 | if (value == 1) { |
| 194 | mEnabledFunctions.add(functionName); |
| 195 | } else { |
| 196 | mDisabledFunctions.add(functionName); |
| 197 | } |
| 198 | } |
| 199 | } catch (FileNotFoundException e) { |
| 200 | Slog.w(TAG, "This kernel does not have USB composite class support"); |
| 201 | } catch (Exception e) { |
| 202 | Slog.e(TAG, "" , e); |
| 203 | } |
| 204 | } |
| 205 | |
Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 206 | private boolean isBlackListed(String deviceName) { |
| 207 | int count = mHostBlacklist.length; |
| 208 | for (int i = 0; i < count; i++) { |
| 209 | if (deviceName.startsWith(mHostBlacklist[i])) { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 216 | private boolean isBlackListed(int clazz, int subClass, int protocol) { |
| 217 | // blacklist hubs |
| 218 | if (clazz == UsbConstants.USB_CLASS_HUB) return true; |
| 219 | |
| 220 | // blacklist HID boot devices (mouse and keyboard) |
| 221 | if (clazz == UsbConstants.USB_CLASS_HID && |
| 222 | subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) { |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 229 | // called from JNI in monitorUsbHostBus() |
| 230 | private void usbDeviceAdded(String deviceName, int vendorID, int productID, |
| 231 | int deviceClass, int deviceSubclass, int deviceProtocol, |
| 232 | /* array of quintuples containing id, class, subclass, protocol |
| 233 | and number of endpoints for each interface */ |
| 234 | int[] interfaceValues, |
| 235 | /* array of quadruples containing address, attributes, max packet size |
| 236 | and interval for each endpoint */ |
| 237 | int[] endpointValues) { |
| 238 | |
Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 239 | if (isBlackListed(deviceName) || |
| 240 | isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) { |
Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 244 | synchronized (mDevices) { |
| 245 | if (mDevices.get(deviceName) != null) { |
| 246 | Log.w(TAG, "device already on mDevices list: " + deviceName); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | int numInterfaces = interfaceValues.length / 5; |
| 251 | Parcelable[] interfaces = new UsbInterface[numInterfaces]; |
| 252 | try { |
| 253 | // repackage interfaceValues as an array of UsbInterface |
| 254 | int intf, endp, ival = 0, eval = 0; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 255 | for (intf = 0; intf < numInterfaces; intf++) { |
| 256 | int interfaceId = interfaceValues[ival++]; |
| 257 | int interfaceClass = interfaceValues[ival++]; |
| 258 | int interfaceSubclass = interfaceValues[ival++]; |
| 259 | int interfaceProtocol = interfaceValues[ival++]; |
| 260 | int numEndpoints = interfaceValues[ival++]; |
| 261 | |
| 262 | Parcelable[] endpoints = new UsbEndpoint[numEndpoints]; |
| 263 | for (endp = 0; endp < numEndpoints; endp++) { |
| 264 | int address = endpointValues[eval++]; |
| 265 | int attributes = endpointValues[eval++]; |
| 266 | int maxPacketSize = endpointValues[eval++]; |
| 267 | int interval = endpointValues[eval++]; |
| 268 | endpoints[endp] = new UsbEndpoint(address, attributes, |
| 269 | maxPacketSize, interval); |
| 270 | } |
| 271 | |
Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 272 | // don't allow if any interfaces are blacklisted |
| 273 | if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) { |
| 274 | return; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 275 | } |
| 276 | interfaces[intf] = new UsbInterface(interfaceId, interfaceClass, |
| 277 | interfaceSubclass, interfaceProtocol, endpoints); |
| 278 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 279 | } catch (Exception e) { |
| 280 | // beware of index out of bound exceptions, which might happen if |
| 281 | // a device does not set bNumEndpoints correctly |
| 282 | Log.e(TAG, "error parsing USB descriptors", e); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | UsbDevice device = new UsbDevice(deviceName, vendorID, productID, |
| 287 | deviceClass, deviceSubclass, deviceProtocol, interfaces); |
| 288 | mDevices.put(deviceName, device); |
| 289 | |
| 290 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
| 291 | intent.putExtra(UsbManager.EXTRA_DEVICE_NAME, deviceName); |
| 292 | intent.putExtra(UsbManager.EXTRA_VENDOR_ID, vendorID); |
| 293 | intent.putExtra(UsbManager.EXTRA_PRODUCT_ID, productID); |
| 294 | intent.putExtra(UsbManager.EXTRA_DEVICE_CLASS, deviceClass); |
| 295 | intent.putExtra(UsbManager.EXTRA_DEVICE_SUBCLASS, deviceSubclass); |
| 296 | intent.putExtra(UsbManager.EXTRA_DEVICE_PROTOCOL, deviceProtocol); |
| 297 | intent.putExtra(UsbManager.EXTRA_DEVICE, device); |
| 298 | Log.d(TAG, "usbDeviceAdded, sending " + intent); |
| 299 | mContext.sendBroadcast(intent); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // called from JNI in monitorUsbHostBus() |
| 304 | private void usbDeviceRemoved(String deviceName) { |
| 305 | synchronized (mDevices) { |
| 306 | UsbDevice device = mDevices.remove(deviceName); |
| 307 | if (device != null) { |
| 308 | Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_DETACHED); |
| 309 | intent.putExtra(UsbManager.EXTRA_DEVICE_NAME, deviceName); |
| 310 | Log.d(TAG, "usbDeviceRemoved, sending " + intent); |
| 311 | mContext.sendBroadcast(intent); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 316 | private void initHostSupport() { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 317 | // Create a thread to call into native code to wait for USB host events. |
| 318 | // This thread will call us back on usbDeviceAdded and usbDeviceRemoved. |
| 319 | Runnable runnable = new Runnable() { |
| 320 | public void run() { |
| 321 | monitorUsbHostBus(); |
| 322 | } |
| 323 | }; |
| 324 | new Thread(null, runnable, "UsbService host thread").start(); |
Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 327 | void systemReady() { |
| 328 | synchronized (this) { |
Mike Lockwood | da39f0e | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 329 | if (mContext.getResources().getBoolean( |
| 330 | com.android.internal.R.bool.config_hasUsbHostSupport)) { |
| 331 | // start monitoring for connected USB devices |
| 332 | initHostSupport(); |
| 333 | } |
| 334 | |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 335 | update(false); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 336 | mSystemReady = true; |
| 337 | } |
| 338 | } |
| 339 | |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 340 | private final void update(boolean delayed) { |
| 341 | mHandler.removeMessages(MSG_UPDATE); |
| 342 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 345 | /* Returns a list of all currently attached USB devices */ |
| 346 | public void getDeviceList(Bundle devices) { |
| 347 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
| 348 | synchronized (mDevices) { |
| 349 | for (String name : mDevices.keySet()) { |
| 350 | devices.putParcelable(name, mDevices.get(name)); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | public ParcelFileDescriptor openDevice(String deviceName) { |
Mike Lockwood | a8e3a89 | 2011-02-01 13:46:50 -0500 | [diff] [blame] | 356 | if (isBlackListed(deviceName)) { |
| 357 | throw new SecurityException("USB device is on a restricted bus"); |
| 358 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 359 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_USB, null); |
Mike Lockwood | 8b682ad | 2011-02-01 15:53:11 -0500 | [diff] [blame] | 360 | if (mDevices.get(deviceName) == null) { |
| 361 | // if it is not in mDevices, it either does not exist or is blacklisted |
| 362 | throw new IllegalArgumentException("device " + deviceName + " does not exist or is restricted"); |
| 363 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 364 | return nativeOpenDevice(deviceName); |
| 365 | } |
| 366 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 367 | private final Handler mHandler = new Handler() { |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 368 | private void addEnabledFunctions(Intent intent) { |
| 369 | // include state of all USB functions in our extras |
| 370 | for (int i = 0; i < mEnabledFunctions.size(); i++) { |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 371 | intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED); |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 372 | } |
| 373 | for (int i = 0; i < mDisabledFunctions.size(); i++) { |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 374 | intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED); |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 378 | @Override |
| 379 | public void handleMessage(Message msg) { |
| 380 | switch (msg.what) { |
| 381 | case MSG_UPDATE: |
| 382 | synchronized (this) { |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 383 | if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) { |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 384 | |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 385 | final ContentResolver cr = mContext.getContentResolver(); |
| 386 | if (Settings.Secure.getInt(cr, |
| 387 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 388 | Slog.i(TAG, "Device not provisioned, skipping USB broadcast"); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | mLastConnected = mConnected; |
| 393 | mLastConfiguration = mConfiguration; |
| 394 | |
| 395 | // send a sticky broadcast containing current USB state |
| 396 | Intent intent = new Intent(UsbManager.ACTION_USB_STATE); |
| 397 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 398 | intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0); |
| 399 | intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration); |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 400 | addEnabledFunctions(intent); |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 401 | mContext.sendStickyBroadcast(intent); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 402 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 403 | } |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | }; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 408 | |
| 409 | private native void monitorUsbHostBus(); |
| 410 | private native ParcelFileDescriptor nativeOpenDevice(String deviceName); |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 411 | } |