Android4.4电池管理
healthd.cpp
static void uevent_event(void){
...
uevent_kernel_multicast_recv(uevent_fd, msg,UEVENT_MSG_LEN); 接收kernel上报的Uevent
...
battery_update();
...
}
static void battery_update(void) {
// Fast wakeinterval when on charger (watch for overheat);
// slow wakeinterval when on battery (watch for drained battery).
intnew_wake_interval = gBatteryMonitor->update() ?//调用BatteryMonitor.cpp的update()
healthd_config.periodic_chores_interval_fast :
healthd_config.periodic_chores_interval_slow;
if(new_wake_interval != wakealarm_wake_interval)
wakealarm_set_interval(new_wake_interval);
// During awakeperiods poll at fast rate. If wake alarmis set at fast
// rate thenjust use the alarm; if wake alarm is set at slow rate then
// poll at fastrate while awake and let alarm wake up at slow rate when
// asleep.
if(healthd_config.periodic_chores_interval_fast == -1)
awake_poll_interval = -1;
else
awake_poll_interval =
new_wake_interval ==healthd_config.periodic_chores_interval_fast ?
-1: healthd_config.periodic_chores_interval_fast * 1000;
}
接下来就是BatteryMonitor.cpp的update()
mBatteryPropertiesRegistrar->notifyListeners(props);//这里是BatteryService注册的监听器
BatteryService.java
private final class BatteryListener extendsIBatteryPropertiesListener.Stub {
public voidbatteryPropertiesChanged(BatteryProperties props) {
BatteryService.this.update(props);//这是重点,呵呵
}
}
private void update(BatteryProperties props) {
synchronized (mLock) {
if(!mUpdatesStopped) {
mBatteryProps = props;
//Process the new values.
processValuesLocked();//这里
}
}
}
private void processValuesLocked() {
...
sendIntentLocked();//zheli
...
}
private void sendIntentLocked() {
// Pack up the values and broadcast them toeveryone
finalIntent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
|Intent.FLAG_RECEIVER_REPLACE_PENDING);
int icon =getIconLocked(mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_STATUS,mBatteryProps.batteryStatus);
intent.putExtra(BatteryManager.EXTRA_HEALTH,mBatteryProps.batteryHealth);
intent.putExtra(BatteryManager.EXTRA_PRESENT,mBatteryProps.batteryPresent);
intent.putExtra(BatteryManager.EXTRA_LEVEL,mBatteryProps.batteryLevel);
intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE);
intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon);
intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
intent.putExtra(BatteryManager.EXTRA_VOLTAGE,mBatteryProps.batteryVoltage);
intent.putExtra(BatteryManager.EXTRA_TEMPERATURE,mBatteryProps.batteryTemperature);
intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
if (DEBUG){
Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryProps.batteryLevel +
", scale:" + BATTERY_SCALE + ", status:" +mBatteryProps.batteryStatus +
", health:" + mBatteryProps.batteryHealth + ", present:" +mBatteryProps.batteryPresent +
", voltage: " + mBatteryProps.batteryVoltage +
", temperature: " +mBatteryProps.batteryTemperature +
", technology: " + mBatteryProps.batteryTechnology +
", AC powered:" + mBatteryProps.chargerAcOnline + ", USBpowered:" + mBatteryProps.chargerUsbOnline +
", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
", icon:" + icon +", invalid charger:" + mInvalidCharger);
}
mHandler.post(new Runnable() {
@Override
publicvoid run() {
ActivityManagerNative.broadcastStickyIntent(intent, null,UserHandle.USER_ALL);//这里是发出intent给所有上层使用
}
});
}
Android4.3电池管理
[电池简易流程图片在附件中]
java代码:
frameworks/frameworks/base/services/java/com/android/server/Ba
tteryService.java
JNI代码:
frameworks/base/services/jni/com_android_server_BatteryService.
cpp
kernel driver代码
drivers/power/xx_battery.c
2、BatteryService
BatteryService作为电池及充电相关的服务: 监听Uevent、读取sysfs
里中的状态 、广播Intent.ACTION_BATTERY_CHANGED。
(1)、mUEventObserver
BatteryService实现了一个UevenObserver mUEventObserver。
uevent是Linux内核用来向用户空间主动上报事件的机制,对于JAVA程
序来说,只实现UEventObserver的虚函数onUEvent,然后注册即可。
- private UEventObserver mUEventObserver = new UEventObserver(){
- @Override
- public void onUEvent(UEventObserver.UEvent event){
- update();
- }
- }
private UEventObserver mUEventObserver = newUEventObserver(){
@Override
public void onUEvent(UEventObserver.UEventevent){
update();
}
}
BatteryService只关注power_supply的事件,所以在构造函数注册:
- public BatteryService(Context context){
- mContext = context;
- mBatteryStats = BatteryStatsService.getService();
- mUEventObserver.startObserving("SUBSYSTEM=power_supply");
- //set initial status
- update();
- }
public BatteryService(Context context){
mContext = context;
mBatteryStats =BatteryStatsService.getService();
mUEventObserver.startObserving("SUBSYSTEM=power_supply");
//set initial status
update();
}
(2)、update()
update读取sysfs文件做到同步取得电池信息, 然后根据读到的状态更新
BatteryService的成员变量,并广播一个Intent来通知其它关注电源状态的
组件。
当kernel 有power_supply 事件上报时,mUEventObserver 调用
update()函数,然后update 调用native_update从sysfs 中读取相关状态
(com_android_server_BatteryService.cpp):
- private synchronized final void update(){
- native_update();
- }
private synchronized final voidupdate(){
native_update();
}
(3)、sysfs
Linux驱动driver维护着保存电池信息的一组文件sysfs,供应用程序获
取电源相关状态:
#define AC_ONLINE_PATH "/sys/class/power_supply/ac/online" AC 电源连接
状态
#define USB_ONLINE_PATH "/sys/class/power_supply/usb/online" USB 电源
连接状态
#define BATTERY_STATUS_PATH
"/sys/class/power_supply/battery/status"充电状态
#define BATTERY_HEALTH_PATH
"/sys/class/power_supply/battery/health"电池状态
#define BATTERY_PRESENT_PATH
"/sys/class/power_supply/battery/present"使用状态
#define BATTERY_CAPACITY_PATH
"/sys/class/power_supply/battery/capacity"电池level
#define BATTERY_VOLTAGE_PATH
"/sys/class/power_supply/battery/batt_vol"电池电压
#define BATTERY_TEMPERATURE_PATH
"/sys/class/power_supply/battery/batt_temp"电池温度
#define BATTERY_TECHNOLOGY_PATH
"/sys/class/power_supply/battery/technology"电池技术
当电池状态发生变化时,driver会更新这些文件。