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