Modify getUserProfiles to return only enabled profiles:

Add a new enabled state for a managed profile.
Expose that as a new API on DevicePolicyManager.
Set the new state when enabling the profile.
Return only enabled profiles from the user manager.

Bug: 13755441
Bug: 13755091
Change-Id: I2907b182e19b3562592da688b3f68ef5f4088557
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index 53db9ef..210e151b 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -24,6 +24,7 @@
 import android.app.ActivityThread;
 import android.app.admin.DevicePolicyManager;
 import android.app.IStopUserCallback;
+import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -258,11 +259,20 @@
     }
 
     @Override
-    public List<UserInfo> getProfiles(int userId) {
+    public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
         if (userId != UserHandle.getCallingUserId()) {
             checkManageUsersPermission("getting profiles related to user " + userId);
         }
         synchronized (mPackagesLock) {
+            // Getting the service here is not good for testing purposes. However, this service
+            // is not available when UserManagerService starts up so we need a lazy load.
+
+            DevicePolicyManager dpm = null;
+            if (enabledOnly) {
+                dpm = (DevicePolicyManager)
+                        mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+            }
+
             UserInfo user = getUserInfoLocked(userId);
             ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
             for (int i = 0; i < mUsers.size(); i++) {
@@ -270,6 +280,23 @@
                 if (!isProfileOf(user, profile)) {
                     continue;
                 }
+
+                if (enabledOnly && profile.isManagedProfile()) {
+                    if (dpm != null) {
+                        if(!dpm.isProfileEnabled(profile.id)) {
+                            continue;
+                        }
+                    } else {
+                        Log.w(LOG_TAG,
+                                "Attempting to reach DevicePolicyManager before it was started");
+                        // TODO: There might be system apps that need to call this. Make sure that
+                        // DevicePolicyManagerService is ready at that time (otherwise, any default
+                        // value is a bad one).
+                        throw new IllegalArgumentException(String.format(
+                                "Attempting to get enabled profiles for %d before "
+                                + "DevicePolicyManagerService has been started.", userId));
+                    }
+                }
                 users.add(profile);
             }
             return users;