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 | |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Amith Yamasani | 14fb81a | 2009-11-05 11:41:58 -0800 | [diff] [blame] | 20 | import android.bluetooth.BluetoothHeadset; |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothProfile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 25 | import android.os.BatteryStats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Binder; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 27 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.IBinder; |
| 29 | import android.os.Parcel; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 30 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 31 | import android.os.PowerManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.Process; |
| 33 | import android.os.ServiceManager; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 36 | import android.os.WorkSource; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 37 | import android.telephony.SignalStrength; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 38 | import android.telephony.TelephonyManager; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 39 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 41 | import com.android.internal.app.IBatteryStats; |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 42 | import com.android.internal.os.BatteryStatsHelper; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 43 | import com.android.internal.os.BatteryStatsImpl; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 44 | import com.android.internal.os.PowerProfile; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 45 | import com.android.server.LocalServices; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 46 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 47 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 49 | import java.io.FileOutputStream; |
| 50 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import java.io.PrintWriter; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 52 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * All information we are collecting about things that can happen that impact |
| 56 | * battery life. |
| 57 | */ |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 58 | public final class BatteryStatsService extends IBatteryStats.Stub |
| 59 | implements PowerManagerInternal.LowPowerModeListener { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 60 | static final String TAG = "BatteryStatsService"; |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | static IBatteryStats sService; |
| 63 | |
| 64 | final BatteryStatsImpl mStats; |
| 65 | Context mContext; |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 66 | private boolean mBluetoothPendingStats; |
| 67 | private BluetoothHeadset mBluetoothHeadset; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 68 | PowerManagerInternal mPowerManagerInternal; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 69 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 70 | BatteryStatsService(File systemDir, Handler handler) { |
| 71 | mStats = new BatteryStatsImpl(systemDir, handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public void publish(Context context) { |
| 75 | mContext = context; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 76 | ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder()); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 77 | mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps()); |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 78 | mStats.setRadioScanningTimeout(mContext.getResources().getInteger( |
| 79 | com.android.internal.R.integer.config_radioScanningTimeout) |
| 80 | * 1000L); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * At the time when the constructor runs, the power manager has not yet been |
| 85 | * initialized. So we initialize the low power observer later. |
| 86 | */ |
| 87 | public void initPowerManagement() { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 88 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 89 | mPowerManagerInternal.registerLowPowerModeObserver(this); |
| 90 | mStats.noteLowPowerMode(mPowerManagerInternal.getLowPowerModeEnabled()); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 91 | (new WakeupReasonThread()).start(); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 94 | public void shutdown() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 95 | Slog.w("BatteryStats", "Writing battery stats before shutdown..."); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 96 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 97 | mStats.shutdownLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | public static IBatteryStats getService() { |
| 102 | if (sService != null) { |
| 103 | return sService; |
| 104 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 105 | IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | sService = asInterface(b); |
| 107 | return sService; |
| 108 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | public void onLowPowerModeChanged(boolean enabled) { |
| 112 | synchronized (mStats) { |
| 113 | mStats.noteLowPowerMode(enabled); |
| 114 | } |
| 115 | } |
| 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | /** |
| 118 | * @return the current statistics object, which may be modified |
| 119 | * to reflect events that affect battery usage. You must lock the |
| 120 | * stats object before doing anything with it. |
| 121 | */ |
| 122 | public BatteryStatsImpl getActiveStatistics() { |
| 123 | return mStats; |
| 124 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | public byte[] getStatistics() { |
| 127 | mContext.enforceCallingPermission( |
| 128 | android.Manifest.permission.BATTERY_STATS, null); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 129 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 130 | //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] | 131 | Parcel out = Parcel.obtain(); |
| 132 | mStats.writeToParcel(out, 0); |
| 133 | byte[] data = out.marshall(); |
| 134 | out.recycle(); |
| 135 | return data; |
| 136 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 137 | |
| 138 | public ParcelFileDescriptor getStatisticsStream() { |
| 139 | mContext.enforceCallingPermission( |
| 140 | android.Manifest.permission.BATTERY_STATS, null); |
| 141 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 142 | //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM)); |
| 143 | Parcel out = Parcel.obtain(); |
| 144 | mStats.writeToParcel(out, 0); |
| 145 | byte[] data = out.marshall(); |
| 146 | out.recycle(); |
| 147 | try { |
| 148 | return ParcelFileDescriptor.fromData(data, "battery-stats"); |
| 149 | } catch (IOException e) { |
| 150 | Slog.w(TAG, "Unable to create shared memory", e); |
| 151 | return null; |
| 152 | } |
| 153 | } |
| 154 | |
Dianne Hackborn | ab5c0ea | 2014-04-29 14:53:32 -0700 | [diff] [blame] | 155 | public long computeBatteryTimeRemaining() { |
| 156 | synchronized (mStats) { |
| 157 | long time = mStats.computeBatteryTimeRemaining(SystemClock.elapsedRealtime()); |
| 158 | return time >= 0 ? (time/1000) : time; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public long computeChargeTimeRemaining() { |
| 163 | synchronized (mStats) { |
| 164 | long time = mStats.computeChargeTimeRemaining(SystemClock.elapsedRealtime()); |
| 165 | return time >= 0 ? (time/1000) : time; |
| 166 | } |
| 167 | } |
| 168 | |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 169 | public void addIsolatedUid(int isolatedUid, int appUid) { |
| 170 | enforceCallingPermission(); |
| 171 | synchronized (mStats) { |
| 172 | mStats.addIsolatedUidLocked(isolatedUid, appUid); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public void removeIsolatedUid(int isolatedUid, int appUid) { |
| 177 | enforceCallingPermission(); |
| 178 | synchronized (mStats) { |
| 179 | mStats.removeIsolatedUidLocked(isolatedUid, appUid); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public void noteEvent(int code, String name, int uid) { |
| 184 | enforceCallingPermission(); |
| 185 | synchronized (mStats) { |
| 186 | mStats.noteEventLocked(code, name, uid); |
| 187 | } |
| 188 | } |
| 189 | |
Dianne Hackborn | 61659e5 | 2014-07-09 16:13:01 -0700 | [diff] [blame] | 190 | public void noteProcessStart(String name, int uid) { |
| 191 | enforceCallingPermission(); |
| 192 | synchronized (mStats) { |
| 193 | mStats.noteProcessStartLocked(name, uid); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public void noteProcessState(String name, int uid, int state) { |
| 198 | enforceCallingPermission(); |
| 199 | synchronized (mStats) { |
| 200 | mStats.noteProcessStateLocked(name, uid, state); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | public void noteProcessFinish(String name, int uid) { |
| 205 | enforceCallingPermission(); |
| 206 | synchronized (mStats) { |
| 207 | mStats.noteProcessFinishLocked(name, uid); |
| 208 | } |
| 209 | } |
| 210 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 211 | public void noteSyncStart(String name, int uid) { |
| 212 | enforceCallingPermission(); |
| 213 | synchronized (mStats) { |
| 214 | mStats.noteSyncStartLocked(name, uid); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public void noteSyncFinish(String name, int uid) { |
| 219 | enforceCallingPermission(); |
| 220 | synchronized (mStats) { |
| 221 | mStats.noteSyncFinishLocked(name, uid); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | public void noteJobStart(String name, int uid) { |
| 226 | enforceCallingPermission(); |
| 227 | synchronized (mStats) { |
| 228 | mStats.noteJobStartLocked(name, uid); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | public void noteJobFinish(String name, int uid) { |
| 233 | enforceCallingPermission(); |
| 234 | synchronized (mStats) { |
| 235 | mStats.noteJobFinishLocked(name, uid); |
| 236 | } |
| 237 | } |
| 238 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 239 | 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] | 240 | boolean unimportantForLogging) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | enforceCallingPermission(); |
| 242 | synchronized (mStats) { |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 243 | mStats.noteStartWakeLocked(uid, pid, name, historyName, type, unimportantForLogging, |
Dianne Hackborn | 40c8725 | 2014-03-19 16:55:40 -0700 | [diff] [blame] | 244 | SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 248 | 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] | 249 | enforceCallingPermission(); |
| 250 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 251 | mStats.noteStopWakeLocked(uid, pid, name, historyName, type, |
| 252 | SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 256 | public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, |
| 257 | String historyName, int type, boolean unimportantForLogging) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 258 | enforceCallingPermission(); |
| 259 | synchronized (mStats) { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 260 | mStats.noteStartWakeFromSourceLocked(ws, pid, name, historyName, |
| 261 | type, unimportantForLogging); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 265 | public void noteChangeWakelockFromSource(WorkSource ws, int pid, String name, |
| 266 | String historyName, int type, WorkSource newWs, int newPid, String newName, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 267 | String newHistoryName, int newType, boolean newUnimportantForLogging) { |
| 268 | enforceCallingPermission(); |
| 269 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 270 | mStats.noteChangeWakelockFromSourceLocked(ws, pid, name, historyName, type, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 271 | newWs, newPid, newName, newHistoryName, newType, newUnimportantForLogging); |
| 272 | } |
| 273 | } |
| 274 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 275 | public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, String historyName, |
| 276 | int type) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 277 | enforceCallingPermission(); |
| 278 | synchronized (mStats) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 279 | mStats.noteStopWakeFromSourceLocked(ws, pid, name, historyName, type); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | public void noteStartSensor(int uid, int sensor) { |
| 284 | enforceCallingPermission(); |
| 285 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 286 | mStats.noteStartSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | public void noteStopSensor(int uid, int sensor) { |
| 291 | enforceCallingPermission(); |
| 292 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 293 | mStats.noteStopSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 297 | public void noteVibratorOn(int uid, long durationMillis) { |
| 298 | enforceCallingPermission(); |
| 299 | synchronized (mStats) { |
| 300 | mStats.noteVibratorOnLocked(uid, durationMillis); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public void noteVibratorOff(int uid) { |
| 305 | enforceCallingPermission(); |
| 306 | synchronized (mStats) { |
| 307 | mStats.noteVibratorOffLocked(uid); |
| 308 | } |
| 309 | } |
| 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | public void noteStartGps(int uid) { |
| 312 | enforceCallingPermission(); |
| 313 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 314 | mStats.noteStartGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | public void noteStopGps(int uid) { |
| 319 | enforceCallingPermission(); |
| 320 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 321 | mStats.noteStopGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 325 | public void noteScreenState(int state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | enforceCallingPermission(); |
| 327 | synchronized (mStats) { |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 328 | mStats.noteScreenStateLocked(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 332 | public void noteScreenBrightness(int brightness) { |
| 333 | enforceCallingPermission(); |
| 334 | synchronized (mStats) { |
| 335 | mStats.noteScreenBrightnessLocked(brightness); |
| 336 | } |
| 337 | } |
| 338 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 339 | public void noteUserActivity(int uid, int event) { |
| 340 | enforceCallingPermission(); |
| 341 | synchronized (mStats) { |
| 342 | mStats.noteUserActivityLocked(uid, event); |
| 343 | } |
| 344 | } |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 345 | |
| 346 | public void noteInteractive(boolean interactive) { |
| 347 | enforceCallingPermission(); |
| 348 | synchronized (mStats) { |
| 349 | mStats.noteInteractiveLocked(interactive); |
| 350 | } |
| 351 | } |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 352 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 353 | public void noteMobileRadioPowerState(int powerState, long timestampNs) { |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 354 | enforceCallingPermission(); |
| 355 | synchronized (mStats) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 356 | mStats.noteMobileRadioPowerState(powerState, timestampNs); |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | public void notePhoneOn() { |
| 361 | enforceCallingPermission(); |
| 362 | synchronized (mStats) { |
| 363 | mStats.notePhoneOnLocked(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | public void notePhoneOff() { |
| 368 | enforceCallingPermission(); |
| 369 | synchronized (mStats) { |
| 370 | mStats.notePhoneOffLocked(); |
| 371 | } |
| 372 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 373 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 374 | public void notePhoneSignalStrength(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 375 | enforceCallingPermission(); |
| 376 | synchronized (mStats) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 377 | mStats.notePhoneSignalStrengthLocked(signalStrength); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | public void notePhoneDataConnectionState(int dataType, boolean hasData) { |
| 382 | enforceCallingPermission(); |
| 383 | synchronized (mStats) { |
| 384 | mStats.notePhoneDataConnectionStateLocked(dataType, hasData); |
| 385 | } |
| 386 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 387 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 388 | public void notePhoneState(int state) { |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 389 | enforceCallingPermission(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 390 | int simState = TelephonyManager.getDefault().getSimState(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 391 | synchronized (mStats) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 392 | mStats.notePhoneStateLocked(state, simState); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 396 | public void noteWifiOn() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 397 | enforceCallingPermission(); |
| 398 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 399 | mStats.noteWifiOnLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 403 | public void noteWifiOff() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 404 | enforceCallingPermission(); |
| 405 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 406 | mStats.noteWifiOffLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 409 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 410 | public void noteStartAudio(int uid) { |
| 411 | enforceCallingPermission(); |
| 412 | synchronized (mStats) { |
| 413 | mStats.noteAudioOnLocked(uid); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | public void noteStopAudio(int uid) { |
| 418 | enforceCallingPermission(); |
| 419 | synchronized (mStats) { |
| 420 | mStats.noteAudioOffLocked(uid); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | public void noteStartVideo(int uid) { |
| 425 | enforceCallingPermission(); |
| 426 | synchronized (mStats) { |
| 427 | mStats.noteVideoOnLocked(uid); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | public void noteStopVideo(int uid) { |
| 432 | enforceCallingPermission(); |
| 433 | synchronized (mStats) { |
| 434 | mStats.noteVideoOffLocked(uid); |
| 435 | } |
| 436 | } |
| 437 | |
Dianne Hackborn | 10eaa85 | 2014-07-22 22:54:55 -0700 | [diff] [blame] | 438 | public void noteResetAudio() { |
| 439 | enforceCallingPermission(); |
| 440 | synchronized (mStats) { |
| 441 | mStats.noteResetAudioLocked(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | public void noteResetVideo() { |
| 446 | enforceCallingPermission(); |
| 447 | synchronized (mStats) { |
| 448 | mStats.noteResetVideoLocked(); |
| 449 | } |
| 450 | } |
| 451 | |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 452 | public void noteFlashlightOn() { |
| 453 | enforceCallingPermission(); |
| 454 | synchronized (mStats) { |
| 455 | mStats.noteFlashlightOnLocked(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | public void noteFlashlightOff() { |
| 460 | enforceCallingPermission(); |
| 461 | synchronized (mStats) { |
| 462 | mStats.noteFlashlightOffLocked(); |
| 463 | } |
| 464 | } |
| 465 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 466 | public void noteWifiRunning(WorkSource ws) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 467 | enforceCallingPermission(); |
| 468 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 469 | mStats.noteWifiRunningLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 473 | public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 474 | enforceCallingPermission(); |
| 475 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 476 | mStats.noteWifiRunningChangedLocked(oldWs, newWs); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | public void noteWifiStopped(WorkSource ws) { |
| 481 | enforceCallingPermission(); |
| 482 | synchronized (mStats) { |
| 483 | mStats.noteWifiStoppedLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Dianne Hackborn | ca1bf21 | 2014-02-14 14:18:36 -0800 | [diff] [blame] | 487 | public void noteWifiState(int wifiState, String accessPoint) { |
| 488 | enforceCallingPermission(); |
| 489 | synchronized (mStats) { |
| 490 | mStats.noteWifiStateLocked(wifiState, accessPoint); |
| 491 | } |
| 492 | } |
| 493 | |
Dianne Hackborn | 3251b90 | 2014-06-20 14:40:53 -0700 | [diff] [blame] | 494 | public void noteWifiSupplicantStateChanged(int supplState, boolean failedAuth) { |
| 495 | enforceCallingPermission(); |
| 496 | synchronized (mStats) { |
| 497 | mStats.noteWifiSupplicantStateChangedLocked(supplState, failedAuth); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | public void noteWifiRssiChanged(int newRssi) { |
| 502 | enforceCallingPermission(); |
| 503 | synchronized (mStats) { |
| 504 | mStats.noteWifiRssiChangedLocked(newRssi); |
| 505 | } |
| 506 | } |
| 507 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 508 | public void noteBluetoothOn() { |
| 509 | enforceCallingPermission(); |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 510 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 511 | if (adapter != null) { |
| 512 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 513 | BluetoothProfile.HEADSET); |
| 514 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 515 | synchronized (mStats) { |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 516 | if (mBluetoothHeadset != null) { |
| 517 | mStats.noteBluetoothOnLocked(); |
| 518 | mStats.setBtHeadset(mBluetoothHeadset); |
| 519 | } else { |
| 520 | mBluetoothPendingStats = true; |
| 521 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 524 | |
| 525 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 526 | new BluetoothProfile.ServiceListener() { |
| 527 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 528 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 529 | synchronized (mStats) { |
| 530 | if (mBluetoothPendingStats) { |
| 531 | mStats.noteBluetoothOnLocked(); |
| 532 | mStats.setBtHeadset(mBluetoothHeadset); |
| 533 | mBluetoothPendingStats = false; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | public void onServiceDisconnected(int profile) { |
| 539 | mBluetoothHeadset = null; |
| 540 | } |
| 541 | }; |
| 542 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 543 | public void noteBluetoothOff() { |
| 544 | enforceCallingPermission(); |
| 545 | synchronized (mStats) { |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 546 | mBluetoothPendingStats = false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 547 | mStats.noteBluetoothOffLocked(); |
| 548 | } |
| 549 | } |
| 550 | |
Dianne Hackborn | ca1bf21 | 2014-02-14 14:18:36 -0800 | [diff] [blame] | 551 | public void noteBluetoothState(int bluetoothState) { |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 552 | enforceCallingPermission(); |
| 553 | synchronized (mStats) { |
Dianne Hackborn | ca1bf21 | 2014-02-14 14:18:36 -0800 | [diff] [blame] | 554 | mStats.noteBluetoothStateLocked(bluetoothState); |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 558 | public void noteFullWifiLockAcquired(int uid) { |
| 559 | enforceCallingPermission(); |
| 560 | synchronized (mStats) { |
| 561 | mStats.noteFullWifiLockAcquiredLocked(uid); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | public void noteFullWifiLockReleased(int uid) { |
| 566 | enforceCallingPermission(); |
| 567 | synchronized (mStats) { |
| 568 | mStats.noteFullWifiLockReleasedLocked(uid); |
| 569 | } |
| 570 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 571 | |
| 572 | public void noteWifiScanStarted(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 573 | enforceCallingPermission(); |
| 574 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 575 | mStats.noteWifiScanStartedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 578 | |
| 579 | public void noteWifiScanStopped(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 580 | enforceCallingPermission(); |
| 581 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 582 | mStats.noteWifiScanStoppedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 586 | public void noteWifiMulticastEnabled(int uid) { |
| 587 | enforceCallingPermission(); |
| 588 | synchronized (mStats) { |
| 589 | mStats.noteWifiMulticastEnabledLocked(uid); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | public void noteWifiMulticastDisabled(int uid) { |
| 594 | enforceCallingPermission(); |
| 595 | synchronized (mStats) { |
| 596 | mStats.noteWifiMulticastDisabledLocked(uid); |
| 597 | } |
| 598 | } |
| 599 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 600 | public void noteFullWifiLockAcquiredFromSource(WorkSource ws) { |
| 601 | enforceCallingPermission(); |
| 602 | synchronized (mStats) { |
| 603 | mStats.noteFullWifiLockAcquiredFromSourceLocked(ws); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | public void noteFullWifiLockReleasedFromSource(WorkSource ws) { |
| 608 | enforceCallingPermission(); |
| 609 | synchronized (mStats) { |
| 610 | mStats.noteFullWifiLockReleasedFromSourceLocked(ws); |
| 611 | } |
| 612 | } |
| 613 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 614 | public void noteWifiScanStartedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 615 | enforceCallingPermission(); |
| 616 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 617 | mStats.noteWifiScanStartedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 621 | public void noteWifiScanStoppedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 622 | enforceCallingPermission(); |
| 623 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 624 | mStats.noteWifiScanStoppedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 628 | public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) { |
| 629 | enforceCallingPermission(); |
| 630 | synchronized (mStats) { |
| 631 | mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) { |
| 636 | enforceCallingPermission(); |
| 637 | synchronized (mStats) { |
| 638 | mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws); |
| 639 | } |
| 640 | } |
| 641 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 642 | public void noteWifiMulticastEnabledFromSource(WorkSource ws) { |
| 643 | enforceCallingPermission(); |
| 644 | synchronized (mStats) { |
| 645 | mStats.noteWifiMulticastEnabledFromSourceLocked(ws); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | public void noteWifiMulticastDisabledFromSource(WorkSource ws) { |
| 650 | enforceCallingPermission(); |
| 651 | synchronized (mStats) { |
| 652 | mStats.noteWifiMulticastDisabledFromSourceLocked(ws); |
| 653 | } |
| 654 | } |
| 655 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 656 | @Override |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 657 | public void noteNetworkInterfaceType(String iface, int type) { |
| 658 | enforceCallingPermission(); |
| 659 | synchronized (mStats) { |
| 660 | mStats.noteNetworkInterfaceTypeLocked(iface, type); |
| 661 | } |
| 662 | } |
| 663 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 664 | @Override |
| 665 | public void noteNetworkStatsEnabled() { |
| 666 | enforceCallingPermission(); |
| 667 | synchronized (mStats) { |
| 668 | mStats.noteNetworkStatsEnabledLocked(); |
| 669 | } |
| 670 | } |
| 671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | public boolean isOnBattery() { |
| 673 | return mStats.isOnBattery(); |
| 674 | } |
| 675 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 676 | public void setBatteryState(int status, int health, int plugType, int level, |
| 677 | int temp, int volt) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | enforceCallingPermission(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 679 | mStats.setBatteryState(status, health, plugType, level, temp, volt); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 680 | } |
| 681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | public long getAwakeTimeBattery() { |
| 683 | mContext.enforceCallingOrSelfPermission( |
| 684 | android.Manifest.permission.BATTERY_STATS, null); |
| 685 | return mStats.getAwakeTimeBattery(); |
| 686 | } |
| 687 | |
| 688 | public long getAwakeTimePlugged() { |
| 689 | mContext.enforceCallingOrSelfPermission( |
| 690 | android.Manifest.permission.BATTERY_STATS, null); |
| 691 | return mStats.getAwakeTimePlugged(); |
| 692 | } |
| 693 | |
| 694 | public void enforceCallingPermission() { |
| 695 | if (Binder.getCallingPid() == Process.myPid()) { |
| 696 | return; |
| 697 | } |
| 698 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 699 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 700 | } |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 701 | |
| 702 | final class WakeupReasonThread extends Thread { |
| 703 | final int[] mIrqs = new int[32]; |
| 704 | final String[] mReasons = new String[32]; |
| 705 | |
| 706 | WakeupReasonThread() { |
| 707 | super("BatteryStats_wakeupReason"); |
| 708 | } |
| 709 | |
| 710 | public void run() { |
| 711 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
| 712 | |
| 713 | try { |
| 714 | int num; |
| 715 | while ((num=nativeWaitWakeup(mIrqs, mReasons)) >= 0) { |
| 716 | synchronized (mStats) { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 717 | if (num > 0) { |
| 718 | for (int i=0; i<num; i++) { |
| 719 | mStats.noteWakeupReasonLocked(mReasons[i]); |
| 720 | } |
| 721 | } else { |
| 722 | mStats.noteWakeupReasonLocked("unknown"); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| 726 | } catch (RuntimeException e) { |
| 727 | Slog.e(TAG, "Failure reading wakeup reasons", e); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | private static native int nativeWaitWakeup(int[] outIrqs, String[] outReasons); |
| 733 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 734 | private void dumpHelp(PrintWriter pw) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 735 | pw.println("Battery stats (batterystats) dump options:"); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 736 | pw.println(" [--checkin] [--history] [--history-start] [--unplugged] [--charged] [-c]"); |
| 737 | pw.println(" [--reset] [--write] [-h] [<package.name>]"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 738 | pw.println(" --checkin: format output for a checkin report."); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 739 | pw.println(" --history: show only history data."); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 740 | pw.println(" --history-start <num>: show only history data starting at given time offset."); |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 741 | pw.println(" --unplugged: only output data since last unplugged."); |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 742 | pw.println(" --charged: only output data since last charged."); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 743 | pw.println(" --reset: reset the stats, clearing all current data."); |
| 744 | pw.println(" --write: force write current collected stats to disk."); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 745 | pw.println(" <package.name>: optional name of package to filter output by."); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 746 | pw.println(" -h: print this help text."); |
| 747 | pw.println("Battery stats (batterystats) commands:"); |
| 748 | pw.println(" enable|disable <option>"); |
| 749 | pw.println(" Enable or disable a running option. Option state is not saved across boots."); |
| 750 | pw.println(" Options are:"); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 751 | pw.println(" full-history: include additional detailed events in battery history:"); |
| 752 | pw.println(" wake_lock_in and proc events"); |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 753 | pw.println(" no-auto-reset: don't automatically reset stats when unplugged"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 756 | private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) { |
| 757 | i++; |
| 758 | if (i >= args.length) { |
| 759 | pw.println("Missing option argument for " + (enable ? "--enable" : "--disable")); |
| 760 | dumpHelp(pw); |
| 761 | return -1; |
| 762 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 763 | if ("full-wake-history".equals(args[i]) || "full-history".equals(args[i])) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 764 | synchronized (mStats) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 765 | mStats.setRecordAllHistoryLocked(enable); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 766 | } |
Dianne Hackborn | 9a75543 | 2014-05-15 17:05:22 -0700 | [diff] [blame] | 767 | } else if ("no-auto-reset".equals(args[i])) { |
| 768 | synchronized (mStats) { |
| 769 | mStats.setNoAutoReset(enable); |
| 770 | } |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 771 | } else { |
| 772 | pw.println("Unknown enable/disable option: " + args[i]); |
| 773 | dumpHelp(pw); |
| 774 | return -1; |
| 775 | } |
| 776 | return i; |
| 777 | } |
| 778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | @Override |
| 780 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 781 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 782 | != PackageManager.PERMISSION_GRANTED) { |
| 783 | pw.println("Permission Denial: can't dump BatteryStats from from pid=" |
| 784 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 785 | + " without permission " + android.Manifest.permission.DUMP); |
| 786 | return; |
| 787 | } |
| 788 | |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 789 | int flags = 0; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 790 | boolean useCheckinFormat = false; |
| 791 | boolean isRealCheckin = false; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 792 | boolean noOutput = false; |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 793 | boolean writeData = false; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 794 | long historyStart = -1; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 795 | int reqUid = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 796 | if (args != null) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 797 | for (int i=0; i<args.length; i++) { |
| 798 | String arg = args[i]; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 799 | if ("--checkin".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 800 | useCheckinFormat = true; |
| 801 | isRealCheckin = true; |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 802 | } else if ("--history".equals(arg)) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 803 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 804 | } else if ("--history-start".equals(arg)) { |
| 805 | flags |= BatteryStats.DUMP_HISTORY_ONLY; |
| 806 | i++; |
| 807 | if (i >= args.length) { |
| 808 | pw.println("Missing time argument for --history-since"); |
| 809 | dumpHelp(pw); |
| 810 | return; |
| 811 | } |
| 812 | historyStart = Long.parseLong(args[i]); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 813 | writeData = true; |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 814 | } else if ("-c".equals(arg)) { |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 815 | useCheckinFormat = true; |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 816 | flags |= BatteryStats.DUMP_INCLUDE_HISTORY; |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 817 | } else if ("--unplugged".equals(arg)) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 818 | flags |= BatteryStats.DUMP_UNPLUGGED_ONLY; |
| 819 | } else if ("--charged".equals(arg)) { |
| 820 | flags |= BatteryStats.DUMP_CHARGED_ONLY; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 821 | } else if ("--reset".equals(arg)) { |
| 822 | synchronized (mStats) { |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 823 | mStats.resetAllStatsCmdLocked(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 824 | pw.println("Battery stats reset."); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 825 | noOutput = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 827 | } else if ("--write".equals(arg)) { |
| 828 | synchronized (mStats) { |
| 829 | mStats.writeSyncLocked(); |
| 830 | pw.println("Battery stats written."); |
| 831 | noOutput = true; |
| 832 | } |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 833 | } else if ("--enable".equals(arg) || "enable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 834 | i = doEnableOrDisable(pw, i, args, true); |
| 835 | if (i < 0) { |
| 836 | return; |
| 837 | } |
| 838 | pw.println("Enabled: " + args[i]); |
| 839 | return; |
Dianne Hackborn | fc06413 | 2014-06-02 12:42:12 -0700 | [diff] [blame] | 840 | } else if ("--disable".equals(arg) || "disable".equals(arg)) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 841 | i = doEnableOrDisable(pw, i, args, false); |
| 842 | if (i < 0) { |
| 843 | return; |
| 844 | } |
| 845 | pw.println("Disabled: " + args[i]); |
| 846 | return; |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 847 | } else if ("-h".equals(arg)) { |
| 848 | dumpHelp(pw); |
| 849 | return; |
Mike Lockwood | e817404 | 2011-08-16 12:53:43 -0700 | [diff] [blame] | 850 | } else if ("-a".equals(arg)) { |
Dianne Hackborn | a1bd792 | 2014-03-21 11:07:11 -0700 | [diff] [blame] | 851 | flags |= BatteryStats.DUMP_VERBOSE; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 852 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 853 | pw.println("Unknown option: " + arg); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 854 | dumpHelp(pw); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 855 | return; |
| 856 | } else { |
| 857 | // Not an option, last argument must be a package name. |
| 858 | try { |
| 859 | reqUid = mContext.getPackageManager().getPackageUid(arg, |
| 860 | UserHandle.getCallingUserId()); |
| 861 | } catch (PackageManager.NameNotFoundException e) { |
| 862 | pw.println("Unknown package: " + arg); |
| 863 | dumpHelp(pw); |
| 864 | return; |
| 865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 868 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 869 | if (noOutput) { |
| 870 | return; |
| 871 | } |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 872 | if (BatteryStatsHelper.checkWifiOnly(mContext)) { |
| 873 | flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY; |
| 874 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 875 | if (useCheckinFormat) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 876 | List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 877 | if (isRealCheckin) { |
| 878 | // For a real checkin, first we want to prefer to use the last complete checkin |
| 879 | // file if there is one. |
| 880 | synchronized (mStats.mCheckinFile) { |
| 881 | if (mStats.mCheckinFile.exists()) { |
| 882 | try { |
| 883 | byte[] raw = mStats.mCheckinFile.readFully(); |
| 884 | if (raw != null) { |
| 885 | Parcel in = Parcel.obtain(); |
| 886 | in.unmarshall(raw, 0, raw.length); |
| 887 | in.setDataPosition(0); |
| 888 | BatteryStatsImpl checkinStats = new BatteryStatsImpl( |
| 889 | null, mStats.mHandler); |
| 890 | checkinStats.readSummaryFromParcel(in); |
| 891 | in.recycle(); |
| 892 | checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, |
| 893 | historyStart); |
| 894 | mStats.mCheckinFile.delete(); |
| 895 | return; |
| 896 | } |
| 897 | } catch (IOException e) { |
| 898 | Slog.w(TAG, "Failure reading checkin file " |
| 899 | + mStats.mCheckinFile.getBaseFile(), e); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 904 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 905 | mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 906 | if (writeData) { |
| 907 | mStats.writeAsyncLocked(); |
| 908 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 909 | } |
| 910 | } else { |
| 911 | synchronized (mStats) { |
Dianne Hackborn | c51cf03 | 2014-03-02 19:08:15 -0800 | [diff] [blame] | 912 | mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart); |
Dianne Hackborn | 16b0b56 | 2014-06-03 17:24:42 -0700 | [diff] [blame] | 913 | if (writeData) { |
| 914 | mStats.writeAsyncLocked(); |
| 915 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | } |