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    ```gn
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    ```
59
605. 参考[默认关机充电动画的配置文件夹中的animation.json](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/animation.json)编写定制的animation.json。包含定制后的关机充电动画配置如下:
61
62    ```json
63    {
64        "animation": {
65            "components": [
66                {
67                    "type": "UIImageView",
68                    "id": "Charging_Animation_Image",
69                    "x": 200,
70                    "y": 480,
71                    "w": 400,
72                    "h": 400,
73                    "resPath": "/system/etc/charger/resources/",
74                    "imgCnt": 62,
75                    "updInterval": 60,
76                    "filePrefix": "loop"
77                },
78                {
79                    "type": "UILabel",
80                    "id": "Charging_Percent_Label",
81                    "text": "",
82                    "x": 326,
83                    "y": 616,
84                    "w": 68,
85                    "h": 48,
86                    "fontSize": 32,
87                    "fontColor": "#ffffffe6",
88                    "bgColor": "#00000000"
89                    "align": "center"
90                }
91            ]
92        },
93        "lackpowerChargingPrompt": {
94            "components": [
95                {
96                    "type": "UILabel",
97                    "id": "LackPower_Charging_Label",
98                    "text": "电池电量低",
99                    "x": 229,
100                    "y": 1037,
101                    "w": 250,
102                    "h": 45,
103                    "fontSize": 42,
104                    "fontColor": "#ff0000ff",
105                    "bgColor": "#00000000",
106                    "align": "center"
107                }
108            ]
109        },
110        "lackpowerNotChargingPrompt": {
111            "components": [
112                {
113                    "type": "UILabel",
114                    "id": "LackPower_Not_Charging_Label",
115                    "text": "电池电量低,请连接电源",
116                    "x": 110,
117                    "y": 1037,
118                    "w": 500,
119                    "h": 45,
120                    "fontSize": 42,
121                    "fontColor": "#ff0000ff",
122                    "bgColor": "#00000000",
123                    "align": "center"
124                }
125            ]
126        }
127    }
128    ```
129
130    **表1** 关机充电动画配置说明
131    | 节点名称 | 作用 |
132    | -------- | -------- |
133    | animation | 动画配置信息 |
134    | lackpowerChargingPrompt| 低电量充电状态提示配置信息 |
135    | lackpowerNotChargingPrompt | 低电量未充电状态提示配置信息 |
136    | components | 组件集 |
137    | type | 组件类型 |
138    | id | 组件ID |
139    | text | 组件的文本内容 |
140    | x | 组件的X轴坐标 |
141    | y | 组件的Y轴坐标 |
142    | w | 组件的宽度(像素点) |
143    | h | 组件的高度(像素点) |
144    | fontSize | 文本的字体大小 |
145    | fontColor | 文本的字体颜色 |
146    | align | 文本对齐方式 |
147    | imgCnt | 图片数量 |
148    | updInterval | 图片更新的时间间隔(毫秒) |
149    | filePrefix | 图片文件名的前缀 |
150    | resPath | 组件的资源文件路径 |
151
152
153
1546. 参考[默认关机充电动画的配置文件夹中的BUILD.gn](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/BUILD.gn)编写BUILD.gn文件,将animation.json打包到`/vendor/etc/charger`目录下,配置如下:
155
156    ```gn
157    import("//build/ohos.gni")
158
159    ohos_prebuilt_etc("animation_config") {
160        source = "animation.json"             #引用build/ohos.gni
161        relative_install_dir = "animation/resources"
162        install_images = [ chipset_base_dir ] #安装到vendor目录下的必要配置
163        part_name = "product_rk3568"          #part_name为product_rk3568,以实现后续编译
164    }
165    ```
166
1677. 将编译目标添加到`/vendor/hihope/rk3568`目录下[ohos.build](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/ohos.build)的"module_list"中,例如:
168
169    ```json
170    {
171    "parts": {
172        "product_rk3568": {
173        "module_list": [
174            "//vendor/hihope/rk3568/default_app_config:default_app_config",
175            "//vendor/hihope/rk3568/image_conf:custom_image_conf",
176            "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
177            "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
178            "//vendor/hihope/rk3568/etc:product_etc_conf",
179            "//vendor/hihope/rk3568/battery/profile:battery_config",
180            "//vendor/hihope/rk3568/animation/profile:animation_config", #添加animation_config的编译
181            "//vendor/hihope/rk3568/animation/resource/resources_config" #添加图片资源的编译
182        ]
183        }
184    },
185    "subsystem": "product_hihope"
186    }
187    ```
188    “//vendor/hihope/rk3568/animation/”为文件夹路径,“profile”为创建的文件夹名字,“animation_config”为编译目标。
189
1908. 参考《[快速入门](../quick-start/quickstart-overview.md)》编译定制版本,编译命令如下:
191
192    ```shell
193    ./build.sh --product-name rk3568 --ccache
194    ```
195
1969. 将定制版本烧录到DAYU200开发板中。
197
198### 调测验证
1991. 修改代码配置:
200    修改代码路径:base/startup/init/services/init/init_config.c
201    修改函数:ReadConfig
202    修改前
203    ```c
204    void ReadConfig(void)
205    {
206        // parse cfg
207        char buffer[32] = {0}; // 32 reason max leb
208        uint32_t len = sizeof(buffer);
209        SystemReadParam("ohos.boot.mode", buffer, &len);
210        INIT_LOGI("ohos.boot.mode %s", buffer);
211        if (strcmp(buffer, "charger_mode") == 0) {
212            // 产品化定制执行此分支流程
213            ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
214            ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL);
215            ParseInitCfgByPriority();
216        } else if (strcmp(buffer, "charger") == 0) {
217            ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
218            ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL);
219        } else if (InUpdaterMode() == 0) {
220            ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
221            ParseInitCfgByPriority();
222        } else {
223            ReadFileInDir("/etc", ".cfg", ParseInitCfg, NULL);
224        }
225    }
226    ```
227    修改后
228    ```c
229    void ReadConfig(void)
230    {
231        // parse cfg
232        char buffer[32] = {0}; // 32 reason max leb
233        uint32_t len = sizeof(buffer);
234        SystemReadParam("ohos.boot.mode", buffer, &len);
235        INIT_LOGI("ohos.boot.mode %s", buffer);
236        ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
237        ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL);
238        ParseInitCfgByPriority();
239    }
240    ```
241
2422. 使用hdc执行如下命令,使开发板进入关机充电状态。
243    ```shell
244    hdc shell
245    reboot charge
246    ```
247
248    ![animation_initial_power](figures/animation_initial_power.jpg)
249
2503. 进入电池电量的节点路径(以当前DAYU开发版路径为例)。
251    ```shell
252    cd /data/service/el0/battery/battery
253    ```
254
2554. 修改电量数值,并观察充电动画数值变化。
256    ```shell
257    cat capacity
258    ```
259    修改当前电量为3
260    ```shell
261    echo 3 > capacity
262    ```
263    ![animation_charing_power](figures/animation_charing_power.jpg)
264
2655. 切为Not charging状态,直接进入关机状态。
266    ```shell
267    echo Not charging > status
268    ```
269
2706. 在极低电量时(此处默认1%)修改充电状态,不同的充电状态对应显示动画或关机。
271
272    1. 进入电池电量的节点路径。
273    ```shell
274    cd /data/service/el0/battery/battery
275    cat capacity
276    ```
277    2. 修改当前电量为1。
278    ```shell
279    echo 1 > capacity
280    ```
281    3. 查看当前状态。
282    ```shell
283    cat status
284    ```
285    当前状态为Charging
286
287    ![animation_low_power](figures/animation_low_power.jpg)
288
289    4. 改变当前状态,为未连接电源状态。
290    ```shell
291    echo Not charging > status
292    ```
293    ![animation_low_power2](figures/animation_low_power2.jpg)
294
2957. 以下对关机动画的图片进行可定制化的测试修改,步骤与上方相同,只是图片作出替换。
296
297    1. 初始状态。
298
299    ![animation_charging_power2](figures/animation_charging_power2.jpg)
300
301    2. 3%电量状态。
302
303    ![animation_initial_power2](figures/animation_initial_power2.jpg)
304
305    3. 1%电量,充电状态。
306
307    ![animation_low_power](figures/animation_low_power.jpg)
308
309    4. 1%电量,未充电状态。
310
311    ![animation_low_power2](figures/animation_low_power2.jpg)
312
313    5. 当电量高于1%,切为Not charging状态
314    ```shell
315    echo 3 > capacity
316    echo Not charging > status
317    ```
318
319
320
321## 参考
322开发过程中可参考的配置文件路径:[默认关机充电动画配置源码路径](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/charger/sa_profile/animation.json)
323
324默认配置:
325```json
326{
327        "animation": {
328            "components": [
329                {
330                    "type": "UIImageView",
331                    "id": "Charging_Animation_Image",
332                    "x": 180,
333                    "y": 410,
334                    "w": 400,
335                    "h": 400,
336                    "resPath": "/system/etc/charger/resources/",
337                    "imgCnt": 62,
338                    "updInterval": 60,
339                    "filePrefix": "loop"
340                },
341                {
342                    "type": "UILabel",
343                    "id": "Charging_Percent_Label",
344                    "text": "",
345                    "x": 365,
346                    "y": 580,
347                    "w": 65,
348                    "h": 43,
349                    "fontSize": 32,
350                    "fontColor": "#ffffffe6",
351                    "bgColor": "#00000000",
352                    "align": "center"
353                }
354            ],
355        },
356        "lackpowerChargingPrompt": {
357            "components": [
358                {
359                    "type": "UILabel",
360                    "id": "LackPower_Charging_Label",
361                    "text": "电池电量低",
362                    "x": 229,
363                    "y": 1037,
364                    "w": 250,
365                    "h": 45,
366                    "fontSize": 42,
367                    "fontColor": "#ff0000ff",
368                    "bgColor": "#00000000",
369                    "align": "center"
370                }
371            ]
372        },
373        "lackpowerNotChargingPrompt": {
374            "components": [
375                {
376                    "type": "UILabel",
377                    "id": "LackPower_Not_Charging_Label",
378                    "text": "电池电量低,请连接电源",
379                    "x": 110,
380                    "y": 1037,
381                    "w": 500,
382                    "h": 45,
383                    "fontSize": 42,
384                    "fontColor": "#ff0000ff",
385                    "bgColor": "#00000000",
386                    "align": "center"
387                }
388            ]
389        }
390    }
391```
392
393打包路径:/system/etc/charger/resource
394