blob: 5255f165b92b91db8186459c1e6756a1bada7d60 [file] [log] [blame]
Kevin Crossand4285492016-11-28 18:40:43 -08001package android.car.usb.handler;
2
3import android.content.BroadcastReceiver;
4import android.content.Context;
5import android.content.Intent;
6import android.hardware.usb.UsbDevice;
7import android.hardware.usb.UsbDeviceConnection;
8import android.hardware.usb.UsbManager;
9
10public class BootUsbScanner extends BroadcastReceiver {
11 @Override
12 public void onReceive(Context context, Intent intent) {
13 // TODO: move probing of devices to a service, since AoapInterface.isSupported() could take
14 // up to 2 seconds and many USB devices could be connected.
15 UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
16 for (UsbDevice device : manager.getDeviceList().values()) {
17 if (AoapInterface.isDeviceInAoapMode(device)) {
18 // This could happen if we reboot. We should try to handle this accessory.
19 handle(context, device);
20 } else {
21 UsbDeviceConnection connection = UsbUtil.openConnection(manager, device);
22 try {
23 if (AoapInterface.isSupported(connection)) {
24 handle(context, device);
25 }
26 } finally {
27 connection.close();
28 }
29 }
30 }
31 }
32
33 private void handle(Context context, UsbDevice device) {
34 Intent manageDevice = new Intent(context, UsbHostManagementActivity.class);
35 manageDevice.setAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
36 manageDevice.putExtra(UsbManager.EXTRA_DEVICE, device);
37 manageDevice.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
38 context.startActivity(manageDevice);
39 }
40}