allow external USB host management

- Setting config_UsbDeviceConnectionHandling_component leads into
  launching specified Activity whenever USB device is connected.
- This allows external Activity to manage USB device based on
  its own setup and settings.
- Device access can be passed to other app with permission update
  by UsbManager.grantPermission.
- added UsbDeviceConnection.resetDevice() to reset USB device connected.
  This is necessary to get device out from AOAP.
- Test requires installing UsbHostExternalManagmentTestApp and
  AoapTestHost to USB host, and AoapTestDevice to USB Device.

bug: 26404209
Change-Id: I8e77ddc646c15454d9b2ecf1356924cf6351fc28
This commit is contained in:
Keun-young Park
2016-01-05 13:27:29 -08:00
committed by Vitalii Tomkiv
parent d3ca5980f7
commit badbbae6fa
28 changed files with 1648 additions and 7 deletions

View File

@@ -17,6 +17,7 @@
package android.hardware.usb;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbPort;
@@ -116,4 +117,7 @@ interface IUsbManager
/* Sets the port's current role. */
void setPortRoles(in String portId, int powerRole, int dataRole);
/* Sets USB device connection handler. */
void setUsbDeviceConnectionHandler(in ComponentName usbDeviceConnectionHandler);
}

View File

@@ -16,6 +16,7 @@
package android.hardware.usb;
import android.annotation.SystemApi;
import android.os.ParcelFileDescriptor;
import java.io.FileDescriptor;
@@ -214,9 +215,21 @@ public class UsbDeviceConnection {
return native_bulk_request(endpoint.getAddress(), buffer, offset, length, timeout);
}
/**
* Reset USB port for the connected device.
*
* @return true if reset succeeds.
*
* @hide
*/
@SystemApi
public boolean resetDevice() {
return native_reset_device();
}
/**
* Waits for the result of a {@link android.hardware.usb.UsbRequest#queue} operation
* Note that this may return requests queued on multiple
* Note that this may return requests queued on multiple
* {@link android.hardware.usb.UsbEndpoint}s.
* When multiple endpoints are in use, {@link android.hardware.usb.UsbRequest#getEndpoint} and
* {@link android.hardware.usb.UsbRequest#getClientData} can be useful in determining
@@ -263,4 +276,5 @@ public class UsbDeviceConnection {
int offset, int length, int timeout);
private native UsbRequest native_request_wait();
private native String native_get_serial();
private native boolean native_reset_device();
}

View File

@@ -19,7 +19,9 @@ package android.hardware.usb;
import com.android.internal.util.Preconditions;
import android.annotation.Nullable;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
@@ -469,8 +471,20 @@ public class UsbManager {
* {@hide}
*/
public void grantPermission(UsbDevice device) {
grantPermission(device, Process.myUid());
}
/**
* Grants permission for USB device to given uid without showing system dialog.
* Only system components can call this function.
* @param device to request permissions for
* @uid uid to give permission
*
* {@hide}
*/
public void grantPermission(UsbDevice device, int uid) {
try {
mService.grantDevicePermission(device, Process.myUid());
mService.grantDevicePermission(device, uid);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
@@ -488,11 +502,9 @@ public class UsbManager {
try {
int uid = mContext.getPackageManager()
.getPackageUidAsUser(packageName, mContext.getUserId());
mService.grantDevicePermission(device, uid);
grantPermission(device, uid);
} catch (NameNotFoundException e) {
Log.e(TAG, "Package " + packageName + " not found.", e);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
@@ -631,6 +643,26 @@ public class UsbManager {
}
}
/**
* Sets the component that will handle USB device connection.
* <p>
* Setting component allows to specify external USB host manager to handle use cases, where
* selection dialog for an activity that will handle USB device is undesirable.
* Only system components can call this function, as it requires the MANAGE_USB permission.
*
* @param usbDeviceConnectionHandler The component to handle usb connections,
* {@code null} to unset.
*
* {@hide}
*/
public void setUsbDeviceConnectionHandler(@Nullable ComponentName usbDeviceConnectionHandler) {
try {
mService.setUsbDeviceConnectionHandler(usbDeviceConnectionHandler);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide */
public static String addFunction(String functions, String function) {
if (USB_FUNCTION_NONE.equals(functions)) {