Loading cmds/svc/src/com/android/commands/svc/UsbCommand.java +9 −1 Original line number Diff line number Diff line Loading @@ -29,12 +29,18 @@ import android.os.ServiceManager; import java.util.function.Consumer; import java.util.concurrent.Executor; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class UsbCommand extends Svc.Command { public UsbCommand() { super("usb"); } /** * Counter for tracking UsbOperation operations. */ private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); @Override public String shortHelp() { return "Control Usb state"; Loading Loading @@ -92,8 +98,10 @@ public class UsbCommand extends Svc.Command { if ("setFunctions".equals(args[1])) { try { int operationId = sUsbOperationCount.incrementAndGet(); System.out.println("setCurrentFunctions opId:" + operationId); usbMgr.setCurrentFunctions(UsbManager.usbFunctionsFromString( args.length >= 3 ? args[2] : "")); args.length >= 3 ? args[2] : ""), operationId); } catch (RemoteException e) { System.err.println("Error communicating with UsbManager: " + e); } Loading core/api/module-lib-current.txt +1 −0 Original line number Diff line number Diff line Loading @@ -120,6 +120,7 @@ package android.hardware.usb { field public static final int GADGET_HAL_V1_0 = 10; // 0xa field public static final int GADGET_HAL_V1_1 = 11; // 0xb field public static final int GADGET_HAL_V1_2 = 12; // 0xc field public static final int GADGET_HAL_V2_0 = 20; // 0x14 field public static final int USB_DATA_TRANSFER_RATE_10G = 10240; // 0x2800 field public static final int USB_DATA_TRANSFER_RATE_20G = 20480; // 0x5000 field public static final int USB_DATA_TRANSFER_RATE_40G = 40960; // 0xa000 Loading core/java/android/hardware/usb/IUsbManager.aidl +2 −2 Original line number Diff line number Diff line Loading @@ -111,10 +111,10 @@ interface IUsbManager boolean isFunctionEnabled(String function); /* Sets the current USB function. */ void setCurrentFunctions(long functions); void setCurrentFunctions(long functions, int operationId); /* Compatibility version of setCurrentFunctions(long). */ void setCurrentFunction(String function, boolean usbDataUnlocked); void setCurrentFunction(String function, boolean usbDataUnlocked, int operationId); /* Gets the current USB functions. */ long getCurrentFunctions(); Loading core/java/android/hardware/usb/UsbManager.java +103 −74 Original line number Diff line number Diff line Loading @@ -38,6 +38,7 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.hardware.usb.gadget.V1_0.GadgetFunction; import android.hardware.usb.gadget.V1_2.UsbSpeed; import android.os.Binder; import android.os.Build; import android.os.Bundle; import android.os.ParcelFileDescriptor; Loading @@ -52,6 +53,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.StringJoiner; import java.util.concurrent.atomic.AtomicInteger; /** * This class allows you to access the state of USB and communicate with USB devices. Loading Loading @@ -95,7 +97,7 @@ public class UsbManager { * If the sticky intent has not been found, that indicates USB is disconnected, * USB is not configued, MTP function is enabled, and all the other functions are disabled. * * {@hide} * @hide */ @SystemApi public static final String ACTION_USB_STATE = Loading Loading @@ -172,7 +174,7 @@ public class UsbManager { * <p>For more information about communicating with USB accessory handshake, refer to * <a href="https://source.android.com/devices/accessories/aoa">AOA</a> developer guide.</p> * * {@hide} * @hide */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) @SystemApi Loading @@ -184,7 +186,7 @@ public class UsbManager { * Boolean extra indicating whether USB is connected or disconnected. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @SystemApi public static final String USB_CONNECTED = "connected"; Loading @@ -193,7 +195,7 @@ public class UsbManager { * Boolean extra indicating whether USB is connected or disconnected as host. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ public static final String USB_HOST_CONNECTED = "host_connected"; Loading @@ -201,7 +203,7 @@ public class UsbManager { * Boolean extra indicating whether USB is configured. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @SystemApi public static final String USB_CONFIGURED = "configured"; Loading @@ -212,7 +214,7 @@ public class UsbManager { * has explicitly asked for this data to be unlocked. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public static final String USB_DATA_UNLOCKED = "unlocked"; Loading @@ -221,7 +223,7 @@ public class UsbManager { * A placeholder indicating that no USB function is being specified. * Used for compatibility with old init scripts to indicate no functions vs. charging function. * * {@hide} * @hide */ @UnsupportedAppUsage public static final String USB_FUNCTION_NONE = "none"; Loading @@ -230,7 +232,7 @@ public class UsbManager { * Name of the adb USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_ADB = "adb"; Loading @@ -238,7 +240,7 @@ public class UsbManager { * Name of the RNDIS ethernet USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ @SystemApi public static final String USB_FUNCTION_RNDIS = "rndis"; Loading @@ -247,7 +249,7 @@ public class UsbManager { * Name of the MTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_MTP = "mtp"; Loading @@ -255,7 +257,7 @@ public class UsbManager { * Name of the PTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_PTP = "ptp"; Loading @@ -263,7 +265,7 @@ public class UsbManager { * Name of the audio source USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; Loading @@ -271,7 +273,7 @@ public class UsbManager { * Name of the MIDI USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_MIDI = "midi"; Loading @@ -279,7 +281,7 @@ public class UsbManager { * Name of the Accessory USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_ACCESSORY = "accessory"; Loading @@ -287,7 +289,7 @@ public class UsbManager { * Name of the NCM USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ @SystemApi public static final String USB_FUNCTION_NCM = "ncm"; Loading @@ -295,31 +297,38 @@ public class UsbManager { /** * Name of Gadget Hal Not Present; * * {@hide} * @hide */ public static final String GADGET_HAL_UNKNOWN = "unknown"; /** * Name of the USB Gadget Hal Version v1.0; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_0 = "V1_0"; /** * Name of the USB Gadget Hal Version v1.1; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_1 = "V1_1"; /** * Name of the USB Gadget Hal Version v1.2; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_2 = "V1_2"; /** * Name of the USB Gadget Hal Version v2.0; * * @hide */ public static final String GADGET_HAL_VERSION_2_0 = "V2_0"; /** * Name of extra for {@link #ACTION_USB_PORT_CHANGED} * containing the {@link UsbPort} object for the port. Loading Loading @@ -356,7 +365,7 @@ public class UsbManager { * This is obtained with SystemClock.elapsedRealtime() * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_UEVENT_TIME = Loading @@ -370,7 +379,7 @@ public class UsbManager { * between communicating with USB accessory handshake, refer to * <a href="https://source.android.com/devices/accessories/aoa">AOA</a> developer guide.</p> * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_STRING_COUNT = Loading @@ -380,7 +389,7 @@ public class UsbManager { * Boolean extra indicating whether got start accessory or not * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_START = Loading @@ -392,7 +401,7 @@ public class UsbManager { * sending {@link #ACTION_USB_ACCESSORY_HANDSHAKE}. * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_HANDSHAKE_END = Loading Loading @@ -426,7 +435,7 @@ public class UsbManager { /** * The Value for USB gadget hal is not presented. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_NOT_SUPPORTED = -1; Loading @@ -434,7 +443,7 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.0. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_0 = 10; Loading @@ -442,7 +451,7 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.1. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_1 = 11; Loading @@ -450,15 +459,23 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.2. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_2 = 12; /** * Value for Gadget Hal Version v2.0. * * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V2_0 = 20; /** * Value for USB_STATE is not configured. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1; Loading @@ -466,7 +483,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Low Speed in Mbps (real value is 1.5Mbps). * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2; Loading @@ -474,7 +491,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Full Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12; Loading @@ -482,7 +499,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of High Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480; Loading @@ -490,7 +507,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Super Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_5G = 5 * 1024; Loading @@ -498,7 +515,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 10G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_10G = 10 * 1024; Loading @@ -506,7 +523,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 20G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_20G = 20 * 1024; Loading @@ -514,7 +531,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 40G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_40G = 40 * 1024; Loading @@ -530,7 +547,7 @@ public class UsbManager { /** * The Value for USB hal is not presented. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_NOT_SUPPORTED = -1; Loading @@ -538,7 +555,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.0. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_0 = 10; Loading @@ -546,7 +563,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.1. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_1 = 11; Loading @@ -554,7 +571,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.2. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_2 = 12; Loading @@ -562,7 +579,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.3. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_3 = 13; Loading @@ -577,63 +594,63 @@ public class UsbManager { /** * Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_NONE = 0; /** * Code for the mtp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_MTP = GadgetFunction.MTP; /** * Code for the ptp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_PTP = GadgetFunction.PTP; /** * Code for the rndis usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_RNDIS = GadgetFunction.RNDIS; /** * Code for the midi usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_MIDI = GadgetFunction.MIDI; /** * Code for the accessory usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_ACCESSORY = GadgetFunction.ACCESSORY; /** * Code for the audio source usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_AUDIO_SOURCE = GadgetFunction.AUDIO_SOURCE; /** * Code for the adb usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_ADB = GadgetFunction.ADB; /** * Code for the ncm source usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_NCM = 1 << 10; Loading @@ -643,6 +660,11 @@ public class UsbManager { private static final Map<String, Long> FUNCTION_NAME_TO_CODE = new HashMap<>(); /** * Counter for tracking UsbOperation operations. */ private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); static { FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_MTP, FUNCTION_MTP); FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_PTP, FUNCTION_PTP); Loading Loading @@ -674,6 +696,7 @@ public class UsbManager { GADGET_HAL_V1_0, GADGET_HAL_V1_1, GADGET_HAL_V1_2, GADGET_HAL_V2_0, }) public @interface UsbGadgetHalVersion {} Loading @@ -692,7 +715,7 @@ public class UsbManager { private final IUsbManager mService; /** * {@hide} * @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public UsbManager(Context context, IUsbManager service) { Loading Loading @@ -803,7 +826,7 @@ public class UsbManager { * {@link #FUNCTION_PTP} are supported. * @return A ParcelFileDescriptor holding the valid fd, or null if the fd was not found. * * {@hide} * @hide */ public ParcelFileDescriptor getControlFd(long function) { try { Loading Loading @@ -921,7 +944,7 @@ public class UsbManager { * Only system components can call this function. * @param device to request permissions for * * {@hide} * @hide */ public void grantPermission(UsbDevice device) { grantPermission(device, Process.myUid()); Loading @@ -933,7 +956,7 @@ public class UsbManager { * @param device to request permissions for * @uid uid to give permission * * {@hide} * @hide */ public void grantPermission(UsbDevice device, int uid) { try { Loading @@ -949,7 +972,7 @@ public class UsbManager { * @param device to request permissions for * @param packageName of package to grant permissions * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -974,7 +997,7 @@ public class UsbManager { * @param function name of the USB function * @return true if the USB function is enabled * * {@hide} * @hide */ @Deprecated @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) Loading Loading @@ -1006,14 +1029,17 @@ public class UsbManager { * @param functions the USB function(s) to set, as a bitwise mask. * Must satisfy {@link UsbManager#areSettableFunctions} * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) public void setCurrentFunctions(@UsbFunctionMode long functions) { int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); try { mService.setCurrentFunctions(functions); mService.setCurrentFunctions(functions, operationId); } catch (RemoteException e) { Log.e(TAG, "setCurrentFunctions: failed to call setCurrentFunctions. functions:" + functions + ", opId:" + operationId, e); throw e.rethrowFromSystemServer(); } } Loading @@ -1025,14 +1051,17 @@ public class UsbManager { * @param functions the USB function(s) to set. * @param usbDataUnlocked unused * {@hide} * @hide */ @Deprecated @UnsupportedAppUsage public void setCurrentFunction(String functions, boolean usbDataUnlocked) { int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); try { mService.setCurrentFunction(functions, usbDataUnlocked); mService.setCurrentFunction(functions, usbDataUnlocked, operationId); } catch (RemoteException e) { Log.e(TAG, "setCurrentFunction: failed to call setCurrentFunction. functions:" + functions + ", opId:" + operationId, e); throw e.rethrowFromSystemServer(); } } Loading @@ -1047,7 +1076,7 @@ public class UsbManager { * @return The currently enabled functions, in a bitwise mask. * A zero mask indicates that the current function is the charging function. * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -1073,7 +1102,7 @@ public class UsbManager { * @param functions functions to set, in a bitwise mask. * Must satisfy {@link UsbManager#areSettableFunctions} * * {@hide} * @hide */ public void setScreenUnlockedFunctions(long functions) { try { Loading @@ -1089,7 +1118,7 @@ public class UsbManager { * @return The currently set screen enabled functions. * A zero mask indicates that the screen unlocked functions feature is not enabled. * * {@hide} * @hide */ public long getScreenUnlockedFunctions() { try { Loading @@ -1111,19 +1140,17 @@ public class UsbManager { * * @return The value of currently USB Bandwidth. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) public int getUsbBandwidthMbps() { int usbSpeed; try { usbSpeed = mService.getCurrentUsbSpeed(); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } return usbSpeedToBandwidth(usbSpeed); } Loading @@ -1135,7 +1162,7 @@ public class UsbManager { * * @return a integer {@code GADGET_HAL_*} represent hal version. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -1155,7 +1182,7 @@ public class UsbManager { * * @return a integer {@code USB_HAL_*} represent hal version. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) Loading Loading @@ -1451,7 +1478,7 @@ public class UsbManager { * @param usbDeviceConnectionHandler The component to handle usb connections, * {@code null} to unset. * * {@hide} * @hide */ public void setUsbDeviceConnectionHandler(@Nullable ComponentName usbDeviceConnectionHandler) { try { Loading @@ -1470,7 +1497,7 @@ public class UsbManager { * * @return Whether the mask is settable. * * {@hide} * @hide */ public static boolean areSettableFunctions(long functions) { return functions == FUNCTION_NONE Loading @@ -1484,7 +1511,7 @@ public class UsbManager { * * @return String representation of given mask * * {@hide} * @hide */ public static String usbFunctionsToString(long functions) { StringJoiner joiner = new StringJoiner(","); Loading Loading @@ -1520,7 +1547,7 @@ public class UsbManager { * * @return A mask of all valid functions in the string * * {@hide} * @hide */ public static long usbFunctionsFromString(String functions) { if (functions == null || functions.equals(USB_FUNCTION_NONE)) { Loading @@ -1542,7 +1569,7 @@ public class UsbManager { * * @return a value of USB bandwidth * * {@hide} * @hide */ public static int usbSpeedToBandwidth(int speed) { switch (speed) { Loading Loading @@ -1576,12 +1603,14 @@ public class UsbManager { * * @return String representation of Usb Gadget Hal Version * * {@hide} * @hide */ public static @NonNull String usbGadgetHalVersionToString(int version) { String halVersion; if (version == GADGET_HAL_V1_2) { if (version == GADGET_HAL_V2_0) { halVersion = GADGET_HAL_VERSION_2_0; } else if (version == GADGET_HAL_V1_2) { halVersion = GADGET_HAL_VERSION_1_2; } else if (version == GADGET_HAL_V1_1) { halVersion = GADGET_HAL_VERSION_1_1; Loading services/usb/Android.bp +2 −1 Original line number Diff line number Diff line Loading @@ -33,5 +33,6 @@ java_library_static { "android.hardware.usb.gadget-V1.0-java", "android.hardware.usb.gadget-V1.1-java", "android.hardware.usb.gadget-V1.2-java", "android.hardware.usb.gadget-V1-java", ], } Loading
cmds/svc/src/com/android/commands/svc/UsbCommand.java +9 −1 Original line number Diff line number Diff line Loading @@ -29,12 +29,18 @@ import android.os.ServiceManager; import java.util.function.Consumer; import java.util.concurrent.Executor; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class UsbCommand extends Svc.Command { public UsbCommand() { super("usb"); } /** * Counter for tracking UsbOperation operations. */ private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); @Override public String shortHelp() { return "Control Usb state"; Loading Loading @@ -92,8 +98,10 @@ public class UsbCommand extends Svc.Command { if ("setFunctions".equals(args[1])) { try { int operationId = sUsbOperationCount.incrementAndGet(); System.out.println("setCurrentFunctions opId:" + operationId); usbMgr.setCurrentFunctions(UsbManager.usbFunctionsFromString( args.length >= 3 ? args[2] : "")); args.length >= 3 ? args[2] : ""), operationId); } catch (RemoteException e) { System.err.println("Error communicating with UsbManager: " + e); } Loading
core/api/module-lib-current.txt +1 −0 Original line number Diff line number Diff line Loading @@ -120,6 +120,7 @@ package android.hardware.usb { field public static final int GADGET_HAL_V1_0 = 10; // 0xa field public static final int GADGET_HAL_V1_1 = 11; // 0xb field public static final int GADGET_HAL_V1_2 = 12; // 0xc field public static final int GADGET_HAL_V2_0 = 20; // 0x14 field public static final int USB_DATA_TRANSFER_RATE_10G = 10240; // 0x2800 field public static final int USB_DATA_TRANSFER_RATE_20G = 20480; // 0x5000 field public static final int USB_DATA_TRANSFER_RATE_40G = 40960; // 0xa000 Loading
core/java/android/hardware/usb/IUsbManager.aidl +2 −2 Original line number Diff line number Diff line Loading @@ -111,10 +111,10 @@ interface IUsbManager boolean isFunctionEnabled(String function); /* Sets the current USB function. */ void setCurrentFunctions(long functions); void setCurrentFunctions(long functions, int operationId); /* Compatibility version of setCurrentFunctions(long). */ void setCurrentFunction(String function, boolean usbDataUnlocked); void setCurrentFunction(String function, boolean usbDataUnlocked, int operationId); /* Gets the current USB functions. */ long getCurrentFunctions(); Loading
core/java/android/hardware/usb/UsbManager.java +103 −74 Original line number Diff line number Diff line Loading @@ -38,6 +38,7 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.hardware.usb.gadget.V1_0.GadgetFunction; import android.hardware.usb.gadget.V1_2.UsbSpeed; import android.os.Binder; import android.os.Build; import android.os.Bundle; import android.os.ParcelFileDescriptor; Loading @@ -52,6 +53,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.StringJoiner; import java.util.concurrent.atomic.AtomicInteger; /** * This class allows you to access the state of USB and communicate with USB devices. Loading Loading @@ -95,7 +97,7 @@ public class UsbManager { * If the sticky intent has not been found, that indicates USB is disconnected, * USB is not configued, MTP function is enabled, and all the other functions are disabled. * * {@hide} * @hide */ @SystemApi public static final String ACTION_USB_STATE = Loading Loading @@ -172,7 +174,7 @@ public class UsbManager { * <p>For more information about communicating with USB accessory handshake, refer to * <a href="https://source.android.com/devices/accessories/aoa">AOA</a> developer guide.</p> * * {@hide} * @hide */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) @SystemApi Loading @@ -184,7 +186,7 @@ public class UsbManager { * Boolean extra indicating whether USB is connected or disconnected. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @SystemApi public static final String USB_CONNECTED = "connected"; Loading @@ -193,7 +195,7 @@ public class UsbManager { * Boolean extra indicating whether USB is connected or disconnected as host. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ public static final String USB_HOST_CONNECTED = "host_connected"; Loading @@ -201,7 +203,7 @@ public class UsbManager { * Boolean extra indicating whether USB is configured. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @SystemApi public static final String USB_CONFIGURED = "configured"; Loading @@ -212,7 +214,7 @@ public class UsbManager { * has explicitly asked for this data to be unlocked. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} * @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public static final String USB_DATA_UNLOCKED = "unlocked"; Loading @@ -221,7 +223,7 @@ public class UsbManager { * A placeholder indicating that no USB function is being specified. * Used for compatibility with old init scripts to indicate no functions vs. charging function. * * {@hide} * @hide */ @UnsupportedAppUsage public static final String USB_FUNCTION_NONE = "none"; Loading @@ -230,7 +232,7 @@ public class UsbManager { * Name of the adb USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_ADB = "adb"; Loading @@ -238,7 +240,7 @@ public class UsbManager { * Name of the RNDIS ethernet USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ @SystemApi public static final String USB_FUNCTION_RNDIS = "rndis"; Loading @@ -247,7 +249,7 @@ public class UsbManager { * Name of the MTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_MTP = "mtp"; Loading @@ -255,7 +257,7 @@ public class UsbManager { * Name of the PTP USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_PTP = "ptp"; Loading @@ -263,7 +265,7 @@ public class UsbManager { * Name of the audio source USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source"; Loading @@ -271,7 +273,7 @@ public class UsbManager { * Name of the MIDI USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_MIDI = "midi"; Loading @@ -279,7 +281,7 @@ public class UsbManager { * Name of the Accessory USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ public static final String USB_FUNCTION_ACCESSORY = "accessory"; Loading @@ -287,7 +289,7 @@ public class UsbManager { * Name of the NCM USB function. * Used in extras for the {@link #ACTION_USB_STATE} broadcast * * {@hide} * @hide */ @SystemApi public static final String USB_FUNCTION_NCM = "ncm"; Loading @@ -295,31 +297,38 @@ public class UsbManager { /** * Name of Gadget Hal Not Present; * * {@hide} * @hide */ public static final String GADGET_HAL_UNKNOWN = "unknown"; /** * Name of the USB Gadget Hal Version v1.0; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_0 = "V1_0"; /** * Name of the USB Gadget Hal Version v1.1; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_1 = "V1_1"; /** * Name of the USB Gadget Hal Version v1.2; * * {@hide} * @hide */ public static final String GADGET_HAL_VERSION_1_2 = "V1_2"; /** * Name of the USB Gadget Hal Version v2.0; * * @hide */ public static final String GADGET_HAL_VERSION_2_0 = "V2_0"; /** * Name of extra for {@link #ACTION_USB_PORT_CHANGED} * containing the {@link UsbPort} object for the port. Loading Loading @@ -356,7 +365,7 @@ public class UsbManager { * This is obtained with SystemClock.elapsedRealtime() * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_UEVENT_TIME = Loading @@ -370,7 +379,7 @@ public class UsbManager { * between communicating with USB accessory handshake, refer to * <a href="https://source.android.com/devices/accessories/aoa">AOA</a> developer guide.</p> * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_STRING_COUNT = Loading @@ -380,7 +389,7 @@ public class UsbManager { * Boolean extra indicating whether got start accessory or not * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_START = Loading @@ -392,7 +401,7 @@ public class UsbManager { * sending {@link #ACTION_USB_ACCESSORY_HANDSHAKE}. * Used in extras for {@link #ACTION_USB_ACCESSORY_HANDSHAKE} broadcasts. * * {@hide} * @hide */ @SystemApi public static final String EXTRA_ACCESSORY_HANDSHAKE_END = Loading Loading @@ -426,7 +435,7 @@ public class UsbManager { /** * The Value for USB gadget hal is not presented. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_NOT_SUPPORTED = -1; Loading @@ -434,7 +443,7 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.0. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_0 = 10; Loading @@ -442,7 +451,7 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.1. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_1 = 11; Loading @@ -450,15 +459,23 @@ public class UsbManager { /** * Value for Gadget Hal Version v1.2. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V1_2 = 12; /** * Value for Gadget Hal Version v2.0. * * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int GADGET_HAL_V2_0 = 20; /** * Value for USB_STATE is not configured. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1; Loading @@ -466,7 +483,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Low Speed in Mbps (real value is 1.5Mbps). * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2; Loading @@ -474,7 +491,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Full Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12; Loading @@ -482,7 +499,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of High Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480; Loading @@ -490,7 +507,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of Super Speed in Mbps. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_5G = 5 * 1024; Loading @@ -498,7 +515,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 10G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_10G = 10 * 1024; Loading @@ -506,7 +523,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 20G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_20G = 20 * 1024; Loading @@ -514,7 +531,7 @@ public class UsbManager { /** * Value for USB Transfer Rate of 40G. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_DATA_TRANSFER_RATE_40G = 40 * 1024; Loading @@ -530,7 +547,7 @@ public class UsbManager { /** * The Value for USB hal is not presented. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_NOT_SUPPORTED = -1; Loading @@ -538,7 +555,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.0. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_0 = 10; Loading @@ -546,7 +563,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.1. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_1 = 11; Loading @@ -554,7 +571,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.2. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_2 = 12; Loading @@ -562,7 +579,7 @@ public class UsbManager { /** * Value for USB Hal Version v1.3. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) public static final int USB_HAL_V1_3 = 13; Loading @@ -577,63 +594,63 @@ public class UsbManager { /** * Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_NONE = 0; /** * Code for the mtp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_MTP = GadgetFunction.MTP; /** * Code for the ptp usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_PTP = GadgetFunction.PTP; /** * Code for the rndis usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_RNDIS = GadgetFunction.RNDIS; /** * Code for the midi usb function. Passed as a mask into {@link #setCurrentFunctions(long)} * {@hide} * @hide */ @SystemApi public static final long FUNCTION_MIDI = GadgetFunction.MIDI; /** * Code for the accessory usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_ACCESSORY = GadgetFunction.ACCESSORY; /** * Code for the audio source usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_AUDIO_SOURCE = GadgetFunction.AUDIO_SOURCE; /** * Code for the adb usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_ADB = GadgetFunction.ADB; /** * Code for the ncm source usb function. * {@hide} * @hide */ @SystemApi public static final long FUNCTION_NCM = 1 << 10; Loading @@ -643,6 +660,11 @@ public class UsbManager { private static final Map<String, Long> FUNCTION_NAME_TO_CODE = new HashMap<>(); /** * Counter for tracking UsbOperation operations. */ private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); static { FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_MTP, FUNCTION_MTP); FUNCTION_NAME_TO_CODE.put(UsbManager.USB_FUNCTION_PTP, FUNCTION_PTP); Loading Loading @@ -674,6 +696,7 @@ public class UsbManager { GADGET_HAL_V1_0, GADGET_HAL_V1_1, GADGET_HAL_V1_2, GADGET_HAL_V2_0, }) public @interface UsbGadgetHalVersion {} Loading @@ -692,7 +715,7 @@ public class UsbManager { private final IUsbManager mService; /** * {@hide} * @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public UsbManager(Context context, IUsbManager service) { Loading Loading @@ -803,7 +826,7 @@ public class UsbManager { * {@link #FUNCTION_PTP} are supported. * @return A ParcelFileDescriptor holding the valid fd, or null if the fd was not found. * * {@hide} * @hide */ public ParcelFileDescriptor getControlFd(long function) { try { Loading Loading @@ -921,7 +944,7 @@ public class UsbManager { * Only system components can call this function. * @param device to request permissions for * * {@hide} * @hide */ public void grantPermission(UsbDevice device) { grantPermission(device, Process.myUid()); Loading @@ -933,7 +956,7 @@ public class UsbManager { * @param device to request permissions for * @uid uid to give permission * * {@hide} * @hide */ public void grantPermission(UsbDevice device, int uid) { try { Loading @@ -949,7 +972,7 @@ public class UsbManager { * @param device to request permissions for * @param packageName of package to grant permissions * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -974,7 +997,7 @@ public class UsbManager { * @param function name of the USB function * @return true if the USB function is enabled * * {@hide} * @hide */ @Deprecated @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) Loading Loading @@ -1006,14 +1029,17 @@ public class UsbManager { * @param functions the USB function(s) to set, as a bitwise mask. * Must satisfy {@link UsbManager#areSettableFunctions} * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) public void setCurrentFunctions(@UsbFunctionMode long functions) { int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); try { mService.setCurrentFunctions(functions); mService.setCurrentFunctions(functions, operationId); } catch (RemoteException e) { Log.e(TAG, "setCurrentFunctions: failed to call setCurrentFunctions. functions:" + functions + ", opId:" + operationId, e); throw e.rethrowFromSystemServer(); } } Loading @@ -1025,14 +1051,17 @@ public class UsbManager { * @param functions the USB function(s) to set. * @param usbDataUnlocked unused * {@hide} * @hide */ @Deprecated @UnsupportedAppUsage public void setCurrentFunction(String functions, boolean usbDataUnlocked) { int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); try { mService.setCurrentFunction(functions, usbDataUnlocked); mService.setCurrentFunction(functions, usbDataUnlocked, operationId); } catch (RemoteException e) { Log.e(TAG, "setCurrentFunction: failed to call setCurrentFunction. functions:" + functions + ", opId:" + operationId, e); throw e.rethrowFromSystemServer(); } } Loading @@ -1047,7 +1076,7 @@ public class UsbManager { * @return The currently enabled functions, in a bitwise mask. * A zero mask indicates that the current function is the charging function. * * {@hide} * @hide */ @SystemApi @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -1073,7 +1102,7 @@ public class UsbManager { * @param functions functions to set, in a bitwise mask. * Must satisfy {@link UsbManager#areSettableFunctions} * * {@hide} * @hide */ public void setScreenUnlockedFunctions(long functions) { try { Loading @@ -1089,7 +1118,7 @@ public class UsbManager { * @return The currently set screen enabled functions. * A zero mask indicates that the screen unlocked functions feature is not enabled. * * {@hide} * @hide */ public long getScreenUnlockedFunctions() { try { Loading @@ -1111,19 +1140,17 @@ public class UsbManager { * * @return The value of currently USB Bandwidth. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) public int getUsbBandwidthMbps() { int usbSpeed; try { usbSpeed = mService.getCurrentUsbSpeed(); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } return usbSpeedToBandwidth(usbSpeed); } Loading @@ -1135,7 +1162,7 @@ public class UsbManager { * * @return a integer {@code GADGET_HAL_*} represent hal version. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) Loading @@ -1155,7 +1182,7 @@ public class UsbManager { * * @return a integer {@code USB_HAL_*} represent hal version. * * {@hide} * @hide */ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @RequiresPermission(Manifest.permission.MANAGE_USB) Loading Loading @@ -1451,7 +1478,7 @@ public class UsbManager { * @param usbDeviceConnectionHandler The component to handle usb connections, * {@code null} to unset. * * {@hide} * @hide */ public void setUsbDeviceConnectionHandler(@Nullable ComponentName usbDeviceConnectionHandler) { try { Loading @@ -1470,7 +1497,7 @@ public class UsbManager { * * @return Whether the mask is settable. * * {@hide} * @hide */ public static boolean areSettableFunctions(long functions) { return functions == FUNCTION_NONE Loading @@ -1484,7 +1511,7 @@ public class UsbManager { * * @return String representation of given mask * * {@hide} * @hide */ public static String usbFunctionsToString(long functions) { StringJoiner joiner = new StringJoiner(","); Loading Loading @@ -1520,7 +1547,7 @@ public class UsbManager { * * @return A mask of all valid functions in the string * * {@hide} * @hide */ public static long usbFunctionsFromString(String functions) { if (functions == null || functions.equals(USB_FUNCTION_NONE)) { Loading @@ -1542,7 +1569,7 @@ public class UsbManager { * * @return a value of USB bandwidth * * {@hide} * @hide */ public static int usbSpeedToBandwidth(int speed) { switch (speed) { Loading Loading @@ -1576,12 +1603,14 @@ public class UsbManager { * * @return String representation of Usb Gadget Hal Version * * {@hide} * @hide */ public static @NonNull String usbGadgetHalVersionToString(int version) { String halVersion; if (version == GADGET_HAL_V1_2) { if (version == GADGET_HAL_V2_0) { halVersion = GADGET_HAL_VERSION_2_0; } else if (version == GADGET_HAL_V1_2) { halVersion = GADGET_HAL_VERSION_1_2; } else if (version == GADGET_HAL_V1_1) { halVersion = GADGET_HAL_VERSION_1_1; Loading
services/usb/Android.bp +2 −1 Original line number Diff line number Diff line Loading @@ -33,5 +33,6 @@ java_library_static { "android.hardware.usb.gadget-V1.0-java", "android.hardware.usb.gadget-V1.1-java", "android.hardware.usb.gadget-V1.2-java", "android.hardware.usb.gadget-V1-java", ], }