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