blob: 13b75abcf0f54451e2ea9833177c077bd1df036e [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
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070019import android.bluetooth.BluetoothActivityEnergyInfo;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070020import android.bluetooth.BluetoothAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080022import android.content.pm.ApplicationInfo;
Kenny Root3abd75b2011-09-29 11:00:41 -070023import android.content.pm.PackageManager;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070024import android.net.wifi.IWifiManager;
25import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070026import android.os.BatteryStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Binder;
Jeff Brown6f357d32014-01-15 20:40:55 -080028import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.IBinder;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070030import android.os.Looper;
31import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.Parcel;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070033import android.os.ParcelFileDescriptor;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070034import android.os.ParcelFormatException;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070035import android.os.PowerManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Process;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070037import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.ServiceManager;
Dianne Hackborne5167ca2014-03-08 14:39:10 -080039import android.os.SystemClock;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070040import android.os.UserHandle;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070041import android.os.WorkSource;
Adam Lesinski06f46cb2015-06-23 13:42:53 -070042import android.telephony.DataConnectionRealTimeInfo;
Wink Savillee9b06d72009-05-18 21:47:50 -070043import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080044import android.telephony.TelephonyManager;
Adam Lesinski61db88f2015-07-01 15:05:07 -070045import android.util.IntArray;
Joe Onorato8a9b2202010-02-26 18:56:32 -080046import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Adam Lesinskibd6704f2015-06-18 18:05:20 -070048import android.util.TimeUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070049import com.android.internal.annotations.GuardedBy;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070050import com.android.internal.app.IBatteryStats;
Dianne Hackbornd953c532014-08-16 18:17:38 -070051import com.android.internal.os.BatteryStatsHelper;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070052import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070053import com.android.internal.os.PowerProfile;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070054import com.android.server.FgThread;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070055import com.android.server.LocalServices;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070056
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070057import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.io.FileDescriptor;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070059import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080061import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63/**
64 * All information we are collecting about things that can happen that impact
65 * battery life.
66 */
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070067public final class BatteryStatsService extends IBatteryStats.Stub
68 implements PowerManagerInternal.LowPowerModeListener {
Dianne Hackbornc51cf032014-03-02 19:08:15 -080069 static final String TAG = "BatteryStatsService";
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 static IBatteryStats sService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 final BatteryStatsImpl mStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070073 final BatteryStatsHandler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 Context mContext;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070075 PowerManagerInternal mPowerManagerInternal;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070076
Adam Lesinskia7c90c82015-06-18 14:52:24 -070077 final int UPDATE_CPU = 0x01;
78 final int UPDATE_WIFI = 0x02;
79 final int UPDATE_RADIO = 0x04;
80 final int UPDATE_BT = 0x08;
81 final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
82
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070083 class BatteryStatsHandler extends Handler implements BatteryStatsImpl.ExternalStatsSync {
84 public static final int MSG_SYNC_EXTERNAL_STATS = 1;
85 public static final int MSG_WRITE_TO_DISK = 2;
Adam Lesinskia7c90c82015-06-18 14:52:24 -070086 private int mUpdateFlags = 0;
Adam Lesinski61db88f2015-07-01 15:05:07 -070087 private IntArray mUidsToRemove = new IntArray();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070088
89 public BatteryStatsHandler(Looper looper) {
90 super(looper);
91 }
92
93 @Override
94 public void handleMessage(Message msg) {
95 switch (msg.what) {
96 case MSG_SYNC_EXTERNAL_STATS:
Adam Lesinskia7c90c82015-06-18 14:52:24 -070097 final int updateFlags;
98 synchronized (this) {
99 removeMessages(MSG_SYNC_EXTERNAL_STATS);
100 updateFlags = mUpdateFlags;
101 mUpdateFlags = 0;
102 }
103 updateExternalStats((String)msg.obj, updateFlags);
Adam Lesinski56d82dd2015-07-14 16:27:40 -0700104
105 // other parts of the system could be calling into us
106 // from mStats in order to report of changes. We must grab the mStats
107 // lock before grabbing our own or we'll end up in a deadlock.
108 synchronized (mStats) {
109 synchronized (this) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700110 final int numUidsToRemove = mUidsToRemove.size();
111 for (int i = 0; i < numUidsToRemove; i++) {
112 mStats.removeIsolatedUidLocked(mUidsToRemove.get(i));
113 }
114 }
115 mUidsToRemove.clear();
116 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700117 break;
118
119 case MSG_WRITE_TO_DISK:
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700120 updateExternalStats("write", UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700121 synchronized (mStats) {
122 mStats.writeAsyncLocked();
123 }
124 break;
125 }
126 }
127
128 @Override
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700129 public void scheduleSync(String reason) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700130 synchronized (this) {
131 scheduleSyncLocked(reason, UPDATE_ALL);
132 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700133 }
134
135 @Override
136 public void scheduleWifiSync(String reason) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700137 synchronized (this) {
138 scheduleSyncLocked(reason, UPDATE_WIFI);
139 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700140 }
141
Adam Lesinski61db88f2015-07-01 15:05:07 -0700142 @Override
143 public void scheduleCpuSyncDueToRemovedUid(int uid) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700144 synchronized (this) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700145 scheduleSyncLocked("remove-uid", UPDATE_CPU);
146 mUidsToRemove.add(uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700147 }
148 }
Adam Lesinski61db88f2015-07-01 15:05:07 -0700149
150 private void scheduleSyncLocked(String reason, int updateFlags) {
151 if (mUpdateFlags == 0) {
152 sendMessage(Message.obtain(this, MSG_SYNC_EXTERNAL_STATS, reason));
153 }
154 mUpdateFlags |= updateFlags;
155 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700156 }
157
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700158 BatteryStatsService(File systemDir, Handler handler) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700159 // Our handler here will be accessing the disk, use a different thread than
160 // what the ActivityManagerService gave us (no I/O on that one!).
161 mHandler = new BatteryStatsHandler(FgThread.getHandler().getLooper());
162
163 // BatteryStatsImpl expects the ActivityManagerService handler, so pass that one through.
164 mStats = new BatteryStatsImpl(systemDir, handler, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166
167 public void publish(Context context) {
168 mContext = context;
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700169 ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
Amith Yamasanie43530a2009-08-21 13:11:37 -0700170 mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
Amith Yamasanif37447b2009-10-08 18:28:01 -0700171 mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
172 com.android.internal.R.integer.config_radioScanningTimeout)
173 * 1000L);
Adam Lesinskie08af192015-03-25 16:42:59 -0700174 mStats.setPowerProfile(new PowerProfile(context));
Jeff Brown2c43c332014-06-12 22:38:59 -0700175 }
176
177 /**
178 * At the time when the constructor runs, the power manager has not yet been
179 * initialized. So we initialize the low power observer later.
180 */
181 public void initPowerManagement() {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700182 mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
183 mPowerManagerInternal.registerLowPowerModeObserver(this);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700184 mStats.notePowerSaveMode(mPowerManagerInternal.getLowPowerModeEnabled());
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800185 (new WakeupReasonThread()).start();
Jeff Brown2c43c332014-06-12 22:38:59 -0700186 }
187
Dianne Hackborn55280a92009-05-07 15:53:46 -0700188 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800189 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700190
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700191 updateExternalStats("shutdown", UPDATE_ALL);
Dianne Hackborn55280a92009-05-07 15:53:46 -0700192 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700193 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700194 }
195 }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static IBatteryStats getService() {
198 if (sService != null) {
199 return sService;
200 }
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700201 IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 sService = asInterface(b);
203 return sService;
204 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700205
206 @Override
207 public void onLowPowerModeChanged(boolean enabled) {
208 synchronized (mStats) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700209 mStats.notePowerSaveMode(enabled);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700210 }
211 }
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 /**
214 * @return the current statistics object, which may be modified
215 * to reflect events that affect battery usage. You must lock the
216 * stats object before doing anything with it.
217 */
218 public BatteryStatsImpl getActiveStatistics() {
219 return mStats;
220 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700221
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700222 /**
223 * Schedules a write to disk to occur. This will cause the BatteryStatsImpl
224 * object to update with the latest info, then write to disk.
225 */
226 public void scheduleWriteToDisk() {
227 mHandler.sendEmptyMessage(BatteryStatsHandler.MSG_WRITE_TO_DISK);
228 }
229
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700230 // These are for direct use by the activity manager...
231
Adam Lesinskib83ffee2015-05-12 14:43:47 -0700232 /**
233 * Remove a UID from the BatteryStats and BatteryStats' external dependencies.
234 */
235 void removeUid(int uid) {
236 synchronized (mStats) {
237 mStats.removeUidStatsLocked(uid);
238 }
239 }
240
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700241 void addIsolatedUid(int isolatedUid, int appUid) {
242 synchronized (mStats) {
243 mStats.addIsolatedUidLocked(isolatedUid, appUid);
244 }
245 }
246
247 void removeIsolatedUid(int isolatedUid, int appUid) {
248 synchronized (mStats) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700249 mStats.scheduleRemoveIsolatedUidLocked(isolatedUid, appUid);
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700250 }
251 }
252
253 void noteProcessStart(String name, int uid) {
254 synchronized (mStats) {
255 mStats.noteProcessStartLocked(name, uid);
256 }
257 }
258
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800259 void noteProcessCrash(String name, int uid) {
260 synchronized (mStats) {
261 mStats.noteProcessCrashLocked(name, uid);
262 }
263 }
264
265 void noteProcessAnr(String name, int uid) {
266 synchronized (mStats) {
267 mStats.noteProcessAnrLocked(name, uid);
268 }
269 }
270
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700271 void noteProcessState(String name, int uid, int state) {
272 synchronized (mStats) {
273 mStats.noteProcessStateLocked(name, uid, state);
274 }
275 }
276
277 void noteProcessFinish(String name, int uid) {
278 synchronized (mStats) {
279 mStats.noteProcessFinishLocked(name, uid);
280 }
281 }
282
283 // Public interface...
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 public byte[] getStatistics() {
286 mContext.enforceCallingPermission(
287 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800288 //Slog.i("foo", "SENDING BATTERY INFO:");
289 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 Parcel out = Parcel.obtain();
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700291 updateExternalStats("get-stats", UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700292 synchronized (mStats) {
293 mStats.writeToParcel(out, 0);
294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 byte[] data = out.marshall();
296 out.recycle();
297 return data;
298 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700299
300 public ParcelFileDescriptor getStatisticsStream() {
301 mContext.enforceCallingPermission(
302 android.Manifest.permission.BATTERY_STATS, null);
303 //Slog.i("foo", "SENDING BATTERY INFO:");
304 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
305 Parcel out = Parcel.obtain();
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700306 updateExternalStats("get-stats", UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700307 synchronized (mStats) {
308 mStats.writeToParcel(out, 0);
309 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700310 byte[] data = out.marshall();
311 out.recycle();
312 try {
313 return ParcelFileDescriptor.fromData(data, "battery-stats");
314 } catch (IOException e) {
315 Slog.w(TAG, "Unable to create shared memory", e);
316 return null;
317 }
318 }
319
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700320 public boolean isCharging() {
321 synchronized (mStats) {
322 return mStats.isCharging();
323 }
324 }
325
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700326 public long computeBatteryTimeRemaining() {
327 synchronized (mStats) {
328 long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
329 return time >= 0 ? (time/1000) : time;
330 }
331 }
332
333 public long computeChargeTimeRemaining() {
334 synchronized (mStats) {
335 long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime());
336 return time >= 0 ? (time/1000) : time;
337 }
338 }
339
Dianne Hackborn099bc622014-01-22 13:39:16 -0800340 public void noteEvent(int code, String name, int uid) {
341 enforceCallingPermission();
342 synchronized (mStats) {
343 mStats.noteEventLocked(code, name, uid);
344 }
345 }
346
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700347 public void noteSyncStart(String name, int uid) {
348 enforceCallingPermission();
349 synchronized (mStats) {
350 mStats.noteSyncStartLocked(name, uid);
351 }
352 }
353
354 public void noteSyncFinish(String name, int uid) {
355 enforceCallingPermission();
356 synchronized (mStats) {
357 mStats.noteSyncFinishLocked(name, uid);
358 }
359 }
360
361 public void noteJobStart(String name, int uid) {
362 enforceCallingPermission();
363 synchronized (mStats) {
364 mStats.noteJobStartLocked(name, uid);
365 }
366 }
367
368 public void noteJobFinish(String name, int uid) {
369 enforceCallingPermission();
370 synchronized (mStats) {
371 mStats.noteJobFinishLocked(name, uid);
372 }
373 }
374
Dianne Hackborn1e383822015-04-10 14:02:33 -0700375 public void noteAlarmStart(String name, int uid) {
376 enforceCallingPermission();
377 synchronized (mStats) {
378 mStats.noteAlarmStartLocked(name, uid);
379 }
380 }
381
382 public void noteAlarmFinish(String name, int uid) {
383 enforceCallingPermission();
384 synchronized (mStats) {
385 mStats.noteAlarmFinishLocked(name, uid);
386 }
387 }
388
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800389 public void noteStartWakelock(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800390 boolean unimportantForLogging) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 enforceCallingPermission();
392 synchronized (mStats) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800393 mStats.noteStartWakeLocked(uid, pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -0700394 SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 }
397
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700398 public void noteStopWakelock(int uid, int pid, String name, String historyName, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 enforceCallingPermission();
400 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700401 mStats.noteStopWakeLocked(uid, pid, name, historyName, type,
402 SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404 }
405
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800406 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name,
407 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700408 enforceCallingPermission();
409 synchronized (mStats) {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800410 mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName,
411 type, unimportantForLogging);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700412 }
413 }
414
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700415 public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name,
416 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800417 String newHistoryName, int newType, boolean newUnimportantForLogging) {
418 enforceCallingPermission();
419 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700420 mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800421 newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging);
422 }
423 }
424
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700425 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName,
426 int type) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700427 enforceCallingPermission();
428 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700429 mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700430 }
431 }
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public void noteStartSensor(int uid, int sensor) {
434 enforceCallingPermission();
435 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700436 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
438 }
439
440 public void noteStopSensor(int uid, int sensor) {
441 enforceCallingPermission();
442 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700443 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800447 public void noteVibratorOn(int uid, long durationMillis) {
448 enforceCallingPermission();
449 synchronized (mStats) {
450 mStats.noteVibratorOnLocked(uid, durationMillis);
451 }
452 }
453
454 public void noteVibratorOff(int uid) {
455 enforceCallingPermission();
456 synchronized (mStats) {
457 mStats.noteVibratorOffLocked(uid);
458 }
459 }
460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public void noteStartGps(int uid) {
462 enforceCallingPermission();
463 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700464 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 }
467
468 public void noteStopGps(int uid) {
469 enforceCallingPermission();
470 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700471 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
473 }
474
Jeff Browne95c3cd2014-05-02 16:59:26 -0700475 public void noteScreenState(int state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 enforceCallingPermission();
477 synchronized (mStats) {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700478 mStats.noteScreenStateLocked(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480 }
481
Dianne Hackborn617f8772009-03-31 15:04:46 -0700482 public void noteScreenBrightness(int brightness) {
483 enforceCallingPermission();
484 synchronized (mStats) {
485 mStats.noteScreenBrightnessLocked(brightness);
486 }
487 }
488
Dianne Hackborn617f8772009-03-31 15:04:46 -0700489 public void noteUserActivity(int uid, int event) {
490 enforceCallingPermission();
491 synchronized (mStats) {
492 mStats.noteUserActivityLocked(uid, event);
493 }
494 }
Jeff Browne95c3cd2014-05-02 16:59:26 -0700495
496 public void noteInteractive(boolean interactive) {
497 enforceCallingPermission();
498 synchronized (mStats) {
499 mStats.noteInteractiveLocked(interactive);
500 }
501 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800502
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800503 public void noteConnectivityChanged(int type, String extra) {
504 enforceCallingPermission();
505 synchronized (mStats) {
506 mStats.noteConnectivityChangedLocked(type, extra);
507 }
508 }
509
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700510 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800511 enforceCallingPermission();
512 synchronized (mStats) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700513 mStats.noteMobileRadioPowerState(powerState, timestampNs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800514 }
515 }
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public void notePhoneOn() {
518 enforceCallingPermission();
519 synchronized (mStats) {
520 mStats.notePhoneOnLocked();
521 }
522 }
523
524 public void notePhoneOff() {
525 enforceCallingPermission();
526 synchronized (mStats) {
527 mStats.notePhoneOffLocked();
528 }
529 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700530
Wink Savillee9b06d72009-05-18 21:47:50 -0700531 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700532 enforceCallingPermission();
533 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700534 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700535 }
536 }
537
538 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
539 enforceCallingPermission();
540 synchronized (mStats) {
541 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
542 }
543 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700544
Amith Yamasanif37447b2009-10-08 18:28:01 -0700545 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700546 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800547 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700548 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800549 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700550 }
551 }
552
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700553 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700554 enforceCallingPermission();
555 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700556 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700557 }
558 }
559
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700560 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700561 enforceCallingPermission();
562 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700563 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700564 }
565 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700566
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700567 public void noteStartAudio(int uid) {
568 enforceCallingPermission();
569 synchronized (mStats) {
570 mStats.noteAudioOnLocked(uid);
571 }
572 }
573
574 public void noteStopAudio(int uid) {
575 enforceCallingPermission();
576 synchronized (mStats) {
577 mStats.noteAudioOffLocked(uid);
578 }
579 }
580
581 public void noteStartVideo(int uid) {
582 enforceCallingPermission();
583 synchronized (mStats) {
584 mStats.noteVideoOnLocked(uid);
585 }
586 }
587
588 public void noteStopVideo(int uid) {
589 enforceCallingPermission();
590 synchronized (mStats) {
591 mStats.noteVideoOffLocked(uid);
592 }
593 }
594
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700595 public void noteResetAudio() {
596 enforceCallingPermission();
597 synchronized (mStats) {
598 mStats.noteResetAudioLocked();
599 }
600 }
601
602 public void noteResetVideo() {
603 enforceCallingPermission();
604 synchronized (mStats) {
605 mStats.noteResetVideoLocked();
606 }
607 }
608
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700609 public void noteFlashlightOn(int uid) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700610 enforceCallingPermission();
611 synchronized (mStats) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700612 mStats.noteFlashlightOnLocked(uid);
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700613 }
614 }
615
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700616 public void noteFlashlightOff(int uid) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700617 enforceCallingPermission();
618 synchronized (mStats) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700619 mStats.noteFlashlightOffLocked(uid);
620 }
621 }
622
623 public void noteStartCamera(int uid) {
624 enforceCallingPermission();
625 synchronized (mStats) {
626 mStats.noteCameraOnLocked(uid);
627 }
628 }
629
630 public void noteStopCamera(int uid) {
631 enforceCallingPermission();
632 synchronized (mStats) {
633 mStats.noteCameraOffLocked(uid);
634 }
635 }
636
637 public void noteResetCamera() {
638 enforceCallingPermission();
639 synchronized (mStats) {
640 mStats.noteResetCameraLocked();
641 }
642 }
643
644 public void noteResetFlashlight() {
645 enforceCallingPermission();
646 synchronized (mStats) {
647 mStats.noteResetFlashlightLocked();
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700648 }
649 }
650
Adam Lesinskie08af192015-03-25 16:42:59 -0700651 @Override
652 public void noteWifiRadioPowerState(int powerState, long tsNanos) {
653 enforceCallingPermission();
654
655 // There was a change in WiFi power state.
656 // Collect data now for the past activity.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700657 synchronized (mStats) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700658 if (mStats.isOnBattery()) {
Adam Lesinski06f46cb2015-06-23 13:42:53 -0700659 final String type = (powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH ||
660 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active"
661 : "inactive";
662 mHandler.scheduleWifiSync("wifi-data: " + type);
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700663 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700664 mStats.noteWifiRadioPowerState(powerState, tsNanos);
665 }
Adam Lesinskie08af192015-03-25 16:42:59 -0700666 }
667
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700668 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700669 enforceCallingPermission();
670 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700671 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700672 }
673 }
674
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700675 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700676 enforceCallingPermission();
677 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700678 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
679 }
680 }
681
682 public void noteWifiStopped(WorkSource ws) {
683 enforceCallingPermission();
684 synchronized (mStats) {
685 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700686 }
687 }
688
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800689 public void noteWifiState(int wifiState, String accessPoint) {
690 enforceCallingPermission();
691 synchronized (mStats) {
692 mStats.noteWifiStateLocked(wifiState, accessPoint);
693 }
694 }
695
Dianne Hackborn3251b902014-06-20 14:40:53 -0700696 public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) {
697 enforceCallingPermission();
698 synchronized (mStats) {
699 mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth);
700 }
701 }
702
703 public void noteWifiRssiChanged(int newRssi) {
704 enforceCallingPermission();
705 synchronized (mStats) {
706 mStats.noteWifiRssiChangedLocked(newRssi);
707 }
708 }
709
The Android Open Source Project10592532009-03-18 17:39:46 -0700710 public void noteFullWifiLockAcquired(int uid) {
711 enforceCallingPermission();
712 synchronized (mStats) {
713 mStats.noteFullWifiLockAcquiredLocked(uid);
714 }
715 }
716
717 public void noteFullWifiLockReleased(int uid) {
718 enforceCallingPermission();
719 synchronized (mStats) {
720 mStats.noteFullWifiLockReleasedLocked(uid);
721 }
722 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700723
724 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700725 enforceCallingPermission();
726 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700727 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700728 }
729 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700730
731 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700732 enforceCallingPermission();
733 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700734 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700735 }
736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700738 public void noteWifiMulticastEnabled(int uid) {
739 enforceCallingPermission();
740 synchronized (mStats) {
741 mStats.noteWifiMulticastEnabledLocked(uid);
742 }
743 }
744
745 public void noteWifiMulticastDisabled(int uid) {
746 enforceCallingPermission();
747 synchronized (mStats) {
748 mStats.noteWifiMulticastDisabledLocked(uid);
749 }
750 }
751
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700752 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
753 enforceCallingPermission();
754 synchronized (mStats) {
755 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
756 }
757 }
758
759 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
760 enforceCallingPermission();
761 synchronized (mStats) {
762 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
763 }
764 }
765
Nick Pelly6ccaa542012-06-15 15:22:47 -0700766 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700767 enforceCallingPermission();
768 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700769 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700770 }
771 }
772
Nick Pelly6ccaa542012-06-15 15:22:47 -0700773 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700774 enforceCallingPermission();
775 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700776 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700777 }
778 }
779
Robert Greenwalta029ea12013-09-25 16:38:12 -0700780 public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
781 enforceCallingPermission();
782 synchronized (mStats) {
783 mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
784 }
785 }
786
787 public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
788 enforceCallingPermission();
789 synchronized (mStats) {
790 mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
791 }
792 }
793
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700794 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
795 enforceCallingPermission();
796 synchronized (mStats) {
797 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
798 }
799 }
800
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700801 @Override
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700802 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
803 enforceCallingPermission();
804 synchronized (mStats) {
805 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
806 }
807 }
808
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700809 @Override
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700810 public void noteNetworkInterfaceType(String iface, int networkType) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700811 enforceCallingPermission();
812 synchronized (mStats) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700813 mStats.noteNetworkInterfaceTypeLocked(iface, networkType);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700814 }
815 }
816
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700817 @Override
818 public void noteNetworkStatsEnabled() {
819 enforceCallingPermission();
820 synchronized (mStats) {
821 mStats.noteNetworkStatsEnabledLocked();
822 }
823 }
824
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700825 @Override
Dianne Hackbornb6683c42015-06-18 17:40:33 -0700826 public void noteDeviceIdleMode(boolean enabled, String activeReason, int activeUid) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700827 enforceCallingPermission();
828 synchronized (mStats) {
Dianne Hackbornb6683c42015-06-18 17:40:33 -0700829 mStats.noteDeviceIdleModeLocked(enabled, activeReason, activeUid);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700830 }
831 }
832
833 public void notePackageInstalled(String pkgName, int versionCode) {
834 enforceCallingPermission();
835 synchronized (mStats) {
836 mStats.notePackageInstalledLocked(pkgName, versionCode);
837 }
838 }
839
840 public void notePackageUninstalled(String pkgName) {
841 enforceCallingPermission();
842 synchronized (mStats) {
843 mStats.notePackageUninstalledLocked(pkgName);
844 }
845 }
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public boolean isOnBattery() {
848 return mStats.isOnBattery();
849 }
850
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700851 public void setBatteryState(int status, int health, int plugType, int level,
852 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 enforceCallingPermission();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700854 synchronized (mStats) {
855 final boolean onBattery = plugType == BatteryStatsImpl.BATTERY_PLUGGED_NONE;
856 if (mStats.isOnBattery() == onBattery) {
857 // The battery state has not changed, so we don't need to sync external
858 // stats immediately.
859 mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt);
860 return;
861 }
862 }
863
864 // Sync external stats first as the battery has changed states. If we don't sync
865 // immediately here, we may not collect the relevant data later.
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700866 updateExternalStats("battery-state", UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700867 synchronized (mStats) {
868 mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt);
869 }
Evan Millar633a1742009-04-02 16:36:33 -0700870 }
871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 public long getAwakeTimeBattery() {
873 mContext.enforceCallingOrSelfPermission(
874 android.Manifest.permission.BATTERY_STATS, null);
875 return mStats.getAwakeTimeBattery();
876 }
877
878 public long getAwakeTimePlugged() {
879 mContext.enforceCallingOrSelfPermission(
880 android.Manifest.permission.BATTERY_STATS, null);
881 return mStats.getAwakeTimePlugged();
882 }
883
884 public void enforceCallingPermission() {
885 if (Binder.getCallingPid() == Process.myPid()) {
886 return;
887 }
888 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
889 Binder.getCallingPid(), Binder.getCallingUid(), null);
890 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800891
892 final class WakeupReasonThread extends Thread {
Adam Lesinski87fd3222015-06-25 13:10:36 -0700893 final String[] mReason = new String[1];
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800894
895 WakeupReasonThread() {
896 super("BatteryStats_wakeupReason");
897 }
898
899 public void run() {
900 Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
901
902 try {
903 int num;
Adam Lesinski87fd3222015-06-25 13:10:36 -0700904 while ((num = nativeWaitWakeup(mReason)) >= 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800905 synchronized (mStats) {
Adam Lesinski87fd3222015-06-25 13:10:36 -0700906 // num will be either 0 or 1.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700907 if (num > 0) {
Adam Lesinski87fd3222015-06-25 13:10:36 -0700908 mStats.noteWakeupReasonLocked(mReason[0]);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700909 } else {
910 mStats.noteWakeupReasonLocked("unknown");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800911 }
912 }
913 }
914 } catch (RuntimeException e) {
915 Slog.e(TAG, "Failure reading wakeup reasons", e);
916 }
917 }
918 }
919
Adam Lesinski87fd3222015-06-25 13:10:36 -0700920 private static native int nativeWaitWakeup(String[] outReason);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800921
Dianne Hackbornae384452011-06-28 12:33:48 -0700922 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700923 pw.println("Battery stats (batterystats) dump options:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700924 pw.println(" [--checkin] [--history] [--history-start] [--charged] [-c]");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800925 pw.println(" [--daily] [--reset] [--write] [--new-daily] [--read-daily] [-h] [<package.name>]");
Dianne Hackbornae384452011-06-28 12:33:48 -0700926 pw.println(" --checkin: format output for a checkin report.");
Dianne Hackborn099bc622014-01-22 13:39:16 -0800927 pw.println(" --history: show only history data.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800928 pw.println(" --history-start <num>: show only history data starting at given time offset.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800929 pw.println(" --charged: only output data since last charged.");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800930 pw.println(" --daily: only output full daily data.");
Dianne Hackbornae384452011-06-28 12:33:48 -0700931 pw.println(" --reset: reset the stats, clearing all current data.");
932 pw.println(" --write: force write current collected stats to disk.");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800933 pw.println(" --new-daily: immediately create and write new daily stats record.");
934 pw.println(" --read-daily: read-load last written daily stats.");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700935 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornfc064132014-06-02 12:42:12 -0700936 pw.println(" -h: print this help text.");
937 pw.println("Battery stats (batterystats) commands:");
938 pw.println(" enable|disable <option>");
939 pw.println(" Enable or disable a running option. Option state is not saved across boots.");
940 pw.println(" Options are:");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700941 pw.println(" full-history: include additional detailed events in battery history:");
Dianne Hackborn1e383822015-04-10 14:02:33 -0700942 pw.println(" wake_lock_in, alarms and proc events");
Dianne Hackbornfc064132014-06-02 12:42:12 -0700943 pw.println(" no-auto-reset: don't automatically reset stats when unplugged");
Dianne Hackbornae384452011-06-28 12:33:48 -0700944 }
945
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700946 private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) {
947 i++;
948 if (i >= args.length) {
949 pw.println("Missing option argument for " + (enable ? "--enable" : "--disable"));
950 dumpHelp(pw);
951 return -1;
952 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700953 if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700954 synchronized (mStats) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700955 mStats.setRecordAllHistoryLocked(enable);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700956 }
Dianne Hackborn9a755432014-05-15 17:05:22 -0700957 } else if ("no-auto-reset".equals(args[i])) {
958 synchronized (mStats) {
959 mStats.setNoAutoReset(enable);
960 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700961 } else {
962 pw.println("Unknown enable/disable option: " + args[i]);
963 dumpHelp(pw);
964 return -1;
965 }
966 return i;
967 }
968
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 @Override
971 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -0700972 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
973 != PackageManager.PERMISSION_GRANTED) {
974 pw.println("Permission Denial: can't dump BatteryStats from from pid="
975 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
976 + " without permission " + android.Manifest.permission.DUMP);
977 return;
978 }
979
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800980 int flags = 0;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700981 boolean useCheckinFormat = false;
982 boolean isRealCheckin = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700983 boolean noOutput = false;
Dianne Hackborn16b0b562014-06-03 17:24:42 -0700984 boolean writeData = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800985 long historyStart = -1;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700986 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800987 if (args != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800988 for (int i=0; i<args.length; i++) {
989 String arg = args[i];
Dianne Hackborne4a59512010-12-07 11:08:07 -0800990 if ("--checkin".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700991 useCheckinFormat = true;
992 isRealCheckin = true;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800993 } else if ("--history".equals(arg)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800994 flags |= BatteryStats.DUMP_HISTORY_ONLY;
995 } else if ("--history-start".equals(arg)) {
996 flags |= BatteryStats.DUMP_HISTORY_ONLY;
997 i++;
998 if (i >= args.length) {
999 pw.println("Missing time argument for --history-since");
1000 dumpHelp(pw);
1001 return;
1002 }
1003 historyStart = Long.parseLong(args[i]);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001004 writeData = true;
Dianne Hackborn49021f52013-09-04 18:03:40 -07001005 } else if ("-c".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001006 useCheckinFormat = true;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001007 flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001008 } else if ("--charged".equals(arg)) {
1009 flags |= BatteryStats.DUMP_CHARGED_ONLY;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001010 } else if ("--daily".equals(arg)) {
1011 flags |= BatteryStats.DUMP_DAILY_ONLY;
Dianne Hackborne4a59512010-12-07 11:08:07 -08001012 } else if ("--reset".equals(arg)) {
1013 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001014 mStats.resetAllStatsCmdLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -08001015 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001016 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001018 updateExternalStats("dump", UPDATE_ALL);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001019 } else if ("--write".equals(arg)) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001020 updateExternalStats("dump", UPDATE_ALL);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001021 synchronized (mStats) {
1022 mStats.writeSyncLocked();
1023 pw.println("Battery stats written.");
1024 noOutput = true;
1025 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001026 } else if ("--new-daily".equals(arg)) {
1027 synchronized (mStats) {
1028 mStats.recordDailyStatsLocked();
1029 pw.println("New daily stats written.");
1030 noOutput = true;
1031 }
1032 } else if ("--read-daily".equals(arg)) {
1033 synchronized (mStats) {
1034 mStats.readDailyStatsLocked();
1035 pw.println("Last daily stats read.");
1036 noOutput = true;
1037 }
Dianne Hackbornfc064132014-06-02 12:42:12 -07001038 } else if ("--enable".equals(arg) || "enable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001039 i = doEnableOrDisable(pw, i, args, true);
1040 if (i < 0) {
1041 return;
1042 }
1043 pw.println("Enabled: " + args[i]);
1044 return;
Dianne Hackbornfc064132014-06-02 12:42:12 -07001045 } else if ("--disable".equals(arg) || "disable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001046 i = doEnableOrDisable(pw, i, args, false);
1047 if (i < 0) {
1048 return;
1049 }
1050 pw.println("Disabled: " + args[i]);
1051 return;
Dianne Hackbornae384452011-06-28 12:33:48 -07001052 } else if ("-h".equals(arg)) {
1053 dumpHelp(pw);
1054 return;
Mike Lockwoode8174042011-08-16 12:53:43 -07001055 } else if ("-a".equals(arg)) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001056 flags |= BatteryStats.DUMP_VERBOSE;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001057 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001058 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -07001059 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001060 return;
1061 } else {
1062 // Not an option, last argument must be a package name.
1063 try {
1064 reqUid = mContext.getPackageManager().getPackageUid(arg,
1065 UserHandle.getCallingUserId());
1066 } catch (PackageManager.NameNotFoundException e) {
1067 pw.println("Unknown package: " + arg);
1068 dumpHelp(pw);
1069 return;
1070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001073 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001074 if (noOutput) {
1075 return;
1076 }
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001077
1078 long ident = Binder.clearCallingIdentity();
1079 try {
1080 if (BatteryStatsHelper.checkWifiOnly(mContext)) {
1081 flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
1082 }
1083 // Fetch data from external sources and update the BatteryStatsImpl object with them.
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001084 updateExternalStats("dump", UPDATE_ALL);
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001085 } finally {
1086 Binder.restoreCallingIdentity(ident);
Dianne Hackbornd953c532014-08-16 18:17:38 -07001087 }
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001088
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07001089 if (reqUid >= 0) {
1090 // By default, if the caller is only interested in a specific package, then
1091 // we only dump the aggregated data since charged.
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001092 if ((flags&(BatteryStats.DUMP_HISTORY_ONLY|BatteryStats.DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07001093 flags |= BatteryStats.DUMP_CHARGED_ONLY;
1094 // Also if they are doing -c, we don't want history.
1095 flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY;
1096 }
1097 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001098
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001099 if (useCheckinFormat) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08001100 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001101 if (isRealCheckin) {
1102 // For a real checkin, first we want to prefer to use the last complete checkin
1103 // file if there is one.
1104 synchronized (mStats.mCheckinFile) {
1105 if (mStats.mCheckinFile.exists()) {
1106 try {
1107 byte[] raw = mStats.mCheckinFile.readFully();
1108 if (raw != null) {
1109 Parcel in = Parcel.obtain();
1110 in.unmarshall(raw, 0, raw.length);
1111 in.setDataPosition(0);
1112 BatteryStatsImpl checkinStats = new BatteryStatsImpl(
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001113 null, mStats.mHandler, null);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001114 checkinStats.readSummaryFromParcel(in);
1115 in.recycle();
1116 checkinStats.dumpCheckinLocked(mContext, pw, apps, flags,
1117 historyStart);
1118 mStats.mCheckinFile.delete();
1119 return;
1120 }
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001121 } catch (IOException | ParcelFormatException e) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001122 Slog.w(TAG, "Failure reading checkin file "
1123 + mStats.mCheckinFile.getBaseFile(), e);
1124 }
1125 }
1126 }
1127 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001128 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001129 mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001130 if (writeData) {
1131 mStats.writeAsyncLocked();
1132 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001133 }
1134 } else {
1135 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001136 mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001137 if (writeData) {
1138 mStats.writeAsyncLocked();
1139 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001143
1144 // Objects for extracting data from external sources.
1145 private final Object mExternalStatsLock = new Object();
1146
1147 @GuardedBy("mExternalStatsLock")
1148 private IWifiManager mWifiManager;
1149
1150 // WiFi keeps an accumulated total of stats, unlike Bluetooth.
1151 // Keep the last WiFi stats so we can compute a delta.
1152 @GuardedBy("mExternalStatsLock")
1153 private WifiActivityEnergyInfo mLastInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0);
1154
1155 @GuardedBy("mExternalStatsLock")
1156 private WifiActivityEnergyInfo pullWifiEnergyInfoLocked() {
1157 if (mWifiManager == null) {
1158 mWifiManager = IWifiManager.Stub.asInterface(
1159 ServiceManager.getService(Context.WIFI_SERVICE));
1160 if (mWifiManager == null) {
1161 return null;
1162 }
1163 }
1164
1165 try {
1166 // We read the data even if we are not on battery. This is so that we keep the
1167 // correct delta from when we should start reading (aka when we are on battery).
1168 WifiActivityEnergyInfo info = mWifiManager.reportActivityInfo();
1169 if (info != null && info.isValid()) {
Adam Lesinskif4013aa2015-06-12 15:18:06 -07001170 if (info.mControllerEnergyUsed < 0 || info.mControllerIdleTimeMs < 0 ||
1171 info.mControllerRxTimeMs < 0 || info.mControllerTxTimeMs < 0) {
1172 Slog.wtf(TAG, "Reported WiFi energy data is invalid: " + info);
1173 return null;
1174 }
1175
Adam Lesinskibd6704f2015-06-18 18:05:20 -07001176 final long timePeriodMs = info.mTimestamp - mLastInfo.mTimestamp;
1177 final long lastIdleMs = mLastInfo.mControllerIdleTimeMs;
1178 final long lastTxMs = mLastInfo.mControllerTxTimeMs;
1179 final long lastRxMs = mLastInfo.mControllerRxTimeMs;
1180 final long lastEnergy = mLastInfo.mControllerEnergyUsed;
1181
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001182 // We will modify the last info object to be the delta, and store the new
1183 // WifiActivityEnergyInfo object as our last one.
1184 final WifiActivityEnergyInfo result = mLastInfo;
1185 result.mTimestamp = info.getTimeStamp();
1186 result.mStackState = info.getStackState();
Adam Lesinskif4013aa2015-06-12 15:18:06 -07001187
1188 // These times seem to be the most reliable.
Adam Lesinskibd6704f2015-06-18 18:05:20 -07001189 result.mControllerTxTimeMs = info.mControllerTxTimeMs - lastTxMs;
1190 result.mControllerRxTimeMs = info.mControllerRxTimeMs - lastRxMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07001191
1192 // WiFi calculates the idle time as a difference from the on time and the various
1193 // Rx + Tx times. There seems to be some missing time there because this sometimes
1194 // becomes negative. Just cap it at 0 and move on.
Adam Lesinskif4013aa2015-06-12 15:18:06 -07001195 // b/21613534
Adam Lesinskibd6704f2015-06-18 18:05:20 -07001196 result.mControllerIdleTimeMs = Math.max(0, info.mControllerIdleTimeMs - lastIdleMs);
Adam Lesinskif4013aa2015-06-12 15:18:06 -07001197 result.mControllerEnergyUsed =
Adam Lesinskibd6704f2015-06-18 18:05:20 -07001198 Math.max(0, info.mControllerEnergyUsed - lastEnergy);
Adam Lesinskie08af192015-03-25 16:42:59 -07001199
1200 if (result.mControllerTxTimeMs < 0 ||
1201 result.mControllerRxTimeMs < 0) {
1202 // The stats were reset by the WiFi system (which is why our delta is negative).
1203 // Returns the unaltered stats.
1204 result.mControllerEnergyUsed = info.mControllerEnergyUsed;
1205 result.mControllerRxTimeMs = info.mControllerRxTimeMs;
1206 result.mControllerTxTimeMs = info.mControllerTxTimeMs;
1207 result.mControllerIdleTimeMs = info.mControllerIdleTimeMs;
1208
1209 Slog.v(TAG, "WiFi energy data was reset, new WiFi energy data is " + result);
1210 }
Adam Lesinskibd6704f2015-06-18 18:05:20 -07001211
1212 final long totalTimeMs = result.mControllerIdleTimeMs + result.mControllerRxTimeMs +
1213 result.mControllerTxTimeMs;
1214 if (totalTimeMs > timePeriodMs) {
1215 StringBuilder sb = new StringBuilder();
1216 sb.append("Total time ");
1217 TimeUtils.formatDuration(totalTimeMs, sb);
1218 sb.append(" is longer than sample period ");
1219 TimeUtils.formatDuration(timePeriodMs, sb);
1220 sb.append(".\n");
1221 sb.append("Previous WiFi snapshot: ").append("idle=");
1222 TimeUtils.formatDuration(lastIdleMs, sb);
1223 sb.append(" rx=");
1224 TimeUtils.formatDuration(lastRxMs, sb);
1225 sb.append(" tx=");
1226 TimeUtils.formatDuration(lastTxMs, sb);
1227 sb.append(" e=").append(lastEnergy);
1228 sb.append("\n");
1229 sb.append("Current WiFi snapshot: ").append("idle=");
1230 TimeUtils.formatDuration(info.mControllerIdleTimeMs, sb);
1231 sb.append(" rx=");
1232 TimeUtils.formatDuration(info.mControllerRxTimeMs, sb);
1233 sb.append(" tx=");
1234 TimeUtils.formatDuration(info.mControllerTxTimeMs, sb);
1235 sb.append(" e=").append(info.mControllerEnergyUsed);
1236 Slog.wtf(TAG, sb.toString());
1237 }
1238
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001239 mLastInfo = info;
1240 return result;
1241 }
1242 } catch (RemoteException e) {
1243 // Nothing to report, WiFi is dead.
1244 }
1245 return null;
1246 }
1247
1248 @GuardedBy("mExternalStatsLock")
1249 private BluetoothActivityEnergyInfo pullBluetoothEnergyInfoLocked() {
1250 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
1251 if (adapter != null) {
1252 BluetoothActivityEnergyInfo info = adapter.getControllerActivityEnergyInfo(
1253 BluetoothAdapter.ACTIVITY_ENERGY_INFO_REFRESHED);
1254 if (info != null && info.isValid()) {
Adam Lesinski8a351372015-06-15 17:19:07 -07001255 if (info.getControllerEnergyUsed() < 0 || info.getControllerIdleTimeMillis() < 0 ||
1256 info.getControllerRxTimeMillis() < 0 || info.getControllerTxTimeMillis() < 0) {
1257 Slog.wtf(TAG, "Bluetooth energy data is invalid: " + info);
1258 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001259 return info;
1260 }
1261 }
1262 return null;
1263 }
1264
1265 /**
1266 * Fetches data from external sources (WiFi controller, bluetooth chipset) and updates
1267 * batterystats with that information.
1268 *
1269 * We first grab a lock specific to this method, then once all the data has been collected,
1270 * we grab the mStats lock and update the data.
Adam Lesinskic14c2732015-05-21 15:04:18 -07001271 *
Adam Lesinskic14c2732015-05-21 15:04:18 -07001272 * @param reason The reason why this collection was requested. Useful for debugging.
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001273 * @param updateFlags Which external stats to update. Can be a combination of
1274 * {@link #UPDATE_CPU}, {@link #UPDATE_RADIO}, {@link #UPDATE_WIFI},
1275 * and {@link #UPDATE_BT}.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001276 */
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001277 void updateExternalStats(final String reason, final int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001278 synchronized (mExternalStatsLock) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001279 if (mContext == null) {
1280 // We haven't started yet (which means the BatteryStatsImpl object has
1281 // no power profile. Don't consume data we can't compute yet.
1282 return;
1283 }
1284
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001285 if (BatteryStatsImpl.DEBUG_ENERGY) {
1286 Slog.d(TAG, "Updating external stats: reason=" + reason);
1287 }
1288
1289 WifiActivityEnergyInfo wifiEnergyInfo = null;
1290 if ((updateFlags & UPDATE_WIFI) != 0) {
1291 wifiEnergyInfo = pullWifiEnergyInfoLocked();
1292 }
1293
1294 BluetoothActivityEnergyInfo bluetoothEnergyInfo = null;
1295 if ((updateFlags & UPDATE_BT) != 0) {
Adam Lesinskic14c2732015-05-21 15:04:18 -07001296 // We only pull bluetooth stats when we have to, as we are not distributing its
1297 // use amongst apps and the sampling frequency does not matter.
1298 bluetoothEnergyInfo = pullBluetoothEnergyInfoLocked();
Adam Lesinskic14c2732015-05-21 15:04:18 -07001299 }
1300
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001301 synchronized (mStats) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001302 final long elapsedRealtime = SystemClock.elapsedRealtime();
1303 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001304 if (mStats.mRecordAllHistory) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001305 mStats.addHistoryEventLocked(elapsedRealtime, uptime,
1306 BatteryStats.HistoryItem.EVENT_COLLECT_EXTERNAL_STATS, reason, 0);
1307 }
Adam Lesinski72478f02015-06-17 15:39:43 -07001308
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001309 if ((updateFlags & UPDATE_CPU) != 0) {
1310 mStats.updateCpuTimeLocked();
1311 mStats.updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07001312 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -07001313
1314 if ((updateFlags & UPDATE_RADIO) != 0) {
1315 mStats.updateMobileRadioStateLocked(elapsedRealtime);
1316 }
1317
1318 if ((updateFlags & UPDATE_WIFI) != 0) {
1319 mStats.updateWifiStateLocked(wifiEnergyInfo);
1320 }
1321
1322 if ((updateFlags & UPDATE_BT) != 0) {
1323 mStats.updateBluetoothStateLocked(bluetoothEnergyInfo);
1324 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001325 }
1326 }
1327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328}