2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.os.BatteryStats;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.os.Process;
28 import android.os.UserHandle;
29 import android.support.annotation.VisibleForTesting;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.PreferenceGroup;
32 import android.text.TextUtils;
33 import android.util.SparseArray;
34 import android.util.TypedValue;
35 import android.view.Menu;
36 import android.view.MenuInflater;
37 import android.view.MenuItem;
38 import com.android.internal.logging.MetricsProto.MetricsEvent;
39 import com.android.internal.os.BatterySipper;
40 import com.android.internal.os.BatterySipper.DrainType;
41 import com.android.internal.os.PowerProfile;
42 import com.android.settings.R;
43 import com.android.settings.Settings.HighPowerApplicationsActivity;
44 import com.android.settings.SettingsActivity;
45 import com.android.settings.applications.ManageApplications;
46 import com.android.settings.dashboard.SummaryLoader;
47 import com.android.settings.overlay.FeatureFactory;
48 import com.android.settingslib.BatteryInfo;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.Comparator;
53 import java.util.List;
56 * Displays a list of apps and subsystems that consume power, ordered by how much power was
57 * consumed since the last time it was unplugged.
59 public class PowerUsageSummary extends PowerUsageBase {
61 private static final boolean DEBUG = false;
63 private static final boolean USE_FAKE_DATA = false;
65 static final String TAG = "PowerUsageSummary";
67 private static final String KEY_APP_LIST = "app_list";
68 private static final String KEY_BATTERY_HISTORY = "battery_history";
70 private static final int MENU_STATS_TYPE = Menu.FIRST;
71 private static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
73 static final int MENU_ADDITIONAL_BATTERY_INFO = Menu.FIRST + 4;
74 private static final int MENU_HELP = Menu.FIRST + 5;
76 private BatteryHistoryPreference mHistPref;
77 private PreferenceGroup mAppListGroup;
79 private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
81 private static final int MIN_POWER_THRESHOLD_MILLI_AMP = 5;
82 private static final int MAX_ITEMS_TO_LIST = USE_FAKE_DATA ? 30 : 10;
83 private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
84 private static final int SECONDS_IN_HOUR = 60 * 60;
87 public void onCreate(Bundle icicle) {
88 super.onCreate(icicle);
89 setAnimationAllowed(true);
91 addPreferencesFromResource(R.xml.power_usage_summary);
92 mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_HISTORY);
93 mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
97 protected int getMetricsCategory() {
98 return MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY;
102 public void onResume() {
108 public void onPause() {
109 BatteryEntry.stopRequestQueue();
110 mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
115 public void onDestroy() {
117 if (getActivity().isChangingConfigurations()) {
118 BatteryEntry.clearUidCache();
123 public boolean onPreferenceTreeClick(Preference preference) {
124 if (!(preference instanceof PowerGaugePreference)) {
125 return super.onPreferenceTreeClick(preference);
127 PowerGaugePreference pgp = (PowerGaugePreference) preference;
128 BatteryEntry entry = pgp.getInfo();
129 PowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(), mStatsHelper,
130 mStatsType, entry, true, true);
131 return super.onPreferenceTreeClick(preference);
135 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
137 menu.add(Menu.NONE, MENU_STATS_TYPE, Menu.NONE, R.string.menu_stats_total)
138 .setIcon(com.android.internal.R.drawable.ic_menu_info_details)
139 .setAlphabeticShortcut('t');
142 menu.add(Menu.NONE, MENU_HIGH_POWER_APPS, Menu.NONE, R.string.high_power_apps);
144 PowerUsageFeatureProvider powerUsageFeatureProvider =
145 FeatureFactory.getFactory(getContext()).getPowerUsageFeatureProvider(getContext());
146 if (powerUsageFeatureProvider != null &&
147 powerUsageFeatureProvider.isAdditionalBatteryInfoEnabled()) {
148 menu.add(Menu.NONE, MENU_ADDITIONAL_BATTERY_INFO,
149 Menu.NONE, R.string.additional_battery_info);
151 super.onCreateOptionsMenu(menu, inflater);
155 protected int getHelpResource() {
156 return R.string.help_url_battery;
160 public boolean onOptionsItemSelected(MenuItem item) {
161 final SettingsActivity sa = (SettingsActivity) getActivity();
162 switch (item.getItemId()) {
163 case MENU_STATS_TYPE:
164 if (mStatsType == BatteryStats.STATS_SINCE_CHARGED) {
165 mStatsType = BatteryStats.STATS_SINCE_UNPLUGGED;
167 mStatsType = BatteryStats.STATS_SINCE_CHARGED;
171 case MENU_HIGH_POWER_APPS:
172 Bundle args = new Bundle();
173 args.putString(ManageApplications.EXTRA_CLASSNAME,
174 HighPowerApplicationsActivity.class.getName());
175 sa.startPreferencePanel(ManageApplications.class.getName(), args,
176 R.string.high_power_apps, null, null, 0);
179 return super.onOptionsItemSelected(item);
183 private void addNotAvailableMessage() {
184 final String NOT_AVAILABLE = "not_available";
185 Preference notAvailable = getCachedPreference(NOT_AVAILABLE);
186 if (notAvailable == null) {
187 notAvailable = new Preference(getPrefContext());
188 notAvailable.setKey(NOT_AVAILABLE);
189 notAvailable.setTitle(R.string.power_usage_not_available);
190 mAppListGroup.addPreference(notAvailable);
194 private static boolean isSharedGid(int uid) {
195 return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
198 private static boolean isSystemUid(int uid) {
199 return uid >= Process.SYSTEM_UID && uid < Process.FIRST_APPLICATION_UID;
203 * We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
204 * exists for all users of the same app. We detect this case and merge the power use
205 * for dex2oat to the device OWNER's use of the app.
206 * @return A sorted list of apps using power.
208 private static List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
209 final SparseArray<BatterySipper> uidList = new SparseArray<>();
211 final ArrayList<BatterySipper> results = new ArrayList<>();
212 final int numSippers = sippers.size();
213 for (int i = 0; i < numSippers; i++) {
214 BatterySipper sipper = sippers.get(i);
215 if (sipper.getUid() > 0) {
216 int realUid = sipper.getUid();
218 // Check if this UID is a shared GID. If so, we combine it with the OWNER's
220 if (isSharedGid(sipper.getUid())) {
221 realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
222 UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
225 // Check if this UID is a system UID (mediaserver, logd, nfc, drm, etc).
226 if (isSystemUid(realUid)
227 && !"mediaserver".equals(sipper.packageWithHighestDrain)) {
228 // Use the system UID for all UIDs running in their own sandbox that
229 // are not apps. We exclude mediaserver because we already are expected to
230 // report that as a separate item.
231 realUid = Process.SYSTEM_UID;
234 if (realUid != sipper.getUid()) {
235 // Replace the BatterySipper with a new one with the real UID set.
236 BatterySipper newSipper = new BatterySipper(sipper.drainType,
237 new FakeUid(realUid), 0.0);
238 newSipper.add(sipper);
239 newSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
240 newSipper.mPackages = sipper.mPackages;
244 int index = uidList.indexOfKey(realUid);
247 uidList.put(realUid, sipper);
249 // Combine BatterySippers if we already have one with this UID.
250 final BatterySipper existingSipper = uidList.valueAt(index);
251 existingSipper.add(sipper);
252 if (existingSipper.packageWithHighestDrain == null
253 && sipper.packageWithHighestDrain != null) {
254 existingSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
257 final int existingPackageLen = existingSipper.mPackages != null ?
258 existingSipper.mPackages.length : 0;
259 final int newPackageLen = sipper.mPackages != null ?
260 sipper.mPackages.length : 0;
261 if (newPackageLen > 0) {
262 String[] newPackages = new String[existingPackageLen + newPackageLen];
263 if (existingPackageLen > 0) {
264 System.arraycopy(existingSipper.mPackages, 0, newPackages, 0,
267 System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
269 existingSipper.mPackages = newPackages;
277 final int numUidSippers = uidList.size();
278 for (int i = 0; i < numUidSippers; i++) {
279 results.add(uidList.valueAt(i));
282 // The sort order must have changed, so re-sort based on total power use.
283 Collections.sort(results, new Comparator<BatterySipper>() {
285 public int compare(BatterySipper a, BatterySipper b) {
286 return Double.compare(b.totalPowerMah, a.totalPowerMah);
292 protected void refreshStats() {
293 super.refreshStats();
294 updatePreference(mHistPref);
295 cacheRemoveAllPrefs(mAppListGroup);
296 mAppListGroup.setOrderingAsAdded(false);
297 boolean addedSome = false;
299 final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
300 final BatteryStats stats = mStatsHelper.getStats();
301 final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
303 TypedValue value = new TypedValue();
304 getContext().getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
305 int colorControl = getContext().getColor(value.resourceId);
307 if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
308 final List<BatterySipper> usageList = getCoalescedUsageList(
309 USE_FAKE_DATA ? getFakeStats() : mStatsHelper.getUsageList());
311 final int dischargeAmount = USE_FAKE_DATA ? 5000
312 : stats != null ? stats.getDischargeAmount(mStatsType) : 0;
313 final int numSippers = usageList.size();
314 for (int i = 0; i < numSippers; i++) {
315 final BatterySipper sipper = usageList.get(i);
316 if ((sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP) {
319 double totalPower = USE_FAKE_DATA ? 4000 : mStatsHelper.getTotalPower();
320 final double percentOfTotal =
321 ((sipper.totalPowerMah / totalPower) * dischargeAmount);
322 if (((int) (percentOfTotal + .5)) < 1) {
325 if (sipper.drainType == BatterySipper.DrainType.OVERCOUNTED) {
326 // Don't show over-counted unless it is at least 2/3 the size of
327 // the largest real entry, and its percent of total is more significant
328 if (sipper.totalPowerMah < ((mStatsHelper.getMaxRealPower()*2)/3)) {
331 if (percentOfTotal < 10) {
334 if ("user".equals(Build.TYPE)) {
338 if (sipper.drainType == BatterySipper.DrainType.UNACCOUNTED) {
339 // Don't show over-counted unless it is at least 1/2 the size of
340 // the largest real entry, and its percent of total is more significant
341 if (sipper.totalPowerMah < (mStatsHelper.getMaxRealPower()/2)) {
344 if (percentOfTotal < 5) {
347 if ("user".equals(Build.TYPE)) {
351 final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
352 final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
353 final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
355 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
357 final String key = sipper.drainType == DrainType.APP ? sipper.getPackages() != null
358 ? TextUtils.concat(sipper.getPackages()).toString()
359 : String.valueOf(sipper.getUid())
360 : sipper.drainType.toString();
361 PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
363 pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
364 contentDescription, entry);
368 final double percentOfMax = (sipper.totalPowerMah * 100)
369 / mStatsHelper.getMaxPower();
370 sipper.percent = percentOfTotal;
371 pref.setTitle(entry.getLabel());
372 pref.setOrder(i + 1);
373 pref.setPercent(percentOfMax, percentOfTotal);
374 if (sipper.uidObj != null) {
375 pref.setKey(Integer.toString(sipper.uidObj.getUid()));
377 if ((sipper.drainType != DrainType.APP || sipper.uidObj.getUid() == 0)
378 && sipper.drainType != DrainType.USER) {
379 pref.setTint(colorControl);
382 mAppListGroup.addPreference(pref);
383 if (mAppListGroup.getPreferenceCount() - getCachedCount()
384 > (MAX_ITEMS_TO_LIST + 1)) {
390 addNotAvailableMessage();
392 removeCachedPrefs(mAppListGroup);
394 BatteryEntry.startRequestQueue();
397 private static List<BatterySipper> getFakeStats() {
398 ArrayList<BatterySipper> stats = new ArrayList<>();
400 for (DrainType type : DrainType.values()) {
401 if (type == DrainType.APP) {
404 stats.add(new BatterySipper(type, null, use));
407 for (int i = 0; i < 100; i++) {
408 stats.add(new BatterySipper(DrainType.APP,
409 new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
411 stats.add(new BatterySipper(DrainType.APP,
412 new FakeUid(0), use));
414 // Simulate dex2oat process.
415 BatterySipper sipper = new BatterySipper(DrainType.APP,
416 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
417 sipper.packageWithHighestDrain = "dex2oat";
420 sipper = new BatterySipper(DrainType.APP,
421 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
422 sipper.packageWithHighestDrain = "dex2oat";
425 sipper = new BatterySipper(DrainType.APP,
426 new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
432 Handler mHandler = new Handler() {
435 public void handleMessage(Message msg) {
437 case BatteryEntry.MSG_UPDATE_NAME_ICON:
438 BatteryEntry entry = (BatteryEntry) msg.obj;
439 PowerGaugePreference pgp =
440 (PowerGaugePreference) findPreference(
441 Integer.toString(entry.sipper.uidObj.getUid()));
443 final int userId = UserHandle.getUserId(entry.sipper.getUid());
444 final UserHandle userHandle = new UserHandle(userId);
445 pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
446 pgp.setTitle(entry.name);
447 if (entry.sipper.drainType == DrainType.APP) {
448 pgp.setContentDescription(entry.name);
452 case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
453 Activity activity = getActivity();
454 if (activity != null) {
455 activity.reportFullyDrawn();
459 super.handleMessage(msg);
463 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
464 private final Context mContext;
465 private final SummaryLoader mLoader;
467 private SummaryProvider(Context context, SummaryLoader loader) {
473 public void setListening(boolean listening) {
476 BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
478 public void onBatteryInfoLoaded(BatteryInfo info) {
479 mLoader.setSummary(SummaryProvider.this, info.mChargeLabelString);
486 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
487 = new SummaryLoader.SummaryProviderFactory() {
489 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
490 SummaryLoader summaryLoader) {
491 return new SummaryProvider(activity, summaryLoader);