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.content.Context; 21 import android.content.res.Resources; 22 import android.icu.util.MeasureUnit; 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 StorageUsageBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43 public StorageUsageBasePreferenceController(Context context, String preferenceKey, 44 FragmentController fragmentController, 45 CarUxRestrictions uxRestrictions) { 46 super(context, preferenceKey, fragmentController, uxRestrictions); 47 } 48 49 @Override getPreferenceType()50 protected Class<ProgressBarPreference> getPreferenceType() { 51 return ProgressBarPreference.class; 52 } 53 54 /** 55 * Calculates the storage used by the category. 56 * 57 * @return usage value in bytes. 58 */ calculateCategoryUsage( SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes)59 protected abstract long calculateCategoryUsage( 60 SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes); 61 62 @Override onCreateInternal()63 protected void onCreateInternal() { 64 getPreference().setSummary(R.string.memory_calculating_size); 65 getPreference().setMax(PROGRESS_MAX); 66 } 67 68 @Override onDataLoaded(SparseArray<StorageAsyncLoader.AppsStorageResult> result, long usedSizeBytes, long totalSizeBytes)69 public void onDataLoaded(SparseArray<StorageAsyncLoader.AppsStorageResult> result, 70 long usedSizeBytes, long totalSizeBytes) { 71 setStorageSize(calculateCategoryUsage(result, usedSizeBytes), totalSizeBytes); 72 } 73 getVolumeInfo()74 public VolumeInfo getVolumeInfo() { 75 return mVolumeInfo; 76 } 77 setVolumeInfo(VolumeInfo volumeInfo)78 public void setVolumeInfo(VolumeInfo volumeInfo) { 79 mVolumeInfo = volumeInfo; 80 } 81 82 /** 83 * Sets the storage size for this preference that will be displayed as a summary. It will also 84 * update the progress bar accordingly. 85 */ setStorageSize(long size, long total)86 private void setStorageSize(long size, long total) { 87 getPreference().setSummary( 88 FileSizeFormatter.formatFileSize( 89 getContext(), 90 size, 91 MeasureUnit.GIGABYTE, 92 FileSizeFormatter.GIGABYTE_IN_BYTES)); 93 int progressPercent; 94 if (total == 0) { 95 progressPercent = 0; 96 } else { 97 progressPercent = (int) (size * PROGRESS_MAX / total); 98 } 99 getPreference().setProgress(progressPercent); 100 } 101 getGigabyteSuffix(Resources res)102 private static int getGigabyteSuffix(Resources res) { 103 return res.getIdentifier("gigabyteShort", "string", "android"); 104 } 105 } 106