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