Add support for visualizing secondary users.

This functionality adds the secondary users to the screen, but
currently does not populate the information for them. Once the
external stats query works, I will add a loader which will
populate this information.

This also does not cover work profiles. Support for that is
forthcoming.

Bug: 34715777, 34225103
Test: Settings Robotest
Change-Id: Ib9b692b214f5ce5d303dfd64516381443d4acebd
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 0ac5c6a..d6b962d 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -1246,4 +1246,16 @@
                 && (Settings.Secure.getInt(context.getContentResolver(),
                         carrierDemoModeSetting, 0) == 1);
     }
+
+    /**
+     * Returns if a given user is a profile of another user.
+     * @param user The user whose profiles will be checked.
+     * @param profile The (potential) profile.
+     * @return if the profile is actually a profile
+     */
+    public static boolean isProfileOf(UserInfo user, UserInfo profile) {
+        return user.id == profile.id ||
+                (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
+                        && user.profileGroupId == profile.profileGroupId);
+    }
 }
diff --git a/src/com/android/settings/applications/UserManagerWrapper.java b/src/com/android/settings/applications/UserManagerWrapper.java
new file mode 100644
index 0000000..5b4ed2a
--- /dev/null
+++ b/src/com/android/settings/applications/UserManagerWrapper.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.applications;
+
+import android.content.pm.UserInfo;
+
+import java.util.List;
+
+/**
+ * This interface replicates a subset of the android.os.UserManager. The interface
+ * exists so that we can use a thin wrapper around the UserManager in production code and a mock in
+ * tests. We cannot directly mock or shadow the UserManager, because some of the methods we rely on
+ * are newer than the API version supported by Robolectric or are hidden.
+ */
+public interface UserManagerWrapper {
+    UserInfo getPrimaryUser();
+    List<UserInfo> getUsers();
+}
diff --git a/src/com/android/settings/applications/UserManagerWrapperImpl.java b/src/com/android/settings/applications/UserManagerWrapperImpl.java
new file mode 100644
index 0000000..14ea64a
--- /dev/null
+++ b/src/com/android/settings/applications/UserManagerWrapperImpl.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.applications;
+
+import android.content.pm.UserInfo;
+import android.os.UserManager;
+
+import java.util.List;
+
+public class UserManagerWrapperImpl implements UserManagerWrapper {
+    private UserManager mUserManager;
+
+    public UserManagerWrapperImpl(UserManager userManager) {
+        mUserManager = userManager;
+    }
+
+    @Override
+    public UserInfo getPrimaryUser() {
+        return mUserManager.getPrimaryUser();
+    }
+
+    @Override
+    public List<UserInfo> getUsers() {
+        return mUserManager.getUsers();
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/PrivateVolumeSettings.java b/src/com/android/settings/deviceinfo/PrivateVolumeSettings.java
index 6c3b9e6..eb07a7f 100644
--- a/src/com/android/settings/deviceinfo/PrivateVolumeSettings.java
+++ b/src/com/android/settings/deviceinfo/PrivateVolumeSettings.java
@@ -228,7 +228,7 @@
         // Add current user and its profiles first
         for (int userIndex = 0; userIndex < userCount; ++userIndex) {
             final UserInfo userInfo = allUsers.get(userIndex);
-            if (isProfileOf(mCurrentUser, userInfo)) {
+            if (Utils.isProfileOf(mCurrentUser, userInfo)) {
                 final PreferenceGroup details = showHeaders ?
                         addCategory(screen, userInfo.name) : screen;
                 addDetailItems(details, showShared, userInfo.id);
@@ -242,7 +242,7 @@
                     getText(R.string.storage_other_users));
             for (int userIndex = 0; userIndex < userCount; ++userIndex) {
                 final UserInfo userInfo = allUsers.get(userIndex);
-                if (!isProfileOf(mCurrentUser, userInfo)) {
+                if (!Utils.isProfileOf(mCurrentUser, userInfo)) {
                     addItem(otherUsers, /* titleRes */ 0, userInfo.name, userInfo.id);
                 }
             }
@@ -649,12 +649,6 @@
         pref.setStorageSize(size, mTotalSize);
     }
 
-    private boolean isProfileOf(UserInfo user, UserInfo profile) {
-        return user.id == profile.id ||
-                (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
-                && user.profileGroupId == profile.profileGroupId);
-    }
-
     private static long totalValues(MeasurementDetails details, int userId, String... keys) {
         long total = 0;
         HashMap<String, Long> map = details.mediaSize.get(userId);
diff --git a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
index 914a8fe..0160534 100644
--- a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
+++ b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.os.Bundle;
+import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
 import android.provider.SearchIndexableResource;
@@ -25,8 +26,11 @@
 
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
+import com.android.settings.applications.UserManagerWrapper;
+import com.android.settings.applications.UserManagerWrapperImpl;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.deviceinfo.storage.SecondaryUserController;
 import com.android.settings.deviceinfo.storage.StorageItemPreferenceController;
 import com.android.settings.deviceinfo.storage.StorageSummaryDonutPreferenceController;
 import com.android.settings.overlay.FeatureFactory;
@@ -110,6 +114,10 @@
         mPreferenceController = new StorageItemPreferenceController(context, this,
                 mVolume, new StorageManagerVolumeProvider(sm));
         controllers.add(mPreferenceController);
+
+        UserManagerWrapper userManager =
+                new UserManagerWrapperImpl(context.getSystemService(UserManager.class));
+        SecondaryUserController.addAllSecondaryUserControllers(context, userManager, controllers);
         controllers.add(new ManageStoragePreferenceController(context));
         return controllers;
     }
diff --git a/src/com/android/settings/deviceinfo/storage/SecondaryUserController.java b/src/com/android/settings/deviceinfo/storage/SecondaryUserController.java
new file mode 100644
index 0000000..b27dfa3
--- /dev/null
+++ b/src/com/android/settings/deviceinfo/storage/SecondaryUserController.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.deviceinfo.storage;
+
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.Utils;
+import com.android.settings.applications.UserManagerWrapper;
+import com.android.settings.core.PreferenceController;
+
+import java.util.List;
+
+/**
+ * SecondaryUserController controls the preferences on the Storage screen which had to do with
+ * secondary users.
+ */
+public class SecondaryUserController extends PreferenceController {
+    // PreferenceGroupKey to try to add our preference onto.
+    private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
+    private static final String PREFERENCE_KEY_BASE = "pref_user_";
+
+    private UserInfo mUser;
+    private StorageItemPreferenceAlternate mPreference;
+
+    /**
+     * Adds the appropriate controllers to a controller list for handling all secondary users on
+     * a device.
+     * @param context Context for initializing the preference controllers.
+     * @param controllers List of preference controllers for a Settings fragment.
+     */
+    public static void addAllSecondaryUserControllers(Context context,
+            UserManagerWrapper userManager, List<PreferenceController> controllers) {
+        UserInfo primaryUser = userManager.getPrimaryUser();
+        boolean addedUser = false;
+        List<UserInfo> infos = userManager.getUsers();
+        for (int i = 0, size = infos.size(); i < size; i++) {
+            UserInfo info = infos.get(i);
+            if (Utils.isProfileOf(primaryUser, info)) {
+                continue;
+            }
+
+            controllers.add(new SecondaryUserController(context, info));
+            addedUser = true;
+        }
+
+        if (!addedUser) {
+            controllers.add(new NoSecondaryUserController(context));
+        }
+    }
+
+    /**
+     * Constructor for a given secondary user.
+     * @param context Context to initialize the underlying {@link PreferenceController}.
+     * @param info {@link UserInfo} for the secondary user which this controllers covers.
+     */
+    @VisibleForTesting
+    SecondaryUserController(Context context, UserInfo info) {
+        super(context);
+        mUser = info;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        if (mPreference == null) {
+            mPreference = new StorageItemPreferenceAlternate(mContext);
+
+            PreferenceGroup group =
+                    (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
+            mPreference.setTitle(mUser.name);
+            mPreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
+            group.setVisible(true);
+            group.addPreference(mPreference);
+        }
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return (mPreference != null) ? mPreference.getKey() : null;
+    }
+
+    /**
+     * Sets the size for the preference.
+     * @param size Size in bytes.
+     */
+    public void setSize(long size) {
+        if (mPreference != null) {
+            mPreference.setStorageSize(size);
+        }
+    }
+
+    private static class NoSecondaryUserController extends PreferenceController {
+        public NoSecondaryUserController(Context context) {
+            super(context);
+        }
+
+        @Override
+        public void displayPreference(PreferenceScreen screen) {
+            PreferenceGroup group =
+                    (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
+            if (group == null) {
+                return;
+            }
+            screen.removePreference(group);
+        }
+
+        @Override
+        public boolean isAvailable() {
+            return true;
+        }
+
+        @Override
+        public String getPreferenceKey() {
+            return null;
+        }
+
+    }
+}
diff --git a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
index 54a65da..7afd061 100644
--- a/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceController.java
@@ -110,6 +110,9 @@
         // TODO: Currently, this reflects the existing behavior for these toggles.
         //       After the intermediate views are built, swap them in.
         Intent intent = null;
+        if (preference.getKey() == null) {
+            return false;
+        }
         switch (preference.getKey()) {
             case PHOTO_KEY:
                 intent = getPhotosIntent();