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; |
| 30 | import android.os.Process; |
| 31 | import android.os.ServiceManager; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 33 | import android.os.WorkSource; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 34 | import android.telephony.SignalStrength; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 35 | import android.telephony.TelephonyManager; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 36 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 38 | import com.android.internal.app.IBatteryStats; |
| 39 | import com.android.internal.os.BatteryStatsImpl; |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 40 | import com.android.internal.os.PowerProfile; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import java.io.FileDescriptor; |
| 43 | import java.io.PrintWriter; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 44 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * All information we are collecting about things that can happen that impact |
| 48 | * battery life. |
| 49 | */ |
| 50 | public final class BatteryStatsService extends IBatteryStats.Stub { |
| 51 | static IBatteryStats sService; |
| 52 | |
| 53 | final BatteryStatsImpl mStats; |
| 54 | Context mContext; |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 55 | private boolean mBluetoothPendingStats; |
| 56 | private BluetoothHeadset mBluetoothHeadset; |
Amith Yamasani | 3f7e35c | 2009-07-13 16:02:45 -0700 | [diff] [blame] | 57 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 58 | BatteryStatsService(String filename, Handler handler) { |
| 59 | mStats = new BatteryStatsImpl(filename, handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public void publish(Context context) { |
| 63 | mContext = context; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 64 | ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder()); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 65 | mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps()); |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 66 | mStats.setRadioScanningTimeout(mContext.getResources().getInteger( |
| 67 | com.android.internal.R.integer.config_radioScanningTimeout) |
| 68 | * 1000L); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 71 | public void shutdown() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 72 | Slog.w("BatteryStats", "Writing battery stats before shutdown..."); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 73 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 74 | mStats.shutdownLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | public static IBatteryStats getService() { |
| 79 | if (sService != null) { |
| 80 | return sService; |
| 81 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 82 | IBinder b = ServiceManager.getService(BatteryStats.SERVICE_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | sService = asInterface(b); |
| 84 | return sService; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return the current statistics object, which may be modified |
| 89 | * to reflect events that affect battery usage. You must lock the |
| 90 | * stats object before doing anything with it. |
| 91 | */ |
| 92 | public BatteryStatsImpl getActiveStatistics() { |
| 93 | return mStats; |
| 94 | } |
| 95 | |
| 96 | public byte[] getStatistics() { |
| 97 | mContext.enforceCallingPermission( |
| 98 | android.Manifest.permission.BATTERY_STATS, null); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 99 | //Slog.i("foo", "SENDING BATTERY INFO:"); |
| 100 | //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] | 101 | Parcel out = Parcel.obtain(); |
| 102 | mStats.writeToParcel(out, 0); |
| 103 | byte[] data = out.marshall(); |
| 104 | out.recycle(); |
| 105 | return data; |
| 106 | } |
| 107 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 108 | public void noteStartWakelock(int uid, int pid, String name, int type) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | enforceCallingPermission(); |
| 110 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 111 | mStats.noteStartWakeLocked(uid, pid, name, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 115 | public void noteStopWakelock(int uid, int pid, String name, int type) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | enforceCallingPermission(); |
| 117 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 118 | mStats.noteStopWakeLocked(uid, pid, name, type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 122 | public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, int type) { |
| 123 | enforceCallingPermission(); |
| 124 | synchronized (mStats) { |
| 125 | mStats.noteStartWakeFromSourceLocked(ws, pid, name, type); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, int type) { |
| 130 | enforceCallingPermission(); |
| 131 | synchronized (mStats) { |
| 132 | mStats.noteStopWakeFromSourceLocked(ws, pid, name, type); |
| 133 | } |
| 134 | } |
| 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | public void noteStartSensor(int uid, int sensor) { |
| 137 | enforceCallingPermission(); |
| 138 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 139 | mStats.noteStartSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | public void noteStopSensor(int uid, int sensor) { |
| 144 | enforceCallingPermission(); |
| 145 | synchronized (mStats) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 146 | mStats.noteStopSensorLocked(uid, sensor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 150 | public void noteVibratorOn(int uid, long durationMillis) { |
| 151 | enforceCallingPermission(); |
| 152 | synchronized (mStats) { |
| 153 | mStats.noteVibratorOnLocked(uid, durationMillis); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public void noteVibratorOff(int uid) { |
| 158 | enforceCallingPermission(); |
| 159 | synchronized (mStats) { |
| 160 | mStats.noteVibratorOffLocked(uid); |
| 161 | } |
| 162 | } |
| 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | public void noteStartGps(int uid) { |
| 165 | enforceCallingPermission(); |
| 166 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 167 | mStats.noteStartGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | public void noteStopGps(int uid) { |
| 172 | enforceCallingPermission(); |
| 173 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 174 | mStats.noteStopGpsLocked(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | public void noteScreenOn() { |
| 179 | enforceCallingPermission(); |
| 180 | synchronized (mStats) { |
| 181 | mStats.noteScreenOnLocked(); |
| 182 | } |
| 183 | } |
| 184 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 185 | public void noteScreenBrightness(int brightness) { |
| 186 | enforceCallingPermission(); |
| 187 | synchronized (mStats) { |
| 188 | mStats.noteScreenBrightnessLocked(brightness); |
| 189 | } |
| 190 | } |
| 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | public void noteScreenOff() { |
| 193 | enforceCallingPermission(); |
| 194 | synchronized (mStats) { |
| 195 | mStats.noteScreenOffLocked(); |
| 196 | } |
| 197 | } |
| 198 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 199 | public void noteInputEvent() { |
| 200 | enforceCallingPermission(); |
Christopher Tate | 4cee725 | 2010-03-19 14:50:40 -0700 | [diff] [blame] | 201 | mStats.noteInputEventAtomic(); |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public void noteUserActivity(int uid, int event) { |
| 205 | enforceCallingPermission(); |
| 206 | synchronized (mStats) { |
| 207 | mStats.noteUserActivityLocked(uid, event); |
| 208 | } |
| 209 | } |
| 210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | public void notePhoneOn() { |
| 212 | enforceCallingPermission(); |
| 213 | synchronized (mStats) { |
| 214 | mStats.notePhoneOnLocked(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public void notePhoneOff() { |
| 219 | enforceCallingPermission(); |
| 220 | synchronized (mStats) { |
| 221 | mStats.notePhoneOffLocked(); |
| 222 | } |
| 223 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 224 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 225 | public void notePhoneSignalStrength(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 226 | enforceCallingPermission(); |
| 227 | synchronized (mStats) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 228 | mStats.notePhoneSignalStrengthLocked(signalStrength); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | public void notePhoneDataConnectionState(int dataType, boolean hasData) { |
| 233 | enforceCallingPermission(); |
| 234 | synchronized (mStats) { |
| 235 | mStats.notePhoneDataConnectionStateLocked(dataType, hasData); |
| 236 | } |
| 237 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 238 | |
Amith Yamasani | f37447b | 2009-10-08 18:28:01 -0700 | [diff] [blame] | 239 | public void notePhoneState(int state) { |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 240 | enforceCallingPermission(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 241 | int simState = TelephonyManager.getDefault().getSimState(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 242 | synchronized (mStats) { |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 243 | mStats.notePhoneStateLocked(state, simState); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 247 | public void noteWifiOn() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 248 | enforceCallingPermission(); |
| 249 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 250 | mStats.noteWifiOnLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 254 | public void noteWifiOff() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 255 | enforceCallingPermission(); |
| 256 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 257 | mStats.noteWifiOffLocked(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 260 | |
Amith Yamasani | 244fa5c | 2009-05-22 14:36:07 -0700 | [diff] [blame] | 261 | public void noteStartAudio(int uid) { |
| 262 | enforceCallingPermission(); |
| 263 | synchronized (mStats) { |
| 264 | mStats.noteAudioOnLocked(uid); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | public void noteStopAudio(int uid) { |
| 269 | enforceCallingPermission(); |
| 270 | synchronized (mStats) { |
| 271 | mStats.noteAudioOffLocked(uid); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | public void noteStartVideo(int uid) { |
| 276 | enforceCallingPermission(); |
| 277 | synchronized (mStats) { |
| 278 | mStats.noteVideoOnLocked(uid); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public void noteStopVideo(int uid) { |
| 283 | enforceCallingPermission(); |
| 284 | synchronized (mStats) { |
| 285 | mStats.noteVideoOffLocked(uid); |
| 286 | } |
| 287 | } |
| 288 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 289 | public void noteWifiRunning(WorkSource ws) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 290 | enforceCallingPermission(); |
| 291 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 292 | mStats.noteWifiRunningLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 296 | public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 297 | enforceCallingPermission(); |
| 298 | synchronized (mStats) { |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 299 | mStats.noteWifiRunningChangedLocked(oldWs, newWs); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | public void noteWifiStopped(WorkSource ws) { |
| 304 | enforceCallingPermission(); |
| 305 | synchronized (mStats) { |
| 306 | mStats.noteWifiStoppedLocked(ws); |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 310 | public void noteBluetoothOn() { |
| 311 | enforceCallingPermission(); |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 312 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 313 | if (adapter != null) { |
| 314 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 315 | BluetoothProfile.HEADSET); |
| 316 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 317 | synchronized (mStats) { |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 318 | if (mBluetoothHeadset != null) { |
| 319 | mStats.noteBluetoothOnLocked(); |
| 320 | mStats.setBtHeadset(mBluetoothHeadset); |
| 321 | } else { |
| 322 | mBluetoothPendingStats = true; |
| 323 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 326 | |
| 327 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 328 | new BluetoothProfile.ServiceListener() { |
| 329 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 330 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 331 | synchronized (mStats) { |
| 332 | if (mBluetoothPendingStats) { |
| 333 | mStats.noteBluetoothOnLocked(); |
| 334 | mStats.setBtHeadset(mBluetoothHeadset); |
| 335 | mBluetoothPendingStats = false; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | public void onServiceDisconnected(int profile) { |
| 341 | mBluetoothHeadset = null; |
| 342 | } |
| 343 | }; |
| 344 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 345 | public void noteBluetoothOff() { |
| 346 | enforceCallingPermission(); |
| 347 | synchronized (mStats) { |
Jaikumar Ganesh | 3f03496 | 2010-09-27 17:02:23 -0700 | [diff] [blame] | 348 | mBluetoothPendingStats = false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 349 | mStats.noteBluetoothOffLocked(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | public void noteFullWifiLockAcquired(int uid) { |
| 354 | enforceCallingPermission(); |
| 355 | synchronized (mStats) { |
| 356 | mStats.noteFullWifiLockAcquiredLocked(uid); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | public void noteFullWifiLockReleased(int uid) { |
| 361 | enforceCallingPermission(); |
| 362 | synchronized (mStats) { |
| 363 | mStats.noteFullWifiLockReleasedLocked(uid); |
| 364 | } |
| 365 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 366 | |
| 367 | public void noteWifiScanStarted(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 368 | enforceCallingPermission(); |
| 369 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 370 | mStats.noteWifiScanStartedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 373 | |
| 374 | public void noteWifiScanStopped(int uid) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 375 | enforceCallingPermission(); |
| 376 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 377 | mStats.noteWifiScanStoppedLocked(uid); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 381 | public void noteWifiMulticastEnabled(int uid) { |
| 382 | enforceCallingPermission(); |
| 383 | synchronized (mStats) { |
| 384 | mStats.noteWifiMulticastEnabledLocked(uid); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | public void noteWifiMulticastDisabled(int uid) { |
| 389 | enforceCallingPermission(); |
| 390 | synchronized (mStats) { |
| 391 | mStats.noteWifiMulticastDisabledLocked(uid); |
| 392 | } |
| 393 | } |
| 394 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 395 | public void noteFullWifiLockAcquiredFromSource(WorkSource ws) { |
| 396 | enforceCallingPermission(); |
| 397 | synchronized (mStats) { |
| 398 | mStats.noteFullWifiLockAcquiredFromSourceLocked(ws); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | public void noteFullWifiLockReleasedFromSource(WorkSource ws) { |
| 403 | enforceCallingPermission(); |
| 404 | synchronized (mStats) { |
| 405 | mStats.noteFullWifiLockReleasedFromSourceLocked(ws); |
| 406 | } |
| 407 | } |
| 408 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 409 | public void noteWifiScanStartedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 410 | enforceCallingPermission(); |
| 411 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 412 | mStats.noteWifiScanStartedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 416 | public void noteWifiScanStoppedFromSource(WorkSource ws) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 417 | enforceCallingPermission(); |
| 418 | synchronized (mStats) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 419 | mStats.noteWifiScanStoppedFromSourceLocked(ws); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Robert Greenwalt | a029ea1 | 2013-09-25 16:38:12 -0700 | [diff] [blame] | 423 | public void noteWifiBatchedScanStartedFromSource(WorkSource ws, int csph) { |
| 424 | enforceCallingPermission(); |
| 425 | synchronized (mStats) { |
| 426 | mStats.noteWifiBatchedScanStartedFromSourceLocked(ws, csph); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | public void noteWifiBatchedScanStoppedFromSource(WorkSource ws) { |
| 431 | enforceCallingPermission(); |
| 432 | synchronized (mStats) { |
| 433 | mStats.noteWifiBatchedScanStoppedFromSourceLocked(ws); |
| 434 | } |
| 435 | } |
| 436 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 437 | public void noteWifiMulticastEnabledFromSource(WorkSource ws) { |
| 438 | enforceCallingPermission(); |
| 439 | synchronized (mStats) { |
| 440 | mStats.noteWifiMulticastEnabledFromSourceLocked(ws); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | public void noteWifiMulticastDisabledFromSource(WorkSource ws) { |
| 445 | enforceCallingPermission(); |
| 446 | synchronized (mStats) { |
| 447 | mStats.noteWifiMulticastDisabledFromSourceLocked(ws); |
| 448 | } |
| 449 | } |
| 450 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 451 | @Override |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 452 | public void noteNetworkInterfaceType(String iface, int type) { |
| 453 | enforceCallingPermission(); |
| 454 | synchronized (mStats) { |
| 455 | mStats.noteNetworkInterfaceTypeLocked(iface, type); |
| 456 | } |
| 457 | } |
| 458 | |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 459 | @Override |
| 460 | public void noteNetworkStatsEnabled() { |
| 461 | enforceCallingPermission(); |
| 462 | synchronized (mStats) { |
| 463 | mStats.noteNetworkStatsEnabledLocked(); |
| 464 | } |
| 465 | } |
| 466 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | public boolean isOnBattery() { |
| 468 | return mStats.isOnBattery(); |
| 469 | } |
| 470 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 471 | public void setBatteryState(int status, int health, int plugType, int level, |
| 472 | int temp, int volt) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | enforceCallingPermission(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 474 | mStats.setBatteryState(status, health, plugType, level, temp, volt); |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 475 | } |
| 476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | public long getAwakeTimeBattery() { |
| 478 | mContext.enforceCallingOrSelfPermission( |
| 479 | android.Manifest.permission.BATTERY_STATS, null); |
| 480 | return mStats.getAwakeTimeBattery(); |
| 481 | } |
| 482 | |
| 483 | public long getAwakeTimePlugged() { |
| 484 | mContext.enforceCallingOrSelfPermission( |
| 485 | android.Manifest.permission.BATTERY_STATS, null); |
| 486 | return mStats.getAwakeTimePlugged(); |
| 487 | } |
| 488 | |
| 489 | public void enforceCallingPermission() { |
| 490 | if (Binder.getCallingPid() == Process.myPid()) { |
| 491 | return; |
| 492 | } |
| 493 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 494 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 495 | } |
| 496 | |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 497 | private void dumpHelp(PrintWriter pw) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 498 | pw.println("Battery stats (batterystats) dump options:"); |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 499 | pw.println(" [--checkin] [-c] [--unplugged] [--reset] [--write] [-h] [<package.name>]"); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 500 | pw.println(" --checkin: format output for a checkin report."); |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 501 | pw.println(" --unplugged: only output data since last unplugged."); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 502 | pw.println(" --reset: reset the stats, clearing all current data."); |
| 503 | pw.println(" --write: force write current collected stats to disk."); |
| 504 | pw.println(" -h: print this help text."); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 505 | pw.println(" <package.name>: optional name of package to filter output by."); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 506 | } |
| 507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | @Override |
| 509 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 510 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 511 | != PackageManager.PERMISSION_GRANTED) { |
| 512 | pw.println("Permission Denial: can't dump BatteryStats from from pid=" |
| 513 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 514 | + " without permission " + android.Manifest.permission.DUMP); |
| 515 | return; |
| 516 | } |
| 517 | |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 518 | boolean isCheckin = false; |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 519 | boolean includeHistory = false; |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 520 | boolean isUnpluggedOnly = false; |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 521 | boolean noOutput = false; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 522 | int reqUid = -1; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 523 | if (args != null) { |
| 524 | for (String arg : args) { |
| 525 | if ("--checkin".equals(arg)) { |
| 526 | isCheckin = true; |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 527 | } else if ("-c".equals(arg)) { |
| 528 | isCheckin = true; |
| 529 | includeHistory = true; |
Jeff Sharkey | ec43a6b | 2013-04-30 13:33:18 -0700 | [diff] [blame] | 530 | } else if ("--unplugged".equals(arg)) { |
| 531 | isUnpluggedOnly = true; |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 532 | } else if ("--reset".equals(arg)) { |
| 533 | synchronized (mStats) { |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 534 | mStats.resetAllStatsLocked(); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 535 | pw.println("Battery stats reset."); |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 536 | noOutput = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 538 | } else if ("--write".equals(arg)) { |
| 539 | synchronized (mStats) { |
| 540 | mStats.writeSyncLocked(); |
| 541 | pw.println("Battery stats written."); |
| 542 | noOutput = true; |
| 543 | } |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 544 | } else if ("-h".equals(arg)) { |
| 545 | dumpHelp(pw); |
| 546 | return; |
Mike Lockwood | e817404 | 2011-08-16 12:53:43 -0700 | [diff] [blame] | 547 | } else if ("-a".equals(arg)) { |
| 548 | // fall through |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 549 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 550 | pw.println("Unknown option: " + arg); |
Dianne Hackborn | ae38445 | 2011-06-28 12:33:48 -0700 | [diff] [blame] | 551 | dumpHelp(pw); |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 552 | return; |
| 553 | } else { |
| 554 | // Not an option, last argument must be a package name. |
| 555 | try { |
| 556 | reqUid = mContext.getPackageManager().getPackageUid(arg, |
| 557 | UserHandle.getCallingUserId()); |
| 558 | } catch (PackageManager.NameNotFoundException e) { |
| 559 | pw.println("Unknown package: " + arg); |
| 560 | dumpHelp(pw); |
| 561 | return; |
| 562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 565 | } |
Dianne Hackborn | 0ffc988 | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 566 | if (noOutput) { |
| 567 | return; |
| 568 | } |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 569 | if (isCheckin) { |
| 570 | List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0); |
| 571 | synchronized (mStats) { |
Dianne Hackborn | 49021f5 | 2013-09-04 18:03:40 -0700 | [diff] [blame] | 572 | mStats.dumpCheckinLocked(pw, apps, isUnpluggedOnly, includeHistory); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 573 | } |
| 574 | } else { |
| 575 | synchronized (mStats) { |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 576 | mStats.dumpLocked(pw, isUnpluggedOnly, reqUid); |
Dianne Hackborn | e4a5951 | 2010-12-07 11:08:07 -0800 | [diff] [blame] | 577 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | } |