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.Intent;
20 import android.os.BatteryStats;
21 import android.os.BatteryStats.HistoryItem;
22 import android.os.Bundle;
23 import android.os.SystemClock;
24 import android.util.TypedValue;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 import com.android.internal.logging.MetricsProto.MetricsEvent;
30 import com.android.internal.os.BatteryStatsHelper;
31 import com.android.settings.R;
32 import com.android.settings.SettingsPreferenceFragment;
33 import com.android.settings.fuelgauge.BatteryActiveView.BatteryActiveProvider;
34 import com.android.settingslib.BatteryInfo;
35 import com.android.settingslib.graph.UsageView;
36 
37 public class BatteryHistoryDetail extends SettingsPreferenceFragment {
38     public static final String EXTRA_STATS = "stats";
39     public static final String EXTRA_BROADCAST = "broadcast";
40 
41     private BatteryStats mStats;
42     private Intent mBatteryBroadcast;
43 
44     private BatteryFlagParser mChargingParser;
45     private BatteryFlagParser mScreenOn;
46     private BatteryFlagParser mGpsParser;
47     private BatteryFlagParser mFlashlightParser;
48     private BatteryFlagParser mCameraParser;
49     private BatteryWifiParser mWifiParser;
50     private BatteryFlagParser mCpuParser;
51     private BatteryCellParser mPhoneParser;
52 
53     @Override
onCreate(Bundle icicle)54     public void onCreate(Bundle icicle) {
55         super.onCreate(icicle);
56         String histFile = getArguments().getString(EXTRA_STATS);
57         mStats = BatteryStatsHelper.statsFromFile(getActivity(), histFile);
58         mBatteryBroadcast = getArguments().getParcelable(EXTRA_BROADCAST);
59 
60         TypedValue value = new TypedValue();
61         getContext().getTheme().resolveAttribute(android.R.attr.colorAccent, value, true);
62         int accentColor = getContext().getColor(value.resourceId);
63 
64         mChargingParser = new BatteryFlagParser(accentColor, false,
65                 HistoryItem.STATE_BATTERY_PLUGGED_FLAG);
66         mScreenOn = new BatteryFlagParser(accentColor, false,
67                 HistoryItem.STATE_SCREEN_ON_FLAG);
68         mGpsParser = new BatteryFlagParser(accentColor, false,
69                 HistoryItem.STATE_GPS_ON_FLAG);
70         mFlashlightParser = new BatteryFlagParser(accentColor, true,
71                 HistoryItem.STATE2_FLASHLIGHT_FLAG);
72         mCameraParser = new BatteryFlagParser(accentColor, true,
73                 HistoryItem.STATE2_CAMERA_FLAG);
74         mWifiParser = new BatteryWifiParser(accentColor);
75         mCpuParser = new BatteryFlagParser(accentColor, false,
76                 HistoryItem.STATE_CPU_RUNNING_FLAG);
77         mPhoneParser = new BatteryCellParser();
78         setHasOptionsMenu(true);
79     }
80 
81     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)82     public View onCreateView(LayoutInflater inflater, ViewGroup container,
83             Bundle savedInstanceState) {
84         return inflater.inflate(R.layout.battery_history_detail, container, false);
85     }
86 
87     @Override
onViewCreated(View view, Bundle savedInstanceState)88     public void onViewCreated(View view, Bundle savedInstanceState) {
89         super.onViewCreated(view, savedInstanceState);
90         updateEverything();
91     }
92 
updateEverything()93     private void updateEverything() {
94         BatteryInfo info = BatteryInfo.getBatteryInfo(getContext(), mBatteryBroadcast, mStats,
95                 SystemClock.elapsedRealtime() * 1000);
96         final View view = getView();
97         info.bindHistory((UsageView) view.findViewById(R.id.battery_usage), mChargingParser,
98                 mScreenOn, mGpsParser, mFlashlightParser, mCameraParser, mWifiParser, mCpuParser,
99                 mPhoneParser);
100         ((TextView) view.findViewById(R.id.charge)).setText(info.batteryPercentString);
101         ((TextView) view.findViewById(R.id.estimation)).setText(info.remainingLabel);
102 
103         bindData(mChargingParser, R.string.battery_stats_charging_label, R.id.charging_group);
104         bindData(mScreenOn, R.string.battery_stats_screen_on_label, R.id.screen_on_group);
105         bindData(mGpsParser, R.string.battery_stats_gps_on_label, R.id.gps_group);
106         bindData(mFlashlightParser, R.string.battery_stats_flashlight_on_label,
107                 R.id.flashlight_group);
108         bindData(mCameraParser, R.string.battery_stats_camera_on_label, R.id.camera_group);
109         bindData(mWifiParser, R.string.battery_stats_wifi_running_label, R.id.wifi_group);
110         bindData(mCpuParser, R.string.battery_stats_wake_lock_label, R.id.cpu_group);
111         bindData(mPhoneParser, R.string.battery_stats_phone_signal_label, R.id.cell_network_group);
112     }
113 
bindData(BatteryActiveProvider provider, int label, int groupId)114     private void bindData(BatteryActiveProvider provider, int label, int groupId) {
115         View group = getView().findViewById(groupId);
116         group.setVisibility(provider.hasData() ? View.VISIBLE : View.GONE);
117         ((TextView) group.findViewById(android.R.id.title)).setText(label);
118         ((BatteryActiveView) group.findViewById(R.id.battery_active)).setProvider(provider);
119     }
120 
121     @Override
getMetricsCategory()122     protected int getMetricsCategory() {
123         return MetricsEvent.FUELGAUGE_BATTERY_HISTORY_DETAIL;
124     }
125 }
126