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