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 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 18 | package android.hardware.usb; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 19 | |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 20 | import com.android.internal.util.Preconditions; |
| 21 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 22 | import android.app.PendingIntent; |
| 23 | import android.content.Context; |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 24 | import android.content.pm.PackageManager.NameNotFoundException; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 27 | import android.os.Process; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 31 | import java.util.HashMap; |
Mike Lockwood | 08bff3b | 2010-08-31 13:27:05 -0400 | [diff] [blame] | 32 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 33 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 34 | * This class allows you to access the state of USB and communicate with USB devices. |
| 35 | * Currently only host mode is supported in the public API. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 36 | * |
| 37 | * <p>You can obtain an instance of this class by calling |
| 38 | * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. |
| 39 | * |
| 40 | * {@samplecode |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 41 | * UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);} |
| 42 | * |
| 43 | * <div class="special reference"> |
| 44 | * <h3>Developer Guides</h3> |
| 45 | * <p>For more information about communicating with USB hardware, read the |
Mark Lu | 8b73990 | 2016-06-24 15:38:02 -0700 | [diff] [blame] | 46 | * <a href="{@docRoot}guide/topics/connectivity/usb/index.html">USB developer guide</a>.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 47 | * </div> |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 48 | */ |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 49 | public class UsbManager { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 50 | private static final String TAG = "UsbManager"; |
| 51 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 52 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 53 | * Broadcast Action: A sticky broadcast for USB state change events when in device mode. |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 54 | * |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 55 | * This is a sticky broadcast for clients that includes USB connected/disconnected state, |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 56 | * <ul> |
| 57 | * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. |
Daichi Hirono | 618fa6f | 2016-03-17 14:14:32 +0900 | [diff] [blame] | 58 | * <li> {@link #USB_HOST_CONNECTED} boolean indicating whether USB is connected or |
| 59 | * disconnected as host. |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 60 | * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 61 | * currently zero if not configured, one for configured. |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 62 | * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the |
| 63 | * adb function is enabled |
| 64 | * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the |
| 65 | * RNDIS ethernet function is enabled |
| 66 | * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the |
| 67 | * MTP function is enabled |
| 68 | * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the |
| 69 | * PTP function is enabled |
| 70 | * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the |
| 71 | * accessory function is enabled |
Mike Lockwood | 9d5a4be | 2012-04-06 09:41:32 -0700 | [diff] [blame] | 72 | * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the |
| 73 | * audio source function is enabled |
Mike Lockwood | 2a57bc7 | 2014-09-19 11:16:52 -0700 | [diff] [blame] | 74 | * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the |
| 75 | * MIDI function is enabled |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 76 | * </ul> |
Yasuhiro Matsuda | 48b9a7c | 2015-07-16 19:00:16 +0900 | [diff] [blame] | 77 | * If the sticky intent has not been found, that indicates USB is disconnected, |
| 78 | * USB is not configued, MTP function is enabled, and all the other functions are disabled. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 79 | * |
| 80 | * {@hide} |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 81 | */ |
| 82 | public static final String ACTION_USB_STATE = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 83 | "android.hardware.usb.action.USB_STATE"; |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 84 | |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Broadcast Action: A broadcast for USB port changes. |
| 87 | * |
| 88 | * This intent is sent when a USB port is added, removed, or changes state. |
| 89 | * <ul> |
| 90 | * <li> {@link #EXTRA_PORT} containing the {@link android.hardware.usb.UsbPort} |
| 91 | * for the port. |
| 92 | * <li> {@link #EXTRA_PORT_STATUS} containing the {@link android.hardware.usb.UsbPortStatus} |
| 93 | * for the port, or null if the port has been removed |
| 94 | * </ul> |
| 95 | * |
| 96 | * @hide |
| 97 | */ |
| 98 | public static final String ACTION_USB_PORT_CHANGED = |
| 99 | "android.hardware.usb.action.USB_PORT_CHANGED"; |
| 100 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 101 | /** |
| 102 | * Broadcast Action: A broadcast for USB device attached event. |
| 103 | * |
| 104 | * This intent is sent when a USB device is attached to the USB bus when in host mode. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 105 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 106 | * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 107 | * for the attached device |
| 108 | * </ul> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 109 | */ |
| 110 | public static final String ACTION_USB_DEVICE_ATTACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 111 | "android.hardware.usb.action.USB_DEVICE_ATTACHED"; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Broadcast Action: A broadcast for USB device detached event. |
| 115 | * |
| 116 | * This intent is sent when a USB device is detached from the USB bus when in host mode. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 117 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 118 | * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} |
Mike Lockwood | 188d00b | 2011-02-23 13:14:33 -0800 | [diff] [blame] | 119 | * for the detached device |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 120 | * </ul> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 121 | */ |
| 122 | public static final String ACTION_USB_DEVICE_DETACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 123 | "android.hardware.usb.action.USB_DEVICE_DETACHED"; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 124 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 125 | /** |
| 126 | * Broadcast Action: A broadcast for USB accessory attached event. |
| 127 | * |
| 128 | * This intent is sent when a USB accessory is attached. |
| 129 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 130 | * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 131 | * for the attached accessory |
| 132 | * </ul> |
| 133 | */ |
| 134 | public static final String ACTION_USB_ACCESSORY_ATTACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 135 | "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Broadcast Action: A broadcast for USB accessory detached event. |
| 139 | * |
| 140 | * This intent is sent when a USB accessory is detached. |
| 141 | * <ul> |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 142 | * <li> {@link #EXTRA_ACCESSORY} containing the {@link UsbAccessory} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 143 | * for the attached accessory that was detached |
| 144 | * </ul> |
| 145 | */ |
| 146 | public static final String ACTION_USB_ACCESSORY_DETACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 147 | "android.hardware.usb.action.USB_ACCESSORY_DETACHED"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 148 | |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 149 | /** |
| 150 | * Boolean extra indicating whether USB is connected or disconnected. |
| 151 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 152 | * |
| 153 | * {@hide} |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 154 | */ |
| 155 | public static final String USB_CONNECTED = "connected"; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 156 | |
| 157 | /** |
Daichi Hirono | 618fa6f | 2016-03-17 14:14:32 +0900 | [diff] [blame] | 158 | * Boolean extra indicating whether USB is connected or disconnected as host. |
| 159 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
| 160 | * |
| 161 | * {@hide} |
| 162 | */ |
| 163 | public static final String USB_HOST_CONNECTED = "host_connected"; |
| 164 | |
| 165 | /** |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 166 | * Boolean extra indicating whether USB is configured. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 167 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 168 | * |
| 169 | * {@hide} |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 170 | */ |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 171 | public static final String USB_CONFIGURED = "configured"; |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 172 | |
| 173 | /** |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 174 | * Boolean extra indicating whether confidential user data, such as photos, should be |
| 175 | * made available on the USB connection. This variable will only be set when the user |
| 176 | * has explicitly asked for this data to be unlocked. |
| 177 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
| 178 | * |
| 179 | * {@hide} |
| 180 | */ |
| 181 | public static final String USB_DATA_UNLOCKED = "unlocked"; |
| 182 | |
| 183 | /** |
Jerry Zhang | bb598ee | 2016-10-24 14:35:08 -0700 | [diff] [blame] | 184 | * Boolean extra indicating whether the intent represents a change in the usb |
| 185 | * configuration (as opposed to a state update). |
| 186 | * |
| 187 | * {@hide} |
| 188 | */ |
| 189 | public static final String USB_CONFIG_CHANGED = "config_changed"; |
| 190 | |
| 191 | /** |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 192 | * A placeholder indicating that no USB function is being specified. |
| 193 | * Used to distinguish between selecting no function vs. the default function in |
| 194 | * {@link #setCurrentFunction(String)}. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 195 | * |
| 196 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 197 | */ |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 198 | public static final String USB_FUNCTION_NONE = "none"; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Name of the adb USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 202 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 203 | * |
| 204 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 205 | */ |
| 206 | public static final String USB_FUNCTION_ADB = "adb"; |
| 207 | |
| 208 | /** |
| 209 | * Name of the RNDIS ethernet USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 210 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 211 | * |
| 212 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 213 | */ |
| 214 | public static final String USB_FUNCTION_RNDIS = "rndis"; |
| 215 | |
| 216 | /** |
| 217 | * Name of the MTP USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 218 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 219 | * |
| 220 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 221 | */ |
| 222 | public static final String USB_FUNCTION_MTP = "mtp"; |
| 223 | |
| 224 | /** |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 225 | * Name of the PTP USB function. |
| 226 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 227 | * |
| 228 | * {@hide} |
| 229 | */ |
| 230 | public static final String USB_FUNCTION_PTP = "ptp"; |
| 231 | |
| 232 | /** |
Mike Lockwood | 9d5a4be | 2012-04-06 09:41:32 -0700 | [diff] [blame] | 233 | * Name of the audio source USB function. |
| 234 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 235 | * |
| 236 | * {@hide} |
| 237 | */ |
| 238 | public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; |
| 239 | |
| 240 | /** |
Mike Lockwood | 2a57bc7 | 2014-09-19 11:16:52 -0700 | [diff] [blame] | 241 | * Name of the MIDI USB function. |
| 242 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 243 | * |
| 244 | * {@hide} |
| 245 | */ |
| 246 | public static final String USB_FUNCTION_MIDI = "midi"; |
| 247 | |
| 248 | /** |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 249 | * Name of the Accessory USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 250 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 251 | * |
| 252 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 253 | */ |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 254 | public static final String USB_FUNCTION_ACCESSORY = "accessory"; |
| 255 | |
| 256 | /** |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 257 | * Name of extra for {@link #ACTION_USB_PORT_CHANGED} |
| 258 | * containing the {@link UsbPort} object for the port. |
| 259 | * |
| 260 | * @hide |
| 261 | */ |
| 262 | public static final String EXTRA_PORT = "port"; |
| 263 | |
| 264 | /** |
| 265 | * Name of extra for {@link #ACTION_USB_PORT_CHANGED} |
| 266 | * containing the {@link UsbPortStatus} object for the port, or null if the port |
| 267 | * was removed. |
| 268 | * |
| 269 | * @hide |
| 270 | */ |
| 271 | public static final String EXTRA_PORT_STATUS = "portStatus"; |
| 272 | |
| 273 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 274 | * Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 275 | * {@link #ACTION_USB_DEVICE_DETACHED} broadcasts |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 276 | * containing the {@link UsbDevice} object for the device. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 277 | */ |
| 278 | public static final String EXTRA_DEVICE = "device"; |
| 279 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 280 | /** |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 281 | * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and |
| 282 | * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 283 | * containing the {@link UsbAccessory} object for the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 284 | */ |
| 285 | public static final String EXTRA_ACCESSORY = "accessory"; |
| 286 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 287 | /** |
| 288 | * Name of extra added to the {@link android.app.PendingIntent} |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 289 | * passed into {@link #requestPermission(UsbDevice, PendingIntent)} |
| 290 | * or {@link #requestPermission(UsbAccessory, PendingIntent)} |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 291 | * containing a boolean value indicating whether the user granted permission or not. |
| 292 | */ |
| 293 | public static final String EXTRA_PERMISSION_GRANTED = "permission"; |
| 294 | |
| 295 | private final Context mContext; |
| 296 | private final IUsbManager mService; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * {@hide} |
| 300 | */ |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 301 | public UsbManager(Context context, IUsbManager service) { |
| 302 | mContext = context; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 303 | mService = service; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns a HashMap containing all USB devices currently attached. |
| 308 | * USB device name is the key for the returned HashMap. |
| 309 | * The result will be empty if no devices are attached, or if |
| 310 | * USB host mode is inactive or unsupported. |
| 311 | * |
| 312 | * @return HashMap containing all connected USB devices. |
| 313 | */ |
| 314 | public HashMap<String,UsbDevice> getDeviceList() { |
| 315 | Bundle bundle = new Bundle(); |
| 316 | try { |
| 317 | mService.getDeviceList(bundle); |
| 318 | HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>(); |
| 319 | for (String name : bundle.keySet()) { |
| 320 | result.put(name, (UsbDevice)bundle.get(name)); |
| 321 | } |
| 322 | return result; |
| 323 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 324 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Opens the device so it can be used to send and receive |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 330 | * data using {@link android.hardware.usb.UsbRequest}. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 331 | * |
| 332 | * @param device the device to open |
mike wakerly | 1567a43 | 2012-07-17 19:58:19 -0700 | [diff] [blame] | 333 | * @return a {@link UsbDeviceConnection}, or {@code null} if open failed |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 334 | */ |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 335 | public UsbDeviceConnection openDevice(UsbDevice device) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 336 | try { |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 337 | String deviceName = device.getDeviceName(); |
| 338 | ParcelFileDescriptor pfd = mService.openDevice(deviceName); |
| 339 | if (pfd != null) { |
| 340 | UsbDeviceConnection connection = new UsbDeviceConnection(device); |
Philip P. Moltmann | 5201f1e | 2016-09-30 14:58:27 -0700 | [diff] [blame] | 341 | boolean result = connection.open(deviceName, pfd, mContext); |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 342 | pfd.close(); |
| 343 | if (result) { |
| 344 | return connection; |
| 345 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 346 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 347 | } catch (Exception e) { |
| 348 | Log.e(TAG, "exception in UsbManager.openDevice", e); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 349 | } |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 350 | return null; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 353 | /** |
| 354 | * Returns a list of currently attached USB accessories. |
| 355 | * (in the current implementation there can be at most one) |
| 356 | * |
| 357 | * @return list of USB accessories, or null if none are attached. |
| 358 | */ |
| 359 | public UsbAccessory[] getAccessoryList() { |
| 360 | try { |
| 361 | UsbAccessory accessory = mService.getCurrentAccessory(); |
| 362 | if (accessory == null) { |
| 363 | return null; |
| 364 | } else { |
| 365 | return new UsbAccessory[] { accessory }; |
| 366 | } |
| 367 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 368 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Opens a file descriptor for reading and writing data to the USB accessory. |
| 374 | * |
| 375 | * @param accessory the USB accessory to open |
| 376 | * @return file descriptor, or null if the accessor could not be opened. |
| 377 | */ |
| 378 | public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { |
| 379 | try { |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 380 | return mService.openAccessory(accessory); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 381 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 382 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 386 | /** |
| 387 | * Returns true if the caller has permission to access the device. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 388 | * Permission might have been granted temporarily via |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 389 | * {@link #requestPermission(UsbDevice, PendingIntent)} or |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 390 | * by the user choosing the caller as the default application for the device. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 391 | * |
| 392 | * @param device to check permissions for |
| 393 | * @return true if caller has permission |
| 394 | */ |
| 395 | public boolean hasPermission(UsbDevice device) { |
| 396 | try { |
| 397 | return mService.hasDevicePermission(device); |
| 398 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 399 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Returns true if the caller has permission to access the accessory. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 405 | * Permission might have been granted temporarily via |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 406 | * {@link #requestPermission(UsbAccessory, PendingIntent)} or |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 407 | * by the user choosing the caller as the default application for the accessory. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 408 | * |
| 409 | * @param accessory to check permissions for |
| 410 | * @return true if caller has permission |
| 411 | */ |
| 412 | public boolean hasPermission(UsbAccessory accessory) { |
| 413 | try { |
| 414 | return mService.hasAccessoryPermission(accessory); |
| 415 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 416 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 421 | * Requests temporary permission for the given package to access the device. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 422 | * This may result in a system dialog being displayed to the user |
| 423 | * if permission had not already been granted. |
| 424 | * Success or failure is returned via the {@link android.app.PendingIntent} pi. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 425 | * If successful, this grants the caller permission to access the device only |
| 426 | * until the device is disconnected. |
| 427 | * |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 428 | * The following extras will be added to pi: |
| 429 | * <ul> |
| 430 | * <li> {@link #EXTRA_DEVICE} containing the device passed into this call |
| 431 | * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether |
| 432 | * permission was granted by the user |
| 433 | * </ul> |
| 434 | * |
| 435 | * @param device to request permissions for |
| 436 | * @param pi PendingIntent for returning result |
| 437 | */ |
| 438 | public void requestPermission(UsbDevice device, PendingIntent pi) { |
| 439 | try { |
| 440 | mService.requestDevicePermission(device, mContext.getPackageName(), pi); |
| 441 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 442 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 447 | * Requests temporary permission for the given package to access the accessory. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 448 | * This may result in a system dialog being displayed to the user |
| 449 | * if permission had not already been granted. |
| 450 | * Success or failure is returned via the {@link android.app.PendingIntent} pi. |
Mike Lockwood | 62cfeeb | 2011-03-11 18:39:03 -0500 | [diff] [blame] | 451 | * If successful, this grants the caller permission to access the accessory only |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 452 | * until the device is disconnected. |
| 453 | * |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 454 | * The following extras will be added to pi: |
| 455 | * <ul> |
| 456 | * <li> {@link #EXTRA_ACCESSORY} containing the accessory passed into this call |
| 457 | * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether |
| 458 | * permission was granted by the user |
| 459 | * </ul> |
| 460 | * |
| 461 | * @param accessory to request permissions for |
| 462 | * @param pi PendingIntent for returning result |
| 463 | */ |
| 464 | public void requestPermission(UsbAccessory accessory, PendingIntent pi) { |
| 465 | try { |
| 466 | mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi); |
| 467 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 468 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 472 | /** |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 473 | * Grants permission for USB device without showing system dialog. |
| 474 | * Only system components can call this function. |
| 475 | * @param device to request permissions for |
| 476 | * |
| 477 | * {@hide} |
| 478 | */ |
| 479 | public void grantPermission(UsbDevice device) { |
| 480 | try { |
| 481 | mService.grantDevicePermission(device, Process.myUid()); |
| 482 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 483 | throw e.rethrowFromSystemServer(); |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 488 | * Grants permission to specified package for USB device without showing system dialog. |
| 489 | * Only system components can call this function, as it requires the MANAGE_USB permission. |
| 490 | * @param device to request permissions for |
| 491 | * @param packageName of package to grant permissions |
| 492 | * |
| 493 | * {@hide} |
| 494 | */ |
| 495 | public void grantPermission(UsbDevice device, String packageName) { |
| 496 | try { |
| 497 | int uid = mContext.getPackageManager() |
| 498 | .getPackageUidAsUser(packageName, mContext.getUserId()); |
| 499 | mService.grantDevicePermission(device, uid); |
| 500 | } catch (NameNotFoundException e) { |
| 501 | Log.e(TAG, "Package " + packageName + " not found.", e); |
| 502 | } catch (RemoteException e) { |
| 503 | throw e.rethrowFromSystemServer(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 508 | * Returns true if the specified USB function is currently enabled when in device mode. |
| 509 | * <p> |
| 510 | * USB functions represent interfaces which are published to the host to access |
| 511 | * services offered by the device. |
| 512 | * </p> |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 513 | * |
Nick Kralevich | fcf10f7 | 2015-05-13 11:54:03 -0700 | [diff] [blame] | 514 | * @param function name of the USB function |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 515 | * @return true if the USB function is enabled |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 516 | * |
| 517 | * {@hide} |
| 518 | */ |
Nick Kralevich | fcf10f7 | 2015-05-13 11:54:03 -0700 | [diff] [blame] | 519 | public boolean isFunctionEnabled(String function) { |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 520 | try { |
| 521 | return mService.isFunctionEnabled(function); |
| 522 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 523 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 524 | } |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 528 | * Sets the current USB function when in device mode. |
| 529 | * <p> |
| 530 | * USB functions represent interfaces which are published to the host to access |
| 531 | * services offered by the device. |
| 532 | * </p><p> |
| 533 | * This method is intended to select among primary USB functions. The system may |
| 534 | * automatically activate additional functions such as {@link #USB_FUNCTION_ADB} |
| 535 | * or {@link #USB_FUNCTION_ACCESSORY} based on other settings and states. |
| 536 | * </p><p> |
| 537 | * The allowed values are: {@link #USB_FUNCTION_NONE}, {@link #USB_FUNCTION_AUDIO_SOURCE}, |
| 538 | * {@link #USB_FUNCTION_MIDI}, {@link #USB_FUNCTION_MTP}, {@link #USB_FUNCTION_PTP}, |
| 539 | * or {@link #USB_FUNCTION_RNDIS}. |
| 540 | * </p><p> |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 541 | * Also sets whether USB data (for example, MTP exposed pictures) should be made available |
| 542 | * on the USB connection when in device mode. Unlocking usb data should only be done with |
| 543 | * user involvement, since exposing pictures or other data could leak sensitive |
| 544 | * user information. |
| 545 | * </p><p> |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 546 | * Note: This function is asynchronous and may fail silently without applying |
| 547 | * the requested changes. |
| 548 | * </p> |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 549 | * |
Mike Lockwood | 875c24b | 2011-07-18 10:54:32 -0700 | [diff] [blame] | 550 | * @param function name of the USB function, or null to restore the default function |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 551 | * @param usbDataUnlocked whether user data is accessible |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 552 | * |
| 553 | * {@hide} |
| 554 | */ |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 555 | public void setCurrentFunction(String function, boolean usbDataUnlocked) { |
Mike Lockwood | 08bff3b | 2010-08-31 13:27:05 -0400 | [diff] [blame] | 556 | try { |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 557 | mService.setCurrentFunction(function, usbDataUnlocked); |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 558 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 559 | throw e.rethrowFromSystemServer(); |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | /** |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 564 | * Returns a list of physical USB ports on the device. |
| 565 | * <p> |
| 566 | * This list is guaranteed to contain all dual-role USB Type C ports but it might |
| 567 | * be missing other ports depending on whether the kernel USB drivers have been |
| 568 | * updated to publish all of the device's ports through the new "dual_role_usb" |
| 569 | * device class (which supports all types of ports despite its name). |
| 570 | * </p> |
| 571 | * |
| 572 | * @return The list of USB ports, or null if none. |
| 573 | * |
| 574 | * @hide |
| 575 | */ |
| 576 | public UsbPort[] getPorts() { |
| 577 | try { |
| 578 | return mService.getPorts(); |
| 579 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 580 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 581 | } |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Gets the status of the specified USB port. |
| 586 | * |
| 587 | * @param port The port to query. |
| 588 | * @return The status of the specified USB port, or null if unknown. |
| 589 | * |
| 590 | * @hide |
| 591 | */ |
| 592 | public UsbPortStatus getPortStatus(UsbPort port) { |
| 593 | Preconditions.checkNotNull(port, "port must not be null"); |
| 594 | |
| 595 | try { |
| 596 | return mService.getPortStatus(port.getId()); |
| 597 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 598 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 599 | } |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Sets the desired role combination of the port. |
| 604 | * <p> |
| 605 | * The supported role combinations depend on what is connected to the port and may be |
| 606 | * determined by consulting |
| 607 | * {@link UsbPortStatus#isRoleCombinationSupported UsbPortStatus.isRoleCombinationSupported}. |
| 608 | * </p><p> |
| 609 | * Note: This function is asynchronous and may fail silently without applying |
| 610 | * the requested changes. If this function does cause a status change to occur then |
| 611 | * a {@link #ACTION_USB_PORT_CHANGED} broadcast will be sent. |
| 612 | * </p> |
| 613 | * |
| 614 | * @param powerRole The desired power role: {@link UsbPort#POWER_ROLE_SOURCE} |
| 615 | * or {@link UsbPort#POWER_ROLE_SINK}, or 0 if no power role. |
| 616 | * @param dataRole The desired data role: {@link UsbPort#DATA_ROLE_HOST} |
| 617 | * or {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if no data role. |
| 618 | * |
| 619 | * @hide |
| 620 | */ |
| 621 | public void setPortRoles(UsbPort port, int powerRole, int dataRole) { |
| 622 | Preconditions.checkNotNull(port, "port must not be null"); |
| 623 | UsbPort.checkRoles(powerRole, dataRole); |
| 624 | |
| 625 | try { |
| 626 | mService.setPortRoles(port.getId(), powerRole, dataRole); |
| 627 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 628 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 632 | /** @hide */ |
| 633 | public static String addFunction(String functions, String function) { |
Yasuhiro Matsuda | 48b9a7c | 2015-07-16 19:00:16 +0900 | [diff] [blame] | 634 | if (USB_FUNCTION_NONE.equals(functions)) { |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 635 | return function; |
| 636 | } |
| 637 | if (!containsFunction(functions, function)) { |
| 638 | if (functions.length() > 0) { |
| 639 | functions += ","; |
| 640 | } |
| 641 | functions += function; |
| 642 | } |
| 643 | return functions; |
| 644 | } |
| 645 | |
| 646 | /** @hide */ |
| 647 | public static String removeFunction(String functions, String function) { |
| 648 | String[] split = functions.split(","); |
| 649 | for (int i = 0; i < split.length; i++) { |
| 650 | if (function.equals(split[i])) { |
| 651 | split[i] = null; |
| 652 | } |
| 653 | } |
| 654 | if (split.length == 1 && split[0] == null) { |
Yasuhiro Matsuda | 48b9a7c | 2015-07-16 19:00:16 +0900 | [diff] [blame] | 655 | return USB_FUNCTION_NONE; |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 656 | } |
| 657 | StringBuilder builder = new StringBuilder(); |
| 658 | for (int i = 0; i < split.length; i++) { |
| 659 | String s = split[i]; |
| 660 | if (s != null) { |
| 661 | if (builder.length() > 0) { |
| 662 | builder.append(","); |
| 663 | } |
| 664 | builder.append(s); |
| 665 | } |
| 666 | } |
| 667 | return builder.toString(); |
| 668 | } |
| 669 | |
| 670 | /** @hide */ |
| 671 | public static boolean containsFunction(String functions, String function) { |
| 672 | int index = functions.indexOf(function); |
| 673 | if (index < 0) return false; |
| 674 | if (index > 0 && functions.charAt(index - 1) != ',') return false; |
| 675 | int charAfter = index + function.length(); |
| 676 | if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false; |
| 677 | return true; |
| 678 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 679 | } |