The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Bookatz | db026a2 | 2018-01-10 19:01:56 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 20 | import android.app.job.JobProtoEnums; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothActivityEnergyInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 25 | import android.net.wifi.WifiActivityEnergyInfo; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 26 | import android.os.BatteryStats; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 27 | import android.os.BatteryStatsInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.Binder; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.IBinder; |
| 31 | import android.os.Parcel; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 32 | import android.os.ParcelFileDescriptor; |
Adam Lesinski | 9ae9cba | 2015-07-08 17:09:34 -0700 | [diff] [blame] | 33 | import android.os.ParcelFormatException; |
Mike Ma | fbc01fc | 2018-04-02 10:28:28 -0700 | [diff] [blame] | 34 | import android.os.PowerManager.ServiceType; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 35 | import android.os.PowerManagerInternal; |
Mike Ma | fbc01fc | 2018-04-02 10:28:28 -0700 | [diff] [blame] | 36 | import android.os.PowerSaveState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.Process; |
| 38 | import android.os.ServiceManager; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 39 | import android.os.SystemClock; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 40 | import android.os.UserHandle; |
Suprabh Shukla | e6e723d | 2017-06-14 16:14:43 -0700 | [diff] [blame] | 41 | import android.os.UserManagerInternal; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 42 | import android.os.WorkSource; |
Siddharth Ray | a1fd057 | 2017-11-13 14:20:47 -0800 | [diff] [blame] | 43 | import android.os.connectivity.CellularBatteryStats; |
Siddharth Ray | 78ccaf5 | 2017-12-23 16:16:21 -0800 | [diff] [blame] | 44 | import android.os.connectivity.GpsBatteryStats; |
Mike Ma | fbc01fc | 2018-04-02 10:28:28 -0700 | [diff] [blame] | 45 | import android.os.connectivity.WifiBatteryStats; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 46 | import android.os.health.HealthStatsParceler; |
| 47 | import android.os.health.HealthStatsWriter; |
| 48 | import android.os.health.UidHealthStats; |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 49 | import android.telephony.DataConnectionRealTimeInfo; |
Adam Lesinski | 21f76aa | 2016-01-25 12:27:06 -0800 | [diff] [blame] | 50 | import android.telephony.ModemActivityInfo; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 51 | import android.telephony.SignalStrength; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 52 | import android.telephony.TelephonyManager; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 53 | import android.util.Slog; |
Mike Ma | fbc01fc | 2018-04-02 10:28:28 -0700 | [diff] [blame] | 54 | import android.util.StatsLog; |
Amith Yamasani | 674c9bb | 2017-02-01 09:45:17 -0800 | [diff] [blame] | 55 | |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 56 | import com.android.internal.app.IBatteryStats; |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 57 | import com.android.internal.os.BatteryStatsHelper; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 58 | import com.android.internal.os.BatteryStatsImpl; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 59 | import com.android.internal.os.PowerProfile; |
Bookatz | 50df711 | 2017-08-04 14:53:26 -0700 | [diff] [blame] | 60 | import com.android.internal.os.RpmStats; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 61 | import com.android.internal.util.DumpUtils; |
Hui Yu | 0ed84f1 | 2018-06-20 19:07:56 -0700 | [diff] [blame] | 62 | import com.android.internal.util.ParseUtils; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 63 | import com.android.server.LocalServices; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 64 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 65 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 67 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import java.io.PrintWriter; |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 69 | import java.nio.ByteBuffer; |
| 70 | import java.nio.CharBuffer; |
| 71 | import java.nio.charset.CharsetDecoder; |
| 72 | import java.nio.charset.CodingErrorAction; |
| 73 | import java.nio.charset.StandardCharsets; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 74 | import java.util.Arrays; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 75 | import java.util.List; |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 76 | import java.util.concurrent.ExecutionException; |
| 77 | import java.util.concurrent.Future; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * All information we are collecting about things that can happen that impact |
| 81 | * battery life. |
| 82 | */ |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 83 | public final class BatteryStatsService extends IBatteryStats.Stub |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 84 | implements PowerManagerInternal.LowPowerModeListener, |
| 85 | BatteryStatsImpl.PlatformIdleStateCallback { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 86 | static final String TAG = "BatteryStatsService"; |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 87 | static final boolean DBG = false; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 89 | private static IBatteryStats sService; |
| 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | final BatteryStatsImpl mStats; |
Suprabh Shukla | e6e723d | 2017-06-14 16:14:43 -0700 | [diff] [blame] | 92 | private final BatteryStatsImpl.UserInfoProvider mUserManagerUserInfoProvider; |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 93 | private final Context mContext; |
| 94 | private final BatteryExternalStatsWorker mWorker; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 95 | |
Bookatz | 50df711 | 2017-08-04 14:53:26 -0700 | [diff] [blame] | 96 | private native void getLowPowerStats(RpmStats rpmStats); |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 97 | private native int getPlatformLowPowerStats(ByteBuffer outBuffer); |
Ahmed ElArabawy | d8b4411 | 2017-05-23 21:25:02 +0000 | [diff] [blame] | 98 | private native int getSubsystemLowPowerStats(ByteBuffer outBuffer); |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 99 | private CharsetDecoder mDecoderStat = StandardCharsets.UTF_8 |
| 100 | .newDecoder() |
| 101 | .onMalformedInput(CodingErrorAction.REPLACE) |
| 102 | .onUnmappableCharacter(CodingErrorAction.REPLACE) |
| 103 | .replaceWith("?"); |
| 104 | private ByteBuffer mUtf8BufferStat = ByteBuffer.allocateDirect(MAX_LOW_POWER_STATS_SIZE); |
| 105 | private CharBuffer mUtf16BufferStat = CharBuffer.allocate(MAX_LOW_POWER_STATS_SIZE); |
Kelly Rossmoyer | 03ad13e | 2018-03-13 13:10:14 -0700 | [diff] [blame] | 106 | private static final int MAX_LOW_POWER_STATS_SIZE = 2048; |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 107 | |
Bookatz | 50df711 | 2017-08-04 14:53:26 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Replaces the information in the given rpmStats with up-to-date information. |
| 110 | */ |
| 111 | @Override |
| 112 | public void fillLowPowerStats(RpmStats rpmStats) { |
| 113 | if (DBG) Slog.d(TAG, "begin getLowPowerStats"); |
| 114 | try { |
| 115 | getLowPowerStats(rpmStats); |
| 116 | } finally { |
| 117 | if (DBG) Slog.d(TAG, "end getLowPowerStats"); |
| 118 | } |
| 119 | } |
| 120 | |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public String getPlatformLowPowerStats() { |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 123 | if (DBG) Slog.d(TAG, "begin getPlatformLowPowerStats"); |
Adam Lesinski | cd72208 | 2017-03-30 13:54:09 -0700 | [diff] [blame] | 124 | try { |
| 125 | mUtf8BufferStat.clear(); |
| 126 | mUtf16BufferStat.clear(); |
| 127 | mDecoderStat.reset(); |
| 128 | int bytesWritten = getPlatformLowPowerStats(mUtf8BufferStat); |
| 129 | if (bytesWritten < 0) { |
| 130 | return null; |
| 131 | } else if (bytesWritten == 0) { |
| 132 | return "Empty"; |
| 133 | } |
| 134 | mUtf8BufferStat.limit(bytesWritten); |
| 135 | mDecoderStat.decode(mUtf8BufferStat, mUtf16BufferStat, true); |
| 136 | mUtf16BufferStat.flip(); |
| 137 | return mUtf16BufferStat.toString(); |
| 138 | } finally { |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 139 | if (DBG) Slog.d(TAG, "end getPlatformLowPowerStats"); |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 140 | } |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ahmed ElArabawy | d8b4411 | 2017-05-23 21:25:02 +0000 | [diff] [blame] | 143 | @Override |
| 144 | public String getSubsystemLowPowerStats() { |
Bookatz | 50df711 | 2017-08-04 14:53:26 -0700 | [diff] [blame] | 145 | if (DBG) Slog.d(TAG, "begin getSubsystemLowPowerStats"); |
Ahmed ElArabawy | d8b4411 | 2017-05-23 21:25:02 +0000 | [diff] [blame] | 146 | try { |
| 147 | mUtf8BufferStat.clear(); |
| 148 | mUtf16BufferStat.clear(); |
| 149 | mDecoderStat.reset(); |
| 150 | int bytesWritten = getSubsystemLowPowerStats(mUtf8BufferStat); |
| 151 | if (bytesWritten < 0) { |
| 152 | return null; |
| 153 | } else if (bytesWritten == 0) { |
| 154 | return "Empty"; |
| 155 | } |
| 156 | mUtf8BufferStat.limit(bytesWritten); |
| 157 | mDecoderStat.decode(mUtf8BufferStat, mUtf16BufferStat, true); |
| 158 | mUtf16BufferStat.flip(); |
| 159 | return mUtf16BufferStat.toString(); |
| 160 | } finally { |
Bookatz | 50df711 | 2017-08-04 14:53:26 -0700 | [diff] [blame] | 161 | if (DBG) Slog.d(TAG, "end getSubsystemLowPowerStats"); |
Ahmed ElArabawy | d8b4411 | 2017-05-23 21:25:02 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 165 | BatteryStatsService(Context context, File systemDir, Handler handler) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 166 | // BatteryStatsImpl expects the ActivityManagerService handler, so pass that one through. |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 167 | mContext = context; |
Suprabh Shukla | e6e723d | 2017-06-14 16:14:43 -0700 | [diff] [blame] | 168 | mUserManagerUserInfoProvider = new BatteryStatsImpl.UserInfoProvider() { |
| 169 | private UserManagerInternal umi; |
| 170 | @Override |
| 171 | public int[] getUserIds() { |
| 172 | if (umi == null) { |
| 173 | umi = LocalServices.getService(UserManagerInternal.class); |
| 174 | } |
| 175 | return (umi != null) ? umi.getUserIds() : null; |
| 176 | } |
| 177 | }; |
| 178 | mStats = new BatteryStatsImpl(systemDir, handler, this, mUserManagerUserInfoProvider); |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 179 | mWorker = new BatteryExternalStatsWorker(context, mStats); |
| 180 | mStats.setExternalStatsSyncLocked(mWorker); |
| 181 | mStats.setRadioScanningTimeoutLocked(mContext.getResources().getInteger( |
| 182 | com.android.internal.R.integer.config_radioScanningTimeout) * 1000L); |
| 183 | mStats.setPowerProfileLocked(new PowerProfile(context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } |
Badhri Jagan Sridharan | 68cdf19 | 2016-04-03 21:57:15 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 186 | public void publish() { |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 187 | LocalServices.addService(BatteryStatsInternal.class, new LocalService()); |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 188 | ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder()); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Sudheer Shanka | 5c19b89 | 2018-01-05 17:25:46 -0800 | [diff] [blame] | 191 | public void systemServicesReady() { |
| 192 | mStats.systemServicesReady(mContext); |
| 193 | } |
| 194 | |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 195 | private final class LocalService extends BatteryStatsInternal { |
| 196 | @Override |
| 197 | public String[] getWifiIfaces() { |
| 198 | return mStats.getWifiIfaces().clone(); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public String[] getMobileIfaces() { |
| 203 | return mStats.getMobileIfaces().clone(); |
| 204 | } |
Amith Yamasani | 977e11f | 2018-02-16 11:29:54 -0800 | [diff] [blame] | 205 | |
| 206 | @Override |
| 207 | public void noteJobsDeferred(int uid, int numDeferred, long sinceLast) { |
| 208 | if (DBG) Slog.d(TAG, "Jobs deferred " + uid + ": " + numDeferred + " " + sinceLast); |
| 209 | BatteryStatsService.this.noteJobsDeferred(uid, numDeferred, sinceLast); |
| 210 | } |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 213 | private static void awaitUninterruptibly(Future<?> future) { |
| 214 | while (true) { |
| 215 | try { |
| 216 | future.get(); |
| 217 | return; |
| 218 | } catch (ExecutionException e) { |
| 219 | return; |
| 220 | } catch (InterruptedException e) { |
| 221 | // Keep looping |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 226 | private void syncStats(String reason, int flags) { |
| 227 | awaitUninterruptibly(mWorker.scheduleSync(reason, flags)); |
| 228 | } |
| 229 | |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 230 | /** |
| 231 | * At the time when the constructor runs, the power manager has not yet been |
| 232 | * initialized. So we initialize the low power observer later. |
| 233 | */ |
| 234 | public void initPowerManagement() { |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 235 | final PowerManagerInternal powerMgr = LocalServices.getService(PowerManagerInternal.class); |
| 236 | powerMgr.registerLowPowerModeObserver(this); |
Adam Lesinski | 14ae39a | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 237 | synchronized (mStats) { |
| 238 | mStats.notePowerSaveModeLocked( |
| 239 | powerMgr.getLowPowerState(ServiceType.BATTERY_STATS) |
| 240 | .batterySaverEnabled); |
| 241 | } |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 242 | (new WakeupReasonThread()).start(); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 245 | public void shutdown() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 246 | Slog.w("BatteryStats", "Writing battery stats before shutdown..."); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 247 | |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 248 | syncStats("shutdown", BatteryExternalStatsWorker.UPDATE_ALL); |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 249 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 250 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 251 | mStats.shutdownLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 252 | } |
Adam Lesinski | 20b84df | 2016-04-19 17:33:33 -0700 | [diff] [blame] | 253 | |
| 254 | // Shutdown the thread we made. |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 255 | mWorker.shutdown(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 256 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | public static IBatteryStats getService() { |
| 259 | if (sService != null) { |
| 260 | return sService; |
| 261 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 262 | IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | sService = asInterface(b); |
| 264 | return sService; |
| 265 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 266 | |
| 267 | @Override |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 268 | public int getServiceType() { |
| 269 | return ServiceType.BATTERY_STATS; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onLowPowerModeChanged(PowerSaveState result) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 274 | synchronized (mStats) { |
Adam Lesinski | 14ae39a | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 275 | mStats.notePowerSaveModeLocked(result.batterySaverEnabled); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | /** |
| 280 | * @return the current statistics object, which may be modified |
| 281 | * to reflect events that affect battery usage. You must lock the |
| 282 | * stats object before doing anything with it. |
| 283 | */ |
| 284 | public BatteryStatsImpl getActiveStatistics() { |
| 285 | return mStats; |
| 286 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 287 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Schedules a write to disk to occur. This will cause the BatteryStatsImpl |
| 290 | * object to update with the latest info, then write to disk. |
| 291 | */ |
| 292 | public void scheduleWriteToDisk() { |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 293 | mWorker.scheduleWrite(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 296 | // These are for direct use by the activity manager... |
| 297 | |
Adam Lesinski | b83ffee | 2015-05-12 14:43:47 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Remove a UID from the BatteryStats and BatteryStats' external dependencies. |
| 300 | */ |
| 301 | void removeUid(int uid) { |
| 302 | synchronized (mStats) { |
| 303 | mStats.removeUidStatsLocked(uid); |
| 304 | } |
| 305 | } |
| 306 | |
Suprabh Shukla | e6e723d | 2017-06-14 16:14:43 -0700 | [diff] [blame] | 307 | void onCleanupUser(int userId) { |
| 308 | synchronized (mStats) { |
| 309 | mStats.onCleanupUserLocked(userId); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void onUserRemoved(int userId) { |
| 314 | synchronized (mStats) { |
| 315 | mStats.onUserRemovedLocked(userId); |
| 316 | } |
| 317 | } |
| 318 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 319 | void addIsolatedUid(int isolatedUid, int appUid) { |
| 320 | synchronized (mStats) { |
| 321 | mStats.addIsolatedUidLocked(isolatedUid, appUid); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void removeIsolatedUid(int isolatedUid, int appUid) { |
| 326 | synchronized (mStats) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 327 | mStats.scheduleRemoveIsolatedUidLocked(isolatedUid, appUid); |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | void noteProcessStart(String name, int uid) { |
| 332 | synchronized (mStats) { |
| 333 | mStats.noteProcessStartLocked(name, uid); |
Bookatz | 9086762 | 2018-01-31 15:05:57 -0800 | [diff] [blame] | 334 | StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name, |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 335 | StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__STARTED); |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 339 | void noteProcessCrash(String name, int uid) { |
| 340 | synchronized (mStats) { |
| 341 | mStats.noteProcessCrashLocked(name, uid); |
Bookatz | 9086762 | 2018-01-31 15:05:57 -0800 | [diff] [blame] | 342 | StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name, |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 343 | StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__CRASHED); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | void noteProcessAnr(String name, int uid) { |
| 348 | synchronized (mStats) { |
| 349 | mStats.noteProcessAnrLocked(name, uid); |
| 350 | } |
| 351 | } |
| 352 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 353 | void noteProcessFinish(String name, int uid) { |
| 354 | synchronized (mStats) { |
| 355 | mStats.noteProcessFinishLocked(name, uid); |
Bookatz | 9086762 | 2018-01-31 15:05:57 -0800 | [diff] [blame] | 356 | StatsLog.write(StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED, uid, name, |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 357 | StatsLog.PROCESS_LIFE_CYCLE_STATE_CHANGED__STATE__FINISHED); |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Bookatz | db026a2 | 2018-01-10 19:01:56 -0800 | [diff] [blame] | 361 | /** @param state Process state from ActivityManager.java. */ |
Dianne Hackborn | a8d1094 | 2015-11-19 17:55:19 -0800 | [diff] [blame] | 362 | void noteUidProcessState(int uid, int state) { |
| 363 | synchronized (mStats) { |
Bookatz | db026a2 | 2018-01-10 19:01:56 -0800 | [diff] [blame] | 364 | StatsLog.write(StatsLog.UID_PROCESS_STATE_CHANGED, uid, |
| 365 | ActivityManager.processStateAmToProto(state)); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 366 | |
Dianne Hackborn | a8d1094 | 2015-11-19 17:55:19 -0800 | [diff] [blame] | 367 | mStats.noteUidProcessStateLocked(uid, state); |
| 368 | } |
| 369 | } |
| 370 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 371 | // Public interface... |
| 372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | public byte[] getStatistics() { |
| 374 | mContext.enforceCallingPermission( |
| 375 | android.Manifest.permission.BATTERY_STATS, null); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 376 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 377 | //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | Parcel out = Parcel.obtain(); |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 379 | syncStats("get-stats", BatteryExternalStatsWorker.UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 380 | synchronized (mStats) { |
| 381 | mStats.writeToParcel(out, 0); |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | byte[] data = out.marshall(); |
| 384 | out.recycle(); |
| 385 | return data; |
| 386 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 387 | |
| 388 | public ParcelFileDescriptor getStatisticsStream() { |
| 389 | mContext.enforceCallingPermission( |
| 390 | android.Manifest.permission.BATTERY_STATS, null); |
| 391 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 392 | //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM)); |
| 393 | Parcel out = Parcel.obtain(); |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 394 | syncStats("get-stats", BatteryExternalStatsWorker.UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 395 | synchronized (mStats) { |
| 396 | mStats.writeToParcel(out, 0); |
| 397 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 398 | byte[] data = out.marshall(); |
Hui Yu | 0ed84f1 | 2018-06-20 19:07:56 -0700 | [diff] [blame] | 399 | if (DBG) Slog.d(TAG, "getStatisticsStream parcel size is:" + data.length); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 400 | out.recycle(); |
| 401 | try { |
| 402 | return ParcelFileDescriptor.fromData(data, "battery-stats"); |
| 403 | } catch (IOException e) { |
| 404 | Slog.w(TAG, "Unable to create shared memory", e); |
| 405 | return null; |
| 406 | } |
| 407 | } |
| 408 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 409 | public boolean isCharging() { |
| 410 | synchronized (mStats) { |
| 411 | return mStats.isCharging(); |
| 412 | } |
| 413 | } |
| 414 | |
Dianne Hackborn | ab5c0ea | 2014-04-29 14:53:32 -0700 | [diff] [blame] | 415 | public long computeBatteryTimeRemaining() { |
| 416 | synchronized (mStats) { |
| 417 | long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime()); |
| 418 | return time >= 0 ? (time/1000) : time; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | public long computeChargeTimeRemaining() { |
| 423 | synchronized (mStats) { |
| 424 | long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime()); |
| 425 | return time >= 0 ? (time/1000) : time; |
| 426 | } |
| 427 | } |
| 428 | |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 429 | public void noteEvent(int code, String name, int uid) { |
| 430 | enforceCallingPermission(); |
| 431 | synchronized (mStats) { |
| 432 | mStats.noteEventLocked(code, name, uid); |
| 433 | } |
| 434 | } |
| 435 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 436 | public void noteSyncStart(String name, int uid) { |
| 437 | enforceCallingPermission(); |
| 438 | synchronized (mStats) { |
| 439 | mStats.noteSyncStartLocked(name, uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 440 | StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, uid, null, name, |
| 441 | StatsLog.SYNC_STATE_CHANGED__STATE__ON); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | public void noteSyncFinish(String name, int uid) { |
| 446 | enforceCallingPermission(); |
| 447 | synchronized (mStats) { |
| 448 | mStats.noteSyncFinishLocked(name, uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 449 | StatsLog.write_non_chained(StatsLog.SYNC_STATE_CHANGED, uid, null, name, |
| 450 | StatsLog.SYNC_STATE_CHANGED__STATE__OFF); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | public void noteJobStart(String name, int uid) { |
| 455 | enforceCallingPermission(); |
| 456 | synchronized (mStats) { |
| 457 | mStats.noteJobStartLocked(name, uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 458 | StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, uid, null, |
| 459 | name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__STARTED, |
| 460 | JobProtoEnums.STOP_REASON_UNKNOWN); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Dianne Hackborn | 94326cb | 2017-06-28 16:17:20 -0700 | [diff] [blame] | 464 | public void noteJobFinish(String name, int uid, int stopReason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 465 | enforceCallingPermission(); |
| 466 | synchronized (mStats) { |
Dianne Hackborn | 94326cb | 2017-06-28 16:17:20 -0700 | [diff] [blame] | 467 | mStats.noteJobFinishLocked(name, uid, stopReason); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 468 | StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, uid, null, |
| 469 | name, StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__FINISHED, |
| 470 | stopReason); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Amith Yamasani | 977e11f | 2018-02-16 11:29:54 -0800 | [diff] [blame] | 474 | void noteJobsDeferred(int uid, int numDeferred, long sinceLast) { |
| 475 | // No need to enforce calling permission, as it is called from an internal interface |
| 476 | synchronized (mStats) { |
| 477 | mStats.noteJobsDeferredLocked(uid, numDeferred, sinceLast); |
| 478 | } |
| 479 | } |
| 480 | |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 481 | public void noteWakupAlarm(String name, int uid, WorkSource workSource, String tag) { |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 482 | enforceCallingPermission(); |
| 483 | synchronized (mStats) { |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 484 | mStats.noteWakupAlarmLocked(name, uid, workSource, tag); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 488 | public void noteAlarmStart(String name, WorkSource workSource, int uid) { |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 489 | enforceCallingPermission(); |
| 490 | synchronized (mStats) { |
Narayan Kamath | 695cf72 | 2017-12-21 18:32:47 +0000 | [diff] [blame] | 491 | mStats.noteAlarmStartLocked(name, workSource, uid); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | public void noteAlarmFinish(String name, WorkSource workSource, int uid) { |
| 496 | enforceCallingPermission(); |
| 497 | synchronized (mStats) { |
| 498 | mStats.noteAlarmFinishLocked(name, workSource, uid); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 502 | public void noteStartWakelock(int uid, int pid, String name, String historyName, int type, |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 503 | boolean unimportantForLogging) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | enforceCallingPermission(); |
| 505 | synchronized (mStats) { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 506 | mStats.noteStartWakeLocked(uid, pid, null, name, historyName, type, |
| 507 | unimportantForLogging, SystemClock.elapsedRealtime(), |
| 508 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 512 | public void noteStopWakelock(int uid, int pid, String name, String historyName, int type) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | enforceCallingPermission(); |
| 514 | synchronized (mStats) { |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 515 | mStats.noteStopWakeLocked(uid, pid, null, name, historyName, type, |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 516 | SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 520 | public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, |
| 521 | String historyName, int type, boolean unimportantForLogging) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 522 | enforceCallingPermission(); |
| 523 | synchronized (mStats) { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 524 | mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName, |
| 525 | type, unimportantForLogging); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 529 | public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name, |
| 530 | String historyName, int type, WorkSource newWs, int newPid, String newName, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 531 | String newHistoryName, int newType, boolean newUnimportantForLogging) { |
| 532 | enforceCallingPermission(); |
| 533 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 534 | mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 535 | newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging); |
| 536 | } |
| 537 | } |
| 538 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 539 | public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName, |
| 540 | int type) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 541 | enforceCallingPermission(); |
| 542 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 543 | mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Narayan Kamath | 96a9256 | 2018-01-02 18:57:17 +0000 | [diff] [blame] | 547 | @Override |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 548 | public void noteLongPartialWakelockStart(String name, String historyName, int uid) { |
| 549 | enforceCallingPermission(); |
| 550 | synchronized (mStats) { |
| 551 | mStats.noteLongPartialWakelockStart(name, historyName, uid); |
| 552 | } |
| 553 | } |
| 554 | |
Narayan Kamath | 96a9256 | 2018-01-02 18:57:17 +0000 | [diff] [blame] | 555 | @Override |
| 556 | public void noteLongPartialWakelockStartFromSource(String name, String historyName, |
| 557 | WorkSource workSource) { |
| 558 | enforceCallingPermission(); |
| 559 | synchronized (mStats) { |
| 560 | mStats.noteLongPartialWakelockStartFromSource(name, historyName, workSource); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | @Override |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 565 | public void noteLongPartialWakelockFinish(String name, String historyName, int uid) { |
| 566 | enforceCallingPermission(); |
| 567 | synchronized (mStats) { |
| 568 | mStats.noteLongPartialWakelockFinish(name, historyName, uid); |
| 569 | } |
| 570 | } |
| 571 | |
Narayan Kamath | 96a9256 | 2018-01-02 18:57:17 +0000 | [diff] [blame] | 572 | @Override |
| 573 | public void noteLongPartialWakelockFinishFromSource(String name, String historyName, |
| 574 | WorkSource workSource) { |
| 575 | enforceCallingPermission(); |
| 576 | synchronized (mStats) { |
| 577 | mStats.noteLongPartialWakelockFinishFromSource(name, historyName, workSource); |
| 578 | } |
| 579 | } |
| 580 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | public void noteStartSensor(int uid, int sensor) { |
| 582 | enforceCallingPermission(); |
| 583 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 584 | mStats.noteStartSensorLocked(uid, sensor); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 585 | StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, uid, null, sensor, |
| 586 | StatsLog.SENSOR_STATE_CHANGED__STATE__ON); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
Chenjie Yu | 29fb1f7 | 2017-09-13 12:18:50 -0700 | [diff] [blame] | 589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | public void noteStopSensor(int uid, int sensor) { |
| 591 | enforceCallingPermission(); |
| 592 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 593 | mStats.noteStopSensorLocked(uid, sensor); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 594 | StatsLog.write_non_chained(StatsLog.SENSOR_STATE_CHANGED, uid, null, |
| 595 | sensor, StatsLog.SENSOR_STATE_CHANGED__STATE__OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
Chenjie Yu | 29fb1f7 | 2017-09-13 12:18:50 -0700 | [diff] [blame] | 598 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 599 | public void noteVibratorOn(int uid, long durationMillis) { |
| 600 | enforceCallingPermission(); |
| 601 | synchronized (mStats) { |
| 602 | mStats.noteVibratorOnLocked(uid, durationMillis); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | public void noteVibratorOff(int uid) { |
| 607 | enforceCallingPermission(); |
| 608 | synchronized (mStats) { |
| 609 | mStats.noteVibratorOffLocked(uid); |
| 610 | } |
| 611 | } |
| 612 | |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 613 | @Override |
| 614 | public void noteGpsChanged(WorkSource oldWs, WorkSource newWs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | enforceCallingPermission(); |
| 616 | synchronized (mStats) { |
Narayan Kamath | 32684dd | 2018-01-08 17:32:51 +0000 | [diff] [blame] | 617 | mStats.noteGpsChangedLocked(oldWs, newWs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
Chenjie Yu | 29fb1f7 | 2017-09-13 12:18:50 -0700 | [diff] [blame] | 620 | |
Siddharth Ray | 78ccaf5 | 2017-12-23 16:16:21 -0800 | [diff] [blame] | 621 | public void noteGpsSignalQuality(int signalLevel) { |
| 622 | synchronized (mStats) { |
| 623 | mStats.noteGpsSignalQualityLocked(signalLevel); |
| 624 | } |
| 625 | } |
| 626 | |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 627 | public void noteScreenState(int state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | enforceCallingPermission(); |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 629 | if (DBG) Slog.d(TAG, "begin noteScreenState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | synchronized (mStats) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 631 | StatsLog.write(StatsLog.SCREEN_STATE_CHANGED, state); |
| 632 | |
| 633 | mStats.noteScreenStateLocked(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | } |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 635 | if (DBG) Slog.d(TAG, "end noteScreenState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | } |
Chenjie Yu | 29fb1f7 | 2017-09-13 12:18:50 -0700 | [diff] [blame] | 637 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 638 | public void noteScreenBrightness(int brightness) { |
| 639 | enforceCallingPermission(); |
| 640 | synchronized (mStats) { |
Bookatz | c1a050a | 2017-10-10 15:49:28 -0700 | [diff] [blame] | 641 | StatsLog.write(StatsLog.SCREEN_BRIGHTNESS_CHANGED, brightness); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 642 | mStats.noteScreenBrightnessLocked(brightness); |
| 643 | } |
| 644 | } |
yro | e03583e | 2017-09-20 22:51:26 -0700 | [diff] [blame] | 645 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 646 | public void noteUserActivity(int uid, int event) { |
| 647 | enforceCallingPermission(); |
| 648 | synchronized (mStats) { |
| 649 | mStats.noteUserActivityLocked(uid, event); |
| 650 | } |
| 651 | } |
yro | e03583e | 2017-09-20 22:51:26 -0700 | [diff] [blame] | 652 | |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 653 | public void noteWakeUp(String reason, int reasonUid) { |
| 654 | enforceCallingPermission(); |
| 655 | synchronized (mStats) { |
| 656 | mStats.noteWakeUpLocked(reason, reasonUid); |
| 657 | } |
| 658 | } |
| 659 | |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 660 | public void noteInteractive(boolean interactive) { |
| 661 | enforceCallingPermission(); |
| 662 | synchronized (mStats) { |
| 663 | mStats.noteInteractiveLocked(interactive); |
| 664 | } |
| 665 | } |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 666 | |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 667 | public void noteConnectivityChanged(int type, String extra) { |
| 668 | enforceCallingPermission(); |
| 669 | synchronized (mStats) { |
| 670 | mStats.noteConnectivityChangedLocked(type, extra); |
| 671 | } |
| 672 | } |
| 673 | |
Ruchi Kandoi | fa97fcf | 2016-05-13 15:10:39 -0700 | [diff] [blame] | 674 | public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) { |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 675 | enforceCallingPermission(); |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 676 | final boolean update; |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 677 | synchronized (mStats) { |
Adam Lesinski | 14ae39a | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 678 | update = mStats.noteMobileRadioPowerStateLocked(powerState, timestampNs, uid); |
| 679 | } |
| 680 | |
| 681 | if (update) { |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 682 | mWorker.scheduleSync("modem-data", BatteryExternalStatsWorker.UPDATE_RADIO); |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | public void notePhoneOn() { |
| 687 | enforceCallingPermission(); |
| 688 | synchronized (mStats) { |
| 689 | mStats.notePhoneOnLocked(); |
| 690 | } |
| 691 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | public void notePhoneOff() { |
| 694 | enforceCallingPermission(); |
| 695 | synchronized (mStats) { |
| 696 | mStats.notePhoneOffLocked(); |
| 697 | } |
| 698 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 699 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 700 | public void notePhoneSignalStrength(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 701 | enforceCallingPermission(); |
| 702 | synchronized (mStats) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 703 | mStats.notePhoneSignalStrengthLocked(signalStrength); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 706 | |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 707 | public void notePhoneDataConnectionState(int dataType, boolean hasData) { |
| 708 | enforceCallingPermission(); |
| 709 | synchronized (mStats) { |
| 710 | mStats.notePhoneDataConnectionStateLocked(dataType, hasData); |
| 711 | } |
| 712 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 713 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 714 | public void notePhoneState(int state) { |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 715 | enforceCallingPermission(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 716 | int simState = TelephonyManager.getDefault().getSimState(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 717 | synchronized (mStats) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 718 | mStats.notePhoneStateLocked(state, simState); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 722 | public void noteWifiOn() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 723 | enforceCallingPermission(); |
| 724 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 725 | mStats.noteWifiOnLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 726 | } |
Bookatz | da1798c | 2018-12-13 14:16:00 -0800 | [diff] [blame] | 727 | StatsLog.write(StatsLog.WIFI_ENABLED_STATE_CHANGED, |
| 728 | StatsLog.WIFI_ENABLED_STATE_CHANGED__STATE__ON); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 729 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 730 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 731 | public void noteWifiOff() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 732 | enforceCallingPermission(); |
| 733 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 734 | mStats.noteWifiOffLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 735 | } |
Bookatz | da1798c | 2018-12-13 14:16:00 -0800 | [diff] [blame] | 736 | StatsLog.write(StatsLog.WIFI_ENABLED_STATE_CHANGED, |
| 737 | StatsLog.WIFI_ENABLED_STATE_CHANGED__STATE__OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 738 | } |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 739 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 740 | public void noteStartAudio(int uid) { |
| 741 | enforceCallingPermission(); |
| 742 | synchronized (mStats) { |
| 743 | mStats.noteAudioOnLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 744 | StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, uid, null, |
| 745 | StatsLog.AUDIO_STATE_CHANGED__STATE__ON); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | public void noteStopAudio(int uid) { |
| 750 | enforceCallingPermission(); |
| 751 | synchronized (mStats) { |
| 752 | mStats.noteAudioOffLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 753 | StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, uid, null, |
| 754 | StatsLog.AUDIO_STATE_CHANGED__STATE__OFF); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | public void noteStartVideo(int uid) { |
| 759 | enforceCallingPermission(); |
| 760 | synchronized (mStats) { |
| 761 | mStats.noteVideoOnLocked(uid); |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 762 | StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, uid, null, |
| 763 | StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__ON); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | public void noteStopVideo(int uid) { |
| 768 | enforceCallingPermission(); |
| 769 | synchronized (mStats) { |
| 770 | mStats.noteVideoOffLocked(uid); |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 771 | StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, uid, |
| 772 | null, StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__OFF); |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Dianne Hackborn | 10eaa85 | 2014-07-22 22:54:55 -0700 | [diff] [blame] | 776 | public void noteResetAudio() { |
| 777 | enforceCallingPermission(); |
| 778 | synchronized (mStats) { |
| 779 | mStats.noteResetAudioLocked(); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 780 | StatsLog.write_non_chained(StatsLog.AUDIO_STATE_CHANGED, -1, null, |
| 781 | StatsLog.AUDIO_STATE_CHANGED__STATE__RESET); |
Dianne Hackborn | 10eaa85 | 2014-07-22 22:54:55 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | public void noteResetVideo() { |
| 786 | enforceCallingPermission(); |
| 787 | synchronized (mStats) { |
| 788 | mStats.noteResetVideoLocked(); |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 789 | StatsLog.write_non_chained(StatsLog.MEDIA_CODEC_STATE_CHANGED, -1, null, |
| 790 | StatsLog.MEDIA_CODEC_STATE_CHANGED__STATE__RESET); |
Dianne Hackborn | 10eaa85 | 2014-07-22 22:54:55 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 794 | public void noteFlashlightOn(int uid) { |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 795 | enforceCallingPermission(); |
| 796 | synchronized (mStats) { |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 797 | mStats.noteFlashlightOnLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 798 | StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, uid, null, |
| 799 | StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__ON); |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 803 | public void noteFlashlightOff(int uid) { |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 804 | enforceCallingPermission(); |
| 805 | synchronized (mStats) { |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 806 | mStats.noteFlashlightOffLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 807 | StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, uid, null, |
| 808 | StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__OFF); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | public void noteStartCamera(int uid) { |
| 813 | enforceCallingPermission(); |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 814 | if (DBG) Slog.d(TAG, "begin noteStartCamera"); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 815 | synchronized (mStats) { |
| 816 | mStats.noteCameraOnLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 817 | StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, uid, null, |
| 818 | StatsLog.CAMERA_STATE_CHANGED__STATE__ON); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 819 | } |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 820 | if (DBG) Slog.d(TAG, "end noteStartCamera"); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | public void noteStopCamera(int uid) { |
| 824 | enforceCallingPermission(); |
| 825 | synchronized (mStats) { |
| 826 | mStats.noteCameraOffLocked(uid); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 827 | StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, uid, null, |
| 828 | StatsLog.CAMERA_STATE_CHANGED__STATE__OFF); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | public void noteResetCamera() { |
| 833 | enforceCallingPermission(); |
| 834 | synchronized (mStats) { |
| 835 | mStats.noteResetCameraLocked(); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 836 | StatsLog.write_non_chained(StatsLog.CAMERA_STATE_CHANGED, -1, null, |
| 837 | StatsLog.CAMERA_STATE_CHANGED__STATE__RESET); |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | public void noteResetFlashlight() { |
| 842 | enforceCallingPermission(); |
| 843 | synchronized (mStats) { |
| 844 | mStats.noteResetFlashlightLocked(); |
Bookatz | 235343d | 2018-03-26 13:03:50 -0700 | [diff] [blame] | 845 | StatsLog.write_non_chained(StatsLog.FLASHLIGHT_STATE_CHANGED, -1, null, |
| 846 | StatsLog.FLASHLIGHT_STATE_CHANGED__STATE__RESET); |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 850 | @Override |
Adam Lesinski | 5f056f6 | 2016-07-14 16:56:08 -0700 | [diff] [blame] | 851 | public void noteWifiRadioPowerState(int powerState, long tsNanos, int uid) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 852 | enforceCallingPermission(); |
| 853 | |
| 854 | // There was a change in WiFi power state. |
| 855 | // Collect data now for the past activity. |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 856 | synchronized (mStats) { |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 857 | if (mStats.isOnBattery()) { |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 858 | final String type = (powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH || |
| 859 | powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active" |
| 860 | : "inactive"; |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 861 | mWorker.scheduleSync("wifi-data: " + type, BatteryExternalStatsWorker.UPDATE_WIFI); |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 862 | } |
Adam Lesinski | 5f056f6 | 2016-07-14 16:56:08 -0700 | [diff] [blame] | 863 | mStats.noteWifiRadioPowerState(powerState, tsNanos, uid); |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 864 | } |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 867 | public void noteWifiRunning(WorkSource ws) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 868 | enforceCallingPermission(); |
| 869 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 870 | mStats.noteWifiRunningLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 871 | } |
Bookatz | da1798c | 2018-12-13 14:16:00 -0800 | [diff] [blame] | 872 | // TODO: Log WIFI_RUNNING_STATE_CHANGED in a better spot to include Hotspot too. |
| 873 | StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED, |
| 874 | ws, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__ON); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 877 | public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 878 | enforceCallingPermission(); |
| 879 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 880 | mStats.noteWifiRunningChangedLocked(oldWs, newWs); |
| 881 | } |
Bookatz | da1798c | 2018-12-13 14:16:00 -0800 | [diff] [blame] | 882 | StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED, |
| 883 | newWs, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__ON); |
| 884 | StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED, |
| 885 | oldWs, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__OFF); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | public void noteWifiStopped(WorkSource ws) { |
| 889 | enforceCallingPermission(); |
| 890 | synchronized (mStats) { |
| 891 | mStats.noteWifiStoppedLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 892 | } |
Bookatz | da1798c | 2018-12-13 14:16:00 -0800 | [diff] [blame] | 893 | StatsLog.write(StatsLog.WIFI_RUNNING_STATE_CHANGED, |
| 894 | ws, StatsLog.WIFI_RUNNING_STATE_CHANGED__STATE__OFF); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Dianne Hackborn | ca1bf21 | 2014-02-14 14:18:36 -0800 | [diff] [blame] | 897 | public void noteWifiState(int wifiState, String accessPoint) { |
| 898 | enforceCallingPermission(); |
| 899 | synchronized (mStats) { |
| 900 | mStats.noteWifiStateLocked(wifiState, accessPoint); |
| 901 | } |
| 902 | } |
| 903 | |
Dianne Hackborn | 3251b90 | 2014-06-20 14:40:53 -0700 | [diff] [blame] | 904 | public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) { |
| 905 | enforceCallingPermission(); |
| 906 | synchronized (mStats) { |
| 907 | mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | public void noteWifiRssiChanged(int newRssi) { |
| 912 | enforceCallingPermission(); |
| 913 | synchronized (mStats) { |
| 914 | mStats.noteWifiRssiChangedLocked(newRssi); |
| 915 | } |
| 916 | } |
| 917 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 918 | public void noteFullWifiLockAcquired(int uid) { |
| 919 | enforceCallingPermission(); |
| 920 | synchronized (mStats) { |
| 921 | mStats.noteFullWifiLockAcquiredLocked(uid); |
| 922 | } |
| 923 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 924 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 925 | public void noteFullWifiLockReleased(int uid) { |
| 926 | enforceCallingPermission(); |
| 927 | synchronized (mStats) { |
| 928 | mStats.noteFullWifiLockReleasedLocked(uid); |
| 929 | } |
| 930 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 931 | |
| 932 | public void noteWifiScanStarted(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 933 | enforceCallingPermission(); |
| 934 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 935 | mStats.noteWifiScanStartedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 938 | |
| 939 | public void noteWifiScanStopped(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 940 | enforceCallingPermission(); |
| 941 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 942 | mStats.noteWifiScanStoppedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 946 | public void noteWifiMulticastEnabled(int uid) { |
| 947 | enforceCallingPermission(); |
| 948 | synchronized (mStats) { |
| 949 | mStats.noteWifiMulticastEnabledLocked(uid); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | public void noteWifiMulticastDisabled(int uid) { |
| 954 | enforceCallingPermission(); |
| 955 | synchronized (mStats) { |
| 956 | mStats.noteWifiMulticastDisabledLocked(uid); |
| 957 | } |
| 958 | } |
| 959 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 960 | public void noteFullWifiLockAcquiredFromSource(WorkSource ws) { |
| 961 | enforceCallingPermission(); |
| 962 | synchronized (mStats) { |
| 963 | mStats.noteFullWifiLockAcquiredFromSourceLocked(ws); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | public void noteFullWifiLockReleasedFromSource(WorkSource ws) { |
| 968 | enforceCallingPermission(); |
| 969 | synchronized (mStats) { |
| 970 | mStats.noteFullWifiLockReleasedFromSourceLocked(ws); |
| 971 | } |
| 972 | } |
| 973 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 974 | public void noteWifiScanStartedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 975 | enforceCallingPermission(); |
| 976 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 977 | mStats.noteWifiScanStartedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 981 | public void noteWifiScanStoppedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 982 | enforceCallingPermission(); |
| 983 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 984 | mStats.noteWifiScanStoppedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 988 | public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) { |
| 989 | enforceCallingPermission(); |
| 990 | synchronized (mStats) { |
| 991 | mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) { |
| 996 | enforceCallingPermission(); |
| 997 | synchronized (mStats) { |
| 998 | mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws); |
| 999 | } |
| 1000 | } |
| 1001 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1002 | @Override |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1003 | public void noteNetworkInterfaceType(String iface, int networkType) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1004 | enforceCallingPermission(); |
| 1005 | synchronized (mStats) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1006 | mStats.noteNetworkInterfaceTypeLocked(iface, networkType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1010 | @Override |
| 1011 | public void noteNetworkStatsEnabled() { |
| 1012 | enforceCallingPermission(); |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1013 | // During device boot, qtaguid isn't enabled until after the inital |
| 1014 | // loading of battery stats. Now that they're enabled, take our initial |
| 1015 | // snapshot for future delta calculation. |
| 1016 | mWorker.scheduleSync("network-stats-enabled", |
| 1017 | BatteryExternalStatsWorker.UPDATE_RADIO | BatteryExternalStatsWorker.UPDATE_WIFI); |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 1020 | @Override |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1021 | public void noteDeviceIdleMode(int mode, String activeReason, int activeUid) { |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 1022 | enforceCallingPermission(); |
| 1023 | synchronized (mStats) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1024 | mStats.noteDeviceIdleModeLocked(mode, activeReason, activeUid); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1028 | public void notePackageInstalled(String pkgName, long versionCode) { |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 1029 | enforceCallingPermission(); |
| 1030 | synchronized (mStats) { |
| 1031 | mStats.notePackageInstalledLocked(pkgName, versionCode); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | public void notePackageUninstalled(String pkgName) { |
| 1036 | enforceCallingPermission(); |
| 1037 | synchronized (mStats) { |
| 1038 | mStats.notePackageUninstalledLocked(pkgName); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Adam Lesinski | 6771d62 | 2016-01-15 18:14:47 -0800 | [diff] [blame] | 1042 | @Override |
Bookatz | b1f04f3 | 2017-05-19 13:57:32 -0700 | [diff] [blame] | 1043 | public void noteBleScanStarted(WorkSource ws, boolean isUnoptimized) { |
Adam Lesinski | 6771d62 | 2016-01-15 18:14:47 -0800 | [diff] [blame] | 1044 | enforceCallingPermission(); |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 1045 | synchronized (mStats) { |
Bookatz | b1f04f3 | 2017-05-19 13:57:32 -0700 | [diff] [blame] | 1046 | mStats.noteBluetoothScanStartedFromSourceLocked(ws, isUnoptimized); |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 1047 | } |
Adam Lesinski | 6771d62 | 2016-01-15 18:14:47 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | @Override |
Bookatz | 94c5a31 | 2017-07-11 16:49:17 -0700 | [diff] [blame] | 1051 | public void noteBleScanStopped(WorkSource ws, boolean isUnoptimized) { |
Adam Lesinski | 6771d62 | 2016-01-15 18:14:47 -0800 | [diff] [blame] | 1052 | enforceCallingPermission(); |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 1053 | synchronized (mStats) { |
Bookatz | 94c5a31 | 2017-07-11 16:49:17 -0700 | [diff] [blame] | 1054 | mStats.noteBluetoothScanStoppedFromSourceLocked(ws, isUnoptimized); |
Adam Lesinski | 9f55cc7 | 2016-01-27 20:42:14 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | @Override |
| 1059 | public void noteResetBleScan() { |
| 1060 | enforceCallingPermission(); |
| 1061 | synchronized (mStats) { |
| 1062 | mStats.noteResetBluetoothScanLocked(); |
| 1063 | } |
Adam Lesinski | 6771d62 | 2016-01-15 18:14:47 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1066 | @Override |
Bookatz | 4ebc064 | 2017-05-11 12:21:19 -0700 | [diff] [blame] | 1067 | public void noteBleScanResults(WorkSource ws, int numNewResults) { |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 1068 | enforceCallingPermission(); |
| 1069 | synchronized (mStats) { |
Bookatz | 4ebc064 | 2017-05-11 12:21:19 -0700 | [diff] [blame] | 1070 | mStats.noteBluetoothScanResultsFromSourceLocked(ws, numNewResults); |
Bookatz | 956f36bf | 2017-04-28 09:48:17 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | @Override |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1075 | public void noteWifiControllerActivity(WifiActivityEnergyInfo info) { |
| 1076 | enforceCallingPermission(); |
| 1077 | |
| 1078 | if (info == null || !info.isValid()) { |
| 1079 | Slog.e(TAG, "invalid wifi data given: " + info); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
Adam Lesinski | 14ae39a | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1083 | mStats.updateWifiState(info); |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | @Override |
| 1087 | public void noteBluetoothControllerActivity(BluetoothActivityEnergyInfo info) { |
| 1088 | enforceCallingPermission(); |
| 1089 | if (info == null || !info.isValid()) { |
| 1090 | Slog.e(TAG, "invalid bluetooth data given: " + info); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
Adam Lesinski | c357e68 | 2018-02-28 12:55:39 -0800 | [diff] [blame] | 1094 | synchronized (mStats) { |
| 1095 | mStats.updateBluetoothStateLocked(info); |
| 1096 | } |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | @Override |
| 1100 | public void noteModemControllerActivity(ModemActivityInfo info) { |
| 1101 | enforceCallingPermission(); |
| 1102 | |
| 1103 | if (info == null || !info.isValid()) { |
| 1104 | Slog.e(TAG, "invalid modem data given: " + info); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
Adam Lesinski | 14ae39a | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1108 | mStats.updateMobileRadioState(info); |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | public boolean isOnBattery() { |
| 1112 | return mStats.isOnBattery(); |
| 1113 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1114 | |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1115 | @Override |
| 1116 | public void setBatteryState(final int status, final int health, final int plugType, |
Adam Lesinski | 041d917 | 2016-12-12 12:03:56 -0800 | [diff] [blame] | 1117 | final int level, final int temp, final int volt, final int chargeUAh, |
| 1118 | final int chargeFullUAh) { |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1119 | enforceCallingPermission(); |
| 1120 | |
| 1121 | // BatteryService calls us here and we may update external state. It would be wrong |
| 1122 | // to block such a low level service like BatteryService on external stats like WiFi. |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1123 | mWorker.scheduleRunnable(() -> { |
| 1124 | synchronized (mStats) { |
Sudheer Shanka | c57729a | 2018-02-09 15:44:42 -0800 | [diff] [blame] | 1125 | final boolean onBattery = BatteryStatsImpl.isOnBattery(plugType, status); |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1126 | if (mStats.isOnBattery() == onBattery) { |
| 1127 | // The battery state has not changed, so we don't need to sync external |
| 1128 | // stats immediately. |
| 1129 | mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt, |
| 1130 | chargeUAh, chargeFullUAh); |
| 1131 | return; |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1132 | } |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1133 | } |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1134 | |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1135 | // Sync external stats first as the battery has changed states. If we don't sync |
| 1136 | // before changing the state, we may not collect the relevant data later. |
| 1137 | // Order here is guaranteed since we're scheduling from the same thread and we are |
| 1138 | // using a single threaded executor. |
| 1139 | mWorker.scheduleSync("battery-state", BatteryExternalStatsWorker.UPDATE_ALL); |
| 1140 | mWorker.scheduleRunnable(() -> { |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1141 | synchronized (mStats) { |
Adam Lesinski | 926969b | 2016-04-28 17:31:12 -0700 | [diff] [blame] | 1142 | mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt, |
Adam Lesinski | 041d917 | 2016-12-12 12:03:56 -0800 | [diff] [blame] | 1143 | chargeUAh, chargeFullUAh); |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1144 | } |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1145 | }); |
Adam Lesinski | d7616ef | 2015-07-27 11:43:05 -0700 | [diff] [blame] | 1146 | }); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 1147 | } |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | public long getAwakeTimeBattery() { |
| 1150 | mContext.enforceCallingOrSelfPermission( |
| 1151 | android.Manifest.permission.BATTERY_STATS, null); |
| 1152 | return mStats.getAwakeTimeBattery(); |
| 1153 | } |
| 1154 | |
| 1155 | public long getAwakeTimePlugged() { |
| 1156 | mContext.enforceCallingOrSelfPermission( |
| 1157 | android.Manifest.permission.BATTERY_STATS, null); |
| 1158 | return mStats.getAwakeTimePlugged(); |
| 1159 | } |
| 1160 | |
| 1161 | public void enforceCallingPermission() { |
| 1162 | if (Binder.getCallingPid() == Process.myPid()) { |
| 1163 | return; |
| 1164 | } |
| 1165 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 1166 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 1167 | } |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1168 | |
| 1169 | final class WakeupReasonThread extends Thread { |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1170 | private static final int MAX_REASON_SIZE = 512; |
| 1171 | private CharsetDecoder mDecoder; |
| 1172 | private ByteBuffer mUtf8Buffer; |
| 1173 | private CharBuffer mUtf16Buffer; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1174 | |
| 1175 | WakeupReasonThread() { |
| 1176 | super("BatteryStats_wakeupReason"); |
| 1177 | } |
| 1178 | |
| 1179 | public void run() { |
| 1180 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
| 1181 | |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1182 | mDecoder = StandardCharsets.UTF_8 |
| 1183 | .newDecoder() |
| 1184 | .onMalformedInput(CodingErrorAction.REPLACE) |
| 1185 | .onUnmappableCharacter(CodingErrorAction.REPLACE) |
| 1186 | .replaceWith("?"); |
| 1187 | |
| 1188 | mUtf8Buffer = ByteBuffer.allocateDirect(MAX_REASON_SIZE); |
| 1189 | mUtf16Buffer = CharBuffer.allocate(MAX_REASON_SIZE); |
| 1190 | |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1191 | try { |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1192 | String reason; |
| 1193 | while ((reason = waitWakeup()) != null) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1194 | synchronized (mStats) { |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1195 | mStats.noteWakeupReasonLocked(reason); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | } catch (RuntimeException e) { |
| 1199 | Slog.e(TAG, "Failure reading wakeup reasons", e); |
| 1200 | } |
| 1201 | } |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1202 | |
| 1203 | private String waitWakeup() { |
| 1204 | mUtf8Buffer.clear(); |
| 1205 | mUtf16Buffer.clear(); |
| 1206 | mDecoder.reset(); |
| 1207 | |
| 1208 | int bytesWritten = nativeWaitWakeup(mUtf8Buffer); |
| 1209 | if (bytesWritten < 0) { |
| 1210 | return null; |
| 1211 | } else if (bytesWritten == 0) { |
| 1212 | return "unknown"; |
| 1213 | } |
| 1214 | |
| 1215 | // Set the buffer's limit to the number of bytes written. |
| 1216 | mUtf8Buffer.limit(bytesWritten); |
| 1217 | |
| 1218 | // Decode the buffer from UTF-8 to UTF-16. |
| 1219 | // Unmappable characters will be replaced. |
| 1220 | mDecoder.decode(mUtf8Buffer, mUtf16Buffer, true); |
| 1221 | mUtf16Buffer.flip(); |
| 1222 | |
| 1223 | // Create a String from the UTF-16 buffer. |
| 1224 | return mUtf16Buffer.toString(); |
| 1225 | } |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
Adam Lesinski | 515702c | 2015-07-23 18:13:38 -0700 | [diff] [blame] | 1228 | private static native int nativeWaitWakeup(ByteBuffer outBuffer); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1229 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1230 | private void dumpHelp(PrintWriter pw) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1231 | pw.println("Battery stats (batterystats) dump options:"); |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 1232 | pw.println(" [--checkin] [--proto] [--history] [--history-start] [--charged] [-c]"); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1233 | pw.println(" [--daily] [--reset] [--write] [--new-daily] [--read-daily] [-h] [<package.name>]"); |
Dianne Hackborn | 865b79b | 2015-08-18 17:33:00 -0700 | [diff] [blame] | 1234 | pw.println(" --checkin: generate output for a checkin report; will write (and clear) the"); |
| 1235 | pw.println(" last old completed stats when they had been reset."); |
Joe Onorato | 1476d32 | 2016-05-05 14:46:15 -0700 | [diff] [blame] | 1236 | pw.println(" -c: write the current stats in checkin format."); |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 1237 | pw.println(" --proto: write the current aggregate stats (without history) in proto format."); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 1238 | pw.println(" --history: show only history data."); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1239 | pw.println(" --history-start <num>: show only history data starting at given time offset."); |
Hui Yu | 0ed84f1 | 2018-06-20 19:07:56 -0700 | [diff] [blame] | 1240 | pw.println(" --history-create-events <num>: create <num> of battery history events."); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1241 | pw.println(" --charged: only output data since last charged."); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1242 | pw.println(" --daily: only output full daily data."); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1243 | pw.println(" --reset: reset the stats, clearing all current data."); |
| 1244 | pw.println(" --write: force write current collected stats to disk."); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1245 | pw.println(" --new-daily: immediately create and write new daily stats record."); |
| 1246 | pw.println(" --read-daily: read-load last written daily stats."); |
Sudheer Shanka | 5c19b89 | 2018-01-05 17:25:46 -0800 | [diff] [blame] | 1247 | pw.println(" --settings: dump the settings key/values related to batterystats"); |
Mike Ma | 234d182 | 2018-03-13 18:53:21 -0700 | [diff] [blame] | 1248 | pw.println(" --cpu: dump cpu stats for debugging purpose"); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1249 | pw.println(" <package.name>: optional name of package to filter output by."); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1250 | pw.println(" -h: print this help text."); |
| 1251 | pw.println("Battery stats (batterystats) commands:"); |
| 1252 | pw.println(" enable|disable <option>"); |
| 1253 | pw.println(" Enable or disable a running option. Option state is not saved across boots."); |
| 1254 | pw.println(" Options are:"); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1255 | pw.println(" full-history: include additional detailed events in battery history:"); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 1256 | pw.println(" wake_lock_in, alarms and proc events"); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1257 | pw.println(" no-auto-reset: don't automatically reset stats when unplugged"); |
Amith Yamasani | 674c9bb | 2017-02-01 09:45:17 -0800 | [diff] [blame] | 1258 | pw.println(" pretend-screen-off: pretend the screen is off, even if screen state changes"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Sudheer Shanka | 5c19b89 | 2018-01-05 17:25:46 -0800 | [diff] [blame] | 1261 | private void dumpSettings(PrintWriter pw) { |
| 1262 | synchronized (mStats) { |
| 1263 | mStats.dumpConstantsLocked(pw); |
| 1264 | } |
| 1265 | } |
| 1266 | |
Mike Ma | 234d182 | 2018-03-13 18:53:21 -0700 | [diff] [blame] | 1267 | private void dumpCpuStats(PrintWriter pw) { |
| 1268 | synchronized (mStats) { |
| 1269 | mStats.dumpCpuStatsLocked(pw); |
| 1270 | } |
| 1271 | } |
| 1272 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1273 | private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) { |
| 1274 | i++; |
| 1275 | if (i >= args.length) { |
| 1276 | pw.println("Missing option argument for " + (enable ? "--enable" : "--disable")); |
| 1277 | dumpHelp(pw); |
| 1278 | return -1; |
| 1279 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1280 | if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1281 | synchronized (mStats) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1282 | mStats.setRecordAllHistoryLocked(enable); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1283 | } |
Dianne Hackborn | 9a75543 | 2014-05-15 17:05:22 -0700 | [diff] [blame] | 1284 | } else if ("no-auto-reset".equals(args[i])) { |
| 1285 | synchronized (mStats) { |
| 1286 | mStats.setNoAutoReset(enable); |
| 1287 | } |
Amith Yamasani | 674c9bb | 2017-02-01 09:45:17 -0800 | [diff] [blame] | 1288 | } else if ("pretend-screen-off".equals(args[i])) { |
| 1289 | synchronized (mStats) { |
| 1290 | mStats.setPretendScreenOff(enable); |
| 1291 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1292 | } else { |
| 1293 | pw.println("Unknown enable/disable option: " + args[i]); |
| 1294 | dumpHelp(pw); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | return i; |
| 1298 | } |
| 1299 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | @Override |
| 1302 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1303 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return; |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1304 | |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1305 | int flags = 0; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1306 | boolean useCheckinFormat = false; |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1307 | boolean toProto = false; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1308 | boolean isRealCheckin = false; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1309 | boolean noOutput = false; |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1310 | boolean writeData = false; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1311 | long historyStart = -1; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1312 | int reqUid = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1313 | if (args != null) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1314 | for (int i=0; i<args.length; i++) { |
| 1315 | String arg = args[i]; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1316 | if ("--checkin".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1317 | useCheckinFormat = true; |
| 1318 | isRealCheckin = true; |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 1319 | } else if ("--history".equals(arg)) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1320 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 1321 | } else if ("--history-start".equals(arg)) { |
| 1322 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 1323 | i++; |
| 1324 | if (i >= args.length) { |
| 1325 | pw.println("Missing time argument for --history-since"); |
| 1326 | dumpHelp(pw); |
| 1327 | return; |
| 1328 | } |
Hui Yu | 0ed84f1 | 2018-06-20 19:07:56 -0700 | [diff] [blame] | 1329 | historyStart = ParseUtils.parseLong(args[i], 0); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1330 | writeData = true; |
Hui Yu | 0ed84f1 | 2018-06-20 19:07:56 -0700 | [diff] [blame] | 1331 | } else if ("--history-create-events".equals(arg)) { |
| 1332 | i++; |
| 1333 | if (i >= args.length) { |
| 1334 | pw.println("Missing events argument for --history-create-events"); |
| 1335 | dumpHelp(pw); |
| 1336 | return; |
| 1337 | } |
| 1338 | final long events = ParseUtils.parseLong(args[i], 0); |
| 1339 | synchronized (mStats) { |
| 1340 | mStats.createFakeHistoryEvents(events); |
| 1341 | pw.println("Battery history create events started."); |
| 1342 | noOutput = true; |
| 1343 | } |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 1344 | } else if ("-c".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1345 | useCheckinFormat = true; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1346 | flags |= BatteryStats.DUMP_INCLUDE_HISTORY; |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1347 | } else if ("--proto".equals(arg)) { |
| 1348 | toProto = true; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1349 | } else if ("--charged".equals(arg)) { |
| 1350 | flags |= BatteryStats.DUMP_CHARGED_ONLY; |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1351 | } else if ("--daily".equals(arg)) { |
| 1352 | flags |= BatteryStats.DUMP_DAILY_ONLY; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1353 | } else if ("--reset".equals(arg)) { |
| 1354 | synchronized (mStats) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1355 | mStats.resetAllStatsCmdLocked(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1356 | pw.println("Battery stats reset."); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1357 | noOutput = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | } |
Adam Lesinski | b3a1bad | 2017-05-26 11:50:40 -0700 | [diff] [blame] | 1359 | mWorker.scheduleSync("dump", BatteryExternalStatsWorker.UPDATE_ALL); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1360 | } else if ("--write".equals(arg)) { |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 1361 | syncStats("dump", BatteryExternalStatsWorker.UPDATE_ALL); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1362 | synchronized (mStats) { |
| 1363 | mStats.writeSyncLocked(); |
| 1364 | pw.println("Battery stats written."); |
| 1365 | noOutput = true; |
| 1366 | } |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1367 | } else if ("--new-daily".equals(arg)) { |
| 1368 | synchronized (mStats) { |
| 1369 | mStats.recordDailyStatsLocked(); |
| 1370 | pw.println("New daily stats written."); |
| 1371 | noOutput = true; |
| 1372 | } |
| 1373 | } else if ("--read-daily".equals(arg)) { |
| 1374 | synchronized (mStats) { |
| 1375 | mStats.readDailyStatsLocked(); |
| 1376 | pw.println("Last daily stats read."); |
| 1377 | noOutput = true; |
| 1378 | } |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1379 | } else if ("--enable".equals(arg) || "enable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1380 | i = doEnableOrDisable(pw, i, args, true); |
| 1381 | if (i < 0) { |
| 1382 | return; |
| 1383 | } |
| 1384 | pw.println("Enabled: " + args[i]); |
| 1385 | return; |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1386 | } else if ("--disable".equals(arg) || "disable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1387 | i = doEnableOrDisable(pw, i, args, false); |
| 1388 | if (i < 0) { |
| 1389 | return; |
| 1390 | } |
| 1391 | pw.println("Disabled: " + args[i]); |
| 1392 | return; |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1393 | } else if ("-h".equals(arg)) { |
| 1394 | dumpHelp(pw); |
| 1395 | return; |
Sudheer Shanka | 5c19b89 | 2018-01-05 17:25:46 -0800 | [diff] [blame] | 1396 | } else if ("--settings".equals(arg)) { |
| 1397 | dumpSettings(pw); |
| 1398 | return; |
Mike Ma | 234d182 | 2018-03-13 18:53:21 -0700 | [diff] [blame] | 1399 | } else if ("--cpu".equals(arg)) { |
| 1400 | dumpCpuStats(pw); |
| 1401 | return; |
Mike Lockwood | e817404 | 2011-08-16 12:53:43 -0700 | [diff] [blame] | 1402 | } else if ("-a".equals(arg)) { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 1403 | flags |= BatteryStats.DUMP_VERBOSE; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1404 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1405 | pw.println("Unknown option: " + arg); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1406 | dumpHelp(pw); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1407 | return; |
| 1408 | } else { |
| 1409 | // Not an option, last argument must be a package name. |
| 1410 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1411 | reqUid = mContext.getPackageManager().getPackageUidAsUser(arg, |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1412 | UserHandle.getCallingUserId()); |
| 1413 | } catch (PackageManager.NameNotFoundException e) { |
| 1414 | pw.println("Unknown package: " + arg); |
| 1415 | dumpHelp(pw); |
| 1416 | return; |
| 1417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | } |
| 1419 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1420 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1421 | if (noOutput) { |
| 1422 | return; |
| 1423 | } |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1424 | |
| 1425 | long ident = Binder.clearCallingIdentity(); |
| 1426 | try { |
| 1427 | if (BatteryStatsHelper.checkWifiOnly(mContext)) { |
| 1428 | flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY; |
| 1429 | } |
| 1430 | // Fetch data from external sources and update the BatteryStatsImpl object with them. |
Sudheer Shanka | b2f83c1 | 2017-11-13 19:25:01 -0800 | [diff] [blame] | 1431 | syncStats("dump", BatteryExternalStatsWorker.UPDATE_ALL); |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1432 | } finally { |
| 1433 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 1434 | } |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1435 | |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 1436 | if (reqUid >= 0) { |
| 1437 | // By default, if the caller is only interested in a specific package, then |
| 1438 | // we only dump the aggregated data since charged. |
Dianne Hackborn | 1e725a7 | 2015-03-24 18:23:19 -0700 | [diff] [blame] | 1439 | if ((flags&(BatteryStats.DUMP_HISTORY_ONLY|BatteryStats.DUMP_CHARGED_ONLY)) == 0) { |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 1440 | flags |= BatteryStats.DUMP_CHARGED_ONLY; |
| 1441 | // Also if they are doing -c, we don't want history. |
| 1442 | flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY; |
| 1443 | } |
| 1444 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1445 | |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1446 | if (toProto) { |
| 1447 | List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications( |
| 1448 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_ALL); |
| 1449 | if (isRealCheckin) { |
| 1450 | // For a real checkin, first we want to prefer to use the last complete checkin |
| 1451 | // file if there is one. |
| 1452 | synchronized (mStats.mCheckinFile) { |
| 1453 | if (mStats.mCheckinFile.exists()) { |
| 1454 | try { |
| 1455 | byte[] raw = mStats.mCheckinFile.readFully(); |
| 1456 | if (raw != null) { |
| 1457 | Parcel in = Parcel.obtain(); |
| 1458 | in.unmarshall(raw, 0, raw.length); |
| 1459 | in.setDataPosition(0); |
| 1460 | BatteryStatsImpl checkinStats = new BatteryStatsImpl( |
| 1461 | null, mStats.mHandler, null, mUserManagerUserInfoProvider); |
| 1462 | checkinStats.readSummaryFromParcel(in); |
| 1463 | in.recycle(); |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 1464 | checkinStats.dumpProtoLocked( |
| 1465 | mContext, fd, apps, flags, historyStart); |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1466 | mStats.mCheckinFile.delete(); |
| 1467 | return; |
| 1468 | } |
| 1469 | } catch (IOException | ParcelFormatException e) { |
| 1470 | Slog.w(TAG, "Failure reading checkin file " |
| 1471 | + mStats.mCheckinFile.getBaseFile(), e); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | if (DBG) Slog.d(TAG, "begin dumpProtoLocked from UID " + Binder.getCallingUid()); |
| 1477 | synchronized (mStats) { |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 1478 | mStats.dumpProtoLocked(mContext, fd, apps, flags, historyStart); |
Kweku Adams | 2f73ecd | 2017-09-27 16:59:19 -0700 | [diff] [blame] | 1479 | if (writeData) { |
| 1480 | mStats.writeAsyncLocked(); |
| 1481 | } |
| 1482 | } |
| 1483 | if (DBG) Slog.d(TAG, "end dumpProtoLocked"); |
| 1484 | } else if (useCheckinFormat) { |
Dianne Hackborn | 9cfba35 | 2016-03-24 17:31:28 -0700 | [diff] [blame] | 1485 | List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications( |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 1486 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_ALL); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1487 | if (isRealCheckin) { |
| 1488 | // For a real checkin, first we want to prefer to use the last complete checkin |
| 1489 | // file if there is one. |
| 1490 | synchronized (mStats.mCheckinFile) { |
| 1491 | if (mStats.mCheckinFile.exists()) { |
| 1492 | try { |
| 1493 | byte[] raw = mStats.mCheckinFile.readFully(); |
| 1494 | if (raw != null) { |
| 1495 | Parcel in = Parcel.obtain(); |
| 1496 | in.unmarshall(raw, 0, raw.length); |
| 1497 | in.setDataPosition(0); |
| 1498 | BatteryStatsImpl checkinStats = new BatteryStatsImpl( |
Suprabh Shukla | e6e723d | 2017-06-14 16:14:43 -0700 | [diff] [blame] | 1499 | null, mStats.mHandler, null, mUserManagerUserInfoProvider); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1500 | checkinStats.readSummaryFromParcel(in); |
| 1501 | in.recycle(); |
| 1502 | checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, |
| 1503 | historyStart); |
| 1504 | mStats.mCheckinFile.delete(); |
| 1505 | return; |
| 1506 | } |
Adam Lesinski | 9ae9cba | 2015-07-08 17:09:34 -0700 | [diff] [blame] | 1507 | } catch (IOException | ParcelFormatException e) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1508 | Slog.w(TAG, "Failure reading checkin file " |
| 1509 | + mStats.mCheckinFile.getBaseFile(), e); |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | } |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1514 | if (DBG) Slog.d(TAG, "begin dumpCheckinLocked from UID " + Binder.getCallingUid()); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1515 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1516 | mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1517 | if (writeData) { |
| 1518 | mStats.writeAsyncLocked(); |
| 1519 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1520 | } |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1521 | if (DBG) Slog.d(TAG, "end dumpCheckinLocked"); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1522 | } else { |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1523 | if (DBG) Slog.d(TAG, "begin dumpLocked from UID " + Binder.getCallingUid()); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1524 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1525 | mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1526 | if (writeData) { |
| 1527 | mStats.writeAsyncLocked(); |
| 1528 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1529 | } |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1530 | if (DBG) Slog.d(TAG, "end dumpLocked"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | } |
| 1532 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1533 | |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1534 | /** |
Siddharth Ray | a1fd057 | 2017-11-13 14:20:47 -0800 | [diff] [blame] | 1535 | * Gets a snapshot of cellular stats |
| 1536 | * @hide |
| 1537 | */ |
| 1538 | public CellularBatteryStats getCellularBatteryStats() { |
| 1539 | synchronized (mStats) { |
| 1540 | return mStats.getCellularBatteryStats(); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /** |
Siddharth Ray | b50a684 | 2017-12-14 15:15:28 -0800 | [diff] [blame] | 1545 | * Gets a snapshot of Wifi stats |
| 1546 | * @hide |
| 1547 | */ |
| 1548 | public WifiBatteryStats getWifiBatteryStats() { |
| 1549 | synchronized (mStats) { |
| 1550 | return mStats.getWifiBatteryStats(); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | /** |
Siddharth Ray | 78ccaf5 | 2017-12-23 16:16:21 -0800 | [diff] [blame] | 1555 | * Gets a snapshot of Gps stats |
| 1556 | * @hide |
| 1557 | */ |
| 1558 | public GpsBatteryStats getGpsBatteryStats() { |
| 1559 | synchronized (mStats) { |
| 1560 | return mStats.getGpsBatteryStats(); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /** |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1565 | * Gets a snapshot of the system health for a particular uid. |
| 1566 | */ |
| 1567 | @Override |
| 1568 | public HealthStatsParceler takeUidSnapshot(int requestUid) { |
| 1569 | if (requestUid != Binder.getCallingUid()) { |
| 1570 | mContext.enforceCallingOrSelfPermission( |
| 1571 | android.Manifest.permission.BATTERY_STATS, null); |
| 1572 | } |
| 1573 | long ident = Binder.clearCallingIdentity(); |
| 1574 | try { |
Sudheer Shanka | e56013a | 2018-04-23 11:22:15 -0700 | [diff] [blame] | 1575 | if (shouldCollectExternalStats()) { |
| 1576 | syncStats("get-health-stats-for-uids", BatteryExternalStatsWorker.UPDATE_ALL); |
| 1577 | } |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1578 | synchronized (mStats) { |
| 1579 | return getHealthStatsForUidLocked(requestUid); |
| 1580 | } |
| 1581 | } catch (Exception ex) { |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1582 | Slog.w(TAG, "Crashed while writing for takeUidSnapshot(" + requestUid + ")", ex); |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1583 | throw ex; |
| 1584 | } finally { |
| 1585 | Binder.restoreCallingIdentity(ident); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * Gets a snapshot of the system health for a number of uids. |
| 1591 | */ |
| 1592 | @Override |
| 1593 | public HealthStatsParceler[] takeUidSnapshots(int[] requestUids) { |
| 1594 | if (!onlyCaller(requestUids)) { |
| 1595 | mContext.enforceCallingOrSelfPermission( |
| 1596 | android.Manifest.permission.BATTERY_STATS, null); |
| 1597 | } |
| 1598 | long ident = Binder.clearCallingIdentity(); |
| 1599 | int i=-1; |
| 1600 | try { |
Sudheer Shanka | e56013a | 2018-04-23 11:22:15 -0700 | [diff] [blame] | 1601 | if (shouldCollectExternalStats()) { |
| 1602 | syncStats("get-health-stats-for-uids", BatteryExternalStatsWorker.UPDATE_ALL); |
| 1603 | } |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1604 | synchronized (mStats) { |
| 1605 | final int N = requestUids.length; |
| 1606 | final HealthStatsParceler[] results = new HealthStatsParceler[N]; |
| 1607 | for (i=0; i<N; i++) { |
| 1608 | results[i] = getHealthStatsForUidLocked(requestUids[i]); |
| 1609 | } |
| 1610 | return results; |
| 1611 | } |
| 1612 | } catch (Exception ex) { |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 1613 | if (DBG) Slog.d(TAG, "Crashed while writing for takeUidSnapshots(" |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1614 | + Arrays.toString(requestUids) + ") i=" + i, ex); |
| 1615 | throw ex; |
| 1616 | } finally { |
| 1617 | Binder.restoreCallingIdentity(ident); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Sudheer Shanka | e56013a | 2018-04-23 11:22:15 -0700 | [diff] [blame] | 1621 | private boolean shouldCollectExternalStats() { |
| 1622 | return (SystemClock.elapsedRealtime() - mWorker.getLastCollectionTimeStamp()) |
| 1623 | > mStats.getExternalStatsCollectionRateLimitMs(); |
| 1624 | } |
| 1625 | |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1626 | /** |
| 1627 | * Returns whether the Binder.getCallingUid is the only thing in requestUids. |
| 1628 | */ |
| 1629 | private static boolean onlyCaller(int[] requestUids) { |
| 1630 | final int caller = Binder.getCallingUid(); |
| 1631 | final int N = requestUids.length; |
| 1632 | for (int i=0; i<N; i++) { |
| 1633 | if (requestUids[i] != caller) { |
| 1634 | return false; |
| 1635 | } |
| 1636 | } |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * Gets a HealthStatsParceler for the given uid. You should probably call |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 1642 | * updateExternalStatsSync first. |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 1643 | */ |
| 1644 | HealthStatsParceler getHealthStatsForUidLocked(int requestUid) { |
| 1645 | final HealthStatsBatteryStatsWriter writer = new HealthStatsBatteryStatsWriter(); |
| 1646 | final HealthStatsWriter uidWriter = new HealthStatsWriter(UidHealthStats.CONSTANTS); |
| 1647 | final BatteryStats.Uid uid = mStats.getUidStats().get(requestUid); |
| 1648 | if (uid != null) { |
| 1649 | writer.writeUid(uidWriter, mStats, uid); |
| 1650 | } |
| 1651 | return new HealthStatsParceler(uidWriter); |
| 1652 | } |
| 1653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | } |