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 | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 22 | import android.annotation.RequiresFeature; |
Philip P. Moltmann | f3189e6 | 2017-08-16 16:04:24 -0700 | [diff] [blame] | 23 | import android.annotation.RequiresPermission; |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 24 | import android.annotation.SdkConstant; |
| 25 | import android.annotation.SdkConstant.SdkConstantType; |
Philip P. Moltmann | f3189e6 | 2017-08-16 16:04:24 -0700 | [diff] [blame] | 26 | import android.annotation.SystemApi; |
| 27 | import android.annotation.SystemService; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 28 | import android.app.PendingIntent; |
Keun-young Park | badbbae | 2016-01-05 13:27:29 -0800 | [diff] [blame] | 29 | import android.content.ComponentName; |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 30 | import android.content.Context; |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 32 | import android.content.pm.PackageManager.NameNotFoundException; |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 33 | import android.hardware.usb.gadget.V1_0.GadgetFunction; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 36 | import android.os.Process; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 37 | import android.os.RemoteException; |
| 38 | import android.util.Log; |
| 39 | |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 40 | import com.android.internal.util.Preconditions; |
| 41 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 42 | import java.util.HashMap; |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 43 | import java.util.Map; |
| 44 | import java.util.StringJoiner; |
Mike Lockwood | 08bff3b | 2010-08-31 13:27:05 -0400 | [diff] [blame] | 45 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 46 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 47 | * This class allows you to access the state of USB and communicate with USB devices. |
| 48 | * Currently only host mode is supported in the public API. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 49 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 50 | * <div class="special reference"> |
| 51 | * <h3>Developer Guides</h3> |
| 52 | * <p>For more information about communicating with USB hardware, read the |
Mark Lu | 8b73990 | 2016-06-24 15:38:02 -0700 | [diff] [blame] | 53 | * <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] | 54 | * </div> |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 55 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 56 | @SystemService(Context.USB_SERVICE) |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 57 | public class UsbManager { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 58 | private static final String TAG = "UsbManager"; |
| 59 | |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 60 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 61 | * 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] | 62 | * |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 63 | * 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] | 64 | * <ul> |
| 65 | * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. |
Daichi Hirono | 618fa6f | 2016-03-17 14:14:32 +0900 | [diff] [blame] | 66 | * <li> {@link #USB_HOST_CONNECTED} boolean indicating whether USB is connected or |
| 67 | * disconnected as host. |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 68 | * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 69 | * currently zero if not configured, one for configured. |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 70 | * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the |
| 71 | * adb function is enabled |
| 72 | * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the |
| 73 | * RNDIS ethernet function is enabled |
| 74 | * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the |
| 75 | * MTP function is enabled |
| 76 | * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the |
| 77 | * PTP function is enabled |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 78 | * <li> {@link #USB_FUNCTION_ACCESSORY} boolean extra indicating whether the |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 79 | * accessory function is enabled |
Mike Lockwood | 9d5a4be | 2012-04-06 09:41:32 -0700 | [diff] [blame] | 80 | * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the |
| 81 | * audio source function is enabled |
Mike Lockwood | 2a57bc7 | 2014-09-19 11:16:52 -0700 | [diff] [blame] | 82 | * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the |
| 83 | * MIDI function is enabled |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 84 | * </ul> |
Yasuhiro Matsuda | 48b9a7c | 2015-07-16 19:00:16 +0900 | [diff] [blame] | 85 | * If the sticky intent has not been found, that indicates USB is disconnected, |
| 86 | * 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] | 87 | * |
| 88 | * {@hide} |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 89 | */ |
| 90 | public static final String ACTION_USB_STATE = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 91 | "android.hardware.usb.action.USB_STATE"; |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 92 | |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Broadcast Action: A broadcast for USB port changes. |
| 95 | * |
| 96 | * This intent is sent when a USB port is added, removed, or changes state. |
| 97 | * <ul> |
| 98 | * <li> {@link #EXTRA_PORT} containing the {@link android.hardware.usb.UsbPort} |
| 99 | * for the port. |
| 100 | * <li> {@link #EXTRA_PORT_STATUS} containing the {@link android.hardware.usb.UsbPortStatus} |
| 101 | * for the port, or null if the port has been removed |
| 102 | * </ul> |
| 103 | * |
| 104 | * @hide |
| 105 | */ |
| 106 | public static final String ACTION_USB_PORT_CHANGED = |
| 107 | "android.hardware.usb.action.USB_PORT_CHANGED"; |
| 108 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 109 | /** |
Andrew Solovay | 6534291 | 2017-10-27 13:02:34 -0700 | [diff] [blame] | 110 | * Activity intent sent when user attaches a USB device. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 111 | * |
| 112 | * 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] | 113 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 114 | * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 115 | * for the attached device |
| 116 | * </ul> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 117 | */ |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 118 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 119 | public static final String ACTION_USB_DEVICE_ATTACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 120 | "android.hardware.usb.action.USB_DEVICE_ATTACHED"; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Broadcast Action: A broadcast for USB device detached event. |
| 124 | * |
| 125 | * 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] | 126 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 127 | * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} |
Mike Lockwood | 188d00b | 2011-02-23 13:14:33 -0800 | [diff] [blame] | 128 | * for the detached device |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 129 | * </ul> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 130 | */ |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 131 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 132 | public static final String ACTION_USB_DEVICE_DETACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 133 | "android.hardware.usb.action.USB_DEVICE_DETACHED"; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 134 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 135 | /** |
Andrew Solovay | 6534291 | 2017-10-27 13:02:34 -0700 | [diff] [blame] | 136 | * Activity intent sent when user attaches a USB accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 137 | * |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 138 | * <ul> |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 139 | * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 140 | * for the attached accessory |
| 141 | * </ul> |
| 142 | */ |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 143 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 144 | public static final String ACTION_USB_ACCESSORY_ATTACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 145 | "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Broadcast Action: A broadcast for USB accessory detached event. |
| 149 | * |
| 150 | * This intent is sent when a USB accessory is detached. |
| 151 | * <ul> |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 152 | * <li> {@link #EXTRA_ACCESSORY} containing the {@link UsbAccessory} |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 153 | * for the attached accessory that was detached |
| 154 | * </ul> |
| 155 | */ |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 156 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 157 | public static final String ACTION_USB_ACCESSORY_DETACHED = |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 158 | "android.hardware.usb.action.USB_ACCESSORY_DETACHED"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 159 | |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 160 | /** |
| 161 | * Boolean extra indicating whether USB is connected or disconnected. |
| 162 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 163 | * |
| 164 | * {@hide} |
Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 165 | */ |
| 166 | public static final String USB_CONNECTED = "connected"; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 167 | |
| 168 | /** |
Daichi Hirono | 618fa6f | 2016-03-17 14:14:32 +0900 | [diff] [blame] | 169 | * Boolean extra indicating whether USB is connected or disconnected as host. |
| 170 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
| 171 | * |
| 172 | * {@hide} |
| 173 | */ |
| 174 | public static final String USB_HOST_CONNECTED = "host_connected"; |
| 175 | |
| 176 | /** |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 177 | * Boolean extra indicating whether USB is configured. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 178 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 179 | * |
| 180 | * {@hide} |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 181 | */ |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 182 | public static final String USB_CONFIGURED = "configured"; |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 185 | * Boolean extra indicating whether confidential user data, such as photos, should be |
| 186 | * made available on the USB connection. This variable will only be set when the user |
| 187 | * has explicitly asked for this data to be unlocked. |
| 188 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast. |
| 189 | * |
| 190 | * {@hide} |
| 191 | */ |
| 192 | public static final String USB_DATA_UNLOCKED = "unlocked"; |
| 193 | |
| 194 | /** |
Jerry Zhang | bb598ee | 2016-10-24 14:35:08 -0700 | [diff] [blame] | 195 | * Boolean extra indicating whether the intent represents a change in the usb |
| 196 | * configuration (as opposed to a state update). |
| 197 | * |
| 198 | * {@hide} |
| 199 | */ |
| 200 | public static final String USB_CONFIG_CHANGED = "config_changed"; |
| 201 | |
| 202 | /** |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 203 | * A placeholder indicating that no USB function is being specified. |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 204 | * Used for compatibility with old init scripts to indicate no functions vs. charging function. |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 205 | * |
| 206 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 207 | */ |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 208 | public static final String USB_FUNCTION_NONE = "none"; |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Name of the adb USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 212 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 213 | * |
| 214 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 215 | */ |
| 216 | public static final String USB_FUNCTION_ADB = "adb"; |
| 217 | |
| 218 | /** |
| 219 | * Name of the RNDIS ethernet USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 220 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 221 | * |
| 222 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 223 | */ |
| 224 | public static final String USB_FUNCTION_RNDIS = "rndis"; |
| 225 | |
| 226 | /** |
| 227 | * Name of the MTP USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 228 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 229 | * |
| 230 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 231 | */ |
| 232 | public static final String USB_FUNCTION_MTP = "mtp"; |
| 233 | |
| 234 | /** |
Mike Lockwood | 9eb014a | 2011-06-08 09:17:45 -0700 | [diff] [blame] | 235 | * Name of the PTP USB function. |
| 236 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 237 | * |
| 238 | * {@hide} |
| 239 | */ |
| 240 | public static final String USB_FUNCTION_PTP = "ptp"; |
| 241 | |
| 242 | /** |
Mike Lockwood | 9d5a4be | 2012-04-06 09:41:32 -0700 | [diff] [blame] | 243 | * Name of the audio source USB function. |
| 244 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 245 | * |
| 246 | * {@hide} |
| 247 | */ |
| 248 | public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; |
| 249 | |
| 250 | /** |
Mike Lockwood | 2a57bc7 | 2014-09-19 11:16:52 -0700 | [diff] [blame] | 251 | * Name of the MIDI USB function. |
| 252 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
| 253 | * |
| 254 | * {@hide} |
| 255 | */ |
| 256 | public static final String USB_FUNCTION_MIDI = "midi"; |
| 257 | |
| 258 | /** |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 259 | * Name of the Accessory USB function. |
Mike Lockwood | b92df0f | 2010-12-10 16:19:32 -0800 | [diff] [blame] | 260 | * Used in extras for the {@link #ACTION_USB_STATE} broadcast |
Mike Lockwood | a75075e1 | 2011-03-11 11:26:11 -0500 | [diff] [blame] | 261 | * |
| 262 | * {@hide} |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 263 | */ |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 264 | public static final String USB_FUNCTION_ACCESSORY = "accessory"; |
| 265 | |
| 266 | /** |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 267 | * Name of extra for {@link #ACTION_USB_PORT_CHANGED} |
| 268 | * containing the {@link UsbPort} object for the port. |
| 269 | * |
| 270 | * @hide |
| 271 | */ |
| 272 | public static final String EXTRA_PORT = "port"; |
| 273 | |
| 274 | /** |
| 275 | * Name of extra for {@link #ACTION_USB_PORT_CHANGED} |
| 276 | * containing the {@link UsbPortStatus} object for the port, or null if the port |
| 277 | * was removed. |
| 278 | * |
| 279 | * @hide |
| 280 | */ |
| 281 | public static final String EXTRA_PORT_STATUS = "portStatus"; |
| 282 | |
| 283 | /** |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 284 | * Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 285 | * {@link #ACTION_USB_DEVICE_DETACHED} broadcasts |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 286 | * containing the {@link UsbDevice} object for the device. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 287 | */ |
| 288 | public static final String EXTRA_DEVICE = "device"; |
| 289 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 290 | /** |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 291 | * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and |
| 292 | * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 293 | * containing the {@link UsbAccessory} object for the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 294 | */ |
| 295 | public static final String EXTRA_ACCESSORY = "accessory"; |
| 296 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 297 | /** |
| 298 | * Name of extra added to the {@link android.app.PendingIntent} |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 299 | * passed into {@link #requestPermission(UsbDevice, PendingIntent)} |
| 300 | * or {@link #requestPermission(UsbAccessory, PendingIntent)} |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 301 | * containing a boolean value indicating whether the user granted permission or not. |
| 302 | */ |
| 303 | public static final String EXTRA_PERMISSION_GRANTED = "permission"; |
| 304 | |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 305 | /** |
| 306 | * Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)} |
| 307 | * {@hide} |
| 308 | */ |
| 309 | public static final long FUNCTION_NONE = 0; |
| 310 | |
| 311 | /** |
| 312 | * Code for the mtp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} |
| 313 | * {@hide} |
| 314 | */ |
| 315 | public static final long FUNCTION_MTP = GadgetFunction.MTP; |
| 316 | |
| 317 | /** |
| 318 | * Code for the ptp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} |
| 319 | * {@hide} |
| 320 | */ |
| 321 | public static final long FUNCTION_PTP = GadgetFunction.PTP; |
| 322 | |
| 323 | /** |
| 324 | * Code for the rndis usb function. Passed as a mask into {@link #setCurrentFunctions(long)} |
| 325 | * {@hide} |
| 326 | */ |
| 327 | public static final long FUNCTION_RNDIS = GadgetFunction.RNDIS; |
| 328 | |
| 329 | /** |
| 330 | * Code for the midi usb function. Passed as a mask into {@link #setCurrentFunctions(long)} |
| 331 | * {@hide} |
| 332 | */ |
| 333 | public static final long FUNCTION_MIDI = GadgetFunction.MIDI; |
| 334 | |
| 335 | /** |
| 336 | * Code for the accessory usb function. |
| 337 | * {@hide} |
| 338 | */ |
| 339 | public static final long FUNCTION_ACCESSORY = GadgetFunction.ACCESSORY; |
| 340 | |
| 341 | /** |
| 342 | * Code for the audio source usb function. |
| 343 | * {@hide} |
| 344 | */ |
| 345 | public static final long FUNCTION_AUDIO_SOURCE = GadgetFunction.AUDIO_SOURCE; |
| 346 | |
| 347 | /** |
| 348 | * Code for the adb usb function. |
| 349 | * {@hide} |
| 350 | */ |
| 351 | public static final long FUNCTION_ADB = GadgetFunction.ADB; |
| 352 | |
| 353 | private static final long SETTABLE_FUNCTIONS = FUNCTION_MTP | FUNCTION_PTP | FUNCTION_RNDIS |
| 354 | | FUNCTION_MIDI; |
| 355 | |
| 356 | private static final Map<String, Long> FUNCTION_NAME_TO_CODE = new HashMap<>(); |
| 357 | |
| 358 | static { |
| 359 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_MTP, FUNCTION_MTP); |
| 360 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_PTP, FUNCTION_PTP); |
| 361 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_RNDIS, FUNCTION_RNDIS); |
| 362 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_MIDI, FUNCTION_MIDI); |
| 363 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_ACCESSORY, FUNCTION_ACCESSORY); |
| 364 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_AUDIO_SOURCE, FUNCTION_AUDIO_SOURCE); |
| 365 | FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_ADB, FUNCTION_ADB); |
| 366 | } |
| 367 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 368 | private final Context mContext; |
| 369 | private final IUsbManager mService; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * {@hide} |
| 373 | */ |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 374 | public UsbManager(Context context, IUsbManager service) { |
| 375 | mContext = context; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 376 | mService = service; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Returns a HashMap containing all USB devices currently attached. |
| 381 | * USB device name is the key for the returned HashMap. |
| 382 | * The result will be empty if no devices are attached, or if |
| 383 | * USB host mode is inactive or unsupported. |
| 384 | * |
| 385 | * @return HashMap containing all connected USB devices. |
| 386 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 387 | @RequiresFeature(PackageManager.FEATURE_USB_HOST) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 388 | public HashMap<String,UsbDevice> getDeviceList() { |
Jim Kaye | 9b5ee82 | 2017-02-15 14:28:23 -0800 | [diff] [blame] | 389 | HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>(); |
| 390 | if (mService == null) { |
| 391 | return result; |
| 392 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 393 | Bundle bundle = new Bundle(); |
| 394 | try { |
| 395 | mService.getDeviceList(bundle); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 396 | for (String name : bundle.keySet()) { |
| 397 | result.put(name, (UsbDevice)bundle.get(name)); |
| 398 | } |
| 399 | return result; |
| 400 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 401 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Opens the device so it can be used to send and receive |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 407 | * data using {@link android.hardware.usb.UsbRequest}. |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 408 | * |
| 409 | * @param device the device to open |
mike wakerly | 1567a43 | 2012-07-17 19:58:19 -0700 | [diff] [blame] | 410 | * @return a {@link UsbDeviceConnection}, or {@code null} if open failed |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 411 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 412 | @RequiresFeature(PackageManager.FEATURE_USB_HOST) |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 413 | public UsbDeviceConnection openDevice(UsbDevice device) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 414 | try { |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 415 | String deviceName = device.getDeviceName(); |
Emilian Peev | c8ed5ec | 2017-09-25 13:03:08 +0100 | [diff] [blame] | 416 | ParcelFileDescriptor pfd = mService.openDevice(deviceName, mContext.getPackageName()); |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 417 | if (pfd != null) { |
| 418 | UsbDeviceConnection connection = new UsbDeviceConnection(device); |
Philip P. Moltmann | ec3cbb2 | 2016-09-14 13:24:52 -0700 | [diff] [blame] | 419 | boolean result = connection.open(deviceName, pfd, mContext); |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 420 | pfd.close(); |
| 421 | if (result) { |
| 422 | return connection; |
| 423 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 424 | } |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 425 | } catch (Exception e) { |
| 426 | Log.e(TAG, "exception in UsbManager.openDevice", e); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 427 | } |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 428 | return null; |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 431 | /** |
| 432 | * Returns a list of currently attached USB accessories. |
| 433 | * (in the current implementation there can be at most one) |
| 434 | * |
| 435 | * @return list of USB accessories, or null if none are attached. |
| 436 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 437 | @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 438 | public UsbAccessory[] getAccessoryList() { |
Jim Kaye | 9b5ee82 | 2017-02-15 14:28:23 -0800 | [diff] [blame] | 439 | if (mService == null) { |
| 440 | return null; |
| 441 | } |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 442 | try { |
| 443 | UsbAccessory accessory = mService.getCurrentAccessory(); |
| 444 | if (accessory == null) { |
| 445 | return null; |
| 446 | } else { |
| 447 | return new UsbAccessory[] { accessory }; |
| 448 | } |
| 449 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 450 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Opens a file descriptor for reading and writing data to the USB accessory. |
| 456 | * |
Philip P. Moltmann | 84e0479 | 2018-02-16 16:37:07 -0800 | [diff] [blame] | 457 | * <p>If data is read from the {@link java.io.InputStream} created from this file descriptor all |
| 458 | * data of a USB transfer should be read at once. If only a partial request is read the rest of |
| 459 | * the transfer is dropped. |
| 460 | * |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 461 | * @param accessory the USB accessory to open |
Philip P. Moltmann | 84e0479 | 2018-02-16 16:37:07 -0800 | [diff] [blame] | 462 | * @return file descriptor, or null if the accessory could not be opened. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 463 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 464 | @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 465 | public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { |
| 466 | try { |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 467 | return mService.openAccessory(accessory); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 468 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 469 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 473 | /** |
| 474 | * Returns true if the caller has permission to access the device. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 475 | * Permission might have been granted temporarily via |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 476 | * {@link #requestPermission(UsbDevice, PendingIntent)} or |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 477 | * by the user choosing the caller as the default application for the device. |
Emilian Peev | c8ed5ec | 2017-09-25 13:03:08 +0100 | [diff] [blame] | 478 | * Permission for USB devices of class {@link UsbConstants#USB_CLASS_VIDEO} for clients that |
| 479 | * target SDK {@link android.os.Build.VERSION_CODES#P} and above can be granted only if they |
| 480 | * have additionally the {@link android.Manifest.permission#CAMERA} permission. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 481 | * |
| 482 | * @param device to check permissions for |
| 483 | * @return true if caller has permission |
| 484 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 485 | @RequiresFeature(PackageManager.FEATURE_USB_HOST) |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 486 | public boolean hasPermission(UsbDevice device) { |
Jim Kaye | 9b5ee82 | 2017-02-15 14:28:23 -0800 | [diff] [blame] | 487 | if (mService == null) { |
| 488 | return false; |
| 489 | } |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 490 | try { |
Emilian Peev | c8ed5ec | 2017-09-25 13:03:08 +0100 | [diff] [blame] | 491 | return mService.hasDevicePermission(device, mContext.getPackageName()); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 492 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 493 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Returns true if the caller has permission to access the accessory. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 499 | * Permission might have been granted temporarily via |
Mike Lockwood | 980f043 | 2011-03-09 15:49:13 -0500 | [diff] [blame] | 500 | * {@link #requestPermission(UsbAccessory, PendingIntent)} or |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 501 | * 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] | 502 | * |
| 503 | * @param accessory to check permissions for |
| 504 | * @return true if caller has permission |
| 505 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 506 | @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 507 | public boolean hasPermission(UsbAccessory accessory) { |
Jim Kaye | 9b5ee82 | 2017-02-15 14:28:23 -0800 | [diff] [blame] | 508 | if (mService == null) { |
| 509 | return false; |
| 510 | } |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 511 | try { |
| 512 | return mService.hasAccessoryPermission(accessory); |
| 513 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 514 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 519 | * Requests temporary permission for the given package to access the device. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 520 | * This may result in a system dialog being displayed to the user |
| 521 | * if permission had not already been granted. |
| 522 | * Success or failure is returned via the {@link android.app.PendingIntent} pi. |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 523 | * If successful, this grants the caller permission to access the device only |
| 524 | * until the device is disconnected. |
| 525 | * |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 526 | * The following extras will be added to pi: |
| 527 | * <ul> |
| 528 | * <li> {@link #EXTRA_DEVICE} containing the device passed into this call |
| 529 | * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether |
| 530 | * permission was granted by the user |
| 531 | * </ul> |
| 532 | * |
Emilian Peev | c8ed5ec | 2017-09-25 13:03:08 +0100 | [diff] [blame] | 533 | * Permission for USB devices of class {@link UsbConstants#USB_CLASS_VIDEO} for clients that |
| 534 | * target SDK {@link android.os.Build.VERSION_CODES#P} and above can be granted only if they |
| 535 | * have additionally the {@link android.Manifest.permission#CAMERA} permission. |
| 536 | * |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 537 | * @param device to request permissions for |
| 538 | * @param pi PendingIntent for returning result |
| 539 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 540 | @RequiresFeature(PackageManager.FEATURE_USB_HOST) |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 541 | public void requestPermission(UsbDevice device, PendingIntent pi) { |
| 542 | try { |
| 543 | mService.requestDevicePermission(device, mContext.getPackageName(), pi); |
| 544 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 545 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | /** |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 550 | * Requests temporary permission for the given package to access the accessory. |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 551 | * This may result in a system dialog being displayed to the user |
| 552 | * if permission had not already been granted. |
| 553 | * Success or failure is returned via the {@link android.app.PendingIntent} pi. |
Mike Lockwood | 62cfeeb | 2011-03-11 18:39:03 -0500 | [diff] [blame] | 554 | * If successful, this grants the caller permission to access the accessory only |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 555 | * until the device is disconnected. |
| 556 | * |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 557 | * The following extras will be added to pi: |
| 558 | * <ul> |
| 559 | * <li> {@link #EXTRA_ACCESSORY} containing the accessory passed into this call |
| 560 | * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether |
| 561 | * permission was granted by the user |
| 562 | * </ul> |
| 563 | * |
| 564 | * @param accessory to request permissions for |
| 565 | * @param pi PendingIntent for returning result |
| 566 | */ |
Philip P. Moltmann | 00d2de9 | 2018-02-16 11:14:54 -0800 | [diff] [blame] | 567 | @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY) |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 568 | public void requestPermission(UsbAccessory accessory, PendingIntent pi) { |
| 569 | try { |
| 570 | mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi); |
| 571 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 572 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 576 | /** |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 577 | * Grants permission for USB device without showing system dialog. |
| 578 | * Only system components can call this function. |
| 579 | * @param device to request permissions for |
| 580 | * |
| 581 | * {@hide} |
| 582 | */ |
| 583 | public void grantPermission(UsbDevice device) { |
Keun-young Park | badbbae | 2016-01-05 13:27:29 -0800 | [diff] [blame] | 584 | grantPermission(device, Process.myUid()); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Grants permission for USB device to given uid without showing system dialog. |
| 589 | * Only system components can call this function. |
| 590 | * @param device to request permissions for |
| 591 | * @uid uid to give permission |
| 592 | * |
| 593 | * {@hide} |
| 594 | */ |
| 595 | public void grantPermission(UsbDevice device, int uid) { |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 596 | try { |
Keun-young Park | badbbae | 2016-01-05 13:27:29 -0800 | [diff] [blame] | 597 | mService.grantDevicePermission(device, uid); |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 598 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 599 | throw e.rethrowFromSystemServer(); |
Daichi Hirono | 4751880 | 2015-12-08 09:51:19 +0900 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 604 | * Grants permission to specified package for USB device without showing system dialog. |
| 605 | * Only system components can call this function, as it requires the MANAGE_USB permission. |
| 606 | * @param device to request permissions for |
| 607 | * @param packageName of package to grant permissions |
| 608 | * |
| 609 | * {@hide} |
| 610 | */ |
Philip P. Moltmann | f3189e6 | 2017-08-16 16:04:24 -0700 | [diff] [blame] | 611 | @SystemApi |
| 612 | @RequiresPermission(Manifest.permission.MANAGE_USB) |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 613 | public void grantPermission(UsbDevice device, String packageName) { |
| 614 | try { |
| 615 | int uid = mContext.getPackageManager() |
| 616 | .getPackageUidAsUser(packageName, mContext.getUserId()); |
Keun-young Park | badbbae | 2016-01-05 13:27:29 -0800 | [diff] [blame] | 617 | grantPermission(device, uid); |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 618 | } catch (NameNotFoundException e) { |
| 619 | Log.e(TAG, "Package " + packageName + " not found.", e); |
Johan T. Halseth | 244259a | 2016-05-24 14:49:04 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 624 | * Returns true if the specified USB function is currently enabled when in device mode. |
| 625 | * <p> |
| 626 | * USB functions represent interfaces which are published to the host to access |
| 627 | * services offered by the device. |
| 628 | * </p> |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 629 | * |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 630 | * @deprecated use getCurrentFunctions() instead. |
Nick Kralevich | fcf10f7 | 2015-05-13 11:54:03 -0700 | [diff] [blame] | 631 | * @param function name of the USB function |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 632 | * @return true if the USB function is enabled |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 633 | * |
| 634 | * {@hide} |
| 635 | */ |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 636 | @Deprecated |
Nick Kralevich | fcf10f7 | 2015-05-13 11:54:03 -0700 | [diff] [blame] | 637 | public boolean isFunctionEnabled(String function) { |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 638 | try { |
| 639 | return mService.isFunctionEnabled(function); |
| 640 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 641 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 642 | } |
Mike Lockwood | e51099f | 2011-08-02 12:54:49 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 646 | * Sets the current USB functions when in device mode. |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 647 | * <p> |
| 648 | * USB functions represent interfaces which are published to the host to access |
| 649 | * services offered by the device. |
| 650 | * </p><p> |
| 651 | * This method is intended to select among primary USB functions. The system may |
| 652 | * automatically activate additional functions such as {@link #USB_FUNCTION_ADB} |
| 653 | * or {@link #USB_FUNCTION_ACCESSORY} based on other settings and states. |
| 654 | * </p><p> |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 655 | * An argument of 0 indicates that the device is charging, and can pick any |
| 656 | * appropriate function for that purpose. |
Jerry Zhang | 7a396be | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 657 | * </p><p> |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 658 | * Note: This function is asynchronous and may fail silently without applying |
| 659 | * the requested changes. |
| 660 | * </p> |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 661 | * |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 662 | * @param functions the USB function(s) to set, as a bitwise mask. |
| 663 | * Must satisfy {@link UsbManager#areSettableFunctions} |
Mike Lockwood | 02e4569 | 2011-06-14 15:43:51 -0400 | [diff] [blame] | 664 | * |
| 665 | * {@hide} |
| 666 | */ |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 667 | public void setCurrentFunctions(long functions) { |
Mike Lockwood | 08bff3b | 2010-08-31 13:27:05 -0400 | [diff] [blame] | 668 | try { |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 669 | mService.setCurrentFunctions(functions); |
| 670 | } catch (RemoteException e) { |
| 671 | throw e.rethrowFromSystemServer(); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Sets the current USB functions when in device mode. |
| 677 | * |
| 678 | * @deprecated use setCurrentFunctions(long) instead. |
| 679 | * @param functions the USB function(s) to set. |
| 680 | * @param usbDataUnlocked unused |
| 681 | |
| 682 | * {@hide} |
| 683 | */ |
| 684 | @Deprecated |
| 685 | public void setCurrentFunction(String functions, boolean usbDataUnlocked) { |
| 686 | try { |
| 687 | mService.setCurrentFunction(functions, usbDataUnlocked); |
| 688 | } catch (RemoteException e) { |
| 689 | throw e.rethrowFromSystemServer(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Returns the current USB functions in device mode. |
| 695 | * <p> |
| 696 | * This function returns the state of primary USB functions and can return a |
| 697 | * mask containing any usb function(s) except for ADB. |
| 698 | * </p> |
| 699 | * |
| 700 | * @return The currently enabled functions, in a bitwise mask. |
| 701 | * A zero mask indicates that the current function is the charging function. |
| 702 | * |
| 703 | * {@hide} |
| 704 | */ |
| 705 | public long getCurrentFunctions() { |
| 706 | try { |
| 707 | return mService.getCurrentFunctions(); |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 708 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 709 | throw e.rethrowFromSystemServer(); |
Nick Kralevich | 6740190 | 2015-06-10 09:38:42 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
Jerry Zhang | 30b9adf | 2017-11-30 19:10:55 -0800 | [diff] [blame] | 714 | * Sets the screen unlocked functions, which are persisted and set as the current functions |
| 715 | * whenever the screen is unlocked. |
| 716 | * <p> |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 717 | * A zero mask has the effect of switching off this feature, so functions |
Jerry Zhang | 30b9adf | 2017-11-30 19:10:55 -0800 | [diff] [blame] | 718 | * no longer change on screen unlock. |
| 719 | * </p><p> |
| 720 | * Note: When the screen is on, this method will apply given functions as current functions, |
| 721 | * which is asynchronous and may fail silently without applying the requested changes. |
| 722 | * </p> |
| 723 | * |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 724 | * @param functions functions to set, in a bitwise mask. |
| 725 | * Must satisfy {@link UsbManager#areSettableFunctions} |
Jerry Zhang | 30b9adf | 2017-11-30 19:10:55 -0800 | [diff] [blame] | 726 | * |
| 727 | * {@hide} |
| 728 | */ |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 729 | public void setScreenUnlockedFunctions(long functions) { |
Jerry Zhang | 30b9adf | 2017-11-30 19:10:55 -0800 | [diff] [blame] | 730 | try { |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 731 | mService.setScreenUnlockedFunctions(functions); |
| 732 | } catch (RemoteException e) { |
| 733 | throw e.rethrowFromSystemServer(); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Gets the current screen unlocked functions. |
| 739 | * |
| 740 | * @return The currently set screen enabled functions. |
| 741 | * A zero mask indicates that the screen unlocked functions feature is not enabled. |
| 742 | * |
| 743 | * {@hide} |
| 744 | */ |
| 745 | public long getScreenUnlockedFunctions() { |
| 746 | try { |
| 747 | return mService.getScreenUnlockedFunctions(); |
Jerry Zhang | 30b9adf | 2017-11-30 19:10:55 -0800 | [diff] [blame] | 748 | } catch (RemoteException e) { |
| 749 | throw e.rethrowFromSystemServer(); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 754 | * Returns a list of physical USB ports on the device. |
| 755 | * <p> |
| 756 | * This list is guaranteed to contain all dual-role USB Type C ports but it might |
| 757 | * be missing other ports depending on whether the kernel USB drivers have been |
| 758 | * updated to publish all of the device's ports through the new "dual_role_usb" |
| 759 | * device class (which supports all types of ports despite its name). |
| 760 | * </p> |
| 761 | * |
| 762 | * @return The list of USB ports, or null if none. |
| 763 | * |
| 764 | * @hide |
| 765 | */ |
| 766 | public UsbPort[] getPorts() { |
Jim Kaye | 9b5ee82 | 2017-02-15 14:28:23 -0800 | [diff] [blame] | 767 | if (mService == null) { |
| 768 | return null; |
| 769 | } |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 770 | try { |
| 771 | return mService.getPorts(); |
| 772 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 773 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 774 | } |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Gets the status of the specified USB port. |
| 779 | * |
| 780 | * @param port The port to query. |
| 781 | * @return The status of the specified USB port, or null if unknown. |
| 782 | * |
| 783 | * @hide |
| 784 | */ |
| 785 | public UsbPortStatus getPortStatus(UsbPort port) { |
| 786 | Preconditions.checkNotNull(port, "port must not be null"); |
| 787 | |
| 788 | try { |
| 789 | return mService.getPortStatus(port.getId()); |
| 790 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 791 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 792 | } |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Sets the desired role combination of the port. |
| 797 | * <p> |
| 798 | * The supported role combinations depend on what is connected to the port and may be |
| 799 | * determined by consulting |
| 800 | * {@link UsbPortStatus#isRoleCombinationSupported UsbPortStatus.isRoleCombinationSupported}. |
| 801 | * </p><p> |
| 802 | * Note: This function is asynchronous and may fail silently without applying |
| 803 | * the requested changes. If this function does cause a status change to occur then |
| 804 | * a {@link #ACTION_USB_PORT_CHANGED} broadcast will be sent. |
| 805 | * </p> |
| 806 | * |
| 807 | * @param powerRole The desired power role: {@link UsbPort#POWER_ROLE_SOURCE} |
| 808 | * or {@link UsbPort#POWER_ROLE_SINK}, or 0 if no power role. |
| 809 | * @param dataRole The desired data role: {@link UsbPort#DATA_ROLE_HOST} |
| 810 | * or {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if no data role. |
| 811 | * |
| 812 | * @hide |
| 813 | */ |
| 814 | public void setPortRoles(UsbPort port, int powerRole, int dataRole) { |
| 815 | Preconditions.checkNotNull(port, "port must not be null"); |
| 816 | UsbPort.checkRoles(powerRole, dataRole); |
| 817 | |
Badhri Jagan Sridharan | 4641fc3 | 2017-06-22 16:39:47 -0700 | [diff] [blame] | 818 | Log.d(TAG, "setPortRoles Package:" + mContext.getPackageName()); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 819 | try { |
| 820 | mService.setPortRoles(port.getId(), powerRole, dataRole); |
| 821 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 822 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 76c4c66 | 2015-07-07 12:44:17 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Keun-young Park | badbbae | 2016-01-05 13:27:29 -0800 | [diff] [blame] | 826 | /** |
| 827 | * Sets the component that will handle USB device connection. |
| 828 | * <p> |
| 829 | * Setting component allows to specify external USB host manager to handle use cases, where |
| 830 | * selection dialog for an activity that will handle USB device is undesirable. |
| 831 | * Only system components can call this function, as it requires the MANAGE_USB permission. |
| 832 | * |
| 833 | * @param usbDeviceConnectionHandler The component to handle usb connections, |
| 834 | * {@code null} to unset. |
| 835 | * |
| 836 | * {@hide} |
| 837 | */ |
| 838 | public void setUsbDeviceConnectionHandler(@Nullable ComponentName usbDeviceConnectionHandler) { |
| 839 | try { |
| 840 | mService.setUsbDeviceConnectionHandler(usbDeviceConnectionHandler); |
| 841 | } catch (RemoteException e) { |
| 842 | throw e.rethrowFromSystemServer(); |
| 843 | } |
| 844 | } |
| 845 | |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 846 | /** |
| 847 | * Returns whether the given functions are valid inputs to UsbManager. |
| 848 | * Currently the empty functions or any of MTP, PTP, RNDIS, MIDI are accepted. |
| 849 | * |
| 850 | * @return Whether the mask is settable. |
| 851 | * |
| 852 | * {@hide} |
| 853 | */ |
| 854 | public static boolean areSettableFunctions(long functions) { |
| 855 | return functions == FUNCTION_NONE |
| 856 | || ((~SETTABLE_FUNCTIONS & functions) == 0 && Long.bitCount(functions) == 1); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 859 | /** |
| 860 | * Converts the given function mask to string. Maintains ordering with respect to init scripts. |
| 861 | * |
| 862 | * @return String representation of given mask |
| 863 | * |
| 864 | * {@hide} |
| 865 | */ |
| 866 | public static String usbFunctionsToString(long functions) { |
| 867 | StringJoiner joiner = new StringJoiner(","); |
| 868 | if ((functions & FUNCTION_MTP) != 0) { |
| 869 | joiner.add(UsbManager.USB_FUNCTION_MTP); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 870 | } |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 871 | if ((functions & FUNCTION_PTP) != 0) { |
| 872 | joiner.add(UsbManager.USB_FUNCTION_PTP); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 873 | } |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 874 | if ((functions & FUNCTION_RNDIS) != 0) { |
| 875 | joiner.add(UsbManager.USB_FUNCTION_RNDIS); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 876 | } |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 877 | if ((functions & FUNCTION_MIDI) != 0) { |
| 878 | joiner.add(UsbManager.USB_FUNCTION_MIDI); |
| 879 | } |
| 880 | if ((functions & FUNCTION_ACCESSORY) != 0) { |
| 881 | joiner.add(UsbManager.USB_FUNCTION_ACCESSORY); |
| 882 | } |
| 883 | if ((functions & FUNCTION_AUDIO_SOURCE) != 0) { |
| 884 | joiner.add(UsbManager.USB_FUNCTION_AUDIO_SOURCE); |
| 885 | } |
| 886 | if ((functions & FUNCTION_ADB) != 0) { |
| 887 | joiner.add(UsbManager.USB_FUNCTION_ADB); |
| 888 | } |
| 889 | return joiner.toString(); |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Jerry Zhang | 327b809 | 2018-01-09 17:53:04 -0800 | [diff] [blame] | 892 | /** |
| 893 | * Parses a string of usb functions that are comma separated. |
| 894 | * |
| 895 | * @return A mask of all valid functions in the string |
| 896 | * |
| 897 | * {@hide} |
| 898 | */ |
| 899 | public static long usbFunctionsFromString(String functions) { |
| 900 | if (functions == null || functions.equals(USB_FUNCTION_NONE)) { |
| 901 | return FUNCTION_NONE; |
| 902 | } |
| 903 | long ret = 0; |
| 904 | for (String function : functions.split(",")) { |
| 905 | if (FUNCTION_NAME_TO_CODE.containsKey(function)) { |
| 906 | ret |= FUNCTION_NAME_TO_CODE.get(function); |
| 907 | } else if (function.length() > 0) { |
| 908 | throw new IllegalArgumentException("Invalid usb function " + functions); |
| 909 | } |
| 910 | } |
| 911 | return ret; |
Jeff Brown | 460a146 | 2015-06-30 17:57:12 -0700 | [diff] [blame] | 912 | } |
Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 913 | } |