Home | History | Annotate | Download | only in storage
      1 /*
      2  * Copyright (C) 2015 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.tv.settings.device.storage;
     18 
     19 import android.content.Context;
     20 import android.support.v7.preference.Preference;
     21 import android.text.format.Formatter;
     22 import android.util.AttributeSet;
     23 
     24 import com.android.tv.settings.R;
     25 
     26 public class StoragePreference extends Preference {
     27 
     28     private static final long SIZE_CALCULATING = -1;
     29 
     30     public StoragePreference(Context context, AttributeSet attrs,
     31             int defStyleAttr, int defStyleRes) {
     32         super(context, attrs, defStyleAttr, defStyleRes);
     33         setSize(SIZE_CALCULATING);
     34     }
     35 
     36     public StoragePreference(Context context, AttributeSet attrs,
     37             int defStyleAttr) {
     38         super(context, attrs, defStyleAttr);
     39         setSize(SIZE_CALCULATING);
     40     }
     41 
     42     public StoragePreference(Context context, AttributeSet attrs) {
     43         super(context, attrs);
     44         setSize(SIZE_CALCULATING);
     45     }
     46 
     47     public StoragePreference(Context context) {
     48         super(context);
     49         setSize(SIZE_CALCULATING);
     50     }
     51 
     52     public void setSize(long size) {
     53         setSummary(formatSize(getContext(), size));
     54     }
     55 
     56     public static String formatSize(Context context, long size) {
     57         return (size == SIZE_CALCULATING)
     58                 ? context.getString(R.string.storage_calculating_size)
     59                 : Formatter.formatShortFileSize(context, size);
     60     }
     61 }
     62