1 /* 2 * Copyright (C) 2019 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.car.settings.storage; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.car.userlib.CarUserManagerHelper; 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.os.storage.VolumeInfo; 24 import android.util.SparseArray; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.FragmentController; 28 import com.android.car.settings.common.PreferenceController; 29 import com.android.car.settings.common.ProgressBarPreference; 30 31 /** 32 * Controller which have the basic logic to determines the storage for different categories visible 33 * in the storage preference screen. 34 */ 35 public abstract class StorageUsageBasePreferenceController extends 36 PreferenceController<ProgressBarPreference> implements 37 StorageSettingsManager.VolumeListener { 38 39 private static final int PROGRESS_MAX = 100; 40 41 private VolumeInfo mVolumeInfo; 42 private CarUserManagerHelper mCarUserManagerHelper; 43 StorageUsageBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44 public StorageUsageBasePreferenceController(Context context, String preferenceKey, 45 FragmentController fragmentController, 46 CarUxRestrictions uxRestrictions) { 47 super(context, preferenceKey, fragmentController, uxRestrictions); 48 mCarUserManagerHelper = new CarUserManagerHelper(context); 49 } 50 51 @Override getPreferenceType()52 protected Class<ProgressBarPreference> getPreferenceType() { 53 return ProgressBarPreference.class; 54 } 55 56 /** 57 * Calculates the storage used by the category. 58 * 59 * @return usage value in bytes. 60 */ calculateCategoryUsage( SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes)61 protected abstract long calculateCategoryUsage( 62 SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes); 63 64 @Override onCreateInternal()65 protected void onCreateInternal() { 66 getPreference().setSummary(R.string.memory_calculating_size); 67 getPreference().setMax(PROGRESS_MAX); 68 } 69 70 @Override onDataLoaded(SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes, long totalSizeBytes)71 public void onDataLoaded(SparseArray<StorageAsyncLoader.AppsStorageResult> result, 72 long usedSizeBytes, long totalSizeBytes) { 73 setStorageSize(calculateCategoryUsage(result, usedSizeBytes), totalSizeBytes); 74 } 75 getCarUserManagerHelper()76 CarUserManagerHelper getCarUserManagerHelper() { 77 return mCarUserManagerHelper; 78 } 79 getVolumeInfo()80 public VolumeInfo getVolumeInfo() { 81 return mVolumeInfo; 82 } 83 setVolumeInfo(VolumeInfo volumeInfo)84 public void setVolumeInfo(VolumeInfo volumeInfo) { 85 mVolumeInfo = volumeInfo; 86 } 87 88 /** 89 * Sets the storage size for this preference that will be displayed as a summary. It will also 90 * update the progress bar accordingly. 91 */ setStorageSize(long size, long total)92 private void setStorageSize(long size, long total) { 93 getPreference().setSummary( 94 FileSizeFormatter.formatFileSize( 95 getContext(), 96 size, 97 getGigabyteSuffix(getContext().getResources()), 98 FileSizeFormatter.GIGABYTE_IN_BYTES)); 99 int progressPercent; 100 if (total == 0) { 101 progressPercent = 0; 102 } else { 103 progressPercent = (int) (size * PROGRESS_MAX / total); 104 } 105 getPreference().setProgress(progressPercent); 106 } 107 getGigabyteSuffix(Resources res)108 private static int getGigabyteSuffix(Resources res) { 109 return res.getIdentifier("gigabyteShort", "string", "android"); 110 } 111 } 112