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.PreferenceController;
34 import com.android.settings.deviceinfo.StorageItemPreference;
35 import com.android.settings.deviceinfo.StorageProfileFragment;
36 import com.android.settingslib.drawer.SettingsDrawerActivity;
38 /** Defines a {@link PreferenceController} which handles a single profile of the primary user. */
39 public class UserProfileController extends PreferenceController
40 implements StorageAsyncLoader.ResultHandler, UserIconLoader.UserIconHandler {
41 private static final String PREFERENCE_KEY_BASE = "pref_profile_";
42 private StorageItemPreference mStoragePreference;
43 private UserManagerWrapper mUserManager;
44 private UserInfo mUser;
45 private long mTotalSizeBytes;
46 private final int mPreferenceOrder;
48 public UserProfileController(
49 Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder) {
51 mUser = Preconditions.checkNotNull(info);
52 mUserManager = userManager;
53 mPreferenceOrder = preferenceOrder;
57 public boolean isAvailable() {
62 public String getPreferenceKey() {
63 return PREFERENCE_KEY_BASE + mUser.id;
67 public void displayPreference(PreferenceScreen screen) {
68 mStoragePreference = new StorageItemPreference(screen.getContext());
69 mStoragePreference.setOrder(mPreferenceOrder);
70 mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
71 mStoragePreference.setTitle(mUser.name);
72 screen.addPreference(mStoragePreference);
76 public boolean handlePreferenceTreeClick(Preference preference) {
77 if (preference != null && mStoragePreference == preference) {
78 Bundle args = new Bundle(2);
79 args.putInt(StorageProfileFragment.USER_ID_EXTRA, mUser.id);
80 args.putString(VolumeInfo.EXTRA_VOLUME_ID, VolumeInfo.ID_PRIVATE_INTERNAL);
81 Intent intent = Utils.onBuildStartFragmentIntent(mContext,
82 StorageProfileFragment.class.getName(), args, null, 0,
83 mUser.name, false, MetricsProto.MetricsEvent.DEVICEINFO_STORAGE);
84 intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true);
85 mContext.startActivity(intent);
93 public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
94 Preconditions.checkNotNull(stats);
96 int userId = mUser.id;
97 StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
100 result.externalStats.totalBytes
101 + result.otherAppsSize
102 + result.videoAppsSize
103 + result.musicAppsSize
110 * Sets the size for the preference using a byte count.
112 public void setSize(long size, long totalSize) {
113 if (mStoragePreference != null) {
114 mStoragePreference.setStorageSize(size, totalSize);
118 public void setTotalSize(long totalSize) {
119 mTotalSizeBytes = totalSize;
123 public void handleUserIcons(SparseArray<Drawable> fetchedIcons) {
124 Drawable userIcon = fetchedIcons.get(mUser.id);
125 if (userIcon != null) {
126 mStoragePreference.setIcon(userIcon);