blob: dba8fcac78b248fb4c82864907cf4d5fafaf5e08 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2006 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;
18
19import com.android.internal.app.IBatteryStats;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080020import com.android.server.am.BatteryStatsService;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070021
22import android.app.ActivityManagerNative;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.os.BatteryManager;
27import android.os.Binder;
28import android.os.RemoteException;
29import android.os.UEventObserver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070030import android.util.EventLog;
31import android.util.Log;
32
33import java.io.FileDescriptor;
34import java.io.PrintWriter;
35import java.lang.String;
36
37/**
38 * <p>BatteryService monitors the charging status, and charge level of the device
39 * battery. When these values change this service broadcasts the new values
40 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are
41 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED
42 * BATTERY_CHANGED} action.</p>
43 * <p>The new values are stored in the Intent data and can be retrieved by
44 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the
45 * following keys:</p>
46 * <p>&quot;scale&quot; - int, the maximum value for the charge level</p>
47 * <p>&quot;level&quot; - int, charge level, from 0 through &quot;scale&quot; inclusive</p>
48 * <p>&quot;status&quot; - String, the current charging status.<br />
49 * <p>&quot;health&quot; - String, the current battery health.<br />
50 * <p>&quot;present&quot; - boolean, true if the battery is present<br />
51 * <p>&quot;icon-small&quot; - int, suggested small icon to use for this state</p>
52 * <p>&quot;plugged&quot; - int, 0 if the device is not plugged in; 1 if plugged
53 * into an AC power adapter; 2 if plugged in via USB.</p>
54 * <p>&quot;voltage&quot; - int, current battery voltage in millivolts</p>
55 * <p>&quot;temperature&quot; - int, current battery temperature in tenths of
56 * a degree Centigrade</p>
57 * <p>&quot;technology&quot; - String, the type of battery installed, e.g. "Li-ion"</p>
58 */
59class BatteryService extends Binder {
60 private static final String TAG = BatteryService.class.getSimpleName();
61
62 static final int LOG_BATTERY_LEVEL = 2722;
63 static final int LOG_BATTERY_STATUS = 2723;
64
65 static final int BATTERY_SCALE = 100; // battery capacity is a percentage
66
67 private final Context mContext;
68 private final IBatteryStats mBatteryStats;
69
70 private boolean mAcOnline;
71 private boolean mUsbOnline;
72 private int mBatteryStatus;
73 private int mBatteryHealth;
74 private boolean mBatteryPresent;
75 private int mBatteryLevel;
76 private int mBatteryVoltage;
77 private int mBatteryTemperature;
78 private String mBatteryTechnology;
79
80 private int mLastBatteryStatus;
81 private int mLastBatteryHealth;
82 private boolean mLastBatteryPresent;
83 private int mLastBatteryLevel;
84 private int mLastBatteryVoltage;
85 private int mLastBatteryTemperature;
86
87 private int mPlugType;
88 private int mLastPlugType;
89
90 public BatteryService(Context context) {
91 mContext = context;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080092 mBatteryStats = BatteryStatsService.getService();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070093
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080094 mUEventObserver.startObserving("SUBSYSTEM=power_supply");
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070095
96 // set initial status
97 update();
98 }
99
100 final boolean isPowered() {
101 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
102 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
103 }
104
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800105 final boolean isPowered(int plugTypeSet) {
106 // assume we are powered if battery state is unknown so
107 // the "stay on while plugged in" option will work.
108 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) {
109 return true;
110 }
111 if (plugTypeSet == 0) {
112 return false;
113 }
114 int plugTypeBit = 0;
115 if (mAcOnline) {
116 plugTypeBit = BatteryManager.BATTERY_PLUGGED_AC;
117 } else if (mUsbOnline) {
118 plugTypeBit = BatteryManager.BATTERY_PLUGGED_USB;
119 }
120 return (plugTypeSet & plugTypeBit) != 0;
121 }
122
123 final int getPlugType() {
124 return mPlugType;
125 }
126
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700127 private UEventObserver mUEventObserver = new UEventObserver() {
128 @Override
129 public void onUEvent(UEventObserver.UEvent event) {
130 update();
131 }
132 };
133
134 // returns battery level as a percentage
135 final int getBatteryLevel() {
136 return mBatteryLevel;
137 }
138
139 private native void native_update();
140
141 private synchronized final void update() {
142 native_update();
143 if (mAcOnline) {
144 mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
145 } else if (mUsbOnline) {
146 mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
147 } else {
148 mPlugType = 0;
149 }
150 if (mBatteryStatus != mLastBatteryStatus ||
151 mBatteryHealth != mLastBatteryHealth ||
152 mBatteryPresent != mLastBatteryPresent ||
153 mBatteryLevel != mLastBatteryLevel ||
154 mPlugType != mLastPlugType ||
155 mBatteryVoltage != mLastBatteryVoltage ||
156 mBatteryTemperature != mLastBatteryTemperature) {
157
158 if (mBatteryStatus != mLastBatteryStatus ||
159 mBatteryHealth != mLastBatteryHealth ||
160 mBatteryPresent != mLastBatteryPresent ||
161 mPlugType != mLastPlugType) {
162 EventLog.writeEvent(LOG_BATTERY_STATUS,
163 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0,
164 mPlugType, mBatteryTechnology);
165 }
166 if (mBatteryLevel != mLastBatteryLevel ||
167 mBatteryVoltage != mLastBatteryVoltage ||
168 mBatteryTemperature != mLastBatteryTemperature) {
169 EventLog.writeEvent(LOG_BATTERY_LEVEL,
170 mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
171 }
172
173 mLastBatteryStatus = mBatteryStatus;
174 mLastBatteryHealth = mBatteryHealth;
175 mLastBatteryPresent = mBatteryPresent;
176 mLastBatteryLevel = mBatteryLevel;
177 mLastPlugType = mPlugType;
178 mLastBatteryVoltage = mBatteryVoltage;
179 mLastBatteryTemperature = mBatteryTemperature;
180
181 sendIntent();
182 }
183 }
184
185 private final void sendIntent() {
186 // Pack up the values and broadcast them to everyone
187 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
188 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
189 try {
190 mBatteryStats.setOnBattery(mPlugType == 0);
191 } catch (RemoteException e) {
192 // Should never happen.
193 }
194
195 int icon = getIcon(mBatteryLevel);
196
197 intent.putExtra("status", mBatteryStatus);
198 intent.putExtra("health", mBatteryHealth);
199 intent.putExtra("present", mBatteryPresent);
200 intent.putExtra("level", mBatteryLevel);
201 intent.putExtra("scale", BATTERY_SCALE);
202 intent.putExtra("icon-small", icon);
203 intent.putExtra("plugged", mPlugType);
204 intent.putExtra("voltage", mBatteryVoltage);
205 intent.putExtra("temperature", mBatteryTemperature);
206 intent.putExtra("technology", mBatteryTechnology);
207
208 if (false) {
209 Log.d(TAG, "updateBattery level:" + mBatteryLevel +
210 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus +
211 " health:" + mBatteryHealth + " present:" + mBatteryPresent +
212 " voltage: " + mBatteryVoltage +
213 " temperature: " + mBatteryTemperature +
214 " technology: " + mBatteryTechnology +
215 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline +
216 " icon:" + icon );
217 }
218
219 ActivityManagerNative.broadcastStickyIntent(intent, null);
220 }
221
222 private final int getIcon(int level) {
223 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
224 return com.android.internal.R.drawable.stat_sys_battery_charge;
225 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING ||
226 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING ||
227 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
228 return com.android.internal.R.drawable.stat_sys_battery;
229 } else {
230 return com.android.internal.R.drawable.stat_sys_battery_unknown;
231 }
232 }
233
234 @Override
235 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
236 if (mContext.checkCallingPermission("android.permission.DUMP")
237 != PackageManager.PERMISSION_GRANTED) {
238
239 pw.println("Permission Denial: can't dump Battery service from from pid="
240 + Binder.getCallingPid()
241 + ", uid=" + Binder.getCallingUid());
242 return;
243 }
244
245 synchronized (this) {
246 pw.println("Current Battery Service state:");
247 pw.println(" AC powered: " + mAcOnline);
248 pw.println(" USB powered: " + mUsbOnline);
249 pw.println(" status: " + mBatteryStatus);
250 pw.println(" health: " + mBatteryHealth);
251 pw.println(" present: " + mBatteryPresent);
252 pw.println(" level: " + mBatteryLevel);
253 pw.println(" scale: " + BATTERY_SCALE);
254 pw.println(" voltage:" + mBatteryVoltage);
255 pw.println(" temperature: " + mBatteryTemperature);
256 pw.println(" technology: " + mBatteryTechnology);
257 }
258 }
259}