blob: 1e3bcd0fc796a95058659d04d3a89e455c35a439 [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
Jeff Brown76c4c662015-07-07 12:44:17 -070020import com.android.internal.util.Preconditions;
21
Mike Lockwood3a68b832011-03-08 10:08:59 -050022import android.app.PendingIntent;
23import android.content.Context;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050024import android.os.Bundle;
25import android.os.ParcelFileDescriptor;
Daichi Hirono47518802015-12-08 09:51:19 +090026import android.os.Process;
Mike Lockwoode7d511e2010-12-30 13:39:37 -050027import android.os.RemoteException;
28import android.util.Log;
29
Mike Lockwoode7d511e2010-12-30 13:39:37 -050030import java.util.HashMap;
Mike Lockwood08bff3b2010-08-31 13:27:05 -040031
Mike Lockwood24236072010-06-23 17:36:36 -040032/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040033 * This class allows you to access the state of USB and communicate with USB devices.
34 * Currently only host mode is supported in the public API.
Mike Lockwoode7d511e2010-12-30 13:39:37 -050035 *
36 * <p>You can obtain an instance of this class by calling
37 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
38 *
39 * {@samplecode
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080040 * UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);}
41 *
42 * <div class="special reference">
43 * <h3>Developer Guides</h3>
44 * <p>For more information about communicating with USB hardware, read the
45 * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
46 * </div>
Mike Lockwood24236072010-06-23 17:36:36 -040047 */
Mike Lockwood770126a2010-12-09 22:30:37 -080048public class UsbManager {
Mike Lockwoode7d511e2010-12-30 13:39:37 -050049 private static final String TAG = "UsbManager";
50
Mike Lockwood24236072010-06-23 17:36:36 -040051 /**
Mike Lockwoode7d511e2010-12-30 13:39:37 -050052 * Broadcast Action: A sticky broadcast for USB state change events when in device mode.
Mike Lockwood709981e2010-06-28 09:58:58 -040053 *
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080054 * This is a sticky broadcast for clients that includes USB connected/disconnected state,
Mike Lockwood9182d3c2011-02-15 09:50:22 -050055 * <ul>
56 * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
Mike Lockwood02e45692011-06-14 15:43:51 -040057 * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured.
Mike Lockwood9eb014a2011-06-08 09:17:45 -070058 * currently zero if not configured, one for configured.
Mike Lockwood9eb014a2011-06-08 09:17:45 -070059 * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the
60 * adb function is enabled
61 * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the
62 * RNDIS ethernet function is enabled
63 * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the
64 * MTP function is enabled
65 * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
66 * PTP function is enabled
67 * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
68 * accessory function is enabled
Mike Lockwood9d5a4be2012-04-06 09:41:32 -070069 * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
70 * audio source function is enabled
Mike Lockwood2a57bc72014-09-19 11:16:52 -070071 * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the
72 * MIDI function is enabled
Mike Lockwood9182d3c2011-02-15 09:50:22 -050073 * </ul>
Yasuhiro Matsuda48b9a7c2015-07-16 19:00:16 +090074 * If the sticky intent has not been found, that indicates USB is disconnected,
75 * USB is not configued, MTP function is enabled, and all the other functions are disabled.
Mike Lockwooda75075e12011-03-11 11:26:11 -050076 *
77 * {@hide}
Mike Lockwood709981e2010-06-28 09:58:58 -040078 */
79 public static final String ACTION_USB_STATE =
Mike Lockwoodc4308f02011-03-01 08:04:54 -080080 "android.hardware.usb.action.USB_STATE";
Mike Lockwood709981e2010-06-28 09:58:58 -040081
Jeff Brown76c4c662015-07-07 12:44:17 -070082 /**
83 * Broadcast Action: A broadcast for USB port changes.
84 *
85 * This intent is sent when a USB port is added, removed, or changes state.
86 * <ul>
87 * <li> {@link #EXTRA_PORT} containing the {@link android.hardware.usb.UsbPort}
88 * for the port.
89 * <li> {@link #EXTRA_PORT_STATUS} containing the {@link android.hardware.usb.UsbPortStatus}
90 * for the port, or null if the port has been removed
91 * </ul>
92 *
93 * @hide
94 */
95 public static final String ACTION_USB_PORT_CHANGED =
96 "android.hardware.usb.action.USB_PORT_CHANGED";
97
Mike Lockwoode7d511e2010-12-30 13:39:37 -050098 /**
99 * Broadcast Action: A broadcast for USB device attached event.
100 *
101 * 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 -0500102 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800103 * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500104 * for the attached device
105 * </ul>
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500106 */
107 public static final String ACTION_USB_DEVICE_ATTACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800108 "android.hardware.usb.action.USB_DEVICE_ATTACHED";
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500109
110 /**
111 * Broadcast Action: A broadcast for USB device detached event.
112 *
113 * 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 -0500114 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800115 * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
Mike Lockwood188d00b2011-02-23 13:14:33 -0800116 * for the detached device
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500117 * </ul>
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500118 */
119 public static final String ACTION_USB_DEVICE_DETACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800120 "android.hardware.usb.action.USB_DEVICE_DETACHED";
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500121
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500122 /**
123 * Broadcast Action: A broadcast for USB accessory attached event.
124 *
125 * This intent is sent when a USB accessory is attached.
126 * <ul>
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800127 * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500128 * for the attached accessory
129 * </ul>
130 */
131 public static final String ACTION_USB_ACCESSORY_ATTACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800132 "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500133
134 /**
135 * Broadcast Action: A broadcast for USB accessory detached event.
136 *
137 * This intent is sent when a USB accessory is detached.
138 * <ul>
Mike Lockwood980f0432011-03-09 15:49:13 -0500139 * <li> {@link #EXTRA_ACCESSORY} containing the {@link UsbAccessory}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500140 * for the attached accessory that was detached
141 * </ul>
142 */
143 public static final String ACTION_USB_ACCESSORY_DETACHED =
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800144 "android.hardware.usb.action.USB_ACCESSORY_DETACHED";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500145
Mike Lockwood709981e2010-06-28 09:58:58 -0400146 /**
147 * Boolean extra indicating whether USB is connected or disconnected.
148 * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500149 *
150 * {@hide}
Mike Lockwood709981e2010-06-28 09:58:58 -0400151 */
152 public static final String USB_CONNECTED = "connected";
Mike Lockwood24236072010-06-23 17:36:36 -0400153
154 /**
Mike Lockwood02e45692011-06-14 15:43:51 -0400155 * Boolean extra indicating whether USB is configured.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800156 * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500157 *
158 * {@hide}
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800159 */
Mike Lockwood02e45692011-06-14 15:43:51 -0400160 public static final String USB_CONFIGURED = "configured";
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800161
162 /**
Nick Kralevich67401902015-06-10 09:38:42 -0700163 * Boolean extra indicating whether confidential user data, such as photos, should be
164 * made available on the USB connection. This variable will only be set when the user
165 * has explicitly asked for this data to be unlocked.
166 * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
167 *
168 * {@hide}
169 */
170 public static final String USB_DATA_UNLOCKED = "unlocked";
171
172 /**
Jeff Brown460a1462015-06-30 17:57:12 -0700173 * A placeholder indicating that no USB function is being specified.
174 * Used to distinguish between selecting no function vs. the default function in
175 * {@link #setCurrentFunction(String)}.
Mike Lockwooda75075e12011-03-11 11:26:11 -0500176 *
177 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400178 */
Jeff Brown460a1462015-06-30 17:57:12 -0700179 public static final String USB_FUNCTION_NONE = "none";
Mike Lockwood24236072010-06-23 17:36:36 -0400180
181 /**
182 * Name of the adb USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800183 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500184 *
185 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400186 */
187 public static final String USB_FUNCTION_ADB = "adb";
188
189 /**
190 * Name of the RNDIS ethernet USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800191 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500192 *
193 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400194 */
195 public static final String USB_FUNCTION_RNDIS = "rndis";
196
197 /**
198 * Name of the MTP USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800199 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500200 *
201 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400202 */
203 public static final String USB_FUNCTION_MTP = "mtp";
204
205 /**
Mike Lockwood9eb014a2011-06-08 09:17:45 -0700206 * Name of the PTP USB function.
207 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
208 *
209 * {@hide}
210 */
211 public static final String USB_FUNCTION_PTP = "ptp";
212
213 /**
Mike Lockwood9d5a4be2012-04-06 09:41:32 -0700214 * Name of the audio source USB function.
215 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
216 *
217 * {@hide}
218 */
219 public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source";
220
221 /**
Mike Lockwood2a57bc72014-09-19 11:16:52 -0700222 * Name of the MIDI USB function.
223 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
224 *
225 * {@hide}
226 */
227 public static final String USB_FUNCTION_MIDI = "midi";
228
229 /**
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500230 * Name of the Accessory USB function.
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800231 * Used in extras for the {@link #ACTION_USB_STATE} broadcast
Mike Lockwooda75075e12011-03-11 11:26:11 -0500232 *
233 * {@hide}
Mike Lockwood24236072010-06-23 17:36:36 -0400234 */
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500235 public static final String USB_FUNCTION_ACCESSORY = "accessory";
236
237 /**
Jeff Brown76c4c662015-07-07 12:44:17 -0700238 * Name of extra for {@link #ACTION_USB_PORT_CHANGED}
239 * containing the {@link UsbPort} object for the port.
240 *
241 * @hide
242 */
243 public static final String EXTRA_PORT = "port";
244
245 /**
246 * Name of extra for {@link #ACTION_USB_PORT_CHANGED}
247 * containing the {@link UsbPortStatus} object for the port, or null if the port
248 * was removed.
249 *
250 * @hide
251 */
252 public static final String EXTRA_PORT_STATUS = "portStatus";
253
254 /**
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500255 * Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and
Mike Lockwood02eb8742011-02-27 09:10:37 -0800256 * {@link #ACTION_USB_DEVICE_DETACHED} broadcasts
Jeff Brown460a1462015-06-30 17:57:12 -0700257 * containing the {@link UsbDevice} object for the device.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500258 */
259 public static final String EXTRA_DEVICE = "device";
260
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500261 /**
Mike Lockwood02eb8742011-02-27 09:10:37 -0800262 * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and
263 * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts
Jeff Brown460a1462015-06-30 17:57:12 -0700264 * containing the {@link UsbAccessory} object for the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500265 */
266 public static final String EXTRA_ACCESSORY = "accessory";
267
Mike Lockwood3a68b832011-03-08 10:08:59 -0500268 /**
269 * Name of extra added to the {@link android.app.PendingIntent}
Mike Lockwood980f0432011-03-09 15:49:13 -0500270 * passed into {@link #requestPermission(UsbDevice, PendingIntent)}
271 * or {@link #requestPermission(UsbAccessory, PendingIntent)}
Mike Lockwood3a68b832011-03-08 10:08:59 -0500272 * containing a boolean value indicating whether the user granted permission or not.
273 */
274 public static final String EXTRA_PERMISSION_GRANTED = "permission";
275
276 private final Context mContext;
277 private final IUsbManager mService;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500278
279 /**
280 * {@hide}
281 */
Mike Lockwood3a68b832011-03-08 10:08:59 -0500282 public UsbManager(Context context, IUsbManager service) {
283 mContext = context;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500284 mService = service;
285 }
286
287 /**
288 * Returns a HashMap containing all USB devices currently attached.
289 * USB device name is the key for the returned HashMap.
290 * The result will be empty if no devices are attached, or if
291 * USB host mode is inactive or unsupported.
292 *
293 * @return HashMap containing all connected USB devices.
294 */
295 public HashMap<String,UsbDevice> getDeviceList() {
296 Bundle bundle = new Bundle();
297 try {
298 mService.getDeviceList(bundle);
299 HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>();
300 for (String name : bundle.keySet()) {
301 result.put(name, (UsbDevice)bundle.get(name));
302 }
303 return result;
304 } catch (RemoteException e) {
305 Log.e(TAG, "RemoteException in getDeviceList", e);
306 return null;
307 }
308 }
309
310 /**
311 * Opens the device so it can be used to send and receive
Mike Lockwoodc4308f02011-03-01 08:04:54 -0800312 * data using {@link android.hardware.usb.UsbRequest}.
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500313 *
314 * @param device the device to open
mike wakerly1567a432012-07-17 19:58:19 -0700315 * @return a {@link UsbDeviceConnection}, or {@code null} if open failed
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500316 */
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500317 public UsbDeviceConnection openDevice(UsbDevice device) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500318 try {
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500319 String deviceName = device.getDeviceName();
320 ParcelFileDescriptor pfd = mService.openDevice(deviceName);
321 if (pfd != null) {
322 UsbDeviceConnection connection = new UsbDeviceConnection(device);
323 boolean result = connection.open(deviceName, pfd);
324 pfd.close();
325 if (result) {
326 return connection;
327 }
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500328 }
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500329 } catch (Exception e) {
330 Log.e(TAG, "exception in UsbManager.openDevice", e);
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500331 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500332 return null;
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500333 }
334
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500335 /**
336 * Returns a list of currently attached USB accessories.
337 * (in the current implementation there can be at most one)
338 *
339 * @return list of USB accessories, or null if none are attached.
340 */
341 public UsbAccessory[] getAccessoryList() {
342 try {
343 UsbAccessory accessory = mService.getCurrentAccessory();
344 if (accessory == null) {
345 return null;
346 } else {
347 return new UsbAccessory[] { accessory };
348 }
349 } catch (RemoteException e) {
Mike Lockwood3a68b832011-03-08 10:08:59 -0500350 Log.e(TAG, "RemoteException in getAccessoryList", e);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500351 return null;
352 }
353 }
354
355 /**
356 * Opens a file descriptor for reading and writing data to the USB accessory.
357 *
358 * @param accessory the USB accessory to open
359 * @return file descriptor, or null if the accessor could not be opened.
360 */
361 public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
362 try {
Mike Lockwood02eb8742011-02-27 09:10:37 -0800363 return mService.openAccessory(accessory);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500364 } catch (RemoteException e) {
Mike Lockwood3a68b832011-03-08 10:08:59 -0500365 Log.e(TAG, "RemoteException in openAccessory", e);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500366 return null;
367 }
368 }
369
Mike Lockwood3a68b832011-03-08 10:08:59 -0500370 /**
371 * Returns true if the caller has permission to access the device.
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500372 * Permission might have been granted temporarily via
Mike Lockwood980f0432011-03-09 15:49:13 -0500373 * {@link #requestPermission(UsbDevice, PendingIntent)} or
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500374 * by the user choosing the caller as the default application for the device.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500375 *
376 * @param device to check permissions for
377 * @return true if caller has permission
378 */
379 public boolean hasPermission(UsbDevice device) {
380 try {
381 return mService.hasDevicePermission(device);
382 } catch (RemoteException e) {
383 Log.e(TAG, "RemoteException in hasPermission", e);
384 return false;
385 }
386 }
387
388 /**
389 * Returns true if the caller has permission to access the accessory.
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500390 * Permission might have been granted temporarily via
Mike Lockwood980f0432011-03-09 15:49:13 -0500391 * {@link #requestPermission(UsbAccessory, PendingIntent)} or
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500392 * by the user choosing the caller as the default application for the accessory.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500393 *
394 * @param accessory to check permissions for
395 * @return true if caller has permission
396 */
397 public boolean hasPermission(UsbAccessory accessory) {
398 try {
399 return mService.hasAccessoryPermission(accessory);
400 } catch (RemoteException e) {
401 Log.e(TAG, "RemoteException in hasPermission", e);
402 return false;
403 }
404 }
405
406 /**
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500407 * Requests temporary permission for the given package to access the device.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500408 * This may result in a system dialog being displayed to the user
409 * if permission had not already been granted.
410 * Success or failure is returned via the {@link android.app.PendingIntent} pi.
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500411 * If successful, this grants the caller permission to access the device only
412 * until the device is disconnected.
413 *
Mike Lockwood3a68b832011-03-08 10:08:59 -0500414 * The following extras will be added to pi:
415 * <ul>
416 * <li> {@link #EXTRA_DEVICE} containing the device passed into this call
417 * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether
418 * permission was granted by the user
419 * </ul>
420 *
421 * @param device to request permissions for
422 * @param pi PendingIntent for returning result
423 */
424 public void requestPermission(UsbDevice device, PendingIntent pi) {
425 try {
426 mService.requestDevicePermission(device, mContext.getPackageName(), pi);
427 } catch (RemoteException e) {
428 Log.e(TAG, "RemoteException in requestPermission", e);
429 }
430 }
431
432 /**
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500433 * Requests temporary permission for the given package to access the accessory.
Mike Lockwood3a68b832011-03-08 10:08:59 -0500434 * This may result in a system dialog being displayed to the user
435 * if permission had not already been granted.
436 * Success or failure is returned via the {@link android.app.PendingIntent} pi.
Mike Lockwood62cfeeb2011-03-11 18:39:03 -0500437 * If successful, this grants the caller permission to access the accessory only
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500438 * until the device is disconnected.
439 *
Mike Lockwood3a68b832011-03-08 10:08:59 -0500440 * The following extras will be added to pi:
441 * <ul>
442 * <li> {@link #EXTRA_ACCESSORY} containing the accessory passed into this call
443 * <li> {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether
444 * permission was granted by the user
445 * </ul>
446 *
447 * @param accessory to request permissions for
448 * @param pi PendingIntent for returning result
449 */
450 public void requestPermission(UsbAccessory accessory, PendingIntent pi) {
451 try {
452 mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi);
453 } catch (RemoteException e) {
454 Log.e(TAG, "RemoteException in requestPermission", e);
455 }
456 }
457
Mike Lockwood02e45692011-06-14 15:43:51 -0400458 /**
Daichi Hirono47518802015-12-08 09:51:19 +0900459 * Grants permission for USB device without showing system dialog.
460 * Only system components can call this function.
461 * @param device to request permissions for
462 *
463 * {@hide}
464 */
465 public void grantPermission(UsbDevice device) {
466 try {
467 mService.grantDevicePermission(device, Process.myUid());
468 } catch (RemoteException e) {
469 Log.e(TAG, "RemoteException in grantPermission", e);
470 }
471 }
472
473 /**
Jeff Brown460a1462015-06-30 17:57:12 -0700474 * Returns true if the specified USB function is currently enabled when in device mode.
475 * <p>
476 * USB functions represent interfaces which are published to the host to access
477 * services offered by the device.
478 * </p>
Mike Lockwoode51099f2011-08-02 12:54:49 -0400479 *
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700480 * @param function name of the USB function
Jeff Brown460a1462015-06-30 17:57:12 -0700481 * @return true if the USB function is enabled
Mike Lockwoode51099f2011-08-02 12:54:49 -0400482 *
483 * {@hide}
484 */
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700485 public boolean isFunctionEnabled(String function) {
Jeff Brown460a1462015-06-30 17:57:12 -0700486 try {
487 return mService.isFunctionEnabled(function);
488 } catch (RemoteException e) {
489 Log.e(TAG, "RemoteException in setCurrentFunction", e);
490 return false;
491 }
Mike Lockwoode51099f2011-08-02 12:54:49 -0400492 }
493
494 /**
Jeff Brown460a1462015-06-30 17:57:12 -0700495 * Sets the current USB function when in device mode.
496 * <p>
497 * USB functions represent interfaces which are published to the host to access
498 * services offered by the device.
499 * </p><p>
500 * This method is intended to select among primary USB functions. The system may
501 * automatically activate additional functions such as {@link #USB_FUNCTION_ADB}
502 * or {@link #USB_FUNCTION_ACCESSORY} based on other settings and states.
503 * </p><p>
504 * The allowed values are: {@link #USB_FUNCTION_NONE}, {@link #USB_FUNCTION_AUDIO_SOURCE},
505 * {@link #USB_FUNCTION_MIDI}, {@link #USB_FUNCTION_MTP}, {@link #USB_FUNCTION_PTP},
506 * or {@link #USB_FUNCTION_RNDIS}.
507 * </p><p>
508 * Note: This function is asynchronous and may fail silently without applying
509 * the requested changes.
510 * </p>
Mike Lockwood02e45692011-06-14 15:43:51 -0400511 *
Mike Lockwood875c24b2011-07-18 10:54:32 -0700512 * @param function name of the USB function, or null to restore the default function
Mike Lockwood02e45692011-06-14 15:43:51 -0400513 *
514 * {@hide}
515 */
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700516 public void setCurrentFunction(String function) {
Mike Lockwood08bff3b2010-08-31 13:27:05 -0400517 try {
Nick Kralevichfcf10f72015-05-13 11:54:03 -0700518 mService.setCurrentFunction(function);
Mike Lockwood02e45692011-06-14 15:43:51 -0400519 } catch (RemoteException e) {
Mike Lockwoodf59717d2011-06-22 15:19:33 -0400520 Log.e(TAG, "RemoteException in setCurrentFunction", e);
Mike Lockwood02e45692011-06-14 15:43:51 -0400521 }
522 }
Nick Kralevich67401902015-06-10 09:38:42 -0700523
524 /**
525 * Sets whether USB data (for example, MTP exposed pictures) should be made available
Jeff Brown460a1462015-06-30 17:57:12 -0700526 * on the USB connection when in device mode. Unlocking usb data should only be done with
527 * user involvement, since exposing pictures or other data could leak sensitive
528 * user information.
Nick Kralevich67401902015-06-10 09:38:42 -0700529 *
530 * {@hide}
531 */
532 public void setUsbDataUnlocked(boolean unlocked) {
533 try {
534 mService.setUsbDataUnlocked(unlocked);
535 } catch (RemoteException e) {
536 Log.e(TAG, "RemoteException in setUsbDataUnlocked", e);
537 }
538 }
539
540 /**
Jeff Brown76c4c662015-07-07 12:44:17 -0700541 * Returns a list of physical USB ports on the device.
542 * <p>
543 * This list is guaranteed to contain all dual-role USB Type C ports but it might
544 * be missing other ports depending on whether the kernel USB drivers have been
545 * updated to publish all of the device's ports through the new "dual_role_usb"
546 * device class (which supports all types of ports despite its name).
547 * </p>
548 *
549 * @return The list of USB ports, or null if none.
550 *
551 * @hide
552 */
553 public UsbPort[] getPorts() {
554 try {
555 return mService.getPorts();
556 } catch (RemoteException e) {
557 Log.e(TAG, "RemoteException in getPorts", e);
558 }
559 return null;
560 }
561
562 /**
563 * Gets the status of the specified USB port.
564 *
565 * @param port The port to query.
566 * @return The status of the specified USB port, or null if unknown.
567 *
568 * @hide
569 */
570 public UsbPortStatus getPortStatus(UsbPort port) {
571 Preconditions.checkNotNull(port, "port must not be null");
572
573 try {
574 return mService.getPortStatus(port.getId());
575 } catch (RemoteException e) {
576 Log.e(TAG, "RemoteException in getPortStatus", e);
577 }
578 return null;
579 }
580
581 /**
582 * Sets the desired role combination of the port.
583 * <p>
584 * The supported role combinations depend on what is connected to the port and may be
585 * determined by consulting
586 * {@link UsbPortStatus#isRoleCombinationSupported UsbPortStatus.isRoleCombinationSupported}.
587 * </p><p>
588 * Note: This function is asynchronous and may fail silently without applying
589 * the requested changes. If this function does cause a status change to occur then
590 * a {@link #ACTION_USB_PORT_CHANGED} broadcast will be sent.
591 * </p>
592 *
593 * @param powerRole The desired power role: {@link UsbPort#POWER_ROLE_SOURCE}
594 * or {@link UsbPort#POWER_ROLE_SINK}, or 0 if no power role.
595 * @param dataRole The desired data role: {@link UsbPort#DATA_ROLE_HOST}
596 * or {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if no data role.
597 *
598 * @hide
599 */
600 public void setPortRoles(UsbPort port, int powerRole, int dataRole) {
601 Preconditions.checkNotNull(port, "port must not be null");
602 UsbPort.checkRoles(powerRole, dataRole);
603
604 try {
605 mService.setPortRoles(port.getId(), powerRole, dataRole);
606 } catch (RemoteException e) {
607 Log.e(TAG, "RemoteException in setPortRole", e);
608 }
609 }
610
Jeff Brown460a1462015-06-30 17:57:12 -0700611 /** @hide */
612 public static String addFunction(String functions, String function) {
Yasuhiro Matsuda48b9a7c2015-07-16 19:00:16 +0900613 if (USB_FUNCTION_NONE.equals(functions)) {
Jeff Brown460a1462015-06-30 17:57:12 -0700614 return function;
615 }
616 if (!containsFunction(functions, function)) {
617 if (functions.length() > 0) {
618 functions += ",";
619 }
620 functions += function;
621 }
622 return functions;
623 }
624
625 /** @hide */
626 public static String removeFunction(String functions, String function) {
627 String[] split = functions.split(",");
628 for (int i = 0; i < split.length; i++) {
629 if (function.equals(split[i])) {
630 split[i] = null;
631 }
632 }
633 if (split.length == 1 && split[0] == null) {
Yasuhiro Matsuda48b9a7c2015-07-16 19:00:16 +0900634 return USB_FUNCTION_NONE;
Jeff Brown460a1462015-06-30 17:57:12 -0700635 }
636 StringBuilder builder = new StringBuilder();
637 for (int i = 0; i < split.length; i++) {
638 String s = split[i];
639 if (s != null) {
640 if (builder.length() > 0) {
641 builder.append(",");
642 }
643 builder.append(s);
644 }
645 }
646 return builder.toString();
647 }
648
649 /** @hide */
650 public static boolean containsFunction(String functions, String function) {
651 int index = functions.indexOf(function);
652 if (index < 0) return false;
653 if (index > 0 && functions.charAt(index - 1) != ',') return false;
654 int charAfter = index + function.length();
655 if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false;
656 return true;
657 }
Mike Lockwood24236072010-06-23 17:36:36 -0400658}