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.settings.deviceinfo; 18 19 import android.content.Context; 20 import android.graphics.Color; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceViewHolder; 23 import android.util.MathUtils; 24 import android.view.View; 25 import android.widget.ProgressBar; 26 import android.widget.TextView; 27 28 import com.android.settings.R; 29 30 public class StorageSummaryPreference extends Preference { 31 private int mPercent = -1; 32 StorageSummaryPreference(Context context)33 public StorageSummaryPreference(Context context) { 34 super(context); 35 36 setLayoutResource(R.layout.storage_summary); 37 setEnabled(false); 38 } 39 setPercent(long usedBytes, long totalBytes)40 public void setPercent(long usedBytes, long totalBytes) { 41 mPercent = MathUtils.constrain((int) ((usedBytes * 100) / totalBytes), 42 (usedBytes > 0) ? 1 : 0, 100); 43 } 44 45 @Override onBindViewHolder(PreferenceViewHolder view)46 public void onBindViewHolder(PreferenceViewHolder view) { 47 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress); 48 if (mPercent != -1) { 49 progress.setVisibility(View.VISIBLE); 50 progress.setProgress(mPercent); 51 progress.setScaleY(7f); 52 } else { 53 progress.setVisibility(View.GONE); 54 } 55 56 final TextView summary = (TextView) view.findViewById(android.R.id.summary); 57 summary.setTextColor(Color.parseColor("#8a000000")); 58 59 super.onBindViewHolder(view); 60 } 61 } 62