blob: 963a691bb727aa963a67da52d4e98397eb1c8385 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server.am;
18
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070019import android.bluetooth.BluetoothAdapter;
Amith Yamasani14fb81a2009-11-05 11:41:58 -080020import android.bluetooth.BluetoothHeadset;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070021import android.bluetooth.BluetoothProfile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080023import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.os.Binder;
25import android.os.IBinder;
26import android.os.Parcel;
27import android.os.Process;
28import android.os.ServiceManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070029import android.os.WorkSource;
Wink Savillee9b06d72009-05-18 21:47:50 -070030import android.telephony.SignalStrength;
Dianne Hackborne4a59512010-12-07 11:08:07 -080031import android.telephony.TelephonyManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080032import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Amith Yamasani32dbefd2009-06-19 09:21:17 -070034import com.android.internal.app.IBatteryStats;
35import com.android.internal.os.BatteryStatsImpl;
Amith Yamasanie43530a2009-08-21 13:11:37 -070036import com.android.internal.os.PowerProfile;
Amith Yamasani32dbefd2009-06-19 09:21:17 -070037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import java.io.FileDescriptor;
39import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080040import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42/**
43 * All information we are collecting about things that can happen that impact
44 * battery life.
45 */
46public final class BatteryStatsService extends IBatteryStats.Stub {
47 static IBatteryStats sService;
48
49 final BatteryStatsImpl mStats;
50 Context mContext;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070051 private boolean mBluetoothPendingStats;
52 private BluetoothHeadset mBluetoothHeadset;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 BatteryStatsService(String filename) {
55 mStats = new BatteryStatsImpl(filename);
56 }
57
58 public void publish(Context context) {
59 mContext = context;
60 ServiceManager.addService("batteryinfo", asBinder());
Amith Yamasanie43530a2009-08-21 13:11:37 -070061 mStats.setNumSpeedSteps(new PowerProfile(mContext).getNumSpeedSteps());
Amith Yamasanif37447b2009-10-08 18:28:01 -070062 mStats.setRadioScanningTimeout(mContext.getResources().getInteger(
63 com.android.internal.R.integer.config_radioScanningTimeout)
64 * 1000L);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
66
Dianne Hackborn55280a92009-05-07 15:53:46 -070067 public void shutdown() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080068 Slog.w("BatteryStats", "Writing battery stats before shutdown...");
Dianne Hackborn55280a92009-05-07 15:53:46 -070069 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070070 mStats.shutdownLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -070071 }
72 }
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 public static IBatteryStats getService() {
75 if (sService != null) {
76 return sService;
77 }
78 IBinder b = ServiceManager.getService("batteryinfo");
79 sService = asInterface(b);
80 return sService;
81 }
82
83 /**
84 * @return the current statistics object, which may be modified
85 * to reflect events that affect battery usage. You must lock the
86 * stats object before doing anything with it.
87 */
88 public BatteryStatsImpl getActiveStatistics() {
89 return mStats;
90 }
91
92 public byte[] getStatistics() {
93 mContext.enforceCallingPermission(
94 android.Manifest.permission.BATTERY_STATS, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080095 //Slog.i("foo", "SENDING BATTERY INFO:");
96 //mStats.dumpLocked(new LogPrinter(Log.INFO, "foo", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 Parcel out = Parcel.obtain();
98 mStats.writeToParcel(out, 0);
99 byte[] data = out.marshall();
100 out.recycle();
101 return data;
102 }
103
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700104 public void noteStartWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 enforceCallingPermission();
106 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700107 mStats.noteStartWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 }
109 }
110
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700111 public void noteStopWakelock(int uid, int pid, String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 enforceCallingPermission();
113 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700114 mStats.noteStopWakeLocked(uid, pid, name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116 }
117
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700118 public void noteStartWakelockFromSource(WorkSource ws, int pid, String name, int type) {
119 enforceCallingPermission();
120 synchronized (mStats) {
121 mStats.noteStartWakeFromSourceLocked(ws, pid, name, type);
122 }
123 }
124
125 public void noteStopWakelockFromSource(WorkSource ws, int pid, String name, int type) {
126 enforceCallingPermission();
127 synchronized (mStats) {
128 mStats.noteStopWakeFromSourceLocked(ws, pid, name, type);
129 }
130 }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public void noteStartSensor(int uid, int sensor) {
133 enforceCallingPermission();
134 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700135 mStats.noteStartSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
137 }
138
139 public void noteStopSensor(int uid, int sensor) {
140 enforceCallingPermission();
141 synchronized (mStats) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700142 mStats.noteStopSensorLocked(uid, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 }
144 }
145
146 public void noteStartGps(int uid) {
147 enforceCallingPermission();
148 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700149 mStats.noteStartGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
151 }
152
153 public void noteStopGps(int uid) {
154 enforceCallingPermission();
155 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700156 mStats.noteStopGpsLocked(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158 }
159
160 public void noteScreenOn() {
161 enforceCallingPermission();
162 synchronized (mStats) {
163 mStats.noteScreenOnLocked();
164 }
165 }
166
Dianne Hackborn617f8772009-03-31 15:04:46 -0700167 public void noteScreenBrightness(int brightness) {
168 enforceCallingPermission();
169 synchronized (mStats) {
170 mStats.noteScreenBrightnessLocked(brightness);
171 }
172 }
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public void noteScreenOff() {
175 enforceCallingPermission();
176 synchronized (mStats) {
177 mStats.noteScreenOffLocked();
178 }
179 }
180
Dianne Hackborn617f8772009-03-31 15:04:46 -0700181 public void noteInputEvent() {
182 enforceCallingPermission();
Christopher Tate4cee7252010-03-19 14:50:40 -0700183 mStats.noteInputEventAtomic();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700184 }
185
186 public void noteUserActivity(int uid, int event) {
187 enforceCallingPermission();
188 synchronized (mStats) {
189 mStats.noteUserActivityLocked(uid, event);
190 }
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 public void notePhoneOn() {
194 enforceCallingPermission();
195 synchronized (mStats) {
196 mStats.notePhoneOnLocked();
197 }
198 }
199
200 public void notePhoneOff() {
201 enforceCallingPermission();
202 synchronized (mStats) {
203 mStats.notePhoneOffLocked();
204 }
205 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700206
Wink Savillee9b06d72009-05-18 21:47:50 -0700207 public void notePhoneSignalStrength(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700208 enforceCallingPermission();
209 synchronized (mStats) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700210 mStats.notePhoneSignalStrengthLocked(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700211 }
212 }
213
214 public void notePhoneDataConnectionState(int dataType, boolean hasData) {
215 enforceCallingPermission();
216 synchronized (mStats) {
217 mStats.notePhoneDataConnectionStateLocked(dataType, hasData);
218 }
219 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700220
Amith Yamasanif37447b2009-10-08 18:28:01 -0700221 public void notePhoneState(int state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700222 enforceCallingPermission();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800223 int simState = TelephonyManager.getDefault().getSimState();
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700224 synchronized (mStats) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800225 mStats.notePhoneStateLocked(state, simState);
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700226 }
227 }
228
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700229 public void noteWifiOn() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700230 enforceCallingPermission();
231 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700232 mStats.noteWifiOnLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700233 }
234 }
235
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700236 public void noteWifiOff() {
The Android Open Source Project10592532009-03-18 17:39:46 -0700237 enforceCallingPermission();
238 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700239 mStats.noteWifiOffLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700240 }
241 }
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700242
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700243 public void noteStartAudio(int uid) {
244 enforceCallingPermission();
245 synchronized (mStats) {
246 mStats.noteAudioOnLocked(uid);
247 }
248 }
249
250 public void noteStopAudio(int uid) {
251 enforceCallingPermission();
252 synchronized (mStats) {
253 mStats.noteAudioOffLocked(uid);
254 }
255 }
256
257 public void noteStartVideo(int uid) {
258 enforceCallingPermission();
259 synchronized (mStats) {
260 mStats.noteVideoOnLocked(uid);
261 }
262 }
263
264 public void noteStopVideo(int uid) {
265 enforceCallingPermission();
266 synchronized (mStats) {
267 mStats.noteVideoOffLocked(uid);
268 }
269 }
270
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700271 public void noteWifiRunning(WorkSource ws) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700272 enforceCallingPermission();
273 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700274 mStats.noteWifiRunningLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700275 }
276 }
277
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700278 public void noteWifiRunningChanged(WorkSource oldWs, WorkSource newWs) {
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700279 enforceCallingPermission();
280 synchronized (mStats) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700281 mStats.noteWifiRunningChangedLocked(oldWs, newWs);
282 }
283 }
284
285 public void noteWifiStopped(WorkSource ws) {
286 enforceCallingPermission();
287 synchronized (mStats) {
288 mStats.noteWifiStoppedLocked(ws);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700289 }
290 }
291
The Android Open Source Project10592532009-03-18 17:39:46 -0700292 public void noteBluetoothOn() {
293 enforceCallingPermission();
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700294 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
295 if (adapter != null) {
296 adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
297 BluetoothProfile.HEADSET);
298 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700299 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700300 if (mBluetoothHeadset != null) {
301 mStats.noteBluetoothOnLocked();
302 mStats.setBtHeadset(mBluetoothHeadset);
303 } else {
304 mBluetoothPendingStats = true;
305 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700306 }
307 }
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700308
309 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
310 new BluetoothProfile.ServiceListener() {
311 public void onServiceConnected(int profile, BluetoothProfile proxy) {
312 mBluetoothHeadset = (BluetoothHeadset) proxy;
313 synchronized (mStats) {
314 if (mBluetoothPendingStats) {
315 mStats.noteBluetoothOnLocked();
316 mStats.setBtHeadset(mBluetoothHeadset);
317 mBluetoothPendingStats = false;
318 }
319 }
320 }
321
322 public void onServiceDisconnected(int profile) {
323 mBluetoothHeadset = null;
324 }
325 };
326
The Android Open Source Project10592532009-03-18 17:39:46 -0700327 public void noteBluetoothOff() {
328 enforceCallingPermission();
329 synchronized (mStats) {
Jaikumar Ganesh3f034962010-09-27 17:02:23 -0700330 mBluetoothPendingStats = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700331 mStats.noteBluetoothOffLocked();
332 }
333 }
334
335 public void noteFullWifiLockAcquired(int uid) {
336 enforceCallingPermission();
337 synchronized (mStats) {
338 mStats.noteFullWifiLockAcquiredLocked(uid);
339 }
340 }
341
342 public void noteFullWifiLockReleased(int uid) {
343 enforceCallingPermission();
344 synchronized (mStats) {
345 mStats.noteFullWifiLockReleasedLocked(uid);
346 }
347 }
348
349 public void noteScanWifiLockAcquired(int uid) {
350 enforceCallingPermission();
351 synchronized (mStats) {
352 mStats.noteScanWifiLockAcquiredLocked(uid);
353 }
354 }
355
356 public void noteScanWifiLockReleased(int uid) {
357 enforceCallingPermission();
358 synchronized (mStats) {
359 mStats.noteScanWifiLockReleasedLocked(uid);
360 }
361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700363 public void noteWifiMulticastEnabled(int uid) {
364 enforceCallingPermission();
365 synchronized (mStats) {
366 mStats.noteWifiMulticastEnabledLocked(uid);
367 }
368 }
369
370 public void noteWifiMulticastDisabled(int uid) {
371 enforceCallingPermission();
372 synchronized (mStats) {
373 mStats.noteWifiMulticastDisabledLocked(uid);
374 }
375 }
376
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700377 public void noteFullWifiLockAcquiredFromSource(WorkSource ws) {
378 enforceCallingPermission();
379 synchronized (mStats) {
380 mStats.noteFullWifiLockAcquiredFromSourceLocked(ws);
381 }
382 }
383
384 public void noteFullWifiLockReleasedFromSource(WorkSource ws) {
385 enforceCallingPermission();
386 synchronized (mStats) {
387 mStats.noteFullWifiLockReleasedFromSourceLocked(ws);
388 }
389 }
390
391 public void noteScanWifiLockAcquiredFromSource(WorkSource ws) {
392 enforceCallingPermission();
393 synchronized (mStats) {
394 mStats.noteScanWifiLockAcquiredFromSourceLocked(ws);
395 }
396 }
397
398 public void noteScanWifiLockReleasedFromSource(WorkSource ws) {
399 enforceCallingPermission();
400 synchronized (mStats) {
401 mStats.noteScanWifiLockReleasedFromSourceLocked(ws);
402 }
403 }
404
405 public void noteWifiMulticastEnabledFromSource(WorkSource ws) {
406 enforceCallingPermission();
407 synchronized (mStats) {
408 mStats.noteWifiMulticastEnabledFromSourceLocked(ws);
409 }
410 }
411
412 public void noteWifiMulticastDisabledFromSource(WorkSource ws) {
413 enforceCallingPermission();
414 synchronized (mStats) {
415 mStats.noteWifiMulticastDisabledFromSourceLocked(ws);
416 }
417 }
418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 public boolean isOnBattery() {
420 return mStats.isOnBattery();
421 }
422
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700423 public void setBatteryState(int status, int health, int plugType, int level,
424 int temp, int volt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 enforceCallingPermission();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700426 mStats.setBatteryState(status, health, plugType, level, temp, volt);
Evan Millar633a1742009-04-02 16:36:33 -0700427 }
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public long getAwakeTimeBattery() {
430 mContext.enforceCallingOrSelfPermission(
431 android.Manifest.permission.BATTERY_STATS, null);
432 return mStats.getAwakeTimeBattery();
433 }
434
435 public long getAwakeTimePlugged() {
436 mContext.enforceCallingOrSelfPermission(
437 android.Manifest.permission.BATTERY_STATS, null);
438 return mStats.getAwakeTimePlugged();
439 }
440
441 public void enforceCallingPermission() {
442 if (Binder.getCallingPid() == Process.myPid()) {
443 return;
444 }
445 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
446 Binder.getCallingPid(), Binder.getCallingUid(), null);
447 }
448
449 @Override
450 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackborne4a59512010-12-07 11:08:07 -0800451 boolean isCheckin = false;
452 if (args != null) {
453 for (String arg : args) {
454 if ("--checkin".equals(arg)) {
455 isCheckin = true;
456 } else if ("--reset".equals(arg)) {
457 synchronized (mStats) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700458 mStats.resetAllStatsLocked();
Dianne Hackborne4a59512010-12-07 11:08:07 -0800459 pw.println("Battery stats reset.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462 }
Dianne Hackborne4a59512010-12-07 11:08:07 -0800463 }
464 if (isCheckin) {
465 List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
466 synchronized (mStats) {
467 mStats.dumpCheckinLocked(pw, args, apps);
468 }
469 } else {
470 synchronized (mStats) {
471 mStats.dumpLocked(pw);
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474 }
475}