OSDN Git Service

Revamp battery Ui in low battery mode
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / PowerUsageSummary.java
1 /*
2  * Copyright (C) 2009 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
17 package com.android.settings.fuelgauge;
18
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;
43
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;
64
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;
70
71 /**
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.
74  */
75 public class PowerUsageSummary extends PowerUsageBase {
76
77     static final String TAG = "PowerUsageSummary";
78
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
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;
88
89     private static final String KEY_SCREEN_USAGE = "screen_usage";
90     private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
91
92
93     private static final int MENU_STATS_TYPE = Menu.FIRST;
94     @VisibleForTesting
95     static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
96     @VisibleForTesting
97     static final int MENU_ADDITIONAL_BATTERY_INFO = Menu.FIRST + 4;
98     @VisibleForTesting
99     static final int MENU_TOGGLE_APPS = Menu.FIRST + 5;
100     private static final int MENU_HELP = Menu.FIRST + 6;
101
102     private final FooterPreferenceMixin mFooterPreferenceMixin =
103             new FooterPreferenceMixin(this, getLifecycle());
104
105     @VisibleForTesting
106     boolean mShowAllApps = false;
107     @VisibleForTesting
108     PowerGaugePreference mScreenUsagePref;
109     @VisibleForTesting
110     PowerGaugePreference mLastFullChargePref;
111     @VisibleForTesting
112     PowerUsageFeatureProvider mPowerFeatureProvider;
113     @VisibleForTesting
114     BatteryUtils mBatteryUtils;
115
116     private LayoutPreference mBatteryLayoutPref;
117     private PreferenceGroup mAppListGroup;
118     private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
119
120     @Override
121     public void onCreate(Bundle icicle) {
122         super.onCreate(icicle);
123         setAnimationAllowed(true);
124
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);
131
132         mBatteryUtils = BatteryUtils.getInstance(getContext());
133
134         initFeatureProvider();
135     }
136
137     @Override
138     public int getMetricsCategory() {
139         return MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY;
140     }
141
142     @Override
143     public void onResume() {
144         super.onResume();
145         refreshStats();
146     }
147
148     @Override
149     public void onPause() {
150         BatteryEntry.stopRequestQueue();
151         mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
152         super.onPause();
153     }
154
155     @Override
156     public void onDestroy() {
157         super.onDestroy();
158         if (getActivity().isChangingConfigurations()) {
159             BatteryEntry.clearUidCache();
160         }
161     }
162
163     @Override
164     public boolean onPreferenceTreeClick(Preference preference) {
165         if (KEY_BATTERY_HEADER.equals(preference.getKey())) {
166             performBatteryHeaderClick();
167             return true;
168         } else if (!(preference instanceof PowerGaugePreference)) {
169             return super.onPreferenceTreeClick(preference);
170         }
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);
176     }
177
178     @Override
179     protected String getLogTag() {
180         return TAG;
181     }
182
183     @Override
184     protected int getPreferenceScreenResId() {
185         return R.xml.power_usage_summary;
186     }
187
188     @Override
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));
195         return controllers;
196     }
197
198     @Override
199     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
200         if (DEBUG) {
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');
204         }
205
206         menu.add(Menu.NONE, MENU_HIGH_POWER_APPS, Menu.NONE, R.string.high_power_apps);
207
208         if (mPowerFeatureProvider.isAdditionalBatteryInfoEnabled()) {
209             menu.add(Menu.NONE, MENU_ADDITIONAL_BATTERY_INFO,
210                     Menu.NONE, R.string.additional_battery_info);
211         }
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);
215         }
216
217         super.onCreateOptionsMenu(menu, inflater);
218     }
219
220     @Override
221     protected int getHelpResource() {
222         return R.string.help_url_battery;
223     }
224
225     @Override
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();
231
232         switch (item.getItemId()) {
233             case MENU_STATS_TYPE:
234                 if (mStatsType == BatteryStats.STATS_SINCE_CHARGED) {
235                     mStatsType = BatteryStats.STATS_SINCE_UNPLUGGED;
236                 } else {
237                     mStatsType = BatteryStats.STATS_SINCE_CHARGED;
238                 }
239                 refreshStats();
240                 return true;
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);
249                 return true;
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);
256                 return true;
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);
262                 refreshStats();
263                 return true;
264             default:
265                 return super.onOptionsItemSelected(item);
266         }
267     }
268
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);
277         }
278     }
279
280     private void performBatteryHeaderClick() {
281         final Context context = getContext();
282         final PowerUsageFeatureProvider featureProvider = FeatureFactory.getFactory(context)
283                 .getPowerUsageFeatureProvider(context);
284
285         if (featureProvider.isAdvancedUiEnabled()) {
286             Utils.startWithFragment(getContext(), PowerUsageAdvanced.class.getName(), null,
287                     null, 0, R.string.advanced_battery_title, null, getMetricsCategory());
288         } else {
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());
297         }
298     }
299
300     private static boolean isSharedGid(int uid) {
301         return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
302     }
303
304     private static boolean isSystemUid(int uid) {
305         return uid >= Process.SYSTEM_UID && uid < Process.FIRST_APPLICATION_UID;
306     }
307
308     /**
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.
312      *
313      * @return A sorted list of apps using power.
314      */
315     private static List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
316         final SparseArray<BatterySipper> uidList = new SparseArray<>();
317
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();
324
325                 // Check if this UID is a shared GID. If so, we combine it with the OWNER's
326                 // actual app UID.
327                 if (isSharedGid(sipper.getUid())) {
328                     realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
329                             UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
330                 }
331
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;
339                 }
340
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;
348                     sipper = newSipper;
349                 }
350
351                 int index = uidList.indexOfKey(realUid);
352                 if (index < 0) {
353                     // New entry.
354                     uidList.put(realUid, sipper);
355                 } else {
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;
362                     }
363
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,
372                                     existingPackageLen);
373                         }
374                         System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
375                                 newPackageLen);
376                         existingSipper.mPackages = newPackages;
377                     }
378                 }
379             } else {
380                 results.add(sipper);
381             }
382         }
383
384         final int numUidSippers = uidList.size();
385         for (int i = 0; i < numUidSippers; i++) {
386             results.add(uidList.valueAt(i));
387         }
388
389         // The sort order must have changed, so re-sort based on total power use.
390         Collections.sort(results, new Comparator<BatterySipper>() {
391             @Override
392             public int compare(BatterySipper a, BatterySipper b) {
393                 return Double.compare(b.totalPowerMah, a.totalPowerMah);
394             }
395         });
396         return results;
397     }
398
399     protected void refreshStats() {
400         super.refreshStats();
401
402         BatteryInfo.getBatteryInfo(getContext(), new BatteryInfo.Callback() {
403             @Override
404             public void onBatteryInfoLoaded(BatteryInfo info) {
405                 updateHeaderPreference(info);
406             }
407         });
408
409         cacheRemoveAllPrefs(mAppListGroup);
410         mAppListGroup.setOrderingAsAdded(false);
411         boolean addedSome = false;
412
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();
417
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;
424
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)));
430
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());
434
435             double hiddenPowerMah = mShowAllApps ? 0 : removeHiddenBatterySippers(usageList);
436
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;
444
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);
449
450                 if (((int) (percentOfTotal + .5)) < 1) {
451                     continue;
452                 }
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)) {
457                         continue;
458                     }
459                     if (percentOfTotal < 10) {
460                         continue;
461                     }
462                     if ("user".equals(Build.TYPE)) {
463                         continue;
464                     }
465                 }
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)) {
470                         continue;
471                     }
472                     if (percentOfTotal < 5) {
473                         continue;
474                     }
475                     if ("user".equals(Build.TYPE)) {
476                         continue;
477                     }
478                 }
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(),
482                         userHandle);
483                 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
484                         userHandle);
485
486                 final String key = extractKeyFromSipper(sipper);
487                 PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
488                 if (pref == null) {
489                     pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
490                             contentDescription, entry);
491                     pref.setKey(key);
492                 }
493
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);
503                 }
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);
509                 }
510                 addedSome = true;
511                 mAppListGroup.addPreference(pref);
512                 if (mAppListGroup.getPreferenceCount() - getCachedCount()
513                         > (MAX_ITEMS_TO_LIST + 1)) {
514                     break;
515                 }
516             }
517         }
518         if (!addedSome) {
519             addNotAvailableMessage();
520         }
521         removeCachedPrefs(mAppListGroup);
522
523         BatteryEntry.startRequestQueue();
524     }
525
526     @VisibleForTesting
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) {
531                 return sipper;
532             }
533         }
534         return null;
535     }
536
537     @VisibleForTesting
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;
543
544         mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(context, usageTimeMs, false));
545     }
546
547     @VisibleForTesting
548     void updateLastFullChargePreference(long timeMs) {
549         mLastFullChargePref.setSubtitle(getString(R.string.power_last_full_charge_summary,
550                 Utils.formatElapsedTime(getContext(), timeMs, false)));
551     }
552
553     @VisibleForTesting
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;
559     }
560
561     @VisibleForTesting
562     void updateHeaderPreference(BatteryInfo info) {
563         final Context context = getContext();
564         if (context == null) {
565             return;
566         }
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);
574         } else {
575             summary1.setText(info.remainingLabel);
576         }
577
578         batteryView.setBatteryLevel(info.batteryLevel);
579         batteryView.setCharging(!info.discharging);
580     }
581
582     @VisibleForTesting
583     double calculatePercentage(double powerUsage, double dischargeAmount) {
584         final double totalPower = mStatsHelper.getTotalPower();
585         return totalPower == 0 ? 0 :
586                 ((powerUsage / totalPower) * dischargeAmount);
587     }
588
589     @VisibleForTesting
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)));
595         }
596     }
597
598     @VisibleForTesting
599     boolean shouldHideSipper(BatterySipper sipper) {
600         final DrainType drainType = sipper.drainType;
601
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);
612     }
613
614     @VisibleForTesting
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();
622         } else {
623             Log.w(TAG, "Inappropriate BatterySipper without uid and package names: " + sipper);
624             return "-1";
625         }
626     }
627
628     @VisibleForTesting
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)) {
634                 sippers.remove(i);
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;
639                 }
640             }
641         }
642
643         return totalPowerMah;
644     }
645
646     @VisibleForTesting
647     void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
648         mBatteryLayoutPref = layoutPreference;
649     }
650
651     @VisibleForTesting
652     void initFeatureProvider() {
653         final Context context = getContext();
654         mPowerFeatureProvider = FeatureFactory.getFactory(context)
655                 .getPowerUsageFeatureProvider(context);
656     }
657
658     private static List<BatterySipper> getFakeStats() {
659         ArrayList<BatterySipper> stats = new ArrayList<>();
660         float use = 5;
661         for (DrainType type : DrainType.values()) {
662             if (type == DrainType.APP) {
663                 continue;
664             }
665             stats.add(new BatterySipper(type, null, use));
666             use += 5;
667         }
668         for (int i = 0; i < 100; i++) {
669             stats.add(new BatterySipper(DrainType.APP,
670                     new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
671         }
672         stats.add(new BatterySipper(DrainType.APP,
673                 new FakeUid(0), use));
674
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";
679         stats.add(sipper);
680
681         sipper = new BatterySipper(DrainType.APP,
682                 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
683         sipper.packageWithHighestDrain = "dex2oat";
684         stats.add(sipper);
685
686         sipper = new BatterySipper(DrainType.APP,
687                 new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
688         stats.add(sipper);
689
690         return stats;
691     }
692
693     Handler mHandler = new Handler() {
694
695         @Override
696         public void handleMessage(Message msg) {
697             switch (msg.what) {
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()));
703                     if (pgp != null) {
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);
710                         }
711                     }
712                     break;
713                 case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
714                     Activity activity = getActivity();
715                     if (activity != null) {
716                         activity.reportFullyDrawn();
717                     }
718                     break;
719             }
720             super.handleMessage(msg);
721         }
722     };
723
724     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
725         private final Context mContext;
726         private final SummaryLoader mLoader;
727
728         private SummaryProvider(Context context, SummaryLoader loader) {
729             mContext = context;
730             mLoader = loader;
731         }
732
733         @Override
734         public void setListening(boolean listening) {
735             if (listening) {
736                 // TODO: Listen.
737                 BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
738                     @Override
739                     public void onBatteryInfoLoaded(BatteryInfo info) {
740                         mLoader.setSummary(SummaryProvider.this, info.chargeLabelString);
741                     }
742                 });
743             }
744         }
745     }
746
747     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
748             new BaseSearchIndexProvider() {
749                 @Override
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);
755                 }
756             };
757
758     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
759             = new SummaryLoader.SummaryProviderFactory() {
760         @Override
761         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
762                 SummaryLoader summaryLoader) {
763             return new SummaryProvider(activity, summaryLoader);
764         }
765     };
766 }