OSDN Git Service

Merge "Fix search indexing for encryption_and_credential page" into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / storage / SecondaryUserController.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings.deviceinfo.storage;
18
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;
28
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;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38  * SecondaryUserController controls the preferences on the Storage screen which had to do with
39  * secondary users.
40  */
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;
48
49     private @NonNull UserInfo mUser;
50     private @Nullable StorageItemPreference mStoragePreference;
51     private Drawable mUserIcon;
52     private long mSize;
53     private long mTotalSizeBytes;
54
55     /**
56      * Adds the appropriate controllers to a controller list for handling all secondary users on
57      * a device.
58      * @param context Context for initializing the preference controllers.
59      * @param userManager UserManagerWrapper for figuring out which controllers to add.
60      */
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()) {
70                 continue;
71             }
72
73             if (info == null || Utils.isProfileOf(primaryUser, info)) {
74                 controllers.add(
75                         new UserProfileController(
76                                 context, info, userManager, USER_PROFILE_INSERTION_LOCATION));
77                 continue;
78             }
79
80             controllers.add(new SecondaryUserController(context, info));
81             addedUser = true;
82         }
83
84         if (!addedUser) {
85             controllers.add(new NoSecondaryUserController(context));
86         }
87         return controllers;
88     }
89
90     /**
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.
94      */
95     @VisibleForTesting
96     SecondaryUserController(Context context, @NonNull UserInfo info) {
97         super(context);
98         mUser = info;
99         mSize = SIZE_NOT_SET;
100     }
101
102     @Override
103     public void displayPreference(PreferenceScreen screen) {
104         if (mStoragePreference == null) {
105             mStoragePreference = new StorageItemPreference(screen.getContext());
106
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);
113             }
114
115             group.setVisible(true);
116             group.addPreference(mStoragePreference);
117             maybeSetIcon();
118         }
119     }
120
121     @Override
122     public boolean isAvailable() {
123         return true;
124     }
125
126     @Override
127     public String getPreferenceKey() {
128         return mStoragePreference != null ? mStoragePreference.getKey() : null;
129     }
130
131     /**
132      * Returns the user for which this is the secondary user controller.
133      */
134     @NonNull
135     public UserInfo getUser() {
136         return mUser;
137     }
138
139     /**
140      * Sets the size for the preference.
141      * @param size Size in bytes.
142      */
143     public void setSize(long size) {
144         mSize = size;
145         if (mStoragePreference != null) {
146             mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
147         }
148     }
149
150     /**
151      * Sets the total size for the preference for the progress bar.
152      * @param totalSizeBytes Total size in bytes.
153      */
154     public void setTotalSize(long totalSizeBytes) {
155         mTotalSizeBytes = totalSizeBytes;
156     }
157
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);
163         }
164     }
165
166     @Override
167     public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
168         mUserIcon = fetchedIcons.get(mUser.id);
169         maybeSetIcon();
170     }
171
172     private void maybeSetIcon() {
173         if (mUserIcon != null && mStoragePreference != null) {
174             mStoragePreference.setIcon(mUserIcon);
175         }
176     }
177
178     private static class NoSecondaryUserController extends PreferenceController {
179         public NoSecondaryUserController(Context context) {
180             super(context);
181         }
182
183         @Override
184         public void displayPreference(PreferenceScreen screen) {
185             PreferenceGroup group =
186                     (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
187             if (group == null) {
188                 return;
189             }
190             screen.removePreference(group);
191         }
192
193         @Override
194         public boolean isAvailable() {
195             return true;
196         }
197
198         @Override
199         public String getPreferenceKey() {
200             return null;
201         }
202
203     }
204 }