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