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.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceViewHolder;
22 import android.text.format.Formatter;
23 import android.view.View;
24 import android.widget.ProgressBar;
25 
26 import com.android.settings.R;
27 
28 public class StorageItemPreference extends Preference {
29     public int userHandle;
30 
31     private ProgressBar progressBar;
32     private static final int PROGRESS_MAX = 100;
33     private int progress = -1;
34 
StorageItemPreference(Context context)35     public StorageItemPreference(Context context) {
36         super(context);
37         setLayoutResource(R.layout.storage_item);
38     }
39 
setStorageSize(long size, long total)40     public void setStorageSize(long size, long total) {
41         setSummary(size == 0
42                 ? String.valueOf(0)
43                 : Formatter.formatFileSize(getContext(), size));
44         if (total == 0) {
45             progress = 0;
46         } else {
47             progress = (int)(size * PROGRESS_MAX / total);
48         }
49         updateProgressBar();
50     }
51 
updateProgressBar()52     protected void updateProgressBar() {
53         if (progressBar == null)
54             return;
55 
56         if (progress == -1) {
57             progressBar.setVisibility(View.GONE);
58             return;
59         }
60 
61         progressBar.setVisibility(View.VISIBLE);
62         progressBar.setMax(PROGRESS_MAX);
63         progressBar.setProgress(progress);
64     }
65 
66     @Override
onBindViewHolder(PreferenceViewHolder view)67     public void onBindViewHolder(PreferenceViewHolder view) {
68         progressBar = (ProgressBar) view.findViewById(android.R.id.progress);
69         updateProgressBar();
70         super.onBindViewHolder(view);
71     }
72 }
73