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.os.BatteryStats; 22 import android.os.Bundle; 23 import android.preference.Preference; 24 import android.preference.PreferenceScreen; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.view.ViewGroup; 28 29 import com.android.internal.os.BatteryStatsHelper; 30 import com.android.settings.R; 31 import com.android.settings.SettingsActivity; 32 33 /** 34 * Custom preference for displaying power consumption as a bar and an icon on the left for the 35 * subsystem/app type. 36 * 37 */ 38 public class BatteryHistoryPreference extends Preference { 39 40 protected static final String BATTERY_HISTORY_FILE = "tmp_bat_history.bin"; 41 42 private BatteryStats mStats; 43 private Intent mBatteryBroadcast; 44 45 private BatteryHistoryChart mChart; 46 private BatteryStatsHelper mHelper; 47 48 public BatteryHistoryPreference(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 } 51 52 @Override 53 public void performClick(PreferenceScreen preferenceScreen) { 54 if (!isEnabled()) { 55 return; 56 } 57 mHelper.storeStatsHistoryInFile(BATTERY_HISTORY_FILE); 58 Bundle args = new Bundle(); 59 args.putString(BatteryHistoryDetail.EXTRA_STATS, BATTERY_HISTORY_FILE); 60 args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST, 61 mHelper.getBatteryBroadcast()); 62 if (getContext() instanceof SettingsActivity) { 63 SettingsActivity sa = (SettingsActivity) getContext(); 64 sa.startPreferencePanel(BatteryHistoryDetail.class.getName(), args, 65 R.string.history_details_title, null, null, 0); 66 } 67 } 68 69 public void setStats(BatteryStatsHelper batteryStats) { 70 // Clear out the chart to receive new data. 71 mChart = null; 72 mHelper = batteryStats; 73 mStats = batteryStats.getStats(); 74 mBatteryBroadcast = batteryStats.getBatteryBroadcast(); 75 if (getLayoutResource() != R.layout.battery_history_chart) { 76 // Now we should have some data, set the layout we want. 77 setLayoutResource(R.layout.battery_history_chart); 78 } 79 notifyChanged(); 80 } 81 82 BatteryStats getStats() { 83 return mStats; 84 } 85 86 @Override 87 protected void onBindView(View view) { 88 super.onBindView(view); 89 90 if (mStats == null) { 91 return; 92 } 93 BatteryHistoryChart chart = (BatteryHistoryChart) view.findViewById( 94 R.id.battery_history_chart); 95 if (mChart == null) { 96 // First time: use and initialize this chart. 97 chart.setStats(mStats, mBatteryBroadcast); 98 mChart = chart; 99 } else { 100 // All future times: forget the newly inflated chart, re-use the 101 // already initialized chart from last time. 102 ViewGroup parent = (ViewGroup) chart.getParent(); 103 int index = parent.indexOfChild(chart); 104 parent.removeViewAt(index); 105 if (mChart.getParent() != null) { 106 ((ViewGroup) mChart.getParent()).removeView(mChart); 107 } 108 parent.addView(mChart, index); 109 } 110 } 111 } 112