BatteryService.java revision d24b8183b93e781080b2c16c487e60d51c12da31
1/* 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; 20import com.android.server.am.BatteryStatsService; 21 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.SystemClock; 30import android.os.UEventObserver; 31import android.util.EventLog; 32import android.util.Log; 33 34import java.io.FileDescriptor; 35import java.io.PrintWriter; 36import java.lang.String; 37 38/** 39 * <p>BatteryService monitors the charging status, and charge level of the device 40 * battery. When these values change this service broadcasts the new values 41 * to all {@link android.content.BroadcastReceiver IntentReceivers} that are 42 * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED 43 * BATTERY_CHANGED} action.</p> 44 * <p>The new values are stored in the Intent data and can be retrieved by 45 * calling {@link android.content.Intent#getExtra Intent.getExtra} with the 46 * following keys:</p> 47 * <p>"scale" - int, the maximum value for the charge level</p> 48 * <p>"level" - int, charge level, from 0 through "scale" inclusive</p> 49 * <p>"status" - String, the current charging status.<br /> 50 * <p>"health" - String, the current battery health.<br /> 51 * <p>"present" - boolean, true if the battery is present<br /> 52 * <p>"icon-small" - int, suggested small icon to use for this state</p> 53 * <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged 54 * into an AC power adapter; 2 if plugged in via USB.</p> 55 * <p>"voltage" - int, current battery voltage in millivolts</p> 56 * <p>"temperature" - int, current battery temperature in tenths of 57 * a degree Centigrade</p> 58 * <p>"technology" - String, the type of battery installed, e.g. "Li-ion"</p> 59 */ 60class BatteryService extends Binder { 61 private static final String TAG = BatteryService.class.getSimpleName(); 62 63 static final int LOG_BATTERY_LEVEL = 2722; 64 static final int LOG_BATTERY_STATUS = 2723; 65 static final int LOG_BATTERY_DISCHARGE_STATUS = 2730; 66 67 static final int BATTERY_SCALE = 100; // battery capacity is a percentage 68 69 // This should probably be exposed in the API, though it's not critical 70 private static final int BATTERY_PLUGGED_NONE = 0; 71 72 private final Context mContext; 73 private final IBatteryStats mBatteryStats; 74 75 private boolean mAcOnline; 76 private boolean mUsbOnline; 77 private int mBatteryStatus; 78 private int mBatteryHealth; 79 private boolean mBatteryPresent; 80 private int mBatteryLevel; 81 private int mBatteryVoltage; 82 private int mBatteryTemperature; 83 private String mBatteryTechnology; 84 85 private int mLastBatteryStatus; 86 private int mLastBatteryHealth; 87 private boolean mLastBatteryPresent; 88 private int mLastBatteryLevel; 89 private int mLastBatteryVoltage; 90 private int mLastBatteryTemperature; 91 92 private int mPlugType; 93 private int mLastPlugType = -1; // Extra state so we can detect first run 94 95 private long mDischargeStartTime; 96 private int mDischargeStartLevel; 97 98 public BatteryService(Context context) { 99 mContext = context; 100 mBatteryStats = BatteryStatsService.getService(); 101 102 mUEventObserver.startObserving("SUBSYSTEM=power_supply"); 103 104 // set initial status 105 update(); 106 } 107 108 final boolean isPowered() { 109 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work. 110 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN); 111 } 112 113 final boolean isPowered(int plugTypeSet) { 114 // assume we are powered if battery state is unknown so 115 // the "stay on while plugged in" option will work. 116 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { 117 return true; 118 } 119 if (plugTypeSet == 0) { 120 return false; 121 } 122 int plugTypeBit = 0; 123 if (mAcOnline) { 124 plugTypeBit = BatteryManager.BATTERY_PLUGGED_AC; 125 } else if (mUsbOnline) { 126 plugTypeBit = BatteryManager.BATTERY_PLUGGED_USB; 127 } 128 return (plugTypeSet & plugTypeBit) != 0; 129 } 130 131 final int getPlugType() { 132 return mPlugType; 133 } 134 135 private UEventObserver mUEventObserver = new UEventObserver() { 136 @Override 137 public void onUEvent(UEventObserver.UEvent event) { 138 update(); 139 } 140 }; 141 142 // returns battery level as a percentage 143 final int getBatteryLevel() { 144 return mBatteryLevel; 145 } 146 147 private native void native_update(); 148 149 private synchronized final void update() { 150 native_update(); 151 if (mAcOnline) { 152 mPlugType = BatteryManager.BATTERY_PLUGGED_AC; 153 } else if (mUsbOnline) { 154 mPlugType = BatteryManager.BATTERY_PLUGGED_USB; 155 } else { 156 mPlugType = BATTERY_PLUGGED_NONE; 157 } 158 if (mBatteryStatus != mLastBatteryStatus || 159 mBatteryHealth != mLastBatteryHealth || 160 mBatteryPresent != mLastBatteryPresent || 161 mBatteryLevel != mLastBatteryLevel || 162 mPlugType != mLastPlugType || 163 mBatteryVoltage != mLastBatteryVoltage || 164 mBatteryTemperature != mLastBatteryTemperature) { 165 166 if (mPlugType != mLastPlugType) { 167 if (mLastPlugType == BATTERY_PLUGGED_NONE) { 168 // discharging -> charging 169 170 // There's no value in this data unless we've discharged at least once and the 171 // battery level has changed; so don't log until it does. 172 if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) { 173 long duration = SystemClock.elapsedRealtime() - mDischargeStartTime; 174 EventLog.writeEvent(LOG_BATTERY_DISCHARGE_STATUS, duration, 175 mDischargeStartLevel, mBatteryLevel); 176 // make sure we see a discharge event before logging again 177 mDischargeStartTime = 0; 178 } 179 } else if (mPlugType == BATTERY_PLUGGED_NONE) { 180 // charging -> discharging or we just powered up 181 mDischargeStartTime = SystemClock.elapsedRealtime(); 182 mDischargeStartLevel = mBatteryLevel; 183 } 184 } 185 if (mBatteryStatus != mLastBatteryStatus || 186 mBatteryHealth != mLastBatteryHealth || 187 mBatteryPresent != mLastBatteryPresent || 188 mPlugType != mLastPlugType) { 189 EventLog.writeEvent(LOG_BATTERY_STATUS, 190 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0, 191 mPlugType, mBatteryTechnology); 192 } 193 if (mBatteryLevel != mLastBatteryLevel || 194 mBatteryVoltage != mLastBatteryVoltage || 195 mBatteryTemperature != mLastBatteryTemperature) { 196 EventLog.writeEvent(LOG_BATTERY_LEVEL, 197 mBatteryLevel, mBatteryVoltage, mBatteryTemperature); 198 } 199 200 mLastBatteryStatus = mBatteryStatus; 201 mLastBatteryHealth = mBatteryHealth; 202 mLastBatteryPresent = mBatteryPresent; 203 mLastBatteryLevel = mBatteryLevel; 204 mLastPlugType = mPlugType; 205 mLastBatteryVoltage = mBatteryVoltage; 206 mLastBatteryTemperature = mBatteryTemperature; 207 208 sendIntent(); 209 } 210 } 211 212 private final void sendIntent() { 213 // Pack up the values and broadcast them to everyone 214 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 215 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 216 try { 217 mBatteryStats.setOnBattery(mPlugType == BATTERY_PLUGGED_NONE); 218 } catch (RemoteException e) { 219 // Should never happen. 220 } 221 222 int icon = getIcon(mBatteryLevel); 223 224 intent.putExtra("status", mBatteryStatus); 225 intent.putExtra("health", mBatteryHealth); 226 intent.putExtra("present", mBatteryPresent); 227 intent.putExtra("level", mBatteryLevel); 228 intent.putExtra("scale", BATTERY_SCALE); 229 intent.putExtra("icon-small", icon); 230 intent.putExtra("plugged", mPlugType); 231 intent.putExtra("voltage", mBatteryVoltage); 232 intent.putExtra("temperature", mBatteryTemperature); 233 intent.putExtra("technology", mBatteryTechnology); 234 235 if (false) { 236 Log.d(TAG, "updateBattery level:" + mBatteryLevel + 237 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus + 238 " health:" + mBatteryHealth + " present:" + mBatteryPresent + 239 " voltage: " + mBatteryVoltage + 240 " temperature: " + mBatteryTemperature + 241 " technology: " + mBatteryTechnology + 242 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline + 243 " icon:" + icon ); 244 } 245 246 ActivityManagerNative.broadcastStickyIntent(intent, null); 247 } 248 249 private final int getIcon(int level) { 250 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { 251 return com.android.internal.R.drawable.stat_sys_battery_charge; 252 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING || 253 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING || 254 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) { 255 return com.android.internal.R.drawable.stat_sys_battery; 256 } else { 257 return com.android.internal.R.drawable.stat_sys_battery_unknown; 258 } 259 } 260 261 @Override 262 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 263 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 264 != PackageManager.PERMISSION_GRANTED) { 265 266 pw.println("Permission Denial: can't dump Battery service from from pid=" 267 + Binder.getCallingPid() 268 + ", uid=" + Binder.getCallingUid()); 269 return; 270 } 271 272 synchronized (this) { 273 pw.println("Current Battery Service state:"); 274 pw.println(" AC powered: " + mAcOnline); 275 pw.println(" USB powered: " + mUsbOnline); 276 pw.println(" status: " + mBatteryStatus); 277 pw.println(" health: " + mBatteryHealth); 278 pw.println(" present: " + mBatteryPresent); 279 pw.println(" level: " + mBatteryLevel); 280 pw.println(" scale: " + BATTERY_SCALE); 281 pw.println(" voltage:" + mBatteryVoltage); 282 pw.println(" temperature: " + mBatteryTemperature); 283 pw.println(" technology: " + mBatteryTechnology); 284 } 285 } 286} 287