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