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.SystemClock;
29 import android.os.UserHandle;
30 import android.provider.SearchIndexableResource;
31 import android.support.annotation.VisibleForTesting;
32 import android.support.v7.preference.Preference;
33 import android.support.v7.preference.PreferenceGroup;
34 import android.text.TextUtils;
35 import android.text.format.DateUtils;
36 import android.util.Log;
37 import android.util.SparseArray;
38 import android.util.TypedValue;
39 import android.view.Menu;
40 import android.view.MenuInflater;
41 import android.view.MenuItem;
42 import android.widget.TextView;
44 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
45 import com.android.internal.os.BatterySipper;
46 import com.android.internal.os.BatterySipper.DrainType;
47 import com.android.internal.os.PowerProfile;
48 import com.android.settings.R;
49 import com.android.settings.Settings.HighPowerApplicationsActivity;
50 import com.android.settings.SettingsActivity;
51 import com.android.settings.Utils;
52 import com.android.settings.applications.LayoutPreference;
53 import com.android.settings.applications.ManageApplications;
54 import com.android.settings.core.PreferenceController;
55 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
56 import com.android.settings.dashboard.SummaryLoader;
57 import com.android.settings.display.AutoBrightnessPreferenceController;
58 import com.android.settings.display.BatteryPercentagePreferenceController;
59 import com.android.settings.display.TimeoutPreferenceController;
60 import com.android.settings.overlay.FeatureFactory;
61 import com.android.settings.search.BaseSearchIndexProvider;
62 import com.android.settings.widget.FooterPreferenceMixin;
63 import com.android.settingslib.BatteryInfo;
65 import java.util.ArrayList;
66 import java.util.Arrays;
67 import java.util.Collections;
68 import java.util.Comparator;
69 import java.util.List;
72 * Displays a list of apps and subsystems that consume power, ordered by how much power was
73 * consumed since the last time it was unplugged.
75 public class PowerUsageSummary extends PowerUsageBase {
77 static final String TAG = "PowerUsageSummary";
79 private static final boolean DEBUG = false;
80 private static final boolean USE_FAKE_DATA = false;
81 private static final String KEY_APP_LIST = "app_list";
82 private static final String KEY_BATTERY_HEADER = "battery_header";
83 private static final int MAX_ITEMS_TO_LIST = USE_FAKE_DATA ? 30 : 10;
84 private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
86 private static final String KEY_SCREEN_USAGE = "screen_usage";
87 private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
89 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness_battery";
90 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout_battery";
91 private static final String KEY_BATTERY_SAVER_SUMMARY = "battery_saver_summary";
93 private static final int MENU_STATS_TYPE = Menu.FIRST;
95 static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
97 static final int MENU_ADDITIONAL_BATTERY_INFO = Menu.FIRST + 4;
99 static final int MENU_TOGGLE_APPS = Menu.FIRST + 5;
100 private static final int MENU_HELP = Menu.FIRST + 6;
102 private final FooterPreferenceMixin mFooterPreferenceMixin =
103 new FooterPreferenceMixin(this, getLifecycle());
106 boolean mShowAllApps = false;
108 PowerGaugePreference mScreenUsagePref;
110 PowerGaugePreference mLastFullChargePref;
112 PowerUsageFeatureProvider mPowerFeatureProvider;
114 BatteryUtils mBatteryUtils;
116 private LayoutPreference mBatteryLayoutPref;
117 private PreferenceGroup mAppListGroup;
118 private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
121 public void onCreate(Bundle icicle) {
122 super.onCreate(icicle);
123 setAnimationAllowed(true);
125 mBatteryLayoutPref = (LayoutPreference) findPreference(KEY_BATTERY_HEADER);
126 mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
127 mScreenUsagePref = (PowerGaugePreference) findPreference(KEY_SCREEN_USAGE);
128 mLastFullChargePref = (PowerGaugePreference) findPreference(
129 KEY_TIME_SINCE_LAST_FULL_CHARGE);
130 mFooterPreferenceMixin.createFooterPreference().setTitle(R.string.battery_footer_summary);
132 mBatteryUtils = BatteryUtils.getInstance(getContext());
134 initFeatureProvider();
138 public int getMetricsCategory() {
139 return MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY;
143 public void onResume() {
149 public void onPause() {
150 BatteryEntry.stopRequestQueue();
151 mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
156 public void onDestroy() {
158 if (getActivity().isChangingConfigurations()) {
159 BatteryEntry.clearUidCache();
164 public boolean onPreferenceTreeClick(Preference preference) {
165 if (KEY_BATTERY_HEADER.equals(preference.getKey())) {
166 performBatteryHeaderClick();
168 } else if (!(preference instanceof PowerGaugePreference)) {
169 return super.onPreferenceTreeClick(preference);
171 PowerGaugePreference pgp = (PowerGaugePreference) preference;
172 BatteryEntry entry = pgp.getInfo();
173 AdvancedPowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
174 this, mStatsHelper, mStatsType, entry, pgp.getPercent());
175 return super.onPreferenceTreeClick(preference);
179 protected String getLogTag() {
184 protected int getPreferenceScreenResId() {
185 return R.xml.power_usage_summary;
189 protected List<PreferenceController> getPreferenceControllers(Context context) {
190 final List<PreferenceController> controllers = new ArrayList<>();
191 controllers.add(new AutoBrightnessPreferenceController(context, KEY_AUTO_BRIGHTNESS));
192 controllers.add(new TimeoutPreferenceController(context, KEY_SCREEN_TIMEOUT));
193 controllers.add(new BatterySaverController(context, getLifecycle()));
194 controllers.add(new BatteryPercentagePreferenceController(context));
199 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
201 menu.add(Menu.NONE, MENU_STATS_TYPE, Menu.NONE, R.string.menu_stats_total)
202 .setIcon(com.android.internal.R.drawable.ic_menu_info_details)
203 .setAlphabeticShortcut('t');
206 menu.add(Menu.NONE, MENU_HIGH_POWER_APPS, Menu.NONE, R.string.high_power_apps);
208 if (mPowerFeatureProvider.isAdditionalBatteryInfoEnabled()) {
209 menu.add(Menu.NONE, MENU_ADDITIONAL_BATTERY_INFO,
210 Menu.NONE, R.string.additional_battery_info);
212 if (mPowerFeatureProvider.isPowerAccountingToggleEnabled()) {
213 menu.add(Menu.NONE, MENU_TOGGLE_APPS, Menu.NONE,
214 mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps);
217 super.onCreateOptionsMenu(menu, inflater);
221 protected int getHelpResource() {
222 return R.string.help_url_battery;
226 public boolean onOptionsItemSelected(MenuItem item) {
227 final SettingsActivity sa = (SettingsActivity) getActivity();
228 final Context context = getContext();
229 final MetricsFeatureProvider metricsFeatureProvider =
230 FeatureFactory.getFactory(context).getMetricsFeatureProvider();
232 switch (item.getItemId()) {
233 case MENU_STATS_TYPE:
234 if (mStatsType == BatteryStats.STATS_SINCE_CHARGED) {
235 mStatsType = BatteryStats.STATS_SINCE_UNPLUGGED;
237 mStatsType = BatteryStats.STATS_SINCE_CHARGED;
241 case MENU_HIGH_POWER_APPS:
242 Bundle args = new Bundle();
243 args.putString(ManageApplications.EXTRA_CLASSNAME,
244 HighPowerApplicationsActivity.class.getName());
245 sa.startPreferencePanel(this, ManageApplications.class.getName(), args,
246 R.string.high_power_apps, null, null, 0);
247 metricsFeatureProvider.action(context,
248 MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_OPTIMIZATION);
250 case MENU_ADDITIONAL_BATTERY_INFO:
251 startActivity(FeatureFactory.getFactory(getContext())
252 .getPowerUsageFeatureProvider(getContext())
253 .getAdditionalBatteryInfoIntent());
254 metricsFeatureProvider.action(context,
255 MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_USAGE_ALERTS);
257 case MENU_TOGGLE_APPS:
258 mShowAllApps = !mShowAllApps;
259 item.setTitle(mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps);
260 metricsFeatureProvider.action(context,
261 MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE, mShowAllApps);
265 return super.onOptionsItemSelected(item);
269 private void addNotAvailableMessage() {
270 final String NOT_AVAILABLE = "not_available";
271 Preference notAvailable = getCachedPreference(NOT_AVAILABLE);
272 if (notAvailable == null) {
273 notAvailable = new Preference(getPrefContext());
274 notAvailable.setKey(NOT_AVAILABLE);
275 notAvailable.setTitle(R.string.power_usage_not_available);
276 mAppListGroup.addPreference(notAvailable);
280 private void performBatteryHeaderClick() {
281 final Context context = getContext();
282 final PowerUsageFeatureProvider featureProvider = FeatureFactory.getFactory(context)
283 .getPowerUsageFeatureProvider(context);
285 if (featureProvider.isAdvancedUiEnabled()) {
286 Utils.startWithFragment(getContext(), PowerUsageAdvanced.class.getName(), null,
287 null, 0, R.string.advanced_battery_title, null, getMetricsCategory());
289 mStatsHelper.storeStatsHistoryInFile(BatteryHistoryDetail.BATTERY_HISTORY_FILE);
290 Bundle args = new Bundle(2);
291 args.putString(BatteryHistoryDetail.EXTRA_STATS,
292 BatteryHistoryDetail.BATTERY_HISTORY_FILE);
293 args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST,
294 mStatsHelper.getBatteryBroadcast());
295 Utils.startWithFragment(getContext(), BatteryHistoryDetail.class.getName(), args,
296 null, 0, R.string.history_details_title, null, getMetricsCategory());
300 private static boolean isSharedGid(int uid) {
301 return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
304 private static boolean isSystemUid(int uid) {
305 return uid >= Process.SYSTEM_UID && uid < Process.FIRST_APPLICATION_UID;
309 * We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
310 * exists for all users of the same app. We detect this case and merge the power use
311 * for dex2oat to the device OWNER's use of the app.
313 * @return A sorted list of apps using power.
315 private static List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
316 final SparseArray<BatterySipper> uidList = new SparseArray<>();
318 final ArrayList<BatterySipper> results = new ArrayList<>();
319 final int numSippers = sippers.size();
320 for (int i = 0; i < numSippers; i++) {
321 BatterySipper sipper = sippers.get(i);
322 if (sipper.getUid() > 0) {
323 int realUid = sipper.getUid();
325 // Check if this UID is a shared GID. If so, we combine it with the OWNER's
327 if (isSharedGid(sipper.getUid())) {
328 realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
329 UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
332 // Check if this UID is a system UID (mediaserver, logd, nfc, drm, etc).
333 if (isSystemUid(realUid)
334 && !"mediaserver".equals(sipper.packageWithHighestDrain)) {
335 // Use the system UID for all UIDs running in their own sandbox that
336 // are not apps. We exclude mediaserver because we already are expected to
337 // report that as a separate item.
338 realUid = Process.SYSTEM_UID;
341 if (realUid != sipper.getUid()) {
342 // Replace the BatterySipper with a new one with the real UID set.
343 BatterySipper newSipper = new BatterySipper(sipper.drainType,
344 new FakeUid(realUid), 0.0);
345 newSipper.add(sipper);
346 newSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
347 newSipper.mPackages = sipper.mPackages;
351 int index = uidList.indexOfKey(realUid);
354 uidList.put(realUid, sipper);
356 // Combine BatterySippers if we already have one with this UID.
357 final BatterySipper existingSipper = uidList.valueAt(index);
358 existingSipper.add(sipper);
359 if (existingSipper.packageWithHighestDrain == null
360 && sipper.packageWithHighestDrain != null) {
361 existingSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
364 final int existingPackageLen = existingSipper.mPackages != null ?
365 existingSipper.mPackages.length : 0;
366 final int newPackageLen = sipper.mPackages != null ?
367 sipper.mPackages.length : 0;
368 if (newPackageLen > 0) {
369 String[] newPackages = new String[existingPackageLen + newPackageLen];
370 if (existingPackageLen > 0) {
371 System.arraycopy(existingSipper.mPackages, 0, newPackages, 0,
374 System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
376 existingSipper.mPackages = newPackages;
384 final int numUidSippers = uidList.size();
385 for (int i = 0; i < numUidSippers; i++) {
386 results.add(uidList.valueAt(i));
389 // The sort order must have changed, so re-sort based on total power use.
390 Collections.sort(results, new Comparator<BatterySipper>() {
392 public int compare(BatterySipper a, BatterySipper b) {
393 return Double.compare(b.totalPowerMah, a.totalPowerMah);
399 protected void refreshStats() {
400 super.refreshStats();
402 BatteryInfo.getBatteryInfo(getContext(), new BatteryInfo.Callback() {
404 public void onBatteryInfoLoaded(BatteryInfo info) {
405 updateHeaderPreference(info);
409 cacheRemoveAllPrefs(mAppListGroup);
410 mAppListGroup.setOrderingAsAdded(false);
411 boolean addedSome = false;
413 final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
414 final BatteryStats stats = mStatsHelper.getStats();
415 final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
416 final Context context = getContext();
418 final TypedValue value = new TypedValue();
419 context.getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
420 final int colorControl = context.getColor(value.resourceId);
421 final String usedTime = context.getString(R.string.battery_used_for);
422 final int dischargeAmount = USE_FAKE_DATA ? 5000
423 : stats != null ? stats.getDischargeAmount(mStatsType) : 0;
425 final long runningTime = calculateRunningTimeBasedOnStatsType();
426 updateScreenPreference();
427 updateLastFullChargePreference(runningTime);
428 mAppListGroup.setTitle(getString(R.string.power_usage_list_summary,
429 Utils.formatElapsedTime(context, runningTime, false)));
431 if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
432 final List<BatterySipper> usageList = getCoalescedUsageList(
433 USE_FAKE_DATA ? getFakeStats() : mStatsHelper.getUsageList());
435 double hiddenPowerMah = mShowAllApps ? 0 :
436 mBatteryUtils.removeHiddenBatterySippers(usageList);
438 final int numSippers = usageList.size();
439 for (int i = 0; i < numSippers; i++) {
440 final BatterySipper sipper = usageList.get(i);
441 double totalPower = USE_FAKE_DATA ? 4000 : mStatsHelper.getTotalPower();
443 final double percentOfTotal = mBatteryUtils.calculateBatteryPercent(
444 sipper.totalPowerMah, totalPower, hiddenPowerMah, dischargeAmount);
446 if (((int) (percentOfTotal + .5)) < 1) {
449 if (sipper.drainType == BatterySipper.DrainType.OVERCOUNTED) {
450 // Don't show over-counted unless it is at least 2/3 the size of
451 // the largest real entry, and its percent of total is more significant
452 if (sipper.totalPowerMah < ((mStatsHelper.getMaxRealPower() * 2) / 3)) {
455 if (percentOfTotal < 10) {
458 if ("user".equals(Build.TYPE)) {
462 if (sipper.drainType == BatterySipper.DrainType.UNACCOUNTED) {
463 // Don't show over-counted unless it is at least 1/2 the size of
464 // the largest real entry, and its percent of total is more significant
465 if (sipper.totalPowerMah < (mStatsHelper.getMaxRealPower() / 2)) {
468 if (percentOfTotal < 5) {
471 if ("user".equals(Build.TYPE)) {
475 final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
476 final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
477 final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
479 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
482 final String key = extractKeyFromSipper(sipper);
483 PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
485 pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
486 contentDescription, entry);
490 final double percentOfMax = (sipper.totalPowerMah * 100)
491 / mStatsHelper.getMaxPower();
492 sipper.percent = percentOfTotal;
493 pref.setTitle(entry.getLabel());
494 pref.setOrder(i + 1);
495 pref.setPercent(percentOfTotal);
496 if (sipper.usageTimeMs == 0 && sipper.drainType == DrainType.APP) {
497 sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(
498 BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, mStatsType);
500 setUsageSummary(pref, usedTime, sipper.usageTimeMs);
501 if ((sipper.drainType != DrainType.APP
502 || sipper.uidObj.getUid() == Process.ROOT_UID)
503 && sipper.drainType != DrainType.USER) {
504 pref.setTint(colorControl);
507 mAppListGroup.addPreference(pref);
508 if (mAppListGroup.getPreferenceCount() - getCachedCount()
509 > (MAX_ITEMS_TO_LIST + 1)) {
515 addNotAvailableMessage();
517 removeCachedPrefs(mAppListGroup);
519 BatteryEntry.startRequestQueue();
523 BatterySipper findBatterySipperByType(List<BatterySipper> usageList, DrainType type) {
524 for (int i = 0, size = usageList.size(); i < size; i++) {
525 final BatterySipper sipper = usageList.get(i);
526 if (sipper.drainType == type) {
534 void updateScreenPreference() {
535 final BatterySipper sipper = findBatterySipperByType(
536 mStatsHelper.getUsageList(), DrainType.SCREEN);
537 final Context context = getContext();
538 final long usageTimeMs = sipper != null ? sipper.usageTimeMs : 0;
540 mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(context, usageTimeMs, false));
544 void updateLastFullChargePreference(long timeMs) {
545 mLastFullChargePref.setSubtitle(getString(R.string.power_last_full_charge_summary,
546 Utils.formatElapsedTime(getContext(), timeMs, false)));
550 long calculateRunningTimeBasedOnStatsType() {
551 final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
552 // Return the battery time (millisecond) on status mStatsType
553 return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
554 mStatsType /* STATS_SINCE_CHARGED */) / 1000;
558 void updateHeaderPreference(BatteryInfo info) {
559 final Context context = getContext();
560 if (context == null) {
563 final BatteryMeterView batteryView = (BatteryMeterView) mBatteryLayoutPref
564 .findViewById(R.id.battery_header_icon);
565 final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent);
566 final TextView summary1 = (TextView) mBatteryLayoutPref.findViewById(R.id.summary1);
567 timeText.setText(Utils.formatPercentage(info.batteryLevel));
568 if (info.remainingLabel == null ) {
569 summary1.setText(info.statusLabel);
571 summary1.setText(info.remainingLabel);
574 batteryView.setBatteryLevel(info.batteryLevel);
575 batteryView.setCharging(!info.discharging);
579 double calculatePercentage(double powerUsage, double dischargeAmount) {
580 final double totalPower = mStatsHelper.getTotalPower();
581 return totalPower == 0 ? 0 :
582 ((powerUsage / totalPower) * dischargeAmount);
586 void setUsageSummary(Preference preference, String usedTimePrefix, long usageTimeMs) {
587 // Only show summary when usage time is longer than one minute
588 if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
589 preference.setSummary(String.format(usedTimePrefix,
590 Utils.formatElapsedTime(getContext(), usageTimeMs, false)));
595 String extractKeyFromSipper(BatterySipper sipper) {
596 if (sipper.uidObj != null) {
597 return Integer.toString(sipper.getUid());
598 } else if (sipper.drainType != DrainType.APP) {
599 return sipper.drainType.toString();
600 } else if (sipper.getPackages() != null) {
601 return TextUtils.concat(sipper.getPackages()).toString();
603 Log.w(TAG, "Inappropriate BatterySipper without uid and package names: " + sipper);
609 void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
610 mBatteryLayoutPref = layoutPreference;
614 void initFeatureProvider() {
615 final Context context = getContext();
616 mPowerFeatureProvider = FeatureFactory.getFactory(context)
617 .getPowerUsageFeatureProvider(context);
620 private static List<BatterySipper> getFakeStats() {
621 ArrayList<BatterySipper> stats = new ArrayList<>();
623 for (DrainType type : DrainType.values()) {
624 if (type == DrainType.APP) {
627 stats.add(new BatterySipper(type, null, use));
630 for (int i = 0; i < 100; i++) {
631 stats.add(new BatterySipper(DrainType.APP,
632 new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
634 stats.add(new BatterySipper(DrainType.APP,
635 new FakeUid(0), use));
637 // Simulate dex2oat process.
638 BatterySipper sipper = new BatterySipper(DrainType.APP,
639 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
640 sipper.packageWithHighestDrain = "dex2oat";
643 sipper = new BatterySipper(DrainType.APP,
644 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
645 sipper.packageWithHighestDrain = "dex2oat";
648 sipper = new BatterySipper(DrainType.APP,
649 new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
655 Handler mHandler = new Handler() {
658 public void handleMessage(Message msg) {
660 case BatteryEntry.MSG_UPDATE_NAME_ICON:
661 BatteryEntry entry = (BatteryEntry) msg.obj;
662 PowerGaugePreference pgp =
663 (PowerGaugePreference) findPreference(
664 Integer.toString(entry.sipper.uidObj.getUid()));
666 final int userId = UserHandle.getUserId(entry.sipper.getUid());
667 final UserHandle userHandle = new UserHandle(userId);
668 pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
669 pgp.setTitle(entry.name);
670 if (entry.sipper.drainType == DrainType.APP) {
671 pgp.setContentDescription(entry.name);
675 case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
676 Activity activity = getActivity();
677 if (activity != null) {
678 activity.reportFullyDrawn();
682 super.handleMessage(msg);
686 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
687 private final Context mContext;
688 private final SummaryLoader mLoader;
689 private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
691 private SummaryProvider(Context context, SummaryLoader loader) {
694 mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
695 mBatteryBroadcastReceiver.setBatteryChangedListener(() -> {
696 BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
698 public void onBatteryInfoLoaded(BatteryInfo info) {
699 mLoader.setSummary(SummaryProvider.this, info.chargeLabelString);
706 public void setListening(boolean listening) {
708 mBatteryBroadcastReceiver.register();
710 mBatteryBroadcastReceiver.unRegister();
715 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
716 new BaseSearchIndexProvider() {
718 public List<SearchIndexableResource> getXmlResourcesToIndex(
719 Context context, boolean enabled) {
720 final SearchIndexableResource sir = new SearchIndexableResource(context);
721 sir.xmlResId = R.xml.power_usage_summary;
722 return Arrays.asList(sir);
726 public List<String> getNonIndexableKeys(Context context) {
727 List<String> niks = new ArrayList<>();
728 // Duplicates in display
729 niks.add(KEY_AUTO_BRIGHTNESS);
730 niks.add(KEY_SCREEN_TIMEOUT);
731 niks.add(KEY_BATTERY_SAVER_SUMMARY);
736 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
737 = new SummaryLoader.SummaryProviderFactory() {
739 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
740 SummaryLoader summaryLoader) {
741 return new SummaryProvider(activity, summaryLoader);