2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.deviceinfo.storage;
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.graphics.drawable.Drawable;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.preference.PreferenceGroup;
26 import android.support.v7.preference.PreferenceScreen;
27 import android.util.SparseArray;
29 import com.android.settings.Utils;
30 import com.android.settings.applications.UserManagerWrapper;
31 import com.android.settings.core.PreferenceController;
32 import com.android.settings.deviceinfo.StorageItemPreference;
34 import java.util.ArrayList;
35 import java.util.List;
38 * SecondaryUserController controls the preferences on the Storage screen which had to do with
41 public class SecondaryUserController extends PreferenceController
42 implements StorageAsyncLoader.ResultHandler, UserIconLoader.UserIconHandler {
43 // PreferenceGroupKey to try to add our preference onto.
44 private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
45 private static final String PREFERENCE_KEY_BASE = "pref_user_";
46 private static final int USER_PROFILE_INSERTION_LOCATION = 6;
47 private static final int SIZE_NOT_SET = -1;
49 private @NonNull UserInfo mUser;
50 private @Nullable StorageItemPreference mStoragePreference;
51 private Drawable mUserIcon;
53 private long mTotalSizeBytes;
56 * Adds the appropriate controllers to a controller list for handling all secondary users on
58 * @param context Context for initializing the preference controllers.
59 * @param userManager UserManagerWrapper for figuring out which controllers to add.
61 public static List<PreferenceController> getSecondaryUserControllers(
62 Context context, UserManagerWrapper userManager) {
63 List<PreferenceController> controllers = new ArrayList<>();
64 UserInfo primaryUser = userManager.getPrimaryUser();
65 boolean addedUser = false;
66 List<UserInfo> infos = userManager.getUsers();
67 for (int i = 0, size = infos.size(); i < size; i++) {
68 UserInfo info = infos.get(i);
69 if (info.isPrimary()) {
73 if (info == null || Utils.isProfileOf(primaryUser, info)) {
75 new UserProfileController(
76 context, info, userManager, USER_PROFILE_INSERTION_LOCATION));
80 controllers.add(new SecondaryUserController(context, info));
85 controllers.add(new NoSecondaryUserController(context));
91 * Constructor for a given secondary user.
92 * @param context Context to initialize the underlying {@link PreferenceController}.
93 * @param info {@link UserInfo} for the secondary user which this controllers covers.
96 SecondaryUserController(Context context, @NonNull UserInfo info) {
103 public void displayPreference(PreferenceScreen screen) {
104 if (mStoragePreference == null) {
105 mStoragePreference = new StorageItemPreference(screen.getContext());
107 PreferenceGroup group =
108 (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
109 mStoragePreference.setTitle(mUser.name);
110 mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
111 if (mSize != SIZE_NOT_SET) {
112 mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
115 group.setVisible(true);
116 group.addPreference(mStoragePreference);
122 public boolean isAvailable() {
127 public String getPreferenceKey() {
128 return mStoragePreference != null ? mStoragePreference.getKey() : null;
132 * Returns the user for which this is the secondary user controller.
135 public UserInfo getUser() {
140 * Sets the size for the preference.
141 * @param size Size in bytes.
143 public void setSize(long size) {
145 if (mStoragePreference != null) {
146 mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
151 * Sets the total size for the preference for the progress bar.
152 * @param totalSizeBytes Total size in bytes.
154 public void setTotalSize(long totalSizeBytes) {
155 mTotalSizeBytes = totalSizeBytes;
158 public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
159 int userId = getUser().id;
160 StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
161 if (result != null) {
162 setSize(result.externalStats.totalBytes);
167 public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
168 mUserIcon = fetchedIcons.get(mUser.id);
172 private void maybeSetIcon() {
173 if (mUserIcon != null && mStoragePreference != null) {
174 mStoragePreference.setIcon(mUserIcon);
178 private static class NoSecondaryUserController extends PreferenceController {
179 public NoSecondaryUserController(Context context) {
184 public void displayPreference(PreferenceScreen screen) {
185 PreferenceGroup group =
186 (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
190 screen.removePreference(group);
194 public boolean isAvailable() {
199 public String getPreferenceKey() {