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.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.os.storage.VolumeInfo;
25 import android.util.SparseArray;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.internal.util.Preconditions;
31 import com.android.settings.Utils;
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settings.core.SubSettingLauncher;
34 import com.android.settings.deviceinfo.StorageItemPreference;
35 import com.android.settings.deviceinfo.StorageProfileFragment;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 
38 /**
39  * Defines a {@link AbstractPreferenceController} which handles a single profile of the primary
40  * user.
41  */
42 public class UserProfileController extends AbstractPreferenceController implements
43         PreferenceControllerMixin, StorageAsyncLoader.ResultHandler,
44         UserIconLoader.UserIconHandler {
45     private static final String PREFERENCE_KEY_BASE = "pref_profile_";
46     private StorageItemPreference mStoragePreference;
47     private UserInfo mUser;
48     private long mTotalSizeBytes;
49     private final int mPreferenceOrder;
50 
UserProfileController(Context context, UserInfo info, int preferenceOrder)51     public UserProfileController(Context context, UserInfo info, int preferenceOrder) {
52         super(context);
53         mUser = Preconditions.checkNotNull(info);
54         mPreferenceOrder = preferenceOrder;
55     }
56 
57     @Override
isAvailable()58     public boolean isAvailable() {
59         return true;
60     }
61 
62     @Override
getPreferenceKey()63     public String getPreferenceKey() {
64         return PREFERENCE_KEY_BASE + mUser.id;
65     }
66 
67     @Override
displayPreference(PreferenceScreen screen)68     public void displayPreference(PreferenceScreen screen) {
69         mStoragePreference = new StorageItemPreference(screen.getContext());
70         mStoragePreference.setOrder(mPreferenceOrder);
71         mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
72         mStoragePreference.setTitle(mUser.name);
73         screen.addPreference(mStoragePreference);
74     }
75 
76     @Override
handlePreferenceTreeClick(Preference preference)77     public boolean handlePreferenceTreeClick(Preference preference) {
78         if (preference != null && mStoragePreference == preference) {
79             final Bundle args = new Bundle();
80             args.putInt(StorageProfileFragment.USER_ID_EXTRA, mUser.id);
81             args.putString(VolumeInfo.EXTRA_VOLUME_ID, VolumeInfo.ID_PRIVATE_INTERNAL);
82 
83             new SubSettingLauncher(mContext)
84                     .setDestination(StorageProfileFragment.class.getName())
85                     .setArguments(args)
86                     .setTitleText(mUser.name)
87                     .setSourceMetricsCategory(SettingsEnums.DEVICEINFO_STORAGE)
88                     .launch();
89             return true;
90         }
91 
92         return false;
93     }
94 
95     @Override
handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats)96     public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
97         Preconditions.checkNotNull(stats);
98 
99         int userId = mUser.id;
100         StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
101         if (result != null) {
102             setSize(result.externalStats.totalBytes
103                             + result.otherAppsSize
104                             + result.videoAppsSize
105                             + result.musicAppsSize
106                             + result.gamesSize,
107                     mTotalSizeBytes);
108         }
109     }
110 
111     /**
112      * Sets the size for the preference using a byte count.
113      */
setSize(long size, long totalSize)114     public void setSize(long size, long totalSize) {
115         if (mStoragePreference != null) {
116             mStoragePreference.setStorageSize(size, totalSize);
117         }
118     }
119 
setTotalSize(long totalSize)120     public void setTotalSize(long totalSize) {
121         mTotalSizeBytes = totalSize;
122     }
123 
124     @Override
handleUserIcons(SparseArray<Drawable> fetchedIcons)125     public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
126         Drawable userIcon = fetchedIcons.get(mUser.id);
127         if (userIcon != null) {
128             mStoragePreference.setIcon(applyTint(mContext, userIcon));
129         }
130     }
131 
applyTint(Context context, Drawable icon)132     private static Drawable applyTint(Context context, Drawable icon) {
133         icon = icon.mutate();
134         icon.setTintList(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
135         return icon;
136     }
137 
138 }
139