BatteryService.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.BatteryStats; 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.UEventObserver; 30import android.util.Config; 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 66 static final int BATTERY_SCALE = 100; // battery capacity is a percentage 67 68 private final Context mContext; 69 private final IBatteryStats mBatteryStats; 70 71 private boolean mAcOnline; 72 private boolean mUsbOnline; 73 private int mBatteryStatus; 74 private int mBatteryHealth; 75 private boolean mBatteryPresent; 76 private int mBatteryLevel; 77 private int mBatteryVoltage; 78 private int mBatteryTemperature; 79 private String mBatteryTechnology; 80 81 private int mLastBatteryStatus; 82 private int mLastBatteryHealth; 83 private boolean mLastBatteryPresent; 84 private int mLastBatteryLevel; 85 private int mLastBatteryVoltage; 86 private int mLastBatteryTemperature; 87 88 private int mPlugType; 89 private int mLastPlugType; 90 91 public BatteryService(Context context) { 92 mContext = context; 93 mBatteryStats = BatteryStats.getService(); 94 95 mUEventObserver.startObserving("DEVPATH=/class/power_supply"); 96 97 // set initial status 98 update(); 99 } 100 101 final boolean isPowered() { 102 // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work. 103 return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN); 104 } 105 106 private UEventObserver mUEventObserver = new UEventObserver() { 107 @Override 108 public void onUEvent(UEventObserver.UEvent event) { 109 update(); 110 } 111 }; 112 113 // returns battery level as a percentage 114 final int getBatteryLevel() { 115 return mBatteryLevel; 116 } 117 118 private native void native_update(); 119 120 private synchronized final void update() { 121 native_update(); 122 if (mAcOnline) { 123 mPlugType = BatteryManager.BATTERY_PLUGGED_AC; 124 } else if (mUsbOnline) { 125 mPlugType = BatteryManager.BATTERY_PLUGGED_USB; 126 } else { 127 mPlugType = 0; 128 } 129 if (mBatteryStatus != mLastBatteryStatus || 130 mBatteryHealth != mLastBatteryHealth || 131 mBatteryPresent != mLastBatteryPresent || 132 mBatteryLevel != mLastBatteryLevel || 133 mPlugType != mLastPlugType || 134 mBatteryVoltage != mLastBatteryVoltage || 135 mBatteryTemperature != mLastBatteryTemperature) { 136 137 if (mBatteryStatus != mLastBatteryStatus || 138 mBatteryHealth != mLastBatteryHealth || 139 mBatteryPresent != mLastBatteryPresent || 140 mPlugType != mLastPlugType) { 141 EventLog.writeEvent(LOG_BATTERY_STATUS, 142 mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0, 143 mPlugType, mBatteryTechnology); 144 } 145 if (mBatteryLevel != mLastBatteryLevel || 146 mBatteryVoltage != mLastBatteryVoltage || 147 mBatteryTemperature != mLastBatteryTemperature) { 148 EventLog.writeEvent(LOG_BATTERY_LEVEL, 149 mBatteryLevel, mBatteryVoltage, mBatteryTemperature); 150 } 151 152 mLastBatteryStatus = mBatteryStatus; 153 mLastBatteryHealth = mBatteryHealth; 154 mLastBatteryPresent = mBatteryPresent; 155 mLastBatteryLevel = mBatteryLevel; 156 mLastPlugType = mPlugType; 157 mLastBatteryVoltage = mBatteryVoltage; 158 mLastBatteryTemperature = mBatteryTemperature; 159 160 sendIntent(); 161 } 162 } 163 164 private final void sendIntent() { 165 // Pack up the values and broadcast them to everyone 166 Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); 167 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 168 try { 169 mBatteryStats.setOnBattery(mPlugType == 0); 170 } catch (RemoteException e) { 171 // Should never happen. 172 } 173 174 int icon = getIcon(mBatteryLevel); 175 176 intent.putExtra("status", mBatteryStatus); 177 intent.putExtra("health", mBatteryHealth); 178 intent.putExtra("present", mBatteryPresent); 179 intent.putExtra("level", mBatteryLevel); 180 intent.putExtra("scale", BATTERY_SCALE); 181 intent.putExtra("icon-small", icon); 182 intent.putExtra("plugged", mPlugType); 183 intent.putExtra("voltage", mBatteryVoltage); 184 intent.putExtra("temperature", mBatteryTemperature); 185 intent.putExtra("technology", mBatteryTechnology); 186 187 if (false) { 188 Log.d(TAG, "updateBattery level:" + mBatteryLevel + 189 " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus + 190 " health:" + mBatteryHealth + " present:" + mBatteryPresent + 191 " voltage: " + mBatteryVoltage + 192 " temperature: " + mBatteryTemperature + 193 " technology: " + mBatteryTechnology + 194 " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline + 195 " icon:" + icon ); 196 } 197 198 ActivityManagerNative.broadcastStickyIntent(intent, null); 199 } 200 201 private final int getIcon(int level) { 202 if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { 203 return com.android.internal.R.drawable.stat_sys_battery_charge; 204 } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING || 205 mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING || 206 mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) { 207 return com.android.internal.R.drawable.stat_sys_battery; 208 } else { 209 return com.android.internal.R.drawable.stat_sys_battery_unknown; 210 } 211 } 212 213 @Override 214 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 215 if (mContext.checkCallingPermission("android.permission.DUMP") 216 != PackageManager.PERMISSION_GRANTED) { 217 218 pw.println("Permission Denial: can't dump Battery service from from pid=" 219 + Binder.getCallingPid() 220 + ", uid=" + Binder.getCallingUid()); 221 return; 222 } 223 224 synchronized (this) { 225 pw.println("Current Battery Service state:"); 226 pw.println(" AC powered: " + mAcOnline); 227 pw.println(" USB powered: " + mUsbOnline); 228 pw.println(" status: " + mBatteryStatus); 229 pw.println(" health: " + mBatteryHealth); 230 pw.println(" present: " + mBatteryPresent); 231 pw.println(" level: " + mBatteryLevel); 232 pw.println(" scale: " + BATTERY_SCALE); 233 pw.println(" voltage:" + mBatteryVoltage); 234 pw.println(" temperature: " + mBatteryTemperature); 235 pw.println(" technology: " + mBatteryTechnology); 236 } 237 } 238} 239