Home | History | Annotate | Download | only in fuelgauge
      1 /*
      2  * Copyright (C) 2010 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.content.Intent;
     21 import android.graphics.drawable.Drawable;
     22 import android.os.BatteryStats;
     23 import android.preference.Preference;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ImageView;
     27 import android.widget.TextView;
     28 
     29 import com.android.settings.R;
     30 
     31 /**
     32  * Custom preference for displaying power consumption as a bar and an icon on the left for the
     33  * subsystem/app type.
     34  *
     35  */
     36 public class BatteryHistoryPreference extends Preference {
     37 
     38     final private BatteryStats mStats;
     39     final private Intent mBatteryBroadcast;
     40 
     41     private boolean mHideLabels;
     42     private View mLabelHeader;
     43     private BatteryHistoryChart mChart;
     44 
     45     public BatteryHistoryPreference(Context context, BatteryStats stats, Intent batteryBroadcast) {
     46         super(context);
     47         setLayoutResource(R.layout.preference_batteryhistory);
     48         mStats = stats;
     49         mBatteryBroadcast = batteryBroadcast;
     50     }
     51 
     52     BatteryStats getStats() {
     53         return mStats;
     54     }
     55 
     56     public void setHideLabels(boolean hide) {
     57         if (mHideLabels != hide) {
     58             mHideLabels = hide;
     59             if (mLabelHeader != null) {
     60                 mLabelHeader.setVisibility(hide ? View.GONE : View.VISIBLE);
     61             }
     62         }
     63     }
     64 
     65     @Override
     66     protected void onBindView(View view) {
     67         super.onBindView(view);
     68 
     69         BatteryHistoryChart chart = (BatteryHistoryChart)view.findViewById(
     70                 R.id.battery_history_chart);
     71         if (mChart == null) {
     72             // First time: use and initialize this chart.
     73             chart.setStats(mStats, mBatteryBroadcast);
     74             mChart = chart;
     75         } else {
     76             // All future times: forget the newly inflated chart, re-use the
     77             // already initialized chart from last time.
     78             ViewGroup parent = (ViewGroup)chart.getParent();
     79             int index = parent.indexOfChild(chart);
     80             parent.removeViewAt(index);
     81             if (mChart.getParent() != null) {
     82                 ((ViewGroup)mChart.getParent()).removeView(mChart);
     83             }
     84             parent.addView(mChart, index);
     85         }
     86         mLabelHeader = view.findViewById(R.id.labelsHeader);
     87         mLabelHeader.setVisibility(mHideLabels ? View.GONE : View.VISIBLE);
     88     }
     89 }
     90