Home | History | Annotate | Download | only in fuelgauge
      1 /*
      2  * Copyright (C) 2009 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.fuelgauge;
     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.view.View;
     24 import android.widget.ProgressBar;
     25 import android.widget.TextView;
     26 
     27 import com.android.settings.R;
     28 
     29 /**
     30  * Custom preference for displaying power consumption as a bar and an icon on
     31  * the left for the subsystem/app type.
     32  */
     33 public class PowerGaugePreference extends Preference {
     34     private BatterySipper mInfo;
     35     private int mProgress;
     36     private CharSequence mProgressText;
     37 
     38     public PowerGaugePreference(Context context, Drawable icon, BatterySipper info) {
     39         super(context);
     40         setLayoutResource(R.layout.app_percentage_item);
     41         setIcon(icon != null ? icon : new ColorDrawable(0));
     42         mInfo = info;
     43     }
     44 
     45     public void setPercent(double percentOfMax, double percentOfTotal) {
     46         mProgress = (int) Math.ceil(percentOfMax);
     47         mProgressText = getContext().getResources().getString(
     48                 R.string.percentage, (int) Math.ceil(percentOfTotal));
     49         notifyChanged();
     50     }
     51 
     52     BatterySipper getInfo() {
     53         return mInfo;
     54     }
     55 
     56     @Override
     57     protected void onBindView(View view) {
     58         super.onBindView(view);
     59 
     60         final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
     61         progress.setProgress(mProgress);
     62 
     63         final TextView text1 = (TextView) view.findViewById(android.R.id.text1);
     64         text1.setText(mProgressText);
     65     }
     66 }
     67