1、healthed服务监听接收内核kernel的电池事件,然后上传数据给framware层的batterysevice,BatteryService计算电池的电量,显示,绘制动画等
android电池系统框架:
2、healthd服务入口:android/system/core/healthd/healthd.cpp 中main函数
healthd_mode_ops是一个关于充电状态的结构体变量,main里面做healthed初始化,并做一个主循环持续
main函数中,首先根据传入的参数不同区分:开机充电、recovery、关机充电。这三种情况,然后指定不同的healthd_mode_ops回调函数
int main(int argc, char **argv) {
。。。。。。。。。。。。。。。//healthd_mode_ops是一个关于充电状态的结构体变量,healthd_mode_ops = &android_ops;//开机充电时,指向android_ops
。。。。。。。。。。。。。。。。。。。ret = healthd_init(); //healthed初始化
。。。。。。。。。。。。。。。。。。。healthd_mainloop(); //主循环
}
3、healthed_init函数:
static int healthd_init() {epollfd = epoll_create(MAX_EPOLL_EVENTS);//创建一个epoll变量if (epollfd == -1) {KLOG_ERROR(LOG_TAG,"epoll_create failed; errno=%d\n",errno);return -1;}//和板子级别的初始化,里面其实是一个空函数,什么也没做healthd_board_init(&healthd_config);//根据系统所处的模式,有三种情况的init,开机充电,关机充电,recoveryhealthd_mode_ops->init(&healthd_config);//wakealarm定时器初始化wakealarm_init();//uevent事件初始化,用以监听电池的uevent事件。uevent_init();//BatteryMonitor初始化。gBatteryMonitor = new BatteryMonitor();//创建batteryMonitor对象gBatteryMonitor->init(&healthd_config);//初始化batteryMonitor,打开/sys/class/power_supply,//遍历该节点下的电池参数初始化healthd的config参数return 0;
}
4、wakeupalarm_init函数:
static void wakealarm_init(void) {//创建一个wakealarm对应的定时器描述符wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);if (wakealarm_fd == -1) {KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");return;}//将wakealarm事件注册到wakealarm_fd文件节点上以监听wakealarm事件。if (healthd_register_event(wakealarm_fd, wakealarm_event))KLOG_ERROR(LOG_TAG,"Registration of wakealarm event failed\n");//设置alarm唤醒间隔wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
}
5、BatteryService电池监听广播服务:
frameworks/base/services/core/java/com/android/server/BatteryService.java
//将电池监听注册到底层
public void onStart() {IBinder b = ServiceManager.getService("batteryproperties");final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =IBatteryPropertiesRegistrar.Stub.asInterface(b);try {//注册电池监听,当底层电池电量发生变化调用此监听,并调用update。batteryPropertiesRegistrar.registerListener(new BatteryListener());} catch (RemoteException e) {// Should never happen.}publishBinderService("battery", new BinderService());publishLocalService(BatteryManagerInternal.class, new LocalService());
}
//当底层有信息时,会调用update更新BatteryService中相关值。
private void update(BatteryProperties props) {synchronized (mLock) {if (!mUpdatesStopped) {mBatteryProps = props;// Process the new values.processValuesLocked(false);} else {mLastBatteryProps.set(props);}}}
processValuesLocked函数更新上报的电池信息:
电池电量、充电状态、低温关机、低电关机、电池状态、适配器插拔等
在这里:
//发送电池状态变换广播
sendIntentLocked();