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
17package com.android.settings.fuelgauge;
18
19import static android.os.BatteryStats.NETWORK_MOBILE_RX_BYTES;
20import static android.os.BatteryStats.NETWORK_MOBILE_TX_BYTES;
21import static android.os.BatteryStats.NETWORK_WIFI_RX_BYTES;
22import static android.os.BatteryStats.NETWORK_WIFI_TX_BYTES;
23
24import android.app.Activity;
25import android.content.Context;
26import android.content.pm.UserInfo;
27import android.graphics.drawable.Drawable;
28import android.hardware.Sensor;
29import android.hardware.SensorManager;
30import android.os.BatteryStats;
31import android.os.BatteryStats.Uid;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Parcel;
35import android.os.Process;
36import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.os.SystemClock;
39import android.os.UserHandle;
40import android.os.UserManager;
41import android.preference.PreferenceActivity;
42import android.telephony.SignalStrength;
43import android.util.Log;
44import android.util.SparseArray;
45
46import com.android.internal.app.IBatteryStats;
47import com.android.internal.os.BatteryStatsImpl;
48import com.android.internal.os.PowerProfile;
49import com.android.internal.util.FastPrintWriter;
50import com.android.settings.R;
51import com.android.settings.fuelgauge.PowerUsageDetail.DrainType;
52import com.android.settings.users.UserUtils;
53
54import java.io.PrintWriter;
55import java.io.StringWriter;
56import java.io.Writer;
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.List;
60import java.util.Map;
61
62/**
63 * A helper class for retrieving the power usage information for all applications and services.
64 *
65 * The caller must initialize this class as soon as activity object is ready to use (for example, in
66 * onAttach() for Fragment), call create() in onCreate() and call destroy() in onDestroy().
67 */
68public class BatteryStatsHelper {
69
70    private static final boolean DEBUG = false;
71
72    private static final String TAG = BatteryStatsHelper.class.getSimpleName();
73
74    private static BatteryStatsImpl sStatsXfer;
75    private IBatteryStats mBatteryInfo;
76    private UserManager mUm;
77    private BatteryStatsImpl mStats;
78    private PowerProfile mPowerProfile;
79
80    private final List<BatterySipper> mUsageList = new ArrayList<BatterySipper>();
81    private final List<BatterySipper> mWifiSippers = new ArrayList<BatterySipper>();
82    private final List<BatterySipper> mBluetoothSippers = new ArrayList<BatterySipper>();
83    private final SparseArray<List<BatterySipper>> mUserSippers
84            = new SparseArray<List<BatterySipper>>();
85    private final SparseArray<Double> mUserPower = new SparseArray<Double>();
86
87    private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
88
89    private long mStatsPeriod = 0;
90    private double mMaxPower = 1;
91    private double mTotalPower;
92    private double mWifiPower;
93    private double mBluetoothPower;
94
95    // How much the apps together have left WIFI running.
96    private long mAppWifiRunning;
97
98    /** Queue for fetching name and icon for an application */
99    private ArrayList<BatterySipper> mRequestQueue = new ArrayList<BatterySipper>();
100
101    private Activity mActivity;
102    private Handler mHandler;
103
104    private class NameAndIconLoader extends Thread {
105        private boolean mAbort = false;
106
107        public NameAndIconLoader() {
108            super("BatteryUsage Icon Loader");
109        }
110
111        public void abort() {
112            mAbort = true;
113        }
114
115        @Override
116        public void run() {
117            while (true) {
118                BatterySipper bs;
119                synchronized (mRequestQueue) {
120                    if (mRequestQueue.isEmpty() || mAbort) {
121                        mHandler.sendEmptyMessage(MSG_REPORT_FULLY_DRAWN);
122                        return;
123                    }
124                    bs = mRequestQueue.remove(0);
125                }
126                bs.loadNameAndIcon();
127            }
128        }
129    }
130
131    private NameAndIconLoader mRequestThread;
132
133    public BatteryStatsHelper(Activity activity, Handler handler) {
134        mActivity = activity;
135        mHandler = handler;
136    }
137
138    /** Clears the current stats and forces recreating for future use. */
139    public void clearStats() {
140        mStats = null;
141    }
142
143    public BatteryStatsImpl getStats() {
144        if (mStats == null) {
145            load();
146        }
147        return mStats;
148    }
149
150    public PowerProfile getPowerProfile() {
151        return mPowerProfile;
152    }
153
154    public void create(Bundle icicle) {
155        if (icicle != null) {
156            mStats = sStatsXfer;
157        }
158        mBatteryInfo = IBatteryStats.Stub.asInterface(
159                ServiceManager.getService(BatteryStats.SERVICE_NAME));
160        mUm = (UserManager) mActivity.getSystemService(Context.USER_SERVICE);
161        mPowerProfile = new PowerProfile(mActivity);
162    }
163
164    public void pause() {
165        if (mRequestThread != null) {
166            mRequestThread.abort();
167        }
168    }
169
170    public void destroy() {
171        if (mActivity.isChangingConfigurations()) {
172            sStatsXfer = mStats;
173        } else {
174            BatterySipper.sUidCache.clear();
175        }
176    }
177
178    public void startBatteryDetailPage(
179            PreferenceActivity caller, BatterySipper sipper, boolean showLocationButton) {
180        // Initialize mStats if necessary.
181        getStats();
182
183        Bundle args = new Bundle();
184        args.putString(PowerUsageDetail.EXTRA_TITLE, sipper.name);
185        args.putInt(PowerUsageDetail.EXTRA_PERCENT, (int)
186                Math.ceil(sipper.getSortValue() * 100 / mTotalPower));
187        args.putInt(PowerUsageDetail.EXTRA_GAUGE, (int)
188                Math.ceil(sipper.getSortValue() * 100 / mMaxPower));
189        args.putLong(PowerUsageDetail.EXTRA_USAGE_DURATION, mStatsPeriod);
190        args.putString(PowerUsageDetail.EXTRA_ICON_PACKAGE, sipper.defaultPackageName);
191        args.putInt(PowerUsageDetail.EXTRA_ICON_ID, sipper.iconId);
192        args.putDouble(PowerUsageDetail.EXTRA_NO_COVERAGE, sipper.noCoveragePercent);
193        if (sipper.uidObj != null) {
194            args.putInt(PowerUsageDetail.EXTRA_UID, sipper.uidObj.getUid());
195        }
196        args.putSerializable(PowerUsageDetail.EXTRA_DRAIN_TYPE, sipper.drainType);
197        args.putBoolean(PowerUsageDetail.EXTRA_SHOW_LOCATION_BUTTON, showLocationButton);
198
199        int[] types;
200        double[] values;
201        switch (sipper.drainType) {
202            case APP:
203            case USER:
204            {
205                Uid uid = sipper.uidObj;
206                types = new int[] {
207                    R.string.usage_type_cpu,
208                    R.string.usage_type_cpu_foreground,
209                    R.string.usage_type_wake_lock,
210                    R.string.usage_type_gps,
211                    R.string.usage_type_wifi_running,
212                    R.string.usage_type_data_recv,
213                    R.string.usage_type_data_send,
214                    R.string.usage_type_data_wifi_recv,
215                    R.string.usage_type_data_wifi_send,
216                    R.string.usage_type_audio,
217                    R.string.usage_type_video,
218                };
219                values = new double[] {
220                    sipper.cpuTime,
221                    sipper.cpuFgTime,
222                    sipper.wakeLockTime,
223                    sipper.gpsTime,
224                    sipper.wifiRunningTime,
225                    sipper.mobileRxBytes,
226                    sipper.mobileTxBytes,
227                    sipper.wifiRxBytes,
228                    sipper.wifiTxBytes,
229                    0,
230                    0
231                };
232
233                if (sipper.drainType == DrainType.APP) {
234                    Writer result = new StringWriter();
235                    PrintWriter printWriter = new FastPrintWriter(result, false, 1024);
236                    mStats.dumpLocked(printWriter, "", mStatsType, uid.getUid());
237                    printWriter.flush();
238                    args.putString(PowerUsageDetail.EXTRA_REPORT_DETAILS, result.toString());
239
240                    result = new StringWriter();
241                    printWriter = new FastPrintWriter(result, false, 1024);
242                    mStats.dumpCheckinLocked(printWriter, mStatsType, uid.getUid());
243                    printWriter.flush();
244                    args.putString(PowerUsageDetail.EXTRA_REPORT_CHECKIN_DETAILS,
245                            result.toString());
246                }
247            }
248            break;
249            case CELL:
250            {
251                types = new int[] {
252                    R.string.usage_type_on_time,
253                    R.string.usage_type_no_coverage
254                };
255                values = new double[] {
256                    sipper.usageTime,
257                    sipper.noCoveragePercent
258                };
259            }
260            break;
261            case WIFI:
262            {
263                types = new int[] {
264                    R.string.usage_type_wifi_running,
265                    R.string.usage_type_cpu,
266                    R.string.usage_type_cpu_foreground,
267                    R.string.usage_type_wake_lock,
268                    R.string.usage_type_data_recv,
269                    R.string.usage_type_data_send,
270                    R.string.usage_type_data_wifi_recv,
271                    R.string.usage_type_data_wifi_send,
272                };
273                values = new double[] {
274                    sipper.usageTime,
275                    sipper.cpuTime,
276                    sipper.cpuFgTime,
277                    sipper.wakeLockTime,
278                    sipper.mobileRxBytes,
279                    sipper.mobileTxBytes,
280                    sipper.wifiRxBytes,
281                    sipper.wifiTxBytes,
282                };
283            } break;
284            case BLUETOOTH:
285            {
286                types = new int[] {
287                    R.string.usage_type_on_time,
288                    R.string.usage_type_cpu,
289                    R.string.usage_type_cpu_foreground,
290                    R.string.usage_type_wake_lock,
291                    R.string.usage_type_data_recv,
292                    R.string.usage_type_data_send,
293                    R.string.usage_type_data_wifi_recv,
294                    R.string.usage_type_data_wifi_send,
295                };
296                values = new double[] {
297                    sipper.usageTime,
298                    sipper.cpuTime,
299                    sipper.cpuFgTime,
300                    sipper.wakeLockTime,
301                    sipper.mobileRxBytes,
302                    sipper.mobileTxBytes,
303                    sipper.wifiRxBytes,
304                    sipper.wifiTxBytes,
305                };
306            } break;
307            default:
308            {
309                types = new int[] {
310                    R.string.usage_type_on_time
311                };
312                values = new double[] {
313                    sipper.usageTime
314                };
315            }
316        }
317        args.putIntArray(PowerUsageDetail.EXTRA_DETAIL_TYPES, types);
318        args.putDoubleArray(PowerUsageDetail.EXTRA_DETAIL_VALUES, values);
319        caller.startPreferencePanel(PowerUsageDetail.class.getName(), args,
320                R.string.details_title, null, null, 0);
321    }
322
323    /**
324     * Refreshes the power usage list.
325     * @param includeZeroConsumption whether includes those applications which have consumed very
326     *                               little power up till now.
327     */
328    public void refreshStats(boolean includeZeroConsumption) {
329        // Initialize mStats if necessary.
330        getStats();
331
332        mMaxPower = 0;
333        mTotalPower = 0;
334        mWifiPower = 0;
335        mBluetoothPower = 0;
336        mAppWifiRunning = 0;
337
338        mUsageList.clear();
339        mWifiSippers.clear();
340        mBluetoothSippers.clear();
341        mUserSippers.clear();
342        mUserPower.clear();
343
344        processAppUsage(includeZeroConsumption);
345        processMiscUsage();
346
347        Collections.sort(mUsageList);
348
349        if (mHandler != null) {
350            synchronized (mRequestQueue) {
351                if (!mRequestQueue.isEmpty()) {
352                    if (mRequestThread != null) {
353                        mRequestThread.abort();
354                    }
355                    mRequestThread = new NameAndIconLoader();
356                    mRequestThread.setPriority(Thread.MIN_PRIORITY);
357                    mRequestThread.start();
358                    mRequestQueue.notify();
359                }
360            }
361        }
362    }
363
364    private void processAppUsage(boolean includeZeroConsumption) {
365        SensorManager sensorManager = (SensorManager) mActivity.getSystemService(
366                Context.SENSOR_SERVICE);
367        final int which = mStatsType;
368        final int speedSteps = mPowerProfile.getNumSpeedSteps();
369        final double[] powerCpuNormal = new double[speedSteps];
370        final long[] cpuSpeedStepTimes = new long[speedSteps];
371        for (int p = 0; p < speedSteps; p++) {
372            powerCpuNormal[p] = mPowerProfile.getAveragePower(PowerProfile.POWER_CPU_ACTIVE, p);
373        }
374        final double mobilePowerPerByte = getMobilePowerPerByte();
375        final double wifiPowerPerByte = getWifiPowerPerByte();
376        long uSecTime = mStats.computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which);
377        long appWakelockTime = 0;
378        BatterySipper osApp = null;
379        mStatsPeriod = uSecTime;
380        SparseArray<? extends Uid> uidStats = mStats.getUidStats();
381        final int NU = uidStats.size();
382        for (int iu = 0; iu < NU; iu++) {
383            Uid u = uidStats.valueAt(iu);
384            double p; // in mAs
385            double power = 0; // in mAs
386            double highestDrain = 0;
387            String packageWithHighestDrain = null;
388            //mUsageList.add(new AppUsage(u.getUid(), new double[] {power}));
389            Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
390            long cpuTime = 0;
391            long cpuFgTime = 0;
392            long wakelockTime = 0;
393            long gpsTime = 0;
394            if (DEBUG) Log.i(TAG, "UID " + u.getUid());
395            if (processStats.size() > 0) {
396                // Process CPU time
397                for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
398                        : processStats.entrySet()) {
399                    Uid.Proc ps = ent.getValue();
400                    final long userTime = ps.getUserTime(which);
401                    final long systemTime = ps.getSystemTime(which);
402                    final long foregroundTime = ps.getForegroundTime(which);
403                    cpuFgTime += foregroundTime * 10; // convert to millis
404                    final long tmpCpuTime = (userTime + systemTime) * 10; // convert to millis
405                    int totalTimeAtSpeeds = 0;
406                    // Get the total first
407                    for (int step = 0; step < speedSteps; step++) {
408                        cpuSpeedStepTimes[step] = ps.getTimeAtCpuSpeedStep(step, which);
409                        totalTimeAtSpeeds += cpuSpeedStepTimes[step];
410                    }
411                    if (totalTimeAtSpeeds == 0) totalTimeAtSpeeds = 1;
412                    // Then compute the ratio of time spent at each speed
413                    double processPower = 0;
414                    for (int step = 0; step < speedSteps; step++) {
415                        double ratio = (double) cpuSpeedStepTimes[step] / totalTimeAtSpeeds;
416                        processPower += ratio * tmpCpuTime * powerCpuNormal[step];
417                    }
418                    cpuTime += tmpCpuTime;
419                    if (DEBUG && processPower != 0) {
420                        Log.i(TAG, String.format("process %s, cpu power=%.2f",
421                                ent.getKey(), processPower / 1000));
422                    }
423                    power += processPower;
424                    if (packageWithHighestDrain == null
425                            || packageWithHighestDrain.startsWith("*")) {
426                        highestDrain = processPower;
427                        packageWithHighestDrain = ent.getKey();
428                    } else if (highestDrain < processPower
429                            && !ent.getKey().startsWith("*")) {
430                        highestDrain = processPower;
431                        packageWithHighestDrain = ent.getKey();
432                    }
433                }
434            }
435            if (cpuFgTime > cpuTime) {
436                if (DEBUG && cpuFgTime > cpuTime + 10000) {
437                    Log.i(TAG, "WARNING! Cputime is more than 10 seconds behind Foreground time");
438                }
439                cpuTime = cpuFgTime; // Statistics may not have been gathered yet.
440            }
441            power /= 1000;
442            if (DEBUG && power != 0) Log.i(TAG, String.format("total cpu power=%.2f", power));
443
444            // Process wake lock usage
445            Map<String, ? extends BatteryStats.Uid.Wakelock> wakelockStats = u.getWakelockStats();
446            for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> wakelockEntry
447                    : wakelockStats.entrySet()) {
448                Uid.Wakelock wakelock = wakelockEntry.getValue();
449                // Only care about partial wake locks since full wake locks
450                // are canceled when the user turns the screen off.
451                BatteryStats.Timer timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_PARTIAL);
452                if (timer != null) {
453                    wakelockTime += timer.getTotalTimeLocked(uSecTime, which);
454                }
455            }
456            wakelockTime /= 1000; // convert to millis
457            appWakelockTime += wakelockTime;
458
459            // Add cost of holding a wake lock
460            p = (wakelockTime
461                    * mPowerProfile.getAveragePower(PowerProfile.POWER_CPU_AWAKE)) / 1000;
462            power += p;
463            if (DEBUG && p != 0) Log.i(TAG, String.format("wakelock power=%.2f", p));
464
465            // Add cost of mobile traffic
466            final long mobileRx = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, mStatsType);
467            final long mobileTx = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, mStatsType);
468            p = (mobileRx + mobileTx) * mobilePowerPerByte;
469            power += p;
470            if (DEBUG && p != 0) Log.i(TAG, String.format("mobile power=%.2f", p));
471
472            // Add cost of wifi traffic
473            final long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, mStatsType);
474            final long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, mStatsType);
475            p = (wifiRx + wifiTx) * wifiPowerPerByte;
476            power += p;
477            if (DEBUG && p != 0) Log.i(TAG, String.format("wifi power=%.2f", p));
478
479            // Add cost of keeping WIFI running.
480            long wifiRunningTimeMs = u.getWifiRunningTime(uSecTime, which) / 1000;
481            mAppWifiRunning += wifiRunningTimeMs;
482            p = (wifiRunningTimeMs
483                    * mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_ON)) / 1000;
484            power += p;
485            if (DEBUG && p != 0) Log.i(TAG, String.format("wifi running power=%.2f", p));
486
487            // Add cost of WIFI scans
488            long wifiScanTimeMs = u.getWifiScanTime(uSecTime, which) / 1000;
489            p = (wifiScanTimeMs
490                    * mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_SCAN)) / 1000;
491            power += p;
492            if (DEBUG && p != 0) Log.i(TAG, String.format("wifi scanning power=%.2f", p));
493
494            // Process Sensor usage
495            Map<Integer, ? extends BatteryStats.Uid.Sensor> sensorStats = u.getSensorStats();
496            for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> sensorEntry
497                    : sensorStats.entrySet()) {
498                Uid.Sensor sensor = sensorEntry.getValue();
499                int sensorHandle = sensor.getHandle();
500                BatteryStats.Timer timer = sensor.getSensorTime();
501                long sensorTime = timer.getTotalTimeLocked(uSecTime, which) / 1000;
502                double multiplier = 0;
503                switch (sensorHandle) {
504                    case Uid.Sensor.GPS:
505                        multiplier = mPowerProfile.getAveragePower(PowerProfile.POWER_GPS_ON);
506                        gpsTime = sensorTime;
507                        break;
508                    default:
509                        List<Sensor> sensorList = sensorManager.getSensorList(
510                                android.hardware.Sensor.TYPE_ALL);
511                        for (android.hardware.Sensor s : sensorList) {
512                            if (s.getHandle() == sensorHandle) {
513                                multiplier = s.getPower();
514                                break;
515                            }
516                        }
517                }
518                p = (multiplier * sensorTime) / 1000;
519                power += p;
520                if (DEBUG && p != 0) {
521                    Log.i(TAG, String.format("sensor %s power=%.2f", sensor.toString(), p));
522                }
523            }
524
525            if (DEBUG) Log.i(TAG, String.format("UID %d total power=%.2f", u.getUid(), power));
526
527            // Add the app to the list if it is consuming power
528            boolean isOtherUser = false;
529            final int userId = UserHandle.getUserId(u.getUid());
530            if (power != 0 || includeZeroConsumption || u.getUid() == 0) {
531                BatterySipper app = new BatterySipper(mActivity, mRequestQueue, mHandler,
532                        packageWithHighestDrain, DrainType.APP, 0, u,
533                        new double[] {power});
534                app.cpuTime = cpuTime;
535                app.gpsTime = gpsTime;
536                app.wifiRunningTime = wifiRunningTimeMs;
537                app.cpuFgTime = cpuFgTime;
538                app.wakeLockTime = wakelockTime;
539                app.mobileRxBytes = mobileRx;
540                app.mobileTxBytes = mobileTx;
541                app.wifiRxBytes = wifiRx;
542                app.wifiTxBytes = wifiTx;
543                if (u.getUid() == Process.WIFI_UID) {
544                    mWifiSippers.add(app);
545                } else if (u.getUid() == Process.BLUETOOTH_UID) {
546                    mBluetoothSippers.add(app);
547                } else if (userId != UserHandle.myUserId()
548                        && UserHandle.getAppId(u.getUid()) >= Process.FIRST_APPLICATION_UID) {
549                    isOtherUser = true;
550                    List<BatterySipper> list = mUserSippers.get(userId);
551                    if (list == null) {
552                        list = new ArrayList<BatterySipper>();
553                        mUserSippers.put(userId, list);
554                    }
555                    list.add(app);
556                } else {
557                    mUsageList.add(app);
558                }
559                if (u.getUid() == 0) {
560                    osApp = app;
561                }
562            }
563            if (power != 0 || includeZeroConsumption) {
564                if (u.getUid() == Process.WIFI_UID) {
565                    mWifiPower += power;
566                } else if (u.getUid() == Process.BLUETOOTH_UID) {
567                    mBluetoothPower += power;
568                } else if (isOtherUser) {
569                    Double userPower = mUserPower.get(userId);
570                    if (userPower == null) {
571                        userPower = power;
572                    } else {
573                        userPower += power;
574                    }
575                    mUserPower.put(userId, userPower);
576                } else {
577                    if (power > mMaxPower) mMaxPower = power;
578                    mTotalPower += power;
579                }
580            }
581        }
582
583        // The device has probably been awake for longer than the screen on
584        // time and application wake lock time would account for.  Assign
585        // this remainder to the OS, if possible.
586        if (osApp != null) {
587            long wakeTimeMillis = mStats.computeBatteryUptime(
588                    SystemClock.uptimeMillis() * 1000, which) / 1000;
589            wakeTimeMillis -= appWakelockTime + (mStats.getScreenOnTime(
590                    SystemClock.elapsedRealtime(), which) / 1000);
591            if (wakeTimeMillis > 0) {
592                double power = (wakeTimeMillis
593                        * mPowerProfile.getAveragePower(PowerProfile.POWER_CPU_AWAKE)) / 1000;
594                if (DEBUG) Log.i(TAG, "OS wakeLockTime " + wakeTimeMillis + " power " + power);
595                osApp.wakeLockTime += wakeTimeMillis;
596                osApp.value += power;
597                osApp.values[0] += power;
598                if (osApp.value > mMaxPower) mMaxPower = osApp.value;
599                mTotalPower += power;
600            }
601        }
602    }
603
604    private void addPhoneUsage(long uSecNow) {
605        long phoneOnTimeMs = mStats.getPhoneOnTime(uSecNow, mStatsType) / 1000;
606        double phoneOnPower = mPowerProfile.getAveragePower(PowerProfile.POWER_RADIO_ACTIVE)
607                * phoneOnTimeMs / 1000;
608        addEntry(mActivity.getString(R.string.power_phone), DrainType.PHONE, phoneOnTimeMs,
609                R.drawable.ic_settings_voice_calls, phoneOnPower);
610    }
611
612    private void addScreenUsage(long uSecNow) {
613        double power = 0;
614        long screenOnTimeMs = mStats.getScreenOnTime(uSecNow, mStatsType) / 1000;
615        power += screenOnTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_SCREEN_ON);
616        final double screenFullPower =
617                mPowerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
618        for (int i = 0; i < BatteryStats.NUM_SCREEN_BRIGHTNESS_BINS; i++) {
619            double screenBinPower = screenFullPower * (i + 0.5f)
620                    / BatteryStats.NUM_SCREEN_BRIGHTNESS_BINS;
621            long brightnessTime = mStats.getScreenBrightnessTime(i, uSecNow, mStatsType) / 1000;
622            power += screenBinPower * brightnessTime;
623            if (DEBUG) {
624                Log.i(TAG, "Screen bin power = " + (int) screenBinPower + ", time = "
625                        + brightnessTime);
626            }
627        }
628        power /= 1000; // To seconds
629        addEntry(mActivity.getString(R.string.power_screen), DrainType.SCREEN, screenOnTimeMs,
630                R.drawable.ic_settings_display, power);
631    }
632
633    private void addRadioUsage(long uSecNow) {
634        double power = 0;
635        final int BINS = SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
636        long signalTimeMs = 0;
637        for (int i = 0; i < BINS; i++) {
638            long strengthTimeMs = mStats.getPhoneSignalStrengthTime(i, uSecNow, mStatsType) / 1000;
639            power += strengthTimeMs / 1000
640                    * mPowerProfile.getAveragePower(PowerProfile.POWER_RADIO_ON, i);
641            signalTimeMs += strengthTimeMs;
642        }
643        long scanningTimeMs = mStats.getPhoneSignalScanningTime(uSecNow, mStatsType) / 1000;
644        power += scanningTimeMs / 1000 * mPowerProfile.getAveragePower(
645                PowerProfile.POWER_RADIO_SCANNING);
646        BatterySipper bs =
647                addEntry(mActivity.getString(R.string.power_cell), DrainType.CELL,
648                        signalTimeMs, R.drawable.ic_settings_cell_standby, power);
649        if (signalTimeMs != 0) {
650            bs.noCoveragePercent = mStats.getPhoneSignalStrengthTime(0, uSecNow, mStatsType)
651                    / 1000 * 100.0 / signalTimeMs;
652        }
653    }
654
655    private void aggregateSippers(BatterySipper bs, List<BatterySipper> from, String tag) {
656        for (int i=0; i<from.size(); i++) {
657            BatterySipper wbs = from.get(i);
658            if (DEBUG) Log.i(TAG, tag + " adding sipper " + wbs + ": cpu=" + wbs.cpuTime);
659            bs.cpuTime += wbs.cpuTime;
660            bs.gpsTime += wbs.gpsTime;
661            bs.wifiRunningTime += wbs.wifiRunningTime;
662            bs.cpuFgTime += wbs.cpuFgTime;
663            bs.wakeLockTime += wbs.wakeLockTime;
664            bs.mobileRxBytes += wbs.mobileRxBytes;
665            bs.mobileTxBytes += wbs.mobileTxBytes;
666            bs.wifiRxBytes += wbs.wifiRxBytes;
667            bs.wifiTxBytes += wbs.wifiTxBytes;
668        }
669    }
670
671    private void addWiFiUsage(long uSecNow) {
672        long onTimeMs = mStats.getWifiOnTime(uSecNow, mStatsType) / 1000;
673        long runningTimeMs = mStats.getGlobalWifiRunningTime(uSecNow, mStatsType) / 1000;
674        if (DEBUG) Log.i(TAG, "WIFI runningTime=" + runningTimeMs
675                + " app runningTime=" + mAppWifiRunning);
676        runningTimeMs -= mAppWifiRunning;
677        if (runningTimeMs < 0) runningTimeMs = 0;
678        double wifiPower = (onTimeMs * 0 /* TODO */
679                * mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_ON)
680            + runningTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_ON)) / 1000;
681        if (DEBUG) Log.i(TAG, "WIFI power=" + wifiPower + " from procs=" + mWifiPower);
682        BatterySipper bs = addEntry(mActivity.getString(R.string.power_wifi), DrainType.WIFI,
683                runningTimeMs, R.drawable.ic_settings_wifi, wifiPower + mWifiPower);
684        aggregateSippers(bs, mWifiSippers, "WIFI");
685    }
686
687    private void addIdleUsage(long uSecNow) {
688        long idleTimeMs = (uSecNow - mStats.getScreenOnTime(uSecNow, mStatsType)) / 1000;
689        double idlePower = (idleTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_CPU_IDLE))
690                / 1000;
691        addEntry(mActivity.getString(R.string.power_idle), DrainType.IDLE, idleTimeMs,
692                R.drawable.ic_settings_phone_idle, idlePower);
693    }
694
695    private void addBluetoothUsage(long uSecNow) {
696        long btOnTimeMs = mStats.getBluetoothOnTime(uSecNow, mStatsType) / 1000;
697        double btPower = btOnTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_ON)
698                / 1000;
699        int btPingCount = mStats.getBluetoothPingCount();
700        btPower += (btPingCount
701                * mPowerProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_AT_CMD)) / 1000;
702        BatterySipper bs = addEntry(mActivity.getString(R.string.power_bluetooth),
703                DrainType.BLUETOOTH, btOnTimeMs, R.drawable.ic_settings_bluetooth,
704                btPower + mBluetoothPower);
705        aggregateSippers(bs, mBluetoothSippers, "Bluetooth");
706    }
707
708    private void addUserUsage() {
709        for (int i=0; i<mUserSippers.size(); i++) {
710            final int userId = mUserSippers.keyAt(i);
711            final List<BatterySipper> sippers = mUserSippers.valueAt(i);
712            UserInfo info = mUm.getUserInfo(userId);
713            Drawable icon;
714            String name;
715            if (info != null) {
716                icon = UserUtils.getUserIcon(mActivity, mUm, info, mActivity.getResources());
717                name = info != null ? info.name : null;
718                if (name == null) {
719                    name = Integer.toString(info.id);
720                }
721                name = mActivity.getResources().getString(
722                        R.string.running_process_item_user_label, name);
723            } else {
724                icon = null;
725                name = mActivity.getResources().getString(
726                        R.string.running_process_item_removed_user_label);
727            }
728            Double userPower = mUserPower.get(userId);
729            double power = (userPower != null) ? userPower : 0.0;
730            BatterySipper bs = addEntry(name, DrainType.USER, 0, 0, power);
731            bs.icon = icon;
732            aggregateSippers(bs, sippers, "User");
733        }
734    }
735
736    /**
737     * Return estimated power (in mAs) of sending a byte with the mobile radio.
738     */
739    private double getMobilePowerPerByte() {
740        final long MOBILE_BPS = 200000; // TODO: Extract average bit rates from system
741        final double MOBILE_POWER = mPowerProfile.getAveragePower(PowerProfile.POWER_RADIO_ACTIVE)
742                / 3600;
743
744        final long mobileRx = mStats.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, mStatsType);
745        final long mobileTx = mStats.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, mStatsType);
746        final long mobileData = mobileRx + mobileTx;
747
748        final long radioDataUptimeMs = mStats.getRadioDataUptime() / 1000;
749        final long mobileBps = radioDataUptimeMs != 0
750                ? mobileData * 8 * 1000 / radioDataUptimeMs
751                : MOBILE_BPS;
752
753        return MOBILE_POWER / (mobileBps / 8);
754    }
755
756    /**
757     * Return estimated power (in mAs) of sending a byte with the Wi-Fi radio.
758     */
759    private double getWifiPowerPerByte() {
760        final long WIFI_BPS = 1000000; // TODO: Extract average bit rates from system
761        final double WIFI_POWER = mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_ACTIVE)
762                / 3600;
763        return WIFI_POWER / (WIFI_BPS / 8);
764    }
765
766    private void processMiscUsage() {
767        final int which = mStatsType;
768        long uSecTime = SystemClock.elapsedRealtime() * 1000;
769        final long uSecNow = mStats.computeBatteryRealtime(uSecTime, which);
770        final long timeSinceUnplugged = uSecNow;
771        if (DEBUG) {
772            Log.i(TAG, "Uptime since last unplugged = " + (timeSinceUnplugged / 1000));
773        }
774
775        addUserUsage();
776        addPhoneUsage(uSecNow);
777        addScreenUsage(uSecNow);
778        addWiFiUsage(uSecNow);
779        addBluetoothUsage(uSecNow);
780        addIdleUsage(uSecNow); // Not including cellular idle power
781        // Don't compute radio usage if it's a wifi-only device
782        if (!com.android.settings.Utils.isWifiOnly(mActivity)) {
783            addRadioUsage(uSecNow);
784        }
785    }
786
787    private BatterySipper addEntry(String label, DrainType drainType, long time, int iconId,
788            double power) {
789        if (power > mMaxPower) mMaxPower = power;
790        mTotalPower += power;
791        BatterySipper bs = new BatterySipper(mActivity, mRequestQueue, mHandler,
792                label, drainType, iconId, null, new double[] {power});
793        bs.usageTime = time;
794        bs.iconId = iconId;
795        mUsageList.add(bs);
796        return bs;
797    }
798
799    public List<BatterySipper> getUsageList() {
800        return mUsageList;
801    }
802
803    static final int MSG_UPDATE_NAME_ICON = 1;
804    static final int MSG_REPORT_FULLY_DRAWN = 2;
805
806    public double getMaxPower() {
807        return mMaxPower;
808    }
809
810    public double getTotalPower() {
811        return mTotalPower;
812    }
813
814    private void load() {
815        try {
816            byte[] data = mBatteryInfo.getStatistics();
817            Parcel parcel = Parcel.obtain();
818            parcel.unmarshall(data, 0, data.length);
819            parcel.setDataPosition(0);
820            mStats = com.android.internal.os.BatteryStatsImpl.CREATOR
821                    .createFromParcel(parcel);
822            mStats.distributeWorkLocked(BatteryStats.STATS_SINCE_CHARGED);
823        } catch (RemoteException e) {
824            Log.e(TAG, "RemoteException:", e);
825        }
826    }
827}
828