Migrate to UserManager.isHeadlessSystemUserMode()

Migrate code to UserManager API.

Bug: 131776619
Test: atest CarServiceUnitTest
Change-Id: Ia5de8832e9d4d6e61236f7a1af0b171254f09ebb
diff --git a/service/src/com/android/car/CarLocationService.java b/service/src/com/android/car/CarLocationService.java
index 15e663f..34a6885 100644
--- a/service/src/com/android/car/CarLocationService.java
+++ b/service/src/com/android/car/CarLocationService.java
@@ -24,7 +24,6 @@
 import android.car.hardware.power.CarPowerManager;
 import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
 import android.car.hardware.power.CarPowerManager.CarPowerStateListenerWithCompletion;
-import android.car.userlib.CarUserManagerHelper;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -36,6 +35,7 @@
 import android.os.RemoteException;
 import android.os.SystemClock;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.util.AtomicFile;
 import android.util.JsonReader;
 import android.util.JsonWriter;
@@ -77,7 +77,6 @@
     private final Object mLocationManagerProxyLock = new Object();
 
     private final Context mContext;
-    private final CarUserManagerHelper mCarUserManagerHelper;
     private int mTaskCount = 0;
     private HandlerThread mHandlerThread;
     private Handler mHandler;
@@ -108,7 +107,7 @@
                     }
                     int currentUser = ActivityManager.getCurrentUser();
                     logd("Current user: " + currentUser);
-                    if (mCarUserManagerHelper.isHeadlessSystemUser()
+                    if (UserManager.isHeadlessSystemUserMode()
                             && currentUser > UserHandle.USER_SYSTEM) {
                         asyncOperation(() -> loadLocation());
                     }
@@ -147,10 +146,9 @@
                 }
             };
 
-    public CarLocationService(Context context, CarUserManagerHelper carUserManagerHelper) {
+    public CarLocationService(Context context) {
         logd("constructed");
         mContext = context;
-        mCarUserManagerHelper = carUserManagerHelper;
     }
 
     @Override
@@ -274,7 +272,7 @@
     /** Tells whether the current foreground user is the headless system user. */
     private boolean isCurrentUserHeadlessSystemUser() {
         int currentUserId = ActivityManager.getCurrentUser();
-        return mCarUserManagerHelper.isHeadlessSystemUser()
+        return UserManager.isHeadlessSystemUserMode()
                 && currentUserId == UserHandle.USER_SYSTEM;
     }
 
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index c2eab7c..6e0b6fa 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -175,7 +175,7 @@
                 systemInterface);
         mCarConfigurationService =
                 new CarConfigurationService(serviceContext, new JsonReaderImpl());
-        mCarLocationService = new CarLocationService(mContext, mUserManagerHelper);
+        mCarLocationService = new CarLocationService(serviceContext);
         mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
         mCarMediaService = new CarMediaService(serviceContext);
         mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
diff --git a/tests/carservice_unit_test/src/android/car/userlib/CarUserManagerHelperTest.java b/tests/carservice_unit_test/src/android/car/userlib/CarUserManagerHelperTest.java
index c95fd9e..1934f3e 100644
--- a/tests/carservice_unit_test/src/android/car/userlib/CarUserManagerHelperTest.java
+++ b/tests/carservice_unit_test/src/android/car/userlib/CarUserManagerHelperTest.java
@@ -104,12 +104,6 @@
                 .thenAnswer(stub -> stub.getArguments()[0]);
     }
 
-    @Test
-    public void checkHeadlessSystemUserFlag() {
-        // Make sure the headless system user flag is on.
-        assertThat(mCarUserManagerHelper.isHeadlessSystemUser()).isTrue();
-    }
-
     // System user will not be returned when calling get all users.
     @Test
     public void testHeadlessUser0GetAllUsers_NotReturnSystemUser() {
diff --git a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
index 33a868e..68d7904 100644
--- a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
@@ -35,7 +35,6 @@
 import android.car.drivingstate.CarDrivingStateEvent;
 import android.car.drivingstate.ICarDrivingStateChangeListener;
 import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
-import android.car.userlib.CarUserManagerHelper;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -43,6 +42,7 @@
 import android.location.Location;
 import android.location.LocationManager;
 import android.os.SystemClock;
+import android.os.UserManager;
 
 import androidx.test.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
@@ -75,12 +75,11 @@
  *
  * The following mocks are used:
  * 1. {@link Context} registers intent receivers.
- * 2. {@link CarUserManagerHelper} tells whether or not the system user is headless.
- * 3. {@link SystemInterface} tells where to store system files.
- * 4. {@link CarDrivingStateService} tells about driving state changes.
- * 5. {@link PerUserCarServiceHelper} provides a mocked {@link IPerUserCarService}.
- * 6. {@link IPerUserCarService} provides a mocked {@link LocationManagerProxy}.
- * 7. {@link LocationManagerProxy} provides dummy {@link Location}s.
+ * 2. {@link SystemInterface} tells where to store system files.
+ * 3. {@link CarDrivingStateService} tells about driving state changes.
+ * 4. {@link PerUserCarServiceHelper} provides a mocked {@link IPerUserCarService}.
+ * 5. {@link IPerUserCarService} provides a mocked {@link LocationManagerProxy}.
+ * 6. {@link LocationManagerProxy} provides dummy {@link Location}s.
  */
 @RunWith(AndroidJUnit4.class)
 public class CarLocationServiceTest {
@@ -96,8 +95,6 @@
     @Mock
     private LocationManagerProxy mMockLocationManagerProxy;
     @Mock
-    private CarUserManagerHelper mMockCarUserManagerHelper;
-    @Mock
     private SystemInterface mMockSystemInterface;
     @Mock
     private CarDrivingStateService mMockCarDrivingStateService;
@@ -115,7 +112,7 @@
         mContext = InstrumentationRegistry.getTargetContext();
         mTempDirectory = new TemporaryDirectory(TAG).getDirectory();
         mLatch = new CountDownLatch(1);
-        mCarLocationService = new CarLocationService(mMockContext, mMockCarUserManagerHelper) {
+        mCarLocationService = new CarLocationService(mMockContext) {
             @Override
             void asyncOperation(Runnable operation) {
                 super.asyncOperation(() -> {
@@ -134,8 +131,10 @@
         when(mMockIPerUserCarService.getLocationManagerProxy())
                 .thenReturn(mMockLocationManagerProxy);
 
-        // We only support and test the headless system user case.
-        when(mMockCarUserManagerHelper.isHeadlessSystemUser()).thenReturn(true);
+        if (!UserManager.isHeadlessSystemUserMode()) {
+            fail("We only support and test the headless system user case. Ensure the system has "
+                    + "the system property 'ro.fw.mu.headless_system_user' set to true.");
+        }
 
         // Store CarLocationService's user switch callback so we can invoke it in the tests.
         doAnswer((invocation) -> {
diff --git a/user/car-user-lib/src/android/car/userlib/CarUserManagerHelper.java b/user/car-user-lib/src/android/car/userlib/CarUserManagerHelper.java
index 8aefca4..b05f6b4 100644
--- a/user/car-user-lib/src/android/car/userlib/CarUserManagerHelper.java
+++ b/user/car-user-lib/src/android/car/userlib/CarUserManagerHelper.java
@@ -34,7 +34,6 @@
 import android.util.Log;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.os.RoSystemProperties;
 import com.android.internal.util.UserIcons;
 
 import com.google.android.collect.Sets;
@@ -205,15 +204,6 @@
     }
 
     /**
-     * Returns {@code true} if the system is in the headless user 0 model.
-     *
-     * @return {@boolean true} if headless system user.
-     */
-    public boolean isHeadlessSystemUser() {
-        return RoSystemProperties.MULTIUSER_HEADLESS_SYSTEM_USER;
-    }
-
-    /**
      * Gets UserInfo for the current foreground user.
      *
      * Concept of foreground user is relevant for the multi-user deployment. Foreground user
@@ -261,7 +251,7 @@
      * @return List of {@code UserInfo} for users that associated with a real person.
      */
     public List<UserInfo> getAllUsers() {
-        if (isHeadlessSystemUser()) {
+        if (UserManager.isHeadlessSystemUserMode()) {
             return getAllUsersExceptSystemUserAndSpecifiedUser(UserHandle.USER_SYSTEM);
         } else {
             return mUserManager.getUsers(/* excludeDying= */ true);
@@ -368,7 +358,7 @@
      * @return Maximum number of users that can be present on the device.
      */
     private int getMaxSupportedUsers() {
-        if (isHeadlessSystemUser()) {
+        if (UserManager.isHeadlessSystemUserMode()) {
             return mTestableFrameworkWrapper.userManagerGetMaxSupportedUsers() - 1;
         }
         return mTestableFrameworkWrapper.userManagerGetMaxSupportedUsers();
@@ -639,7 +629,7 @@
      * @return {@code true} if user switching succeed.
      */
     public boolean switchToUserId(int id) {
-        if (id == UserHandle.USER_SYSTEM && isHeadlessSystemUser()) {
+        if (id == UserHandle.USER_SYSTEM && UserManager.isHeadlessSystemUserMode()) {
             // System User doesn't associate with real person, can not be switched to.
             return false;
         }