Start BootUsbService on user switch
Start BootUsbService for non-system users, or the system user if it is
running as a "real" user.
Fixes: 150893239
Bug: 149046747
Test: Manually switch user and ensure for Android Auto icon is blue
(active)
Change-Id: Ie269bda4f3e2738f2bfc06cb71c3198e177c9006
diff --git a/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java b/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
index ac7a6a5..9f2cd2a 100644
--- a/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
+++ b/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
@@ -6,6 +6,7 @@
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.UserHandle;
+import android.os.UserManager;
import java.util.ArrayList;
import java.util.HashMap;
@@ -15,10 +16,11 @@
@Override
public void onReceive(Context context, Intent intent) {
- // Only start the service if we are the system user. We cannot use the singleUser tag in the
- // manifest for <receiver>s, so there is no way to avoid us registering this receiver as the
- // non system user. Just return immediately if we are not.
- if (context.getUserId() != UserHandle.USER_SYSTEM) {
+ // Only start the service for the non-system user, or for the system user if it is running
+ // as a "real" user. This ensures the service is started only once and is started even on a
+ // foreground user switch.
+ if (context.getUserId() == UserHandle.USER_SYSTEM
+ && !UserManager.isHeadlessSystemUserMode()) {
return;
}
// we defer this processing to BootUsbService so that we are very quick to process
diff --git a/car-usb-handler/src/android/car/usb/handler/BootUsbService.java b/car-usb-handler/src/android/car/usb/handler/BootUsbService.java
index 5d29f54..c98b902 100644
--- a/car-usb-handler/src/android/car/usb/handler/BootUsbService.java
+++ b/car-usb-handler/src/android/car/usb/handler/BootUsbService.java
@@ -15,17 +15,12 @@
*/
package android.car.usb.handler;
-import static android.content.Intent.ACTION_USER_SWITCHED;
-
-import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
-import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
-import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Binder;
@@ -46,14 +41,6 @@
static final String USB_DEVICE_LIST_KEY = "usb_device_list";
private ArrayList<UsbDevice> mDeviceList;
- private boolean mReceiverRegistered = false;
- private final BroadcastReceiver mUserSwitchBroadcastReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- processDevices();
- unregisterUserSwitchReceiver();
- }
- };
@Override
public Binder onBind(Intent intent) {
@@ -81,27 +68,13 @@
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mDeviceList = intent.getParcelableArrayListExtra(USB_DEVICE_LIST_KEY);
- // If the current user is still the system user, wait until the non system user becomes the
- // current/foreground user. Otherwise, we can go ahead and start processing the USB devices
- // immediately.
- if (ActivityManager.getCurrentUser() == UserHandle.USER_SYSTEM) {
- Log.d(TAG, "Current user is still the system user, waiting for user switch");
- registerUserSwitchReceiver();
- } else {
- processDevices();
- }
-
+ processDevices();
return START_NOT_STICKY;
}
- @Override
- public void onDestroy() {
- unregisterUserSwitchReceiver();
- }
-
private void processDevices() {
- Log.d(TAG, "Processing connected USB devices and starting handlers");
for (UsbDevice device : mDeviceList) {
+ Log.d(TAG, "Processing device: " + device.getProductName());
handle(this, device);
}
stopSelf();
@@ -114,18 +87,4 @@
manageDevice.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivityAsUser(manageDevice, UserHandle.CURRENT);
}
-
- private void registerUserSwitchReceiver() {
- if (!mReceiverRegistered) {
- registerReceiver(mUserSwitchBroadcastReceiver, new IntentFilter(ACTION_USER_SWITCHED));
- mReceiverRegistered = true;
- }
- }
-
- private void unregisterUserSwitchReceiver() {
- if (mReceiverRegistered) {
- unregisterReceiver(mUserSwitchBroadcastReceiver);
- mReceiverRegistered = false;
- }
- }
}