diff options
-rw-r--r-- | healthd/Android.mk | 52 | ||||
-rw-r--r-- | healthd/AnimationParser.cpp | 141 | ||||
-rw-r--r-- | healthd/AnimationParser.h | 31 | ||||
-rw-r--r-- | healthd/animation.h | 73 | ||||
-rw-r--r-- | healthd/healthd_mode_charger.cpp | 347 | ||||
-rw-r--r-- | healthd/tests/Android.mk | 21 | ||||
-rw-r--r-- | healthd/tests/AnimationParser_test.cpp | 192 |
7 files changed, 756 insertions, 101 deletions
diff --git a/healthd/Android.mk b/healthd/Android.mk index a4469fc2f..fe65e19fc 100644 --- a/healthd/Android.mk +++ b/healthd/Android.mk | |||
@@ -19,12 +19,39 @@ LOCAL_STATIC_LIBRARIES := libutils | |||
19 | include $(BUILD_STATIC_LIBRARY) | 19 | include $(BUILD_STATIC_LIBRARY) |
20 | 20 | ||
21 | include $(CLEAR_VARS) | 21 | include $(CLEAR_VARS) |
22 | LOCAL_SRC_FILES := \ | ||
23 | healthd_mode_android.cpp \ | ||
24 | healthd_mode_charger.cpp \ | ||
25 | AnimationParser.cpp \ | ||
26 | BatteryPropertiesRegistrar.cpp \ | ||
27 | |||
28 | LOCAL_MODULE := libhealthd_internal | ||
29 | LOCAL_C_INCLUDES := bootable/recovery | ||
30 | LOCAL_EXPORT_C_INCLUDE_DIRS := \ | ||
31 | $(LOCAL_PATH) \ | ||
32 | $(LOCAL_PATH)/include \ | ||
33 | |||
34 | LOCAL_STATIC_LIBRARIES := \ | ||
35 | libbatterymonitor \ | ||
36 | libbatteryservice \ | ||
37 | libbinder \ | ||
38 | libminui \ | ||
39 | libpng \ | ||
40 | libz \ | ||
41 | libutils \ | ||
42 | libbase \ | ||
43 | libcutils \ | ||
44 | liblog \ | ||
45 | libm \ | ||
46 | libc \ | ||
47 | |||
48 | include $(BUILD_STATIC_LIBRARY) | ||
49 | |||
50 | |||
51 | include $(CLEAR_VARS) | ||
22 | 52 | ||
23 | LOCAL_SRC_FILES := \ | 53 | LOCAL_SRC_FILES := \ |
24 | healthd.cpp \ | 54 | healthd.cpp \ |
25 | healthd_mode_android.cpp \ | ||
26 | healthd_mode_charger.cpp \ | ||
27 | BatteryPropertiesRegistrar.cpp | ||
28 | 55 | ||
29 | LOCAL_MODULE := healthd | 56 | LOCAL_MODULE := healthd |
30 | LOCAL_MODULE_TAGS := optional | 57 | LOCAL_MODULE_TAGS := optional |
@@ -44,7 +71,20 @@ endif | |||
44 | 71 | ||
45 | LOCAL_C_INCLUDES := bootable/recovery | 72 | LOCAL_C_INCLUDES := bootable/recovery |
46 | 73 | ||
47 | LOCAL_STATIC_LIBRARIES := libbatterymonitor libbatteryservice libbinder libminui libpng libz libutils libcutils liblog libm libc | 74 | LOCAL_STATIC_LIBRARIES := \ |
75 | libhealthd_internal \ | ||
76 | libbatterymonitor \ | ||
77 | libbatteryservice \ | ||
78 | libbinder \ | ||
79 | libminui \ | ||
80 | libpng \ | ||
81 | libz \ | ||
82 | libutils \ | ||
83 | libbase \ | ||
84 | libcutils \ | ||
85 | liblog \ | ||
86 | libm \ | ||
87 | libc | ||
48 | 88 | ||
49 | ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true) | 89 | ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true) |
50 | LOCAL_STATIC_LIBRARIES += libsuspend | 90 | LOCAL_STATIC_LIBRARIES += libsuspend |
@@ -61,7 +101,7 @@ include $(BUILD_EXECUTABLE) | |||
61 | 101 | ||
62 | define _add-charger-image | 102 | define _add-charger-image |
63 | include $$(CLEAR_VARS) | 103 | include $$(CLEAR_VARS) |
64 | LOCAL_MODULE := system_core_charger_$(notdir $(1)) | 104 | LOCAL_MODULE := system_core_charger_res_images_$(notdir $(1)) |
65 | LOCAL_MODULE_STEM := $(notdir $(1)) | 105 | LOCAL_MODULE_STEM := $(notdir $(1)) |
66 | _img_modules += $$(LOCAL_MODULE) | 106 | _img_modules += $$(LOCAL_MODULE) |
67 | LOCAL_SRC_FILES := $1 | 107 | LOCAL_SRC_FILES := $1 |
diff --git a/healthd/AnimationParser.cpp b/healthd/AnimationParser.cpp new file mode 100644 index 000000000..864038b70 --- /dev/null +++ b/healthd/AnimationParser.cpp | |||
@@ -0,0 +1,141 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "AnimationParser.h" | ||
18 | |||
19 | #include <android-base/stringprintf.h> | ||
20 | #include <android-base/strings.h> | ||
21 | |||
22 | #include <cutils/klog.h> | ||
23 | |||
24 | #include "animation.h" | ||
25 | |||
26 | #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) | ||
27 | #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) | ||
28 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) | ||
29 | |||
30 | namespace android { | ||
31 | |||
32 | // Lines consisting of only whitespace or whitespace followed by '#' can be ignored. | ||
33 | bool can_ignore_line(const char* str) { | ||
34 | for (int i = 0; str[i] != '\0' && str[i] != '#'; i++) { | ||
35 | if (!isspace(str[i])) return false; | ||
36 | } | ||
37 | return true; | ||
38 | } | ||
39 | |||
40 | bool remove_prefix(const std::string& line, const char* prefix, const char** rest) { | ||
41 | const char* str = line.c_str(); | ||
42 | int start; | ||
43 | char c; | ||
44 | |||
45 | std::string format = base::StringPrintf(" %s%%n%%c", prefix); | ||
46 | if (sscanf(str, format.c_str(), &start, &c) != 1) { | ||
47 | return false; | ||
48 | } | ||
49 | |||
50 | *rest = &str[start]; | ||
51 | return true; | ||
52 | } | ||
53 | |||
54 | bool parse_text_field(const char* in, animation::text_field* field) { | ||
55 | int* x = &field->pos_x; | ||
56 | int* y = &field->pos_y; | ||
57 | int* r = &field->color_r; | ||
58 | int* g = &field->color_g; | ||
59 | int* b = &field->color_b; | ||
60 | int* a = &field->color_a; | ||
61 | |||
62 | int start = 0, end = 0; | ||
63 | |||
64 | if (sscanf(in, "c c %d %d %d %d %n%*s%n", r, g, b, a, &start, &end) == 4) { | ||
65 | *x = CENTER_VAL; | ||
66 | *y = CENTER_VAL; | ||
67 | } else if (sscanf(in, "c %d %d %d %d %d %n%*s%n", y, r, g, b, a, &start, &end) == 5) { | ||
68 | *x = CENTER_VAL; | ||
69 | } else if (sscanf(in, "%d c %d %d %d %d %n%*s%n", x, r, g, b, a, &start, &end) == 5) { | ||
70 | *y = CENTER_VAL; | ||
71 | } else if (sscanf(in, "%d %d %d %d %d %d %n%*s%n", x, y, r, g, b, a, &start, &end) != 6) { | ||
72 | return false; | ||
73 | } | ||
74 | |||
75 | if (end == 0) return false; | ||
76 | |||
77 | field->font_file.assign(&in[start], end - start); | ||
78 | |||
79 | return true; | ||
80 | } | ||
81 | |||
82 | bool parse_animation_desc(const std::string& content, animation* anim) { | ||
83 | static constexpr const char* animation_prefix = "animation: "; | ||
84 | static constexpr const char* fail_prefix = "fail: "; | ||
85 | static constexpr const char* clock_prefix = "clock_display: "; | ||
86 | static constexpr const char* percent_prefix = "percent_display: "; | ||
87 | static constexpr const char* frame_prefix = "frame: "; | ||
88 | |||
89 | std::vector<animation::frame> frames; | ||
90 | |||
91 | for (const auto& line : base::Split(content, "\n")) { | ||
92 | animation::frame frame; | ||
93 | const char* rest; | ||
94 | |||
95 | if (can_ignore_line(line.c_str())) { | ||
96 | continue; | ||
97 | } else if (remove_prefix(line, animation_prefix, &rest)) { | ||
98 | int start = 0, end = 0; | ||
99 | if (sscanf(rest, "%d %d %n%*s%n", &anim->num_cycles, &anim->first_frame_repeats, | ||
100 | &start, &end) != 2 || | ||
101 | end == 0) { | ||
102 | LOGE("Bad animation format: %s\n", line.c_str()); | ||
103 | return false; | ||
104 | } else { | ||
105 | anim->animation_file.assign(&rest[start], end - start); | ||
106 | } | ||
107 | } else if (remove_prefix(line, fail_prefix, &rest)) { | ||
108 | anim->fail_file.assign(rest); | ||
109 | } else if (remove_prefix(line, clock_prefix, &rest)) { | ||
110 | if (!parse_text_field(rest, &anim->text_clock)) { | ||
111 | LOGE("Bad clock_display format: %s\n", line.c_str()); | ||
112 | return false; | ||
113 | } | ||
114 | } else if (remove_prefix(line, percent_prefix, &rest)) { | ||
115 | if (!parse_text_field(rest, &anim->text_percent)) { | ||
116 | LOGE("Bad percent_display format: %s\n", line.c_str()); | ||
117 | return false; | ||
118 | } | ||
119 | } else if (sscanf(line.c_str(), " frame: %d %d %d", | ||
120 | &frame.disp_time, &frame.min_level, &frame.max_level) == 3) { | ||
121 | frames.push_back(std::move(frame)); | ||
122 | } else { | ||
123 | LOGE("Malformed animation description line: %s\n", line.c_str()); | ||
124 | return false; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | if (anim->animation_file.empty() || frames.empty()) { | ||
129 | LOGE("Bad animation description. Provide the 'animation: ' line and at least one 'frame: ' " | ||
130 | "line.\n"); | ||
131 | return false; | ||
132 | } | ||
133 | |||
134 | anim->num_frames = frames.size(); | ||
135 | anim->frames = new animation::frame[frames.size()]; | ||
136 | std::copy(frames.begin(), frames.end(), anim->frames); | ||
137 | |||
138 | return true; | ||
139 | } | ||
140 | |||
141 | } // namespace android | ||
diff --git a/healthd/AnimationParser.h b/healthd/AnimationParser.h new file mode 100644 index 000000000..bc0084518 --- /dev/null +++ b/healthd/AnimationParser.h | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #ifndef HEALTHD_ANIMATION_PARSER_H | ||
18 | #define HEALTHD_ANIMATION_PARSER_H | ||
19 | |||
20 | #include "animation.h" | ||
21 | |||
22 | namespace android { | ||
23 | |||
24 | bool parse_animation_desc(const std::string& content, animation* anim); | ||
25 | |||
26 | bool can_ignore_line(const char* str); | ||
27 | bool remove_prefix(const std::string& str, const char* prefix, const char** rest); | ||
28 | bool parse_text_field(const char* in, animation::text_field* field); | ||
29 | } // namespace android | ||
30 | |||
31 | #endif // HEALTHD_ANIMATION_PARSER_H | ||
diff --git a/healthd/animation.h b/healthd/animation.h new file mode 100644 index 000000000..562b68973 --- /dev/null +++ b/healthd/animation.h | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #ifndef HEALTHD_ANIMATION_H | ||
18 | #define HEALTHD_ANIMATION_H | ||
19 | |||
20 | #include <inttypes.h> | ||
21 | #include <string> | ||
22 | |||
23 | struct GRSurface; | ||
24 | struct GRFont; | ||
25 | |||
26 | namespace android { | ||
27 | |||
28 | #define CENTER_VAL INT_MAX | ||
29 | |||
30 | struct animation { | ||
31 | struct frame { | ||
32 | int disp_time; | ||
33 | int min_level; | ||
34 | int max_level; | ||
35 | |||
36 | GRSurface* surface; | ||
37 | }; | ||
38 | |||
39 | struct text_field { | ||
40 | std::string font_file; | ||
41 | int pos_x; | ||
42 | int pos_y; | ||
43 | int color_r; | ||
44 | int color_g; | ||
45 | int color_b; | ||
46 | int color_a; | ||
47 | |||
48 | GRFont* font; | ||
49 | }; | ||
50 | |||
51 | std::string animation_file; | ||
52 | std::string fail_file; | ||
53 | |||
54 | text_field text_clock; | ||
55 | text_field text_percent; | ||
56 | |||
57 | bool run; | ||
58 | |||
59 | frame* frames; | ||
60 | int cur_frame; | ||
61 | int num_frames; | ||
62 | int first_frame_repeats; // Number of times to repeat the first frame in the current cycle | ||
63 | |||
64 | int cur_cycle; | ||
65 | int num_cycles; // Number of cycles to complete before blanking the screen | ||
66 | |||
67 | int cur_level; // current battery level being animated (0-100) | ||
68 | int cur_status; // current battery status - see BatteryService.h for BATTERY_STATUS_* | ||
69 | }; | ||
70 | |||
71 | } | ||
72 | |||
73 | #endif // HEALTHD_ANIMATION_H | ||
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp index 66f927136..fb17f2da1 100644 --- a/healthd/healthd_mode_charger.cpp +++ b/healthd/healthd_mode_charger.cpp | |||
@@ -30,6 +30,9 @@ | |||
30 | #include <time.h> | 30 | #include <time.h> |
31 | #include <unistd.h> | 31 | #include <unistd.h> |
32 | 32 | ||
33 | #include <android-base/file.h> | ||
34 | #include <android-base/stringprintf.h> | ||
35 | |||
33 | #include <sys/socket.h> | 36 | #include <sys/socket.h> |
34 | #include <linux/netlink.h> | 37 | #include <linux/netlink.h> |
35 | 38 | ||
@@ -44,10 +47,14 @@ | |||
44 | #include <suspend/autosuspend.h> | 47 | #include <suspend/autosuspend.h> |
45 | #endif | 48 | #endif |
46 | 49 | ||
50 | #include "animation.h" | ||
51 | #include "AnimationParser.h" | ||
47 | #include "minui/minui.h" | 52 | #include "minui/minui.h" |
48 | 53 | ||
49 | #include <healthd/healthd.h> | 54 | #include <healthd/healthd.h> |
50 | 55 | ||
56 | using namespace android; | ||
57 | |||
51 | char *locale; | 58 | char *locale; |
52 | 59 | ||
53 | #ifndef max | 60 | #ifndef max |
@@ -67,8 +74,6 @@ char *locale; | |||
67 | #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) | 74 | #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) |
68 | #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) | 75 | #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) |
69 | 76 | ||
70 | #define BATTERY_FULL_THRESH 95 | ||
71 | |||
72 | #define LAST_KMSG_PATH "/proc/last_kmsg" | 77 | #define LAST_KMSG_PATH "/proc/last_kmsg" |
73 | #define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops" | 78 | #define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops" |
74 | #define LAST_KMSG_MAX_SZ (32 * 1024) | 79 | #define LAST_KMSG_MAX_SZ (32 * 1024) |
@@ -77,34 +82,14 @@ char *locale; | |||
77 | #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) | 82 | #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) |
78 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) | 83 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) |
79 | 84 | ||
85 | static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt"; | ||
86 | |||
80 | struct key_state { | 87 | struct key_state { |
81 | bool pending; | 88 | bool pending; |
82 | bool down; | 89 | bool down; |
83 | int64_t timestamp; | 90 | int64_t timestamp; |
84 | }; | 91 | }; |
85 | 92 | ||
86 | struct frame { | ||
87 | int disp_time; | ||
88 | int min_capacity; | ||
89 | bool level_only; | ||
90 | |||
91 | GRSurface* surface; | ||
92 | }; | ||
93 | |||
94 | struct animation { | ||
95 | bool run; | ||
96 | |||
97 | struct frame *frames; | ||
98 | int cur_frame; | ||
99 | int num_frames; | ||
100 | |||
101 | int cur_cycle; | ||
102 | int num_cycles; | ||
103 | |||
104 | /* current capacity being animated */ | ||
105 | int capacity; | ||
106 | }; | ||
107 | |||
108 | struct charger { | 93 | struct charger { |
109 | bool have_battery_state; | 94 | bool have_battery_state; |
110 | bool charger_connected; | 95 | bool charger_connected; |
@@ -119,54 +104,83 @@ struct charger { | |||
119 | int boot_min_cap; | 104 | int boot_min_cap; |
120 | }; | 105 | }; |
121 | 106 | ||
122 | static struct frame batt_anim_frames[] = { | 107 | static const struct animation BASE_ANIMATION = { |
108 | .text_clock = { | ||
109 | .pos_x = 0, | ||
110 | .pos_y = 0, | ||
111 | |||
112 | .color_r = 255, | ||
113 | .color_g = 255, | ||
114 | .color_b = 255, | ||
115 | .color_a = 255, | ||
116 | |||
117 | .font = nullptr, | ||
118 | }, | ||
119 | .text_percent = { | ||
120 | .pos_x = 0, | ||
121 | .pos_y = 0, | ||
122 | |||
123 | .color_r = 255, | ||
124 | .color_g = 255, | ||
125 | .color_b = 255, | ||
126 | .color_a = 255, | ||
127 | }, | ||
128 | |||
129 | .run = false, | ||
130 | |||
131 | .frames = nullptr, | ||
132 | .cur_frame = 0, | ||
133 | .num_frames = 0, | ||
134 | .first_frame_repeats = 2, | ||
135 | |||
136 | .cur_cycle = 0, | ||
137 | .num_cycles = 3, | ||
138 | |||
139 | .cur_level = 0, | ||
140 | .cur_status = BATTERY_STATUS_UNKNOWN, | ||
141 | }; | ||
142 | |||
143 | |||
144 | static struct animation::frame default_animation_frames[] = { | ||
123 | { | 145 | { |
124 | .disp_time = 750, | 146 | .disp_time = 750, |
125 | .min_capacity = 0, | 147 | .min_level = 0, |
126 | .level_only = false, | 148 | .max_level = 19, |
127 | .surface = NULL, | 149 | .surface = NULL, |
128 | }, | 150 | }, |
129 | { | 151 | { |
130 | .disp_time = 750, | 152 | .disp_time = 750, |
131 | .min_capacity = 20, | 153 | .min_level = 0, |
132 | .level_only = false, | 154 | .max_level = 39, |
133 | .surface = NULL, | 155 | .surface = NULL, |
134 | }, | 156 | }, |
135 | { | 157 | { |
136 | .disp_time = 750, | 158 | .disp_time = 750, |
137 | .min_capacity = 40, | 159 | .min_level = 0, |
138 | .level_only = false, | 160 | .max_level = 59, |
139 | .surface = NULL, | 161 | .surface = NULL, |
140 | }, | 162 | }, |
141 | { | 163 | { |
142 | .disp_time = 750, | 164 | .disp_time = 750, |
143 | .min_capacity = 60, | 165 | .min_level = 0, |
144 | .level_only = false, | 166 | .max_level = 79, |
145 | .surface = NULL, | 167 | .surface = NULL, |
146 | }, | 168 | }, |
147 | { | 169 | { |
148 | .disp_time = 750, | 170 | .disp_time = 750, |
149 | .min_capacity = 80, | 171 | .min_level = 80, |
150 | .level_only = true, | 172 | .max_level = 95, |
151 | .surface = NULL, | 173 | .surface = NULL, |
152 | }, | 174 | }, |
153 | { | 175 | { |
154 | .disp_time = 750, | 176 | .disp_time = 750, |
155 | .min_capacity = BATTERY_FULL_THRESH, | 177 | .min_level = 0, |
156 | .level_only = false, | 178 | .max_level = 100, |
157 | .surface = NULL, | 179 | .surface = NULL, |
158 | }, | 180 | }, |
159 | }; | 181 | }; |
160 | 182 | ||
161 | static struct animation battery_animation = { | 183 | static struct animation battery_animation = BASE_ANIMATION; |
162 | .run = false, | ||
163 | .frames = batt_anim_frames, | ||
164 | .cur_frame = 0, | ||
165 | .num_frames = ARRAY_SIZE(batt_anim_frames), | ||
166 | .cur_cycle = 0, | ||
167 | .num_cycles = 3, | ||
168 | .capacity = 0, | ||
169 | }; | ||
170 | 184 | ||
171 | static struct charger charger_state; | 185 | static struct charger charger_state; |
172 | static struct healthd_config *healthd_config; | 186 | static struct healthd_config *healthd_config; |
@@ -273,8 +287,79 @@ static void android_green(void) | |||
273 | gr_color(0xa4, 0xc6, 0x39, 255); | 287 | gr_color(0xa4, 0xc6, 0x39, 255); |
274 | } | 288 | } |
275 | 289 | ||
290 | // Negative x or y coordinates position the text away from the opposite edge that positive ones do. | ||
291 | void determine_xy(const animation::text_field& field, const int length, int* x, int* y) | ||
292 | { | ||
293 | *x = field.pos_x; | ||
294 | *y = field.pos_y; | ||
295 | |||
296 | int str_len_px = length * field.font->char_width; | ||
297 | if (field.pos_x == CENTER_VAL) { | ||
298 | *x = (gr_fb_width() - str_len_px) / 2; | ||
299 | } else if (field.pos_x >= 0) { | ||
300 | *x = field.pos_x; | ||
301 | } else { // position from max edge | ||
302 | *x = gr_fb_width() + field.pos_x - str_len_px; | ||
303 | } | ||
304 | |||
305 | if (field.pos_y == CENTER_VAL) { | ||
306 | *y = (gr_fb_height() - field.font->char_height) / 2; | ||
307 | } else if (field.pos_y >= 0) { | ||
308 | *y = field.pos_y; | ||
309 | } else { // position from max edge | ||
310 | *y = gr_fb_height() + field.pos_y - field.font->char_height; | ||
311 | } | ||
312 | } | ||
313 | |||
314 | static void draw_clock(const animation& anim) | ||
315 | { | ||
316 | static constexpr char CLOCK_FORMAT[] = "%H:%M"; | ||
317 | static constexpr int CLOCK_LENGTH = 6; | ||
318 | |||
319 | const animation::text_field& field = anim.text_clock; | ||
320 | |||
321 | if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) return; | ||
322 | |||
323 | time_t rawtime; | ||
324 | time(&rawtime); | ||
325 | struct tm* time_info = localtime(&rawtime); | ||
326 | |||
327 | char clock_str[CLOCK_LENGTH]; | ||
328 | size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info); | ||
329 | if (length != CLOCK_LENGTH - 1) { | ||
330 | LOGE("Could not format time\n"); | ||
331 | return; | ||
332 | } | ||
333 | |||
334 | int x, y; | ||
335 | determine_xy(field, length, &x, &y); | ||
336 | |||
337 | LOGV("drawing clock %s %d %d\n", clock_str, x, y); | ||
338 | gr_color(field.color_r, field.color_g, field.color_b, field.color_a); | ||
339 | gr_text(field.font, x, y, clock_str, false); | ||
340 | } | ||
341 | |||
342 | static void draw_percent(const animation& anim) | ||
343 | { | ||
344 | if (anim.cur_level <= 0 || anim.cur_status != BATTERY_STATUS_CHARGING) return; | ||
345 | |||
346 | const animation::text_field& field = anim.text_percent; | ||
347 | if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) { | ||
348 | return; | ||
349 | } | ||
350 | |||
351 | std::string str = base::StringPrintf("%d%%", anim.cur_level); | ||
352 | |||
353 | int x, y; | ||
354 | determine_xy(field, str.size(), &x, &y); | ||
355 | |||
356 | LOGV("drawing percent %s %d %d\n", str.c_str(), x, y); | ||
357 | gr_color(field.color_r, field.color_g, field.color_b, field.color_a); | ||
358 | gr_text(field.font, x, y, str.c_str(), false); | ||
359 | } | ||
360 | |||
276 | /* returns the last y-offset of where the surface ends */ | 361 | /* returns the last y-offset of where the surface ends */ |
277 | static int draw_surface_centered(struct charger* /*charger*/, GRSurface* surface) | 362 | static int draw_surface_centered(GRSurface* surface) |
278 | { | 363 | { |
279 | int w; | 364 | int w; |
280 | int h; | 365 | int h; |
@@ -295,7 +380,7 @@ static void draw_unknown(struct charger *charger) | |||
295 | { | 380 | { |
296 | int y; | 381 | int y; |
297 | if (charger->surf_unknown) { | 382 | if (charger->surf_unknown) { |
298 | draw_surface_centered(charger, charger->surf_unknown); | 383 | draw_surface_centered(charger->surf_unknown); |
299 | } else { | 384 | } else { |
300 | android_green(); | 385 | android_green(); |
301 | y = draw_text("Charging!", -1, -1); | 386 | y = draw_text("Charging!", -1, -1); |
@@ -303,17 +388,19 @@ static void draw_unknown(struct charger *charger) | |||
303 | } | 388 | } |
304 | } | 389 | } |
305 | 390 | ||
306 | static void draw_battery(struct charger *charger) | 391 | static void draw_battery(const struct charger* charger) |
307 | { | 392 | { |
308 | struct animation *batt_anim = charger->batt_anim; | 393 | const struct animation& anim = *charger->batt_anim; |
309 | struct frame *frame = &batt_anim->frames[batt_anim->cur_frame]; | 394 | const struct animation::frame& frame = anim.frames[anim.cur_frame]; |
310 | 395 | ||
311 | if (batt_anim->num_frames != 0) { | 396 | if (anim.num_frames != 0) { |
312 | draw_surface_centered(charger, frame->surface); | 397 | draw_surface_centered(frame.surface); |
313 | LOGV("drawing frame #%d min_cap=%d time=%d\n", | 398 | LOGV("drawing frame #%d min_cap=%d time=%d\n", |
314 | batt_anim->cur_frame, frame->min_capacity, | 399 | anim.cur_frame, frame.min_level, |
315 | frame->disp_time); | 400 | frame.disp_time); |
316 | } | 401 | } |
402 | draw_clock(anim); | ||
403 | draw_percent(anim); | ||
317 | } | 404 | } |
318 | 405 | ||
319 | static void redraw_screen(struct charger *charger) | 406 | static void redraw_screen(struct charger *charger) |
@@ -323,7 +410,7 @@ static void redraw_screen(struct charger *charger) | |||
323 | clear_screen(); | 410 | clear_screen(); |
324 | 411 | ||
325 | /* try to display *something* */ | 412 | /* try to display *something* */ |
326 | if (batt_anim->capacity < 0 || batt_anim->num_frames == 0) | 413 | if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0) |
327 | draw_unknown(charger); | 414 | draw_unknown(charger); |
328 | else | 415 | else |
329 | draw_battery(charger); | 416 | draw_battery(charger); |
@@ -342,16 +429,33 @@ static void reset_animation(struct animation *anim) | |||
342 | anim->run = false; | 429 | anim->run = false; |
343 | } | 430 | } |
344 | 431 | ||
432 | static void init_status_display(struct animation* anim) | ||
433 | { | ||
434 | int res; | ||
435 | |||
436 | if (!anim->text_clock.font_file.empty()) { | ||
437 | if ((res = | ||
438 | gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) { | ||
439 | LOGE("Could not load time font (%d)\n", res); | ||
440 | } | ||
441 | } | ||
442 | |||
443 | if (!anim->text_percent.font_file.empty()) { | ||
444 | if ((res = | ||
445 | gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) { | ||
446 | LOGE("Could not load percent font (%d)\n", res); | ||
447 | } | ||
448 | } | ||
449 | } | ||
450 | |||
345 | static void update_screen_state(struct charger *charger, int64_t now) | 451 | static void update_screen_state(struct charger *charger, int64_t now) |
346 | { | 452 | { |
347 | struct animation *batt_anim = charger->batt_anim; | 453 | struct animation *batt_anim = charger->batt_anim; |
348 | int disp_time; | 454 | int disp_time; |
349 | 455 | ||
350 | if (!batt_anim->run || now < charger->next_screen_transition) | 456 | if (!batt_anim->run || now < charger->next_screen_transition) return; |
351 | return; | ||
352 | 457 | ||
353 | if (!minui_inited) { | 458 | if (!minui_inited) { |
354 | |||
355 | if (healthd_config && healthd_config->screen_on) { | 459 | if (healthd_config && healthd_config->screen_on) { |
356 | if (!healthd_config->screen_on(batt_prop)) { | 460 | if (!healthd_config->screen_on(batt_prop)) { |
357 | LOGV("[%" PRId64 "] leave screen off\n", now); | 461 | LOGV("[%" PRId64 "] leave screen off\n", now); |
@@ -365,6 +469,7 @@ static void update_screen_state(struct charger *charger, int64_t now) | |||
365 | 469 | ||
366 | gr_init(); | 470 | gr_init(); |
367 | gr_font_size(gr_sys_font(), &char_width, &char_height); | 471 | gr_font_size(gr_sys_font(), &char_width, &char_height); |
472 | init_status_display(batt_anim); | ||
368 | 473 | ||
369 | #ifndef CHARGER_DISABLE_INIT_BLANK | 474 | #ifndef CHARGER_DISABLE_INIT_BLANK |
370 | gr_fb_blank(true); | 475 | gr_fb_blank(true); |
@@ -373,7 +478,7 @@ static void update_screen_state(struct charger *charger, int64_t now) | |||
373 | } | 478 | } |
374 | 479 | ||
375 | /* animation is over, blank screen and leave */ | 480 | /* animation is over, blank screen and leave */ |
376 | if (batt_anim->cur_cycle == batt_anim->num_cycles) { | 481 | if (batt_anim->num_cycles > 0 && batt_anim->cur_cycle == batt_anim->num_cycles) { |
377 | reset_animation(batt_anim); | 482 | reset_animation(batt_anim); |
378 | charger->next_screen_transition = -1; | 483 | charger->next_screen_transition = -1; |
379 | gr_fb_blank(true); | 484 | gr_fb_blank(true); |
@@ -389,21 +494,24 @@ static void update_screen_state(struct charger *charger, int64_t now) | |||
389 | if (batt_anim->cur_frame == 0) { | 494 | if (batt_anim->cur_frame == 0) { |
390 | 495 | ||
391 | LOGV("[%" PRId64 "] animation starting\n", now); | 496 | LOGV("[%" PRId64 "] animation starting\n", now); |
392 | if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) { | 497 | if (batt_prop) { |
393 | int i; | 498 | batt_anim->cur_level = batt_prop->batteryLevel; |
499 | batt_anim->cur_status = batt_prop->batteryStatus; | ||
500 | if (batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) { | ||
501 | /* find first frame given current battery level */ | ||
502 | for (int i = 0; i < batt_anim->num_frames; i++) { | ||
503 | if (batt_anim->cur_level >= batt_anim->frames[i].min_level && | ||
504 | batt_anim->cur_level <= batt_anim->frames[i].max_level) { | ||
505 | batt_anim->cur_frame = i; | ||
506 | break; | ||
507 | } | ||
508 | } | ||
394 | 509 | ||
395 | /* find first frame given current capacity */ | 510 | // repeat the first frame first_frame_repeats times |
396 | for (i = 1; i < batt_anim->num_frames; i++) { | 511 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * |
397 | if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity) | 512 | batt_anim->first_frame_repeats; |
398 | break; | ||
399 | } | 513 | } |
400 | batt_anim->cur_frame = i - 1; | ||
401 | |||
402 | /* show the first frame for twice as long */ | ||
403 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2; | ||
404 | } | 514 | } |
405 | if (batt_prop) | ||
406 | batt_anim->capacity = batt_prop->batteryLevel; | ||
407 | } | 515 | } |
408 | 516 | ||
409 | /* unblank the screen on first cycle */ | 517 | /* unblank the screen on first cycle */ |
@@ -416,8 +524,8 @@ static void update_screen_state(struct charger *charger, int64_t now) | |||
416 | /* if we don't have anim frames, we only have one image, so just bump | 524 | /* if we don't have anim frames, we only have one image, so just bump |
417 | * the cycle counter and exit | 525 | * the cycle counter and exit |
418 | */ | 526 | */ |
419 | if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) { | 527 | if (batt_anim->num_frames == 0 || batt_anim->cur_level < 0) { |
420 | LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now); | 528 | LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now); |
421 | charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; | 529 | charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; |
422 | batt_anim->cur_cycle++; | 530 | batt_anim->cur_cycle++; |
423 | return; | 531 | return; |
@@ -432,12 +540,11 @@ static void update_screen_state(struct charger *charger, int64_t now) | |||
432 | if (charger->charger_connected) { | 540 | if (charger->charger_connected) { |
433 | batt_anim->cur_frame++; | 541 | batt_anim->cur_frame++; |
434 | 542 | ||
435 | /* if the frame is used for level-only, that is only show it when it's | ||
436 | * the current level, skip it during the animation. | ||
437 | */ | ||
438 | while (batt_anim->cur_frame < batt_anim->num_frames && | 543 | while (batt_anim->cur_frame < batt_anim->num_frames && |
439 | batt_anim->frames[batt_anim->cur_frame].level_only) | 544 | (batt_anim->cur_level < batt_anim->frames[batt_anim->cur_frame].min_level || |
545 | batt_anim->cur_level > batt_anim->frames[batt_anim->cur_frame].max_level)) { | ||
440 | batt_anim->cur_frame++; | 546 | batt_anim->cur_frame++; |
547 | } | ||
441 | if (batt_anim->cur_frame >= batt_anim->num_frames) { | 548 | if (batt_anim->cur_frame >= batt_anim->num_frames) { |
442 | batt_anim->cur_cycle++; | 549 | batt_anim->cur_cycle++; |
443 | batt_anim->cur_frame = 0; | 550 | batt_anim->cur_frame = 0; |
@@ -521,7 +628,7 @@ static void process_key(struct charger *charger, int code, int64_t now) | |||
521 | LOGW("[%" PRId64 "] booting from charger mode\n", now); | 628 | LOGW("[%" PRId64 "] booting from charger mode\n", now); |
522 | property_set("sys.boot_from_charger_mode", "1"); | 629 | property_set("sys.boot_from_charger_mode", "1"); |
523 | } else { | 630 | } else { |
524 | if (charger->batt_anim->capacity >= charger->boot_min_cap) { | 631 | if (charger->batt_anim->cur_level >= charger->boot_min_cap) { |
525 | LOGW("[%" PRId64 "] rebooting\n", now); | 632 | LOGW("[%" PRId64 "] rebooting\n", now); |
526 | android_reboot(ANDROID_RB_RESTART, 0, 0); | 633 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
527 | } else { | 634 | } else { |
@@ -672,6 +779,52 @@ static void charger_event_handler(uint32_t /*epevents*/) | |||
672 | ev_dispatch(); | 779 | ev_dispatch(); |
673 | } | 780 | } |
674 | 781 | ||
782 | animation* init_animation() | ||
783 | { | ||
784 | bool parse_success; | ||
785 | |||
786 | std::string content; | ||
787 | if (base::ReadFileToString(animation_desc_path, &content)) { | ||
788 | parse_success = parse_animation_desc(content, &battery_animation); | ||
789 | } else { | ||
790 | LOGW("Could not open animation description at %s\n", animation_desc_path); | ||
791 | parse_success = false; | ||
792 | } | ||
793 | |||
794 | if (!parse_success) { | ||
795 | LOGW("Could not parse animation description. Using default animation.\n"); | ||
796 | battery_animation = BASE_ANIMATION; | ||
797 | battery_animation.animation_file.assign("charger/battery_scale"); | ||
798 | battery_animation.frames = default_animation_frames; | ||
799 | battery_animation.num_frames = ARRAY_SIZE(default_animation_frames); | ||
800 | } | ||
801 | if (battery_animation.fail_file.empty()) { | ||
802 | battery_animation.fail_file.assign("charger/battery_fail"); | ||
803 | } | ||
804 | |||
805 | LOGV("Animation Description:\n"); | ||
806 | LOGV(" animation: %d %d '%s' (%d)\n", | ||
807 | battery_animation.num_cycles, battery_animation.first_frame_repeats, | ||
808 | battery_animation.animation_file.c_str(), battery_animation.num_frames); | ||
809 | LOGV(" fail_file: '%s'\n", battery_animation.fail_file.c_str()); | ||
810 | LOGV(" clock: %d %d %d %d %d %d '%s'\n", | ||
811 | battery_animation.text_clock.pos_x, battery_animation.text_clock.pos_y, | ||
812 | battery_animation.text_clock.color_r, battery_animation.text_clock.color_g, | ||
813 | battery_animation.text_clock.color_b, battery_animation.text_clock.color_a, | ||
814 | battery_animation.text_clock.font_file.c_str()); | ||
815 | LOGV(" percent: %d %d %d %d %d %d '%s'\n", | ||
816 | battery_animation.text_percent.pos_x, battery_animation.text_percent.pos_y, | ||
817 | battery_animation.text_percent.color_r, battery_animation.text_percent.color_g, | ||
818 | battery_animation.text_percent.color_b, battery_animation.text_percent.color_a, | ||
819 | battery_animation.text_percent.font_file.c_str()); | ||
820 | for (int i = 0; i < battery_animation.num_frames; i++) { | ||
821 | LOGV(" frame %.2d: %d %d %d\n", i, battery_animation.frames[i].disp_time, | ||
822 | battery_animation.frames[i].min_level, battery_animation.frames[i].max_level); | ||
823 | } | ||
824 | |||
825 | return &battery_animation; | ||
826 | } | ||
827 | |||
675 | void healthd_mode_charger_init(struct healthd_config* config) | 828 | void healthd_mode_charger_init(struct healthd_config* config) |
676 | { | 829 | { |
677 | int ret; | 830 | int ret; |
@@ -689,35 +842,39 @@ void healthd_mode_charger_init(struct healthd_config* config) | |||
689 | healthd_register_event(epollfd, charger_event_handler); | 842 | healthd_register_event(epollfd, charger_event_handler); |
690 | } | 843 | } |
691 | 844 | ||
692 | ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown); | 845 | struct animation* anim = init_animation(); |
846 | charger->batt_anim = anim; | ||
847 | |||
848 | ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown); | ||
693 | if (ret < 0) { | 849 | if (ret < 0) { |
694 | LOGE("Cannot load battery_fail image\n"); | 850 | LOGE("Cannot load custom battery_fail image. Reverting to built in.\n"); |
695 | charger->surf_unknown = NULL; | 851 | ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown); |
852 | if (ret < 0) { | ||
853 | LOGE("Cannot load built in battery_fail image\n"); | ||
854 | charger->surf_unknown = NULL; | ||
855 | } | ||
696 | } | 856 | } |
697 | 857 | ||
698 | charger->batt_anim = &battery_animation; | ||
699 | |||
700 | GRSurface** scale_frames; | 858 | GRSurface** scale_frames; |
701 | int scale_count; | 859 | int scale_count; |
702 | int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text | 860 | int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text |
703 | // chunk). We are using hard-coded frame.disp_time instead. | 861 | // chunk). We are using hard-coded frame.disp_time instead. |
704 | ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_fps, | 862 | ret = res_create_multi_display_surface(anim->animation_file.c_str(), |
705 | &scale_frames); | 863 | &scale_count, &scale_fps, &scale_frames); |
706 | if (ret < 0) { | 864 | if (ret < 0) { |
707 | LOGE("Cannot load battery_scale image\n"); | 865 | LOGE("Cannot load battery_scale image\n"); |
708 | charger->batt_anim->num_frames = 0; | 866 | anim->num_frames = 0; |
709 | charger->batt_anim->num_cycles = 1; | 867 | anim->num_cycles = 1; |
710 | } else if (scale_count != charger->batt_anim->num_frames) { | 868 | } else if (scale_count != anim->num_frames) { |
711 | LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", | 869 | LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", |
712 | scale_count, charger->batt_anim->num_frames); | 870 | scale_count, anim->num_frames); |
713 | charger->batt_anim->num_frames = 0; | 871 | anim->num_frames = 0; |
714 | charger->batt_anim->num_cycles = 1; | 872 | anim->num_cycles = 1; |
715 | } else { | 873 | } else { |
716 | for (i = 0; i < charger->batt_anim->num_frames; i++) { | 874 | for (i = 0; i < anim->num_frames; i++) { |
717 | charger->batt_anim->frames[i].surface = scale_frames[i]; | 875 | anim->frames[i].surface = scale_frames[i]; |
718 | } | 876 | } |
719 | } | 877 | } |
720 | |||
721 | ev_sync_key_state(set_key_callback, charger); | 878 | ev_sync_key_state(set_key_callback, charger); |
722 | 879 | ||
723 | charger->next_screen_transition = -1; | 880 | charger->next_screen_transition = -1; |
diff --git a/healthd/tests/Android.mk b/healthd/tests/Android.mk new file mode 100644 index 000000000..87e8862d0 --- /dev/null +++ b/healthd/tests/Android.mk | |||
@@ -0,0 +1,21 @@ | |||
1 | # Copyright 2016 The Android Open Source Project | ||
2 | |||
3 | LOCAL_PATH:= $(call my-dir) | ||
4 | |||
5 | include $(CLEAR_VARS) | ||
6 | |||
7 | LOCAL_SRC_FILES := \ | ||
8 | AnimationParser_test.cpp \ | ||
9 | |||
10 | LOCAL_MODULE := healthd_test | ||
11 | LOCAL_MODULE_TAGS := tests | ||
12 | |||
13 | LOCAL_STATIC_LIBRARIES := \ | ||
14 | libhealthd_internal \ | ||
15 | |||
16 | LOCAL_SHARED_LIBRARIES := \ | ||
17 | liblog \ | ||
18 | libbase \ | ||
19 | libcutils \ | ||
20 | |||
21 | include $(BUILD_NATIVE_TEST) | ||
diff --git a/healthd/tests/AnimationParser_test.cpp b/healthd/tests/AnimationParser_test.cpp new file mode 100644 index 000000000..2fc3185e1 --- /dev/null +++ b/healthd/tests/AnimationParser_test.cpp | |||
@@ -0,0 +1,192 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "AnimationParser.h" | ||
18 | |||
19 | #include <gtest/gtest.h> | ||
20 | |||
21 | using namespace android; | ||
22 | |||
23 | TEST(AnimationParserTest, Test_can_ignore_line) { | ||
24 | EXPECT_TRUE(can_ignore_line("")); | ||
25 | EXPECT_TRUE(can_ignore_line(" ")); | ||
26 | EXPECT_TRUE(can_ignore_line("#")); | ||
27 | EXPECT_TRUE(can_ignore_line(" # comment")); | ||
28 | |||
29 | EXPECT_FALSE(can_ignore_line("text")); | ||
30 | EXPECT_FALSE(can_ignore_line("text # comment")); | ||
31 | EXPECT_FALSE(can_ignore_line(" text")); | ||
32 | EXPECT_FALSE(can_ignore_line(" text # comment")); | ||
33 | } | ||
34 | |||
35 | TEST(AnimationParserTest, Test_remove_prefix) { | ||
36 | static const char TEST_STRING[] = "abcdef"; | ||
37 | const char* rest = nullptr; | ||
38 | EXPECT_FALSE(remove_prefix(TEST_STRING, "def", &rest)); | ||
39 | // Ignore strings that only consist of the prefix | ||
40 | EXPECT_FALSE(remove_prefix(TEST_STRING, TEST_STRING, &rest)); | ||
41 | |||
42 | EXPECT_TRUE(remove_prefix(TEST_STRING, "abc", &rest)); | ||
43 | EXPECT_STREQ("def", rest); | ||
44 | |||
45 | EXPECT_TRUE(remove_prefix(" abcdef", "abc", &rest)); | ||
46 | EXPECT_STREQ("def", rest); | ||
47 | } | ||
48 | |||
49 | TEST(AnimationParserTest, Test_parse_text_field) { | ||
50 | static const char TEST_FILE_NAME[] = "font_file"; | ||
51 | static const int TEST_X = 3; | ||
52 | static const int TEST_Y = 6; | ||
53 | static const int TEST_R = 1; | ||
54 | static const int TEST_G = 2; | ||
55 | static const int TEST_B = 4; | ||
56 | static const int TEST_A = 8; | ||
57 | |||
58 | static const char TEST_XCENT_YCENT[] = "c c 1 2 4 8 font_file "; | ||
59 | static const char TEST_XCENT_YVAL[] = "c 6 1 2 4 8 font_file "; | ||
60 | static const char TEST_XVAL_YCENT[] = "3 c 1 2 4 8 font_file "; | ||
61 | static const char TEST_XVAL_YVAL[] = "3 6 1 2 4 8 font_file "; | ||
62 | static const char TEST_BAD_MISSING[] = "c c 1 2 4 font_file"; | ||
63 | static const char TEST_BAD_NO_FILE[] = "c c 1 2 4 8"; | ||
64 | |||
65 | animation::text_field out; | ||
66 | |||
67 | EXPECT_TRUE(parse_text_field(TEST_XCENT_YCENT, &out)); | ||
68 | EXPECT_EQ(CENTER_VAL, out.pos_x); | ||
69 | EXPECT_EQ(CENTER_VAL, out.pos_y); | ||
70 | EXPECT_EQ(TEST_R, out.color_r); | ||
71 | EXPECT_EQ(TEST_G, out.color_g); | ||
72 | EXPECT_EQ(TEST_B, out.color_b); | ||
73 | EXPECT_EQ(TEST_A, out.color_a); | ||
74 | EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str()); | ||
75 | |||
76 | EXPECT_TRUE(parse_text_field(TEST_XCENT_YVAL, &out)); | ||
77 | EXPECT_EQ(CENTER_VAL, out.pos_x); | ||
78 | EXPECT_EQ(TEST_Y, out.pos_y); | ||
79 | EXPECT_EQ(TEST_R, out.color_r); | ||
80 | EXPECT_EQ(TEST_G, out.color_g); | ||
81 | EXPECT_EQ(TEST_B, out.color_b); | ||
82 | EXPECT_EQ(TEST_A, out.color_a); | ||
83 | EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str()); | ||
84 | |||
85 | EXPECT_TRUE(parse_text_field(TEST_XVAL_YCENT, &out)); | ||
86 | EXPECT_EQ(TEST_X, out.pos_x); | ||
87 | EXPECT_EQ(CENTER_VAL, out.pos_y); | ||
88 | EXPECT_EQ(TEST_R, out.color_r); | ||
89 | EXPECT_EQ(TEST_G, out.color_g); | ||
90 | EXPECT_EQ(TEST_B, out.color_b); | ||
91 | EXPECT_EQ(TEST_A, out.color_a); | ||
92 | EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str()); | ||
93 | |||
94 | EXPECT_TRUE(parse_text_field(TEST_XVAL_YVAL, &out)); | ||
95 | EXPECT_EQ(TEST_X, out.pos_x); | ||
96 | EXPECT_EQ(TEST_Y, out.pos_y); | ||
97 | EXPECT_EQ(TEST_R, out.color_r); | ||
98 | EXPECT_EQ(TEST_G, out.color_g); | ||
99 | EXPECT_EQ(TEST_B, out.color_b); | ||
100 | EXPECT_EQ(TEST_A, out.color_a); | ||
101 | EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str()); | ||
102 | |||
103 | EXPECT_FALSE(parse_text_field(TEST_BAD_MISSING, &out)); | ||
104 | EXPECT_FALSE(parse_text_field(TEST_BAD_NO_FILE, &out)); | ||
105 | } | ||
106 | |||
107 | TEST(AnimationParserTest, Test_parse_animation_desc_basic) { | ||
108 | static const char TEST_ANIMATION[] = R"desc( | ||
109 | # Basic animation | ||
110 | animation: 5 1 test/animation_file | ||
111 | frame: 1000 0 100 | ||
112 | )desc"; | ||
113 | animation anim; | ||
114 | |||
115 | EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim)); | ||
116 | } | ||
117 | |||
118 | TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_animation_line) { | ||
119 | static const char TEST_ANIMATION[] = R"desc( | ||
120 | # Bad animation | ||
121 | frame: 1000 90 10 | ||
122 | )desc"; | ||
123 | animation anim; | ||
124 | |||
125 | EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim)); | ||
126 | } | ||
127 | |||
128 | TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_frame) { | ||
129 | static const char TEST_ANIMATION[] = R"desc( | ||
130 | # Bad animation | ||
131 | animation: 5 1 test/animation_file | ||
132 | )desc"; | ||
133 | animation anim; | ||
134 | |||
135 | EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim)); | ||
136 | } | ||
137 | |||
138 | TEST(AnimationParserTest, Test_parse_animation_desc_bad_animation_line_format) { | ||
139 | static const char TEST_ANIMATION[] = R"desc( | ||
140 | # Bad animation | ||
141 | animation: 5 1 | ||
142 | frame: 1000 90 10 | ||
143 | )desc"; | ||
144 | animation anim; | ||
145 | |||
146 | EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim)); | ||
147 | } | ||
148 | |||
149 | TEST(AnimationParserTest, Test_parse_animation_desc_full) { | ||
150 | static const char TEST_ANIMATION[] = R"desc( | ||
151 | # Full animation | ||
152 | animation: 5 1 test/animation_file | ||
153 | clock_display: 11 12 13 14 15 16 test/time_font | ||
154 | percent_display: 21 22 23 24 25 26 test/percent_font | ||
155 | |||
156 | frame: 10 20 30 | ||
157 | frame: 40 50 60 | ||
158 | )desc"; | ||
159 | animation anim; | ||
160 | |||
161 | EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim)); | ||
162 | |||
163 | EXPECT_EQ(5, anim.num_cycles); | ||
164 | EXPECT_EQ(1, anim.first_frame_repeats); | ||
165 | EXPECT_STREQ("test/animation_file", anim.animation_file.c_str()); | ||
166 | |||
167 | EXPECT_EQ(11, anim.text_clock.pos_x); | ||
168 | EXPECT_EQ(12, anim.text_clock.pos_y); | ||
169 | EXPECT_EQ(13, anim.text_clock.color_r); | ||
170 | EXPECT_EQ(14, anim.text_clock.color_g); | ||
171 | EXPECT_EQ(15, anim.text_clock.color_b); | ||
172 | EXPECT_EQ(16, anim.text_clock.color_a); | ||
173 | EXPECT_STREQ("test/time_font", anim.text_clock.font_file.c_str()); | ||
174 | |||
175 | EXPECT_EQ(21, anim.text_percent.pos_x); | ||
176 | EXPECT_EQ(22, anim.text_percent.pos_y); | ||
177 | EXPECT_EQ(23, anim.text_percent.color_r); | ||
178 | EXPECT_EQ(24, anim.text_percent.color_g); | ||
179 | EXPECT_EQ(25, anim.text_percent.color_b); | ||
180 | EXPECT_EQ(26, anim.text_percent.color_a); | ||
181 | EXPECT_STREQ("test/percent_font", anim.text_percent.font_file.c_str()); | ||
182 | |||
183 | EXPECT_EQ(2, anim.num_frames); | ||
184 | |||
185 | EXPECT_EQ(10, anim.frames[0].disp_time); | ||
186 | EXPECT_EQ(20, anim.frames[0].min_level); | ||
187 | EXPECT_EQ(30, anim.frames[0].max_level); | ||
188 | |||
189 | EXPECT_EQ(40, anim.frames[1].disp_time); | ||
190 | EXPECT_EQ(50, anim.frames[1].min_level); | ||
191 | EXPECT_EQ(60, anim.frames[1].max_level); | ||
192 | } | ||