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