1 package android.car.usb.handler; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.hardware.usb.UsbDevice; 7 import android.hardware.usb.UsbDeviceConnection; 8 import android.hardware.usb.UsbManager; 9 10 public 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 } 41