2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge;
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;
29 import com.android.settings.R;
32 * Custom preference for displaying power consumption as a bar and an icon on the left for the
36 public class BatteryHistoryPreference extends Preference {
38 final private BatteryStats mStats;
39 final private Intent mBatteryBroadcast;
41 private boolean mHideLabels;
42 private View mLabelHeader;
43 private BatteryHistoryChart mChart;
45 public BatteryHistoryPreference(Context context, BatteryStats stats, Intent batteryBroadcast) {
47 setLayoutResource(R.layout.preference_batteryhistory);
49 mBatteryBroadcast = batteryBroadcast;
52 BatteryStats getStats() {
56 public void setHideLabels(boolean hide) {
57 if (mHideLabels != hide) {
59 if (mLabelHeader != null) {
60 mLabelHeader.setVisibility(hide ? View.GONE : View.VISIBLE);
66 protected void onBindView(View view) {
67 super.onBindView(view);
69 BatteryHistoryChart chart = (BatteryHistoryChart)view.findViewById(
70 R.id.battery_history_chart);
72 // First time: use and initialize this chart.
73 chart.setStats(mStats, mBatteryBroadcast);
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);
84 parent.addView(mChart, index);
86 mLabelHeader = view.findViewById(R.id.labelsHeader);
87 mLabelHeader.setVisibility(mHideLabels ? View.GONE : View.VISIBLE);