Merge "Enhancing CarMediaService to implement UserLifecycleListener." into rvc-dev
diff --git a/service/src/com/android/car/CarMediaService.java b/service/src/com/android/car/CarMediaService.java
index e5f8ea5..e68b783 100644
--- a/service/src/com/android/car/CarMediaService.java
+++ b/service/src/com/android/car/CarMediaService.java
@@ -179,6 +179,8 @@
         }
         if (CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING == event.getEventType()) {
             maybeInitUser(event.getUserId());
+        } else if (CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKING == event.getEventType()) {
+            onUserUnlock(event.getUserId());
         }
     };
 
@@ -201,7 +203,7 @@
         mPackageUpdateFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
         mPackageUpdateFilter.addDataScheme("package");
         mUserService = userService;
-        mUserService.removeUserLifecycleListener(mUserLifecycleListener);
+        mUserService.addUserLifecycleListener(mUserLifecycleListener);
 
         mPlayOnMediaSourceChangedConfig =
                 mContext.getResources().getInteger(R.integer.config_mediaSourceChangedAutoplay);
@@ -402,33 +404,23 @@
         }
     }
 
-    /**
-     * Sets user lock / unlocking status on main thread. This is coming from system server through
-     * ICar binder call.
-     *
-     * @param userHandle Handle of user
-     * @param unlocked   unlocked (=true) or locked (=false)
-     */
-    public void setUserLockStatus(int userHandle, boolean unlocked) {
-        mMainHandler.post(new Runnable() {
-            @Override
-            public void run() {
+    // TODO(b/153115826): this method was used to be called from the ICar binder thread, but it's
+    // now called by UserCarService. Currently UserCarServie is calling every listener in one
+    // non-main thread, but it's not clear how the final behavior will be. So, for now it's ok
+    // to post it to mMainHandler, but once b/145689885 is fixed, we might not need it.
+    private void onUserUnlock(int userId) {
+        mMainHandler.post(() -> {
+            // No need to handle system user, non current foreground user.
+            if (userId == UserHandle.USER_SYSTEM
+                    || userId != ActivityManager.getCurrentUser()) {
+                return;
+            }
+            if (mPendingInit) {
+                initUser(userId);
+                mPendingInit = false;
                 if (Log.isLoggable(CarLog.TAG_MEDIA, Log.DEBUG)) {
                     Log.d(CarLog.TAG_MEDIA,
-                            "User " + userHandle + " is " + (unlocked ? "unlocked" : "locked"));
-                }
-                // Nothing else to do when it is locked back.
-                if (!unlocked) {
-                    return;
-                }
-                // No need to handle system user, non current foreground user.
-                if (userHandle == UserHandle.USER_SYSTEM
-                        || userHandle != ActivityManager.getCurrentUser()) {
-                    return;
-                }
-                if (mPendingInit) {
-                    initUser(userHandle);
-                    mPendingInit = false;
+                            "User " + userId + " is now unlocked");
                 }
             }
         });
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index da3881a..6693b46 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -355,12 +355,8 @@
         assertCallingFromSystemProcess();
         Log.i(TAG, "onUserLifecycleEvent("
                 + CarUserManager.lifecycleEventTypeToString(eventType) + ", " + toUserId + ")");
-        mCarUserService.onUserLifecycleEvent(new UserLifecycleEvent(eventType, toUserId));
-        if (eventType == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKING) {
-            // TODO(b/145689885): CarMediaService should implement UserLifecycleListener,
-            //     eliminiating the need for this check.
-            mCarMediaService.setUserLockStatus(toUserId, /* unlocked= */ true);
-        }
+        UserLifecycleEvent event = new UserLifecycleEvent(eventType, toUserId);
+        mCarUserService.onUserLifecycleEvent(event);
         mUserMetrics.onEvent(eventType, timestampMs, fromUserId, toUserId);
     }