blob: 572c58564c8d364c320e6ea2c15a3f6f8b52da54 [file] [log] [blame]
Mike Lockwood24236072010-06-23 17:36:36 -04001/*
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 Lockwoodc4308f02011-03-01 08:04:54 -080018package android.hardware.usb;
Mike Lockwood24236072010-06-23 17:36:36 -040019
Philip P. Moltmannf3189e62017-08-16 16:04:24 -070020import android.Manifest;
Keun-young Parkbadbbae2016-01-05 13:27:29 -080021import android.annotation.Nullable;
Philip P. Moltmann00d2de92018-02-16 11:14:54 -080022import android.annotation.RequiresFeature;
Philip P. Moltmannf3189e62017-08-16 16:04:24 -070023import android.annotation.RequiresPermission;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060024import android.annotation.SdkConstant;
25import android.annotation.SdkConstant.SdkConstantType;
Philip P. Moltmannf3189e62017-08-16 16:04:24 -070026import android.annotation.SystemApi;
27import android.annotation.SystemService;
Mike Lockwood3a68b832011-03-08 10:08:59 -050028import android.app.PendingIntent;
Keun-young Parkbadbbae2016-01-05 13:27:29 -080029import android.content.ComponentName;
Mike Lockwood3a68b832011-03-08 10:08:59 -050030import android.content.Context;
Philip P. Moltmann00d2de92018-02-16 11:14:54 -080031import android.content.pm.PackageManager;
Johan T. Halseth244259a2016-05-24 14:49:04 +010032import android.content.pm.PackageManager.NameNotFoundException;
Jerry Zhang327b8092018-01-09 17:53:04 -080033import android.hardware.usb.gadget.V1_0.GadgetFunction;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050034import android.os.Bundle;
35import android.os.ParcelFileDescriptor;
Daichi Hirono47518802015-12-08 09:51:19 +090036import android.os.Process;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050037import android.os.RemoteException;
38import android.util.Log;
39
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060040import com.android.internal.util.Preconditions;
41
Mike Lockwoode7d511e2010-12-30 13:39:37 -050042import java.util.HashMap;
Jerry Zhang327b8092018-01-09 17:53:04 -080043import java.util.Map;
44import java.util.StringJoiner;
Mike Lockwood08bff3b2010-08-31 13:27:05 -040045
Mike Lockwood24236072010-06-23 17:36:36 -040046/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040047 * 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 Lockwoode7d511e2010-12-30 13:39:37 -050049 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080050 * <div class="special reference">
51 * <h3>Developer Guides</h3>
52 * <p>For more information about communicating with USB hardware, read the
Mark Lu8b739902016-06-24 15:38:02 -070053 * <a href="{@docRoot}guide/topics/connectivity/usb/index.html">USB developer guide</a>.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080054 * </div>
Mike Lockwood24236072010-06-23 17:36:36 -040055 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060056@SystemService(Context.USB_SERVICE)
Mike Lockwood770126a2010-12-09 22:30:37 -080057public class UsbManager {
Mike Lockwoode7d511e2010-12-30 13:39:37 -050058 private static final String TAG = "UsbManager";
59
Mike Lockwood24236072010-06-23 17:36:36 -040060 /**
Mike Lockwoode7d511e2010-12-30 13:39:37 -050061 * Broadcast Action: A sticky broadcast for USB state change events when in device mode.
Mike Lockwood709981e2010-06-28 09:58:58 -040062 *
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080063 * This is a sticky broadcast for clients that includes USB connected/disconnected state,
Mike Lockwood9182d3c2011-02-15 09:50:22 -050064 * <ul>
65 * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
Daichi Hirono618fa6f2016-03-17 14:14:32 +090066 * <li> {@link #USB_HOST_CONNECTED} boolean indicating whether USB is connected or
67 * disconnected as host.
Mike Lockwood02e45692011-06-14 15:43:51 -040068 * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured.
Mike Lockwood9eb014a2011-06-08 09:17:45 -070069 * currently zero if not configured, one for configured.
Mike Lockwood9eb014a2011-06-08 09:17:45 -070070 * <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 Zhang327b8092018-01-09 17:53:04 -080078 * <li> {@link #USB_FUNCTION_ACCESSORY} boolean extra indicating whether the
Mike Lockwood9eb014a2011-06-08 09:17:45 -070079 * accessory function is enabled
Mike Lockwood9d5a4be2012-04-06 09:41:32 -070080 * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
81 * audio source function is enabled
Mike Lockwood2a57bc72014-09-19 11:16:52 -070082 * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the
83 * MIDI function is enabled
Mike Lockwood9182d3c2011-02-15 09:50:22 -050084 * </ul>
Yasuhiro Matsuda48b9a7c2015-07-16 19:00:16 +090085 * 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 Lockwooda75075e12011-03-11 11:26:11 -050087 *
88 * {@hide}
Mike Lockwood709981e2010-06-28 09:58:58 -040089 */
90 public static final String ACTION_USB_STATE =
Mike Lockwoodc4308f02011-03-01 08:04:54 -080091 "android.hardware.usb.action.USB_STATE";
Mike Lockwood709981e2010-06-28 09:58:58 -040092
Jeff Brown76c4c662015-07-07 12:44:17 -070093 /**
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 Lockwoode7d511e2010-12-30 13:39:37 -0500109 /**
Andrew Solovay65342912017-10-27 13:02:34 -0700110 * Activity intent sent when user attaches a USB device.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500111 *
112 * This intent is sent when a USB device is attached to the USB bus when in host mode.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500113 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800114 * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500115 * for the attached device
116 * </ul>
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500117 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600118 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500119 public static final String ACTION_USB_DEVICE_ATTACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800120 "android.hardware.usb.action.USB_DEVICE_ATTACHED";
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500121
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 Lockwood9182d3c2011-02-15 09:50:22 -0500126 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800127 * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
Mike Lockwood188d00b2011-02-23 13:14:33 -0800128 * for the detached device
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500129 * </ul>
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500130 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600131 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500132 public static final String ACTION_USB_DEVICE_DETACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800133 "android.hardware.usb.action.USB_DEVICE_DETACHED";
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500134
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500135 /**
Andrew Solovay65342912017-10-27 13:02:34 -0700136 * Activity intent sent when user attaches a USB accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500137 *
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500138 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800139 * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500140 * for the attached accessory
141 * </ul>
142 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600143 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500144 public static final String ACTION_USB_ACCESSORY_ATTACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800145 "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500146
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 Lockwood980f0432011-03-09 15:49:13 -0500152 * <li> {@link #EXTRA_ACCESSORY} containing the {@link UsbAccessory}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500153 * for the attached accessory that was detached
154 * </ul>
155 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600156 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500157 public static final String ACTION_USB_ACCESSORY_DETACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800158 "android.hardware.usb.action.USB_ACCESSORY_DETACHED";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500159
Mike Lockwood709981e2010-06-28 09:58:58 -0400160 /**
161 * Boolean extra indicating whether USB is connected or disconnected.
162 * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500163 *
164 * {@hide}
Mike Lockwood709981e2010-06-28 09:58:58 -0400165 */
166 public static final String USB_CONNECTED = "connected";
Mike Lockwood24236072010-06-23 17:36:36 -0400167
168 /**
Daichi Hirono618fa6f2016-03-17 14:14:32 +0900169 * 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 Lockwood02e45692011-06-14 15:43:51 -0400177 * Boolean extra indicating whether USB is configured.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800178 * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500179 *
180 * {@hide}
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800181 */
Mike Lockwood02e45692011-06-14 15:43:51 -0400182 public static final String USB_CONFIGURED = "configured";
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800183
184 /**
Nick Kralevich67401902015-06-10 09:38:42 -0700185 * 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 Zhangbb598ee2016-10-24 14:35:08 -0700195 * 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 Brown460a1462015-06-30 17:57:12 -0700203 * A placeholder indicating that no USB function is being specified.
Jerry Zhang327b8092018-01-09 17:53:04 -0800204 * Used for compatibility with old init scripts to indicate no functions vs. charging function.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500205 *
206 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400207 */
Jeff Brown460a1462015-06-30 17:57:12 -0700208 public static final String USB_FUNCTION_NONE = "none";
Mike Lockwood24236072010-06-23 17:36:36 -0400209
210 /**
211 * Name of the adb USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800212 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500213 *
214 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400215 */
216 public static final String USB_FUNCTION_ADB = "adb";
217
218 /**
219 * Name of the RNDIS ethernet USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800220 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500221 *
222 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400223 */
224 public static final String USB_FUNCTION_RNDIS = "rndis";
225
226 /**
227 * Name of the MTP USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800228 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500229 *
230 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400231 */
232 public static final String USB_FUNCTION_MTP = "mtp";
233
234 /**
Mike Lockwood9eb014a2011-06-08 09:17:45 -0700235 * 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 Lockwood9d5a4be2012-04-06 09:41:32 -0700243 * 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 Lockwood2a57bc72014-09-19 11:16:52 -0700251 * 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 Lockwood9182d3c2011-02-15 09:50:22 -0500259 * Name of the Accessory USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800260 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500261 *
262 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400263 */
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500264 public static final String USB_FUNCTION_ACCESSORY = "accessory";
265
266 /**
Jeff Brown76c4c662015-07-07 12:44:17 -0700267 * 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 Lockwoode7d511e2010-12-30 13:39:37 -0500284 * Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and
Mike Lockwood02eb8742011-02-27 09:10:37 -0800285 * {@link #ACTION_USB_DEVICE_DETACHED} broadcasts
Jeff Brown460a1462015-06-30 17:57:12 -0700286 * containing the {@link UsbDevice} object for the device.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500287 */
288 public static final String EXTRA_DEVICE = "device";
289
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500290 /**
Mike Lockwood02eb8742011-02-27 09:10:37 -0800291 * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and
292 * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts
Jeff Brown460a1462015-06-30 17:57:12 -0700293 * containing the {@link UsbAccessory} object for the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500294 */
295 public static final String EXTRA_ACCESSORY = "accessory";
296
Mike Lockwood3a68b832011-03-08 10:08:59 -0500297 /**
298 * Name of extra added to the {@link android.app.PendingIntent}
Mike Lockwood980f0432011-03-09 15:49:13 -0500299 * passed into {@link #requestPermission(UsbDevice, PendingIntent)}
300 * or {@link #requestPermission(UsbAccessory, PendingIntent)}
Mike Lockwood3a68b832011-03-08 10:08:59 -0500301 * containing a boolean value indicating whether the user granted permission or not.
302 */
303 public static final String EXTRA_PERMISSION_GRANTED = "permission";
304
Jerry Zhang327b8092018-01-09 17:53:04 -0800305 /**
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 Lockwood3a68b832011-03-08 10:08:59 -0500368 private final Context mContext;
369 private final IUsbManager mService;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500370
371 /**
372 * {@hide}
373 */
Mike Lockwood3a68b832011-03-08 10:08:59 -0500374 public UsbManager(Context context, IUsbManager service) {
375 mContext = context;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500376 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. Moltmann00d2de92018-02-16 11:14:54 -0800387 @RequiresFeature(PackageManager.FEATURE_USB_HOST)
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500388 public HashMap<String,UsbDevice> getDeviceList() {
Jim Kaye9b5ee822017-02-15 14:28:23 -0800389 HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>();
390 if (mService == null) {
391 return result;
392 }
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500393 Bundle bundle = new Bundle();
394 try {
395 mService.getDeviceList(bundle);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500396 for (String name : bundle.keySet()) {
397 result.put(name, (UsbDevice)bundle.get(name));
398 }
399 return result;
400 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700401 throw e.rethrowFromSystemServer();
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500402 }
403 }
404
405 /**
406 * Opens the device so it can be used to send and receive
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800407 * data using {@link android.hardware.usb.UsbRequest}.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500408 *
409 * @param device the device to open
mike wakerly1567a432012-07-17 19:58:19 -0700410 * @return a {@link UsbDeviceConnection}, or {@code null} if open failed
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500411 */
Philip P. Moltmann00d2de92018-02-16 11:14:54 -0800412 @RequiresFeature(PackageManager.FEATURE_USB_HOST)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500413 public UsbDeviceConnection openDevice(UsbDevice device) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500414 try {
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500415 String deviceName = device.getDeviceName();
Emilian Peevc8ed5ec2017-09-25 13:03:08 +0100416 ParcelFileDescriptor pfd = mService.openDevice(deviceName, mContext.getPackageName());
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500417 if (pfd != null) {
418 UsbDeviceConnection connection = new UsbDeviceConnection(device);
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -0700419 boolean result = connection.open(deviceName, pfd, mContext);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500420 pfd.close();
421 if (result) {
422 return connection;
423 }
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500424 }
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500425 } catch (Exception e) {
426 Log.e(TAG, "exception in UsbManager.openDevice", e);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500427 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500428 return null;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500429 }
430
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500431 /**
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. Moltmann00d2de92018-02-16 11:14:54 -0800437 @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500438 public UsbAccessory[] getAccessoryList() {
Jim Kaye9b5ee822017-02-15 14:28:23 -0800439 if (mService == null) {
440 return null;
441 }
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500442 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 Sharkeyf8880562016-02-26 13:03:01 -0700450 throw e.rethrowFromSystemServer();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500451 }
452 }
453
454 /**
455 * Opens a file descriptor for reading and writing data to the USB accessory.
456 *
Philip P. Moltmann84e04792018-02-16 16:37:07 -0800457 * <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 Lockwood9182d3c2011-02-15 09:50:22 -0500461 * @param accessory the USB accessory to open
Philip P. Moltmann84e04792018-02-16 16:37:07 -0800462 * @return file descriptor, or null if the accessory could not be opened.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500463 */
Philip P. Moltmann00d2de92018-02-16 11:14:54 -0800464 @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500465 public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
466 try {
Mike Lockwood02eb8742011-02-27 09:10:37 -0800467 return mService.openAccessory(accessory);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500468 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700469 throw e.rethrowFromSystemServer();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500470 }
471 }
472
Mike Lockwood3a68b832011-03-08 10:08:59 -0500473 /**
474 * Returns true if the caller has permission to access the device.
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500475 * Permission might have been granted temporarily via
Mike Lockwood980f0432011-03-09 15:49:13 -0500476 * {@link #requestPermission(UsbDevice, PendingIntent)} or
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500477 * by the user choosing the caller as the default application for the device.
Emilian Peevc8ed5ec2017-09-25 13:03:08 +0100478 * 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 Lockwood3a68b832011-03-08 10:08:59 -0500481 *
482 * @param device to check permissions for
483 * @return true if caller has permission
484 */
Philip P. Moltmann00d2de92018-02-16 11:14:54 -0800485 @RequiresFeature(PackageManager.FEATURE_USB_HOST)
Mike Lockwood3a68b832011-03-08 10:08:59 -0500486 public boolean hasPermission(UsbDevice device) {
Jim Kaye9b5ee822017-02-15 14:28:23 -0800487 if (mService == null) {
488 return false;
489 }
Mike Lockwood3a68b832011-03-08 10:08:59 -0500490 try {
Emilian Peevc8ed5ec2017-09-25 13:03:08 +0100491 return mService.hasDevicePermission(device, mContext.getPackageName());
Mike Lockwood3a68b832011-03-08 10:08:59 -0500492 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700493 throw e.rethrowFromSystemServer();
Mike Lockwood3a68b832011-03-08 10:08:59 -0500494 }
495 }
496
497 /**
498 * Returns true if the caller has permission to access the accessory.
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500499 * Permission might have been granted temporarily via
Mike Lockwood980f0432011-03-09 15:49:13 -0500500 * {@link #requestPermission(UsbAccessory, PendingIntent)} or
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500501 * by the user choosing the caller as the default application for the accessory.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500502 *
503 * @param accessory to check permissions for
504 * @return true if caller has permission
505 */
Philip P. Moltmann00d2de92018-02-16 11:14:54 -0800506 @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
Mike Lockwood3a68b832011-03-08 10:08:59 -0500507 public boolean hasPermission(UsbAccessory accessory) {
Jim Kaye9b5ee822017-02-15 14:28:23 -0800508 if (mService == null) {
509 return false;
510 }
Mike Lockwood3a68b832011-03-08 10:08:59 -0500511 try {
512 return mService.hasAccessoryPermission(accessory);
513 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700514 throw e.rethrowFromSystemServer();
Mike Lockwood3a68b832011-03-08 10:08:59 -0500515 }
516 }
517
518 /**
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500519 * Requests temporary permission for the given package to access the device.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500520 * 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 Lockwoodc6f23e82011-03-09 12:05:20 -0500523 * If successful, this grants the caller permission to access the device only
524 * until the device is disconnected.
525 *
Mike Lockwood3a68b832011-03-08 10:08:59 -0500526 * 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 Peevc8ed5ec2017-09-25 13:03:08 +0100533 * 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 Lockwood3a68b832011-03-08 10:08:59 -0500537 * @param device to request permissions for
538 * @param pi PendingIntent for returning result
539 */
Philip P. Moltmann00d2de92018-02-16 11:14:54 -0800540 @RequiresFeature(PackageManager.FEATURE_USB_HOST)
Mike Lockwood3a68b832011-03-08 10:08:59 -0500541 public void requestPermission(UsbDevice device, PendingIntent pi) {
542 try {
543 mService.requestDevicePermission(device, mContext.getPackageName(), pi);
544 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700545 throw e.rethrowFromSystemServer();
Mike Lockwood3a68b832011-03-08 10:08:59 -0500546 }
547 }
548
549 /**
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500550 * Requests temporary permission for the given package to access the accessory.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500551 * 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 Lockwood62cfeeb2011-03-11 18:39:03 -0500554 * If successful, this grants the caller permission to access the accessory only
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500555 * until the device is disconnected.
556 *
Mike Lockwood3a68b832011-03-08 10:08:59 -0500557 * 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. Moltmann00d2de92018-02-16 11:14:54 -0800567 @RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
Mike Lockwood3a68b832011-03-08 10:08:59 -0500568 public void requestPermission(UsbAccessory accessory, PendingIntent pi) {
569 try {
570 mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi);
571 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700572 throw e.rethrowFromSystemServer();
Mike Lockwood3a68b832011-03-08 10:08:59 -0500573 }
574 }
575
Mike Lockwood02e45692011-06-14 15:43:51 -0400576 /**
Daichi Hirono47518802015-12-08 09:51:19 +0900577 * 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 Parkbadbbae2016-01-05 13:27:29 -0800584 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 Hirono47518802015-12-08 09:51:19 +0900596 try {
Keun-young Parkbadbbae2016-01-05 13:27:29 -0800597 mService.grantDevicePermission(device, uid);
Daichi Hirono47518802015-12-08 09:51:19 +0900598 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700599 throw e.rethrowFromSystemServer();
Daichi Hirono47518802015-12-08 09:51:19 +0900600 }
601 }
602
603 /**
Johan T. Halseth244259a2016-05-24 14:49:04 +0100604 * 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. Moltmannf3189e62017-08-16 16:04:24 -0700611 @SystemApi
612 @RequiresPermission(Manifest.permission.MANAGE_USB)
Johan T. Halseth244259a2016-05-24 14:49:04 +0100613 public void grantPermission(UsbDevice device, String packageName) {
614 try {
615 int uid = mContext.getPackageManager()
616 .getPackageUidAsUser(packageName, mContext.getUserId());
Keun-young Parkbadbbae2016-01-05 13:27:29 -0800617 grantPermission(device, uid);
Johan T. Halseth244259a2016-05-24 14:49:04 +0100618 } catch (NameNotFoundException e) {
619 Log.e(TAG, "Package " + packageName + " not found.", e);
Johan T. Halseth244259a2016-05-24 14:49:04 +0100620 }
621 }
622
623 /**
Jeff Brown460a1462015-06-30 17:57:12 -0700624 * 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 Lockwoode51099f2011-08-02 12:54:49 -0400629 *
Jerry Zhang327b8092018-01-09 17:53:04 -0800630 * @deprecated use getCurrentFunctions() instead.
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700631 * @param function name of the USB function
Jeff Brown460a1462015-06-30 17:57:12 -0700632 * @return true if the USB function is enabled
Mike Lockwoode51099f2011-08-02 12:54:49 -0400633 *
634 * {@hide}
635 */
Jerry Zhang327b8092018-01-09 17:53:04 -0800636 @Deprecated
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700637 public boolean isFunctionEnabled(String function) {
Jeff Brown460a1462015-06-30 17:57:12 -0700638 try {
639 return mService.isFunctionEnabled(function);
640 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700641 throw e.rethrowFromSystemServer();
Jeff Brown460a1462015-06-30 17:57:12 -0700642 }
Mike Lockwoode51099f2011-08-02 12:54:49 -0400643 }
644
645 /**
Jerry Zhang327b8092018-01-09 17:53:04 -0800646 * Sets the current USB functions when in device mode.
Jeff Brown460a1462015-06-30 17:57:12 -0700647 * <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 Zhang327b8092018-01-09 17:53:04 -0800655 * An argument of 0 indicates that the device is charging, and can pick any
656 * appropriate function for that purpose.
Jerry Zhang7a396be2016-10-12 15:49:32 -0700657 * </p><p>
Jeff Brown460a1462015-06-30 17:57:12 -0700658 * Note: This function is asynchronous and may fail silently without applying
659 * the requested changes.
660 * </p>
Mike Lockwood02e45692011-06-14 15:43:51 -0400661 *
Jerry Zhang327b8092018-01-09 17:53:04 -0800662 * @param functions the USB function(s) to set, as a bitwise mask.
663 * Must satisfy {@link UsbManager#areSettableFunctions}
Mike Lockwood02e45692011-06-14 15:43:51 -0400664 *
665 * {@hide}
666 */
Jerry Zhang327b8092018-01-09 17:53:04 -0800667 public void setCurrentFunctions(long functions) {
Mike Lockwood08bff3b2010-08-31 13:27:05 -0400668 try {
Jerry Zhang327b8092018-01-09 17:53:04 -0800669 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 Kralevich67401902015-06-10 09:38:42 -0700708 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700709 throw e.rethrowFromSystemServer();
Nick Kralevich67401902015-06-10 09:38:42 -0700710 }
711 }
712
713 /**
Jerry Zhang30b9adf2017-11-30 19:10:55 -0800714 * Sets the screen unlocked functions, which are persisted and set as the current functions
715 * whenever the screen is unlocked.
716 * <p>
Jerry Zhang327b8092018-01-09 17:53:04 -0800717 * A zero mask has the effect of switching off this feature, so functions
Jerry Zhang30b9adf2017-11-30 19:10:55 -0800718 * 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 Zhang327b8092018-01-09 17:53:04 -0800724 * @param functions functions to set, in a bitwise mask.
725 * Must satisfy {@link UsbManager#areSettableFunctions}
Jerry Zhang30b9adf2017-11-30 19:10:55 -0800726 *
727 * {@hide}
728 */
Jerry Zhang327b8092018-01-09 17:53:04 -0800729 public void setScreenUnlockedFunctions(long functions) {
Jerry Zhang30b9adf2017-11-30 19:10:55 -0800730 try {
Jerry Zhang327b8092018-01-09 17:53:04 -0800731 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 Zhang30b9adf2017-11-30 19:10:55 -0800748 } catch (RemoteException e) {
749 throw e.rethrowFromSystemServer();
750 }
751 }
752
753 /**
Jeff Brown76c4c662015-07-07 12:44:17 -0700754 * 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 Kaye9b5ee822017-02-15 14:28:23 -0800767 if (mService == null) {
768 return null;
769 }
Jeff Brown76c4c662015-07-07 12:44:17 -0700770 try {
771 return mService.getPorts();
772 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700773 throw e.rethrowFromSystemServer();
Jeff Brown76c4c662015-07-07 12:44:17 -0700774 }
Jeff Brown76c4c662015-07-07 12:44:17 -0700775 }
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 Sharkeyf8880562016-02-26 13:03:01 -0700791 throw e.rethrowFromSystemServer();
Jeff Brown76c4c662015-07-07 12:44:17 -0700792 }
Jeff Brown76c4c662015-07-07 12:44:17 -0700793 }
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 Sridharan4641fc32017-06-22 16:39:47 -0700818 Log.d(TAG, "setPortRoles Package:" + mContext.getPackageName());
Jeff Brown76c4c662015-07-07 12:44:17 -0700819 try {
820 mService.setPortRoles(port.getId(), powerRole, dataRole);
821 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700822 throw e.rethrowFromSystemServer();
Jeff Brown76c4c662015-07-07 12:44:17 -0700823 }
824 }
825
Keun-young Parkbadbbae2016-01-05 13:27:29 -0800826 /**
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 Zhang327b8092018-01-09 17:53:04 -0800846 /**
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 Brown460a1462015-06-30 17:57:12 -0700857 }
858
Jerry Zhang327b8092018-01-09 17:53:04 -0800859 /**
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 Brown460a1462015-06-30 17:57:12 -0700870 }
Jerry Zhang327b8092018-01-09 17:53:04 -0800871 if ((functions & FUNCTION_PTP) != 0) {
872 joiner.add(UsbManager.USB_FUNCTION_PTP);
Jeff Brown460a1462015-06-30 17:57:12 -0700873 }
Jerry Zhang327b8092018-01-09 17:53:04 -0800874 if ((functions & FUNCTION_RNDIS) != 0) {
875 joiner.add(UsbManager.USB_FUNCTION_RNDIS);
Jeff Brown460a1462015-06-30 17:57:12 -0700876 }
Jerry Zhang327b8092018-01-09 17:53:04 -0800877 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 Brown460a1462015-06-30 17:57:12 -0700890 }
891
Jerry Zhang327b8092018-01-09 17:53:04 -0800892 /**
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 Brown460a1462015-06-30 17:57:12 -0700912 }
Mike Lockwood24236072010-06-23 17:36:36 -0400913}