Integrated CarUserManager with ICarServiceHelper for UX restrictions.

Test: atest CarDevicePolicySafetyCheckerTest \
            CarDevicePolicyManagerTest#testLockNow_safe \
            CarDevicePolicyManagerTest#testLockNow_unsafe
Bug: 172376923

Change-Id: I12a6cdca840ac7161c23deb4db3cee22210f5f1a
diff --git a/src/com/android/internal/car/CarDevicePolicySafetyChecker.java b/src/com/android/internal/car/CarDevicePolicySafetyChecker.java
index a57d7f0..4a7035e 100644
--- a/src/com/android/internal/car/CarDevicePolicySafetyChecker.java
+++ b/src/com/android/internal/car/CarDevicePolicySafetyChecker.java
@@ -28,15 +28,12 @@
 
     private static final String TAG = CarDevicePolicySafetyChecker.class.getSimpleName();
 
-    // TODO(b/172376923): STOPSHIP if it's not false
-    private static final boolean DEBUG = true;
+    private static final boolean DEBUG = false;
 
     private boolean mSafe = true;
 
     @Override
     public boolean isDevicePolicyOperationSafe(@DevicePolicyOperation int operation) {
-        // TODO(b/172376923): add unit test once it's integrated with
-        // CarUxRestrictionsManagerService
         // TODO(b/172376923): use a lookup table as not all operations need to be checked
         boolean safe = mSafe;
         if (DEBUG) {
@@ -45,11 +42,9 @@
         return safe;
     }
 
-    // TODO(b/172376923): override getUnsafeStateException to showerror message explaining how to
+    // TODO(b/172376923): override getUnsafeStateException to show error message explaining how to
     // wrap it under CarDevicePolicyManager
 
-    // TODO(b/172376923): temporary method until it's properly integrated with
-    // CarUxRestrictionsManagerService
     void setSafe(boolean safe) {
         Slog.i(TAG, "Setting safe to " + safe);
         mSafe = safe;
diff --git a/src/com/android/internal/car/CarServiceHelperService.java b/src/com/android/internal/car/CarServiceHelperService.java
index ffcfe6d..1837de0 100644
--- a/src/com/android/internal/car/CarServiceHelperService.java
+++ b/src/com/android/internal/car/CarServiceHelperService.java
@@ -199,7 +199,7 @@
         }
     };
 
-    private final CarDevicePolicySafetyChecker mDevicePolicySafetyChecker =
+    private final CarDevicePolicySafetyChecker mCarDevicePolicySafetyChecker =
             new CarDevicePolicySafetyChecker();
 
     public CarServiceHelperService(Context context) {
@@ -284,7 +284,7 @@
             TimeUtils.formatDuration(mFirstUnlockedUserDuration, pw); pw.println();
             pw.printf("Queued tasks: %d\n", mProcessTerminator.mQueuedTask);
             mCarServiceProxy.dump(pw);
-            mDevicePolicySafetyChecker.dump(pw);
+            mCarDevicePolicySafetyChecker.dump(pw);
             return;
         }
 
@@ -293,18 +293,6 @@
             return;
         }
 
-        // TODO(b/172376923): temporary commands until CarDevicePolicySafetyChecker is properly
-        // integrated with CarUxRestrictionsManagerService
-        if ("--drive".equals(args[0])) {
-            pw.println("Changing safe to false");
-            mDevicePolicySafetyChecker.setSafe(false);
-            return;
-        } else if ("--park".equals(args[0])) {
-            pw.println("Changing safe to false");
-            mDevicePolicySafetyChecker.setSafe(true);
-            return;
-        }
-
         pw.printf("Invalid args: %s\n", Arrays.toString(args));
     }
 
@@ -383,12 +371,12 @@
 
     @Override // from SafetyChecker
     public boolean isDevicePolicyOperationSafe(@DevicePolicyOperation int operation) {
-        return mDevicePolicySafetyChecker.isDevicePolicyOperationSafe(operation);
+        return mCarDevicePolicySafetyChecker.isDevicePolicyOperationSafe(operation);
     }
 
     @Override
     public UnsafeStateException newUnsafeStateException(@DevicePolicyOperation int operation) {
-        return mDevicePolicySafetyChecker.newUnsafeStateException(operation);
+        return mCarDevicePolicySafetyChecker.newUnsafeStateException(operation);
     }
 
     @VisibleForTesting
@@ -595,6 +583,8 @@
 
     private static native int nativeForceSuspend(int timeoutMs);
 
+    // TODO: it's missing unit tests (for example, to make sure that
+    // when its setSafetyMode() is called, mCarDevicePolicySafetyChecker is updated).
     private class ICarServiceHelperImpl extends ICarServiceHelper.Stub {
         /**
          * Force device to suspend
@@ -628,6 +618,11 @@
             mCarLaunchParamsModifier.setSourcePreferredComponents(
                     enableSourcePreferred, sourcePreferredComponents);
         }
+
+        @Override
+        public void setSafetyMode(boolean safe) {
+            mCarDevicePolicySafetyChecker.setSafe(safe);
+        }
     }
 
     private class ICarWatchdogMonitorImpl extends ICarWatchdogMonitor.Stub {
diff --git a/tests/src/com/android/internal/car/CarDevicePolicySafetyCheckerTest.java b/tests/src/com/android/internal/car/CarDevicePolicySafetyCheckerTest.java
new file mode 100644
index 0000000..4988b81
--- /dev/null
+++ b/tests/src/com/android/internal/car/CarDevicePolicySafetyCheckerTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.car;
+
+import static android.app.admin.DevicePolicyManager.OPERATION_LOCK_NOW;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+
+public final class CarDevicePolicySafetyCheckerTest {
+
+    private final CarDevicePolicySafetyChecker mChecker = new CarDevicePolicySafetyChecker();
+
+    // TODO(b/172376923): test for all operations / use parameterized test
+
+    @Test
+    public void testSafe() throws Exception {
+        mChecker.setSafe(true);
+        assertThat(mChecker.isDevicePolicyOperationSafe(OPERATION_LOCK_NOW)).isTrue();
+    }
+
+    @Test
+    public void testUnsafe() throws Exception {
+        mChecker.setSafe(false);
+        assertThat(mChecker.isDevicePolicyOperationSafe(OPERATION_LOCK_NOW)).isFalse();
+    }
+}