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