1# 关机充电动画开发指导 2 3## 概述 4 5### 简介 6 7OpenHarmony默认提供了关机充电动画的特性,展示关机状态下的充电电量、充电动画等信息。由于定制厂商希望定制符合自身规则和特点的显示样式,希望根据产品的设计规格来定制此特性。为此OpenHarmony提供了关机充电动画的定制方式,产品定制开发者可根据产品的设计规格来定制此特性。 8 9### 约束与限制 10 11配置策略: 12产品定制的配置路径,需要根据[配置策略](https://gitee.com/openharmony/customization_config_policy)决定。本开发指导中的定制路径以/vendor进行举例,请开发者根据具体的产品配置策略,修改定制路径。 13 14## 开发指导 15 16### 搭建环境 17 18设备要求: 19 20标准系统开发板,如DAYU200/Hi3516DV300开源套件。 21 22环境要求: 23 24Linux调测环境,相关要求和配置可参考《[快速入门](../quick-start/quickstart-overview.md)》。 25 26### 开发步骤 27 28本文以[DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568)为例介绍关机充电动画的定制方法。 29 301. 在产品目录[(/vendor/hihope/rk3568)](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568)下创建animation文件夹。 31 322. 参考[默认关机充电动画配置文件夹](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/charger/sa_profile)创建目标文件夹,并安装到`//vendor/hihope/rk3568/animation`目录下,文件格式如下: 33 34 ```text 35 profile 36 ├── BUILD.gn 37 ├── animation.json 38 ``` 39 403. 参考[关机充电动画图片资源](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/charger/resources)创建resources文件夹放置构成动画的图片文件,并安装到`//vendor/hihope/rk3568/animation`目录下,文件格式如下: 41 42 ```text 43 animation 44 ├── resources 45 ├── profile 46 ``` 47 484. 参考[关机充电动画图片资源中的BUILD.gn](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/resources/BUILD.gn)编写BUILD.gn文件,放入`//vendor/hihope/rk3568/animation/resource`目录下,例如: 49 ```shell 50 import("//build/ohos.gni") 51 52 ohos_prebuilt_etc("resources_config0") { 53 source = "loop00000.png" #图片资源 54 relative_install_dir = "poweroff_charger_animation/resources" 55 install_images = [ chipset_base_dir ] #安装到vendor目录下的必要配置 56 part_name = "product_rk3568" 57 } 58 595. 参考[默认关机充电动画的配置文件夹中的animation.json](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/animation.json)编写定制的animation.json。包含定制后的关机充电动画配置如下: 60 61 ```json 62 { 63 "id": "Charger", 64 "screenWidth": 720, 65 "screenHeight": 1280, 66 "dir": "/vendor/etc/charger/resources", 67 "bgColor": "#000000ff", 68 "subpages": [ 69 { 70 "id": "Animation", 71 "coms": [ 72 "Charging_Animation_Image", 73 "Charging_Percent_Label" 74 ], 75 "bgColor": "#000000ff" 76 }, 77 { 78 "id": "Lackpower_Charging_Prompt", 79 "coms": [ 80 "LackPower_Charging_Label" 81 ], 82 "bgColor": "#000000ff" 83 }, 84 { 85 "id": "Lackpower_Not_Charging_Prompt", 86 "coms": [ 87 "LackPower_Not_Charging_Label" 88 ], 89 "bgColor": "#000000ff" 90 } 91 ], 92 "default": { 93 "Common": { 94 "visible": false 95 }, 96 "UILabel": { 97 "bgColor": "#00000000", 98 "fontColor": "#ffffffe6", 99 "align": "center" 100 }, 101 "UIImageView": { 102 "imgCnt": 1, 103 "updInterval": 0, 104 "filePrefix": "" 105 } 106 }, 107 "coms": [ 108 { 109 "type": "UIImageView", 110 "id": "Charging_Animation_Image", 111 "x": 180, 112 "y": 410, 113 "w": 400, 114 "h": 400, 115 "resPath": "/vendor/etc/charger/resources/", 116 "imgCnt": 62, 117 "updInterval": 60, 118 "filePrefix": "loop" 119 }, 120 { 121 "type": "UILabel", 122 "id": "Charging_Percent_Label", 123 "text": "", 124 "x": 365, 125 "y": 580, 126 "w": 65, 127 "h": 43, 128 "fontSize": 32 129 }, 130 { 131 "type": "UILabel", 132 "id": "LackPower_Charging_Label", 133 "text": "电池电量低", 134 "x": 229, 135 "y": 1037, 136 "w": 250, 137 "h": 45, 138 "fontSize": 42, 139 "fontColor": "#ff0000ff" 140 }, 141 { 142 "type": "UILabel", 143 "id": "LackPower_Not_Charging_Label", 144 "text": "电池电量低,请连接电源", 145 "x": 110, 146 "y": 1037, 147 "w": 500, 148 "h": 45, 149 "fontSize": 42, 150 "fontColor": "#ff0000ff" 151 } 152 ] 153 } 154 ``` 155 156 **表1** 关机充电动画配置说明 157 | 节点名称 | 作用 | 158 | -------- | -------- | 159 | id | 充电界面的唯一标识符 | 160 | screenWidth | 充电界面的屏幕宽度(像素点) | 161 | screenHeight | 充电界面的屏幕高度(像素点) | 162 | dir | 资源路径 | 163 | bgColor | 充电界面的背景颜色 | 164 | subpages | 充电界面的子页面 | 165 | coms | 充电界面的所有组件 | 166 | visible | 元素是否可见 | 167 | text | 组件的文本内容 | 168 | x | 组件的X轴坐标 | 169 | y | 组件的Y轴坐标 | 170 | w | 组件的宽度(像素点) | 171 | h | 组件的高度(像素点) | 172 | fontSize | 组件的字体大小 | 173 | fontColor | 文本的字体颜色 | 174 | align | 文本对齐方式 | 175 | imgCnt | 图片数量 | 176 | updInterval | 图片更新的时间间隔(毫秒) | 177 | filePrefix | 图片文件名的前缀 | 178 | type | 组件的类型 | 179 | resPath | 组件的资源文件路径 | 180 181 182 1836. 参考[默认关机充电动画的配置文件夹中的BUILD.gn](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/BUILD.gn)编写BUILD.gn文件,将animation.json打包到`/vendor/etc/charger`目录下,配置如下: 184 185 ```shell 186 import("//build/ohos.gni") 187 188 ohos_prebuilt_etc("animation_config") { 189 source = "animation.json" #引用build/ohos.gni 190 relative_install_dir = "animation/resources" 191 install_images = [ chipset_base_dir ] #安装到vendor目录下的必要配置 192 part_name = "product_rk3568" #part_name为product_rk3568,以实现后续编译 193 } 194 ``` 195 1967. 将编译目标添加到`/vendor/hihope/rk3568`目录下[ohos.build](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/ohos.build)的"module_list"中,例如: 197 198 ```json 199 { 200 "parts": { 201 "product_rk3568": { 202 "module_list": [ 203 "//vendor/hihope/rk3568/default_app_config:default_app_config", 204 "//vendor/hihope/rk3568/image_conf:custom_image_conf", 205 "//vendor/hihope/rk3568/preinstall-config:preinstall-config", 206 "//vendor/hihope/rk3568/resourceschedule:resourceschedule", 207 "//vendor/hihope/rk3568/etc:product_etc_conf", 208 "//vendor/hihope/rk3568/battery/profile:battery_config", 209 "//vendor/hihope/rk3568/animation/profile:animation_config", #添加animation_config的编译 210 "//vendor/hihope/rk3568/animation/resource/resources_config" #添加图片资源的编译 211 ] 212 } 213 }, 214 "subsystem": "product_hihope" 215 } 216 ``` 217 “//vendor/hihope/rk3568/animation/”为文件夹路径,“profile”为创建的文件夹名字,“animation_config”为编译目标。 218 2198. 参考《[快速入门](../quick-start/quickstart-overview.md)》编译定制版本,编译命令如下: 220 221 ```shell 222 ./build.sh --product-name rk3568 --ccache 223 ``` 224 2259. 将定制版本烧录到DAYU200开发板中。 226 227### 调测验证 228 2291. 修改代码配置: 230 231 修改代码路径:base/startup/init/services/init/init_config.c 232 233 修改函数:ReadConfig 234 ``` 235 修改后: 236 void ReadConfig(void) 237 { 238 // parse cfg 239 char buffer[32] = {0}; // 32 reason max leb 240 uint32_t len = sizeof(buffer); 241 SystemReadParam("ohos.boot.reboot_reason", buffer, &len); 242 INIT_LOGV("ohos.boot.reboot_reason %s", buffer); 243 ParseInitCfg(INIT_CONFIGURATION_FILE, NULL); //新增代码 244 ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL); //新增代码 245 if (strcmp(buffer, "poweroff_charge") == 0) { 246 ParseInitCfg(INIT_CONFIGURATION_FILE, NULL); 247 ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL); 248 } else if (InUpdaterMode() == 0) { 249 ParseInitCfg(INIT_CONFIGURATION_FILE, NULL); 250 ParseInitCfgByPriority(); 251 } else { 252 ReadFileInDir("/etc", ".cfg", ParseInitCfg, NULL); 253 } 254 } 255 ``` 256 2572. 使用hdc执行如下命令,使开发板进入关机充电状态。 258 ``` 259 hdc shell 260 reboot charger 261 ``` 262 263  264 2653. 进入电池电量的节点路径(以当前DAYU开发版路径为例)。 266 ``` 267 cd /data/service/el0/battery/battery 268 ``` 269 2704. 修改电量数值,并观察充电动画数值变化。 271 ``` 272 cat capacity 273 ``` 274 修改当前电量为3 275 ``` 276 echo 3 > capacity 277 ``` 278  279 2805. 在极低电量时(此处默认1%)修改充电状态,不同的充电状态对应显示动画或关机。 281 282 1. 进入电池电量的节点路径。 283 ``` 284 cd /data/service/el0/battery/battery 285 ``` 286 ``` 287 cat capacity 288 ``` 289 2. 修改当前电量为1。 290 ``` 291 echo 1 > capacity 292 ``` 293 3. 查看当前状态。 294 ``` 295 cat status 296 ``` 297 当前状态为Charging 298 299  300 301 4. 改变当前状态,为未连接电源状态。 302 ``` 303 echo Not charging > status 304 ``` 305  306 307 5. 切为Discharging状态,直接进入关机状态。 308 ``` 309 echo Discharging > status 310 ``` 311 3126. 以下对关机动画的图片进行可定制化的测试修改,步骤与上方相同,只是图片作出替换。 313 314 1. 初始状态。 315 316  317 318 2. 3%电量状态。 319 320  321 322 3. 1%电量,充电状态。 323 324  325 326 4. 1%电量,未充电状态。 327 328  329 330 5. 1%电量,Discharing状态。 331 332 设备进入关机状态 333 334 335 336## 参考 337开发过程中可参考的配置文件路径:[默认关机充电动画配置源码路径](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/animation.json) 338 339默认配置: 340```json 341 { 342 "id": "Charger", 343 "screenWidth": 720, 344 "screenHeight": 1280, 345 "dir": "/vendor/etc/charger/resources", 346 "bgColor": "#000000ff", 347 "subpages": [ 348 { 349 "id": "Animation", 350 "coms": [ 351 "Charging_Animation_Image", 352 "Charging_Percent_Label" 353 ], 354 "bgColor": "#000000ff" 355 }, 356 { 357 "id": "Lackpower_Charging_Prompt", 358 "coms": [ 359 "LackPower_Charging_Label" 360 ], 361 "bgColor": "#000000ff" 362 }, 363 { 364 "id": "Lackpower_Not_Charging_Prompt", 365 "coms": [ 366 "LackPower_Not_Charging_Label" 367 ], 368 "bgColor": "#000000ff" 369 } 370 ], 371 "default": { 372 "Common": { 373 "visible": false 374 }, 375 "UILabel": { 376 "bgColor": "#00000000", 377 "fontColor": "#ffffffe6", 378 "align": "center" 379 }, 380 "UIImageView": { 381 "imgCnt": 1, 382 "updInterval": 0, 383 "filePrefix": "" 384 } 385 }, 386 "coms": [ 387 { 388 "type": "UIImageView", 389 "id": "Charging_Animation_Image", 390 "x": 180, 391 "y": 410, 392 "w": 400, 393 "h": 400, 394 "resPath": "/vendor/etc/charger/resources/", 395 "imgCnt": 62, 396 "updInterval": 60, 397 "filePrefix": "loop" 398 }, 399 { 400 "type": "UILabel", 401 "id": "Charging_Percent_Label", 402 "text": "", 403 "x": 365, 404 "y": 580, 405 "w": 65, 406 "h": 43, 407 "fontSize": 32 408 }, 409 { 410 "type": "UILabel", 411 "id": "LackPower_Charging_Label", 412 "text": "电池电量低", 413 "x": 229, 414 "y": 1037, 415 "w": 250, 416 "h": 45, 417 "fontSize": 42, 418 "fontColor": "#ff0000ff" 419 }, 420 { 421 "type": "UILabel", 422 "id": "LackPower_Not_Charging_Label", 423 "text": "电池电量低,请连接电源", 424 "x": 110, 425 "y": 1037, 426 "w": 500, 427 "h": 45, 428 "fontSize": 42, 429 "fontColor": "#ff0000ff" 430 } 431 ] 432 } 433``` 434 435打包路径:/system/etc/charger/resource 436