BatteryStatsService.java revision 2c43c339de5aaf4fef58aa9b5ac3af48609263a8
1/*
2 * Copyright (C) 2006-2007 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.server.am;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothHeadset;
21import android.bluetooth.BluetoothProfile;
22import android.content.Context;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.os.BatteryStats;
26import android.os.Binder;
27import android.os.Handler;
28import android.os.IBinder;
29import android.os.Parcel;
30import android.os.PowerManagerInternal;
31import android.os.Process;
32import android.os.ServiceManager;
33import android.os.SystemClock;
34import android.os.UserHandle;
35import android.os.WorkSource;
36import android.telephony.DataConnectionRealTimeInfo;
37import android.telephony.SignalStrength;
38import android.telephony.TelephonyManager;
39import android.util.Slog;
40
41import com.android.internal.app.IBatteryStats;
42import com.android.internal.os.BatteryStatsImpl;
43import com.android.internal.os.PowerProfile;
44import com.android.server.LocalServices;
45
46import java.io.FileDescriptor;
47import java.io.PrintWriter;
48import java.util.List;
49
50/**
51 * All information we are collecting about things that can happen that impact
52 * battery life.
53 */
54public final class BatteryStatsService extends IBatteryStats.Stub
55        implements PowerManagerInternal.LowPowerModeListener {
56    static final String TAG = "BatteryStatsService";
57
58    static IBatteryStats sService;
59
60    final BatteryStatsImpl mStats;
61    Context mContext;
62    private boolean mBluetoothPendingStats;
63    private BluetoothHeadset mBluetoothHeadset;
64    PowerManagerInternal mPowerManagerInternal;
65
66    BatteryStatsService(String filename, Handler handler) {
67        mStats = new BatteryStatsImpl(filename, handler);
68    }
69
70    public void publish(Context context) {
71        mContext = context;
72        ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
73        mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
74        mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
75                com.android.internal.R.integer.config_radioScanningTimeout)
76                * 1000L);
77    }
78
79    /**
80     * At the time when the constructor runs, the power manager has not yet been
81     * initialized.  So we initialize the low power observer later.
82     */
83    public void initPowerManagement() {
84        mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
85        mPowerManagerInternal.registerLowPowerModeObserver(this);
86        mStats.noteLowPowerMode(mPowerManagerInternal.getLowPowerModeEnabled());
87        (new WakeupReasonThread()).start();
88    }
89
90    public void shutdown() {
91        Slog.w("BatteryStats", "Writing battery stats before shutdown...");
92        synchronized (mStats) {
93            mStats.shutdownLocked();
94        }
95    }
96
97    public static IBatteryStats getService() {
98        if (sService != null) {
99            return sService;
100        }
101        IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
102        sService = asInterface(b);
103        return sService;
104    }
105
106    @Override
107    public void onLowPowerModeChanged(boolean enabled) {
108        synchronized (mStats) {
109            mStats.noteLowPowerMode(enabled);
110        }
111    }
112
113    /**
114     * @return the current statistics object, which may be modified
115     * to reflect events that affect battery usage.  You must lock the
116     * stats object before doing anything with it.
117     */
118    public BatteryStatsImpl getActiveStatistics() {
119        return mStats;
120    }
121
122    public byte[] getStatistics() {
123        mContext.enforceCallingPermission(
124                android.Manifest.permission.BATTERY_STATS, null);
125        //Slog.i("foo", "SENDING BATTERY INFO:");
126        //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
127        Parcel out = Parcel.obtain();
128        mStats.writeToParcel(out, 0);
129        byte[] data = out.marshall();
130        out.recycle();
131        return data;
132    }
133
134    public long computeBatteryTimeRemaining() {
135        synchronized (mStats) {
136            long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
137            return time >= 0 ? (time/1000) : time;
138        }
139    }
140
141    public long computeChargeTimeRemaining() {
142        synchronized (mStats) {
143            long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime());
144            return time >= 0 ? (time/1000) : time;
145        }
146    }
147
148    public void addIsolatedUid(int isolatedUid, int appUid) {
149        enforceCallingPermission();
150        synchronized (mStats) {
151            mStats.addIsolatedUidLocked(isolatedUid, appUid);
152        }
153    }
154
155    public void removeIsolatedUid(int isolatedUid, int appUid) {
156        enforceCallingPermission();
157        synchronized (mStats) {
158            mStats.removeIsolatedUidLocked(isolatedUid, appUid);
159        }
160    }
161
162    public void noteEvent(int code, String name, int uid) {
163        enforceCallingPermission();
164        synchronized (mStats) {
165            mStats.noteEventLocked(code, name, uid);
166        }
167    }
168
169    public void noteStartWakelock(int uid, int pid, String name, String historyName, int type,
170            boolean unimportantForLogging) {
171        enforceCallingPermission();
172        synchronized (mStats) {
173            mStats.noteStartWakeLocked(uid, pid, name, historyName, type, unimportantForLogging,
174                    SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
175        }
176    }
177
178    public void noteStopWakelock(int uid, int pid, String name, String historyName, int type) {
179        enforceCallingPermission();
180        synchronized (mStats) {
181            mStats.noteStopWakeLocked(uid, pid, name, historyName, type,
182                    SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
183        }
184    }
185
186    public void noteStartWakelockFromSource(WorkSource ws, int pid, String name,
187            String historyName, int type, boolean unimportantForLogging) {
188        enforceCallingPermission();
189        synchronized (mStats) {
190            mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName,
191                    type, unimportantForLogging);
192        }
193    }
194
195    public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name,
196            String historyName, int type, WorkSource newWs, int newPid, String newName,
197            String newHistoryName, int newType, boolean newUnimportantForLogging) {
198        enforceCallingPermission();
199        synchronized (mStats) {
200            mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type,
201                    newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging);
202        }
203    }
204
205    public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName,
206            int type) {
207        enforceCallingPermission();
208        synchronized (mStats) {
209            mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type);
210        }
211    }
212
213    public void noteStartSensor(int uid, int sensor) {
214        enforceCallingPermission();
215        synchronized (mStats) {
216            mStats.noteStartSensorLocked(uid, sensor);
217        }
218    }
219
220    public void noteStopSensor(int uid, int sensor) {
221        enforceCallingPermission();
222        synchronized (mStats) {
223            mStats.noteStopSensorLocked(uid, sensor);
224        }
225    }
226
227    public void noteVibratorOn(int uid, long durationMillis) {
228        enforceCallingPermission();
229        synchronized (mStats) {
230            mStats.noteVibratorOnLocked(uid, durationMillis);
231        }
232    }
233
234    public void noteVibratorOff(int uid) {
235        enforceCallingPermission();
236        synchronized (mStats) {
237            mStats.noteVibratorOffLocked(uid);
238        }
239    }
240
241    public void noteStartGps(int uid) {
242        enforceCallingPermission();
243        synchronized (mStats) {
244            mStats.noteStartGpsLocked(uid);
245        }
246    }
247
248    public void noteStopGps(int uid) {
249        enforceCallingPermission();
250        synchronized (mStats) {
251            mStats.noteStopGpsLocked(uid);
252        }
253    }
254
255    public void noteScreenState(int state) {
256        enforceCallingPermission();
257        synchronized (mStats) {
258            mStats.noteScreenStateLocked(state);
259        }
260    }
261
262    public void noteScreenBrightness(int brightness) {
263        enforceCallingPermission();
264        synchronized (mStats) {
265            mStats.noteScreenBrightnessLocked(brightness);
266        }
267    }
268
269    public void noteUserActivity(int uid, int event) {
270        enforceCallingPermission();
271        synchronized (mStats) {
272            mStats.noteUserActivityLocked(uid, event);
273        }
274    }
275
276    public void noteInteractive(boolean interactive) {
277        enforceCallingPermission();
278        synchronized (mStats) {
279            mStats.noteInteractiveLocked(interactive);
280        }
281    }
282
283    public void noteMobileRadioPowerState(int powerState, long timestampNs) {
284        enforceCallingPermission();
285        synchronized (mStats) {
286            mStats.noteMobileRadioPowerState(powerState, timestampNs);
287        }
288    }
289
290    public void notePhoneOn() {
291        enforceCallingPermission();
292        synchronized (mStats) {
293            mStats.notePhoneOnLocked();
294        }
295    }
296
297    public void notePhoneOff() {
298        enforceCallingPermission();
299        synchronized (mStats) {
300            mStats.notePhoneOffLocked();
301        }
302    }
303
304    public void notePhoneSignalStrength(SignalStrength signalStrength) {
305        enforceCallingPermission();
306        synchronized (mStats) {
307            mStats.notePhoneSignalStrengthLocked(signalStrength);
308        }
309    }
310
311    public void notePhoneDataConnectionState(int dataType, boolean hasData) {
312        enforceCallingPermission();
313        synchronized (mStats) {
314            mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
315        }
316    }
317
318    public void notePhoneState(int state) {
319        enforceCallingPermission();
320        int simState = TelephonyManager.getDefault().getSimState();
321        synchronized (mStats) {
322            mStats.notePhoneStateLocked(state, simState);
323        }
324    }
325
326    public void noteWifiOn() {
327        enforceCallingPermission();
328        synchronized (mStats) {
329            mStats.noteWifiOnLocked();
330        }
331    }
332
333    public void noteWifiOff() {
334        enforceCallingPermission();
335        synchronized (mStats) {
336            mStats.noteWifiOffLocked();
337        }
338    }
339
340    public void noteStartAudio(int uid) {
341        enforceCallingPermission();
342        synchronized (mStats) {
343            mStats.noteAudioOnLocked(uid);
344        }
345    }
346
347    public void noteStopAudio(int uid) {
348        enforceCallingPermission();
349        synchronized (mStats) {
350            mStats.noteAudioOffLocked(uid);
351        }
352    }
353
354    public void noteStartVideo(int uid) {
355        enforceCallingPermission();
356        synchronized (mStats) {
357            mStats.noteVideoOnLocked(uid);
358        }
359    }
360
361    public void noteStopVideo(int uid) {
362        enforceCallingPermission();
363        synchronized (mStats) {
364            mStats.noteVideoOffLocked(uid);
365        }
366    }
367
368    public void noteWifiRunning(WorkSource ws) {
369        enforceCallingPermission();
370        synchronized (mStats) {
371            mStats.noteWifiRunningLocked(ws);
372        }
373    }
374
375    public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
376        enforceCallingPermission();
377        synchronized (mStats) {
378            mStats.noteWifiRunningChangedLocked(oldWs, newWs);
379        }
380    }
381
382    public void noteWifiStopped(WorkSource ws) {
383        enforceCallingPermission();
384        synchronized (mStats) {
385            mStats.noteWifiStoppedLocked(ws);
386        }
387    }
388
389    public void noteWifiState(int wifiState, String accessPoint) {
390        enforceCallingPermission();
391        synchronized (mStats) {
392            mStats.noteWifiStateLocked(wifiState, accessPoint);
393        }
394    }
395
396    public void noteBluetoothOn() {
397        enforceCallingPermission();
398        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
399        if (adapter != null) {
400            adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
401                                    BluetoothProfile.HEADSET);
402        }
403        synchronized (mStats) {
404            if (mBluetoothHeadset != null) {
405                mStats.noteBluetoothOnLocked();
406                mStats.setBtHeadset(mBluetoothHeadset);
407            } else {
408                mBluetoothPendingStats = true;
409            }
410        }
411    }
412
413    private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
414        new BluetoothProfile.ServiceListener() {
415        public void onServiceConnected(int profile, BluetoothProfile proxy) {
416            mBluetoothHeadset = (BluetoothHeadset) proxy;
417            synchronized (mStats) {
418                if (mBluetoothPendingStats) {
419                    mStats.noteBluetoothOnLocked();
420                    mStats.setBtHeadset(mBluetoothHeadset);
421                    mBluetoothPendingStats = false;
422                }
423            }
424        }
425
426        public void onServiceDisconnected(int profile) {
427            mBluetoothHeadset = null;
428        }
429    };
430
431    public void noteBluetoothOff() {
432        enforceCallingPermission();
433        synchronized (mStats) {
434            mBluetoothPendingStats = false;
435            mStats.noteBluetoothOffLocked();
436        }
437    }
438
439    public void noteBluetoothState(int bluetoothState) {
440        enforceCallingPermission();
441        synchronized (mStats) {
442            mStats.noteBluetoothStateLocked(bluetoothState);
443        }
444    }
445
446    public void noteFullWifiLockAcquired(int uid) {
447        enforceCallingPermission();
448        synchronized (mStats) {
449            mStats.noteFullWifiLockAcquiredLocked(uid);
450        }
451    }
452
453    public void noteFullWifiLockReleased(int uid) {
454        enforceCallingPermission();
455        synchronized (mStats) {
456            mStats.noteFullWifiLockReleasedLocked(uid);
457        }
458    }
459
460    public void noteWifiScanStarted(int uid) {
461        enforceCallingPermission();
462        synchronized (mStats) {
463            mStats.noteWifiScanStartedLocked(uid);
464        }
465    }
466
467    public void noteWifiScanStopped(int uid) {
468        enforceCallingPermission();
469        synchronized (mStats) {
470            mStats.noteWifiScanStoppedLocked(uid);
471        }
472    }
473
474    public void noteWifiMulticastEnabled(int uid) {
475        enforceCallingPermission();
476        synchronized (mStats) {
477            mStats.noteWifiMulticastEnabledLocked(uid);
478        }
479    }
480
481    public void noteWifiMulticastDisabled(int uid) {
482        enforceCallingPermission();
483        synchronized (mStats) {
484            mStats.noteWifiMulticastDisabledLocked(uid);
485        }
486    }
487
488    public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
489        enforceCallingPermission();
490        synchronized (mStats) {
491            mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
492        }
493    }
494
495    public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
496        enforceCallingPermission();
497        synchronized (mStats) {
498            mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
499        }
500    }
501
502    public void noteWifiScanStartedFromSource(WorkSource ws) {
503        enforceCallingPermission();
504        synchronized (mStats) {
505            mStats.noteWifiScanStartedFromSourceLocked(ws);
506        }
507    }
508
509    public void noteWifiScanStoppedFromSource(WorkSource ws) {
510        enforceCallingPermission();
511        synchronized (mStats) {
512            mStats.noteWifiScanStoppedFromSourceLocked(ws);
513        }
514    }
515
516    public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
517        enforceCallingPermission();
518        synchronized (mStats) {
519            mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
520        }
521    }
522
523    public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
524        enforceCallingPermission();
525        synchronized (mStats) {
526            mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
527        }
528    }
529
530    public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
531        enforceCallingPermission();
532        synchronized (mStats) {
533            mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
534        }
535    }
536
537    public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
538        enforceCallingPermission();
539        synchronized (mStats) {
540            mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
541        }
542    }
543
544    @Override
545    public void noteNetworkInterfaceType(String iface, int type) {
546        enforceCallingPermission();
547        synchronized (mStats) {
548            mStats.noteNetworkInterfaceTypeLocked(iface, type);
549        }
550    }
551
552    @Override
553    public void noteNetworkStatsEnabled() {
554        enforceCallingPermission();
555        synchronized (mStats) {
556            mStats.noteNetworkStatsEnabledLocked();
557        }
558    }
559
560    public boolean isOnBattery() {
561        return mStats.isOnBattery();
562    }
563
564    public void setBatteryState(int status, int health, int plugType, int level,
565            int temp, int volt) {
566        enforceCallingPermission();
567        mStats.setBatteryState(status, health, plugType, level, temp, volt);
568    }
569
570    public long getAwakeTimeBattery() {
571        mContext.enforceCallingOrSelfPermission(
572                android.Manifest.permission.BATTERY_STATS, null);
573        return mStats.getAwakeTimeBattery();
574    }
575
576    public long getAwakeTimePlugged() {
577        mContext.enforceCallingOrSelfPermission(
578                android.Manifest.permission.BATTERY_STATS, null);
579        return mStats.getAwakeTimePlugged();
580    }
581
582    public void enforceCallingPermission() {
583        if (Binder.getCallingPid() == Process.myPid()) {
584            return;
585        }
586        mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
587                Binder.getCallingPid(), Binder.getCallingUid(), null);
588    }
589
590    final class WakeupReasonThread extends Thread {
591        final int[] mIrqs = new int[32];
592        final String[] mReasons = new String[32];
593
594        WakeupReasonThread() {
595            super("BatteryStats_wakeupReason");
596        }
597
598        public void run() {
599            Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
600
601            try {
602                int num;
603                while ((num=nativeWaitWakeup(mIrqs, mReasons)) >= 0) {
604                    synchronized (mStats) {
605                        if (num > 0) {
606                            for (int i=0; i<num; i++) {
607                                mStats.noteWakeupReasonLocked(mReasons[i]);
608                            }
609                        } else {
610                            mStats.noteWakeupReasonLocked("unknown");
611                        }
612                    }
613                }
614            } catch (RuntimeException e) {
615                Slog.e(TAG, "Failure reading wakeup reasons", e);
616            }
617        }
618    }
619
620    private static native int nativeWaitWakeup(int[] outIrqs, String[] outReasons);
621
622    private void dumpHelp(PrintWriter pw) {
623        pw.println("Battery stats (batterystats) dump options:");
624        pw.println("  [--checkin] [--history] [--history-start] [--unplugged] [--charged] [-c]");
625        pw.println("  [--reset] [--write] [-h] [<package.name>]");
626        pw.println("  --checkin: format output for a checkin report.");
627        pw.println("  --history: show only history data.");
628        pw.println("  --history-start <num>: show only history data starting at given time offset.");
629        pw.println("  --unplugged: only output data since last unplugged.");
630        pw.println("  --charged: only output data since last charged.");
631        pw.println("  --reset: reset the stats, clearing all current data.");
632        pw.println("  --write: force write current collected stats to disk.");
633        pw.println("  <package.name>: optional name of package to filter output by.");
634        pw.println("  -h: print this help text.");
635        pw.println("Battery stats (batterystats) commands:");
636        pw.println("  enable|disable <option>");
637        pw.println("    Enable or disable a running option.  Option state is not saved across boots.");
638        pw.println("    Options are:");
639        pw.println("      full-wake-history: include wake_lock_in battery history, full wake details.");
640        pw.println("      no-auto-reset: don't automatically reset stats when unplugged");
641    }
642
643    private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) {
644        i++;
645        if (i >= args.length) {
646            pw.println("Missing option argument for " + (enable ? "--enable" : "--disable"));
647            dumpHelp(pw);
648            return -1;
649        }
650        if ("full-wake-history".equals(args[i])) {
651            synchronized (mStats) {
652                mStats.setRecordAllWakeLocksLocked(enable);
653            }
654        } else if ("no-auto-reset".equals(args[i])) {
655            synchronized (mStats) {
656                mStats.setNoAutoReset(enable);
657            }
658        } else {
659            pw.println("Unknown enable/disable option: " + args[i]);
660            dumpHelp(pw);
661            return -1;
662        }
663        return i;
664    }
665
666    @Override
667    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
668        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
669                != PackageManager.PERMISSION_GRANTED) {
670            pw.println("Permission Denial: can't dump BatteryStats from from pid="
671                    + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
672                    + " without permission " + android.Manifest.permission.DUMP);
673            return;
674        }
675
676        int flags = 0;
677        boolean isCheckin = false;
678        boolean noOutput = false;
679        boolean writeData = false;
680        long historyStart = -1;
681        int reqUid = -1;
682        if (args != null) {
683            for (int i=0; i<args.length; i++) {
684                String arg = args[i];
685                if ("--checkin".equals(arg)) {
686                    isCheckin = true;
687                } else if ("--history".equals(arg)) {
688                    flags |= BatteryStats.DUMP_HISTORY_ONLY;
689                } else if ("--history-start".equals(arg)) {
690                    flags |= BatteryStats.DUMP_HISTORY_ONLY;
691                    i++;
692                    if (i >= args.length) {
693                        pw.println("Missing time argument for --history-since");
694                        dumpHelp(pw);
695                        return;
696                    }
697                    historyStart = Long.parseLong(args[i]);
698                    writeData = true;
699                } else if ("-c".equals(arg)) {
700                    isCheckin = true;
701                    flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
702                } else if ("--unplugged".equals(arg)) {
703                    flags |= BatteryStats.DUMP_UNPLUGGED_ONLY;
704                } else if ("--charged".equals(arg)) {
705                    flags |= BatteryStats.DUMP_CHARGED_ONLY;
706                } else if ("--reset".equals(arg)) {
707                    synchronized (mStats) {
708                        mStats.resetAllStatsCmdLocked();
709                        pw.println("Battery stats reset.");
710                        noOutput = true;
711                    }
712                } else if ("--write".equals(arg)) {
713                    synchronized (mStats) {
714                        mStats.writeSyncLocked();
715                        pw.println("Battery stats written.");
716                        noOutput = true;
717                    }
718                } else if ("--enable".equals(arg) || "enable".equals(arg)) {
719                    i = doEnableOrDisable(pw, i, args, true);
720                    if (i < 0) {
721                        return;
722                    }
723                    pw.println("Enabled: " + args[i]);
724                    return;
725                } else if ("--disable".equals(arg) || "disable".equals(arg)) {
726                    i = doEnableOrDisable(pw, i, args, false);
727                    if (i < 0) {
728                        return;
729                    }
730                    pw.println("Disabled: " + args[i]);
731                    return;
732                } else if ("-h".equals(arg)) {
733                    dumpHelp(pw);
734                    return;
735                } else if ("-a".equals(arg)) {
736                    flags |= BatteryStats.DUMP_VERBOSE;
737                } else if (arg.length() > 0 && arg.charAt(0) == '-'){
738                    pw.println("Unknown option: " + arg);
739                    dumpHelp(pw);
740                    return;
741                } else {
742                    // Not an option, last argument must be a package name.
743                    try {
744                        reqUid = mContext.getPackageManager().getPackageUid(arg,
745                                UserHandle.getCallingUserId());
746                    } catch (PackageManager.NameNotFoundException e) {
747                        pw.println("Unknown package: " + arg);
748                        dumpHelp(pw);
749                        return;
750                    }
751                }
752            }
753        }
754        if (noOutput) {
755            return;
756        }
757        if (isCheckin) {
758            List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
759            synchronized (mStats) {
760                mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
761                if (writeData) {
762                    mStats.writeAsyncLocked();
763                }
764            }
765        } else {
766            synchronized (mStats) {
767                mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
768                if (writeData) {
769                    mStats.writeAsyncLocked();
770                }
771            }
772        }
773    }
774}
775