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