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