blob: c2f4529327755db9915267b0078f6b10504da643 [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
Bookatzdb026a22018-01-10 19:01:56 -080019import android.app.ActivityManager;
Bookatz235343d2018-03-26 13:03:50 -070020import android.app.job.JobProtoEnums;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070021import android.bluetooth.BluetoothActivityEnergyInfo;
Ng Zhi An09411202019-01-23 11:46:50 -080022import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import android.content.pm.ApplicationInfo;
Kenny Root3abd75b2011-09-29 11:00:41 -070025import android.content.pm.PackageManager;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070026import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070027import android.os.BatteryStats;
David Chenc8a43242017-10-17 16:23:28 -070028import android.os.BatteryStatsInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.Binder;
Jeff Brown6f357d32014-01-15 20:40:55 -080030import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.IBinder;
32import 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;
Mike Mafbc01fc2018-04-02 10:28:28 -070035import android.os.PowerManager.ServiceType;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070036import android.os.PowerManagerInternal;
Mike Mafbc01fc2018-04-02 10:28:28 -070037import android.os.PowerSaveState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Process;
39import android.os.ServiceManager;
Dianne Hackborne5167ca2014-03-08 14:39:10 -080040import android.os.SystemClock;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070041import android.os.UserHandle;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070042import android.os.UserManagerInternal;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070043import android.os.WorkSource;
Siddharth Raya1fd0572017-11-13 14:20:47 -080044import android.os.connectivity.CellularBatteryStats;
Siddharth Ray78ccaf52017-12-23 16:16:21 -080045import android.os.connectivity.GpsBatteryStats;
Mike Mafbc01fc2018-04-02 10:28:28 -070046import android.os.connectivity.WifiBatteryStats;
Joe Onorato713fec82016-03-04 10:34:02 -080047import android.os.health.HealthStatsParceler;
48import android.os.health.HealthStatsWriter;
49import android.os.health.UidHealthStats;
Ng Zhi An09411202019-01-23 11:46:50 -080050import android.provider.Settings;
Adam Lesinski06f46cb2015-06-23 13:42:53 -070051import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080052import android.telephony.ModemActivityInfo;
Wink Savillee9b06d72009-05-18 21:47:50 -070053import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080054import android.telephony.TelephonyManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080055import android.util.Slog;
Mike Mafbc01fc2018-04-02 10:28:28 -070056import android.util.StatsLog;
Amith Yamasani674c9bb2017-02-01 09:45:17 -080057
Amith Yamasani32dbefd2009-06-19 09:21:17 -070058import com.android.internal.app.IBatteryStats;
Dianne Hackbornd953c532014-08-16 18:17:38 -070059import com.android.internal.os.BatteryStatsHelper;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070060import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070061import com.android.internal.os.PowerProfile;
Blake Kragtencb308d92019-01-28 10:54:13 -080062import com.android.internal.os.RailStats;
Bookatz50df7112017-08-04 14:53:26 -070063import com.android.internal.os.RpmStats;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060064import com.android.internal.util.DumpUtils;
Hui Yu0ed84f12018-06-20 19:07:56 -070065import com.android.internal.util.ParseUtils;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070066import com.android.server.LocalServices;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070067
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070068import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.FileDescriptor;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070070import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import java.io.PrintWriter;
Adam Lesinski515702c2015-07-23 18:13:38 -070072import java.nio.ByteBuffer;
73import java.nio.CharBuffer;
74import java.nio.charset.CharsetDecoder;
75import java.nio.charset.CodingErrorAction;
76import java.nio.charset.StandardCharsets;
Joe Onorato713fec82016-03-04 10:34:02 -080077import java.util.Arrays;
Dianne Hackborne4a59512010-12-07 11:08:07 -080078import java.util.List;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070079import java.util.concurrent.ExecutionException;
80import java.util.concurrent.Future;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82/**
83 * All information we are collecting about things that can happen that impact
84 * battery life.
85 */
Dianne Hackborncbefd8d2014-05-14 11:42:00 -070086public final class BatteryStatsService extends IBatteryStats.Stub
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -070087 implements PowerManagerInternal.LowPowerModeListener,
Blake Kragtencb308d92019-01-28 10:54:13 -080088 BatteryStatsImpl.PlatformIdleStateCallback,
89 BatteryStatsImpl.RailEnergyDataCallback {
Dianne Hackbornc51cf032014-03-02 19:08:15 -080090 static final String TAG = "BatteryStatsService";
Joe Onorato82ba91d2017-04-27 16:18:05 -070091 static final boolean DBG = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -080092
Adam Lesinski010bf372016-04-11 12:18:18 -070093 private static IBatteryStats sService;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 final BatteryStatsImpl mStats;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070096 private final BatteryStatsImpl.UserInfoProvider mUserManagerUserInfoProvider;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070097 private final Context mContext;
98 private final BatteryExternalStatsWorker mWorker;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070099
Bookatz50df7112017-08-04 14:53:26 -0700100 private native void getLowPowerStats(RpmStats rpmStats);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700101 private native int getPlatformLowPowerStats(ByteBuffer outBuffer);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000102 private native int getSubsystemLowPowerStats(ByteBuffer outBuffer);
Blake Kragtencb308d92019-01-28 10:54:13 -0800103 private native void getRailEnergyPowerStats(RailStats railStats);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700104 private CharsetDecoder mDecoderStat = StandardCharsets.UTF_8
105 .newDecoder()
106 .onMalformedInput(CodingErrorAction.REPLACE)
107 .onUnmappableCharacter(CodingErrorAction.REPLACE)
108 .replaceWith("?");
109 private ByteBuffer mUtf8BufferStat = ByteBuffer.allocateDirect(MAX_LOW_POWER_STATS_SIZE);
110 private CharBuffer mUtf16BufferStat = CharBuffer.allocate(MAX_LOW_POWER_STATS_SIZE);
Benjamin Schwartzcf7935e2019-07-01 11:15:24 -0700111 private static final int MAX_LOW_POWER_STATS_SIZE = 4096;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700112
Bookatz50df7112017-08-04 14:53:26 -0700113 /**
114 * Replaces the information in the given rpmStats with up-to-date information.
115 */
116 @Override
117 public void fillLowPowerStats(RpmStats rpmStats) {
118 if (DBG) Slog.d(TAG, "begin getLowPowerStats");
119 try {
120 getLowPowerStats(rpmStats);
121 } finally {
122 if (DBG) Slog.d(TAG, "end getLowPowerStats");
123 }
124 }
125
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700126 @Override
Blake Kragtencb308d92019-01-28 10:54:13 -0800127 public void fillRailDataStats(RailStats railStats) {
128 if (DBG) Slog.d(TAG, "begin getRailEnergyPowerStats");
129 try {
130 getRailEnergyPowerStats(railStats);
131 } finally {
132 if (DBG) Slog.d(TAG, "end getRailEnergyPowerStats");
133 }
134 }
135
136 @Override
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700137 public String getPlatformLowPowerStats() {
Joe Onorato82ba91d2017-04-27 16:18:05 -0700138 if (DBG) Slog.d(TAG, "begin getPlatformLowPowerStats");
Adam Lesinskicd722082017-03-30 13:54:09 -0700139 try {
140 mUtf8BufferStat.clear();
141 mUtf16BufferStat.clear();
142 mDecoderStat.reset();
143 int bytesWritten = getPlatformLowPowerStats(mUtf8BufferStat);
144 if (bytesWritten < 0) {
145 return null;
146 } else if (bytesWritten == 0) {
147 return "Empty";
148 }
149 mUtf8BufferStat.limit(bytesWritten);
150 mDecoderStat.decode(mUtf8BufferStat, mUtf16BufferStat, true);
151 mUtf16BufferStat.flip();
152 return mUtf16BufferStat.toString();
153 } finally {
Joe Onorato82ba91d2017-04-27 16:18:05 -0700154 if (DBG) Slog.d(TAG, "end getPlatformLowPowerStats");
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700155 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700156 }
157
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000158 @Override
159 public String getSubsystemLowPowerStats() {
Bookatz50df7112017-08-04 14:53:26 -0700160 if (DBG) Slog.d(TAG, "begin getSubsystemLowPowerStats");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000161 try {
162 mUtf8BufferStat.clear();
163 mUtf16BufferStat.clear();
164 mDecoderStat.reset();
165 int bytesWritten = getSubsystemLowPowerStats(mUtf8BufferStat);
166 if (bytesWritten < 0) {
167 return null;
168 } else if (bytesWritten == 0) {
169 return "Empty";
170 }
171 mUtf8BufferStat.limit(bytesWritten);
172 mDecoderStat.decode(mUtf8BufferStat, mUtf16BufferStat, true);
173 mUtf16BufferStat.flip();
174 return mUtf16BufferStat.toString();
175 } finally {
Bookatz50df7112017-08-04 14:53:26 -0700176 if (DBG) Slog.d(TAG, "end getSubsystemLowPowerStats");
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000177 }
178 }
179
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700180 BatteryStatsService(Context context, File systemDir, Handler handler) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700181 // BatteryStatsImpl expects the ActivityManagerService handler, so pass that one through.
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700182 mContext = context;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700183 mUserManagerUserInfoProvider = new BatteryStatsImpl.UserInfoProvider() {
184 private UserManagerInternal umi;
185 @Override
186 public int[] getUserIds() {
187 if (umi == null) {
188 umi = LocalServices.getService(UserManagerInternal.class);
189 }
190 return (umi != null) ? umi.getUserIds() : null;
191 }
192 };
Blake Kragtencb308d92019-01-28 10:54:13 -0800193 mStats = new BatteryStatsImpl(systemDir, handler, this,
194 this, mUserManagerUserInfoProvider);
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700195 mWorker = new BatteryExternalStatsWorker(context, mStats);
196 mStats.setExternalStatsSyncLocked(mWorker);
197 mStats.setRadioScanningTimeoutLocked(mContext.getResources().getInteger(
198 com.android.internal.R.integer.config_radioScanningTimeout) * 1000L);
199 mStats.setPowerProfileLocked(new PowerProfile(context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700201
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700202 public void publish() {
David Chenc8a43242017-10-17 16:23:28 -0700203 LocalServices.addService(BatteryStatsInternal.class, new LocalService());
Adam Lesinski6832f392015-09-05 18:05:40 -0700204 ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
Jeff Brown2c43c332014-06-12 22:38:59 -0700205 }
206
Sudheer Shanka5c19b892018-01-05 17:25:46 -0800207 public void systemServicesReady() {
208 mStats.systemServicesReady(mContext);
209 }
210
David Chenc8a43242017-10-17 16:23:28 -0700211 private final class LocalService extends BatteryStatsInternal {
212 @Override
213 public String[] getWifiIfaces() {
214 return mStats.getWifiIfaces().clone();
215 }
216
217 @Override
218 public String[] getMobileIfaces() {
219 return mStats.getMobileIfaces().clone();
220 }
Amith Yamasani977e11f2018-02-16 11:29:54 -0800221
222 @Override
223 public void noteJobsDeferred(int uid, int numDeferred, long sinceLast) {
224 if (DBG) Slog.d(TAG, "Jobs deferred " + uid + ": " + numDeferred + " " + sinceLast);
225 BatteryStatsService.this.noteJobsDeferred(uid, numDeferred, sinceLast);
226 }
David Chenc8a43242017-10-17 16:23:28 -0700227 }
228
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700229 private static void awaitUninterruptibly(Future<?> future) {
230 while (true) {
231 try {
232 future.get();
233 return;
234 } catch (ExecutionException e) {
235 return;
236 } catch (InterruptedException e) {
237 // Keep looping
238 }
239 }
240 }
241
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800242 private void syncStats(String reason, int flags) {
243 awaitUninterruptibly(mWorker.scheduleSync(reason, flags));
244 }
245
Jeff Brown2c43c332014-06-12 22:38:59 -0700246 /**
247 * At the time when the constructor runs, the power manager has not yet been
248 * initialized. So we initialize the low power observer later.
249 */
250 public void initPowerManagement() {
Adam Lesinski010bf372016-04-11 12:18:18 -0700251 final PowerManagerInternal powerMgr = LocalServices.getService(PowerManagerInternal.class);
252 powerMgr.registerLowPowerModeObserver(this);
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700253 synchronized (mStats) {
254 mStats.notePowerSaveModeLocked(
255 powerMgr.getLowPowerState(ServiceType.BATTERY_STATS)
256 .batterySaverEnabled);
257 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800258 (new WakeupReasonThread()).start();
Jeff Brown2c43c332014-06-12 22:38:59 -0700259 }
260
Dianne Hackborn55280a92009-05-07 15:53:46 -0700261 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800262 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700263
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800264 syncStats("shutdown", BatteryExternalStatsWorker.UPDATE_ALL);
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700265
Dianne Hackborn55280a92009-05-07 15:53:46 -0700266 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700267 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700268 }
Adam Lesinski20b84df2016-04-19 17:33:33 -0700269
270 // Shutdown the thread we made.
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700271 mWorker.shutdown();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700272 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static IBatteryStats getService() {
275 if (sService != null) {
276 return sService;
277 }
Dianne Hackborn91268cf2013-06-13 19:06:50 -0700278 IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 sService = asInterface(b);
280 return sService;
281 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700282
283 @Override
jackqdyulei455e90a2017-02-09 15:29:16 -0800284 public int getServiceType() {
285 return ServiceType.BATTERY_STATS;
286 }
287
288 @Override
289 public void onLowPowerModeChanged(PowerSaveState result) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700290 synchronized (mStats) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700291 mStats.notePowerSaveModeLocked(result.batterySaverEnabled);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700292 }
293 }
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * @return the current statistics object, which may be modified
297 * to reflect events that affect battery usage. You must lock the
298 * stats object before doing anything with it.
299 */
300 public BatteryStatsImpl getActiveStatistics() {
301 return mStats;
302 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700303
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700304 /**
305 * Schedules a write to disk to occur. This will cause the BatteryStatsImpl
306 * object to update with the latest info, then write to disk.
307 */
308 public void scheduleWriteToDisk() {
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700309 mWorker.scheduleWrite();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700310 }
311
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700312 // These are for direct use by the activity manager...
313
Adam Lesinskib83ffee2015-05-12 14:43:47 -0700314 /**
315 * Remove a UID from the BatteryStats and BatteryStats' external dependencies.
316 */
317 void removeUid(int uid) {
318 synchronized (mStats) {
319 mStats.removeUidStatsLocked(uid);
320 }
321 }
322
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700323 void onCleanupUser(int userId) {
324 synchronized (mStats) {
325 mStats.onCleanupUserLocked(userId);
326 }
327 }
328
329 void onUserRemoved(int userId) {
330 synchronized (mStats) {
331 mStats.onUserRemovedLocked(userId);
332 }
333 }
334
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700335 void addIsolatedUid(int isolatedUid, int appUid) {
336 synchronized (mStats) {
337 mStats.addIsolatedUidLocked(isolatedUid, appUid);
338 }
339 }
340
341 void removeIsolatedUid(int isolatedUid, int appUid) {
342 synchronized (mStats) {
Adam Lesinski61db88f2015-07-01 15:05:07 -0700343 mStats.scheduleRemoveIsolatedUidLocked(isolatedUid, appUid);
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700344 }
345 }
346
347 void noteProcessStart(String name, int uid) {
348 synchronized (mStats) {
349 mStats.noteProcessStartLocked(name, uid);
Bookatz90867622018-01-31 15:05:57 -0800350 StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name,
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800351 StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__STARTED);
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700352 }
353 }
354
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800355 void noteProcessCrash(String name, int uid) {
356 synchronized (mStats) {
357 mStats.noteProcessCrashLocked(name, uid);
Bookatz90867622018-01-31 15:05:57 -0800358 StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name,
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800359 StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__CRASHED);
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800360 }
361 }
362
363 void noteProcessAnr(String name, int uid) {
364 synchronized (mStats) {
365 mStats.noteProcessAnrLocked(name, uid);
366 }
367 }
368
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700369 void noteProcessFinish(String name, int uid) {
370 synchronized (mStats) {
371 mStats.noteProcessFinishLocked(name, uid);
Bookatz90867622018-01-31 15:05:57 -0800372 StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name,
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800373 StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__FINISHED);
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700374 }
375 }
376
Bookatzdb026a22018-01-10 19:01:56 -0800377 /** @param state Process state from ActivityManager.java. */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800378 void noteUidProcessState(int uid, int state) {
379 synchronized (mStats) {
Bookatzdb026a22018-01-10 19:01:56 -0800380 StatsLog.write(StatsLog.UID_PROCESS_STATE_CHANGED, uid,
381 ActivityManager.processStateAmToProto(state));
Yao Chend54f9dd2017-10-17 17:37:48 +0000382
Dianne Hackborna8d10942015-11-19 17:55:19 -0800383 mStats.noteUidProcessStateLocked(uid, state);
384 }
385 }
386
Dianne Hackborn2f1993e2014-09-26 11:28:01 -0700387 // Public interface...
388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 public byte[] getStatistics() {
390 mContext.enforceCallingPermission(
391 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800392 //Slog.i("foo", "SENDING BATTERY INFO:");
393 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 Parcel out = Parcel.obtain();
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800395 syncStats("get-stats", BatteryExternalStatsWorker.UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700396 synchronized (mStats) {
397 mStats.writeToParcel(out, 0);
398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 byte[] data = out.marshall();
400 out.recycle();
401 return data;
402 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700403
404 public ParcelFileDescriptor getStatisticsStream() {
405 mContext.enforceCallingPermission(
406 android.Manifest.permission.BATTERY_STATS, null);
407 //Slog.i("foo", "SENDING BATTERY INFO:");
408 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
409 Parcel out = Parcel.obtain();
Sudheer Shankab2f83c12017-11-13 19:25:01 -0800410 syncStats("get-stats", BatteryExternalStatsWorker.UPDATE_ALL);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700411 synchronized (mStats) {
412 mStats.writeToParcel(out, 0);
413 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700414 byte[] data = out.marshall();
Hui Yu0ed84f12018-06-20 19:07:56 -0700415 if (DBG) Slog.d(TAG, "getStatisticsStream parcel size is:" + data.length);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700416 out.recycle();
417 try {
418 return ParcelFileDescriptor.fromData(data, "battery-stats");
419 } catch (IOException e) {
420 Slog.w(TAG, "Unable to create shared memory", e);
421 return null;
422 }
423 }
424
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700425 public boolean isCharging() {
426 synchronized (mStats) {
427 return mStats.isCharging();
428 }
429 }
430
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700431 public long computeBatteryTimeRemaining() {
432 synchronized (mStats) {
433 long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
434 return time >= 0 ? (time/1000) : time;
435 }
436 }
437
438 public long computeChargeTimeRemaining() {
439 synchronized (mStats) {
440 long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime());
441 return time >= 0 ? (time/1000) : time;
442 }
443 }
444
Dianne Hackborn099bc622014-01-22 13:39:16 -0800445 public void noteEvent(int code, String name, int uid) {
446 enforceCallingPermission();
447 synchronized (mStats) {
448 mStats.noteEventLocked(code, name, uid);
449 }
450 }
451
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700452 public void noteSyncStart(String name, int uid) {
453 enforceCallingPermission();
454 synchronized (mStats) {
455 mStats.noteSyncStartLocked(name, uid);
Bookatz235343d2018-03-26 13:03:50 -0700456 StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, uid, null, name,
457 StatsLog.SYNC_STATE_CHANGED__STATE__ON);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700458 }
459 }
460
461 public void noteSyncFinish(String name, int uid) {
462 enforceCallingPermission();
463 synchronized (mStats) {
464 mStats.noteSyncFinishLocked(name, uid);
Bookatz235343d2018-03-26 13:03:50 -0700465 StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, uid, null, name,
466 StatsLog.SYNC_STATE_CHANGED__STATE__OFF);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700467 }
468 }
469
Ben Murdoch741e3352019-05-09 11:27:28 +0100470 /** A scheduled job was started. */
471 public void noteJobStart(String name, int uid, int standbyBucket, int jobid) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700472 enforceCallingPermission();
473 synchronized (mStats) {
474 mStats.noteJobStartLocked(name, uid);
Bookatz235343d2018-03-26 13:03:50 -0700475 StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, uid, null,
476 name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__STARTED,
Ben Murdoch741e3352019-05-09 11:27:28 +0100477 JobProtoEnums.STOP_REASON_UNKNOWN, standbyBucket, jobid);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700478 }
479 }
480
Ben Murdoch741e3352019-05-09 11:27:28 +0100481 /** A scheduled job was finished. */
482 public void noteJobFinish(String name, int uid, int stopReason, int standbyBucket, int jobid) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700483 enforceCallingPermission();
484 synchronized (mStats) {
Dianne Hackborn94326cb2017-06-28 16:17:20 -0700485 mStats.noteJobFinishLocked(name, uid, stopReason);
Bookatz235343d2018-03-26 13:03:50 -0700486 StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, uid, null,
487 name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__FINISHED,
Ben Murdoch741e3352019-05-09 11:27:28 +0100488 stopReason, standbyBucket, jobid);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700489 }
490 }
491
Amith Yamasani977e11f2018-02-16 11:29:54 -0800492 void noteJobsDeferred(int uid, int numDeferred, long sinceLast) {
493 // No need to enforce calling permission, as it is called from an internal interface
494 synchronized (mStats) {
495 mStats.noteJobsDeferredLocked(uid, numDeferred, sinceLast);
496 }
497 }
498
Narayan Kamath695cf722017-12-21 18:32:47 +0000499 public void noteWakupAlarm(String name, int uid, WorkSource workSource, String tag) {
Dianne Hackborn1e383822015-04-10 14:02:33 -0700500 enforceCallingPermission();
501 synchronized (mStats) {
Narayan Kamath695cf722017-12-21 18:32:47 +0000502 mStats.noteWakupAlarmLocked(name, uid, workSource, tag);
Dianne Hackborn1e383822015-04-10 14:02:33 -0700503 }
504 }
505
Narayan Kamath695cf722017-12-21 18:32:47 +0000506 public void noteAlarmStart(String name, WorkSource workSource, int uid) {
Dianne Hackborn1e383822015-04-10 14:02:33 -0700507 enforceCallingPermission();
508 synchronized (mStats) {
Narayan Kamath695cf722017-12-21 18:32:47 +0000509 mStats.noteAlarmStartLocked(name, workSource, uid);
510 }
511 }
512
513 public void noteAlarmFinish(String name, WorkSource workSource, int uid) {
514 enforceCallingPermission();
515 synchronized (mStats) {
516 mStats.noteAlarmFinishLocked(name, workSource, uid);
Dianne Hackborn1e383822015-04-10 14:02:33 -0700517 }
518 }
519
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800520 public void noteStartWakelock(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800521 boolean unimportantForLogging) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 enforceCallingPermission();
523 synchronized (mStats) {
Narayan Kamath81822022017-12-08 11:56:01 +0000524 mStats.noteStartWakeLocked(uid, pid, null, name, historyName, type,
525 unimportantForLogging, SystemClock.elapsedRealtime(),
526 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 }
529
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700530 public void noteStopWakelock(int uid, int pid, String name, String historyName, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 enforceCallingPermission();
532 synchronized (mStats) {
Narayan Kamath81822022017-12-08 11:56:01 +0000533 mStats.noteStopWakeLocked(uid, pid, null, name, historyName, type,
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700534 SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 }
537
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800538 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name,
539 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700540 enforceCallingPermission();
541 synchronized (mStats) {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800542 mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName,
543 type, unimportantForLogging);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700544 }
545 }
546
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700547 public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name,
548 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800549 String newHistoryName, int newType, boolean newUnimportantForLogging) {
550 enforceCallingPermission();
551 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700552 mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type,
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800553 newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging);
554 }
555 }
556
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700557 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName,
558 int type) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700559 enforceCallingPermission();
560 synchronized (mStats) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700561 mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700562 }
563 }
564
Narayan Kamath96a92562018-01-02 18:57:17 +0000565 @Override
Dianne Hackbornd0db6f02016-07-18 14:14:20 -0700566 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
567 enforceCallingPermission();
568 synchronized (mStats) {
569 mStats.noteLongPartialWakelockStart(name, historyName, uid);
570 }
571 }
572
Narayan Kamath96a92562018-01-02 18:57:17 +0000573 @Override
574 public void noteLongPartialWakelockStartFromSource(String name, String historyName,
575 WorkSource workSource) {
576 enforceCallingPermission();
577 synchronized (mStats) {
578 mStats.noteLongPartialWakelockStartFromSource(name, historyName, workSource);
579 }
580 }
581
582 @Override
Dianne Hackbornd0db6f02016-07-18 14:14:20 -0700583 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
584 enforceCallingPermission();
585 synchronized (mStats) {
586 mStats.noteLongPartialWakelockFinish(name, historyName, uid);
587 }
588 }
589
Narayan Kamath96a92562018-01-02 18:57:17 +0000590 @Override
591 public void noteLongPartialWakelockFinishFromSource(String name, String historyName,
592 WorkSource workSource) {
593 enforceCallingPermission();
594 synchronized (mStats) {
595 mStats.noteLongPartialWakelockFinishFromSource(name, historyName, workSource);
596 }
597 }
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public void noteStartSensor(int uid, int sensor) {
600 enforceCallingPermission();
601 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700602 mStats.noteStartSensorLocked(uid, sensor);
Bookatz235343d2018-03-26 13:03:50 -0700603 StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, uid, null, sensor,
604 StatsLog.SENSOR_STATE_CHANGED__STATE__ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 }
Chenjie Yu29fb1f72017-09-13 12:18:50 -0700607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public void noteStopSensor(int uid, int sensor) {
609 enforceCallingPermission();
610 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700611 mStats.noteStopSensorLocked(uid, sensor);
Bookatz235343d2018-03-26 13:03:50 -0700612 StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, uid, null,
613 sensor, StatsLog.SENSOR_STATE_CHANGED__STATE__OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615 }
Chenjie Yu29fb1f72017-09-13 12:18:50 -0700616
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800617 public void noteVibratorOn(int uid, long durationMillis) {
618 enforceCallingPermission();
619 synchronized (mStats) {
620 mStats.noteVibratorOnLocked(uid, durationMillis);
621 }
622 }
623
624 public void noteVibratorOff(int uid) {
625 enforceCallingPermission();
626 synchronized (mStats) {
627 mStats.noteVibratorOffLocked(uid);
628 }
629 }
630
Narayan Kamath32684dd2018-01-08 17:32:51 +0000631 @Override
632 public void noteGpsChanged(WorkSource oldWs, WorkSource newWs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 enforceCallingPermission();
634 synchronized (mStats) {
Narayan Kamath32684dd2018-01-08 17:32:51 +0000635 mStats.noteGpsChangedLocked(oldWs, newWs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637 }
Chenjie Yu29fb1f72017-09-13 12:18:50 -0700638
Siddharth Ray78ccaf52017-12-23 16:16:21 -0800639 public void noteGpsSignalQuality(int signalLevel) {
640 synchronized (mStats) {
641 mStats.noteGpsSignalQualityLocked(signalLevel);
642 }
643 }
644
Jeff Browne95c3cd2014-05-02 16:59:26 -0700645 public void noteScreenState(int state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 enforceCallingPermission();
Joe Onorato82ba91d2017-04-27 16:18:05 -0700647 if (DBG) Slog.d(TAG, "begin noteScreenState");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 synchronized (mStats) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000649 StatsLog.write(StatsLog.SCREEN_STATE_CHANGED, state);
650
651 mStats.noteScreenStateLocked(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
Joe Onorato82ba91d2017-04-27 16:18:05 -0700653 if (DBG) Slog.d(TAG, "end noteScreenState");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
Chenjie Yu29fb1f72017-09-13 12:18:50 -0700655
Dianne Hackborn617f8772009-03-31 15:04:46 -0700656 public void noteScreenBrightness(int brightness) {
657 enforceCallingPermission();
658 synchronized (mStats) {
Bookatzc1a050a2017-10-10 15:49:28 -0700659 StatsLog.write(StatsLog.SCREEN_BRIGHTNESS_CHANGED, brightness);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700660 mStats.noteScreenBrightnessLocked(brightness);
661 }
662 }
yroe03583e2017-09-20 22:51:26 -0700663
Dianne Hackborn617f8772009-03-31 15:04:46 -0700664 public void noteUserActivity(int uid, int event) {
665 enforceCallingPermission();
666 synchronized (mStats) {
667 mStats.noteUserActivityLocked(uid, event);
668 }
669 }
yroe03583e2017-09-20 22:51:26 -0700670
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700671 public void noteWakeUp(String reason, int reasonUid) {
672 enforceCallingPermission();
673 synchronized (mStats) {
674 mStats.noteWakeUpLocked(reason, reasonUid);
675 }
676 }
677
Jeff Browne95c3cd2014-05-02 16:59:26 -0700678 public void noteInteractive(boolean interactive) {
679 enforceCallingPermission();
680 synchronized (mStats) {
681 mStats.noteInteractiveLocked(interactive);
682 }
683 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800684
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800685 public void noteConnectivityChanged(int type, String extra) {
686 enforceCallingPermission();
687 synchronized (mStats) {
688 mStats.noteConnectivityChangedLocked(type, extra);
689 }
690 }
691
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -0700692 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800693 enforceCallingPermission();
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700694 final boolean update;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800695 synchronized (mStats) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700696 update = mStats.noteMobileRadioPowerStateLocked(powerState, timestampNs, uid);
697 }
698
699 if (update) {
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700700 mWorker.scheduleSync("modem-data", BatteryExternalStatsWorker.UPDATE_RADIO);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800701 }
702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 public void notePhoneOn() {
705 enforceCallingPermission();
706 synchronized (mStats) {
707 mStats.notePhoneOnLocked();
708 }
709 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 public void notePhoneOff() {
712 enforceCallingPermission();
713 synchronized (mStats) {
714 mStats.notePhoneOffLocked();
715 }
716 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700717
Wink Savillee9b06d72009-05-18 21:47:50 -0700718 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700719 enforceCallingPermission();
720 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700721 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700722 }
723 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700724
Dianne Hackborn627bba72009-03-24 22:32:56 -0700725 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
726 enforceCallingPermission();
727 synchronized (mStats) {
728 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
729 }
730 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700731
Amith Yamasanif37447b2009-10-08 18:28:01 -0700732 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700733 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800734 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700735 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800736 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700737 }
738 }
739
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700740 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700741 enforceCallingPermission();
742 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700743 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700744 }
Bookatzda1798c2018-12-13 14:16:00 -0800745 StatsLog.write(StatsLog.WIFI_ENABLED_STATE_CHANGED,
746 StatsLog.WIFI_ENABLED_STATE_CHANGED__STATE__ON);
The Android Open Source Project10592532009-03-18 17:39:46 -0700747 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700748
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700749 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700750 enforceCallingPermission();
751 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700752 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700753 }
Bookatzda1798c2018-12-13 14:16:00 -0800754 StatsLog.write(StatsLog.WIFI_ENABLED_STATE_CHANGED,
755 StatsLog.WIFI_ENABLED_STATE_CHANGED__STATE__OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700756 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700757
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700758 public void noteStartAudio(int uid) {
Ray Essick0fc2da02019-02-28 09:26:19 -0800759 enforceSelfOrCallingPermission(uid);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700760 synchronized (mStats) {
761 mStats.noteAudioOnLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700762 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, uid, null,
763 StatsLog.AUDIO_STATE_CHANGED__STATE__ON);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700764 }
765 }
766
767 public void noteStopAudio(int uid) {
Ray Essick0fc2da02019-02-28 09:26:19 -0800768 enforceSelfOrCallingPermission(uid);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700769 synchronized (mStats) {
770 mStats.noteAudioOffLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700771 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, uid, null,
772 StatsLog.AUDIO_STATE_CHANGED__STATE__OFF);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700773 }
774 }
775
776 public void noteStartVideo(int uid) {
Ray Essick0fc2da02019-02-28 09:26:19 -0800777 enforceSelfOrCallingPermission(uid);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700778 synchronized (mStats) {
779 mStats.noteVideoOnLocked(uid);
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800780 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, uid, null,
781 StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__ON);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700782 }
783 }
784
785 public void noteStopVideo(int uid) {
Ray Essick0fc2da02019-02-28 09:26:19 -0800786 enforceSelfOrCallingPermission(uid);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700787 synchronized (mStats) {
788 mStats.noteVideoOffLocked(uid);
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800789 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, uid,
790 null, StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__OFF);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700791 }
792 }
793
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700794 public void noteResetAudio() {
795 enforceCallingPermission();
796 synchronized (mStats) {
797 mStats.noteResetAudioLocked();
Bookatz235343d2018-03-26 13:03:50 -0700798 StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, -1, null,
799 StatsLog.AUDIO_STATE_CHANGED__STATE__RESET);
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700800 }
801 }
802
803 public void noteResetVideo() {
804 enforceCallingPermission();
805 synchronized (mStats) {
806 mStats.noteResetVideoLocked();
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800807 StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, -1, null,
808 StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__RESET);
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700809 }
810 }
811
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700812 public void noteFlashlightOn(int uid) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700813 enforceCallingPermission();
814 synchronized (mStats) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700815 mStats.noteFlashlightOnLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700816 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, uid, null,
817 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__ON);
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700818 }
819 }
820
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700821 public void noteFlashlightOff(int uid) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700822 enforceCallingPermission();
823 synchronized (mStats) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700824 mStats.noteFlashlightOffLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700825 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, uid, null,
826 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__OFF);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700827 }
828 }
829
830 public void noteStartCamera(int uid) {
831 enforceCallingPermission();
Joe Onorato82ba91d2017-04-27 16:18:05 -0700832 if (DBG) Slog.d(TAG, "begin noteStartCamera");
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700833 synchronized (mStats) {
834 mStats.noteCameraOnLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700835 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, uid, null,
836 StatsLog.CAMERA_STATE_CHANGED__STATE__ON);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700837 }
Joe Onorato82ba91d2017-04-27 16:18:05 -0700838 if (DBG) Slog.d(TAG, "end noteStartCamera");
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700839 }
840
841 public void noteStopCamera(int uid) {
842 enforceCallingPermission();
843 synchronized (mStats) {
844 mStats.noteCameraOffLocked(uid);
Bookatz235343d2018-03-26 13:03:50 -0700845 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, uid, null,
846 StatsLog.CAMERA_STATE_CHANGED__STATE__OFF);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700847 }
848 }
849
850 public void noteResetCamera() {
851 enforceCallingPermission();
852 synchronized (mStats) {
853 mStats.noteResetCameraLocked();
Bookatz235343d2018-03-26 13:03:50 -0700854 StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, -1, null,
855 StatsLog.CAMERA_STATE_CHANGED__STATE__RESET);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700856 }
857 }
858
859 public void noteResetFlashlight() {
860 enforceCallingPermission();
861 synchronized (mStats) {
862 mStats.noteResetFlashlightLocked();
Bookatz235343d2018-03-26 13:03:50 -0700863 StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, -1, null,
864 StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__RESET);
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700865 }
866 }
867
Adam Lesinskie08af192015-03-25 16:42:59 -0700868 @Override
Adam Lesinski5f056f62016-07-14 16:56:08 -0700869 public void noteWifiRadioPowerState(int powerState, long tsNanos, int uid) {
Adam Lesinskie08af192015-03-25 16:42:59 -0700870 enforceCallingPermission();
871
872 // There was a change in WiFi power state.
873 // Collect data now for the past activity.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700874 synchronized (mStats) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700875 if (mStats.isOnBattery()) {
Adam Lesinski06f46cb2015-06-23 13:42:53 -0700876 final String type = (powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH ||
877 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active"
878 : "inactive";
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700879 mWorker.scheduleSync("wifi-data: " + type, BatteryExternalStatsWorker.UPDATE_WIFI);
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700880 }
Adam Lesinski5f056f62016-07-14 16:56:08 -0700881 mStats.noteWifiRadioPowerState(powerState, tsNanos, uid);
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700882 }
Adam Lesinskie08af192015-03-25 16:42:59 -0700883 }
884
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700885 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700886 enforceCallingPermission();
887 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700888 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700889 }
Bookatzda1798c2018-12-13 14:16:00 -0800890 // TODO: Log WIFI_RUNNING_STATE_CHANGED in a better spot to include Hotspot too.
891 StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED,
892 ws, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__ON);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700893 }
894
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700895 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700896 enforceCallingPermission();
897 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700898 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
899 }
Bookatzda1798c2018-12-13 14:16:00 -0800900 StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED,
901 newWs, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__ON);
902 StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED,
903 oldWs, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__OFF);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700904 }
905
906 public void noteWifiStopped(WorkSource ws) {
907 enforceCallingPermission();
908 synchronized (mStats) {
909 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700910 }
Bookatzda1798c2018-12-13 14:16:00 -0800911 StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED,
912 ws, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__OFF);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700913 }
914
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800915 public void noteWifiState(int wifiState, String accessPoint) {
916 enforceCallingPermission();
917 synchronized (mStats) {
918 mStats.noteWifiStateLocked(wifiState, accessPoint);
919 }
920 }
921
Dianne Hackborn3251b902014-06-20 14:40:53 -0700922 public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) {
923 enforceCallingPermission();
924 synchronized (mStats) {
925 mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth);
926 }
927 }
928
929 public void noteWifiRssiChanged(int newRssi) {
930 enforceCallingPermission();
931 synchronized (mStats) {
932 mStats.noteWifiRssiChangedLocked(newRssi);
933 }
934 }
935
The Android Open Source Project10592532009-03-18 17:39:46 -0700936 public void noteFullWifiLockAcquired(int uid) {
937 enforceCallingPermission();
938 synchronized (mStats) {
939 mStats.noteFullWifiLockAcquiredLocked(uid);
940 }
941 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -0700942
The Android Open Source Project10592532009-03-18 17:39:46 -0700943 public void noteFullWifiLockReleased(int uid) {
944 enforceCallingPermission();
945 synchronized (mStats) {
946 mStats.noteFullWifiLockReleasedLocked(uid);
947 }
948 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700949
950 public void noteWifiScanStarted(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700951 enforceCallingPermission();
952 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700953 mStats.noteWifiScanStartedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700954 }
955 }
Nick Pelly6ccaa542012-06-15 15:22:47 -0700956
957 public void noteWifiScanStopped(int uid) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700958 enforceCallingPermission();
959 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700960 mStats.noteWifiScanStoppedLocked(uid);
The Android Open Source Project10592532009-03-18 17:39:46 -0700961 }
962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700964 public void noteWifiMulticastEnabled(int uid) {
965 enforceCallingPermission();
966 synchronized (mStats) {
967 mStats.noteWifiMulticastEnabledLocked(uid);
968 }
969 }
970
971 public void noteWifiMulticastDisabled(int uid) {
972 enforceCallingPermission();
973 synchronized (mStats) {
974 mStats.noteWifiMulticastDisabledLocked(uid);
975 }
976 }
977
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700978 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
979 enforceCallingPermission();
980 synchronized (mStats) {
981 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
982 }
983 }
984
985 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
986 enforceCallingPermission();
987 synchronized (mStats) {
988 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
989 }
990 }
991
Nick Pelly6ccaa542012-06-15 15:22:47 -0700992 public void noteWifiScanStartedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700993 enforceCallingPermission();
994 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -0700995 mStats.noteWifiScanStartedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700996 }
997 }
998
Nick Pelly6ccaa542012-06-15 15:22:47 -0700999 public void noteWifiScanStoppedFromSource(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001000 enforceCallingPermission();
1001 synchronized (mStats) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001002 mStats.noteWifiScanStoppedFromSourceLocked(ws);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07001003 }
1004 }
1005
Robert Greenwalta029ea12013-09-25 16:38:12 -07001006 public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) {
1007 enforceCallingPermission();
1008 synchronized (mStats) {
1009 mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph);
1010 }
1011 }
1012
1013 public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) {
1014 enforceCallingPermission();
1015 synchronized (mStats) {
1016 mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws);
1017 }
1018 }
1019
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001020 @Override
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001021 public void noteNetworkInterfaceType(String iface, int networkType) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001022 enforceCallingPermission();
1023 synchronized (mStats) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001024 mStats.noteNetworkInterfaceTypeLocked(iface, networkType);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001025 }
1026 }
1027
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001028 @Override
1029 public void noteNetworkStatsEnabled() {
1030 enforceCallingPermission();
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001031 // During device boot, qtaguid isn't enabled until after the inital
1032 // loading of battery stats. Now that they're enabled, take our initial
1033 // snapshot for future delta calculation.
1034 mWorker.scheduleSync("network-stats-enabled",
1035 BatteryExternalStatsWorker.UPDATE_RADIO | BatteryExternalStatsWorker.UPDATE_WIFI);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001036 }
1037
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001038 @Override
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001039 public void noteDeviceIdleMode(int mode, String activeReason, int activeUid) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001040 enforceCallingPermission();
1041 synchronized (mStats) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001042 mStats.noteDeviceIdleModeLocked(mode, activeReason, activeUid);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001043 }
1044 }
1045
Dianne Hackborn3accca02013-09-20 09:32:11 -07001046 public void notePackageInstalled(String pkgName, long versionCode) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001047 enforceCallingPermission();
1048 synchronized (mStats) {
1049 mStats.notePackageInstalledLocked(pkgName, versionCode);
1050 }
1051 }
1052
1053 public void notePackageUninstalled(String pkgName) {
1054 enforceCallingPermission();
1055 synchronized (mStats) {
1056 mStats.notePackageUninstalledLocked(pkgName);
1057 }
1058 }
1059
Adam Lesinski6771d622016-01-15 18:14:47 -08001060 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07001061 public void noteBleScanStarted(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski6771d622016-01-15 18:14:47 -08001062 enforceCallingPermission();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001063 synchronized (mStats) {
Bookatzb1f04f32017-05-19 13:57:32 -07001064 mStats.noteBluetoothScanStartedFromSourceLocked(ws, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001065 }
Adam Lesinski6771d622016-01-15 18:14:47 -08001066 }
1067
1068 @Override
Bookatz94c5a312017-07-11 16:49:17 -07001069 public void noteBleScanStopped(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski6771d622016-01-15 18:14:47 -08001070 enforceCallingPermission();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001071 synchronized (mStats) {
Bookatz94c5a312017-07-11 16:49:17 -07001072 mStats.noteBluetoothScanStoppedFromSourceLocked(ws, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001073 }
1074 }
1075
1076 @Override
1077 public void noteResetBleScan() {
1078 enforceCallingPermission();
1079 synchronized (mStats) {
1080 mStats.noteResetBluetoothScanLocked();
1081 }
Adam Lesinski6771d622016-01-15 18:14:47 -08001082 }
1083
Adam Lesinski010bf372016-04-11 12:18:18 -07001084 @Override
Bookatz4ebc0642017-05-11 12:21:19 -07001085 public void noteBleScanResults(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07001086 enforceCallingPermission();
1087 synchronized (mStats) {
Bookatz4ebc0642017-05-11 12:21:19 -07001088 mStats.noteBluetoothScanResultsFromSourceLocked(ws, numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07001089 }
1090 }
1091
1092 @Override
Adam Lesinski010bf372016-04-11 12:18:18 -07001093 public void noteWifiControllerActivity(WifiActivityEnergyInfo info) {
1094 enforceCallingPermission();
1095
1096 if (info == null || !info.isValid()) {
1097 Slog.e(TAG, "invalid wifi data given: " + info);
1098 return;
1099 }
1100
Adam Lesinski14ae39a2017-05-26 11:50:40 -07001101 mStats.updateWifiState(info);
Adam Lesinski010bf372016-04-11 12:18:18 -07001102 }
1103
1104 @Override
1105 public void noteBluetoothControllerActivity(BluetoothActivityEnergyInfo info) {
1106 enforceCallingPermission();
1107 if (info == null || !info.isValid()) {
1108 Slog.e(TAG, "invalid bluetooth data given: " + info);
1109 return;
1110 }
1111
Adam Lesinskic357e682018-02-28 12:55:39 -08001112 synchronized (mStats) {
1113 mStats.updateBluetoothStateLocked(info);
1114 }
Adam Lesinski010bf372016-04-11 12:18:18 -07001115 }
1116
1117 @Override
1118 public void noteModemControllerActivity(ModemActivityInfo info) {
1119 enforceCallingPermission();
1120
1121 if (info == null || !info.isValid()) {
1122 Slog.e(TAG, "invalid modem data given: " + info);
1123 return;
1124 }
1125
Adam Lesinski14ae39a2017-05-26 11:50:40 -07001126 mStats.updateMobileRadioState(info);
Adam Lesinski010bf372016-04-11 12:18:18 -07001127 }
1128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 public boolean isOnBattery() {
1130 return mStats.isOnBattery();
1131 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001132
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001133 @Override
1134 public void setBatteryState(final int status, final int health, final int plugType,
Adam Lesinski041d9172016-12-12 12:03:56 -08001135 final int level, final int temp, final int volt, final int chargeUAh,
1136 final int chargeFullUAh) {
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001137 enforceCallingPermission();
1138
1139 // BatteryService calls us here and we may update external state. It would be wrong
1140 // to block such a low level service like BatteryService on external stats like WiFi.
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001141 mWorker.scheduleRunnable(() -> {
1142 synchronized (mStats) {
Sudheer Shankac57729a2018-02-09 15:44:42 -08001143 final boolean onBattery = BatteryStatsImpl.isOnBattery(plugType, status);
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001144 if (mStats.isOnBattery() == onBattery) {
1145 // The battery state has not changed, so we don't need to sync external
1146 // stats immediately.
1147 mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt,
1148 chargeUAh, chargeFullUAh);
1149 return;
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001150 }
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001151 }
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001152
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001153 // Sync external stats first as the battery has changed states. If we don't sync
1154 // before changing the state, we may not collect the relevant data later.
1155 // Order here is guaranteed since we're scheduling from the same thread and we are
1156 // using a single threaded executor.
1157 mWorker.scheduleSync("battery-state", BatteryExternalStatsWorker.UPDATE_ALL);
1158 mWorker.scheduleRunnable(() -> {
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001159 synchronized (mStats) {
Adam Lesinski926969b2016-04-28 17:31:12 -07001160 mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt,
Adam Lesinski041d9172016-12-12 12:03:56 -08001161 chargeUAh, chargeFullUAh);
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001162 }
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001163 });
Adam Lesinskid7616ef2015-07-27 11:43:05 -07001164 });
Evan Millar633a1742009-04-02 16:36:33 -07001165 }
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 public long getAwakeTimeBattery() {
1168 mContext.enforceCallingOrSelfPermission(
1169 android.Manifest.permission.BATTERY_STATS, null);
1170 return mStats.getAwakeTimeBattery();
1171 }
1172
1173 public long getAwakeTimePlugged() {
1174 mContext.enforceCallingOrSelfPermission(
1175 android.Manifest.permission.BATTERY_STATS, null);
1176 return mStats.getAwakeTimePlugged();
1177 }
1178
1179 public void enforceCallingPermission() {
1180 if (Binder.getCallingPid() == Process.myPid()) {
1181 return;
1182 }
1183 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1184 Binder.getCallingPid(), Binder.getCallingUid(), null);
1185 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001186
Ray Essick0fc2da02019-02-28 09:26:19 -08001187 private void enforceSelfOrCallingPermission(int uid) {
1188 if (Binder.getCallingUid() == uid) {
1189 return;
1190 }
1191 enforceCallingPermission();
1192 }
1193
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001194 final class WakeupReasonThread extends Thread {
Adam Lesinski515702c2015-07-23 18:13:38 -07001195 private static final int MAX_REASON_SIZE = 512;
1196 private CharsetDecoder mDecoder;
1197 private ByteBuffer mUtf8Buffer;
1198 private CharBuffer mUtf16Buffer;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001199
1200 WakeupReasonThread() {
1201 super("BatteryStats_wakeupReason");
1202 }
1203
1204 public void run() {
1205 Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
1206
Adam Lesinski515702c2015-07-23 18:13:38 -07001207 mDecoder = StandardCharsets.UTF_8
1208 .newDecoder()
1209 .onMalformedInput(CodingErrorAction.REPLACE)
1210 .onUnmappableCharacter(CodingErrorAction.REPLACE)
1211 .replaceWith("?");
1212
1213 mUtf8Buffer = ByteBuffer.allocateDirect(MAX_REASON_SIZE);
1214 mUtf16Buffer = CharBuffer.allocate(MAX_REASON_SIZE);
1215
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001216 try {
Adam Lesinski515702c2015-07-23 18:13:38 -07001217 String reason;
1218 while ((reason = waitWakeup()) != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001219 synchronized (mStats) {
Adam Lesinski515702c2015-07-23 18:13:38 -07001220 mStats.noteWakeupReasonLocked(reason);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001221 }
1222 }
1223 } catch (RuntimeException e) {
1224 Slog.e(TAG, "Failure reading wakeup reasons", e);
1225 }
1226 }
Adam Lesinski515702c2015-07-23 18:13:38 -07001227
1228 private String waitWakeup() {
1229 mUtf8Buffer.clear();
1230 mUtf16Buffer.clear();
1231 mDecoder.reset();
1232
1233 int bytesWritten = nativeWaitWakeup(mUtf8Buffer);
1234 if (bytesWritten < 0) {
1235 return null;
1236 } else if (bytesWritten == 0) {
1237 return "unknown";
1238 }
1239
1240 // Set the buffer's limit to the number of bytes written.
1241 mUtf8Buffer.limit(bytesWritten);
1242
1243 // Decode the buffer from UTF-8 to UTF-16.
1244 // Unmappable characters will be replaced.
1245 mDecoder.decode(mUtf8Buffer, mUtf16Buffer, true);
1246 mUtf16Buffer.flip();
1247
1248 // Create a String from the UTF-16 buffer.
1249 return mUtf16Buffer.toString();
1250 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001251 }
1252
Adam Lesinski515702c2015-07-23 18:13:38 -07001253 private static native int nativeWaitWakeup(ByteBuffer outBuffer);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001254
Dianne Hackbornae384452011-06-28 12:33:48 -07001255 private void dumpHelp(PrintWriter pw) {
Dianne Hackborn91268cf2013-06-13 19:06:50 -07001256 pw.println("Battery stats (batterystats) dump options:");
Kweku Adams3d160912018-05-07 11:26:27 -07001257 pw.println(" [--checkin] [--proto] [--history] [--history-start] [--charged] [-c]");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001258 pw.println(" [--daily] [--reset] [--write] [--new-daily] [--read-daily] [-h] [<package.name>]");
Dianne Hackborn865b79b2015-08-18 17:33:00 -07001259 pw.println(" --checkin: generate output for a checkin report; will write (and clear) the");
1260 pw.println(" last old completed stats when they had been reset.");
Joe Onorato1476d322016-05-05 14:46:15 -07001261 pw.println(" -c: write the current stats in checkin format.");
Kweku Adams3d160912018-05-07 11:26:27 -07001262 pw.println(" --proto: write the current aggregate stats (without history) in proto format.");
Dianne Hackborn099bc622014-01-22 13:39:16 -08001263 pw.println(" --history: show only history data.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001264 pw.println(" --history-start <num>: show only history data starting at given time offset.");
Hui Yu0ed84f12018-06-20 19:07:56 -07001265 pw.println(" --history-create-events <num>: create <num> of battery history events.");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001266 pw.println(" --charged: only output data since last charged.");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001267 pw.println(" --daily: only output full daily data.");
Dianne Hackbornae384452011-06-28 12:33:48 -07001268 pw.println(" --reset: reset the stats, clearing all current data.");
1269 pw.println(" --write: force write current collected stats to disk.");
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001270 pw.println(" --new-daily: immediately create and write new daily stats record.");
1271 pw.println(" --read-daily: read-load last written daily stats.");
Sudheer Shanka5c19b892018-01-05 17:25:46 -08001272 pw.println(" --settings: dump the settings key/values related to batterystats");
Mike Ma234d1822018-03-13 18:53:21 -07001273 pw.println(" --cpu: dump cpu stats for debugging purpose");
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001274 pw.println(" <package.name>: optional name of package to filter output by.");
Dianne Hackbornfc064132014-06-02 12:42:12 -07001275 pw.println(" -h: print this help text.");
1276 pw.println("Battery stats (batterystats) commands:");
1277 pw.println(" enable|disable <option>");
1278 pw.println(" Enable or disable a running option. Option state is not saved across boots.");
1279 pw.println(" Options are:");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001280 pw.println(" full-history: include additional detailed events in battery history:");
Dianne Hackborn1e383822015-04-10 14:02:33 -07001281 pw.println(" wake_lock_in, alarms and proc events");
Dianne Hackbornfc064132014-06-02 12:42:12 -07001282 pw.println(" no-auto-reset: don't automatically reset stats when unplugged");
Amith Yamasani674c9bb2017-02-01 09:45:17 -08001283 pw.println(" pretend-screen-off: pretend the screen is off, even if screen state changes");
Dianne Hackbornae384452011-06-28 12:33:48 -07001284 }
1285
Sudheer Shanka5c19b892018-01-05 17:25:46 -08001286 private void dumpSettings(PrintWriter pw) {
1287 synchronized (mStats) {
1288 mStats.dumpConstantsLocked(pw);
1289 }
1290 }
1291
Mike Ma234d1822018-03-13 18:53:21 -07001292 private void dumpCpuStats(PrintWriter pw) {
1293 synchronized (mStats) {
1294 mStats.dumpCpuStatsLocked(pw);
1295 }
1296 }
1297
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001298 private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) {
1299 i++;
1300 if (i >= args.length) {
1301 pw.println("Missing option argument for " + (enable ? "--enable" : "--disable"));
1302 dumpHelp(pw);
1303 return -1;
1304 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001305 if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001306 synchronized (mStats) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001307 mStats.setRecordAllHistoryLocked(enable);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001308 }
Dianne Hackborn9a755432014-05-15 17:05:22 -07001309 } else if ("no-auto-reset".equals(args[i])) {
1310 synchronized (mStats) {
1311 mStats.setNoAutoReset(enable);
1312 }
Amith Yamasani674c9bb2017-02-01 09:45:17 -08001313 } else if ("pretend-screen-off".equals(args[i])) {
1314 synchronized (mStats) {
1315 mStats.setPretendScreenOff(enable);
1316 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001317 } else {
1318 pw.println("Unknown enable/disable option: " + args[i]);
1319 dumpHelp(pw);
1320 return -1;
1321 }
1322 return i;
1323 }
1324
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 @Override
1327 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkey6df866a2017-03-31 14:08:23 -06001328 if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;
Kenny Root3abd75b2011-09-29 11:00:41 -07001329
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001330 int flags = 0;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001331 boolean useCheckinFormat = false;
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001332 boolean toProto = false;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001333 boolean isRealCheckin = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001334 boolean noOutput = false;
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001335 boolean writeData = false;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001336 long historyStart = -1;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001337 int reqUid = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -08001338 if (args != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001339 for (int i=0; i<args.length; i++) {
1340 String arg = args[i];
Dianne Hackborne4a59512010-12-07 11:08:07 -08001341 if ("--checkin".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001342 useCheckinFormat = true;
1343 isRealCheckin = true;
Dianne Hackborn099bc622014-01-22 13:39:16 -08001344 } else if ("--history".equals(arg)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001345 flags |= BatteryStats.DUMP_HISTORY_ONLY;
1346 } else if ("--history-start".equals(arg)) {
1347 flags |= BatteryStats.DUMP_HISTORY_ONLY;
1348 i++;
1349 if (i >= args.length) {
1350 pw.println("Missing time argument for --history-since");
1351 dumpHelp(pw);
1352 return;
1353 }
Hui Yu0ed84f12018-06-20 19:07:56 -07001354 historyStart = ParseUtils.parseLong(args[i], 0);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001355 writeData = true;
Hui Yu0ed84f12018-06-20 19:07:56 -07001356 } else if ("--history-create-events".equals(arg)) {
1357 i++;
1358 if (i >= args.length) {
1359 pw.println("Missing events argument for --history-create-events");
1360 dumpHelp(pw);
1361 return;
1362 }
1363 final long events = ParseUtils.parseLong(args[i], 0);
1364 synchronized (mStats) {
1365 mStats.createFakeHistoryEvents(events);
1366 pw.println("Battery history create events started.");
1367 noOutput = true;
1368 }
Dianne Hackborn49021f52013-09-04 18:03:40 -07001369 } else if ("-c".equals(arg)) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001370 useCheckinFormat = true;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001371 flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001372 } else if ("--proto".equals(arg)) {
1373 toProto = true;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001374 } else if ("--charged".equals(arg)) {
1375 flags |= BatteryStats.DUMP_CHARGED_ONLY;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001376 } else if ("--daily".equals(arg)) {
1377 flags |= BatteryStats.DUMP_DAILY_ONLY;
Dianne Hackborne4a59512010-12-07 11:08:07 -08001378 } else if ("--reset".equals(arg)) {
1379 synchronized (mStats) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001380 mStats.resetAllStatsCmdLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -08001381 pw.println("Battery stats reset.");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001382 noOutput = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07001384 mWorker.scheduleSync("dump", BatteryExternalStatsWorker.UPDATE_ALL);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001385 } else if ("--write".equals(arg)) {
Sudheer Shankab2f83c12017-11-13 19:25:01 -08001386 syncStats("dump", BatteryExternalStatsWorker.UPDATE_ALL);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001387 synchronized (mStats) {
1388 mStats.writeSyncLocked();
1389 pw.println("Battery stats written.");
1390 noOutput = true;
1391 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001392 } else if ("--new-daily".equals(arg)) {
1393 synchronized (mStats) {
1394 mStats.recordDailyStatsLocked();
1395 pw.println("New daily stats written.");
1396 noOutput = true;
1397 }
1398 } else if ("--read-daily".equals(arg)) {
1399 synchronized (mStats) {
1400 mStats.readDailyStatsLocked();
1401 pw.println("Last daily stats read.");
1402 noOutput = true;
1403 }
Dianne Hackbornfc064132014-06-02 12:42:12 -07001404 } else if ("--enable".equals(arg) || "enable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001405 i = doEnableOrDisable(pw, i, args, true);
1406 if (i < 0) {
1407 return;
1408 }
1409 pw.println("Enabled: " + args[i]);
1410 return;
Dianne Hackbornfc064132014-06-02 12:42:12 -07001411 } else if ("--disable".equals(arg) || "disable".equals(arg)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001412 i = doEnableOrDisable(pw, i, args, false);
1413 if (i < 0) {
1414 return;
1415 }
1416 pw.println("Disabled: " + args[i]);
1417 return;
Dianne Hackbornae384452011-06-28 12:33:48 -07001418 } else if ("-h".equals(arg)) {
1419 dumpHelp(pw);
1420 return;
Sudheer Shanka5c19b892018-01-05 17:25:46 -08001421 } else if ("--settings".equals(arg)) {
1422 dumpSettings(pw);
1423 return;
Mike Ma234d1822018-03-13 18:53:21 -07001424 } else if ("--cpu".equals(arg)) {
1425 dumpCpuStats(pw);
1426 return;
Mike Lockwoode8174042011-08-16 12:53:43 -07001427 } else if ("-a".equals(arg)) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001428 flags |= BatteryStats.DUMP_VERBOSE;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001429 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001430 pw.println("Unknown option: " + arg);
Dianne Hackbornae384452011-06-28 12:33:48 -07001431 dumpHelp(pw);
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001432 return;
1433 } else {
1434 // Not an option, last argument must be a package name.
1435 try {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001436 reqUid = mContext.getPackageManager().getPackageUidAsUser(arg,
Dianne Hackborncbfd23e2013-06-11 14:26:53 -07001437 UserHandle.getCallingUserId());
1438 } catch (PackageManager.NameNotFoundException e) {
1439 pw.println("Unknown package: " + arg);
1440 dumpHelp(pw);
1441 return;
1442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001445 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001446 if (noOutput) {
1447 return;
1448 }
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001449
1450 long ident = Binder.clearCallingIdentity();
1451 try {
1452 if (BatteryStatsHelper.checkWifiOnly(mContext)) {
1453 flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
1454 }
1455 // Fetch data from external sources and update the BatteryStatsImpl object with them.
Sudheer Shankab2f83c12017-11-13 19:25:01 -08001456 syncStats("dump", BatteryExternalStatsWorker.UPDATE_ALL);
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001457 } finally {
1458 Binder.restoreCallingIdentity(ident);
Dianne Hackbornd953c532014-08-16 18:17:38 -07001459 }
Dianne Hackborn13a6a9b2015-04-22 18:03:59 -07001460
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07001461 if (reqUid >= 0) {
1462 // By default, if the caller is only interested in a specific package, then
1463 // we only dump the aggregated data since charged.
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001464 if ((flags&(BatteryStats.DUMP_HISTORY_ONLY|BatteryStats.DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackbornab4a81b2014-10-09 17:59:38 -07001465 flags |= BatteryStats.DUMP_CHARGED_ONLY;
1466 // Also if they are doing -c, we don't want history.
1467 flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY;
1468 }
1469 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001470
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001471 if (toProto) {
1472 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(
1473 PackageManager.MATCH_ANY_USER | PackageManager.MATCH_ALL);
1474 if (isRealCheckin) {
1475 // For a real checkin, first we want to prefer to use the last complete checkin
1476 // file if there is one.
1477 synchronized (mStats.mCheckinFile) {
1478 if (mStats.mCheckinFile.exists()) {
1479 try {
1480 byte[] raw = mStats.mCheckinFile.readFully();
1481 if (raw != null) {
1482 Parcel in = Parcel.obtain();
1483 in.unmarshall(raw, 0, raw.length);
1484 in.setDataPosition(0);
1485 BatteryStatsImpl checkinStats = new BatteryStatsImpl(
Blake Kragtencb308d92019-01-28 10:54:13 -08001486 null, mStats.mHandler, null, null,
1487 mUserManagerUserInfoProvider);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001488 checkinStats.readSummaryFromParcel(in);
1489 in.recycle();
Kweku Adams3d160912018-05-07 11:26:27 -07001490 checkinStats.dumpProtoLocked(
1491 mContext, fd, apps, flags, historyStart);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001492 mStats.mCheckinFile.delete();
1493 return;
1494 }
1495 } catch (IOException | ParcelFormatException e) {
1496 Slog.w(TAG, "Failure reading checkin file "
1497 + mStats.mCheckinFile.getBaseFile(), e);
1498 }
1499 }
1500 }
1501 }
1502 if (DBG) Slog.d(TAG, "begin dumpProtoLocked from UID " + Binder.getCallingUid());
1503 synchronized (mStats) {
Kweku Adams3d160912018-05-07 11:26:27 -07001504 mStats.dumpProtoLocked(mContext, fd, apps, flags, historyStart);
Kweku Adams2f73ecd2017-09-27 16:59:19 -07001505 if (writeData) {
1506 mStats.writeAsyncLocked();
1507 }
1508 }
1509 if (DBG) Slog.d(TAG, "end dumpProtoLocked");
1510 } else if (useCheckinFormat) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07001511 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001512 PackageManager.MATCH_ANY_USER | PackageManager.MATCH_ALL);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001513 if (isRealCheckin) {
1514 // For a real checkin, first we want to prefer to use the last complete checkin
1515 // file if there is one.
1516 synchronized (mStats.mCheckinFile) {
1517 if (mStats.mCheckinFile.exists()) {
1518 try {
1519 byte[] raw = mStats.mCheckinFile.readFully();
1520 if (raw != null) {
1521 Parcel in = Parcel.obtain();
1522 in.unmarshall(raw, 0, raw.length);
1523 in.setDataPosition(0);
1524 BatteryStatsImpl checkinStats = new BatteryStatsImpl(
Blake Kragtencb308d92019-01-28 10:54:13 -08001525 null, mStats.mHandler, null, null,
1526 mUserManagerUserInfoProvider);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001527 checkinStats.readSummaryFromParcel(in);
1528 in.recycle();
1529 checkinStats.dumpCheckinLocked(mContext, pw, apps, flags,
1530 historyStart);
1531 mStats.mCheckinFile.delete();
1532 return;
1533 }
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001534 } catch (IOException | ParcelFormatException e) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07001535 Slog.w(TAG, "Failure reading checkin file "
1536 + mStats.mCheckinFile.getBaseFile(), e);
1537 }
1538 }
1539 }
1540 }
Joe Onorato82ba91d2017-04-27 16:18:05 -07001541 if (DBG) Slog.d(TAG, "begin dumpCheckinLocked from UID " + Binder.getCallingUid());
Dianne Hackborne4a59512010-12-07 11:08:07 -08001542 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001543 mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001544 if (writeData) {
1545 mStats.writeAsyncLocked();
1546 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001547 }
Joe Onorato82ba91d2017-04-27 16:18:05 -07001548 if (DBG) Slog.d(TAG, "end dumpCheckinLocked");
Dianne Hackborne4a59512010-12-07 11:08:07 -08001549 } else {
Joe Onorato82ba91d2017-04-27 16:18:05 -07001550 if (DBG) Slog.d(TAG, "begin dumpLocked from UID " + Binder.getCallingUid());
Dianne Hackborne4a59512010-12-07 11:08:07 -08001551 synchronized (mStats) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001552 mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
Dianne Hackborn16b0b562014-06-03 17:24:42 -07001553 if (writeData) {
1554 mStats.writeAsyncLocked();
1555 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08001556 }
Joe Onorato82ba91d2017-04-27 16:18:05 -07001557 if (DBG) Slog.d(TAG, "end dumpLocked");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07001560
Joe Onorato713fec82016-03-04 10:34:02 -08001561 /**
Siddharth Raya1fd0572017-11-13 14:20:47 -08001562 * Gets a snapshot of cellular stats
1563 * @hide
1564 */
1565 public CellularBatteryStats getCellularBatteryStats() {
1566 synchronized (mStats) {
1567 return mStats.getCellularBatteryStats();
1568 }
1569 }
1570
1571 /**
Siddharth Rayb50a6842017-12-14 15:15:28 -08001572 * Gets a snapshot of Wifi stats
1573 * @hide
1574 */
1575 public WifiBatteryStats getWifiBatteryStats() {
1576 synchronized (mStats) {
1577 return mStats.getWifiBatteryStats();
1578 }
1579 }
1580
1581 /**
Siddharth Ray78ccaf52017-12-23 16:16:21 -08001582 * Gets a snapshot of Gps stats
1583 * @hide
1584 */
1585 public GpsBatteryStats getGpsBatteryStats() {
1586 synchronized (mStats) {
1587 return mStats.getGpsBatteryStats();
1588 }
1589 }
1590
1591 /**
Joe Onorato713fec82016-03-04 10:34:02 -08001592 * Gets a snapshot of the system health for a particular uid.
1593 */
1594 @Override
1595 public HealthStatsParceler takeUidSnapshot(int requestUid) {
1596 if (requestUid != Binder.getCallingUid()) {
1597 mContext.enforceCallingOrSelfPermission(
1598 android.Manifest.permission.BATTERY_STATS, null);
1599 }
1600 long ident = Binder.clearCallingIdentity();
1601 try {
Sudheer Shankae56013a2018-04-23 11:22:15 -07001602 if (shouldCollectExternalStats()) {
1603 syncStats("get-health-stats-for-uids", BatteryExternalStatsWorker.UPDATE_ALL);
1604 }
Joe Onorato713fec82016-03-04 10:34:02 -08001605 synchronized (mStats) {
1606 return getHealthStatsForUidLocked(requestUid);
1607 }
1608 } catch (Exception ex) {
Joe Onorato82ba91d2017-04-27 16:18:05 -07001609 Slog.w(TAG, "Crashed while writing for takeUidSnapshot(" + requestUid + ")", ex);
Joe Onorato713fec82016-03-04 10:34:02 -08001610 throw ex;
1611 } finally {
1612 Binder.restoreCallingIdentity(ident);
1613 }
1614 }
1615
1616 /**
1617 * Gets a snapshot of the system health for a number of uids.
1618 */
1619 @Override
1620 public HealthStatsParceler[] takeUidSnapshots(int[] requestUids) {
1621 if (!onlyCaller(requestUids)) {
1622 mContext.enforceCallingOrSelfPermission(
1623 android.Manifest.permission.BATTERY_STATS, null);
1624 }
1625 long ident = Binder.clearCallingIdentity();
1626 int i=-1;
1627 try {
Sudheer Shankae56013a2018-04-23 11:22:15 -07001628 if (shouldCollectExternalStats()) {
1629 syncStats("get-health-stats-for-uids", BatteryExternalStatsWorker.UPDATE_ALL);
1630 }
Joe Onorato713fec82016-03-04 10:34:02 -08001631 synchronized (mStats) {
1632 final int N = requestUids.length;
1633 final HealthStatsParceler[] results = new HealthStatsParceler[N];
1634 for (i=0; i<N; i++) {
1635 results[i] = getHealthStatsForUidLocked(requestUids[i]);
1636 }
1637 return results;
1638 }
1639 } catch (Exception ex) {
Joe Onorato82ba91d2017-04-27 16:18:05 -07001640 if (DBG) Slog.d(TAG, "Crashed while writing for takeUidSnapshots("
Joe Onorato713fec82016-03-04 10:34:02 -08001641 + Arrays.toString(requestUids) + ") i=" + i, ex);
1642 throw ex;
1643 } finally {
1644 Binder.restoreCallingIdentity(ident);
1645 }
1646 }
1647
Sudheer Shankae56013a2018-04-23 11:22:15 -07001648 private boolean shouldCollectExternalStats() {
1649 return (SystemClock.elapsedRealtime() - mWorker.getLastCollectionTimeStamp())
1650 > mStats.getExternalStatsCollectionRateLimitMs();
1651 }
1652
Joe Onorato713fec82016-03-04 10:34:02 -08001653 /**
1654 * Returns whether the Binder.getCallingUid is the only thing in requestUids.
1655 */
1656 private static boolean onlyCaller(int[] requestUids) {
1657 final int caller = Binder.getCallingUid();
1658 final int N = requestUids.length;
1659 for (int i=0; i<N; i++) {
1660 if (requestUids[i] != caller) {
1661 return false;
1662 }
1663 }
1664 return true;
1665 }
1666
1667 /**
1668 * Gets a HealthStatsParceler for the given uid. You should probably call
Adam Lesinski010bf372016-04-11 12:18:18 -07001669 * updateExternalStatsSync first.
Joe Onorato713fec82016-03-04 10:34:02 -08001670 */
1671 HealthStatsParceler getHealthStatsForUidLocked(int requestUid) {
1672 final HealthStatsBatteryStatsWriter writer = new HealthStatsBatteryStatsWriter();
1673 final HealthStatsWriter uidWriter = new HealthStatsWriter(UidHealthStats.CONSTANTS);
1674 final BatteryStats.Uid uid = mStats.getUidStats().get(requestUid);
1675 if (uid != null) {
1676 writer.writeUid(uidWriter, mStats, uid);
1677 }
1678 return new HealthStatsParceler(uidWriter);
1679 }
1680
Ng Zhi An09411202019-01-23 11:46:50 -08001681 /**
1682 * Delay for sending ACTION_CHARGING after device is plugged in.
1683 *
1684 * @hide
1685 */
1686 public boolean setChargingStateUpdateDelayMillis(int delayMillis) {
1687 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER, null);
1688 final long ident = Binder.clearCallingIdentity();
1689
1690 try {
1691 final ContentResolver contentResolver = mContext.getContentResolver();
1692 return Settings.Global.putLong(contentResolver,
1693 Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY,
1694 delayMillis);
1695 } finally {
1696 Binder.restoreCallingIdentity(ident);
1697 }
1698 }
1699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700}