OSDN Git Service

Back-port ag/2491664 am: 1d21738bd6 am: 4150739791 -s ours am: 9d889600a9 am: f6b126...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / storage / UserProfileController.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.Intent;
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.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 import android.util.SparseArray;
28
29 import com.android.internal.logging.nano.MetricsProto;
30 import com.android.internal.util.Preconditions;
31 import com.android.settings.Utils;
32 import com.android.settings.applications.UserManagerWrapper;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.deviceinfo.StorageItemPreference;
35 import com.android.settings.deviceinfo.StorageProfileFragment;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.drawer.SettingsDrawerActivity;
38
39 /** Defines a {@link AbstractPreferenceController} which handles a single profile of the primary
40  *  user. */
41 public class UserProfileController extends AbstractPreferenceController implements
42         PreferenceControllerMixin, StorageAsyncLoader.ResultHandler,
43         UserIconLoader.UserIconHandler {
44     private static final String PREFERENCE_KEY_BASE = "pref_profile_";
45     private StorageItemPreference mStoragePreference;
46     private UserManagerWrapper mUserManager;
47     private UserInfo mUser;
48     private long mTotalSizeBytes;
49     private final int mPreferenceOrder;
50
51     public UserProfileController(
52             Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder) {
53         super(context);
54         mUser = Preconditions.checkNotNull(info);
55         mUserManager = userManager;
56         mPreferenceOrder = preferenceOrder;
57     }
58
59     @Override
60     public boolean isAvailable() {
61         return true;
62     }
63
64     @Override
65     public String getPreferenceKey() {
66         return PREFERENCE_KEY_BASE + mUser.id;
67     }
68
69     @Override
70     public void displayPreference(PreferenceScreen screen) {
71         mStoragePreference = new StorageItemPreference(screen.getContext());
72         mStoragePreference.setOrder(mPreferenceOrder);
73         mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
74         mStoragePreference.setTitle(mUser.name);
75         screen.addPreference(mStoragePreference);
76     }
77
78     @Override
79     public boolean handlePreferenceTreeClick(Preference preference) {
80         if (preference != null && mStoragePreference == preference) {
81             Bundle args = new Bundle(2);
82             args.putInt(StorageProfileFragment.USER_ID_EXTRA, mUser.id);
83             args.putString(VolumeInfo.EXTRA_VOLUME_ID, VolumeInfo.ID_PRIVATE_INTERNAL);
84             Intent intent = Utils.onBuildStartFragmentIntent(mContext,
85                     StorageProfileFragment.class.getName(), args, null, 0,
86                     mUser.name, false, MetricsProto.MetricsEvent.DEVICEINFO_STORAGE);
87             intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true);
88             mContext.startActivity(intent);
89             return true;
90         }
91
92         return false;
93     }
94
95     @Override
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(
103                     result.externalStats.totalBytes
104                             + result.otherAppsSize
105                             + result.videoAppsSize
106                             + result.musicAppsSize
107                             + result.gamesSize,
108                     mTotalSizeBytes);
109         }
110     }
111
112     /**
113      * Sets the size for the preference using a byte count.
114      */
115     public void setSize(long size, long totalSize) {
116         if (mStoragePreference != null) {
117             mStoragePreference.setStorageSize(size, totalSize);
118         }
119     }
120
121     public void setTotalSize(long totalSize) {
122         mTotalSizeBytes = totalSize;
123     }
124
125     @Override
126     public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
127         Drawable userIcon = fetchedIcons.get(mUser.id);
128         if (userIcon != null) {
129             mStoragePreference.setIcon(userIcon);
130         }
131     }
132 }