1 /* 2 * Copyright (C) 2013 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.applications; 18 19 import android.content.Context; 20 import android.graphics.drawable.ColorDrawable; 21 import android.graphics.drawable.Drawable; 22 import android.preference.Preference; 23 import android.text.format.Formatter; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.ProgressBar; 27 import android.widget.TextView; 28 import com.android.settings.R; 29 import com.android.settings.Utils; 30 31 public class ProcessStatsPreference extends Preference { 32 private ProcStatsEntry mEntry; 33 private int mProgress; 34 private CharSequence mProgressText; 35 36 public ProcessStatsPreference(Context context) { 37 this(context, null); 38 } 39 40 public ProcessStatsPreference(Context context, AttributeSet attrs) { 41 this(context, attrs, 0); 42 } 43 44 public ProcessStatsPreference(Context context, AttributeSet attrs, int defStyleAttr) { 45 this(context, attrs, defStyleAttr, 0); 46 } 47 48 public ProcessStatsPreference(Context context, AttributeSet attrs, int defStyleAttr, 49 int defStyleRes) { 50 super(context, attrs, defStyleAttr, defStyleRes); 51 setLayoutResource(R.layout.preference_app_percentage); 52 } 53 54 public void init(Drawable icon, ProcStatsEntry entry) { 55 mEntry = entry; 56 setIcon(icon != null ? icon : new ColorDrawable(0)); 57 } 58 59 public ProcStatsEntry getEntry() { 60 return mEntry; 61 } 62 63 public void setPercent(double percentOfWeight, double percentOfTime) { 64 mProgress = (int) Math.ceil(percentOfWeight); 65 mProgressText = Utils.formatPercentage((int) percentOfTime); 66 notifyChanged(); 67 } 68 69 @Override 70 protected void onBindView(View view) { 71 super.onBindView(view); 72 73 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress); 74 progress.setProgress(mProgress); 75 76 final TextView text1 = (TextView) view.findViewById(android.R.id.text1); 77 text1.setText(mProgressText); 78 } 79 } 80