OSDN Git Service

Merge "Never store battery stats for cache" into oc-dr1-dev am: 8f8797f768
[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.PreferenceControllerMixin;
32 import com.android.settings.deviceinfo.StorageItemPreference;
33 import com.android.settingslib.core.AbstractPreferenceController;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39  * SecondaryUserController controls the preferences on the Storage screen which had to do with
40  * secondary users.
41  */
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;
50
51     private @NonNull UserInfo mUser;
52     private @Nullable StorageItemPreference mStoragePreference;
53     private Drawable mUserIcon;
54     private long mSize;
55     private long mTotalSizeBytes;
56
57     /**
58      * Adds the appropriate controllers to a controller list for handling all secondary users on
59      * a device.
60      * @param context Context for initializing the preference controllers.
61      * @param userManager UserManagerWrapper for figuring out which controllers to add.
62      */
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()) {
72                 continue;
73             }
74
75             if (info == null || Utils.isProfileOf(primaryUser, info)) {
76                 controllers.add(
77                         new UserProfileController(
78                                 context, info, userManager, USER_PROFILE_INSERTION_LOCATION));
79                 continue;
80             }
81
82             controllers.add(new SecondaryUserController(context, info));
83             addedUser = true;
84         }
85
86         if (!addedUser) {
87             controllers.add(new NoSecondaryUserController(context));
88         }
89         return controllers;
90     }
91
92     /**
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.
96      */
97     @VisibleForTesting
98     SecondaryUserController(Context context, @NonNull UserInfo info) {
99         super(context);
100         mUser = info;
101         mSize = SIZE_NOT_SET;
102     }
103
104     @Override
105     public void displayPreference(PreferenceScreen screen) {
106         if (mStoragePreference == null) {
107             mStoragePreference = new StorageItemPreference(screen.getContext());
108
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);
115             }
116
117             group.setVisible(true);
118             group.addPreference(mStoragePreference);
119             maybeSetIcon();
120         }
121     }
122
123     @Override
124     public boolean isAvailable() {
125         return true;
126     }
127
128     @Override
129     public String getPreferenceKey() {
130         return mStoragePreference != null ? mStoragePreference.getKey() : null;
131     }
132
133     /**
134      * Returns the user for which this is the secondary user controller.
135      */
136     @NonNull
137     public UserInfo getUser() {
138         return mUser;
139     }
140
141     /**
142      * Sets the size for the preference.
143      * @param size Size in bytes.
144      */
145     public void setSize(long size) {
146         mSize = size;
147         if (mStoragePreference != null) {
148             mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
149         }
150     }
151
152     /**
153      * Sets the total size for the preference for the progress bar.
154      * @param totalSizeBytes Total size in bytes.
155      */
156     public void setTotalSize(long totalSizeBytes) {
157         mTotalSizeBytes = totalSizeBytes;
158     }
159
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);
165         }
166     }
167
168     @Override
169     public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
170         mUserIcon = fetchedIcons.get(mUser.id);
171         maybeSetIcon();
172     }
173
174     private void maybeSetIcon() {
175         if (mUserIcon != null && mStoragePreference != null) {
176             mStoragePreference.setIcon(mUserIcon);
177         }
178     }
179
180     private static class NoSecondaryUserController extends AbstractPreferenceController implements
181             PreferenceControllerMixin {
182         public NoSecondaryUserController(Context context) {
183             super(context);
184         }
185
186         @Override
187         public void displayPreference(PreferenceScreen screen) {
188             PreferenceGroup group =
189                     (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
190             if (group == null) {
191                 return;
192             }
193             screen.removePreference(group);
194         }
195
196         @Override
197         public boolean isAvailable() {
198             return true;
199         }
200
201         @Override
202         public String getPreferenceKey() {
203             return null;
204         }
205
206     }
207 }