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";
84 private static final int MIN_POWER_THRESHOLD_MILLI_AMP = 5;
85 private static final int MAX_ITEMS_TO_LIST = USE_FAKE_DATA ? 30 : 10;
86 private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
87 private static final int SECONDS_IN_HOUR = 60 * 60;
89 private static final String KEY_SCREEN_USAGE = "screen_usage";
90 private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
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));
192 controllers.add(new TimeoutPreferenceController(context));
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 : removeHiddenBatterySippers(usageList);
437 final int numSippers = usageList.size();
438 for (int i = 0; i < numSippers; i++) {
439 final BatterySipper sipper = usageList.get(i);
440 // Deduct the power of hidden items from total power, which is used to
441 // calculate percentOfTotal
442 double totalPower = USE_FAKE_DATA ?
443 4000 : mStatsHelper.getTotalPower() - hiddenPowerMah;
445 // With deduction in totalPower, percentOfTotal is higher because it adds the part
446 // used in screen, system, etc
447 final double percentOfTotal = totalPower == 0 ? 0 :
448 ((sipper.totalPowerMah / totalPower) * dischargeAmount);
450 if (((int) (percentOfTotal + .5)) < 1) {
453 if (sipper.drainType == BatterySipper.DrainType.OVERCOUNTED) {
454 // Don't show over-counted unless it is at least 2/3 the size of
455 // the largest real entry, and its percent of total is more significant
456 if (sipper.totalPowerMah < ((mStatsHelper.getMaxRealPower() * 2) / 3)) {
459 if (percentOfTotal < 10) {
462 if ("user".equals(Build.TYPE)) {
466 if (sipper.drainType == BatterySipper.DrainType.UNACCOUNTED) {
467 // Don't show over-counted unless it is at least 1/2 the size of
468 // the largest real entry, and its percent of total is more significant
469 if (sipper.totalPowerMah < (mStatsHelper.getMaxRealPower() / 2)) {
472 if (percentOfTotal < 5) {
475 if ("user".equals(Build.TYPE)) {
479 final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
480 final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
481 final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
483 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
486 final String key = extractKeyFromSipper(sipper);
487 PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
489 pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
490 contentDescription, entry);
494 final double percentOfMax = (sipper.totalPowerMah * 100)
495 / mStatsHelper.getMaxPower();
496 sipper.percent = percentOfTotal;
497 pref.setTitle(entry.getLabel());
498 pref.setOrder(i + 1);
499 pref.setPercent(percentOfTotal);
500 if (sipper.usageTimeMs == 0 && sipper.drainType == DrainType.APP) {
501 sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(
502 BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, mStatsType);
504 setUsageSummary(pref, usedTime, sipper.usageTimeMs);
505 if ((sipper.drainType != DrainType.APP
506 || sipper.uidObj.getUid() == Process.ROOT_UID)
507 && sipper.drainType != DrainType.USER) {
508 pref.setTint(colorControl);
511 mAppListGroup.addPreference(pref);
512 if (mAppListGroup.getPreferenceCount() - getCachedCount()
513 > (MAX_ITEMS_TO_LIST + 1)) {
519 addNotAvailableMessage();
521 removeCachedPrefs(mAppListGroup);
523 BatteryEntry.startRequestQueue();
527 BatterySipper findBatterySipperByType(List<BatterySipper> usageList, DrainType type) {
528 for (int i = 0, size = usageList.size(); i < size; i++) {
529 final BatterySipper sipper = usageList.get(i);
530 if (sipper.drainType == type) {
538 void updateScreenPreference() {
539 final BatterySipper sipper = findBatterySipperByType(
540 mStatsHelper.getUsageList(), DrainType.SCREEN);
541 final Context context = getContext();
542 final long usageTimeMs = sipper != null ? sipper.usageTimeMs : 0;
544 mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(context, usageTimeMs, false));
548 void updateLastFullChargePreference(long timeMs) {
549 mLastFullChargePref.setSubtitle(getString(R.string.power_last_full_charge_summary,
550 Utils.formatElapsedTime(getContext(), timeMs, false)));
554 long calculateRunningTimeBasedOnStatsType() {
555 final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
556 // Return the battery time (millisecond) on status mStatsType
557 return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
558 mStatsType /* STATS_SINCE_CHARGED */) / 1000;
562 void updateHeaderPreference(BatteryInfo info) {
563 final Context context = getContext();
564 if (context == null) {
567 final BatteryMeterView batteryView = (BatteryMeterView) mBatteryLayoutPref
568 .findViewById(R.id.battery_header_icon);
569 final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent);
570 final TextView summary1 = (TextView) mBatteryLayoutPref.findViewById(R.id.summary1);
571 timeText.setText(Utils.formatPercentage(info.batteryLevel));
572 if (info.remainingLabel == null ) {
573 summary1.setText(info.statusLabel);
575 summary1.setText(info.remainingLabel);
578 batteryView.setBatteryLevel(info.batteryLevel);
579 batteryView.setCharging(!info.discharging);
583 double calculatePercentage(double powerUsage, double dischargeAmount) {
584 final double totalPower = mStatsHelper.getTotalPower();
585 return totalPower == 0 ? 0 :
586 ((powerUsage / totalPower) * dischargeAmount);
590 void setUsageSummary(Preference preference, String usedTimePrefix, long usageTimeMs) {
591 // Only show summary when usage time is longer than one minute
592 if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
593 preference.setSummary(String.format(usedTimePrefix,
594 Utils.formatElapsedTime(getContext(), usageTimeMs, false)));
599 boolean shouldHideSipper(BatterySipper sipper) {
600 final DrainType drainType = sipper.drainType;
602 return drainType == DrainType.IDLE
603 || drainType == DrainType.CELL
604 || drainType == DrainType.WIFI
605 || drainType == DrainType.SCREEN
606 || drainType == DrainType.BLUETOOTH
607 || drainType == DrainType.UNACCOUNTED
608 || drainType == DrainType.OVERCOUNTED
609 || (sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP
610 || mPowerFeatureProvider.isTypeService(sipper)
611 || mPowerFeatureProvider.isTypeSystem(sipper);
615 String extractKeyFromSipper(BatterySipper sipper) {
616 if (sipper.uidObj != null) {
617 return Integer.toString(sipper.getUid());
618 } else if (sipper.drainType != DrainType.APP) {
619 return sipper.drainType.toString();
620 } else if (sipper.getPackages() != null) {
621 return TextUtils.concat(sipper.getPackages()).toString();
623 Log.w(TAG, "Inappropriate BatterySipper without uid and package names: " + sipper);
629 double removeHiddenBatterySippers(List<BatterySipper> sippers) {
630 double totalPowerMah = 0;
631 for (int i = sippers.size() - 1; i >= 0; i--) {
632 final BatterySipper sipper = sippers.get(i);
633 if (shouldHideSipper(sipper)) {
635 if (sipper.drainType != DrainType.OVERCOUNTED
636 && sipper.drainType != DrainType.UNACCOUNTED) {
637 // Don't add it if it is overcounted or unaccounted
638 totalPowerMah += sipper.totalPowerMah;
643 return totalPowerMah;
647 void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
648 mBatteryLayoutPref = layoutPreference;
652 void initFeatureProvider() {
653 final Context context = getContext();
654 mPowerFeatureProvider = FeatureFactory.getFactory(context)
655 .getPowerUsageFeatureProvider(context);
658 private static List<BatterySipper> getFakeStats() {
659 ArrayList<BatterySipper> stats = new ArrayList<>();
661 for (DrainType type : DrainType.values()) {
662 if (type == DrainType.APP) {
665 stats.add(new BatterySipper(type, null, use));
668 for (int i = 0; i < 100; i++) {
669 stats.add(new BatterySipper(DrainType.APP,
670 new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
672 stats.add(new BatterySipper(DrainType.APP,
673 new FakeUid(0), use));
675 // Simulate dex2oat process.
676 BatterySipper sipper = new BatterySipper(DrainType.APP,
677 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
678 sipper.packageWithHighestDrain = "dex2oat";
681 sipper = new BatterySipper(DrainType.APP,
682 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
683 sipper.packageWithHighestDrain = "dex2oat";
686 sipper = new BatterySipper(DrainType.APP,
687 new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
693 Handler mHandler = new Handler() {
696 public void handleMessage(Message msg) {
698 case BatteryEntry.MSG_UPDATE_NAME_ICON:
699 BatteryEntry entry = (BatteryEntry) msg.obj;
700 PowerGaugePreference pgp =
701 (PowerGaugePreference) findPreference(
702 Integer.toString(entry.sipper.uidObj.getUid()));
704 final int userId = UserHandle.getUserId(entry.sipper.getUid());
705 final UserHandle userHandle = new UserHandle(userId);
706 pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
707 pgp.setTitle(entry.name);
708 if (entry.sipper.drainType == DrainType.APP) {
709 pgp.setContentDescription(entry.name);
713 case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
714 Activity activity = getActivity();
715 if (activity != null) {
716 activity.reportFullyDrawn();
720 super.handleMessage(msg);
724 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
725 private final Context mContext;
726 private final SummaryLoader mLoader;
728 private SummaryProvider(Context context, SummaryLoader loader) {
734 public void setListening(boolean listening) {
737 BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
739 public void onBatteryInfoLoaded(BatteryInfo info) {
740 mLoader.setSummary(SummaryProvider.this, info.chargeLabelString);
747 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
748 new BaseSearchIndexProvider() {
750 public List<SearchIndexableResource> getXmlResourcesToIndex(
751 Context context, boolean enabled) {
752 final SearchIndexableResource sir = new SearchIndexableResource(context);
753 sir.xmlResId = R.xml.power_usage_summary;
754 return Arrays.asList(sir);
758 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
759 = new SummaryLoader.SummaryProviderFactory() {
761 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
762 SummaryLoader summaryLoader) {
763 return new SummaryProvider(activity, summaryLoader);