blob: 786196ff05a745dbb141162f8615919a5e9b9fa6 [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;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070030import android.os.ParcelFileDescriptor;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070031import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Process;
33import android.os.ServiceManager;
Dianne Hackborne5167ca2014-03-08 14:39:10 -080034import android.os.SystemClock;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070035import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070036import android.os.WorkSource;
Wink Savillee9b06d72009-05-18 21:47:50 -070037import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080038import android.telephony.TelephonyManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080039import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Amith Yamasani32dbefd2009-06-19 09:21:17 -070041import com.android.internal.app.IBatteryStats;
Dianne Hackbornd953c532014-08-16 18:17:38 -070042import com.android.internal.os.BatteryStatsHelper;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070043import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070044import com.android.internal.os.PowerProfile;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070045import com.android.server.LocalServices;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070046
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070047import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.FileDescriptor;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070049import java.io.FileOutputStream;
50import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080052import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54/**
55 * All information we are collecting about things that can happen that impact
56 * battery life.
57 */
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070058public final class BatteryStatsService extends IBatteryStats.Stub
59 implements PowerManagerInternal.LowPowerModeListener {
Dianne Hackbornc51cf032014-03-02 19:08:15 -080060 static final String TAG = "BatteryStatsService";
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 static IBatteryStats sService;
63
64 final BatteryStatsImpl mStats;
65 Context mContext;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070066 private boolean mBluetoothPendingStats;
67 private BluetoothHeadset mBluetoothHeadset;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070068 PowerManagerInternal mPowerManagerInternal;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070069
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070070 BatteryStatsService(File systemDir, Handler handler) {
71 mStats = new BatteryStatsImpl(systemDir, handler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
73
74 public void publish(Context context) {
75 mContext = context;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070076 ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
Amith Yamasanie43530a2009-08-21 13:11:37 -070077 mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
Amith Yamasanif37447b2009-10-08 18:28:01 -070078 mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
79 com.android.internal.R.integer.config_radioScanningTimeout)
80 * 1000L);
Jeff Brown2c43c332014-06-12 22:38:59 -070081 }
82
83 /**
84 * At the time when the constructor runs, the power manager has not yet been
85 * initialized. So we initialize the low power observer later.
86 */
87 public void initPowerManagement() {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070088 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
89 mPowerManagerInternal.registerLowPowerModeObserver(this);
90 mStats.noteLowPowerMode(mPowerManagerInternal.getLowPowerModeEnabled());
Dianne Hackbornc51cf032014-03-02 19:08:15 -080091 (new WakeupReasonThread()).start();
Jeff Brown2c43c332014-06-12 22:38:59 -070092 }
93
Dianne Hackborn55280a92009-05-07 15:53:46 -070094 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080095 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Dianne Hackborn55280a92009-05-07 15:53:46 -070096 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070097 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -070098 }
99 }
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public static IBatteryStats getService() {
102 if (sService != null) {
103 return sService;
104 }
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700105 IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 sService = asInterface(b);
107 return sService;
108 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700109
110 @Override
111 public void onLowPowerModeChanged(boolean enabled) {
112 synchronized (mStats) {
113 mStats.noteLowPowerMode(enabled);
114 }
115 }
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
118 * @return the current statistics object, which may be modified
119 * to reflect events that affect battery usage. You must lock the
120 * stats object before doing anything with it.
121 */
122 public BatteryStatsImpl getActiveStatistics() {
123 return mStats;
124 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public byte[] getStatistics() {
127 mContext.enforceCallingPermission(
128 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800129 //Slog.i("foo", "SENDING BATTERY INFO:");
130 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 Parcel out = Parcel.obtain();
132 mStats.writeToParcel(out, 0);
133 byte[] data = out.marshall();
134 out.recycle();
135 return data;
136 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700137
138 public ParcelFileDescriptor getStatisticsStream() {
139 mContext.enforceCallingPermission(
140 android.Manifest.permission.BATTERY_STATS, null);
141 //Slog.i("foo", "SENDING BATTERY INFO:");
142 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
143 Parcel out = Parcel.obtain();
144 mStats.writeToParcel(out, 0);
145 byte[] data = out.marshall();
146 out.recycle();
147 try {
148 return ParcelFileDescriptor.fromData(data, "battery-stats");
149 } catch (IOException e) {
150 Slog.w(TAG, "Unable to create shared memory", e);
151 return null;
152 }
153 }
154
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700155 public long computeBatteryTimeRemaining() {
156 synchronized (mStats) {
157 long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
158 return time >= 0 ? (time/1000) : time;
159 }
160 }
161
162 public long computeChargeTimeRemaining() {
163 synchronized (mStats) {
164 long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime());
165 return time >= 0 ? (time/1000) : time;
166 }
167 }
168
Dianne Hackborn099bc622014-01-22 13:39:16 -0800169 public void addIsolatedUid(int isolatedUid, int appUid) {
170 enforceCallingPermission();
171 synchronized (mStats) {
172 mStats.addIsolatedUidLocked(isolatedUid, appUid);
173 }
174 }
175
176 public void removeIsolatedUid(int isolatedUid, int appUid) {
177 enforceCallingPermission();
178 synchronized (mStats) {
179 mStats.removeIsolatedUidLocked(isolatedUid, appUid);
180 }
181 }
182
183 public void noteEvent(int code, String name, int uid) {
184 enforceCallingPermission();
185 synchronized (mStats) {
186 mStats.noteEventLocked(code, name, uid);
187 }
188 }
189
Dianne Hackborn61659e52014-07-09 16:13:01 -0700190 public void noteProcessStart(String name, int uid) {
191 enforceCallingPermission();
192 synchronized (mStats) {
193 mStats.noteProcessStartLocked(name, uid);
194 }
195 }
196
197 public void noteProcessState(String name, int uid, int state) {
198 enforceCallingPermission();
199 synchronized (mStats) {
200 mStats.noteProcessStateLocked(name, uid, state);
201 }
202 }
203
204 public void noteProcessFinish(String name, int uid) {
205 enforceCallingPermission();
206 synchronized (mStats) {
207 mStats.noteProcessFinishLocked(name, uid);
208 }
209 }
210
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700211 public void noteSyncStart(String name, int uid) {
212 enforceCallingPermission();
213 synchronized (mStats) {
214 mStats.noteSyncStartLocked(name, uid);
215 }
216 }
217
218 public void noteSyncFinish(String name, int uid) {
219 enforceCallingPermission();
220 synchronized (mStats) {
221 mStats.noteSyncFinishLocked(name, uid);
222 }
223 }
224
225 public void noteJobStart(String name, int uid) {
226 enforceCallingPermission();
227 synchronized (mStats) {
228 mStats.noteJobStartLocked(name, uid);
229 }
230 }
231
232 public void noteJobFinish(String name, int uid) {
233 enforceCallingPermission();
234 synchronized (mStats) {
235 mStats.noteJobFinishLocked(name, uid);
236 }
237 }
238
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800239 public void noteStartWakelock(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800240 boolean unimportantForLogging) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 enforceCallingPermission();
242 synchronized (mStats) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800243 mStats.noteStartWakeLocked(uid, pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -0700244 SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
247
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700248 public void noteStopWakelock(int uid, int pid, String name, String historyName, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 enforceCallingPermission();
250 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700251 mStats.noteStopWakeLocked(uid, pid, name, historyName, type,
252 SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 }
254 }
255
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800256 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name,
257 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700258 enforceCallingPermission();
259 synchronized (mStats) {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800260 mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName,
261 type, unimportantForLogging);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700262 }
263 }
264
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700265 public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name,
266 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800267 String newHistoryName, int newType, boolean newUnimportantForLogging) {
268 enforceCallingPermission();
269 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700270 mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800271 newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging);
272 }
273 }
274
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700275 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName,
276 int type) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700277 enforceCallingPermission();
278 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700279 mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700280 }
281 }
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 public void noteStartSensor(int uid, int sensor) {
284 enforceCallingPermission();
285 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700286 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 }
289
290 public void noteStopSensor(int uid, int sensor) {
291 enforceCallingPermission();
292 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700293 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295 }
296
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800297 public void noteVibratorOn(int uid, long durationMillis) {
298 enforceCallingPermission();
299 synchronized (mStats) {
300 mStats.noteVibratorOnLocked(uid, durationMillis);
301 }
302 }
303
304 public void noteVibratorOff(int uid) {
305 enforceCallingPermission();
306 synchronized (mStats) {
307 mStats.noteVibratorOffLocked(uid);
308 }
309 }
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public void noteStartGps(int uid) {
312 enforceCallingPermission();
313 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700314 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 }
317
318 public void noteStopGps(int uid) {
319 enforceCallingPermission();
320 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700321 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
323 }
324
Jeff Browne95c3cd2014-05-02 16:59:26 -0700325 public void noteScreenState(int state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 enforceCallingPermission();
327 synchronized (mStats) {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700328 mStats.noteScreenStateLocked(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330 }
331
Dianne Hackborn617f8772009-03-31 15:04:46 -0700332 public void noteScreenBrightness(int brightness) {
333 enforceCallingPermission();
334 synchronized (mStats) {
335 mStats.noteScreenBrightnessLocked(brightness);
336 }
337 }
338
Dianne Hackborn617f8772009-03-31 15:04:46 -0700339 public void noteUserActivity(int uid, int event) {
340 enforceCallingPermission();
341 synchronized (mStats) {
342 mStats.noteUserActivityLocked(uid, event);
343 }
344 }
Jeff Browne95c3cd2014-05-02 16:59:26 -0700345
346 public void noteInteractive(boolean interactive) {
347 enforceCallingPermission();
348 synchronized (mStats) {
349 mStats.noteInteractiveLocked(interactive);
350 }
351 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800352
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700353 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800354 enforceCallingPermission();
355 synchronized (mStats) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700356 mStats.noteMobileRadioPowerState(powerState, timestampNs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800357 }
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 public void notePhoneOn() {
361 enforceCallingPermission();
362 synchronized (mStats) {
363 mStats.notePhoneOnLocked();
364 }
365 }
366
367 public void notePhoneOff() {
368 enforceCallingPermission();
369 synchronized (mStats) {
370 mStats.notePhoneOffLocked();
371 }
372 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700373
Wink Savillee9b06d72009-05-18 21:47:50 -0700374 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700375 enforceCallingPermission();
376 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700377 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700378 }
379 }
380
381 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
382 enforceCallingPermission();
383 synchronized (mStats) {
384 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
385 }
386 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700387
Amith Yamasanif37447b2009-10-08 18:28:01 -0700388 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700389 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800390 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700391 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800392 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700393 }
394 }
395
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700396 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700397 enforceCallingPermission();
398 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700399 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700400 }
401 }
402
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700403 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700404 enforceCallingPermission();
405 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700406 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700407 }
408 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700409
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700410 public void noteStartAudio(int uid) {
411 enforceCallingPermission();
412 synchronized (mStats) {
413 mStats.noteAudioOnLocked(uid);
414 }
415 }
416
417 public void noteStopAudio(int uid) {
418 enforceCallingPermission();
419 synchronized (mStats) {
420 mStats.noteAudioOffLocked(uid);
421 }
422 }
423
424 public void noteStartVideo(int uid) {
425 enforceCallingPermission();
426 synchronized (mStats) {
427 mStats.noteVideoOnLocked(uid);
428 }
429 }
430
431 public void noteStopVideo(int uid) {
432 enforceCallingPermission();
433 synchronized (mStats) {
434 mStats.noteVideoOffLocked(uid);
435 }
436 }
437
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700438 public void noteResetAudio() {
439 enforceCallingPermission();
440 synchronized (mStats) {
441 mStats.noteResetAudioLocked();
442 }
443 }
444
445 public void noteResetVideo() {
446 enforceCallingPermission();
447 synchronized (mStats) {
448 mStats.noteResetVideoLocked();
449 }
450 }
451
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700452 public void noteFlashlightOn() {
453 enforceCallingPermission();
454 synchronized (mStats) {
455 mStats.noteFlashlightOnLocked();
456 }
457 }
458
459 public void noteFlashlightOff() {
460 enforceCallingPermission();
461 synchronized (mStats) {
462 mStats.noteFlashlightOffLocked();
463 }
464 }
465
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700466 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700467 enforceCallingPermission();
468 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700469 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700470 }
471 }
472
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700473 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700474 enforceCallingPermission();
475 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700476 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
477 }
478 }
479
480 public void noteWifiStopped(WorkSource ws) {
481 enforceCallingPermission();
482 synchronized (mStats) {
483 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700484 }
485 }
486
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800487 public void noteWifiState(int wifiState, String accessPoint) {
488 enforceCallingPermission();
489 synchronized (mStats) {
490 mStats.noteWifiStateLocked(wifiState, accessPoint);
491 }
492 }
493
Dianne Hackborn3251b902014-06-20 14:40:53 -0700494 public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) {
495 enforceCallingPermission();
496 synchronized (mStats) {
497 mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth);
498 }
499 }
500
501 public void noteWifiRssiChanged(int newRssi) {
502 enforceCallingPermission();
503 synchronized (mStats) {
504 mStats.noteWifiRssiChangedLocked(newRssi);
505 }
506 }
507
The Android Open Source Project10592532009-03-18 17:39:46 -0700508 public void noteBluetoothOn() {
509 enforceCallingPermission();
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700510 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
511 if (adapter != null) {
512 adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
513 BluetoothProfile.HEADSET);
514 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700515 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700516 if (mBluetoothHeadset != null) {
517 mStats.noteBluetoothOnLocked();
518 mStats.setBtHeadset(mBluetoothHeadset);
519 } else {
520 mBluetoothPendingStats = true;
521 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700522 }
523 }
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700524
525 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
526 new BluetoothProfile.ServiceListener() {
527 public void onServiceConnected(int profile, BluetoothProfile proxy) {
528 mBluetoothHeadset = (BluetoothHeadset) proxy;
529 synchronized (mStats) {
530 if (mBluetoothPendingStats) {
531 mStats.noteBluetoothOnLocked();
532 mStats.setBtHeadset(mBluetoothHeadset);
533 mBluetoothPendingStats = false;
534 }
535 }
536 }
537
538 public void onServiceDisconnected(int profile) {
539 mBluetoothHeadset = null;
540 }
541 };
542
The Android Open Source Project10592532009-03-18 17:39:46 -0700543 public void noteBluetoothOff() {
544 enforceCallingPermission();
545 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700546 mBluetoothPendingStats = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700547 mStats.noteBluetoothOffLocked();
548 }
549 }
550
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800551 public void noteBluetoothState(int bluetoothState) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800552 enforceCallingPermission();
553 synchronized (mStats) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800554 mStats.noteBluetoothStateLocked(bluetoothState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800555 }
556 }
557
The Android Open Source Project10592532009-03-18 17:39:46 -0700558 public void noteFullWifiLockAcquired(int uid) {
559 enforceCallingPermission();
560 synchronized (mStats) {
561 mStats.noteFullWifiLockAcquiredLocked(uid);
562 }
563 }
564
565 public void noteFullWifiLockReleased(int uid) {
566 enforceCallingPermission();
567 synchronized (mStats) {
568 mStats.noteFullWifiLockReleasedLocked(uid);
569 }
570 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700571
572 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700573 enforceCallingPermission();
574 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700575 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700576 }
577 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700578
579 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700580 enforceCallingPermission();
581 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700582 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700583 }
584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700586 public void noteWifiMulticastEnabled(int uid) {
587 enforceCallingPermission();
588 synchronized (mStats) {
589 mStats.noteWifiMulticastEnabledLocked(uid);
590 }
591 }
592
593 public void noteWifiMulticastDisabled(int uid) {
594 enforceCallingPermission();
595 synchronized (mStats) {
596 mStats.noteWifiMulticastDisabledLocked(uid);
597 }
598 }
599
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700600 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
601 enforceCallingPermission();
602 synchronized (mStats) {
603 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
604 }
605 }
606
607 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
608 enforceCallingPermission();
609 synchronized (mStats) {
610 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
611 }
612 }
613
Nick Pelly6ccaa542012-06-15 15:22:47 -0700614 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700615 enforceCallingPermission();
616 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700617 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700618 }
619 }
620
Nick Pelly6ccaa542012-06-15 15:22:47 -0700621 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700622 enforceCallingPermission();
623 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700624 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700625 }
626 }
627
Robert Greenwalta029ea12013-09-25 16:38:12 -0700628 public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
629 enforceCallingPermission();
630 synchronized (mStats) {
631 mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
632 }
633 }
634
635 public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
636 enforceCallingPermission();
637 synchronized (mStats) {
638 mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
639 }
640 }
641
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700642 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
643 enforceCallingPermission();
644 synchronized (mStats) {
645 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
646 }
647 }
648
649 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
650 enforceCallingPermission();
651 synchronized (mStats) {
652 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
653 }
654 }
655
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700656 @Override
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700657 public void noteNetworkInterfaceType(String iface, int type) {
658 enforceCallingPermission();
659 synchronized (mStats) {
660 mStats.noteNetworkInterfaceTypeLocked(iface, type);
661 }
662 }
663
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700664 @Override
665 public void noteNetworkStatsEnabled() {
666 enforceCallingPermission();
667 synchronized (mStats) {
668 mStats.noteNetworkStatsEnabledLocked();
669 }
670 }
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 public boolean isOnBattery() {
673 return mStats.isOnBattery();
674 }
675
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700676 public void setBatteryState(int status, int health, int plugType, int level,
677 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 enforceCallingPermission();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700679 mStats.setBatteryState(status, health, plugType, level, temp, volt);
Evan Millar633a1742009-04-02 16:36:33 -0700680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public long getAwakeTimeBattery() {
683 mContext.enforceCallingOrSelfPermission(
684 android.Manifest.permission.BATTERY_STATS, null);
685 return mStats.getAwakeTimeBattery();
686 }
687
688 public long getAwakeTimePlugged() {
689 mContext.enforceCallingOrSelfPermission(
690 android.Manifest.permission.BATTERY_STATS, null);
691 return mStats.getAwakeTimePlugged();
692 }
693
694 public void enforceCallingPermission() {
695 if (Binder.getCallingPid() == Process.myPid()) {
696 return;
697 }
698 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
699 Binder.getCallingPid(), Binder.getCallingUid(), null);
700 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800701
702 final class WakeupReasonThread extends Thread {
703 final int[] mIrqs = new int[32];
704 final String[] mReasons = new String[32];
705
706 WakeupReasonThread() {
707 super("BatteryStats_wakeupReason");
708 }
709
710 public void run() {
711 Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
712
713 try {
714 int num;
715 while ((num=nativeWaitWakeup(mIrqs, mReasons)) >= 0) {
716 synchronized (mStats) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700717 if (num > 0) {
718 for (int i=0; i<num; i++) {
719 mStats.noteWakeupReasonLocked(mReasons[i]);
720 }
721 } else {
722 mStats.noteWakeupReasonLocked("unknown");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800723 }
724 }
725 }
726 } catch (RuntimeException e) {
727 Slog.e(TAG, "Failure reading wakeup reasons", e);
728 }
729 }
730 }
731
732 private static native int nativeWaitWakeup(int[] outIrqs, String[] outReasons);
733
Dianne Hackbornae384452011-06-28 12:33:48 -0700734 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700735 pw.println("Battery stats (batterystats) dump options:");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800736 pw.println(" [--checkin] [--history] [--history-start] [--unplugged] [--charged] [-c]");
737 pw.println(" [--reset] [--write] [-h] [<package.name>]");
Dianne Hackbornae384452011-06-28 12:33:48 -0700738 pw.println(" --checkin: format output for a checkin report.");
Dianne Hackborn099bc622014-01-22 13:39:16 -0800739 pw.println(" --history: show only history data.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800740 pw.println(" --history-start <num>: show only history data starting at given time offset.");
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700741 pw.println(" --unplugged: only output data since last unplugged.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800742 pw.println(" --charged: only output data since last charged.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700743 pw.println(" --reset: reset the stats, clearing all current data.");
744 pw.println(" --write: force write current collected stats to disk.");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700745 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornfc064132014-06-02 12:42:12 -0700746 pw.println(" -h: print this help text.");
747 pw.println("Battery stats (batterystats) commands:");
748 pw.println(" enable|disable <option>");
749 pw.println(" Enable or disable a running option. Option state is not saved across boots.");
750 pw.println(" Options are:");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700751 pw.println(" full-history: include additional detailed events in battery history:");
752 pw.println(" wake_lock_in and proc events");
Dianne Hackbornfc064132014-06-02 12:42:12 -0700753 pw.println(" no-auto-reset: don't automatically reset stats when unplugged");
Dianne Hackbornae384452011-06-28 12:33:48 -0700754 }
755
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700756 private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) {
757 i++;
758 if (i >= args.length) {
759 pw.println("Missing option argument for " + (enable ? "--enable" : "--disable"));
760 dumpHelp(pw);
761 return -1;
762 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700763 if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700764 synchronized (mStats) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700765 mStats.setRecordAllHistoryLocked(enable);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700766 }
Dianne Hackborn9a755432014-05-15 17:05:22 -0700767 } else if ("no-auto-reset".equals(args[i])) {
768 synchronized (mStats) {
769 mStats.setNoAutoReset(enable);
770 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700771 } else {
772 pw.println("Unknown enable/disable option: " + args[i]);
773 dumpHelp(pw);
774 return -1;
775 }
776 return i;
777 }
778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 @Override
780 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -0700781 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
782 != PackageManager.PERMISSION_GRANTED) {
783 pw.println("Permission Denial: can't dump BatteryStats from from pid="
784 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
785 + " without permission " + android.Manifest.permission.DUMP);
786 return;
787 }
788
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800789 int flags = 0;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700790 boolean useCheckinFormat = false;
791 boolean isRealCheckin = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700792 boolean noOutput = false;
Dianne Hackborn16b0b562014-06-03 17:24:42 -0700793 boolean writeData = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800794 long historyStart = -1;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700795 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800796 if (args != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800797 for (int i=0; i<args.length; i++) {
798 String arg = args[i];
Dianne Hackborne4a59512010-12-07 11:08:07 -0800799 if ("--checkin".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700800 useCheckinFormat = true;
801 isRealCheckin = true;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800802 } else if ("--history".equals(arg)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800803 flags |= BatteryStats.DUMP_HISTORY_ONLY;
804 } else if ("--history-start".equals(arg)) {
805 flags |= BatteryStats.DUMP_HISTORY_ONLY;
806 i++;
807 if (i >= args.length) {
808 pw.println("Missing time argument for --history-since");
809 dumpHelp(pw);
810 return;
811 }
812 historyStart = Long.parseLong(args[i]);
Dianne Hackborn16b0b562014-06-03 17:24:42 -0700813 writeData = true;
Dianne Hackborn49021f52013-09-04 18:03:40 -0700814 } else if ("-c".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700815 useCheckinFormat = true;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800816 flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -0700817 } else if ("--unplugged".equals(arg)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800818 flags |= BatteryStats.DUMP_UNPLUGGED_ONLY;
819 } else if ("--charged".equals(arg)) {
820 flags |= BatteryStats.DUMP_CHARGED_ONLY;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800821 } else if ("--reset".equals(arg)) {
822 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800823 mStats.resetAllStatsCmdLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800824 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700825 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700827 } else if ("--write".equals(arg)) {
828 synchronized (mStats) {
829 mStats.writeSyncLocked();
830 pw.println("Battery stats written.");
831 noOutput = true;
832 }
Dianne Hackbornfc064132014-06-02 12:42:12 -0700833 } else if ("--enable".equals(arg) || "enable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700834 i = doEnableOrDisable(pw, i, args, true);
835 if (i < 0) {
836 return;
837 }
838 pw.println("Enabled: " + args[i]);
839 return;
Dianne Hackbornfc064132014-06-02 12:42:12 -0700840 } else if ("--disable".equals(arg) || "disable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700841 i = doEnableOrDisable(pw, i, args, false);
842 if (i < 0) {
843 return;
844 }
845 pw.println("Disabled: " + args[i]);
846 return;
Dianne Hackbornae384452011-06-28 12:33:48 -0700847 } else if ("-h".equals(arg)) {
848 dumpHelp(pw);
849 return;
Mike Lockwoode8174042011-08-16 12:53:43 -0700850 } else if ("-a".equals(arg)) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700851 flags |= BatteryStats.DUMP_VERBOSE;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700852 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700853 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -0700854 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700855 return;
856 } else {
857 // Not an option, last argument must be a package name.
858 try {
859 reqUid = mContext.getPackageManager().getPackageUid(arg,
860 UserHandle.getCallingUserId());
861 } catch (PackageManager.NameNotFoundException e) {
862 pw.println("Unknown package: " + arg);
863 dumpHelp(pw);
864 return;
865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800868 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700869 if (noOutput) {
870 return;
871 }
Dianne Hackbornd953c532014-08-16 18:17:38 -0700872 if (BatteryStatsHelper.checkWifiOnly(mContext)) {
873 flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
874 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700875 if (useCheckinFormat) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800876 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700877 if (isRealCheckin) {
878 // For a real checkin, first we want to prefer to use the last complete checkin
879 // file if there is one.
880 synchronized (mStats.mCheckinFile) {
881 if (mStats.mCheckinFile.exists()) {
882 try {
883 byte[] raw = mStats.mCheckinFile.readFully();
884 if (raw != null) {
885 Parcel in = Parcel.obtain();
886 in.unmarshall(raw, 0, raw.length);
887 in.setDataPosition(0);
888 BatteryStatsImpl checkinStats = new BatteryStatsImpl(
889 null, mStats.mHandler);
890 checkinStats.readSummaryFromParcel(in);
891 in.recycle();
892 checkinStats.dumpCheckinLocked(mContext, pw, apps, flags,
893 historyStart);
894 mStats.mCheckinFile.delete();
895 return;
896 }
897 } catch (IOException e) {
898 Slog.w(TAG, "Failure reading checkin file "
899 + mStats.mCheckinFile.getBaseFile(), e);
900 }
901 }
902 }
903 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800904 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800905 mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -0700906 if (writeData) {
907 mStats.writeAsyncLocked();
908 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800909 }
910 } else {
911 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800912 mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -0700913 if (writeData) {
914 mStats.writeAsyncLocked();
915 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918 }
919}