Remove restrictions PIN for all users

Bug: 10200097
Change-Id: I4d95cd49a21e8f4f6b2c141d1205f81622c395b5
diff --git a/services/java/com/android/server/pm/UserManagerService.java b/services/java/com/android/server/pm/UserManagerService.java
index 4ead8d5..35b5c85 100644
--- a/services/java/com/android/server/pm/UserManagerService.java
+++ b/services/java/com/android/server/pm/UserManagerService.java
@@ -116,7 +116,7 @@
 
     private static final int MIN_USER_ID = 10;
 
-    private static final int USER_VERSION = 2;
+    private static final int USER_VERSION = 3;
 
     private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
 
@@ -425,6 +425,7 @@
                     && restrictions.getBoolean(UserManager.DISALLOW_APP_RESTRICTIONS, false)) {
                 restrictions.putBoolean(UserManager.DISALLOW_APP_RESTRICTIONS, false);
             }
+            mUserRestrictions.get(userId).clear();
             mUserRestrictions.get(userId).putAll(restrictions);
             writeUserLocked(mUsers.get(userId));
         }
@@ -555,7 +556,7 @@
                 }
             }
             updateUserIdsLocked();
-            upgradeIfNecessary();
+            upgradeIfNecessaryLocked();
         } catch (IOException ioe) {
             fallbackToSingleUserLocked();
         } catch (XmlPullParserException pe) {
@@ -573,7 +574,7 @@
     /**
      * Upgrade steps between versions, either for fixing bugs or changing the data format.
      */
-    private void upgradeIfNecessary() {
+    private void upgradeIfNecessaryLocked() {
         int userVersion = mUserVersion;
         if (userVersion < 1) {
             // Assign a proper name for the owner, if not initialized correctly before
@@ -595,6 +596,18 @@
             userVersion = 2;
         }
 
+        if (userVersion < 3) {
+            // Remove restrictions PIN for all users
+            for (int i = 0; i < mRestrictionsPinStates.size(); i++) {
+                int userId = mRestrictionsPinStates.keyAt(i);
+                RestrictionsPinState state = mRestrictionsPinStates.valueAt(i);
+                if (state.salt != 0 && state.pinHash != null) {
+                    removeRestrictionsForUser(userId);
+                }
+            }
+            userVersion = 3;
+        }
+
         if (userVersion < USER_VERSION) {
             Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
                     + USER_VERSION);
@@ -1221,6 +1234,10 @@
     public void removeRestrictions() {
         checkManageUsersPermission("Only system can remove restrictions");
         final int userHandle = UserHandle.getCallingUserId();
+        removeRestrictionsForUser(userHandle);
+    }
+
+    private void removeRestrictionsForUser(final int userHandle) {
         synchronized (mPackagesLock) {
             // Remove all user restrictions
             setUserRestrictions(new Bundle(), userHandle);