OSDN Git Service

5f17860d908b1d41eba32733052694d8cb5700bb
[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.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;
49
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.Comparator;
53 import java.util.List;
54
55 /**
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.
58  */
59 public class PowerUsageSummary extends PowerUsageBase {
60
61     private static final boolean DEBUG = false;
62
63     private static final boolean USE_FAKE_DATA = false;
64
65     static final String TAG = "PowerUsageSummary";
66
67     private static final String KEY_APP_LIST = "app_list";
68     private static final String KEY_BATTERY_HISTORY = "battery_history";
69
70     private static final int MENU_STATS_TYPE = Menu.FIRST;
71     private static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
72     @VisibleForTesting
73     static final int MENU_ADDITIONAL_BATTERY_INFO = Menu.FIRST + 4;
74     private static final int MENU_HELP = Menu.FIRST + 5;
75
76     private BatteryHistoryPreference mHistPref;
77     private PreferenceGroup mAppListGroup;
78
79     private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
80
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;
85
86     @Override
87     public void onCreate(Bundle icicle) {
88         super.onCreate(icicle);
89         setAnimationAllowed(true);
90
91         addPreferencesFromResource(R.xml.power_usage_summary);
92         mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_HISTORY);
93         mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
94     }
95
96     @Override
97     protected int getMetricsCategory() {
98         return MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY;
99     }
100
101     @Override
102     public void onResume() {
103         super.onResume();
104         refreshStats();
105     }
106
107     @Override
108     public void onPause() {
109         BatteryEntry.stopRequestQueue();
110         mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
111         super.onPause();
112     }
113
114     @Override
115     public void onDestroy() {
116         super.onDestroy();
117         if (getActivity().isChangingConfigurations()) {
118             BatteryEntry.clearUidCache();
119         }
120     }
121
122     @Override
123     public boolean onPreferenceTreeClick(Preference preference) {
124         if (!(preference instanceof PowerGaugePreference)) {
125             return super.onPreferenceTreeClick(preference);
126         }
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);
132     }
133
134     @Override
135     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
136         if (DEBUG) {
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');
140         }
141
142         menu.add(Menu.NONE, MENU_HIGH_POWER_APPS, Menu.NONE, R.string.high_power_apps);
143
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);
150         }
151         super.onCreateOptionsMenu(menu, inflater);
152     }
153
154     @Override
155     protected int getHelpResource() {
156         return R.string.help_url_battery;
157     }
158
159     @Override
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;
166                 } else {
167                     mStatsType = BatteryStats.STATS_SINCE_CHARGED;
168                 }
169                 refreshStats();
170                 return true;
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);
177                 return true;
178             default:
179                 return super.onOptionsItemSelected(item);
180         }
181     }
182
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);
191         }
192     }
193
194     private static boolean isSharedGid(int uid) {
195         return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
196     }
197
198     private static boolean isSystemUid(int uid) {
199         return uid >= Process.SYSTEM_UID && uid < Process.FIRST_APPLICATION_UID;
200     }
201
202     /**
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.
207      */
208     private static List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
209         final SparseArray<BatterySipper> uidList = new SparseArray<>();
210
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();
217
218                 // Check if this UID is a shared GID. If so, we combine it with the OWNER's
219                 // actual app UID.
220                 if (isSharedGid(sipper.getUid())) {
221                     realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
222                             UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
223                 }
224
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;
232                 }
233
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;
241                     sipper = newSipper;
242                 }
243
244                 int index = uidList.indexOfKey(realUid);
245                 if (index < 0) {
246                     // New entry.
247                     uidList.put(realUid, sipper);
248                 } else {
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;
255                     }
256
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,
265                                     existingPackageLen);
266                         }
267                         System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
268                                 newPackageLen);
269                         existingSipper.mPackages = newPackages;
270                     }
271                 }
272             } else {
273                 results.add(sipper);
274             }
275         }
276
277         final int numUidSippers = uidList.size();
278         for (int i = 0; i < numUidSippers; i++) {
279             results.add(uidList.valueAt(i));
280         }
281
282         // The sort order must have changed, so re-sort based on total power use.
283         Collections.sort(results, new Comparator<BatterySipper>() {
284             @Override
285             public int compare(BatterySipper a, BatterySipper b) {
286                 return Double.compare(b.totalPowerMah, a.totalPowerMah);
287             }
288         });
289         return results;
290     }
291
292     protected void refreshStats() {
293         super.refreshStats();
294         updatePreference(mHistPref);
295         cacheRemoveAllPrefs(mAppListGroup);
296         mAppListGroup.setOrderingAsAdded(false);
297         boolean addedSome = false;
298
299         final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
300         final BatteryStats stats = mStatsHelper.getStats();
301         final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
302
303         TypedValue value = new TypedValue();
304         getContext().getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
305         int colorControl = getContext().getColor(value.resourceId);
306
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());
310
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) {
317                     continue;
318                 }
319                 double totalPower = USE_FAKE_DATA ? 4000 : mStatsHelper.getTotalPower();
320                 final double percentOfTotal =
321                         ((sipper.totalPowerMah / totalPower) * dischargeAmount);
322                 if (((int) (percentOfTotal + .5)) < 1) {
323                     continue;
324                 }
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)) {
329                         continue;
330                     }
331                     if (percentOfTotal < 10) {
332                         continue;
333                     }
334                     if ("user".equals(Build.TYPE)) {
335                         continue;
336                     }
337                 }
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)) {
342                         continue;
343                     }
344                     if (percentOfTotal < 5) {
345                         continue;
346                     }
347                     if ("user".equals(Build.TYPE)) {
348                         continue;
349                     }
350                 }
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(),
354                         userHandle);
355                 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
356                         userHandle);
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);
362                 if (pref == null) {
363                     pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
364                             contentDescription, entry);
365                     pref.setKey(key);
366                 }
367
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()));
376                 }
377                 if ((sipper.drainType != DrainType.APP || sipper.uidObj.getUid() == 0)
378                          && sipper.drainType != DrainType.USER) {
379                     pref.setTint(colorControl);
380                 }
381                 addedSome = true;
382                 mAppListGroup.addPreference(pref);
383                 if (mAppListGroup.getPreferenceCount() - getCachedCount()
384                         > (MAX_ITEMS_TO_LIST + 1)) {
385                     break;
386                 }
387             }
388         }
389         if (!addedSome) {
390             addNotAvailableMessage();
391         }
392         removeCachedPrefs(mAppListGroup);
393
394         BatteryEntry.startRequestQueue();
395     }
396
397     private static List<BatterySipper> getFakeStats() {
398         ArrayList<BatterySipper> stats = new ArrayList<>();
399         float use = 5;
400         for (DrainType type : DrainType.values()) {
401             if (type == DrainType.APP) {
402                 continue;
403             }
404             stats.add(new BatterySipper(type, null, use));
405             use += 5;
406         }
407         for (int i = 0; i < 100; i++) {
408             stats.add(new BatterySipper(DrainType.APP,
409                     new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
410         }
411         stats.add(new BatterySipper(DrainType.APP,
412                 new FakeUid(0), use));
413
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";
418         stats.add(sipper);
419
420         sipper = new BatterySipper(DrainType.APP,
421                 new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
422         sipper.packageWithHighestDrain = "dex2oat";
423         stats.add(sipper);
424
425         sipper = new BatterySipper(DrainType.APP,
426                 new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
427         stats.add(sipper);
428
429         return stats;
430     }
431
432     Handler mHandler = new Handler() {
433
434         @Override
435         public void handleMessage(Message msg) {
436             switch (msg.what) {
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()));
442                     if (pgp != null) {
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);
449                         }
450                     }
451                     break;
452                 case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
453                     Activity activity = getActivity();
454                     if (activity != null) {
455                         activity.reportFullyDrawn();
456                     }
457                     break;
458             }
459             super.handleMessage(msg);
460         }
461     };
462
463     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
464         private final Context mContext;
465         private final SummaryLoader mLoader;
466
467         private SummaryProvider(Context context, SummaryLoader loader) {
468             mContext = context;
469             mLoader = loader;
470         }
471
472         @Override
473         public void setListening(boolean listening) {
474             if (listening) {
475                 // TODO: Listen.
476                 BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
477                     @Override
478                     public void onBatteryInfoLoaded(BatteryInfo info) {
479                         mLoader.setSummary(SummaryProvider.this, info.mChargeLabelString);
480                     }
481                 });
482             }
483         }
484     }
485
486     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
487             = new SummaryLoader.SummaryProviderFactory() {
488         @Override
489         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
490                                                                    SummaryLoader summaryLoader) {
491             return new SummaryProvider(activity, summaryLoader);
492         }
493     };
494 }