码迷,mamicode.com
首页 > 其他好文 > 详细

No shutdown animation in the electricity display only 1%

时间:2017-06-08 23:47:26      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:services   分支   data-   boolean   gty   变量   错误处理   sage   toc   

低电量自动关机时无关机动画

1. 问题描述

  • DEFECT DESCRIPTION:
    No shutdown animation in the electricity display only 1%.
  • REPRODUCING PROCEDURES:
    电量消耗显示只有1%时,手机突然黑屏关机,没有关机动画,长按power键后手机又可以正常开机使用.(黑屏关机后插上充电器,电量显示为1%)
  • EXPECTED BEHAVIOUR:
    There should a shutdown animation in the electricity display only 1%.

2. 分析

关机动画的逻辑主要是在上层进行控制的,该上层的入口是文件BatteryService.java。系统会通过Binder机制将底层检测的到电池电量传入到属性mBatteryLevel中,然后对属性mBatteryLevel进行if操作。

  • 首先,底层由c/c++来检测电池信息并周期性更新电池状态。
  • 系统通过bingder机制将底层检测到的电池状态传入上层(BatteryService.java)
  • 上层接收底层传来的电池信息,并根据不同的电池状态做出相应的操作,如当电池电量为0时,会播放关机动画。

1.电池信息
更新电池电量的服务位于health守护进程中,其在init.rc中的定义如下:

  1. service healthd /sbin/healthd
  2. class core
  3. critical
  4. seclabel u:r:healthd:s0
  5. group root system wakelock

而该服务的相关代码位于目录system/core/health下,文件health.cpp的main函数如下:

  1. int main(int argc,char**argv){
  2. int ch;
  3. int ret;
  4. klog_set_level(KLOG_LEVEL);
  5. healthd_mode_ops =&android_ops;
  6. /*通过main函数的参数来判断当前手机状态*/
  7. if(!strcmp(basename(argv[0]),"charger")){
  8. /*从字面意思看 应该是在充电状态*/
  9. healthd_mode_ops =&charger_ops;
  10. }else{
  11. while((ch = getopt(argc, argv,"cr"))!=-1){
  12. switch(ch){
  13. case‘c‘:
  14. healthd_mode_ops =&charger_ops;
  15. break;
  16. case‘r‘:
  17. healthd_mode_ops =&recovery_ops;
  18. break;
  19. case‘?‘:
  20. default:
  21. KLOG_ERROR(LOG_TAG,"Unrecognized healthd option: %c\n",
  22. optopt);
  23. exit(1);
  24. }
  25. }
  26. }
  27. ret = healthd_init();
  28. /*错误处理*/
  29. if(ret){
  30. KLOG_ERROR("Initialization failed, exiting\n");
  31. exit(2);
  32. }
  33. /*进入循环,也就是说手机从开启该服务开始,之后一直停留在循环中*/
  34. healthd_mainloop();
  35. KLOG_ERROR("Main loop terminated, exiting\n");
  36. return3;
  37. }

然后看看函数healthd_init()做了些什么事,其代码如下:

  1. staticint healthd_init(){
  2. /*没看懂是什么意思,好像是调用的bionic基础库的方法*/
  3. epollfd = epoll_create(MAX_EPOLL_EVENTS);
  4. if(epollfd ==-1){
  5. KLOG_ERROR(LOG_TAG,
  6. "epoll_create failed; errno=%d\n",
  7. errno);
  8. return-1;
  9. }
  10. /*看名字该是用电池配置信息进行初始化操作*/
  11. healthd_board_init(&healthd_config);
  12. /*应该是设置电池模式之类的操作*/
  13. healthd_mode_ops->init(&healthd_config);
  14. /*创建一个定时器*/
  15. wakealarm_init();
  16. /*初始化uevent环境*/
  17. uevent_init();
  18. /*创建BatteryMonitor对象*/
  19. gBatteryMonitor =newBatteryMonitor();
  20. /*对创建的BatteryMonitor对象进行初始化*/
  21. gBatteryMonitor->init(&healthd_config);
  22. return0;
  23. }

然后看看这个定时器怎么创建的,具体代码如下:

  1. staticvoid wakealarm_init(void){
  2. wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
  3. if(wakealarm_fd ==-1){
  4. KLOG_ERROR(LOG_TAG,"wakealarm_init: timerfd_create failed\n");
  5. return;
  6. }
  7. if(healthd_register_event(wakealarm_fd, wakealarm_event))
  8. KLOG_ERROR(LOG_TAG,
  9. "Registration of wakealarm event failed\n"); wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
  10. }

用方法timerfd_create()创建一个定时器句柄,然后把它存入到全局变量wakealarm_fd中,函数wakealarm_set_interval()设置超时时间。
然后会创建一个BatteryMonitor对象并调用该对象的init方法,BatteryMonitor文件位于system/core/healthd/BatteryMonitor.cpp,该文件主要是读取电池的各种参数并返回给上层,先来看看该对象的init方法,具体如下:

  1. voidBatteryMonitor::init(struct healthd_config *hc){
  2. String8 path;
  3. char pval[PROPERTY_VALUE_MAX];
  4. mHealthdConfig = hc;
  5. /*
  6. #define POWER_SUPPLY_SUBSYSTEM "power_supply"
  7. #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
  8. 根据宏定义 POWER_SUPPLY_SYSFS_PATH="/sys/class/power_supply"即打开该目录
  9. ,而这个目录下的文件就是存储了电池的各中信息,如我们需要的电池电量值就在/sys/class/power_supply/battery/capacity文件中
  10. ,通过命令cat /sys/class/power_supply/battery/capacity就可以查看到当前电池电量值
  11. */
  12. DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
  13. if(dir == NULL){
  14. KLOG_ERROR(LOG_TAG,"Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
  15. }else{
  16. struct dirent* entry;
  17. while((entry = readdir(dir))){
  18. constchar* name = entry->d_name;
  19. /*过滤掉./目录和../目录*/
  20. if(!strcmp(name,".")||!strcmp(name,".."))
  21. continue;
  22. // Look for "type" file in each subdirectory
  23. path.clear();
  24. path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
  25. switch(readPowerSupplyType(path)){
  26. case ANDROID_POWER_SUPPLY_TYPE_AC:
  27. case ANDROID_POWER_SUPPLY_TYPE_USB:
  28. case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
  29. path.clear();
  30. path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
  31. if(access(path.string(), R_OK)==0)
  32. mChargerNames.add(String8(name));
  33. break;
  34. case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
  35. mBatteryDevicePresent =true;
  36. if(mHealthdConfig->batteryStatusPath.isEmpty()){
  37. path.clear();
  38. path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
  39. name);
  40. if(access(path, R_OK)==0)
  41. mHealthdConfig->batteryStatusPath = path;
  42. }
  43. if(mHealthdConfig->batteryHealthPath.isEmpty()){
  44. path.clear();
  45. path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
  46. name);
  47. if(access(path, R_OK)==0)
  48. mHealthdConfig->batteryHealthPath = path;
  49. }
  50. if(mHealthdConfig->batteryPresentPath.isEmpty()){
  51. path.clear();
  52. path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
  53. name);
  54. if(access(path, R_OK)==0)
  55. mHealthdConfig->batteryPresentPath = path;
  56. }
  57. /*此处就是读取电池电量值,并把读到的值保存到mHealthdConfig成员变量中*/
  58. if(mHealthdConfig->batteryCapacityPath.isEmpty()){
  59. path.clear();
  60. path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
  61. name);
  62. if(access(path, R_OK)==0)
  63. mHealthdConfig->batteryCapacityPath = path;
  64. }
  65. if(mHealthdConfig->batteryVoltagePath.isEmpty()){
  66. path.clear();
  67. path.appendFormat("%s/%s/voltage_now",
  68. POWER_SUPPLY_SYSFS_PATH, name);
  69. if(access(path, R_OK)==0){
  70. mHealthdConfig->batteryVoltagePath = path;
  71. }else{
  72. path.clear();
  73. path.appendFormat("%s/%s/batt_vol",
  74. POWER_SUPPLY_SYSFS_PATH, name);
  75. if(access(path, R_OK)==0)
  76. mHealthdConfig->batteryVoltagePath = path;
  77. }
  78. }
  79. if(mHealthdConfig->batteryFullChargePath.isEmpty()){
  80. path.clear();
  81. path.appendFormat("%s/%s/charge_full",
  82. POWER_SUPPLY_SYSFS_PATH, name);
  83. if(access(path, R_OK)==0)
  84. mHealthdConfig->batteryFullChargePath = path;
  85. }
  86. if(mHealthdConfig->batteryCurrentNowPath.isEmpty()){
  87. path.clear();
  88. path.appendFormat("%s/%s/current_now",
  89. POWER_SUPPLY_SYSFS_PATH, name);
  90. if(access(path, R_OK)==0)
  91. mHealthdConfig->batteryCurrentNowPath = path;
  92. }
  93. if(mHealthdConfig->batteryCycleCountPath.isEmpty()){
  94. path.clear();
  95. path.appendFormat("%s/%s/cycle_count",
  96. POWER_SUPPLY_SYSFS_PATH, name);
  97. if(access(path, R_OK)==0)
  98. mHealthdConfig->batteryCycleCountPath = path;
  99. }
  100. if(mHealthdConfig->batteryCurrentAvgPath.isEmpty()){
  101. path.clear();
  102. path.appendFormat("%s/%s/current_avg",
  103. POWER_SUPPLY_SYSFS_PATH, name);
  104. if(access(path, R_OK)==0)
  105. mHealthdConfig->batteryCurrentAvgPath = path;
  106. }
  107. if(mHealthdConfig->batteryChargeCounterPath.isEmpty()){
  108. path.clear();
  109. path.appendFormat("%s/%s/charge_counter",
  110. POWER_SUPPLY_SYSFS_PATH, name);
  111. if(access(path, R_OK)==0)
  112. mHealthdConfig->batteryChargeCounterPath = path;
  113. }
  114. if(mHealthdConfig->batteryTemperaturePath.isEmpty()){
  115. path.clear();
  116. path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
  117. name);
  118. if(access(path, R_OK)==0){
  119. mHealthdConfig->batteryTemperaturePath = path;
  120. }else{
  121. path.clear();
  122. path.appendFormat("%s/%s/batt_temp",
  123. POWER_SUPPLY_SYSFS_PATH, name);
  124. if(access(path, R_OK)==0)
  125. mHealthdConfig->batteryTemperaturePath = path;
  126. }
  127. }
  128. if(mHealthdConfig->batteryTechnologyPath.isEmpty()){
  129. path.clear();
  130. path.appendFormat("%s/%s/technology",
  131. POWER_SUPPLY_SYSFS_PATH, name);
  132. if(access(path, R_OK)==0)
  133. mHealthdConfig->batteryTechnologyPath = path;
  134. }
  135. if(mHealthdConfig->batteryStatusPath_smb.isEmpty()){
  136. path.clear();
  137. path.appendFormat("%s/%s/status_smb", POWER_SUPPLY_SYSFS_PATH,
  138. name);
  139. if(access(path, R_OK)==0)
  140. mHealthdConfig->batteryStatusPath_smb = path;
  141. }
  142. if(mHealthdConfig->batteryPresentPath_smb.isEmpty()){
  143. path.clear();
  144. path.appendFormat("%s/%s/present_smb", POWER_SUPPLY_SYSFS_PATH,
  145. name);
  146. if(access(path, R_OK)==0)
  147. mHealthdConfig->batteryPresentPath_smb = path;
  148. }
  149. if(mHealthdConfig->batteryCapacityPath_smb.isEmpty()){
  150. path.clear();
  151. path.appendFormat("%s/%s/capacity_smb", POWER_SUPPLY_SYSFS_PATH,
  152. name);
  153. if(access(path, R_OK)==0)
  154. mHealthdConfig->batteryCapacityPath_smb = path;
  155. }
  156. if(mHealthdConfig->batteryAdjustPowerPath.isEmpty()){
  157. path.clear();
  158. path.appendFormat("%s/%s/adjust_power",
  159. POWER_SUPPLY_SYSFS_PATH, name);
  160. if(access(path, R_OK)==0)
  161. mHealthdConfig->batteryAdjustPowerPath = path;
  162. }
  163. break;
  164. case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
  165. break;
  166. }
  167. }
  168. closedir(dir);
  169. }
  170. // Typically the case for devices which do not have a battery and
  171. // and are always plugged into AC mains.
  172. if(!mBatteryDevicePresent){
  173. KLOG_WARNING(LOG_TAG,"No battery devices found\n");
  174. hc->periodic_chores_interval_fast =-1;
  175. hc->periodic_chores_interval_slow =-1;
  176. mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
  177. mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
  178. mAlwaysPluggedDevice =true;
  179. }else{
  180. if(mHealthdConfig->batteryStatusPath.isEmpty())
  181. KLOG_WARNING(LOG_TAG,"BatteryStatusPath not found\n");
  182. if(mHealthdConfig->batteryHealthPath.isEmpty())
  183. KLOG_WARNING(LOG_TAG,"BatteryHealthPath not found\n");
  184. if(mHealthdConfig->batteryPresentPath.isEmpty())
  185. KLOG_WARNING(LOG_TAG,"BatteryPresentPath not found\n");
  186. if(mHealthdConfig->batteryCapacityPath.isEmpty())
  187. KLOG_WARNING(LOG_TAG,"BatteryCapacityPath not found\n");
  188. if(mHealthdConfig->batteryVoltagePath.isEmpty())
  189. KLOG_WARNING(LOG_TAG,"BatteryVoltagePath not found\n");
  190. if(mHealthdConfig->batteryTemperaturePath.isEmpty())
  191. KLOG_WARNING(LOG_TAG,"BatteryTemperaturePath not found\n");
  192. if(mHealthdConfig->batteryTechnologyPath.isEmpty())
  193. KLOG_WARNING(LOG_TAG,"BatteryTechnologyPath not found\n");
  194. if(mHealthdConfig->batteryCurrentNowPath.isEmpty())
  195. KLOG_WARNING(LOG_TAG,"BatteryCurrentNowPath not found\n");
  196. if(mHealthdConfig->batteryFullChargePath.isEmpty())
  197. KLOG_WARNING(LOG_TAG,"BatteryFullChargePath not found\n");
  198. if(mHealthdConfig->batteryCycleCountPath.isEmpty())
  199. KLOG_WARNING(LOG_TAG,"BatteryCycleCountPath not found\n");
  200. if(mHealthdConfig->batteryPresentPath_smb.isEmpty())
  201. KLOG_WARNING(LOG_TAG,"BatteryPresentPath_smb not found\n");
  202. if(mHealthdConfig->batteryCapacityPath_smb.isEmpty())
  203. KLOG_WARNING(LOG_TAG,"BatteryCapacityPath_smb not found\n");
  204. if(mHealthdConfig->batteryStatusPath_smb.isEmpty())
  205. KLOG_WARNING(LOG_TAG,"BatteryStatusPath_smb not found\n");
  206. }
  207. if(property_get("ro.boot.fake_battery", pval, NULL)>0
  208. && strtol(pval, NULL,10)!=0){
  209. mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
  210. mBatteryFixedCapacity_smb = FAKE_BATTERY_CAPACITY_SMB;
  211. mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
  212. }
  213. }

该方法会打开/sys/class/power_supply目录,该目录中包含了一些子目录,/sys/class/power_supply/battery目录下存放得就是电池信息。在case ANDROID_POWER_SUPPLY_TYPE_BATTERY:情况下会生成与电池相关的文件名,并把对应的信息保存到成员变量中,方便后面读取使用。

然后是main函数进入healthd_mainloop()循环,其代码如下:

  1. staticvoid healthd_mainloop(void){
  2. while(1){
  3. struct epoll_event events[eventct];
  4. int nevents;
  5. int timeout = awake_poll_interval;
  6. int mode_timeout;
  7. mode_timeout = healthd_mode_ops->preparetowait();
  8. if(timeout <0||(mode_timeout >0&& mode_timeout < timeout))
  9. timeout = mode_timeout;
  10. nevents = epoll_wait(epollfd, events, eventct, timeout);
  11. if(nevents ==-1){
  12. if(errno == EINTR)
  13. continue;
  14. KLOG_ERROR(LOG_TAG,"healthd_mainloop: epoll_wait failed\n");
  15. break;
  16. }
  17. for(int n =0; n < nevents;++n){
  18. if(events[n].data.ptr)
  19. /* (*(void (*)(int))events[n].data.ptr)(events[n].events)这一行代码看起来好狗屎
  20. ,应该是把events[n].data.ptr(应该是某个函数指针)强制转换为参数为int型的返回值为空的函数的指针
  21. ,反正是一个指定类型的函数指针,然后再加了*号,表示调用该函数*/
  22. (*(void(*)(int))events[n].data.ptr)(events[n].events);
  23. }
  24. if(!nevents)
  25. periodic_chores();
  26. healthd_mode_ops->heartbeat();
  27. }
  28. return;
  29. }
  1. staticvoid wakealarm_event(uint32_t/*epevents*/){
  2. unsignedlonglong wakeups;
  3. if(read(wakealarm_fd,&wakeups,sizeof(wakeups))==-1){
  4. KLOG_ERROR(LOG_TAG,"wakealarm_event: read wakealarm fd failed\n");
  5. return;
  6. }
  7. periodic_chores();
  8. }
  1. staticvoid periodic_chores(){
  2. healthd_battery_update();
  3. }
  4. void healthd_battery_update(void){
  5. // Fast wake interval when on charger (watch for overheat);
  6. // slow wake interval when on battery (watch for drained battery).
  7. /*调用BatteryMonitor的update方法更新电池状态*/
  8. int new_wake_interval = gBatteryMonitor->update()?
  9. healthd_config.periodic_chores_interval_fast :
  10. healthd_config.periodic_chores_interval_slow;
  11. if(new_wake_interval != wakealarm_wake_interval)
  12. wakealarm_set_interval(new_wake_interval);
  13. // During awake periods poll at fast rate. If wake alarm is set at fast
  14. // rate then just use the alarm; if wake alarm is set at slow rate then
  15. // poll at fast rate while awake and let alarm wake up at slow rate when
  16. // asleep.
  17. if(healthd_config.periodic_chores_interval_fast ==-1)
  18. awake_poll_interval =-1;
  19. else
  20. awake_poll_interval =
  21. new_wake_interval == healthd_config.periodic_chores_interval_fast ?
  22. -1: healthd_config.periodic_chores_interval_fast *1000;
  23. }

然后查看文件system/core/healthd/BatteryMonitor.cpp中的update,该方法主要是用来读取电池信息,在health的main方法中,会通过health_mainloop()函数周期性调用它。

  1. boolBatteryMonitor::update(void){
  2. bool logthis;
  3. /*每次调用前会把BatteryProperties 对象中的值清空*/
  4. initBatteryProperties(&props);
  5. if(!mHealthdConfig->batteryPresentPath.isEmpty())
  6. props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
  7. else
  8. props.batteryPresent = mBatteryDevicePresent;
  9. /*将保存在mHealthdConfig中的电池信息赋值给BatteryProperties对象props中的各个成员变量,后面还会通过该对象把电池信息发送给上层*/
  10. props.batteryLevel = mBatteryFixedCapacity ?
  11. mBatteryFixedCapacity :
  12. getIntField(mHealthdConfig->batteryCapacityPath);
  13. props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath);
  14. if(!mHealthdConfig->batteryCurrentNowPath.isEmpty())
  15. props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath)/1000;
  16. if(!mHealthdConfig->batteryFullChargePath.isEmpty())
  17. props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
  18. if(!mHealthdConfig->batteryCycleCountPath.isEmpty())
  19. props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
  20. if(!mHealthdConfig->batteryChargeCounterPath.isEmpty())
  21. props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
  22. props.batteryTemperature = mBatteryFixedTemperature ?
  23. mBatteryFixedTemperature :
  24. getIntField(mHealthdConfig->batteryTemperaturePath);
  25. update_smb();
  26. // For devices which do not have battery and are always plugged
  27. // into power souce.
  28. if(mAlwaysPluggedDevice){
  29. props.chargerAcOnline =true;
  30. props.batteryPresent =true;
  31. props.batteryStatus = BATTERY_STATUS_CHARGING;
  32. props.batteryHealth = BATTERY_HEALTH_GOOD;
  33. }
  34. constint SIZE =128;
  35. char buf[SIZE];
  36. String8 btech;
  37. if(readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE)>0)
  38. props.batteryStatus = getBatteryStatus(buf);
  39. if(readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE)>0)
  40. props.batteryHealth = getBatteryHealth(buf);
  41. if(readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE)>0)
  42. props.batteryTechnology =String8(buf);
  43. unsignedint i;
  44. doubleMaxPower=0;
  45. for(i =0; i < mChargerNames.size(); i++){
  46. String8 path;
  47. path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
  48. mChargerNames[i].string());
  49. if(readFromFile(path, buf, SIZE)>0){
  50. if(buf[0]!=‘0‘){
  51. path.clear();
  52. path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
  53. mChargerNames[i].string());
  54. switch(readPowerSupplyType(path)){
  55. case ANDROID_POWER_SUPPLY_TYPE_AC:
  56. props.chargerAcOnline =true;
  57. break;
  58. case ANDROID_POWER_SUPPLY_TYPE_USB:
  59. props.chargerUsbOnline =true;
  60. break;
  61. case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
  62. props.chargerWirelessOnline =true;
  63. break;
  64. default:
  65. KLOG_WARNING(LOG_TAG,"%s: Unknown power supply type\n",
  66. mChargerNames[i].string());
  67. }
  68. path.clear();
  69. path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
  70. mChargerNames[i].string());
  71. intChargingCurrent=
  72. (access(path.string(), R_OK)==0)? getIntField(path):0;
  73. path.clear();
  74. path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
  75. mChargerNames[i].string());
  76. intChargingVoltage=
  77. (access(path.string(), R_OK)==0)? getIntField(path):
  78. DEFAULT_VBUS_VOLTAGE;
  79. double power =((double)ChargingCurrent/ MILLION)*
  80. ((double)ChargingVoltage/ MILLION);
  81. if(MaxPower< power){
  82. props.maxChargingCurrent =ChargingCurrent;
  83. props.maxChargingVoltage =ChargingVoltage;
  84. MaxPower= power;
  85. }
  86. }
  87. }
  88. }
  89. logthis =!healthd_board_battery_update(&props);
  90. if(logthis){
  91. char dmesgline[256];
  92. size_t len;
  93. if(props.batteryPresent){
  94. snprintf(dmesgline,sizeof(dmesgline),
  95. "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
  96. props.batteryLevel, props.batteryVoltage,
  97. props.batteryTemperature <0?"-":"",
  98. abs(props.batteryTemperature /10),
  99. abs(props.batteryTemperature %10), props.batteryHealth,
  100. props.batteryStatus);
  101. len = strlen(dmesgline);
  102. if(props.batteryPresent_smb){
  103. snprintf(dmesgline,sizeof(dmesgline),
  104. "battery l2=%d st2=%d ext=%d",
  105. props.batteryLevel_smb,
  106. props.batteryStatus_smb,
  107. props.batteryPresent_smb);
  108. }
  109. if(!mHealthdConfig->batteryCurrentNowPath.isEmpty()){
  110. len += snprintf(dmesgline + len,sizeof(dmesgline)- len,
  111. " c=%d", props.batteryCurrent);
  112. }
  113. if(!mHealthdConfig->batteryFullChargePath.isEmpty()){
  114. len += snprintf(dmesgline + len,sizeof(dmesgline)- len,
  115. " fc=%d", props.batteryFullCharge);
  116. }
  117. if(!mHealthdConfig->batteryCycleCountPath.isEmpty()){
  118. len += snprintf(dmesgline + len,sizeof(dmesgline)- len,
  119. " cc=%d", props.batteryCycleCount);
  120. }
  121. }else{
  122. snprintf(dmesgline,sizeof(dmesgline),
  123. "battery none");
  124. }
  125. len = strlen(dmesgline);
  126. KLOG_WARNING(LOG_TAG,"%s chg=%s%s%s\n", dmesgline,
  127. props.chargerAcOnline ?"a":"",
  128. props.chargerUsbOnline ?"u":"",
  129. props.chargerWirelessOnline ?"w":"");
  130. snprintf(dmesgline + len,sizeof(dmesgline)- len," chg=%s%s%s",
  131. props.chargerAcOnline ?"a":"",
  132. props.chargerUsbOnline ?"u":"",
  133. props.chargerWirelessOnline ?"w":"");
  134. KLOG_WARNING(LOG_TAG,"%s\n", dmesgline);
  135. }
  136. cmd_send();
  137. healthd_mode_ops->battery_update(&props);
  138. return props.chargerAcOnline | props.chargerUsbOnline |
  139. props.chargerWirelessOnline;
  140. }

然后是通过binder把电池信息props传入到上层batteryService中,在该Service中有个BatteryListener对象,该类的定义如下:

  1. privatefinalclassBatteryListenerextendsIBatteryPropertiesListener.Stub{
  2. @Overridepublicvoid batteryPropertiesChanged(BatteryProperties props){
  3. finallong identity =Binder.clearCallingIdentity();
  4. try{
  5. BatteryService.this.update(props);
  6. }finally{
  7. Binder.restoreCallingIdentity(identity);
  8. }
  9. }
  10. }

然后会把传入的BatteryProperties对象props(里面包含的都是电池信息)通过update方法赋值给mLastBatteryProps属性,update方法具体如下:

  1. privatevoid update(BatteryProperties props){
  2. synchronized(mLock){
  3. if(!mUpdatesStopped){
  4. mBatteryProps = props;
  5. if(SystemProperties.get("ro.mtk_ipo_support").equals("1")){
  6. if(mIPOShutdown)
  7. return;
  8. }
  9. // Process the new values.
  10. if(mBootCompleted)
  11. processValuesLocked(false);
  12. }else{
  13. mLastBatteryProps.set(props);
  14. }
  15. }
  16. }

然后看看BatteryProperties类里到底有些什么,具体如下:

  1. publicclassBatteryPropertiesimplementsParcelable{
  2. publicboolean chargerAcOnline;
  3. publicboolean chargerUsbOnline;
  4. publicboolean chargerWirelessOnline;
  5. publicint maxChargingCurrent;
  6. publicint maxChargingVoltage;
  7. publicint batteryStatus;
  8. publicint batteryStatus_smb;
  9. publicint batteryHealth;
  10. publicboolean batteryPresent;
  11. publicboolean batteryPresent_smb;
  12. publicint batteryLevel;
  13. publicint batteryLevel_smb;
  14. publicint batteryVoltage;
  15. publicint batteryTemperature;
  16. publicint batteryCurrentNow;
  17. publicint batteryChargeCounter;
  18. publicint adjustPower;
  19. publicString batteryTechnology;
  20. publicBatteryProperties(){
  21. }
  22. publicvoid set(BatteryProperties other){
  23. chargerAcOnline = other.chargerAcOnline;
  24. chargerUsbOnline = other.chargerUsbOnline;
  25. chargerWirelessOnline = other.chargerWirelessOnline;
  26. maxChargingCurrent = other.maxChargingCurrent;
  27. maxChargingVoltage = other.maxChargingVoltage;
  28. batteryStatus = other.batteryStatus;
  29. batteryHealth = other.batteryHealth;
  30. batteryPresent = other.batteryPresent;
  31. batteryLevel = other.batteryLevel;
  32. batteryVoltage = other.batteryVoltage;
  33. batteryTemperature = other.batteryTemperature;
  34. batteryStatus_smb = other.batteryStatus_smb;
  35. batteryPresent_smb = other.batteryPresent_smb;
  36. batteryLevel_smb = other.batteryLevel_smb;
  37. batteryCurrentNow = other.batteryCurrentNow;
  38. batteryChargeCounter = other.batteryChargeCounter;
  39. adjustPower = other.adjustPower;
  40. batteryTechnology = other.batteryTechnology;
  41. }
  42. /*
  43. * Parcel read/write code must be kept in sync with
  44. * frameworks/native/services/batteryservice/BatteryProperties.cpp
  45. */
  46. privateBatteryProperties(Parcel p){
  47. chargerAcOnline = p.readInt()==1?true:false;
  48. chargerUsbOnline = p.readInt()==1?true:false;
  49. chargerWirelessOnline = p.readInt()==1?true:false;
  50. maxChargingCurrent = p.readInt();
  51. maxChargingVoltage = p.readInt();
  52. batteryStatus = p.readInt();
  53. batteryHealth = p.readInt();
  54. batteryPresent = p.readInt()==1?true:false;
  55. batteryLevel = p.readInt();
  56. batteryVoltage = p.readInt();
  57. batteryTemperature = p.readInt();
  58. batteryStatus_smb = p.readInt();
  59. batteryPresent_smb = p.readInt()==1?true:false;
  60. batteryLevel_smb = p.readInt();
  61. batteryCurrentNow = p.readInt();
  62. batteryChargeCounter = p.readInt();
  63. adjustPower = p.readInt();
  64. batteryTechnology = p.readString();
  65. }
  66. publicvoid writeToParcel(Parcel p,int flags){
  67. p.writeInt(chargerAcOnline ?1:0);
  68. p.writeInt(chargerUsbOnline ?1:0);
  69. p.writeInt(chargerWirelessOnline ?1:0);
  70. p.writeInt(maxChargingCurrent);
  71. p.writeInt(maxChargingVoltage);
  72. p.writeInt(batteryStatus);
  73. p.writeInt(batteryHealth);
  74. p.writeInt(batteryPresent ?1:0);
  75. p.writeInt(batteryLevel);
  76. p.writeInt(batteryVoltage);
  77. p.writeInt(batteryTemperature);
  78. p.writeInt(batteryStatus_smb);
  79. p.writeInt(batteryPresent_smb ?1:0);
  80. p.writeInt(batteryLevel_smb);
  81. p.writeInt(batteryCurrentNow);
  82. p.writeInt(batteryChargeCounter);
  83. p.writeInt(adjustPower);
  84. p.writeString(batteryTechnology);
  85. }
  86. publicstaticfinalParcelable.Creator<BatteryProperties> CREATOR
  87. =newParcelable.Creator<BatteryProperties>(){
  88. publicBatteryProperties createFromParcel(Parcel p){
  89. returnnewBatteryProperties(p);
  90. }
  91. publicBatteryProperties[] newArray(int size){
  92. returnnewBatteryProperties[size];
  93. }
  94. };
  95. publicint describeContents(){
  96. return0;
  97. }
  98. }

这个类是个工具类,主要用来打包存放电池信息和为各个电池属性进行赋值操作。然后转入batteryService.java文件。在上层处理关机动画的的流程中,主要根据如下逻辑进行操作的。

Created with Rapha?l 2.1.4StartBatteryService.java中shutdownIfNoPowerLocked()方法 ShutdownActivity.javaPowerManagerService.javaShutdownThread.javaPowerManager.javaEnd
  1. st=>start:Start
  2. e=>end:End
  3. op1=>operation:BatteryService.javashutdownIfNoPowerLocked()方法
  4. op2=>operation:ShutdownActivity.java
  5. op3=>operation:PowerManagerService.java
  6. op4=>operation:ShutdownThread.java
  7. op5=>operation:PowerManager.java
  8. st->op1->op2->op3->op4->op5->e

首先看看batteryService类的构造方法,具体代码如下:

  1. publicBatteryService(Context context){
  2. super(context);
  3. mContext = context;
  4. mHandler =newHandler(true/*async*/);
  5. mLed =newLed(context, getLocalService(LightsManager.class));
  6. mSettingsObserver =newSettingsObserver();
  7. mBatteryStats =BatteryStatsService.getService();
  8. mCriticalBatteryLevel = mContext.getResources().getInteger(
  9. com.android.internal.R.integer.config_criticalBatteryWarningLevel);
  10. mLowBatteryWarningLevel = mContext.getResources().getInteger(
  11. com.android.internal.R.integer.config_lowBatteryWarningLevel);
  12. mLowBatteryCloseWarningLevel = mLowBatteryWarningLevel + mContext.getResources().getInteger(
  13. com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
  14. mShutdownBatteryTemperature = mContext.getResources().getInteger(
  15. com.android.internal.R.integer.config_shutdownBatteryTemperature);
  16. // watch for invalid charger messages if the invalid_charger switch exists
  17. if(newFile("/sys/devices/virtual/switch/invalid_charger/state").exists()){
  18. UEventObserver invalidChargerObserver =newUEventObserver(){
  19. @Override
  20. publicvoid onUEvent(UEvent event){
  21. finalint invalidCharger ="1".equals(event.get("SWITCH_STATE"))?1:0;
  22. synchronized(mLock){
  23. if(mInvalidCharger != invalidCharger){
  24. mInvalidCharger = invalidCharger;
  25. }
  26. }
  27. }
  28. };
  29. invalidChargerObserver.startObserving(
  30. "DEVPATH=/devices/virtual/switch/invalid_charger");
  31. }
  32. }

构造方法中会先获得BatteryStatsService的对象,BatteryStatsService主要功能是手机系统中各个模块和进程的耗电情况,通过BatteryStatsService来得到相关数据。然后是系统设定各种低电量报警值:

  • mLowBatteryWarningLevel
    这个是电池电量不足,系统将自动发出警报
  • mLowBatteryCloseWarningLevel
    表示停止电量不足警告的值
  • mShutdownBatteryTemperature
    表示电池温度过高,系统将自动关闭
  • mCriticalBatteryLevel
    表示电池电量太低了,低于它会自动关机

然后还会创建一个UEventObserver对象,用来监听UEvent事件,主要监听设备插入无效充电器的事件,如果发生该事件,会调用onUEvent方法。

根据控件生命周期该服务会执行onStart()方法,该方法具体代码如下:

  1. publicvoid onStart(){
  2. IBinder b =ServiceManager.getService("batteryproperties");
  3. finalIBatteryPropertiesRegistrar batteryPropertiesRegistrar =
  4. IBatteryPropertiesRegistrar.Stub.asInterface(b);
  5. try{
  6. /*系统注册监听,实时获得底层传来的电池信息*/
  7. batteryPropertiesRegistrar.registerListener(newBatteryListener());
  8. }catch(RemoteException e){
  9. // Should never happen.
  10. }
  11. /*这个if里面的逻辑实际上不会调用*/
  12. if(SystemProperties.get("ro.mtk_ipo_support").equals("1")){
  13. IntentFilter filter =newIntentFilter();
  14. filter.addAction(IPO_POWER_ON);
  15. filter.addAction(IPO_POWER_OFF);
  16. mContext.registerReceiver(newBroadcastReceiver(){
  17. @Override
  18. publicvoid onReceive(Context context,Intent intent){
  19. if(IPO_POWER_ON.equals(intent.getAction())){
  20. mIPOShutdown =false;
  21. mIPOBoot =true;
  22. // Let BatteryService to handle low battery warning.
  23. mLastBatteryLevel = mLowBatteryWarningLevel +1;
  24. update(mBatteryProps);
  25. }else
  26. if(IPO_POWER_OFF.equals(intent.getAction())){
  27. mIPOShutdown =true;
  28. }
  29. }
  30. }, filter);
  31. }
  32. mBinderService =newBinderService();
  33. publishBinderService("battery", mBinderService);
  34. publishLocalService(BatteryManagerInternal.class,newLocalService());
  35. }

该方法中主要是注册一个监听BatteryListener(),接收底层传来的电池信息,并调用 processValuesLocked()方法,BatteryListener类的具体实现前面已经展示过,这里不再累述。接下来看看processValuesLocked方法的具体实现:

  1. privatevoid processValuesLocked(boolean force){
  2. boolean logOutlier =false;
  3. long dischargeDuration =0;
  4. mBatteryLevelCritical =(mBatteryProps.batteryLevel <= mCriticalBatteryLevel);
  5. /*设置手机连接充电设备的方法*/
  6. if(mBatteryProps.chargerAcOnline){
  7. mPlugType =BatteryManager.BATTERY_PLUGGED_AC;
  8. }elseif(mBatteryProps.chargerUsbOnline){
  9. mPlugType =BatteryManager.BATTERY_PLUGGED_USB;
  10. }elseif(mBatteryProps.chargerWirelessOnline){
  11. mPlugType =BatteryManager.BATTERY_PLUGGED_WIRELESS;
  12. }else{
  13. mPlugType = BATTERY_PLUGGED_NONE;
  14. }
  15. /// M: Add for DUAL_INPUT_CHARGER_SUPPORT @{
  16. if(SystemProperties.get("ro.mtk_diso_support").equals("true")){
  17. if(mBatteryProps.chargerAcOnline && mBatteryProps.chargerUsbOnline){
  18. mPlugType =BatteryManager.BATTERY_PLUGGED_AC |BatteryManager.BATTERY_PLUGGED_USB;
  19. }
  20. }
  21. /// M: @}
  22. if(DEBUG){
  23. Slog.d(TAG,"Processing new values: "
  24. +"chargerAcOnline="+ mBatteryProps.chargerAcOnline
  25. +", chargerUsbOnline="+ mBatteryProps.chargerUsbOnline
  26. +", chargerWirelessOnline="+ mBatteryProps.chargerWirelessOnline
  27. +", maxChargingCurrent"+ mBatteryProps.maxChargingCurrent
  28. +", maxChargingVoltage"+ mBatteryProps.maxChargingVoltage
  29. +", chargeCounter"+ mBatteryProps.batteryChargeCounter
  30. +", batteryStatus="+ mBatteryProps.batteryStatus
  31. +", batteryHealth="+ mBatteryProps.batteryHealth
  32. +", batteryPresent="+ mBatteryProps.batteryPresent
  33. +", batteryLevel="+ mBatteryProps.batteryLevel
  34. +", batteryTechnology="+ mBatteryProps.batteryTechnology
  35. +", batteryVoltage="+ mBatteryProps.batteryVoltage
  36. +", batteryTemperature="+ mBatteryProps.batteryTemperature
  37. +", mBatteryLevelCritical="+ mBatteryLevelCritical
  38. +", mPlugType="+ mPlugType);
  39. }
  40. if(mLastBatteryVoltage != mBatteryProps.batteryVoltage){
  41. Log.d(TAG,"mBatteryVoltage="+ mBatteryProps.batteryVoltage +", batteryLevel="+ mBatteryProps.batteryLevel_smb);
  42. }
  43. // Update the battery LED
  44. mLed.updateLightsLocked();
  45. // Let the battery stats keep track of the current level.
  46. try{
  47. mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
  48. mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
  49. mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter);
  50. }catch(RemoteException e){
  51. // Should never happen.
  52. }
  53. /*这里就是判断低电量关机的逻辑*/
  54. shutdownIfNoPowerLocked();
  55. shutdownIfOverTempLocked();
  56. if(force ||(mBatteryProps.batteryStatus != mLastBatteryStatus ||
  57. mBatteryProps.batteryStatus_smb != mLastBatteryStatus_smb ||
  58. mBatteryProps.batteryHealth != mLastBatteryHealth ||
  59. mBatteryProps.batteryPresent != mLastBatteryPresent ||
  60. mBatteryProps.batteryPresent_smb != mLastBatteryPresent_smb ||
  61. mBatteryProps.batteryLevel != mLastBatteryLevel ||
  62. mBatteryProps.batteryLevel_smb != mLastBatteryLevel_smb ||
  63. mPlugType != mLastPlugType ||
  64. mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
  65. mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
  66. mBatteryProps.maxChargingCurrent != mLastMaxChargingCurrent ||
  67. mBatteryProps.maxChargingVoltage != mLastMaxChargingVoltage ||
  68. mBatteryProps.batteryChargeCounter != mLastChargeCounter ||
  69. mInvalidCharger != mLastInvalidCharger)){
  70. if(mPlugType != mLastPlugType){
  71. if(mLastPlugType == BATTERY_PLUGGED_NONE){
  72. // discharging -> charging
  73. // There‘s no value in this data unless we‘ve discharged at least once and the
  74. // battery level has changed; so don‘t log until it does.
  75. if(mDischargeStartTime !=0&& mDischargeStartLevel != mBatteryProps.batteryLevel){
  76. dischargeDuration =SystemClock.elapsedRealtime()- mDischargeStartTime;
  77. logOutlier =true;
  78. EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration,
  79. mDischargeStartLevel, mBatteryProps.batteryLevel);
  80. // make sure we see a discharge event before logging again
  81. mDischargeStartTime =0;
  82. }
  83. }elseif(mPlugType == BATTERY_PLUGGED_NONE){
  84. // charging -> discharging or we just powered up
  85. mDischargeStartTime =SystemClock.elapsedRealtime();
  86. mDischargeStartLevel = mBatteryProps.batteryLevel;
  87. }
  88. }
  89. if(mBatteryProps.batteryStatus != mLastBatteryStatus ||
  90. mBatteryProps.batteryHealth != mLastBatteryHealth ||
  91. mBatteryProps.batteryPresent != mLastBatteryPresent ||
  92. mPlugType != mLastPlugType){
  93. EventLog.writeEvent(EventLogTags.BATTERY_STATUS,
  94. mBatteryProps.batteryStatus, mBatteryProps.batteryHealth, mBatteryProps.batteryPresent ?1:0,
  95. mPlugType, mBatteryProps.batteryTechnology);
  96. }
  97. if(mBatteryProps.batteryLevel != mLastBatteryLevel){
  98. // Don‘t do this just from voltage or temperature changes, that is
  99. // too noisy.
  100. EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
  101. mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
  102. }
  103. if(mBatteryLevelCritical &&!mLastBatteryLevelCritical &&
  104. mPlugType == BATTERY_PLUGGED_NONE){
  105. // We want to make sure we log discharge cycle outliers
  106. // if the battery is about to die.
  107. dischargeDuration =SystemClock.elapsedRealtime()- mDischargeStartTime;
  108. logOutlier =true;
  109. }
  110. if(!mBatteryLevelLow){
  111. // Should we now switch in to low battery mode?
  112. if(mPlugType == BATTERY_PLUGGED_NONE
  113. && mBatteryProps.batteryLevel <= mLowBatteryWarningLevel){
  114. mBatteryLevelLow =true;
  115. }
  116. }else{
  117. // Should we now switch out of low battery mode?
  118. if(mPlugType != BATTERY_PLUGGED_NONE){
  119. mBatteryLevelLow =false;
  120. }elseif(mBatteryProps.batteryLevel >= mLowBatteryCloseWarningLevel){
  121. mBatteryLevelLow =false;
  122. }elseif(force && mBatteryProps.batteryLevel >= mLowBatteryWarningLevel){
  123. // If being forced, the previous state doesn‘t matter, we will just
  124. // absolutely check to see if we are now above the warning level.
  125. mBatteryLevelLow =false;
  126. }
  127. }
  128. sendIntentLocked();
  129. // Separate broadcast is sent for power connected / not connected
  130. // since the standard intent will not wake any applications and some
  131. // applications may want to have smart behavior based on this.
  132. if(mPlugType !=0&& mLastPlugType ==0){
  133. mHandler.post(newRunnable(){
  134. @Override
  135. publicvoid run(){
  136. Intent statusIntent =newIntent(Intent.ACTION_POWER_CONNECTED);
  137. statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
  138. mContext.sendBroadcastAsUser(statusIntent,UserHandle.ALL);
  139. }
  140. });
  141. }
  142. elseif(mPlugType ==0&& mLastPlugType !=0){
  143. mHandler.post(newRunnable(){
  144. @Override
  145. publicvoid run(){
  146. Intent statusIntent =newIntent(Intent.ACTION_POWER_DISCONNECTED);
  147. statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
  148. mContext.sendBroadcastAsUser(statusIntent,UserHandle.ALL);
  149. }
  150. });
  151. }
  152. if(shouldSendBatteryLowLocked()){
  153. mSentLowBatteryBroadcast =true;
  154. mHandler.post(newRunnable(){
  155. @Override
  156. publicvoid run(){
  157. Intent statusIntent =newIntent(Intent.ACTION_BATTERY_LOW);
  158. statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
  159. mContext.sendBroadcastAsUser(statusIntent,UserHandle.ALL);
  160. }
  161. });
  162. }elseif(mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel){
  163. mSentLowBatteryBroadcast =false;
  164. mHandler.post(newRunnable(){
  165. @Override
  166. publicvoid run(){
  167. Intent statusIntent =newIntent(Intent.ACTION_BATTERY_OKAY);
  168. statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
  169. mContext.sendBroadcastAsUser(statusIntent,UserHandle.ALL);
  170. }
  171. });
  172. }
  173. if(mBatteryProps.batteryStatus != mLastBatteryStatus &&
  174. mBatteryProps.batteryStatus ==BatteryManager.BATTERY_STATUS_CMD_DISCHARGING){
  175. mHandler.post(newRunnable(){
  176. @Override
  177. publicvoid run(){
  178. finalString ACTION_IGNORE_DATA_USAGE_ALERT =
  179. "android.intent.action.IGNORE_DATA_USAGE_ALERT";
  180. Log.d(TAG,"sendBroadcast ACTION_IGNORE_DATA_USAGE_ALERT");
  181. Intent statusIntent =newIntent(ACTION_IGNORE_DATA_USAGE_ALERT);
  182. statusIntent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
  183. mContext.sendBroadcastAsUser(statusIntent,UserHandle.ALL);
  184. }
  185. });
  186. }
  187. // Update the battery LED
  188. // mLed.updateLightsLocked();
  189. // This needs to be done after sendIntent() so that we get the lastest battery stats.
  190. if(logOutlier && dischargeDuration !=0){
  191. logOutlierLocked(dischargeDuration);
  192. }
  193. mLastBatteryStatus = mBatteryProps.batteryStatus;
  194. mLastBatteryStatus_smb = mBatteryProps.batteryStatus_smb;
  195. mLastBatteryHealth = mBatteryProps.batteryHealth;
  196. mLastBatteryPresent = mBatteryProps.batteryPresent;
  197. mLastBatteryPresent_smb = mBatteryProps.batteryPresent_smb;
  198. mLastBatteryLevel = mBatteryProps.batteryLevel;
  199. mLastBatteryLevel_smb = mBatteryProps.batteryLevel_smb;
  200. mLastPlugType = mPlugType;
  201. mLastBatteryVoltage = mBatteryProps.batteryVoltage;
  202. mLastBatteryTemperature = mBatteryProps.batteryTemperature;
  203. mLastMaxChargingCurrent = mBatteryProps.maxChargingCurrent;
  204. mLastMaxChargingVoltage = mBatteryProps.maxChargingVoltage;
  205. mLastChargeCounter = mBatteryProps.batteryChargeCounter;
  206. mLastBatteryLevelCritical = mBatteryLevelCritical;
  207. mLastInvalidCharger = mInvalidCharger;
  208. }
  209. }

每次有电池信息改变是,都会调用processValuesLocked这个方法,并通过shutdownIfNoPowerLocked();来判断是否进入低电量关机逻辑。然后看看shutdownIfNoPowerLocked的实现:

  1. privatevoid shutdownIfNoPowerLocked(){
  2. // shut down gracefully if our battery is critically low and we are not powered.
  3. // wait until the system has booted before attempting to display the shutdown dialog.
  4. /*即判断如果电池电量为0,并且没有连接充电设备 就进入if逻辑中*/
  5. if(mBatteryProps.batteryLevel ==0&&!isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)){
  6. mHandler.post(newRunnable(){
  7. @Override
  8. publicvoid run(){
  9. if(ActivityManagerNative.isSystemReady()){
  10. if(SystemProperties.get("ro.mtk_ipo_support").equals("1")){
  11. SystemProperties.set("sys.ipo.battlow","1");
  12. }
  13. Intent intent =newIntent(Intent.ACTION_REQUEST_SHUTDOWN);
  14. /*把一些相关信息通过intent,传入目标activity中*/
  15. intent.putExtra(Intent.EXTRA_KEY_CONFIRM,false);
  16. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  17. mContext.startActivityAsUser(intent,UserHandle.CURRENT);
  18. }
  19. }
  20. });
  21. }
  22. }
  1. <activityandroid:name="com.android.internal.app.ShutdownActivity"
  2. android:permission="android.permission.SHUTDOWN"
  3. android:theme="@style/Theme.NoDisplay"
  4. android:excludeFromRecents="true">
  5. <intent-filter>
  6. <actionandroid:name="android.intent.action.ACTION_REQUEST_SHUTDOWN"/>
  7. <categoryandroid:name="android.intent.category.DEFAULT"/>
  8. </intent-filter>
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.REBOOT"/>
  11. <categoryandroid:name="android.intent.category.DEFAULT"/>
  12. </intent-filter>
  13. </activity>

然后查看ShutdownActivity.java的具体实现

  1. publicclassShutdownActivityextendsActivity{
  2. privatestaticfinalString TAG ="ShutdownActivity";
  3. privateboolean mReboot;
  4. privateboolean mConfirm;
  5. privateboolean mUserRequested;
  6. @Override
  7. protectedvoid onCreate(Bundle savedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. Intent intent = getIntent();
  10. mReboot =Intent.ACTION_REBOOT.equals(intent.getAction());
  11. mConfirm = intent.getBooleanExtra(Intent.EXTRA_KEY_CONFIRM,false);
  12. mUserRequested = intent.getBooleanExtra(Intent.EXTRA_USER_REQUESTED_SHUTDOWN,false);
  13. Slog.i(TAG,"onCreate(): confirm="+ mConfirm);
  14. Thread thr =newThread("ShutdownActivity"){
  15. @Override
  16. publicvoid run(){
  17. IPowerManager pm =IPowerManager.Stub.asInterface(
  18. ServiceManager.getService(Context.POWER_SERVICE));
  19. try{
  20. if(mReboot){
  21. pm.reboot(mConfirm,null,false);
  22. }else{
  23. pm.shutdown(mConfirm,
  24. mUserRequested ?PowerManager.SHUTDOWN_USER_REQUESTED :null,
  25. false);
  26. }
  27. }catch(RemoteException e){
  28. }
  29. }
  30. };
  31. thr.start();
  32. finish();
  33. // Wait for us to tell the power manager to shutdown.
  34. try{
  35. thr.join();
  36. }catch(InterruptedException e){
  37. }
  38. }
  39. }

在该activity中,首先会得到intent中的附件信息,然后创建一个线程,在线程中获得一个IPowerManager对象,并调用它的相关方法。然后转到PowerManagerService.java,PowerManagerService继承了SystemService,它的shutdown方法具体实现如下:

  1. publicvoid shutdown(boolean confirm,String reason,boolean wait){ mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT,null);
  2. finallong ident =Binder.clearCallingIdentity();
  3. try{
  4. shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, confirm, reason, wait);
  5. }finally{
  6. Binder.restoreCallingIdentity(ident);
  7. }
  8. }
  9. privatevoid shutdownOrRebootInternal(final@HaltModeint haltMode,finalboolean confirm,
  10. finalString reason,boolean wait){
  11. if(mHandler ==null||!mSystemReady){
  12. thrownewIllegalStateException("Too early to call shutdown() or reboot()");
  13. }
  14. /*创建一个线程来做关机出来*/
  15. Runnable runnable =newRunnable(){
  16. @Override
  17. publicvoid run(){
  18. synchronized(this){
  19. /*选择重启或关机*/
  20. if(haltMode == HALT_MODE_REBOOT_SAFE_MODE){
  21. ShutdownThread.rebootSafeMode(mContext, confirm);
  22. }elseif(haltMode == HALT_MODE_REBOOT){
  23. ShutdownThread.reboot(mContext, reason, confirm);
  24. }else{
  25. ShutdownThread.shutdown(mContext, reason, confirm);
  26. }
  27. }
  28. }
  29. };
  30. // ShutdownThread must run on a looper capable of displaying the UI.
  31. Message msg =Message.obtain(mHandler, runnable);
  32. msg.setAsynchronous(true);
  33. /*把线程对象通过Message发出去??*/
  34. mHandler.sendMessage(msg);
  35. // PowerManager.reboot() is documented not to return so just wait for the inevitable.
  36. if(wait){
  37. synchronized(runnable){
  38. while(true){
  39. try{
  40. runnable.wait();
  41. }catch(InterruptedException e){
  42. }
  43. }
  44. }
  45. }
  46. }

然后查看ShutdownThread.java的shutdown方法,其实现如下:

  1. publicstaticvoid shutdown(finalContext context,String reason,boolean confirm){
  2. mReboot =false;
  3. mRebootSafeMode =false;
  4. mReason = reason;
  5. Log.d(TAG,"!!! Request to shutdown !!!");
  6. if(mSpew){
  7. StackTraceElement[] stack =newThrowable().getStackTrace();
  8. for(StackTraceElement element : stack)
  9. {
  10. Log.d(TAG," |----"+ element.toString());
  11. }
  12. }
  13. if(SystemProperties.getBoolean("ro.monkey",false)){
  14. Log.d(TAG,"Cannot request to shutdown when Monkey is running, returning.");
  15. return;
  16. }
  17. shutdownInner(context, confirm);
  18. }
  19. staticvoid shutdownInner(finalContext context,boolean confirm){
  20. // ensure that only one thread is trying to power down.
  21. // any additional calls are just returned
  22. synchronized(sIsStartedGuard){
  23. if(sIsStarted){
  24. Log.d(TAG,"Request to shutdown already running, returning.");
  25. return;
  26. }
  27. }
  28. bConfirmForAnimation = confirm;
  29. finalint longPressBehavior = context.getResources().getInteger(
  30. com.android.internal.R.integer.config_longPressOnPowerBehavior);
  31. finalint resourceId = mRebootSafeMode
  32. ? com.android.internal.R.string.reboot_safemode_confirm
  33. :(mReboot
  34. ? com.android.internal.R.string.reboot_confirm
  35. : com.android.internal.R.string.shutdown_confirm);
  36. Log.d(TAG,"Notifying thread to start shutdown longPressBehavior="+ longPressBehavior);
  37. /*低电量关机流程时,confirm==false,因此走else分支*/
  38. if(confirm){
  39. finalCloseDialogReceiver closer =newCloseDialogReceiver(context);
  40. if(sConfirmDialog !=null){
  41. sConfirmDialog.dismiss();
  42. }
  43. bConfirmForAnimation = confirm;
  44. Log.d(TAG,"PowerOff dialog doesn‘t exist. Create it first");
  45. sConfirmDialog =newAlertDialog.Builder(context)
  46. .setTitle(mRebootSafeMode
  47. ? com.android.internal.R.string.reboot_safemode_title
  48. :(mReboot
  49. ? com.android.internal.R.string.reboot_title
  50. : com.android.internal.R.string.power_off))
  51. /*OLD : com.android.internal.R.string.power_off)*/
  52. .setMessage(resourceId)
  53. .setPositiveButton(mReboot
  54. ? com.android.internal.R.string.yes
  55. : com.android.internal.R.string.power_off_confirm_bt,
  56. newDialogInterface.OnClickListener(){
  57. publicvoid onClick(DialogInterface dialog,int which){
  58. beginShutdownSequence(context);
  59. }
  60. })
  61. .setNegativeButton(com.android.internal.R.string.no,null)
  62. .create();
  63. closer.dialog = sConfirmDialog;
  64. sConfirmDialog.setOnDismissListener(closer);
  65. sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  66. sConfirmDialog.show();
  67. }else{/*进入低电量关机流程*/
  68. beginShutdownSequence(context);
  69. }
  70. }
  1. privatestaticvoid beginShutdownSequence(Context context){
  2. Intent newIntent =newIntent("begin.Shutdown.Sequence");
  3. context.sendBroadcast(newIntent);
  4. synchronized(sIsStartedGuard){
  5. if(sIsStarted){
  6. Log.d(TAG,"Shutdown sequence already running, returning.");
  7. return;
  8. }
  9. sIsStarted =true;
  10. }
  11. // Throw up a system dialog to indicate the device is rebooting / shutting down.
  12. /*此处应该是创建Dialog,如正常关机前 会让用户选择是重启 ?关机? 飞行模式?等等 ,不过低电量关机 和这个Dialog 没什么关系*/
  13. ProgressDialog pd =newProgressDialog(context);
  14. // Path 1: Reboot to recovery for update
  15. // Condition: mReason == REBOOT_RECOVERY_UPDATE
  16. //
  17. // Path 1a: uncrypt needed
  18. // Condition: if /cache/recovery/uncrypt_file exists but
  19. // /cache/recovery/block.map doesn‘t.
  20. // UI: determinate progress bar (mRebootHasProgressBar == True)
  21. //
  22. // * Path 1a is expected to be removed once the GmsCore shipped on
  23. // device always calls uncrypt prior to reboot.
  24. //
  25. // Path 1b: uncrypt already done
  26. // UI: spinning circle only (no progress bar)
  27. //
  28. // Path 2: Reboot to recovery for factory reset
  29. // Condition: mReason == REBOOT_RECOVERY
  30. // UI: spinning circle only (no progress bar)
  31. //
  32. // Path 3: Regular reboot / shutdown
  33. // Condition: Otherwise
  34. // UI: spinning circle only (no progress bar)
  35. /*又是一大拨if 判断 */
  36. if(PowerManager.REBOOT_RECOVERY_UPDATE.equals(mReason)){
  37. // We need the progress bar if uncrypt will be invoked during the
  38. // reboot, which might be time-consuming.
  39. mRebootHasProgressBar =RecoverySystem.UNCRYPT_PACKAGE_FILE.exists()
  40. &&!(RecoverySystem.BLOCK_MAP_FILE.exists());
  41. pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_update_title));
  42. if(mRebootHasProgressBar){
  43. pd.setMax(100);
  44. pd.setProgress(0);
  45. pd.setIndeterminate(false);
  46. pd.setProgressNumberFormat(null);
  47. pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  48. pd.setMessage(context.getText(
  49. com.android.internal.R.string.reboot_to_update_prepare));
  50. pd.setCancelable(false);
  51. pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  52. pd.show();
  53. sInstance.mProgressDialog = pd;
  54. }else{
  55. pd.setIndeterminate(true);
  56. pd.setMessage(context.getText(
  57. com.android.internal.R.string.reboot_to_update_reboot));
  58. }
  59. }elseif(PowerManager.REBOOT_RECOVERY.equals(mReason)){
  60. // Factory reset path. Set the dialog message accordingly.
  61. pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_reset_title));
  62. pd.setMessage(context.getText(
  63. com.android.internal.R.string.reboot_to_reset_message));
  64. pd.setIndeterminate(true);
  65. }else{
  66. pd.setTitle(context.getText(com.android.internal.R.string.power_off));
  67. pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
  68. pd.setIndeterminate(true);
  69. }
  70. pd.setCancelable(false);
  71. pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  72. // start the thread that initiates shutdown
  73. sInstance.mContext = context;
  74. sInstance.mPowerManager =(PowerManager)context.getSystemService(Context.POWER_SERVICE);
  75. sInstance.mHandler =newHandler(){
  76. };
  77. beginAnimationTime =0;
  78. /*关机动画客制化*/
  79. boolean mShutOffAnimation = configShutdownAnimation(context);
  80. /*即获得关机时 动画的播放时间???*/
  81. int screenTurnOffTime = getScreenTurnOffTime(context);
  82. synchronized(mEnableAnimatingSync){
  83. if(mEnableAnimating){
  84. if(mRebootHasProgressBar){
  85. pd.show();
  86. screenTurnOffTime += MAX_UNCRYPT_WAIT_TIME;
  87. sInstance.mProgressDialog = pd;
  88. }else{
  89. /*低电量关机会走这个分支*/
  90. Log.d(TAG,"mIBootAnim.isCustBootAnim() is true");
  91. /*这里调用 bootanimCust方法来播放关机动画,实际上是开启一个播放关机动画的服务*/
  92. bootanimCust(context);
  93. }
  94. sInstance.mHandler.postDelayed(mDelayDim, screenTurnOffTime);
  95. }
  96. }
  97. // make sure we never fall asleep again
  98. sInstance.mCpuWakeLock =null;
  99. try{
  100. sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(
  101. PowerManager.PARTIAL_WAKE_LOCK, TAG +"-cpu");
  102. sInstance.mCpuWakeLock.setReferenceCounted(false);
  103. sInstance.mCpuWakeLock.acquire();
  104. }catch(SecurityException e){
  105. Log.w(TAG,"No permission to acquire wake lock", e);
  106. sInstance.mCpuWakeLock =null;
  107. }
  108. // also make sure the screen stays on for better user experience
  109. sInstance.mScreenWakeLock =null;
  110. if(sInstance.mPowerManager.isScreenOn()){
  111. try{
  112. sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
  113. PowerManager.FULL_WAKE_LOCK, TAG +"-screen");
  114. sInstance.mScreenWakeLock.setReferenceCounted(false);
  115. sInstance.mScreenWakeLock.acquire();
  116. }catch(SecurityException e){
  117. Log.w(TAG,"No permission to acquire wake lock", e);
  118. sInstance.mScreenWakeLock =null;
  119. }
  120. }
  121. // also make sure the screen stays on for better user experience
  122. //sInstance.mScreenWakeLock = null;
  123. if(sInstance.mPowerManager.isScreenOn()){
  124. // if Screen old state is on, keep it on
  125. if(sInstance.mScreenWakeLock ==null){
  126. Log.d(TAG,"Screen old state is on, keep screen on !");
  127. try{
  128. sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
  129. PowerManager.FULL_WAKE_LOCK, TAG +"-screen");
  130. sInstance.mScreenWakeLock.setReferenceCounted(false);
  131. sInstance.mScreenWakeLock.acquire();
  132. }catch(SecurityException e){
  133. Log.w(TAG,"No permission to acquire wake lock", e);
  134. sInstance.mScreenWakeLock =null;
  135. }
  136. }else{
  137. Log.d(TAG,"Screen is already wake up !");
  138. }
  139. }else{
  140. // WARING: if system is in low power level, pls charge your phone immediately
  141. Log.d(TAG,"First time wake up Screen failed, try to wake up Screen second time !");
  142. wakeUpScreen();
  143. }
  144. if(sInstance.getState()!=Thread.State.NEW || sInstance.isAlive()){
  145. if(sInstance.mShutdownFlow == IPO_SHUTDOWN_FLOW){
  146. Log.d(TAG,"ShutdownThread exists already");
  147. checkShutdownFlow();
  148. synchronized(mShutdownThreadSync){
  149. mShutdownThreadSync.notify();
  150. }
  151. }else{
  152. Log.e(TAG,"Thread state is not normal! froce to shutdown!");
  153. delayForPlayAnimation();
  154. //unmout data/cache partitions while performing shutdown
  155. sInstance.mPowerManager.goToSleep(SystemClock.uptimeMillis(),
  156. PowerManager.GO_TO_SLEEP_REASON_SHUTDOWN,0);
  157. PowerManagerService.lowLevelShutdown(mReason);
  158. }
  159. }else{
  160. /*开启线程 关闭系统的所有服务*/
  161. sInstance.start();
  162. }
  163. }

beginShutdownSequence主要处理关机过程中的一系列流程,通过调用bootanimCust(context)来开机播放关机动画的服务,然后会计算关机动画时间并保证这个时间中屏幕不会熄灭,同时开启一个sInstance线程来关闭系统中的服务。bootanimCust方法现象代码如下:

  1. privatestaticvoid bootanimCust(Context context){
  2. // [MTK] fix shutdown animation timing issue
  3. //==================================================================
  4. SystemProperties.set("service.shutanim.running","0");
  5. Log.i(TAG,"set service.shutanim.running to 0");
  6. //==================================================================
  7. boolean isRotaionEnabled =false;
  8. try{
  9. isRotaionEnabled =Settings.System.getInt(context.getContentResolver(),
  10. Settings.System.ACCELEROMETER_ROTATION,1)!=0;
  11. if(isRotaionEnabled){
  12. finalIWindowManager wm =IWindowManager.Stub.asInterface(
  13. ServiceManager.getService(Context.WINDOW_SERVICE));
  14. if(wm !=null){
  15. wm.freezeRotation(Surface.ROTATION_0);
  16. }
  17. Settings.System.putInt(context.getContentResolver(),
  18. Settings.System.ACCELEROMETER_ROTATION,0);
  19. Settings.System.putInt(context.getContentResolver(),
  20. Settings.System.ACCELEROMETER_ROTATION_RESTORE,1);
  21. }
  22. }catch(NullPointerException ex){
  23. Log.e(TAG,"check Rotation: context object is null when get Rotation");
  24. }catch(RemoteException e){
  25. e.printStackTrace();
  26. }
  27. beginAnimationTime =SystemClock.elapsedRealtime()+ MIN_SHUTDOWN_ANIMATION_PLAY_TIME;
  28. try{
  29. finalIWindowManager wm =IWindowManager.Stub.asInterface(
  30. ServiceManager.getService(Context.WINDOW_SERVICE));
  31. if(wm !=null){
  32. wm.setEventDispatching(false);
  33. }
  34. }catch(RemoteException e){
  35. e.printStackTrace();
  36. }
  37. startBootAnimation();
  38. }
  39. privatestaticvoid startBootAnimation(){
  40. Log.d(TAG,"Set ‘service.bootanim.exit‘ = 0).");
  41. SystemProperties.set("service.bootanim.exit","0");
  42. if(bPlayaudio){
  43. /*在文件ServiceManager.cpp中有如下注释
  44. - Starting a service is done by writing its name to the "ctl.start" system property. This triggers the init daemon to actually start
  45. the service for us.
  46. 大概意思就是向ctl.start属性中写入某个服务名,触发器会开启该服务吧(英文不好 反正大概知道是会开启某个服务的意思)
  47. Stopping the service is done by writing its name to "ctl.stop"
  48. in a similar way
  49. 低电量关机进入if逻辑中 此处会开启一个服务来播放关机动画*/
  50. SystemProperties.set("ctl.start","banim_shutmp3");
  51. Log.d(TAG,"bootanim:shut mp3");
  52. }else{
  53. //SystemProperties.set("ctl.start", "bootanim:shut nomp3");
  54. SystemProperties.set("ctl.start","banim_shutnomp3");
  55. Log.d(TAG,"bootanim:shut nomp3");
  56. }
  57. }

然后SystemProperties.set("ctl.start", "banim_shutmp3");到底会开启什么服务呢?通过文件frameworks/base/cmds/bootanimation/bootanim.rc可以看到如下代码:

  1. service bootanim /system/bin/bootanimation
  2. class core
  3. user graphics
  4. group graphics audio
  5. disabled
  6. oneshot
  7. # MTK add
  8. service banim_shutmp3 /system/bin/bootanimation shut mp3
  9. class core
  10. user graphics
  11. group graphics audio
  12. disabled
  13. oneshot
  14. service banim_shutnomp3 /system/bin/bootanimation shut nomp3
  15. class core
  16. user graphics
  17. group graphics audio
  18. disabled
  19. oneshot

所以会开启banim_shutmp3这个服务器,该服务的可执行文件为/system/bin/bootanimation,这个服务就是播放关机动画的服务了,该服务的实现代码在frameworks/base/cmds/bootanimation/目录下。
首先查看下frameworks/base/cmds/bootanimation/bootanimation_main.cpp具体代码:

  1. int main(int argc,char** argv)
  2. {
  3. setpriority(PRIO_PROCESS,0, ANDROID_PRIORITY_DISPLAY);
  4. char value[PROPERTY_VALUE_MAX];
  5. /*获得debug.sf.nobootanimation的值 存入value属性中 如果为1 不会进入下面的if中即不会播放动画*/
  6. property_get("debug.sf.nobootanimation", value,"0");
  7. int noBootAnimation = atoi(value);
  8. ALOGI_IF(noBootAnimation,"boot animation disabled");
  9. if(!noBootAnimation){
  10. sp<ProcessState> proc(ProcessState::self());
  11. ProcessState::self()->startThreadPool();
  12. /* M: use our construction method, because we support shut down animation @{
  13. // create the boot animation object
  14. sp<BootAnimation> boot = new BootAnimation();
  15. @} */
  16. bool setBoot =true;
  17. bool setRotated =false;
  18. bool sePaly =true;
  19. /*因为传入了2个参数shut和mp3 */
  20. if(argc >1){
  21. if(!strcmp(argv[1],"shut"))
  22. /*会进入该逻辑*/
  23. setBoot =false;
  24. }
  25. if(argc >2){
  26. if(!strcmp(argv[2],"nomp3"))
  27. sePaly =false;
  28. }
  29. if(argc >3){
  30. if(!strcmp(argv[3],"rotate"))
  31. setRotated =true;
  32. }
  33. ALOGD("[BootAnimation %s %d]setBoot=%d,sePaly=%d,setRotated=%d",__FUNCTION__,__LINE__,setBoot,sePaly,setRotated);
  34. char volume[PROPERTY_VALUE_MAX];
  35. intLoopCounter=60;
  36. int nVolume =-1;
  37. while(LoopCounter-->0&&(nVolume ==-1)){
  38. property_get("persist.sys.mute.state", volume,"-1");
  39. nVolume = atoi(volume);
  40. ALOGD("BootAnimation wait for sample ready, sleep 100ms");
  41. usleep(100*1000);
  42. }
  43. ALOGD("[BootAnimation %s %d]nVolume=%d",__FUNCTION__,__LINE__,nVolume);
  44. if(nVolume ==0|| nVolume ==1){
  45. sePaly =false;
  46. }
  47. ALOGD("before new BootAnimation...");
  48. ALOGD("[BootAnimation %s %d]before new BootAnimation...",__FUNCTION__,__LINE__);
  49. /*创建一个BootAnimation对象 其中 参数值setBoot=0,sePaly=1,setRotated=0*/
  50. sp<BootAnimation> boot =newBootAnimation(setBoot,sePaly,setRotated);
  51. ALOGD("joinThreadPool...");
  52. ALOGD("[BootAnimation %s %d]before joinThreadPool...",__FUNCTION__,__LINE__);
  53. IPCThreadState::self()->joinThreadPool();
  54. }
  55. ALOGD("[BootAnimation %s %s %d]end",__FILE__,__FUNCTION__,__LINE__);
  56. return0;
  57. }

然后看看BootAnimation类,该类的声明如下:

  1. classBootAnimation:publicThread,publicIBinder::DeathRecipient
  2. {
  3. public:
  4. BootAnimation();
  5. BootAnimation(bool bSetBootOrShutDown,bool bSetPlayMP3,bool bSetRotated);
  6. virtual~BootAnimation();
  7. void setBootVideoPlayState(int playState);
  8. sp<SurfaceComposerClient> session()const;
  9. private:
  10. virtualbool threadLoop();
  11. virtualstatus_t readyToRun();
  12. virtualvoid onFirstRef();
  13. virtualvoid binderDied(const wp<IBinder>& who);
  14. structTexture{
  15. GLint w;
  16. GLint h;
  17. GLuint name;
  18. };
  19. structAnimation{
  20. structFrame{
  21. String8 name;
  22. FileMap* map;
  23. String8 fullPath;
  24. mutableGLuint tid;
  25. booloperator<(constFrame& rhs)const{
  26. return name < rhs.name;
  27. }
  28. };
  29. structPart{
  30. int count;// The number of times this part should repeat, 0 for infinite
  31. int pause;// The number of frames to pause for at the end of this part
  32. int clockPosY;// The y position of the clock, in pixels, from the bottom of the
  33. // display (the clock is centred horizontally). -1 to disable the clock
  34. String8 path;
  35. SortedVector<Frame> frames;
  36. bool playUntilComplete;
  37. float backgroundColor[3];
  38. FileMap* audioFile;
  39. Animation* animation;
  40. };
  41. int fps;
  42. int width;
  43. int height;
  44. Vector<Part> parts;
  45. String8 audioConf;
  46. String8 fileName;
  47. ZipFileRO* zip;
  48. };
  49. status_t initTexture(Texture* texture,AssetManager& asset,constchar* name);
  50. status_t initTexture(constAnimation::Frame& frame);
  51. bool android();
  52. bool movie();
  53. status_t initTexture(constSkBitmap* bitmap);
  54. bool gifMovie();
  55. void drawTime(constTexture& clockTex,constint yPos);
  56. Animation* loadAnimation(constString8&);
  57. bool playAnimation(constAnimation&);
  58. void releaseAnimation(Animation*)const;
  59. bool parseAnimationDesc(Animation&);
  60. bool preloadZip(Animation&animation);
  61. void checkExit();
  62. sp<SurfaceComposerClient> mSession;
  63. sp<AudioPlayer> mAudioPlayer;
  64. AssetManager mAssets;
  65. Texture mAndroid[2];
  66. Texture mClock;
  67. int mWidth;
  68. int mHeight;
  69. EGLDisplay mDisplay;
  70. EGLDisplay mContext;
  71. EGLDisplay mSurface;
  72. sp<SurfaceControl> mFlingerSurfaceControl;
  73. sp<Surface> mFlingerSurface;
  74. bool mClockEnabled;
  75. String8 mZipFileName;
  76. SortedVector<String8> mLoadedFiles;
  77. ZipFileRO*mZip;
  78. status_t initTexture(constchar*EntryName);
  79. void initBootanimationZip(char* naimationname);
  80. void initShutanimationZip(char* naimationname);
  81. constchar* initAudioPath();
  82. bool ETC1movie();
  83. void initShader();
  84. GLuint buildShader(constchar* source,GLenum shaderType);
  85. GLuint buildProgram (constchar* vertexShaderSource,constchar* fragmentShaderSource);
  86. bool bBootOrShutDown;
  87. bool bShutRotate;
  88. bool bPlayMP3;
  89. GLuint mProgram;
  90. GLint mAttribPosition;
  91. GLint mAttribTexCoord;
  92. GLint mUniformTexture;
  93. bool bETC1Movie;
  94. int mBootVideoPlayType;
  95. int mBootVideoPlayState;
  96. SkMovie* mSkMovie;
  97. Texture mJRD;
  98. int mMcc;
  99. int mMnc;
  100. int mSpn;
  101. bool isTelefonica;
  102. char mMccMnc[PROPERTY_VALUE_MAX];
  103. char mOperator[PROPERTY_VALUE_MAX];
  104. bool mEnableSsv;
  105. bool mIsValidMccMnc;
  106. char mSsvAnimationPath[PROPERTY_VALUE_MAX];
  107. void getSsvAnimationPath(char* ssvAnimationPath,constchar* mccmnc,constchar* animationName);
  108. void initSsv();
  109. };

3. solution

通过查看关机log,发现电量值为0的时候,方法shutdownIfNoPowerLocked()通过binder接受到从底层传入的电池电量值并不为0,即条件判断if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY))为false。所以后面的播放关机动画的逻辑是不会走的,而电池的实际电量值已经为0了,所以才会没有播放关机动画就黑屏关机。
既然找到了问题,所以查看为什么在电池电量值为0的时候,上层收到mBatteryProps.batteryLevel属性并不为0呢。在文件battery_common_fg_20.c中函数mt_battery_update_EM实现如下:

  1. staticvoid mt_battery_update_EM(struct battery_data *bat_data)
  2. {
  3. if((BMT_status.UI_SOC2 <=0)&&(BMT_status.bat_vol >= SYSTEM_OFF_VOLTAGE ))
  4. bat_data->BAT_CAPACITY =1;
  5. else
  6. bat_data->BAT_CAPACITY = BMT_status.UI_SOC2;
  7. bat_data->BAT_TemperatureR = BMT_status.temperatureR;/* API */
  8. bat_data->BAT_TempBattVoltage = BMT_status.temperatureV;/* API */
  9. bat_data->BAT_InstatVolt = BMT_status.bat_vol;/* VBAT */
  10. bat_data->BAT_BatteryAverageCurrent = BMT_status.ICharging;
  11. bat_data->BAT_BatterySenseVoltage = BMT_status.bat_vol;
  12. bat_data->BAT_ISenseVoltage = BMT_status.Vsense;/* API */
  13. bat_data->BAT_ChargerVoltage = BMT_status.charger_vol;
  14. bat_data->BAT_batt_id = BMT_status.id_vol;
  15. /* Dual battery */
  16. bat_data->status_smb = g_status_smb;
  17. bat_data->capacity_smb = g_capacity_smb;
  18. bat_data->present_smb = g_present_smb;
  19. battery_log(BAT_LOG_FULL,"status_smb = %d, capacity_smb = %d, present_smb = %d\n",
  20. bat_data->status_smb, bat_data->capacity_smb, bat_data->present_smb);
  21. if((BMT_status.UI_SOC2 ==100)&&(BMT_status.charger_exist == KAL_TRUE)
  22. &&(BMT_status.bat_charging_state != CHR_ERROR))
  23. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_FULL;
  24. #ifdef CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION
  25. if(bat_data->BAT_CAPACITY <=0)
  26. bat_data->BAT_CAPACITY =1;
  27. battery_log(BAT_LOG_CRTI,
  28. "BAT_CAPACITY=1, due to define CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION\r\n");
  29. #endif
  30. }

在该函数中就是将电池驱动的信息写入对应节点的操作,属性BMT_status.UI_SOC2就是对应电池的实际电量值。在该if条件语句中,首先判断实际电量值是否低于或等于0,如果是ture就继续判断电池电压值BMT_status.bat_vol是否高于设定关机电压,如果电量值为0但是电压值还是过高(高于SYSTEM_OFF_VOLTAGE),会将bat_data->BAT_CAPACITY设定为1,即不进行关机操作,会让实际电压值继续降一段时间。等到同时满足BMT_status.UI_SOC2 =0而且电压值BMT_status.bat_vol低于设定的关机电压时,进入else逻辑中,即让属性bat_data->BAT_CAPACITY = BMT_status.UI_SOC2;即bat_data->BAT_CAPACITY置为0,然后将bat_data->BAT_CAPACITY值写入节点/sys/class/power_supply/battery/capacity。然后BatteryMonitor.cpp会读取该节点,然后将读到的电量值props.batteryLevel传入上层。
导致本问题出现的原因是设置的关机电压值BMT_status.bat_vol过低,在达到低电量关机条件的时候BMT_status.UI_SOC2 <=0)&&(BMT_status.bat_vol >= SYSTEM_OFF_VOLTAGE依然为true,所以传到上层的bat_data->BAT_CAPACITY不为0,无播放关机动画。
最后需要修改battery_common.h文件中的宏定义#define SYSTEM_OFF_VOLTAGE,把它的值调整的高一点即可,具体修改到多少,可根据具体情况而定。

4. 总结

这个问题解决,首先需要了解电池电量值的读取、传递的逻辑,手机中的电量值会通过底层电池驱动把电池信息写入对应的节点中,然后通过BatteryMonitor.cpp来读取相关信息,然后通过binder机制将电池信息不间断的传到上层,然后上层通过获得的电池信息值来做出相关操作,如电量低时通过广播来发送警告,如果电池电量值非常低时,进行关机操作。同时在关机操作前,还会开机线程来对系统中的所有服务进行关闭操作。至于关机动画则是通过开启一个播放关机动画的服务来处理。通过关机log来定位问题,发现在电量值为0的时候底层通过binder传入到上层的电量值不是0,所以不会进入上层播放关机动画的逻辑中来,而底层检测到电量低,则直接黑屏关机处理了。

No shutdown animation in the electricity display only 1%

标签:services   分支   data-   boolean   gty   变量   错误处理   sage   toc   

原文地址:http://www.cnblogs.com/sayhellowen/p/625e81c3b1dde2d1425eb6c7d739efa7.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!