Merge "Try again to cleanup all blocked apps"
diff --git a/services/java/com/android/server/pm/UserManagerService.java b/services/java/com/android/server/pm/UserManagerService.java
index 35b5c85..bb37917 100644
--- a/services/java/com/android/server/pm/UserManagerService.java
+++ b/services/java/com/android/server/pm/UserManagerService.java
@@ -26,6 +26,7 @@
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.RestrictionEntry;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
@@ -116,7 +117,7 @@
 
     private static final int MIN_USER_ID = 10;
 
-    private static final int USER_VERSION = 3;
+    private static final int USER_VERSION = 4;
 
     private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
 
@@ -162,6 +163,8 @@
     private boolean mGuestEnabled;
     private int mNextSerialNumber;
     private int mUserVersion = 0;
+    // Temporary cleanup variable, this and associated code should be removed later.
+    private boolean mUnblockAppsTemp;
 
     private static UserManagerService sInstance;
 
@@ -232,12 +235,14 @@
                 sInstance = this;
             }
         }
-
     }
 
     void systemReady() {
-        mUserPackageMonitor.register(ActivityThread.systemMain().getSystemContext(),
+        final Context context = ActivityThread.systemMain().getSystemContext();
+        mUserPackageMonitor.register(context,
                 null, UserHandle.ALL, false);
+        context.registerReceiver(mBootCompletedReceiver,
+                new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
         userForeground(UserHandle.USER_OWNER);
     }
 
@@ -602,12 +607,17 @@
                 int userId = mRestrictionsPinStates.keyAt(i);
                 RestrictionsPinState state = mRestrictionsPinStates.valueAt(i);
                 if (state.salt != 0 && state.pinHash != null) {
-                    removeRestrictionsForUser(userId);
+                    removeRestrictionsForUser(userId, false);
                 }
             }
             userVersion = 3;
         }
 
+        if (userVersion < 4) {
+            mUnblockAppsTemp = true;
+            userVersion = 4;
+        }
+
         if (userVersion < USER_VERSION) {
             Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
                     + USER_VERSION);
@@ -1234,10 +1244,10 @@
     public void removeRestrictions() {
         checkManageUsersPermission("Only system can remove restrictions");
         final int userHandle = UserHandle.getCallingUserId();
-        removeRestrictionsForUser(userHandle);
+        removeRestrictionsForUser(userHandle, true);
     }
 
-    private void removeRestrictionsForUser(final int userHandle) {
+    private void removeRestrictionsForUser(final int userHandle, boolean unblockApps) {
         synchronized (mPackagesLock) {
             // Remove all user restrictions
             setUserRestrictions(new Bundle(), userHandle);
@@ -1246,6 +1256,12 @@
             // Remove any app restrictions
             cleanAppRestrictions(userHandle, true);
         }
+        if (unblockApps) {
+            unblockAllAppsForUser(userHandle);
+        }
+    }
+
+    private void unblockAllAppsForUser(final int userHandle) {
         mHandler.post(new Runnable() {
             @Override
             public void run() {
@@ -1572,4 +1588,19 @@
             }
         }
     };
+
+    private BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
+        @Override public void onReceive(Context context, Intent intent) {
+            // This code block can be removed after cleanup
+            if (mUnblockAppsTemp) {
+                synchronized (mPackagesLock) {
+                    // Unblock apps due to removal of restrictions feature
+                    for (int i = 0; i < mUsers.size(); i++) {
+                        int userId = mUsers.keyAt(i);
+                        unblockAllAppsForUser(userId);
+                    }
+                }
+            }
+        }
+    };
 }