blob: ff06513fb858d1ea98d02933a333888b6b8ff46d [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;
Jeff Brown6f357d32014-01-15 20:40:55 -080027import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.IBinder;
29import android.os.Parcel;
30import android.os.Process;
31import android.os.ServiceManager;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070032import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070033import android.os.WorkSource;
Wink Savillee9b06d72009-05-18 21:47:50 -070034import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080035import android.telephony.TelephonyManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080036import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Amith Yamasani32dbefd2009-06-19 09:21:17 -070038import com.android.internal.app.IBatteryStats;
39import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070040import com.android.internal.os.PowerProfile;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.io.FileDescriptor;
43import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080044import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46/**
47 * All information we are collecting about things that can happen that impact
48 * battery life.
49 */
50public final class BatteryStatsService extends IBatteryStats.Stub {
51 static IBatteryStats sService;
52
53 final BatteryStatsImpl mStats;
54 Context mContext;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070055 private boolean mBluetoothPendingStats;
56 private BluetoothHeadset mBluetoothHeadset;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070057
Jeff Brown6f357d32014-01-15 20:40:55 -080058 BatteryStatsService(String filename, Handler handler) {
59 mStats = new BatteryStatsImpl(filename, handler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 }
61
62 public void publish(Context context) {
63 mContext = context;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070064 ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
Amith Yamasanie43530a2009-08-21 13:11:37 -070065 mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
Amith Yamasanif37447b2009-10-08 18:28:01 -070066 mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
67 com.android.internal.R.integer.config_radioScanningTimeout)
68 * 1000L);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
70
Dianne Hackborn55280a92009-05-07 15:53:46 -070071 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080072 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Dianne Hackborn55280a92009-05-07 15:53:46 -070073 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070074 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -070075 }
76 }
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public static IBatteryStats getService() {
79 if (sService != null) {
80 return sService;
81 }
Dianne Hackborn91268cf2013-06-13 19:06:50 -070082 IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 sService = asInterface(b);
84 return sService;
85 }
86
87 /**
88 * @return the current statistics object, which may be modified
89 * to reflect events that affect battery usage. You must lock the
90 * stats object before doing anything with it.
91 */
92 public BatteryStatsImpl getActiveStatistics() {
93 return mStats;
94 }
95
96 public byte[] getStatistics() {
97 mContext.enforceCallingPermission(
98 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080099 //Slog.i("foo", "SENDING BATTERY INFO:");
100 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 Parcel out = Parcel.obtain();
102 mStats.writeToParcel(out, 0);
103 byte[] data = out.marshall();
104 out.recycle();
105 return data;
106 }
107
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700108 public void noteStartWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 enforceCallingPermission();
110 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700111 mStats.noteStartWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 }
113 }
114
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700115 public void noteStopWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 enforceCallingPermission();
117 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700118 mStats.noteStopWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
120 }
121
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700122 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, int type) {
123 enforceCallingPermission();
124 synchronized (mStats) {
125 mStats.noteStartWakeFromSourceLocked(ws, pid, name, type);
126 }
127 }
128
129 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, int type) {
130 enforceCallingPermission();
131 synchronized (mStats) {
132 mStats.noteStopWakeFromSourceLocked(ws, pid, name, type);
133 }
134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public void noteStartSensor(int uid, int sensor) {
137 enforceCallingPermission();
138 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700139 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
141 }
142
143 public void noteStopSensor(int uid, int sensor) {
144 enforceCallingPermission();
145 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700146 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
148 }
149
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800150 public void noteVibratorOn(int uid, long durationMillis) {
151 enforceCallingPermission();
152 synchronized (mStats) {
153 mStats.noteVibratorOnLocked(uid, durationMillis);
154 }
155 }
156
157 public void noteVibratorOff(int uid) {
158 enforceCallingPermission();
159 synchronized (mStats) {
160 mStats.noteVibratorOffLocked(uid);
161 }
162 }
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public void noteStartGps(int uid) {
165 enforceCallingPermission();
166 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700167 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
169 }
170
171 public void noteStopGps(int uid) {
172 enforceCallingPermission();
173 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700174 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
176 }
177
178 public void noteScreenOn() {
179 enforceCallingPermission();
180 synchronized (mStats) {
181 mStats.noteScreenOnLocked();
182 }
183 }
184
Dianne Hackborn617f8772009-03-31 15:04:46 -0700185 public void noteScreenBrightness(int brightness) {
186 enforceCallingPermission();
187 synchronized (mStats) {
188 mStats.noteScreenBrightnessLocked(brightness);
189 }
190 }
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 public void noteScreenOff() {
193 enforceCallingPermission();
194 synchronized (mStats) {
195 mStats.noteScreenOffLocked();
196 }
197 }
198
Dianne Hackborn617f8772009-03-31 15:04:46 -0700199 public void noteInputEvent() {
200 enforceCallingPermission();
Christopher Tate4cee7252010-03-19 14:50:40 -0700201 mStats.noteInputEventAtomic();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700202 }
203
204 public void noteUserActivity(int uid, int event) {
205 enforceCallingPermission();
206 synchronized (mStats) {
207 mStats.noteUserActivityLocked(uid, event);
208 }
209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public void notePhoneOn() {
212 enforceCallingPermission();
213 synchronized (mStats) {
214 mStats.notePhoneOnLocked();
215 }
216 }
217
218 public void notePhoneOff() {
219 enforceCallingPermission();
220 synchronized (mStats) {
221 mStats.notePhoneOffLocked();
222 }
223 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700224
Wink Savillee9b06d72009-05-18 21:47:50 -0700225 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700226 enforceCallingPermission();
227 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700228 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700229 }
230 }
231
232 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
233 enforceCallingPermission();
234 synchronized (mStats) {
235 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
236 }
237 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700238
Amith Yamasanif37447b2009-10-08 18:28:01 -0700239 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700240 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800241 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700242 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800243 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700244 }
245 }
246
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700247 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700248 enforceCallingPermission();
249 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700250 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700251 }
252 }
253
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700254 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700255 enforceCallingPermission();
256 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700257 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700258 }
259 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700260
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700261 public void noteStartAudio(int uid) {
262 enforceCallingPermission();
263 synchronized (mStats) {
264 mStats.noteAudioOnLocked(uid);
265 }
266 }
267
268 public void noteStopAudio(int uid) {
269 enforceCallingPermission();
270 synchronized (mStats) {
271 mStats.noteAudioOffLocked(uid);
272 }
273 }
274
275 public void noteStartVideo(int uid) {
276 enforceCallingPermission();
277 synchronized (mStats) {
278 mStats.noteVideoOnLocked(uid);
279 }
280 }
281
282 public void noteStopVideo(int uid) {
283 enforceCallingPermission();
284 synchronized (mStats) {
285 mStats.noteVideoOffLocked(uid);
286 }
287 }
288
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700289 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700290 enforceCallingPermission();
291 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700292 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700293 }
294 }
295
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700296 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700297 enforceCallingPermission();
298 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700299 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
300 }
301 }
302
303 public void noteWifiStopped(WorkSource ws) {
304 enforceCallingPermission();
305 synchronized (mStats) {
306 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700307 }
308 }
309
The Android Open Source Project10592532009-03-18 17:39:46 -0700310 public void noteBluetoothOn() {
311 enforceCallingPermission();
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700312 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
313 if (adapter != null) {
314 adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
315 BluetoothProfile.HEADSET);
316 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700317 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700318 if (mBluetoothHeadset != null) {
319 mStats.noteBluetoothOnLocked();
320 mStats.setBtHeadset(mBluetoothHeadset);
321 } else {
322 mBluetoothPendingStats = true;
323 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700324 }
325 }
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700326
327 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
328 new BluetoothProfile.ServiceListener() {
329 public void onServiceConnected(int profile, BluetoothProfile proxy) {
330 mBluetoothHeadset = (BluetoothHeadset) proxy;
331 synchronized (mStats) {
332 if (mBluetoothPendingStats) {
333 mStats.noteBluetoothOnLocked();
334 mStats.setBtHeadset(mBluetoothHeadset);
335 mBluetoothPendingStats = false;
336 }
337 }
338 }
339
340 public void onServiceDisconnected(int profile) {
341 mBluetoothHeadset = null;
342 }
343 };
344
The Android Open Source Project10592532009-03-18 17:39:46 -0700345 public void noteBluetoothOff() {
346 enforceCallingPermission();
347 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700348 mBluetoothPendingStats = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700349 mStats.noteBluetoothOffLocked();
350 }
351 }
352
353 public void noteFullWifiLockAcquired(int uid) {
354 enforceCallingPermission();
355 synchronized (mStats) {
356 mStats.noteFullWifiLockAcquiredLocked(uid);
357 }
358 }
359
360 public void noteFullWifiLockReleased(int uid) {
361 enforceCallingPermission();
362 synchronized (mStats) {
363 mStats.noteFullWifiLockReleasedLocked(uid);
364 }
365 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700366
367 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 enforceCallingPermission();
369 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700370 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700371 }
372 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700373
374 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700375 enforceCallingPermission();
376 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700377 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700378 }
379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700381 public void noteWifiMulticastEnabled(int uid) {
382 enforceCallingPermission();
383 synchronized (mStats) {
384 mStats.noteWifiMulticastEnabledLocked(uid);
385 }
386 }
387
388 public void noteWifiMulticastDisabled(int uid) {
389 enforceCallingPermission();
390 synchronized (mStats) {
391 mStats.noteWifiMulticastDisabledLocked(uid);
392 }
393 }
394
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700395 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
396 enforceCallingPermission();
397 synchronized (mStats) {
398 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
399 }
400 }
401
402 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
403 enforceCallingPermission();
404 synchronized (mStats) {
405 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
406 }
407 }
408
Nick Pelly6ccaa542012-06-15 15:22:47 -0700409 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700410 enforceCallingPermission();
411 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700412 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700413 }
414 }
415
Nick Pelly6ccaa542012-06-15 15:22:47 -0700416 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700417 enforceCallingPermission();
418 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700419 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700420 }
421 }
422
Robert Greenwalta029ea12013-09-25 16:38:12 -0700423 public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
424 enforceCallingPermission();
425 synchronized (mStats) {
426 mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
427 }
428 }
429
430 public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
431 enforceCallingPermission();
432 synchronized (mStats) {
433 mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
434 }
435 }
436
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700437 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
438 enforceCallingPermission();
439 synchronized (mStats) {
440 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
441 }
442 }
443
444 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
445 enforceCallingPermission();
446 synchronized (mStats) {
447 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
448 }
449 }
450
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700451 @Override
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700452 public void noteNetworkInterfaceType(String iface, int type) {
453 enforceCallingPermission();
454 synchronized (mStats) {
455 mStats.noteNetworkInterfaceTypeLocked(iface, type);
456 }
457 }
458
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700459 @Override
460 public void noteNetworkStatsEnabled() {
461 enforceCallingPermission();
462 synchronized (mStats) {
463 mStats.noteNetworkStatsEnabledLocked();
464 }
465 }
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public boolean isOnBattery() {
468 return mStats.isOnBattery();
469 }
470
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700471 public void setBatteryState(int status, int health, int plugType, int level,
472 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 enforceCallingPermission();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700474 mStats.setBatteryState(status, health, plugType, level, temp, volt);
Evan Millar633a1742009-04-02 16:36:33 -0700475 }
476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 public long getAwakeTimeBattery() {
478 mContext.enforceCallingOrSelfPermission(
479 android.Manifest.permission.BATTERY_STATS, null);
480 return mStats.getAwakeTimeBattery();
481 }
482
483 public long getAwakeTimePlugged() {
484 mContext.enforceCallingOrSelfPermission(
485 android.Manifest.permission.BATTERY_STATS, null);
486 return mStats.getAwakeTimePlugged();
487 }
488
489 public void enforceCallingPermission() {
490 if (Binder.getCallingPid() == Process.myPid()) {
491 return;
492 }
493 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
494 Binder.getCallingPid(), Binder.getCallingUid(), null);
495 }
496
Dianne Hackbornae384452011-06-28 12:33:48 -0700497 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700498 pw.println("Battery stats (batterystats) dump options:");
Dianne Hackborn49021f52013-09-04 18:03:40 -0700499 pw.println(" [--checkin] [-c] [--unplugged] [--reset] [--write] [-h] [<package.name>]");
Dianne Hackbornae384452011-06-28 12:33:48 -0700500 pw.println(" --checkin: format output for a checkin report.");
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700501 pw.println(" --unplugged: only output data since last unplugged.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700502 pw.println(" --reset: reset the stats, clearing all current data.");
503 pw.println(" --write: force write current collected stats to disk.");
504 pw.println(" -h: print this help text.");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700505 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700506 }
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 @Override
509 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -0700510 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
511 != PackageManager.PERMISSION_GRANTED) {
512 pw.println("Permission Denial: can't dump BatteryStats from from pid="
513 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
514 + " without permission " + android.Manifest.permission.DUMP);
515 return;
516 }
517
Dianne Hackborne4a59512010-12-07 11:08:07 -0800518 boolean isCheckin = false;
Dianne Hackborn49021f52013-09-04 18:03:40 -0700519 boolean includeHistory = false;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700520 boolean isUnpluggedOnly = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700521 boolean noOutput = false;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700522 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800523 if (args != null) {
524 for (String arg : args) {
525 if ("--checkin".equals(arg)) {
526 isCheckin = true;
Dianne Hackborn49021f52013-09-04 18:03:40 -0700527 } else if ("-c".equals(arg)) {
528 isCheckin = true;
529 includeHistory = true;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700530 } else if ("--unplugged".equals(arg)) {
531 isUnpluggedOnly = true;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800532 } else if ("--reset".equals(arg)) {
533 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700534 mStats.resetAllStatsLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800535 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700536 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700538 } else if ("--write".equals(arg)) {
539 synchronized (mStats) {
540 mStats.writeSyncLocked();
541 pw.println("Battery stats written.");
542 noOutput = true;
543 }
Dianne Hackbornae384452011-06-28 12:33:48 -0700544 } else if ("-h".equals(arg)) {
545 dumpHelp(pw);
546 return;
Mike Lockwoode8174042011-08-16 12:53:43 -0700547 } else if ("-a".equals(arg)) {
548 // fall through
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700549 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700550 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -0700551 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700552 return;
553 } else {
554 // Not an option, last argument must be a package name.
555 try {
556 reqUid = mContext.getPackageManager().getPackageUid(arg,
557 UserHandle.getCallingUserId());
558 } catch (PackageManager.NameNotFoundException e) {
559 pw.println("Unknown package: " + arg);
560 dumpHelp(pw);
561 return;
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800565 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700566 if (noOutput) {
567 return;
568 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800569 if (isCheckin) {
570 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
571 synchronized (mStats) {
Dianne Hackborn49021f52013-09-04 18:03:40 -0700572 mStats.dumpCheckinLocked(pw, apps, isUnpluggedOnly, includeHistory);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800573 }
574 } else {
575 synchronized (mStats) {
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700576 mStats.dumpLocked(pw, isUnpluggedOnly, reqUid);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
580}