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.os.Bundle;
21 import android.os.SystemClock;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceViewHolder;
24 import android.util.AttributeSet;
25 import android.widget.TextView;
26 import com.android.internal.os.BatteryStatsHelper;
27 import com.android.settings.R;
28 import com.android.settings.Utils;
29 import com.android.settingslib.BatteryInfo;
30 import com.android.settingslib.graph.UsageView;
31 
32 /**
33  * Custom preference for displaying power consumption as a bar and an icon on the left for the
34  * subsystem/app type.
35  *
36  */
37 public class BatteryHistoryPreference extends Preference {
38 
39     protected static final String BATTERY_HISTORY_FILE = "tmp_bat_history.bin";
40 
41     private BatteryStatsHelper mHelper;
42     private BatteryInfo mBatteryInfo;
43 
BatteryHistoryPreference(Context context, AttributeSet attrs)44     public BatteryHistoryPreference(Context context, AttributeSet attrs) {
45         super(context, attrs);
46         setLayoutResource(R.layout.battery_usage_graph);
47         setSelectable(true);
48     }
49 
50     @Override
performClick()51     public void performClick() {
52         mHelper.storeStatsHistoryInFile(BATTERY_HISTORY_FILE);
53         Bundle args = new Bundle();
54         args.putString(BatteryHistoryDetail.EXTRA_STATS, BATTERY_HISTORY_FILE);
55         args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST, mHelper.getBatteryBroadcast());
56         Utils.startWithFragment(getContext(), BatteryHistoryDetail.class.getName(), args,
57                 null, 0, R.string.history_details_title, null);
58     }
59 
setStats(BatteryStatsHelper batteryStats)60     public void setStats(BatteryStatsHelper batteryStats) {
61         mHelper = batteryStats;
62         final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
63         mBatteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryStats.getBatteryBroadcast(),
64                 batteryStats.getStats(), elapsedRealtimeUs);
65         notifyChanged();
66     }
67 
68     @Override
onBindViewHolder(PreferenceViewHolder view)69     public void onBindViewHolder(PreferenceViewHolder view) {
70         super.onBindViewHolder(view);
71         if (mBatteryInfo == null) {
72             return;
73         }
74         view.itemView.setClickable(true);
75         view.setDividerAllowedAbove(true);
76         ((TextView) view.findViewById(R.id.charge)).setText(mBatteryInfo.batteryPercentString);
77         ((TextView) view.findViewById(R.id.estimation)).setText(mBatteryInfo.remainingLabel);
78         UsageView usageView = (UsageView) view.findViewById(R.id.battery_usage);
79         usageView.findViewById(R.id.label_group).setAlpha(.7f);
80         mBatteryInfo.bindHistory(usageView);
81     }
82 }
83