1 /* 2 * Copyright (C) 2011 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; 18 19 import android.animation.TypeEvaluator; 20 import android.animation.ValueAnimator; 21 import android.content.Context; 22 import android.util.AttributeSet; 23 import android.widget.ProgressBar; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 import com.android.settings.deviceinfo.storage.StorageUtils; 31 32 public class StorageItemPreference extends Preference { 33 public int userHandle; 34 35 private static final int UNINITIALIZED = -1; 36 private static final int ANIMATE_DURATION_IN_MILLIS = 1000; 37 38 private ProgressBar mProgressBar; 39 private static final int PROGRESS_MAX = 100; 40 private int mProgressPercent = UNINITIALIZED; 41 private long mStorageSize; 42 StorageItemPreference(Context context)43 public StorageItemPreference(Context context) { 44 this(context, null); 45 } 46 StorageItemPreference(Context context, AttributeSet attrs)47 public StorageItemPreference(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 setLayoutResource(R.layout.storage_item); 50 } 51 52 @VisibleForTesting setStorageSize(long size, long total)53 public void setStorageSize(long size, long total) { 54 setStorageSize(size, total, false /* animate */); 55 } 56 57 /** 58 * Set the storage size info with/without animation 59 */ setStorageSize(long size, long total, boolean animate)60 public void setStorageSize(long size, long total, boolean animate) { 61 if (animate) { 62 TypeEvaluator<Long> longEvaluator = 63 (fraction, startValue, endValue) -> { 64 // Directly returns end value if fraction is 1.0 and the end value is 0. 65 if (fraction >= 1.0f && endValue == 0) { 66 return endValue; 67 } 68 return startValue + (long) (fraction * (endValue - startValue)); 69 }; 70 ValueAnimator valueAnimator = ValueAnimator.ofObject(longEvaluator, mStorageSize, size); 71 valueAnimator.setDuration(ANIMATE_DURATION_IN_MILLIS); 72 valueAnimator.addUpdateListener( 73 animation -> { 74 updateProgressBarAndSizeInfo((long) animation.getAnimatedValue(), total); 75 }); 76 valueAnimator.start(); 77 } else { 78 updateProgressBarAndSizeInfo(size, total); 79 } 80 mStorageSize = size; 81 } 82 getStorageSize()83 public long getStorageSize() { 84 return mStorageSize; 85 } 86 updateProgressBar()87 protected void updateProgressBar() { 88 if (mProgressBar == null || mProgressPercent == UNINITIALIZED) { 89 return; 90 } 91 92 mProgressBar.setMax(PROGRESS_MAX); 93 mProgressBar.setProgress(mProgressPercent); 94 } 95 updateProgressBarAndSizeInfo(long size, long total)96 private void updateProgressBarAndSizeInfo(long size, long total) { 97 setSummary(StorageUtils.getStorageSizeLabel(getContext(), size)); 98 mProgressPercent = total == 0 ? 0 : (int) (size * PROGRESS_MAX / total); 99 updateProgressBar(); 100 } 101 102 @Override onBindViewHolder(PreferenceViewHolder view)103 public void onBindViewHolder(PreferenceViewHolder view) { 104 mProgressBar = (ProgressBar) view.findViewById(android.R.id.progress); 105 updateProgressBar(); 106 super.onBindViewHolder(view); 107 } 108 } 109