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.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;
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;
39 /** Defines a {@link AbstractPreferenceController} which handles a single profile of the primary
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;
51 public UserProfileController(
52 Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder) {
54 mUser = Preconditions.checkNotNull(info);
55 mUserManager = userManager;
56 mPreferenceOrder = preferenceOrder;
60 public boolean isAvailable() {
65 public String getPreferenceKey() {
66 return PREFERENCE_KEY_BASE + mUser.id;
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);
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);
96 public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
97 Preconditions.checkNotNull(stats);
99 int userId = mUser.id;
100 StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
101 if (result != null) {
103 result.externalStats.totalBytes
104 + result.otherAppsSize
105 + result.videoAppsSize
106 + result.musicAppsSize
113 * Sets the size for the preference using a byte count.
115 public void setSize(long size, long totalSize) {
116 if (mStoragePreference != null) {
117 mStoragePreference.setStorageSize(size, totalSize);
121 public void setTotalSize(long totalSize) {
122 mTotalSizeBytes = totalSize;
126 public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
127 Drawable userIcon = fetchedIcons.get(mUser.id);
128 if (userIcon != null) {
129 mStoragePreference.setIcon(userIcon);