blob: 8a29ac75570dcf5b12bc8f6c2082fe76f4053d39 [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 Hackborn099bc622014-01-22 13:39:16 -0800108 public void addIsolatedUid(int isolatedUid, int appUid) {
109 enforceCallingPermission();
110 synchronized (mStats) {
111 mStats.addIsolatedUidLocked(isolatedUid, appUid);
112 }
113 }
114
115 public void removeIsolatedUid(int isolatedUid, int appUid) {
116 enforceCallingPermission();
117 synchronized (mStats) {
118 mStats.removeIsolatedUidLocked(isolatedUid, appUid);
119 }
120 }
121
122 public void noteEvent(int code, String name, int uid) {
123 enforceCallingPermission();
124 synchronized (mStats) {
125 mStats.noteEventLocked(code, name, uid);
126 }
127 }
128
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800129 public void noteStartWakelock(int uid, int pid, String name, int type,
130 boolean unimportantForLogging) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 enforceCallingPermission();
132 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800133 mStats.noteStartWakeLocked(uid, pid, name, type, unimportantForLogging);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135 }
136
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700137 public void noteStopWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 enforceCallingPermission();
139 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700140 mStats.noteStopWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142 }
143
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800144 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, int type,
145 boolean unimportantForLogging) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700146 enforceCallingPermission();
147 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800148 mStats.noteStartWakeFromSourceLocked(ws, pid, name, type, unimportantForLogging);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700149 }
150 }
151
152 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, int type) {
153 enforceCallingPermission();
154 synchronized (mStats) {
155 mStats.noteStopWakeFromSourceLocked(ws, pid, name, type);
156 }
157 }
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public void noteStartSensor(int uid, int sensor) {
160 enforceCallingPermission();
161 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700162 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
164 }
165
166 public void noteStopSensor(int uid, int sensor) {
167 enforceCallingPermission();
168 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700169 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171 }
172
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800173 public void noteVibratorOn(int uid, long durationMillis) {
174 enforceCallingPermission();
175 synchronized (mStats) {
176 mStats.noteVibratorOnLocked(uid, durationMillis);
177 }
178 }
179
180 public void noteVibratorOff(int uid) {
181 enforceCallingPermission();
182 synchronized (mStats) {
183 mStats.noteVibratorOffLocked(uid);
184 }
185 }
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public void noteStartGps(int uid) {
188 enforceCallingPermission();
189 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700190 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192 }
193
194 public void noteStopGps(int uid) {
195 enforceCallingPermission();
196 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700197 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199 }
200
201 public void noteScreenOn() {
202 enforceCallingPermission();
203 synchronized (mStats) {
204 mStats.noteScreenOnLocked();
205 }
206 }
207
Dianne Hackborn617f8772009-03-31 15:04:46 -0700208 public void noteScreenBrightness(int brightness) {
209 enforceCallingPermission();
210 synchronized (mStats) {
211 mStats.noteScreenBrightnessLocked(brightness);
212 }
213 }
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public void noteScreenOff() {
216 enforceCallingPermission();
217 synchronized (mStats) {
218 mStats.noteScreenOffLocked();
219 }
220 }
221
Dianne Hackborn617f8772009-03-31 15:04:46 -0700222 public void noteInputEvent() {
223 enforceCallingPermission();
Christopher Tate4cee7252010-03-19 14:50:40 -0700224 mStats.noteInputEventAtomic();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700225 }
226
227 public void noteUserActivity(int uid, int event) {
228 enforceCallingPermission();
229 synchronized (mStats) {
230 mStats.noteUserActivityLocked(uid, event);
231 }
232 }
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public void notePhoneOn() {
235 enforceCallingPermission();
236 synchronized (mStats) {
237 mStats.notePhoneOnLocked();
238 }
239 }
240
241 public void notePhoneOff() {
242 enforceCallingPermission();
243 synchronized (mStats) {
244 mStats.notePhoneOffLocked();
245 }
246 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700247
Wink Savillee9b06d72009-05-18 21:47:50 -0700248 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700249 enforceCallingPermission();
250 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700251 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700252 }
253 }
254
255 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
256 enforceCallingPermission();
257 synchronized (mStats) {
258 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
259 }
260 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700261
Amith Yamasanif37447b2009-10-08 18:28:01 -0700262 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700263 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800264 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700265 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800266 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700267 }
268 }
269
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700270 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700271 enforceCallingPermission();
272 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700273 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700274 }
275 }
276
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700277 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700278 enforceCallingPermission();
279 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700280 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700281 }
282 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700283
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700284 public void noteStartAudio(int uid) {
285 enforceCallingPermission();
286 synchronized (mStats) {
287 mStats.noteAudioOnLocked(uid);
288 }
289 }
290
291 public void noteStopAudio(int uid) {
292 enforceCallingPermission();
293 synchronized (mStats) {
294 mStats.noteAudioOffLocked(uid);
295 }
296 }
297
298 public void noteStartVideo(int uid) {
299 enforceCallingPermission();
300 synchronized (mStats) {
301 mStats.noteVideoOnLocked(uid);
302 }
303 }
304
305 public void noteStopVideo(int uid) {
306 enforceCallingPermission();
307 synchronized (mStats) {
308 mStats.noteVideoOffLocked(uid);
309 }
310 }
311
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700312 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700313 enforceCallingPermission();
314 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700315 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700316 }
317 }
318
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700319 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700320 enforceCallingPermission();
321 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700322 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
323 }
324 }
325
326 public void noteWifiStopped(WorkSource ws) {
327 enforceCallingPermission();
328 synchronized (mStats) {
329 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700330 }
331 }
332
The Android Open Source Project10592532009-03-18 17:39:46 -0700333 public void noteBluetoothOn() {
334 enforceCallingPermission();
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700335 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
336 if (adapter != null) {
337 adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
338 BluetoothProfile.HEADSET);
339 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700340 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700341 if (mBluetoothHeadset != null) {
342 mStats.noteBluetoothOnLocked();
343 mStats.setBtHeadset(mBluetoothHeadset);
344 } else {
345 mBluetoothPendingStats = true;
346 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700347 }
348 }
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700349
350 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
351 new BluetoothProfile.ServiceListener() {
352 public void onServiceConnected(int profile, BluetoothProfile proxy) {
353 mBluetoothHeadset = (BluetoothHeadset) proxy;
354 synchronized (mStats) {
355 if (mBluetoothPendingStats) {
356 mStats.noteBluetoothOnLocked();
357 mStats.setBtHeadset(mBluetoothHeadset);
358 mBluetoothPendingStats = false;
359 }
360 }
361 }
362
363 public void onServiceDisconnected(int profile) {
364 mBluetoothHeadset = null;
365 }
366 };
367
The Android Open Source Project10592532009-03-18 17:39:46 -0700368 public void noteBluetoothOff() {
369 enforceCallingPermission();
370 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700371 mBluetoothPendingStats = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700372 mStats.noteBluetoothOffLocked();
373 }
374 }
375
376 public void noteFullWifiLockAcquired(int uid) {
377 enforceCallingPermission();
378 synchronized (mStats) {
379 mStats.noteFullWifiLockAcquiredLocked(uid);
380 }
381 }
382
383 public void noteFullWifiLockReleased(int uid) {
384 enforceCallingPermission();
385 synchronized (mStats) {
386 mStats.noteFullWifiLockReleasedLocked(uid);
387 }
388 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700389
390 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700391 enforceCallingPermission();
392 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700393 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700394 }
395 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700396
397 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700398 enforceCallingPermission();
399 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700400 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700401 }
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700404 public void noteWifiMulticastEnabled(int uid) {
405 enforceCallingPermission();
406 synchronized (mStats) {
407 mStats.noteWifiMulticastEnabledLocked(uid);
408 }
409 }
410
411 public void noteWifiMulticastDisabled(int uid) {
412 enforceCallingPermission();
413 synchronized (mStats) {
414 mStats.noteWifiMulticastDisabledLocked(uid);
415 }
416 }
417
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700418 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
419 enforceCallingPermission();
420 synchronized (mStats) {
421 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
422 }
423 }
424
425 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
426 enforceCallingPermission();
427 synchronized (mStats) {
428 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
429 }
430 }
431
Nick Pelly6ccaa542012-06-15 15:22:47 -0700432 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700433 enforceCallingPermission();
434 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700435 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700436 }
437 }
438
Nick Pelly6ccaa542012-06-15 15:22:47 -0700439 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700440 enforceCallingPermission();
441 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700442 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700443 }
444 }
445
Robert Greenwalta029ea12013-09-25 16:38:12 -0700446 public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
447 enforceCallingPermission();
448 synchronized (mStats) {
449 mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
450 }
451 }
452
453 public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
454 enforceCallingPermission();
455 synchronized (mStats) {
456 mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
457 }
458 }
459
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700460 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
461 enforceCallingPermission();
462 synchronized (mStats) {
463 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
464 }
465 }
466
467 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
468 enforceCallingPermission();
469 synchronized (mStats) {
470 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
471 }
472 }
473
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700474 @Override
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700475 public void noteNetworkInterfaceType(String iface, int type) {
476 enforceCallingPermission();
477 synchronized (mStats) {
478 mStats.noteNetworkInterfaceTypeLocked(iface, type);
479 }
480 }
481
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700482 @Override
483 public void noteNetworkStatsEnabled() {
484 enforceCallingPermission();
485 synchronized (mStats) {
486 mStats.noteNetworkStatsEnabledLocked();
487 }
488 }
489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public boolean isOnBattery() {
491 return mStats.isOnBattery();
492 }
493
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700494 public void setBatteryState(int status, int health, int plugType, int level,
495 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 enforceCallingPermission();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700497 mStats.setBatteryState(status, health, plugType, level, temp, volt);
Evan Millar633a1742009-04-02 16:36:33 -0700498 }
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public long getAwakeTimeBattery() {
501 mContext.enforceCallingOrSelfPermission(
502 android.Manifest.permission.BATTERY_STATS, null);
503 return mStats.getAwakeTimeBattery();
504 }
505
506 public long getAwakeTimePlugged() {
507 mContext.enforceCallingOrSelfPermission(
508 android.Manifest.permission.BATTERY_STATS, null);
509 return mStats.getAwakeTimePlugged();
510 }
511
512 public void enforceCallingPermission() {
513 if (Binder.getCallingPid() == Process.myPid()) {
514 return;
515 }
516 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
517 Binder.getCallingPid(), Binder.getCallingUid(), null);
518 }
519
Dianne Hackbornae384452011-06-28 12:33:48 -0700520 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700521 pw.println("Battery stats (batterystats) dump options:");
Dianne Hackborn099bc622014-01-22 13:39:16 -0800522 pw.println(" [--checkin] [--history] [-c] [--unplugged] [--reset] [--write]");
523 pw.println(" [-h] [<package.name>]");
Dianne Hackbornae384452011-06-28 12:33:48 -0700524 pw.println(" --checkin: format output for a checkin report.");
Dianne Hackborn099bc622014-01-22 13:39:16 -0800525 pw.println(" --history: show only history data.");
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700526 pw.println(" --unplugged: only output data since last unplugged.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700527 pw.println(" --reset: reset the stats, clearing all current data.");
528 pw.println(" --write: force write current collected stats to disk.");
529 pw.println(" -h: print this help text.");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700530 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700531 }
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 @Override
534 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -0700535 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
536 != PackageManager.PERMISSION_GRANTED) {
537 pw.println("Permission Denial: can't dump BatteryStats from from pid="
538 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
539 + " without permission " + android.Manifest.permission.DUMP);
540 return;
541 }
542
Dianne Hackborne4a59512010-12-07 11:08:07 -0800543 boolean isCheckin = false;
Dianne Hackborn49021f52013-09-04 18:03:40 -0700544 boolean includeHistory = false;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800545 boolean historyOnly = false;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700546 boolean isUnpluggedOnly = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700547 boolean noOutput = false;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700548 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800549 if (args != null) {
550 for (String arg : args) {
551 if ("--checkin".equals(arg)) {
552 isCheckin = true;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800553 } else if ("--history".equals(arg)) {
554 historyOnly = true;
Dianne Hackborn49021f52013-09-04 18:03:40 -0700555 } else if ("-c".equals(arg)) {
556 isCheckin = true;
557 includeHistory = true;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700558 } else if ("--unplugged".equals(arg)) {
559 isUnpluggedOnly = true;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800560 } else if ("--reset".equals(arg)) {
561 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800562 mStats.resetAllStatsCmdLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800563 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700564 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700566 } else if ("--write".equals(arg)) {
567 synchronized (mStats) {
568 mStats.writeSyncLocked();
569 pw.println("Battery stats written.");
570 noOutput = true;
571 }
Dianne Hackbornae384452011-06-28 12:33:48 -0700572 } else if ("-h".equals(arg)) {
573 dumpHelp(pw);
574 return;
Mike Lockwoode8174042011-08-16 12:53:43 -0700575 } else if ("-a".equals(arg)) {
576 // fall through
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700577 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700578 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -0700579 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700580 return;
581 } else {
582 // Not an option, last argument must be a package name.
583 try {
584 reqUid = mContext.getPackageManager().getPackageUid(arg,
585 UserHandle.getCallingUserId());
586 } catch (PackageManager.NameNotFoundException e) {
587 pw.println("Unknown package: " + arg);
588 dumpHelp(pw);
589 return;
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800593 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700594 if (noOutput) {
595 return;
596 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800597 if (isCheckin) {
598 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
599 synchronized (mStats) {
Dianne Hackborn099bc622014-01-22 13:39:16 -0800600 mStats.dumpCheckinLocked(mContext, pw, apps, isUnpluggedOnly, includeHistory,
601 historyOnly);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800602 }
603 } else {
604 synchronized (mStats) {
Dianne Hackborn099bc622014-01-22 13:39:16 -0800605 mStats.dumpLocked(mContext, pw, isUnpluggedOnly, reqUid, historyOnly);
Dianne Hackborne4a59512010-12-07 11:08:07 -0800606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608 }
609}