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.content.res.Resources;
21 import android.support.v7.preference.Preference;
22 import android.support.v7.preference.PreferenceViewHolder;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.ProgressBar;
26 
27 import com.android.settings.R;
28 import com.android.settings.utils.FileSizeFormatter;
29 
30 public class StorageItemPreference extends Preference {
31     public int userHandle;
32 
33     private ProgressBar mProgressBar;
34     private static final int PROGRESS_MAX = 100;
35     private int mProgressPercent = -1;
36 
StorageItemPreference(Context context)37     public StorageItemPreference(Context context) {
38         this(context, null);
39     }
40 
StorageItemPreference(Context context, AttributeSet attrs)41     public StorageItemPreference(Context context, AttributeSet attrs) {
42         super(context, attrs);
43         setLayoutResource(R.layout.storage_item);
44         setSummary(R.string.memory_calculating_size);
45     }
46 
setStorageSize(long size, long total)47     public void setStorageSize(long size, long total) {
48         setSummary(
49                 FileSizeFormatter.formatFileSize(
50                         getContext(),
51                         size,
52                         getGigabyteSuffix(getContext().getResources()),
53                         FileSizeFormatter.GIGABYTE_IN_BYTES));
54         if (total == 0) {
55             mProgressPercent = 0;
56         } else {
57             mProgressPercent = (int)(size * PROGRESS_MAX / total);
58         }
59         updateProgressBar();
60     }
61 
updateProgressBar()62     protected void updateProgressBar() {
63         if (mProgressBar == null)
64             return;
65 
66         if (mProgressPercent == -1) {
67             mProgressBar.setVisibility(View.GONE);
68             return;
69         }
70 
71         mProgressBar.setVisibility(View.VISIBLE);
72         mProgressBar.setMax(PROGRESS_MAX);
73         mProgressBar.setProgress(mProgressPercent);
74     }
75 
76     @Override
onBindViewHolder(PreferenceViewHolder view)77     public void onBindViewHolder(PreferenceViewHolder view) {
78         mProgressBar = (ProgressBar) view.findViewById(android.R.id.progress);
79         updateProgressBar();
80         super.onBindViewHolder(view);
81     }
82 
getGigabyteSuffix(Resources res)83     private static int getGigabyteSuffix(Resources res) {
84         return res.getIdentifier("gigabyteShort", "string", "android");
85     }
86 }
87