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