blob: 12cad7be19a497a4d58581b2848717770d9a7f83 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070019import android.bluetooth.BluetoothAdapter;
Amith Yamasani14fb81a2009-11-05 11:41:58 -080020import android.bluetooth.BluetoothHeadset;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070021import android.bluetooth.BluetoothProfile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080023import android.content.pm.ApplicationInfo;
Kenny Root3abd75b2011-09-29 11:00:41 -070024import android.content.pm.PackageManager;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070025import android.os.BatteryStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.Binder;
27import android.os.IBinder;
28import android.os.Parcel;
29import android.os.Process;
30import android.os.ServiceManager;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070031import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070032import android.os.WorkSource;
Wink Savillee9b06d72009-05-18 21:47:50 -070033import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080034import android.telephony.TelephonyManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Amith Yamasani32dbefd2009-06-19 09:21:17 -070037import com.android.internal.app.IBatteryStats;
38import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070039import com.android.internal.os.PowerProfile;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.FileDescriptor;
42import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080043import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * All information we are collecting about things that can happen that impact
47 * battery life.
48 */
49public final class BatteryStatsService extends IBatteryStats.Stub {
50 static IBatteryStats sService;
51
52 final BatteryStatsImpl mStats;
53 Context mContext;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070054 private boolean mBluetoothPendingStats;
55 private BluetoothHeadset mBluetoothHeadset;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 BatteryStatsService(String filename) {
58 mStats = new BatteryStatsImpl(filename);
59 }
60
61 public void publish(Context context) {
62 mContext = context;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070063 ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
Amith Yamasanie43530a2009-08-21 13:11:37 -070064 mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
Amith Yamasanif37447b2009-10-08 18:28:01 -070065 mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
66 com.android.internal.R.integer.config_radioScanningTimeout)
67 * 1000L);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
69
Dianne Hackborn55280a92009-05-07 15:53:46 -070070 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080071 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Dianne Hackborn55280a92009-05-07 15:53:46 -070072 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070073 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -070074 }
75 }
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static IBatteryStats getService() {
78 if (sService != null) {
79 return sService;
80 }
Dianne Hackborn91268cf2013-06-13 19:06:50 -070081 IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 sService = asInterface(b);
83 return sService;
84 }
85
86 /**
87 * @return the current statistics object, which may be modified
88 * to reflect events that affect battery usage. You must lock the
89 * stats object before doing anything with it.
90 */
91 public BatteryStatsImpl getActiveStatistics() {
92 return mStats;
93 }
94
95 public byte[] getStatistics() {
96 mContext.enforceCallingPermission(
97 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080098 //Slog.i("foo", "SENDING BATTERY INFO:");
99 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 Parcel out = Parcel.obtain();
101 mStats.writeToParcel(out, 0);
102 byte[] data = out.marshall();
103 out.recycle();
104 return data;
105 }
106
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700107 public void noteStartWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 enforceCallingPermission();
109 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700110 mStats.noteStartWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
112 }
113
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700114 public void noteStopWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 enforceCallingPermission();
116 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700117 mStats.noteStopWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
119 }
120
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700121 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, int type) {
122 enforceCallingPermission();
123 synchronized (mStats) {
124 mStats.noteStartWakeFromSourceLocked(ws, pid, name, type);
125 }
126 }
127
128 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, int type) {
129 enforceCallingPermission();
130 synchronized (mStats) {
131 mStats.noteStopWakeFromSourceLocked(ws, pid, name, type);
132 }
133 }
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public void noteStartSensor(int uid, int sensor) {
136 enforceCallingPermission();
137 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700138 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
140 }
141
142 public void noteStopSensor(int uid, int sensor) {
143 enforceCallingPermission();
144 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700145 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147 }
148
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800149 public void noteVibratorOn(int uid, long durationMillis) {
150 enforceCallingPermission();
151 synchronized (mStats) {
152 mStats.noteVibratorOnLocked(uid, durationMillis);
153 }
154 }
155
156 public void noteVibratorOff(int uid) {
157 enforceCallingPermission();
158 synchronized (mStats) {
159 mStats.noteVibratorOffLocked(uid);
160 }
161 }
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public void noteStartGps(int uid) {
164 enforceCallingPermission();
165 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700166 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168 }
169
170 public void noteStopGps(int uid) {
171 enforceCallingPermission();
172 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700173 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175 }
176
177 public void noteScreenOn() {
178 enforceCallingPermission();
179 synchronized (mStats) {
180 mStats.noteScreenOnLocked();
181 }
182 }
183
Dianne Hackborn617f8772009-03-31 15:04:46 -0700184 public void noteScreenBrightness(int brightness) {
185 enforceCallingPermission();
186 synchronized (mStats) {
187 mStats.noteScreenBrightnessLocked(brightness);
188 }
189 }
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public void noteScreenOff() {
192 enforceCallingPermission();
193 synchronized (mStats) {
194 mStats.noteScreenOffLocked();
195 }
196 }
197
Dianne Hackborn617f8772009-03-31 15:04:46 -0700198 public void noteInputEvent() {
199 enforceCallingPermission();
Christopher Tate4cee7252010-03-19 14:50:40 -0700200 mStats.noteInputEventAtomic();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700201 }
202
203 public void noteUserActivity(int uid, int event) {
204 enforceCallingPermission();
205 synchronized (mStats) {
206 mStats.noteUserActivityLocked(uid, event);
207 }
208 }
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 public void notePhoneOn() {
211 enforceCallingPermission();
212 synchronized (mStats) {
213 mStats.notePhoneOnLocked();
214 }
215 }
216
217 public void notePhoneOff() {
218 enforceCallingPermission();
219 synchronized (mStats) {
220 mStats.notePhoneOffLocked();
221 }
222 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700223
Wink Savillee9b06d72009-05-18 21:47:50 -0700224 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700225 enforceCallingPermission();
226 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700227 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700228 }
229 }
230
231 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
232 enforceCallingPermission();
233 synchronized (mStats) {
234 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
235 }
236 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700237
Amith Yamasanif37447b2009-10-08 18:28:01 -0700238 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700239 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800240 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700241 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800242 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700243 }
244 }
245
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700246 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700247 enforceCallingPermission();
248 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700249 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700250 }
251 }
252
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700253 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700254 enforceCallingPermission();
255 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700256 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700257 }
258 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700259
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700260 public void noteStartAudio(int uid) {
261 enforceCallingPermission();
262 synchronized (mStats) {
263 mStats.noteAudioOnLocked(uid);
264 }
265 }
266
267 public void noteStopAudio(int uid) {
268 enforceCallingPermission();
269 synchronized (mStats) {
270 mStats.noteAudioOffLocked(uid);
271 }
272 }
273
274 public void noteStartVideo(int uid) {
275 enforceCallingPermission();
276 synchronized (mStats) {
277 mStats.noteVideoOnLocked(uid);
278 }
279 }
280
281 public void noteStopVideo(int uid) {
282 enforceCallingPermission();
283 synchronized (mStats) {
284 mStats.noteVideoOffLocked(uid);
285 }
286 }
287
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700288 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700289 enforceCallingPermission();
290 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700291 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700292 }
293 }
294
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700295 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700296 enforceCallingPermission();
297 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700298 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
299 }
300 }
301
302 public void noteWifiStopped(WorkSource ws) {
303 enforceCallingPermission();
304 synchronized (mStats) {
305 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700306 }
307 }
308
The Android Open Source Project10592532009-03-18 17:39:46 -0700309 public void noteBluetoothOn() {
310 enforceCallingPermission();
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700311 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
312 if (adapter != null) {
313 adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
314 BluetoothProfile.HEADSET);
315 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700316 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700317 if (mBluetoothHeadset != null) {
318 mStats.noteBluetoothOnLocked();
319 mStats.setBtHeadset(mBluetoothHeadset);
320 } else {
321 mBluetoothPendingStats = true;
322 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700323 }
324 }
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700325
326 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
327 new BluetoothProfile.ServiceListener() {
328 public void onServiceConnected(int profile, BluetoothProfile proxy) {
329 mBluetoothHeadset = (BluetoothHeadset) proxy;
330 synchronized (mStats) {
331 if (mBluetoothPendingStats) {
332 mStats.noteBluetoothOnLocked();
333 mStats.setBtHeadset(mBluetoothHeadset);
334 mBluetoothPendingStats = false;
335 }
336 }
337 }
338
339 public void onServiceDisconnected(int profile) {
340 mBluetoothHeadset = null;
341 }
342 };
343
The Android Open Source Project10592532009-03-18 17:39:46 -0700344 public void noteBluetoothOff() {
345 enforceCallingPermission();
346 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700347 mBluetoothPendingStats = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700348 mStats.noteBluetoothOffLocked();
349 }
350 }
351
352 public void noteFullWifiLockAcquired(int uid) {
353 enforceCallingPermission();
354 synchronized (mStats) {
355 mStats.noteFullWifiLockAcquiredLocked(uid);
356 }
357 }
358
359 public void noteFullWifiLockReleased(int uid) {
360 enforceCallingPermission();
361 synchronized (mStats) {
362 mStats.noteFullWifiLockReleasedLocked(uid);
363 }
364 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700365
366 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700367 enforceCallingPermission();
368 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700369 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700370 }
371 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700372
373 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700374 enforceCallingPermission();
375 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700376 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 }
378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700380 public void noteWifiMulticastEnabled(int uid) {
381 enforceCallingPermission();
382 synchronized (mStats) {
383 mStats.noteWifiMulticastEnabledLocked(uid);
384 }
385 }
386
387 public void noteWifiMulticastDisabled(int uid) {
388 enforceCallingPermission();
389 synchronized (mStats) {
390 mStats.noteWifiMulticastDisabledLocked(uid);
391 }
392 }
393
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700394 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
395 enforceCallingPermission();
396 synchronized (mStats) {
397 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
398 }
399 }
400
401 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
402 enforceCallingPermission();
403 synchronized (mStats) {
404 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
405 }
406 }
407
Nick Pelly6ccaa542012-06-15 15:22:47 -0700408 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700409 enforceCallingPermission();
410 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700411 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700412 }
413 }
414
Nick Pelly6ccaa542012-06-15 15:22:47 -0700415 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700416 enforceCallingPermission();
417 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700418 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700419 }
420 }
421
422 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
423 enforceCallingPermission();
424 synchronized (mStats) {
425 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
426 }
427 }
428
429 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
430 enforceCallingPermission();
431 synchronized (mStats) {
432 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
433 }
434 }
435
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700436 @Override
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700437 public void noteNetworkInterfaceType(String iface, int type) {
438 enforceCallingPermission();
439 synchronized (mStats) {
440 mStats.noteNetworkInterfaceTypeLocked(iface, type);
441 }
442 }
443
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700444 @Override
445 public void noteNetworkStatsEnabled() {
446 enforceCallingPermission();
447 synchronized (mStats) {
448 mStats.noteNetworkStatsEnabledLocked();
449 }
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 public boolean isOnBattery() {
453 return mStats.isOnBattery();
454 }
455
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700456 public void setBatteryState(int status, int health, int plugType, int level,
457 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 enforceCallingPermission();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700459 mStats.setBatteryState(status, health, plugType, level, temp, volt);
Evan Millar633a1742009-04-02 16:36:33 -0700460 }
461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public long getAwakeTimeBattery() {
463 mContext.enforceCallingOrSelfPermission(
464 android.Manifest.permission.BATTERY_STATS, null);
465 return mStats.getAwakeTimeBattery();
466 }
467
468 public long getAwakeTimePlugged() {
469 mContext.enforceCallingOrSelfPermission(
470 android.Manifest.permission.BATTERY_STATS, null);
471 return mStats.getAwakeTimePlugged();
472 }
473
474 public void enforceCallingPermission() {
475 if (Binder.getCallingPid() == Process.myPid()) {
476 return;
477 }
478 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
479 Binder.getCallingPid(), Binder.getCallingUid(), null);
480 }
481
Dianne Hackbornae384452011-06-28 12:33:48 -0700482 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700483 pw.println("Battery stats (batterystats) dump options:");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700484 pw.println(" [--checkin] [--unplugged] [--reset] [--write] [-h] [<package.name>]");
Dianne Hackbornae384452011-06-28 12:33:48 -0700485 pw.println(" --checkin: format output for a checkin report.");
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700486 pw.println(" --unplugged: only output data since last unplugged.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700487 pw.println(" --reset: reset the stats, clearing all current data.");
488 pw.println(" --write: force write current collected stats to disk.");
489 pw.println(" -h: print this help text.");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700490 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700491 }
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 @Override
494 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -0700495 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
496 != PackageManager.PERMISSION_GRANTED) {
497 pw.println("Permission Denial: can't dump BatteryStats from from pid="
498 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
499 + " without permission " + android.Manifest.permission.DUMP);
500 return;
501 }
502
Dianne Hackborne4a59512010-12-07 11:08:07 -0800503 boolean isCheckin = false;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700504 boolean isUnpluggedOnly = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700505 boolean noOutput = false;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700506 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800507 if (args != null) {
508 for (String arg : args) {
509 if ("--checkin".equals(arg)) {
510 isCheckin = true;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700511 } else if ("--unplugged".equals(arg)) {
512 isUnpluggedOnly = true;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800513 } else if ("--reset".equals(arg)) {
514 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700515 mStats.resetAllStatsLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800516 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700517 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700519 } else if ("--write".equals(arg)) {
520 synchronized (mStats) {
521 mStats.writeSyncLocked();
522 pw.println("Battery stats written.");
523 noOutput = true;
524 }
Dianne Hackbornae384452011-06-28 12:33:48 -0700525 } else if ("-h".equals(arg)) {
526 dumpHelp(pw);
527 return;
Mike Lockwoode8174042011-08-16 12:53:43 -0700528 } else if ("-a".equals(arg)) {
529 // fall through
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700530 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700531 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -0700532 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700533 return;
534 } else {
535 // Not an option, last argument must be a package name.
536 try {
537 reqUid = mContext.getPackageManager().getPackageUid(arg,
538 UserHandle.getCallingUserId());
539 } catch (PackageManager.NameNotFoundException e) {
540 pw.println("Unknown package: " + arg);
541 dumpHelp(pw);
542 return;
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800546 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700547 if (noOutput) {
548 return;
549 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800550 if (isCheckin) {
551 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
552 synchronized (mStats) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700553 mStats.dumpCheckinLocked(pw, apps, isUnpluggedOnly);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800554 }
555 } else {
556 synchronized (mStats) {
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700557 mStats.dumpLocked(pw, isUnpluggedOnly, reqUid);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561}