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