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.PreferenceControllerMixin;
32 import com.android.settings.deviceinfo.StorageItemPreference;
33 import com.android.settingslib.core.AbstractPreferenceController;
35 import java.util.ArrayList;
36 import java.util.List;
39 * SecondaryUserController controls the preferences on the Storage screen which had to do with
42 public class SecondaryUserController extends AbstractPreferenceController implements
43 PreferenceControllerMixin, StorageAsyncLoader.ResultHandler,
44 UserIconLoader.UserIconHandler {
45 // PreferenceGroupKey to try to add our preference onto.
46 private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
47 private static final String PREFERENCE_KEY_BASE = "pref_user_";
48 private static final int USER_PROFILE_INSERTION_LOCATION = 6;
49 private static final int SIZE_NOT_SET = -1;
51 private @NonNull UserInfo mUser;
52 private @Nullable StorageItemPreference mStoragePreference;
53 private Drawable mUserIcon;
55 private long mTotalSizeBytes;
58 * Adds the appropriate controllers to a controller list for handling all secondary users on
60 * @param context Context for initializing the preference controllers.
61 * @param userManager UserManagerWrapper for figuring out which controllers to add.
63 public static List<AbstractPreferenceController> getSecondaryUserControllers(
64 Context context, UserManagerWrapper userManager) {
65 List<AbstractPreferenceController> controllers = new ArrayList<>();
66 UserInfo primaryUser = userManager.getPrimaryUser();
67 boolean addedUser = false;
68 List<UserInfo> infos = userManager.getUsers();
69 for (int i = 0, size = infos.size(); i < size; i++) {
70 UserInfo info = infos.get(i);
71 if (info.isPrimary()) {
75 if (info == null || Utils.isProfileOf(primaryUser, info)) {
77 new UserProfileController(
78 context, info, userManager, USER_PROFILE_INSERTION_LOCATION));
82 controllers.add(new SecondaryUserController(context, info));
87 controllers.add(new NoSecondaryUserController(context));
93 * Constructor for a given secondary user.
94 * @param context Context to initialize the underlying {@link AbstractPreferenceController}.
95 * @param info {@link UserInfo} for the secondary user which this controllers covers.
98 SecondaryUserController(Context context, @NonNull UserInfo info) {
101 mSize = SIZE_NOT_SET;
105 public void displayPreference(PreferenceScreen screen) {
106 if (mStoragePreference == null) {
107 mStoragePreference = new StorageItemPreference(screen.getContext());
109 PreferenceGroup group =
110 (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
111 mStoragePreference.setTitle(mUser.name);
112 mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
113 if (mSize != SIZE_NOT_SET) {
114 mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
117 group.setVisible(true);
118 group.addPreference(mStoragePreference);
124 public boolean isAvailable() {
129 public String getPreferenceKey() {
130 return mStoragePreference != null ? mStoragePreference.getKey() : null;
134 * Returns the user for which this is the secondary user controller.
137 public UserInfo getUser() {
142 * Sets the size for the preference.
143 * @param size Size in bytes.
145 public void setSize(long size) {
147 if (mStoragePreference != null) {
148 mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
153 * Sets the total size for the preference for the progress bar.
154 * @param totalSizeBytes Total size in bytes.
156 public void setTotalSize(long totalSizeBytes) {
157 mTotalSizeBytes = totalSizeBytes;
160 public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
161 int userId = getUser().id;
162 StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
163 if (result != null) {
164 setSize(result.externalStats.totalBytes);
169 public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
170 mUserIcon = fetchedIcons.get(mUser.id);
174 private void maybeSetIcon() {
175 if (mUserIcon != null && mStoragePreference != null) {
176 mStoragePreference.setIcon(mUserIcon);
180 private static class NoSecondaryUserController extends AbstractPreferenceController implements
181 PreferenceControllerMixin {
182 public NoSecondaryUserController(Context context) {
187 public void displayPreference(PreferenceScreen screen) {
188 PreferenceGroup group =
189 (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
193 screen.removePreference(group);
197 public boolean isAvailable() {
202 public String getPreferenceKey() {