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