• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.settings.fuelgauge;
17 
18 import android.app.Activity;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.BatteryStats;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.os.UserManager;
28 import android.support.annotation.VisibleForTesting;
29 import android.view.Menu;
30 import android.view.MenuInflater;
31 import android.view.MenuItem;
32 
33 import com.android.internal.os.BatteryStatsHelper;
34 import com.android.settings.R;
35 import com.android.settings.SettingsPreferenceFragment;
36 
37 /**
38  * Common base class for things that need to show the battery usage graph.
39  */
40 public abstract class PowerUsageBase extends SettingsPreferenceFragment {
41 
42     // +1 to allow ordering for PowerUsageSummary.
43     @VisibleForTesting
44     static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
45 
46     protected BatteryStatsHelper mStatsHelper;
47     protected UserManager mUm;
48 
49     private String mBatteryLevel;
50     private String mBatteryStatus;
51 
52     @Override
onAttach(Activity activity)53     public void onAttach(Activity activity) {
54         super.onAttach(activity);
55         mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE);
56         mStatsHelper = new BatteryStatsHelper(activity, true);
57     }
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         mStatsHelper.create(icicle);
63         setHasOptionsMenu(true);
64     }
65 
66     @Override
onStart()67     public void onStart() {
68         super.onStart();
69         mStatsHelper.clearStats();
70     }
71 
72     @Override
onResume()73     public void onResume() {
74         super.onResume();
75         BatteryStatsHelper.dropFile(getActivity(), BatteryHistoryPreference.BATTERY_HISTORY_FILE);
76         updateBatteryStatus(getActivity().registerReceiver(mBatteryInfoReceiver,
77                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
78         if (mHandler.hasMessages(MSG_REFRESH_STATS)) {
79             mHandler.removeMessages(MSG_REFRESH_STATS);
80             mStatsHelper.clearStats();
81         }
82     }
83 
84     @Override
onPause()85     public void onPause() {
86         super.onPause();
87         getActivity().unregisterReceiver(mBatteryInfoReceiver);
88     }
89 
90     @Override
onStop()91     public void onStop() {
92         super.onStop();
93         mHandler.removeMessages(MSG_REFRESH_STATS);
94     }
95 
96     @Override
onDestroy()97     public void onDestroy() {
98         super.onDestroy();
99         if (getActivity().isChangingConfigurations()) {
100             mStatsHelper.storeState();
101         }
102     }
103 
104     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)105     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
106         super.onCreateOptionsMenu(menu, inflater);
107         MenuItem refresh = menu.add(0, MENU_STATS_REFRESH, 0, R.string.menu_stats_refresh)
108                 .setIcon(com.android.internal.R.drawable.ic_menu_refresh)
109                 .setAlphabeticShortcut('r');
110         refresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
111                 MenuItem.SHOW_AS_ACTION_WITH_TEXT);
112     }
113 
onOptionsItemSelected(MenuItem item)114     public boolean onOptionsItemSelected(MenuItem item) {
115         switch (item.getItemId()) {
116             case MENU_STATS_REFRESH:
117                 mStatsHelper.clearStats();
118                 refreshStats();
119                 mHandler.removeMessages(MSG_REFRESH_STATS);
120                 return true;
121         }
122         return super.onOptionsItemSelected(item);
123     }
124 
refreshStats()125     protected void refreshStats() {
126         mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, mUm.getUserProfiles());
127     }
128 
updatePreference(BatteryHistoryPreference historyPref)129     protected void updatePreference(BatteryHistoryPreference historyPref) {
130         historyPref.setStats(mStatsHelper);
131     }
132 
updateBatteryStatus(Intent intent)133     private boolean updateBatteryStatus(Intent intent) {
134         if (intent != null) {
135             String batteryLevel = com.android.settings.Utils.getBatteryPercentage(intent);
136             String batteryStatus = com.android.settings.Utils.getBatteryStatus(getResources(),
137                     intent);
138             if (!batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(mBatteryStatus)) {
139                 mBatteryLevel = batteryLevel;
140                 mBatteryStatus = batteryStatus;
141                 return true;
142             }
143         }
144         return false;
145     }
146 
147     static final int MSG_REFRESH_STATS = 100;
148 
149     private final Handler mHandler = new Handler() {
150         @Override
151         public void handleMessage(Message msg) {
152             switch (msg.what) {
153                 case MSG_REFRESH_STATS:
154                     mStatsHelper.clearStats();
155                     refreshStats();
156                     break;
157             }
158         }
159     };
160 
161     private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
162         @Override
163         public void onReceive(Context context, Intent intent) {
164             String action = intent.getAction();
165             if (Intent.ACTION_BATTERY_CHANGED.equals(action)
166                     && updateBatteryStatus(intent)) {
167                 if (!mHandler.hasMessages(MSG_REFRESH_STATS)) {
168                     mHandler.sendEmptyMessageDelayed(MSG_REFRESH_STATS, 500);
169                 }
170             }
171         }
172     };
173 
174 }
175