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.PreferenceController; 34 import com.android.settings.deviceinfo.StorageItemPreference; 35 import com.android.settings.deviceinfo.StorageProfileFragment; 36 import com.android.settingslib.drawer.SettingsDrawerActivity; 37 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; 47 UserProfileController( Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder)48 public UserProfileController( 49 Context context, UserInfo info, UserManagerWrapper userManager, int preferenceOrder) { 50 super(context); 51 mUser = Preconditions.checkNotNull(info); 52 mUserManager = userManager; 53 mPreferenceOrder = preferenceOrder; 54 } 55 56 @Override isAvailable()57 public boolean isAvailable() { 58 return true; 59 } 60 61 @Override getPreferenceKey()62 public String getPreferenceKey() { 63 return PREFERENCE_KEY_BASE + mUser.id; 64 } 65 66 @Override displayPreference(PreferenceScreen screen)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); 73 } 74 75 @Override handlePreferenceTreeClick(Preference preference)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); 86 return true; 87 } 88 89 return false; 90 } 91 92 @Override handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats)93 public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) { 94 Preconditions.checkNotNull(stats); 95 96 int userId = mUser.id; 97 StorageAsyncLoader.AppsStorageResult result = stats.get(userId); 98 if (result != null) { 99 setSize( 100 result.externalStats.totalBytes 101 + result.otherAppsSize 102 + result.videoAppsSize 103 + result.musicAppsSize 104 + result.gamesSize, 105 mTotalSizeBytes); 106 } 107 } 108 109 /** 110 * Sets the size for the preference using a byte count. 111 */ setSize(long size, long totalSize)112 public void setSize(long size, long totalSize) { 113 if (mStoragePreference != null) { 114 mStoragePreference.setStorageSize(size, totalSize); 115 } 116 } 117 setTotalSize(long totalSize)118 public void setTotalSize(long totalSize) { 119 mTotalSizeBytes = totalSize; 120 } 121 122 @Override handleUserIcons(SparseArray<Drawable> fetchedIcons)123 public void handleUserIcons(SparseArray<Drawable> fetchedIcons) { 124 Drawable userIcon = fetchedIcons.get(mUser.id); 125 if (userIcon != null) { 126 mStoragePreference.setIcon(userIcon); 127 } 128 } 129 } 130