Clean up any restrictions files that shouldn't be there.

Partially fixes the mess that was caused by #9681399

Change-Id: Iaabe15c87d1b002f0b2210b34adcc31b8fb4329c
diff --git a/services/java/com/android/server/pm/UserManagerService.java b/services/java/com/android/server/pm/UserManagerService.java
index d86f2c7..2901212 100644
--- a/services/java/com/android/server/pm/UserManagerService.java
+++ b/services/java/com/android/server/pm/UserManagerService.java
@@ -822,6 +822,11 @@
                 pinState.failedAttempts = failedAttempts;
                 pinState.lastAttemptTime = lastAttemptTime;
             }
+            // If this is not a restricted profile and there is no restrictions pin, clean up
+            // any restrictions files that might have been left behind.
+            if (!userInfo.isRestricted() && salt == 0) {
+                cleanAppRestrictions(id);
+            }
             return userInfo;
 
         } catch (IOException ioe) {
@@ -873,6 +878,26 @@
         }
     }
 
+    /**
+     * Removes all the restrictions files (res_<packagename>) for a given user.
+     * Does not do any permissions checking.
+     */
+    private void cleanAppRestrictions(int userId) {
+        synchronized (mPackagesLock) {
+            File dir = Environment.getUserSystemDirectory(userId);
+            String[] files = dir.list();
+            if (files == null) return;
+            for (String fileName : files) {
+                if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
+                    File resFile = new File(dir, fileName);
+                    if (resFile.exists()) {
+                        resFile.delete();
+                    }
+                }
+            }
+        }
+    }
+
     @Override
     public UserInfo createUser(String name, int flags) {
         checkManageUsersPermission("Only the system can create users");