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 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothActivityEnergyInfo; |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 20 | import android.bluetooth.BluetoothAdapter; |
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.IWifiManager; |
| 25 | import android.net.wifi.WifiActivityEnergyInfo; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 26 | import android.os.BatteryStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.Binder; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
| 31 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.ServiceManager; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 39 | import android.os.SystemClock; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 40 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 41 | import android.os.WorkSource; |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 42 | import android.telephony.DataConnectionRealTimeInfo; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 43 | import android.telephony.SignalStrength; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 44 | import android.telephony.TelephonyManager; |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 45 | import android.util.IntArray; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 46 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 48 | import android.util.TimeUtils; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 49 | import com.android.internal.annotations.GuardedBy; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 50 | import com.android.internal.app.IBatteryStats; |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 51 | import com.android.internal.os.BatteryStatsHelper; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 52 | import com.android.internal.os.BatteryStatsImpl; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 53 | import com.android.internal.os.PowerProfile; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 54 | import com.android.server.FgThread; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 55 | import com.android.server.LocalServices; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 56 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 57 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import java.io.FileDescriptor; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 59 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import java.io.PrintWriter; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 61 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * All information we are collecting about things that can happen that impact |
| 65 | * battery life. |
| 66 | */ |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 67 | public final class BatteryStatsService extends IBatteryStats.Stub |
| 68 | implements PowerManagerInternal.LowPowerModeListener { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 69 | static final String TAG = "BatteryStatsService"; |
| 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | static IBatteryStats sService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | final BatteryStatsImpl mStats; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 73 | final BatteryStatsHandler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | Context mContext; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 75 | PowerManagerInternal mPowerManagerInternal; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 77 | final int UPDATE_CPU = 0x01; |
| 78 | final int UPDATE_WIFI = 0x02; |
| 79 | final int UPDATE_RADIO = 0x04; |
| 80 | final int UPDATE_BT = 0x08; |
| 81 | final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT; |
| 82 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 83 | class BatteryStatsHandler extends Handler implements BatteryStatsImpl.ExternalStatsSync { |
| 84 | public static final int MSG_SYNC_EXTERNAL_STATS = 1; |
| 85 | public static final int MSG_WRITE_TO_DISK = 2; |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 86 | private int mUpdateFlags = 0; |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 87 | private IntArray mUidsToRemove = new IntArray(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 88 | |
| 89 | public BatteryStatsHandler(Looper looper) { |
| 90 | super(looper); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void handleMessage(Message msg) { |
| 95 | switch (msg.what) { |
| 96 | case MSG_SYNC_EXTERNAL_STATS: |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 97 | final int updateFlags; |
| 98 | synchronized (this) { |
| 99 | removeMessages(MSG_SYNC_EXTERNAL_STATS); |
| 100 | updateFlags = mUpdateFlags; |
| 101 | mUpdateFlags = 0; |
| 102 | } |
| 103 | updateExternalStats((String)msg.obj, updateFlags); |
Adam Lesinski | 56d82dd | 2015-07-14 16:27:40 -0700 | [diff] [blame] | 104 | |
| 105 | // other parts of the system could be calling into us |
| 106 | // from mStats in order to report of changes. We must grab the mStats |
| 107 | // lock before grabbing our own or we'll end up in a deadlock. |
| 108 | synchronized (mStats) { |
| 109 | synchronized (this) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 110 | final int numUidsToRemove = mUidsToRemove.size(); |
| 111 | for (int i = 0; i < numUidsToRemove; i++) { |
| 112 | mStats.removeIsolatedUidLocked(mUidsToRemove.get(i)); |
| 113 | } |
| 114 | } |
| 115 | mUidsToRemove.clear(); |
| 116 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 117 | break; |
| 118 | |
| 119 | case MSG_WRITE_TO_DISK: |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 120 | updateExternalStats("write", UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 121 | synchronized (mStats) { |
| 122 | mStats.writeAsyncLocked(); |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 129 | public void scheduleSync(String reason) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 130 | synchronized (this) { |
| 131 | scheduleSyncLocked(reason, UPDATE_ALL); |
| 132 | } |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void scheduleWifiSync(String reason) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 137 | synchronized (this) { |
| 138 | scheduleSyncLocked(reason, UPDATE_WIFI); |
| 139 | } |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void scheduleCpuSyncDueToRemovedUid(int uid) { |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 144 | synchronized (this) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 145 | scheduleSyncLocked("remove-uid", UPDATE_CPU); |
| 146 | mUidsToRemove.add(uid); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 149 | |
| 150 | private void scheduleSyncLocked(String reason, int updateFlags) { |
| 151 | if (mUpdateFlags == 0) { |
| 152 | sendMessage(Message.obtain(this, MSG_SYNC_EXTERNAL_STATS, reason)); |
| 153 | } |
| 154 | mUpdateFlags |= updateFlags; |
| 155 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 158 | BatteryStatsService(File systemDir, Handler handler) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 159 | // Our handler here will be accessing the disk, use a different thread than |
| 160 | // what the ActivityManagerService gave us (no I/O on that one!). |
| 161 | mHandler = new BatteryStatsHandler(FgThread.getHandler().getLooper()); |
| 162 | |
| 163 | // BatteryStatsImpl expects the ActivityManagerService handler, so pass that one through. |
| 164 | mStats = new BatteryStatsImpl(systemDir, handler, mHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | public void publish(Context context) { |
| 168 | mContext = context; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 169 | ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder()); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 170 | mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps()); |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 171 | mStats.setRadioScanningTimeout(mContext.getResources().getInteger( |
| 172 | com.android.internal.R.integer.config_radioScanningTimeout) |
| 173 | * 1000L); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 174 | mStats.setPowerProfile(new PowerProfile(context)); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * At the time when the constructor runs, the power manager has not yet been |
| 179 | * initialized. So we initialize the low power observer later. |
| 180 | */ |
| 181 | public void initPowerManagement() { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 182 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 183 | mPowerManagerInternal.registerLowPowerModeObserver(this); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 184 | mStats.notePowerSaveMode(mPowerManagerInternal.getLowPowerModeEnabled()); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 185 | (new WakeupReasonThread()).start(); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 188 | public void shutdown() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 189 | Slog.w("BatteryStats", "Writing battery stats before shutdown..."); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 191 | updateExternalStats("shutdown", UPDATE_ALL); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 192 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 193 | mStats.shutdownLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | public static IBatteryStats getService() { |
| 198 | if (sService != null) { |
| 199 | return sService; |
| 200 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 201 | IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | sService = asInterface(b); |
| 203 | return sService; |
| 204 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 205 | |
| 206 | @Override |
| 207 | public void onLowPowerModeChanged(boolean enabled) { |
| 208 | synchronized (mStats) { |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 209 | mStats.notePowerSaveMode(enabled); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | /** |
| 214 | * @return the current statistics object, which may be modified |
| 215 | * to reflect events that affect battery usage. You must lock the |
| 216 | * stats object before doing anything with it. |
| 217 | */ |
| 218 | public BatteryStatsImpl getActiveStatistics() { |
| 219 | return mStats; |
| 220 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 221 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Schedules a write to disk to occur. This will cause the BatteryStatsImpl |
| 224 | * object to update with the latest info, then write to disk. |
| 225 | */ |
| 226 | public void scheduleWriteToDisk() { |
| 227 | mHandler.sendEmptyMessage(BatteryStatsHandler.MSG_WRITE_TO_DISK); |
| 228 | } |
| 229 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 230 | // These are for direct use by the activity manager... |
| 231 | |
Adam Lesinski | b83ffee | 2015-05-12 14:43:47 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Remove a UID from the BatteryStats and BatteryStats' external dependencies. |
| 234 | */ |
| 235 | void removeUid(int uid) { |
| 236 | synchronized (mStats) { |
| 237 | mStats.removeUidStatsLocked(uid); |
| 238 | } |
| 239 | } |
| 240 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 241 | void addIsolatedUid(int isolatedUid, int appUid) { |
| 242 | synchronized (mStats) { |
| 243 | mStats.addIsolatedUidLocked(isolatedUid, appUid); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void removeIsolatedUid(int isolatedUid, int appUid) { |
| 248 | synchronized (mStats) { |
Adam Lesinski | 61db88f | 2015-07-01 15:05:07 -0700 | [diff] [blame] | 249 | mStats.scheduleRemoveIsolatedUidLocked(isolatedUid, appUid); |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | void noteProcessStart(String name, int uid) { |
| 254 | synchronized (mStats) { |
| 255 | mStats.noteProcessStartLocked(name, uid); |
| 256 | } |
| 257 | } |
| 258 | |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 259 | void noteProcessCrash(String name, int uid) { |
| 260 | synchronized (mStats) { |
| 261 | mStats.noteProcessCrashLocked(name, uid); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void noteProcessAnr(String name, int uid) { |
| 266 | synchronized (mStats) { |
| 267 | mStats.noteProcessAnrLocked(name, uid); |
| 268 | } |
| 269 | } |
| 270 | |
Dianne Hackborn | 2f1993e | 2014-09-26 11:28:01 -0700 | [diff] [blame] | 271 | void noteProcessState(String name, int uid, int state) { |
| 272 | synchronized (mStats) { |
| 273 | mStats.noteProcessStateLocked(name, uid, state); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void noteProcessFinish(String name, int uid) { |
| 278 | synchronized (mStats) { |
| 279 | mStats.noteProcessFinishLocked(name, uid); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Public interface... |
| 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | public byte[] getStatistics() { |
| 286 | mContext.enforceCallingPermission( |
| 287 | android.Manifest.permission.BATTERY_STATS, null); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 288 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 289 | //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] | 290 | Parcel out = Parcel.obtain(); |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 291 | updateExternalStats("get-stats", UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 292 | synchronized (mStats) { |
| 293 | mStats.writeToParcel(out, 0); |
| 294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | byte[] data = out.marshall(); |
| 296 | out.recycle(); |
| 297 | return data; |
| 298 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 299 | |
| 300 | public ParcelFileDescriptor getStatisticsStream() { |
| 301 | mContext.enforceCallingPermission( |
| 302 | android.Manifest.permission.BATTERY_STATS, null); |
| 303 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 304 | //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM)); |
| 305 | Parcel out = Parcel.obtain(); |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 306 | updateExternalStats("get-stats", UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 307 | synchronized (mStats) { |
| 308 | mStats.writeToParcel(out, 0); |
| 309 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 310 | byte[] data = out.marshall(); |
| 311 | out.recycle(); |
| 312 | try { |
| 313 | return ParcelFileDescriptor.fromData(data, "battery-stats"); |
| 314 | } catch (IOException e) { |
| 315 | Slog.w(TAG, "Unable to create shared memory", e); |
| 316 | return null; |
| 317 | } |
| 318 | } |
| 319 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 320 | public boolean isCharging() { |
| 321 | synchronized (mStats) { |
| 322 | return mStats.isCharging(); |
| 323 | } |
| 324 | } |
| 325 | |
Dianne Hackborn | ab5c0ea | 2014-04-29 14:53:32 -0700 | [diff] [blame] | 326 | public long computeBatteryTimeRemaining() { |
| 327 | synchronized (mStats) { |
| 328 | long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime()); |
| 329 | return time >= 0 ? (time/1000) : time; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | public long computeChargeTimeRemaining() { |
| 334 | synchronized (mStats) { |
| 335 | long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime()); |
| 336 | return time >= 0 ? (time/1000) : time; |
| 337 | } |
| 338 | } |
| 339 | |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 340 | public void noteEvent(int code, String name, int uid) { |
| 341 | enforceCallingPermission(); |
| 342 | synchronized (mStats) { |
| 343 | mStats.noteEventLocked(code, name, uid); |
| 344 | } |
| 345 | } |
| 346 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 347 | public void noteSyncStart(String name, int uid) { |
| 348 | enforceCallingPermission(); |
| 349 | synchronized (mStats) { |
| 350 | mStats.noteSyncStartLocked(name, uid); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | public void noteSyncFinish(String name, int uid) { |
| 355 | enforceCallingPermission(); |
| 356 | synchronized (mStats) { |
| 357 | mStats.noteSyncFinishLocked(name, uid); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | public void noteJobStart(String name, int uid) { |
| 362 | enforceCallingPermission(); |
| 363 | synchronized (mStats) { |
| 364 | mStats.noteJobStartLocked(name, uid); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | public void noteJobFinish(String name, int uid) { |
| 369 | enforceCallingPermission(); |
| 370 | synchronized (mStats) { |
| 371 | mStats.noteJobFinishLocked(name, uid); |
| 372 | } |
| 373 | } |
| 374 | |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 375 | public void noteAlarmStart(String name, int uid) { |
| 376 | enforceCallingPermission(); |
| 377 | synchronized (mStats) { |
| 378 | mStats.noteAlarmStartLocked(name, uid); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | public void noteAlarmFinish(String name, int uid) { |
| 383 | enforceCallingPermission(); |
| 384 | synchronized (mStats) { |
| 385 | mStats.noteAlarmFinishLocked(name, uid); |
| 386 | } |
| 387 | } |
| 388 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 389 | 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] | 390 | boolean unimportantForLogging) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | enforceCallingPermission(); |
| 392 | synchronized (mStats) { |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 393 | mStats.noteStartWakeLocked(uid, pid, name, historyName, type, unimportantForLogging, |
Dianne Hackborn | 40c8725 | 2014-03-19 16:55:40 -0700 | [diff] [blame] | 394 | SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 398 | 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] | 399 | enforceCallingPermission(); |
| 400 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 401 | mStats.noteStopWakeLocked(uid, pid, name, historyName, type, |
| 402 | SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 406 | public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, |
| 407 | String historyName, int type, boolean unimportantForLogging) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 408 | enforceCallingPermission(); |
| 409 | synchronized (mStats) { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 410 | mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName, |
| 411 | type, unimportantForLogging); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 415 | public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name, |
| 416 | String historyName, int type, WorkSource newWs, int newPid, String newName, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 417 | String newHistoryName, int newType, boolean newUnimportantForLogging) { |
| 418 | enforceCallingPermission(); |
| 419 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 420 | mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 421 | newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging); |
| 422 | } |
| 423 | } |
| 424 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 425 | public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName, |
| 426 | int type) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 427 | enforceCallingPermission(); |
| 428 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 429 | mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | public void noteStartSensor(int uid, int sensor) { |
| 434 | enforceCallingPermission(); |
| 435 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 436 | mStats.noteStartSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | public void noteStopSensor(int uid, int sensor) { |
| 441 | enforceCallingPermission(); |
| 442 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 443 | mStats.noteStopSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 447 | public void noteVibratorOn(int uid, long durationMillis) { |
| 448 | enforceCallingPermission(); |
| 449 | synchronized (mStats) { |
| 450 | mStats.noteVibratorOnLocked(uid, durationMillis); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | public void noteVibratorOff(int uid) { |
| 455 | enforceCallingPermission(); |
| 456 | synchronized (mStats) { |
| 457 | mStats.noteVibratorOffLocked(uid); |
| 458 | } |
| 459 | } |
| 460 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | public void noteStartGps(int uid) { |
| 462 | enforceCallingPermission(); |
| 463 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 464 | mStats.noteStartGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | public void noteStopGps(int uid) { |
| 469 | enforceCallingPermission(); |
| 470 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 471 | mStats.noteStopGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 475 | public void noteScreenState(int state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | enforceCallingPermission(); |
| 477 | synchronized (mStats) { |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 478 | mStats.noteScreenStateLocked(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 482 | public void noteScreenBrightness(int brightness) { |
| 483 | enforceCallingPermission(); |
| 484 | synchronized (mStats) { |
| 485 | mStats.noteScreenBrightnessLocked(brightness); |
| 486 | } |
| 487 | } |
| 488 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 489 | public void noteUserActivity(int uid, int event) { |
| 490 | enforceCallingPermission(); |
| 491 | synchronized (mStats) { |
| 492 | mStats.noteUserActivityLocked(uid, event); |
| 493 | } |
| 494 | } |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 495 | |
| 496 | public void noteInteractive(boolean interactive) { |
| 497 | enforceCallingPermission(); |
| 498 | synchronized (mStats) { |
| 499 | mStats.noteInteractiveLocked(interactive); |
| 500 | } |
| 501 | } |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 502 | |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 503 | public void noteConnectivityChanged(int type, String extra) { |
| 504 | enforceCallingPermission(); |
| 505 | synchronized (mStats) { |
| 506 | mStats.noteConnectivityChangedLocked(type, extra); |
| 507 | } |
| 508 | } |
| 509 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 510 | public void noteMobileRadioPowerState(int powerState, long timestampNs) { |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 511 | enforceCallingPermission(); |
| 512 | synchronized (mStats) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 513 | mStats.noteMobileRadioPowerState(powerState, timestampNs); |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | public void notePhoneOn() { |
| 518 | enforceCallingPermission(); |
| 519 | synchronized (mStats) { |
| 520 | mStats.notePhoneOnLocked(); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | public void notePhoneOff() { |
| 525 | enforceCallingPermission(); |
| 526 | synchronized (mStats) { |
| 527 | mStats.notePhoneOffLocked(); |
| 528 | } |
| 529 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 530 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 531 | public void notePhoneSignalStrength(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 532 | enforceCallingPermission(); |
| 533 | synchronized (mStats) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 534 | mStats.notePhoneSignalStrengthLocked(signalStrength); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | public void notePhoneDataConnectionState(int dataType, boolean hasData) { |
| 539 | enforceCallingPermission(); |
| 540 | synchronized (mStats) { |
| 541 | mStats.notePhoneDataConnectionStateLocked(dataType, hasData); |
| 542 | } |
| 543 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 544 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 545 | public void notePhoneState(int state) { |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 546 | enforceCallingPermission(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 547 | int simState = TelephonyManager.getDefault().getSimState(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 548 | synchronized (mStats) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 549 | mStats.notePhoneStateLocked(state, simState); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 553 | public void noteWifiOn() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 554 | enforceCallingPermission(); |
| 555 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 556 | mStats.noteWifiOnLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 560 | public void noteWifiOff() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 561 | enforceCallingPermission(); |
| 562 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 563 | mStats.noteWifiOffLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 566 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 567 | public void noteStartAudio(int uid) { |
| 568 | enforceCallingPermission(); |
| 569 | synchronized (mStats) { |
| 570 | mStats.noteAudioOnLocked(uid); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | public void noteStopAudio(int uid) { |
| 575 | enforceCallingPermission(); |
| 576 | synchronized (mStats) { |
| 577 | mStats.noteAudioOffLocked(uid); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | public void noteStartVideo(int uid) { |
| 582 | enforceCallingPermission(); |
| 583 | synchronized (mStats) { |
| 584 | mStats.noteVideoOnLocked(uid); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | public void noteStopVideo(int uid) { |
| 589 | enforceCallingPermission(); |
| 590 | synchronized (mStats) { |
| 591 | mStats.noteVideoOffLocked(uid); |
| 592 | } |
| 593 | } |
| 594 | |
Dianne Hackborn | 10eaa85 | 2014-07-22 22:54:55 -0700 | [diff] [blame] | 595 | public void noteResetAudio() { |
| 596 | enforceCallingPermission(); |
| 597 | synchronized (mStats) { |
| 598 | mStats.noteResetAudioLocked(); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | public void noteResetVideo() { |
| 603 | enforceCallingPermission(); |
| 604 | synchronized (mStats) { |
| 605 | mStats.noteResetVideoLocked(); |
| 606 | } |
| 607 | } |
| 608 | |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 609 | public void noteFlashlightOn(int uid) { |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 610 | enforceCallingPermission(); |
| 611 | synchronized (mStats) { |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 612 | mStats.noteFlashlightOnLocked(uid); |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 616 | public void noteFlashlightOff(int uid) { |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 617 | enforceCallingPermission(); |
| 618 | synchronized (mStats) { |
Ruben Brunk | 6d2c363 | 2015-05-26 17:32:16 -0700 | [diff] [blame] | 619 | mStats.noteFlashlightOffLocked(uid); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | public void noteStartCamera(int uid) { |
| 624 | enforceCallingPermission(); |
| 625 | synchronized (mStats) { |
| 626 | mStats.noteCameraOnLocked(uid); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | public void noteStopCamera(int uid) { |
| 631 | enforceCallingPermission(); |
| 632 | synchronized (mStats) { |
| 633 | mStats.noteCameraOffLocked(uid); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | public void noteResetCamera() { |
| 638 | enforceCallingPermission(); |
| 639 | synchronized (mStats) { |
| 640 | mStats.noteResetCameraLocked(); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | public void noteResetFlashlight() { |
| 645 | enforceCallingPermission(); |
| 646 | synchronized (mStats) { |
| 647 | mStats.noteResetFlashlightLocked(); |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 651 | @Override |
| 652 | public void noteWifiRadioPowerState(int powerState, long tsNanos) { |
| 653 | enforceCallingPermission(); |
| 654 | |
| 655 | // There was a change in WiFi power state. |
| 656 | // Collect data now for the past activity. |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 657 | synchronized (mStats) { |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 658 | if (mStats.isOnBattery()) { |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 659 | final String type = (powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH || |
| 660 | powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active" |
| 661 | : "inactive"; |
| 662 | mHandler.scheduleWifiSync("wifi-data: " + type); |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 663 | } |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 664 | mStats.noteWifiRadioPowerState(powerState, tsNanos); |
| 665 | } |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 668 | public void noteWifiRunning(WorkSource ws) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 669 | enforceCallingPermission(); |
| 670 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 671 | mStats.noteWifiRunningLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 675 | public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 676 | enforceCallingPermission(); |
| 677 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 678 | mStats.noteWifiRunningChangedLocked(oldWs, newWs); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | public void noteWifiStopped(WorkSource ws) { |
| 683 | enforceCallingPermission(); |
| 684 | synchronized (mStats) { |
| 685 | mStats.noteWifiStoppedLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Dianne Hackborn | ca1bf21 | 2014-02-14 14:18:36 -0800 | [diff] [blame] | 689 | public void noteWifiState(int wifiState, String accessPoint) { |
| 690 | enforceCallingPermission(); |
| 691 | synchronized (mStats) { |
| 692 | mStats.noteWifiStateLocked(wifiState, accessPoint); |
| 693 | } |
| 694 | } |
| 695 | |
Dianne Hackborn | 3251b90 | 2014-06-20 14:40:53 -0700 | [diff] [blame] | 696 | public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) { |
| 697 | enforceCallingPermission(); |
| 698 | synchronized (mStats) { |
| 699 | mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | public void noteWifiRssiChanged(int newRssi) { |
| 704 | enforceCallingPermission(); |
| 705 | synchronized (mStats) { |
| 706 | mStats.noteWifiRssiChangedLocked(newRssi); |
| 707 | } |
| 708 | } |
| 709 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 710 | public void noteFullWifiLockAcquired(int uid) { |
| 711 | enforceCallingPermission(); |
| 712 | synchronized (mStats) { |
| 713 | mStats.noteFullWifiLockAcquiredLocked(uid); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | public void noteFullWifiLockReleased(int uid) { |
| 718 | enforceCallingPermission(); |
| 719 | synchronized (mStats) { |
| 720 | mStats.noteFullWifiLockReleasedLocked(uid); |
| 721 | } |
| 722 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 723 | |
| 724 | public void noteWifiScanStarted(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 725 | enforceCallingPermission(); |
| 726 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 727 | mStats.noteWifiScanStartedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 728 | } |
| 729 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 730 | |
| 731 | public void noteWifiScanStopped(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 732 | enforceCallingPermission(); |
| 733 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 734 | mStats.noteWifiScanStoppedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 738 | public void noteWifiMulticastEnabled(int uid) { |
| 739 | enforceCallingPermission(); |
| 740 | synchronized (mStats) { |
| 741 | mStats.noteWifiMulticastEnabledLocked(uid); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | public void noteWifiMulticastDisabled(int uid) { |
| 746 | enforceCallingPermission(); |
| 747 | synchronized (mStats) { |
| 748 | mStats.noteWifiMulticastDisabledLocked(uid); |
| 749 | } |
| 750 | } |
| 751 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 752 | public void noteFullWifiLockAcquiredFromSource(WorkSource ws) { |
| 753 | enforceCallingPermission(); |
| 754 | synchronized (mStats) { |
| 755 | mStats.noteFullWifiLockAcquiredFromSourceLocked(ws); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | public void noteFullWifiLockReleasedFromSource(WorkSource ws) { |
| 760 | enforceCallingPermission(); |
| 761 | synchronized (mStats) { |
| 762 | mStats.noteFullWifiLockReleasedFromSourceLocked(ws); |
| 763 | } |
| 764 | } |
| 765 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 766 | public void noteWifiScanStartedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 767 | enforceCallingPermission(); |
| 768 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 769 | mStats.noteWifiScanStartedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 773 | public void noteWifiScanStoppedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 774 | enforceCallingPermission(); |
| 775 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 776 | mStats.noteWifiScanStoppedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 780 | public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) { |
| 781 | enforceCallingPermission(); |
| 782 | synchronized (mStats) { |
| 783 | mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) { |
| 788 | enforceCallingPermission(); |
| 789 | synchronized (mStats) { |
| 790 | mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws); |
| 791 | } |
| 792 | } |
| 793 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 794 | public void noteWifiMulticastEnabledFromSource(WorkSource ws) { |
| 795 | enforceCallingPermission(); |
| 796 | synchronized (mStats) { |
| 797 | mStats.noteWifiMulticastEnabledFromSourceLocked(ws); |
| 798 | } |
| 799 | } |
| 800 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 801 | @Override |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 802 | public void noteWifiMulticastDisabledFromSource(WorkSource ws) { |
| 803 | enforceCallingPermission(); |
| 804 | synchronized (mStats) { |
| 805 | mStats.noteWifiMulticastDisabledFromSourceLocked(ws); |
| 806 | } |
| 807 | } |
| 808 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 809 | @Override |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 810 | public void noteNetworkInterfaceType(String iface, int networkType) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 811 | enforceCallingPermission(); |
| 812 | synchronized (mStats) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 813 | mStats.noteNetworkInterfaceTypeLocked(iface, networkType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 817 | @Override |
| 818 | public void noteNetworkStatsEnabled() { |
| 819 | enforceCallingPermission(); |
| 820 | synchronized (mStats) { |
| 821 | mStats.noteNetworkStatsEnabledLocked(); |
| 822 | } |
| 823 | } |
| 824 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 825 | @Override |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 826 | public void noteDeviceIdleMode(boolean enabled, String activeReason, int activeUid) { |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 827 | enforceCallingPermission(); |
| 828 | synchronized (mStats) { |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 829 | mStats.noteDeviceIdleModeLocked(enabled, activeReason, activeUid); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
| 833 | public void notePackageInstalled(String pkgName, int versionCode) { |
| 834 | enforceCallingPermission(); |
| 835 | synchronized (mStats) { |
| 836 | mStats.notePackageInstalledLocked(pkgName, versionCode); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | public void notePackageUninstalled(String pkgName) { |
| 841 | enforceCallingPermission(); |
| 842 | synchronized (mStats) { |
| 843 | mStats.notePackageUninstalledLocked(pkgName); |
| 844 | } |
| 845 | } |
| 846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | public boolean isOnBattery() { |
| 848 | return mStats.isOnBattery(); |
| 849 | } |
| 850 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 851 | public void setBatteryState(int status, int health, int plugType, int level, |
| 852 | int temp, int volt) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | enforceCallingPermission(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 854 | synchronized (mStats) { |
| 855 | final boolean onBattery = plugType == BatteryStatsImpl.BATTERY_PLUGGED_NONE; |
| 856 | if (mStats.isOnBattery() == onBattery) { |
| 857 | // The battery state has not changed, so we don't need to sync external |
| 858 | // stats immediately. |
| 859 | mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt); |
| 860 | return; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // Sync external stats first as the battery has changed states. If we don't sync |
| 865 | // immediately here, we may not collect the relevant data later. |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 866 | updateExternalStats("battery-state", UPDATE_ALL); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 867 | synchronized (mStats) { |
| 868 | mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt); |
| 869 | } |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 870 | } |
| 871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | public long getAwakeTimeBattery() { |
| 873 | mContext.enforceCallingOrSelfPermission( |
| 874 | android.Manifest.permission.BATTERY_STATS, null); |
| 875 | return mStats.getAwakeTimeBattery(); |
| 876 | } |
| 877 | |
| 878 | public long getAwakeTimePlugged() { |
| 879 | mContext.enforceCallingOrSelfPermission( |
| 880 | android.Manifest.permission.BATTERY_STATS, null); |
| 881 | return mStats.getAwakeTimePlugged(); |
| 882 | } |
| 883 | |
| 884 | public void enforceCallingPermission() { |
| 885 | if (Binder.getCallingPid() == Process.myPid()) { |
| 886 | return; |
| 887 | } |
| 888 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 889 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 890 | } |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 891 | |
| 892 | final class WakeupReasonThread extends Thread { |
Adam Lesinski | 87fd322 | 2015-06-25 13:10:36 -0700 | [diff] [blame] | 893 | final String[] mReason = new String[1]; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 894 | |
| 895 | WakeupReasonThread() { |
| 896 | super("BatteryStats_wakeupReason"); |
| 897 | } |
| 898 | |
| 899 | public void run() { |
| 900 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
| 901 | |
| 902 | try { |
| 903 | int num; |
Adam Lesinski | 87fd322 | 2015-06-25 13:10:36 -0700 | [diff] [blame] | 904 | while ((num = nativeWaitWakeup(mReason)) >= 0) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 905 | synchronized (mStats) { |
Adam Lesinski | 87fd322 | 2015-06-25 13:10:36 -0700 | [diff] [blame] | 906 | // num will be either 0 or 1. |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 907 | if (num > 0) { |
Adam Lesinski | 87fd322 | 2015-06-25 13:10:36 -0700 | [diff] [blame] | 908 | mStats.noteWakeupReasonLocked(mReason[0]); |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 909 | } else { |
| 910 | mStats.noteWakeupReasonLocked("unknown"); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | } |
| 914 | } catch (RuntimeException e) { |
| 915 | Slog.e(TAG, "Failure reading wakeup reasons", e); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
Adam Lesinski | 87fd322 | 2015-06-25 13:10:36 -0700 | [diff] [blame] | 920 | private static native int nativeWaitWakeup(String[] outReason); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 921 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 922 | private void dumpHelp(PrintWriter pw) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 923 | pw.println("Battery stats (batterystats) dump options:"); |
Dianne Hackborn | 1e725a7 | 2015-03-24 18:23:19 -0700 | [diff] [blame] | 924 | pw.println(" [--checkin] [--history] [--history-start] [--charged] [-c]"); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 925 | pw.println(" [--daily] [--reset] [--write] [--new-daily] [--read-daily] [-h] [<package.name>]"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 926 | pw.println(" --checkin: format output for a checkin report."); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 927 | pw.println(" --history: show only history data."); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 928 | 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] | 929 | pw.println(" --charged: only output data since last charged."); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 930 | pw.println(" --daily: only output full daily data."); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 931 | pw.println(" --reset: reset the stats, clearing all current data."); |
| 932 | pw.println(" --write: force write current collected stats to disk."); |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 933 | pw.println(" --new-daily: immediately create and write new daily stats record."); |
| 934 | pw.println(" --read-daily: read-load last written daily stats."); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 935 | pw.println(" <package.name>: optional name of package to filter output by."); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 936 | pw.println(" -h: print this help text."); |
| 937 | pw.println("Battery stats (batterystats) commands:"); |
| 938 | pw.println(" enable|disable <option>"); |
| 939 | pw.println(" Enable or disable a running option. Option state is not saved across boots."); |
| 940 | pw.println(" Options are:"); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 941 | pw.println(" full-history: include additional detailed events in battery history:"); |
Dianne Hackborn | 1e38382 | 2015-04-10 14:02:33 -0700 | [diff] [blame] | 942 | pw.println(" wake_lock_in, alarms and proc events"); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 943 | pw.println(" no-auto-reset: don't automatically reset stats when unplugged"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 946 | private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) { |
| 947 | i++; |
| 948 | if (i >= args.length) { |
| 949 | pw.println("Missing option argument for " + (enable ? "--enable" : "--disable")); |
| 950 | dumpHelp(pw); |
| 951 | return -1; |
| 952 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 953 | if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 954 | synchronized (mStats) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 955 | mStats.setRecordAllHistoryLocked(enable); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 956 | } |
Dianne Hackborn | 9a75543 | 2014-05-15 17:05:22 -0700 | [diff] [blame] | 957 | } else if ("no-auto-reset".equals(args[i])) { |
| 958 | synchronized (mStats) { |
| 959 | mStats.setNoAutoReset(enable); |
| 960 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 961 | } else { |
| 962 | pw.println("Unknown enable/disable option: " + args[i]); |
| 963 | dumpHelp(pw); |
| 964 | return -1; |
| 965 | } |
| 966 | return i; |
| 967 | } |
| 968 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 969 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | @Override |
| 971 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 972 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 973 | != PackageManager.PERMISSION_GRANTED) { |
| 974 | pw.println("Permission Denial: can't dump BatteryStats from from pid=" |
| 975 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 976 | + " without permission " + android.Manifest.permission.DUMP); |
| 977 | return; |
| 978 | } |
| 979 | |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 980 | int flags = 0; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 981 | boolean useCheckinFormat = false; |
| 982 | boolean isRealCheckin = false; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 983 | boolean noOutput = false; |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 984 | boolean writeData = false; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 985 | long historyStart = -1; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 986 | int reqUid = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 987 | if (args != null) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 988 | for (int i=0; i<args.length; i++) { |
| 989 | String arg = args[i]; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 990 | if ("--checkin".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 991 | useCheckinFormat = true; |
| 992 | isRealCheckin = true; |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 993 | } else if ("--history".equals(arg)) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 994 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 995 | } else if ("--history-start".equals(arg)) { |
| 996 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 997 | i++; |
| 998 | if (i >= args.length) { |
| 999 | pw.println("Missing time argument for --history-since"); |
| 1000 | dumpHelp(pw); |
| 1001 | return; |
| 1002 | } |
| 1003 | historyStart = Long.parseLong(args[i]); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1004 | writeData = true; |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 1005 | } else if ("-c".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1006 | useCheckinFormat = true; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1007 | flags |= BatteryStats.DUMP_INCLUDE_HISTORY; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1008 | } else if ("--charged".equals(arg)) { |
| 1009 | flags |= BatteryStats.DUMP_CHARGED_ONLY; |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1010 | } else if ("--daily".equals(arg)) { |
| 1011 | flags |= BatteryStats.DUMP_DAILY_ONLY; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1012 | } else if ("--reset".equals(arg)) { |
| 1013 | synchronized (mStats) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 1014 | mStats.resetAllStatsCmdLocked(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1015 | pw.println("Battery stats reset."); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1016 | noOutput = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | } |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1018 | updateExternalStats("dump", UPDATE_ALL); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1019 | } else if ("--write".equals(arg)) { |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1020 | updateExternalStats("dump", UPDATE_ALL); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1021 | synchronized (mStats) { |
| 1022 | mStats.writeSyncLocked(); |
| 1023 | pw.println("Battery stats written."); |
| 1024 | noOutput = true; |
| 1025 | } |
Dianne Hackborn | d4a8af7 | 2015-03-03 10:06:15 -0800 | [diff] [blame] | 1026 | } else if ("--new-daily".equals(arg)) { |
| 1027 | synchronized (mStats) { |
| 1028 | mStats.recordDailyStatsLocked(); |
| 1029 | pw.println("New daily stats written."); |
| 1030 | noOutput = true; |
| 1031 | } |
| 1032 | } else if ("--read-daily".equals(arg)) { |
| 1033 | synchronized (mStats) { |
| 1034 | mStats.readDailyStatsLocked(); |
| 1035 | pw.println("Last daily stats read."); |
| 1036 | noOutput = true; |
| 1037 | } |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1038 | } else if ("--enable".equals(arg) || "enable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1039 | i = doEnableOrDisable(pw, i, args, true); |
| 1040 | if (i < 0) { |
| 1041 | return; |
| 1042 | } |
| 1043 | pw.println("Enabled: " + args[i]); |
| 1044 | return; |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 1045 | } else if ("--disable".equals(arg) || "disable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1046 | i = doEnableOrDisable(pw, i, args, false); |
| 1047 | if (i < 0) { |
| 1048 | return; |
| 1049 | } |
| 1050 | pw.println("Disabled: " + args[i]); |
| 1051 | return; |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1052 | } else if ("-h".equals(arg)) { |
| 1053 | dumpHelp(pw); |
| 1054 | return; |
Mike Lockwood | e817404 | 2011-08-16 12:53:43 -0700 | [diff] [blame] | 1055 | } else if ("-a".equals(arg)) { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 1056 | flags |= BatteryStats.DUMP_VERBOSE; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1057 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1058 | pw.println("Unknown option: " + arg); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 1059 | dumpHelp(pw); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 1060 | return; |
| 1061 | } else { |
| 1062 | // Not an option, last argument must be a package name. |
| 1063 | try { |
| 1064 | reqUid = mContext.getPackageManager().getPackageUid(arg, |
| 1065 | UserHandle.getCallingUserId()); |
| 1066 | } catch (PackageManager.NameNotFoundException e) { |
| 1067 | pw.println("Unknown package: " + arg); |
| 1068 | dumpHelp(pw); |
| 1069 | return; |
| 1070 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | } |
| 1072 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1073 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1074 | if (noOutput) { |
| 1075 | return; |
| 1076 | } |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1077 | |
| 1078 | long ident = Binder.clearCallingIdentity(); |
| 1079 | try { |
| 1080 | if (BatteryStatsHelper.checkWifiOnly(mContext)) { |
| 1081 | flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY; |
| 1082 | } |
| 1083 | // Fetch data from external sources and update the BatteryStatsImpl object with them. |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1084 | updateExternalStats("dump", UPDATE_ALL); |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1085 | } finally { |
| 1086 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 1087 | } |
Dianne Hackborn | 13a6a9b | 2015-04-22 18:03:59 -0700 | [diff] [blame] | 1088 | |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 1089 | if (reqUid >= 0) { |
| 1090 | // By default, if the caller is only interested in a specific package, then |
| 1091 | // we only dump the aggregated data since charged. |
Dianne Hackborn | 1e725a7 | 2015-03-24 18:23:19 -0700 | [diff] [blame] | 1092 | if ((flags&(BatteryStats.DUMP_HISTORY_ONLY|BatteryStats.DUMP_CHARGED_ONLY)) == 0) { |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 1093 | flags |= BatteryStats.DUMP_CHARGED_ONLY; |
| 1094 | // Also if they are doing -c, we don't want history. |
| 1095 | flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY; |
| 1096 | } |
| 1097 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1098 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1099 | if (useCheckinFormat) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1100 | List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1101 | if (isRealCheckin) { |
| 1102 | // For a real checkin, first we want to prefer to use the last complete checkin |
| 1103 | // file if there is one. |
| 1104 | synchronized (mStats.mCheckinFile) { |
| 1105 | if (mStats.mCheckinFile.exists()) { |
| 1106 | try { |
| 1107 | byte[] raw = mStats.mCheckinFile.readFully(); |
| 1108 | if (raw != null) { |
| 1109 | Parcel in = Parcel.obtain(); |
| 1110 | in.unmarshall(raw, 0, raw.length); |
| 1111 | in.setDataPosition(0); |
| 1112 | BatteryStatsImpl checkinStats = new BatteryStatsImpl( |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1113 | null, mStats.mHandler, null); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1114 | checkinStats.readSummaryFromParcel(in); |
| 1115 | in.recycle(); |
| 1116 | checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, |
| 1117 | historyStart); |
| 1118 | mStats.mCheckinFile.delete(); |
| 1119 | return; |
| 1120 | } |
Adam Lesinski | 9ae9cba | 2015-07-08 17:09:34 -0700 | [diff] [blame] | 1121 | } catch (IOException | ParcelFormatException e) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 1122 | Slog.w(TAG, "Failure reading checkin file " |
| 1123 | + mStats.mCheckinFile.getBaseFile(), e); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1128 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1129 | mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1130 | if (writeData) { |
| 1131 | mStats.writeAsyncLocked(); |
| 1132 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1133 | } |
| 1134 | } else { |
| 1135 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 1136 | mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 1137 | if (writeData) { |
| 1138 | mStats.writeAsyncLocked(); |
| 1139 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 1140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | } |
| 1142 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1143 | |
| 1144 | // Objects for extracting data from external sources. |
| 1145 | private final Object mExternalStatsLock = new Object(); |
| 1146 | |
| 1147 | @GuardedBy("mExternalStatsLock") |
| 1148 | private IWifiManager mWifiManager; |
| 1149 | |
| 1150 | // WiFi keeps an accumulated total of stats, unlike Bluetooth. |
| 1151 | // Keep the last WiFi stats so we can compute a delta. |
| 1152 | @GuardedBy("mExternalStatsLock") |
| 1153 | private WifiActivityEnergyInfo mLastInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0); |
| 1154 | |
| 1155 | @GuardedBy("mExternalStatsLock") |
| 1156 | private WifiActivityEnergyInfo pullWifiEnergyInfoLocked() { |
| 1157 | if (mWifiManager == null) { |
| 1158 | mWifiManager = IWifiManager.Stub.asInterface( |
| 1159 | ServiceManager.getService(Context.WIFI_SERVICE)); |
| 1160 | if (mWifiManager == null) { |
| 1161 | return null; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | try { |
| 1166 | // We read the data even if we are not on battery. This is so that we keep the |
| 1167 | // correct delta from when we should start reading (aka when we are on battery). |
| 1168 | WifiActivityEnergyInfo info = mWifiManager.reportActivityInfo(); |
| 1169 | if (info != null && info.isValid()) { |
Adam Lesinski | f4013aa | 2015-06-12 15:18:06 -0700 | [diff] [blame] | 1170 | if (info.mControllerEnergyUsed < 0 || info.mControllerIdleTimeMs < 0 || |
| 1171 | info.mControllerRxTimeMs < 0 || info.mControllerTxTimeMs < 0) { |
| 1172 | Slog.wtf(TAG, "Reported WiFi energy data is invalid: " + info); |
| 1173 | return null; |
| 1174 | } |
| 1175 | |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 1176 | final long timePeriodMs = info.mTimestamp - mLastInfo.mTimestamp; |
| 1177 | final long lastIdleMs = mLastInfo.mControllerIdleTimeMs; |
| 1178 | final long lastTxMs = mLastInfo.mControllerTxTimeMs; |
| 1179 | final long lastRxMs = mLastInfo.mControllerRxTimeMs; |
| 1180 | final long lastEnergy = mLastInfo.mControllerEnergyUsed; |
| 1181 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1182 | // We will modify the last info object to be the delta, and store the new |
| 1183 | // WifiActivityEnergyInfo object as our last one. |
| 1184 | final WifiActivityEnergyInfo result = mLastInfo; |
| 1185 | result.mTimestamp = info.getTimeStamp(); |
| 1186 | result.mStackState = info.getStackState(); |
Adam Lesinski | f4013aa | 2015-06-12 15:18:06 -0700 | [diff] [blame] | 1187 | |
| 1188 | // These times seem to be the most reliable. |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 1189 | result.mControllerTxTimeMs = info.mControllerTxTimeMs - lastTxMs; |
| 1190 | result.mControllerRxTimeMs = info.mControllerRxTimeMs - lastRxMs; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 1191 | |
| 1192 | // WiFi calculates the idle time as a difference from the on time and the various |
| 1193 | // Rx + Tx times. There seems to be some missing time there because this sometimes |
| 1194 | // becomes negative. Just cap it at 0 and move on. |
Adam Lesinski | f4013aa | 2015-06-12 15:18:06 -0700 | [diff] [blame] | 1195 | // b/21613534 |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 1196 | result.mControllerIdleTimeMs = Math.max(0, info.mControllerIdleTimeMs - lastIdleMs); |
Adam Lesinski | f4013aa | 2015-06-12 15:18:06 -0700 | [diff] [blame] | 1197 | result.mControllerEnergyUsed = |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 1198 | Math.max(0, info.mControllerEnergyUsed - lastEnergy); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 1199 | |
| 1200 | if (result.mControllerTxTimeMs < 0 || |
| 1201 | result.mControllerRxTimeMs < 0) { |
| 1202 | // The stats were reset by the WiFi system (which is why our delta is negative). |
| 1203 | // Returns the unaltered stats. |
| 1204 | result.mControllerEnergyUsed = info.mControllerEnergyUsed; |
| 1205 | result.mControllerRxTimeMs = info.mControllerRxTimeMs; |
| 1206 | result.mControllerTxTimeMs = info.mControllerTxTimeMs; |
| 1207 | result.mControllerIdleTimeMs = info.mControllerIdleTimeMs; |
| 1208 | |
| 1209 | Slog.v(TAG, "WiFi energy data was reset, new WiFi energy data is " + result); |
| 1210 | } |
Adam Lesinski | bd6704f | 2015-06-18 18:05:20 -0700 | [diff] [blame] | 1211 | |
| 1212 | final long totalTimeMs = result.mControllerIdleTimeMs + result.mControllerRxTimeMs + |
| 1213 | result.mControllerTxTimeMs; |
| 1214 | if (totalTimeMs > timePeriodMs) { |
| 1215 | StringBuilder sb = new StringBuilder(); |
| 1216 | sb.append("Total time "); |
| 1217 | TimeUtils.formatDuration(totalTimeMs, sb); |
| 1218 | sb.append(" is longer than sample period "); |
| 1219 | TimeUtils.formatDuration(timePeriodMs, sb); |
| 1220 | sb.append(".\n"); |
| 1221 | sb.append("Previous WiFi snapshot: ").append("idle="); |
| 1222 | TimeUtils.formatDuration(lastIdleMs, sb); |
| 1223 | sb.append(" rx="); |
| 1224 | TimeUtils.formatDuration(lastRxMs, sb); |
| 1225 | sb.append(" tx="); |
| 1226 | TimeUtils.formatDuration(lastTxMs, sb); |
| 1227 | sb.append(" e=").append(lastEnergy); |
| 1228 | sb.append("\n"); |
| 1229 | sb.append("Current WiFi snapshot: ").append("idle="); |
| 1230 | TimeUtils.formatDuration(info.mControllerIdleTimeMs, sb); |
| 1231 | sb.append(" rx="); |
| 1232 | TimeUtils.formatDuration(info.mControllerRxTimeMs, sb); |
| 1233 | sb.append(" tx="); |
| 1234 | TimeUtils.formatDuration(info.mControllerTxTimeMs, sb); |
| 1235 | sb.append(" e=").append(info.mControllerEnergyUsed); |
| 1236 | Slog.wtf(TAG, sb.toString()); |
| 1237 | } |
| 1238 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1239 | mLastInfo = info; |
| 1240 | return result; |
| 1241 | } |
| 1242 | } catch (RemoteException e) { |
| 1243 | // Nothing to report, WiFi is dead. |
| 1244 | } |
| 1245 | return null; |
| 1246 | } |
| 1247 | |
| 1248 | @GuardedBy("mExternalStatsLock") |
| 1249 | private BluetoothActivityEnergyInfo pullBluetoothEnergyInfoLocked() { |
| 1250 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 1251 | if (adapter != null) { |
| 1252 | BluetoothActivityEnergyInfo info = adapter.getControllerActivityEnergyInfo( |
| 1253 | BluetoothAdapter.ACTIVITY_ENERGY_INFO_REFRESHED); |
| 1254 | if (info != null && info.isValid()) { |
Adam Lesinski | 8a35137 | 2015-06-15 17:19:07 -0700 | [diff] [blame] | 1255 | if (info.getControllerEnergyUsed() < 0 || info.getControllerIdleTimeMillis() < 0 || |
| 1256 | info.getControllerRxTimeMillis() < 0 || info.getControllerTxTimeMillis() < 0) { |
| 1257 | Slog.wtf(TAG, "Bluetooth energy data is invalid: " + info); |
| 1258 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1259 | return info; |
| 1260 | } |
| 1261 | } |
| 1262 | return null; |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * Fetches data from external sources (WiFi controller, bluetooth chipset) and updates |
| 1267 | * batterystats with that information. |
| 1268 | * |
| 1269 | * We first grab a lock specific to this method, then once all the data has been collected, |
| 1270 | * we grab the mStats lock and update the data. |
Adam Lesinski | c14c273 | 2015-05-21 15:04:18 -0700 | [diff] [blame] | 1271 | * |
Adam Lesinski | c14c273 | 2015-05-21 15:04:18 -0700 | [diff] [blame] | 1272 | * @param reason The reason why this collection was requested. Useful for debugging. |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1273 | * @param updateFlags Which external stats to update. Can be a combination of |
| 1274 | * {@link #UPDATE_CPU}, {@link #UPDATE_RADIO}, {@link #UPDATE_WIFI}, |
| 1275 | * and {@link #UPDATE_BT}. |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1276 | */ |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1277 | void updateExternalStats(final String reason, final int updateFlags) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1278 | synchronized (mExternalStatsLock) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 1279 | if (mContext == null) { |
| 1280 | // We haven't started yet (which means the BatteryStatsImpl object has |
| 1281 | // no power profile. Don't consume data we can't compute yet. |
| 1282 | return; |
| 1283 | } |
| 1284 | |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1285 | if (BatteryStatsImpl.DEBUG_ENERGY) { |
| 1286 | Slog.d(TAG, "Updating external stats: reason=" + reason); |
| 1287 | } |
| 1288 | |
| 1289 | WifiActivityEnergyInfo wifiEnergyInfo = null; |
| 1290 | if ((updateFlags & UPDATE_WIFI) != 0) { |
| 1291 | wifiEnergyInfo = pullWifiEnergyInfoLocked(); |
| 1292 | } |
| 1293 | |
| 1294 | BluetoothActivityEnergyInfo bluetoothEnergyInfo = null; |
| 1295 | if ((updateFlags & UPDATE_BT) != 0) { |
Adam Lesinski | c14c273 | 2015-05-21 15:04:18 -0700 | [diff] [blame] | 1296 | // We only pull bluetooth stats when we have to, as we are not distributing its |
| 1297 | // use amongst apps and the sampling frequency does not matter. |
| 1298 | bluetoothEnergyInfo = pullBluetoothEnergyInfoLocked(); |
Adam Lesinski | c14c273 | 2015-05-21 15:04:18 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1301 | synchronized (mStats) { |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1302 | final long elapsedRealtime = SystemClock.elapsedRealtime(); |
| 1303 | final long uptime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 1304 | if (mStats.mRecordAllHistory) { |
Dianne Hackborn | 0c820db | 2015-04-14 17:47:34 -0700 | [diff] [blame] | 1305 | mStats.addHistoryEventLocked(elapsedRealtime, uptime, |
| 1306 | BatteryStats.HistoryItem.EVENT_COLLECT_EXTERNAL_STATS, reason, 0); |
| 1307 | } |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 1308 | |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1309 | if ((updateFlags & UPDATE_CPU) != 0) { |
| 1310 | mStats.updateCpuTimeLocked(); |
| 1311 | mStats.updateKernelWakelocksLocked(); |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 1312 | } |
Adam Lesinski | a7c90c8 | 2015-06-18 14:52:24 -0700 | [diff] [blame] | 1313 | |
| 1314 | if ((updateFlags & UPDATE_RADIO) != 0) { |
| 1315 | mStats.updateMobileRadioStateLocked(elapsedRealtime); |
| 1316 | } |
| 1317 | |
| 1318 | if ((updateFlags & UPDATE_WIFI) != 0) { |
| 1319 | mStats.updateWifiStateLocked(wifiEnergyInfo); |
| 1320 | } |
| 1321 | |
| 1322 | if ((updateFlags & UPDATE_BT) != 0) { |
| 1323 | mStats.updateBluetoothStateLocked(bluetoothEnergyInfo); |
| 1324 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | } |