Set DeviceLockedForUser state synchronously

Setting DeviceLockedForUser on the handler thread will lead to a race
condition where an immedidate read might see a stale value.

Bug: 30728557
Change-Id: Id9d510d7ad4dbcde6344a044e45f4b2feac19257
diff --git a/services/core/java/com/android/server/trust/TrustManagerService.java b/services/core/java/com/android/server/trust/TrustManagerService.java
index 216afe6..c952ee6 100644
--- a/services/core/java/com/android/server/trust/TrustManagerService.java
+++ b/services/core/java/com/android/server/trust/TrustManagerService.java
@@ -102,9 +102,8 @@
     private static final int MSG_START_USER = 7;
     private static final int MSG_CLEANUP_USER = 8;
     private static final int MSG_SWITCH_USER = 9;
-    private static final int MSG_SET_DEVICE_LOCKED = 10;
-    private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 11;
-    private static final int MSG_UNLOCK_USER = 12;
+    private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 10;
+    private static final int MSG_UNLOCK_USER = 11;
 
     private static final int TRUST_USUALLY_MANAGED_FLUSH_DELAY = 2 * 60 * 1000;
 
@@ -352,20 +351,6 @@
         }
     }
 
-    public void setDeviceLockedForUser(int userId, boolean locked) {
-        if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
-            synchronized (mDeviceLockedForUser) {
-                mDeviceLockedForUser.put(userId, locked);
-            }
-            if (locked) {
-                try {
-                    ActivityManagerNative.getDefault().notifyLockedProfile(userId);
-                } catch (RemoteException e) {
-                }
-            }
-        }
-    }
-
     boolean isDeviceLockedInner(int userId) {
         synchronized (mDeviceLockedForUser) {
             return mDeviceLockedForUser.get(userId, true);
@@ -873,10 +858,19 @@
         }
 
         @Override
-        public void setDeviceLockedForUser(int userId, boolean value) {
+        public void setDeviceLockedForUser(int userId, boolean locked) {
             enforceReportPermission();
-            mHandler.obtainMessage(MSG_SET_DEVICE_LOCKED, value ? 1 : 0, userId)
-                    .sendToTarget();
+            if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
+                synchronized (mDeviceLockedForUser) {
+                    mDeviceLockedForUser.put(userId, locked);
+                }
+                if (locked) {
+                    try {
+                        ActivityManagerNative.getDefault().notifyLockedProfile(userId);
+                    } catch (RemoteException e) {
+                    }
+                }
+            }
         }
 
         @Override
@@ -952,9 +946,6 @@
                     mCurrentUser = msg.arg1;
                     refreshDeviceLockedForUser(UserHandle.USER_ALL);
                     break;
-                case MSG_SET_DEVICE_LOCKED:
-                    setDeviceLockedForUser(msg.arg2, msg.arg1 != 0);
-                    break;
                 case MSG_FLUSH_TRUST_USUALLY_MANAGED:
                     SparseBooleanArray usuallyManaged;
                     synchronized (mTrustUsuallyManagedForUser) {