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.UsbManager; 8 import android.os.UserHandle; 9 import android.os.UserManager; 10 11 import java.util.ArrayList; 12 import java.util.HashMap; 13 14 /** Queues work to the BootUsbService job to scan for connected devices. */ 15 public class BootUsbScanner extends BroadcastReceiver { 16 17 @Override onReceive(Context context, Intent intent)18 public void onReceive(Context context, Intent intent) { 19 // Only start the service for the non-system user, or for the system user if it is running 20 // as a "real" user. This ensures the service is started only once and is started even on a 21 // foreground user switch. 22 if (context.getUserId() == UserHandle.USER_SYSTEM 23 && UserManager.isHeadlessSystemUserMode()) { 24 return; 25 } 26 // we defer this processing to BootUsbService so that we are very quick to process 27 // LOCKED_BOOT_COMPLETED 28 UsbManager usbManager = context.getSystemService(UsbManager.class); 29 HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); 30 if (deviceList.size() > 0) { 31 Intent bootUsbServiceIntent = new Intent(context, BootUsbService.class); 32 bootUsbServiceIntent.putParcelableArrayListExtra( 33 BootUsbService.USB_DEVICE_LIST_KEY, new ArrayList<>(deviceList.values())); 34 35 context.startForegroundService(bootUsbServiceIntent); 36 } 37 } 38 } 39