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.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.ProgressBar;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.settings.R;
27 import com.android.settings.deviceinfo.storage.StorageUtils;
28 
29 public class StorageItemPreference extends Preference {
30     public int userHandle;
31 
32     private static final int UNINITIALIZED = -1;
33 
34     private ProgressBar mProgressBar;
35     private static final int PROGRESS_MAX = 100;
36     private int mProgressPercent = UNINITIALIZED;
37     private long mStorageSize;
38 
StorageItemPreference(Context context)39     public StorageItemPreference(Context context) {
40         this(context, null);
41     }
42 
StorageItemPreference(Context context, AttributeSet attrs)43     public StorageItemPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         setLayoutResource(R.layout.storage_item);
46     }
47 
setStorageSize(long size, long total)48     public void setStorageSize(long size, long total) {
49         mStorageSize = size;
50         setSummary(StorageUtils.getStorageSizeLabel(getContext(), size));
51 
52         if (total == 0) {
53             mProgressPercent = 0;
54         } else {
55             mProgressPercent = (int)(size * PROGRESS_MAX / total);
56         }
57         updateProgressBar();
58     }
59 
getStorageSize()60     public long getStorageSize() {
61         return mStorageSize;
62     }
63 
updateProgressBar()64     protected void updateProgressBar() {
65         if (mProgressBar == null || mProgressPercent == UNINITIALIZED)
66             return;
67 
68         mProgressBar.setMax(PROGRESS_MAX);
69         mProgressBar.setProgress(mProgressPercent);
70     }
71 
72     @Override
onBindViewHolder(PreferenceViewHolder view)73     public void onBindViewHolder(PreferenceViewHolder view) {
74         mProgressBar = (ProgressBar) view.findViewById(android.R.id.progress);
75         updateProgressBar();
76         super.onBindViewHolder(view);
77     }
78 }
79