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