blob: 0e6fd27838bbd09b626f6e542af7e4bf589b7a7e [file] [log] [blame]
Luke Song7f386dc2017-07-13 15:10:35 -07001/*
2 * Copyright (C) 2017 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 <android-base/stringprintf.h>
18#include <batteryservice/BatteryService.h>
19#include <cutils/klog.h>
20
21#include "healthd_draw.h"
22
Yifan Honge3ffd1b2021-10-20 22:18:16 -070023#if !defined(__ANDROID_VNDK__)
24#include "charger.sysprop.h"
25#endif
26
Luke Song7f386dc2017-07-13 15:10:35 -070027#define LOGE(x...) KLOG_ERROR("charger", x);
Todd Poynore5d1b622018-06-01 11:09:58 -070028#define LOGW(x...) KLOG_WARNING("charger", x);
Luke Song7f386dc2017-07-13 15:10:35 -070029#define LOGV(x...) KLOG_DEBUG("charger", x);
30
Yifan Hong97eecdc2019-07-03 11:07:37 -070031static bool get_split_screen() {
Yifan Honge3ffd1b2021-10-20 22:18:16 -070032#if !defined(__ANDROID_VNDK__)
Yifan Hong97eecdc2019-07-03 11:07:37 -070033 return android::sysprop::ChargerProperties::draw_split_screen().value_or(false);
Yifan Honge3ffd1b2021-10-20 22:18:16 -070034#else
35 return false;
36#endif
Yifan Hong97eecdc2019-07-03 11:07:37 -070037}
38
39static int get_split_offset() {
Yifan Honge3ffd1b2021-10-20 22:18:16 -070040#if !defined(__ANDROID_VNDK__)
Yifan Hong97eecdc2019-07-03 11:07:37 -070041 int64_t value = android::sysprop::ChargerProperties::draw_split_offset().value_or(0);
Yifan Honge3ffd1b2021-10-20 22:18:16 -070042#else
43 int64_t value = 0;
44#endif
Yifan Hong97eecdc2019-07-03 11:07:37 -070045 if (value < static_cast<int64_t>(std::numeric_limits<int>::min())) {
46 LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
47 std::numeric_limits<int>::min());
48 value = std::numeric_limits<int>::min();
49 }
50 if (value > static_cast<int64_t>(std::numeric_limits<int>::max())) {
51 LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
52 std::numeric_limits<int>::max());
53 value = std::numeric_limits<int>::max();
54 }
55 return static_cast<int>(value);
56}
57
Luke Song7f386dc2017-07-13 15:10:35 -070058HealthdDraw::HealthdDraw(animation* anim)
Yifan Hong97eecdc2019-07-03 11:07:37 -070059 : kSplitScreen(get_split_screen()), kSplitOffset(get_split_offset()) {
Todd Poynore5d1b622018-06-01 11:09:58 -070060 graphics_available = true;
61 sys_font = gr_sys_font();
62 if (sys_font == nullptr) {
63 LOGW("No system font, screen fallback text not available\n");
64 } else {
65 gr_font_size(sys_font, &char_width_, &char_height_);
66 }
67
68 screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
69 screen_height_ = gr_fb_height();
70
71 int res;
72 if (!anim->text_clock.font_file.empty() &&
73 (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
74 LOGE("Could not load time font (%d)\n", res);
75 }
76 if (!anim->text_percent.font_file.empty() &&
77 (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
78 LOGE("Could not load percent font (%d)\n", res);
79 }
Luke Song7f386dc2017-07-13 15:10:35 -070080}
81
82HealthdDraw::~HealthdDraw() {}
83
84void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -070085 if (!graphics_available) return;
86 clear_screen();
Luke Song7f386dc2017-07-13 15:10:35 -070087
Todd Poynore5d1b622018-06-01 11:09:58 -070088 /* try to display *something* */
Ken Tsou6c7ece72019-02-15 10:50:58 +080089 if (batt_anim->cur_status == BATTERY_STATUS_UNKNOWN || batt_anim->cur_level < 0 ||
90 batt_anim->num_frames == 0)
Todd Poynore5d1b622018-06-01 11:09:58 -070091 draw_unknown(surf_unknown);
92 else
93 draw_battery(batt_anim);
94 gr_flip();
Luke Song7f386dc2017-07-13 15:10:35 -070095}
96
Jack Wuc1b17112021-09-29 22:27:56 +080097void HealthdDraw::blank_screen(bool blank, int drm) {
Todd Poynore5d1b622018-06-01 11:09:58 -070098 if (!graphics_available) return;
Jack Wuc1b17112021-09-29 22:27:56 +080099 gr_fb_blank(blank, drm);
Todd Poynore5d1b622018-06-01 11:09:58 -0700100}
Luke Song7f386dc2017-07-13 15:10:35 -0700101
102void HealthdDraw::clear_screen(void) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700103 if (!graphics_available) return;
104 gr_color(0, 0, 0, 255);
105 gr_clear();
Luke Song7f386dc2017-07-13 15:10:35 -0700106}
107
108int HealthdDraw::draw_surface_centered(GRSurface* surface) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700109 if (!graphics_available) return 0;
Luke Song7f386dc2017-07-13 15:10:35 -0700110
Todd Poynore5d1b622018-06-01 11:09:58 -0700111 int w = gr_get_width(surface);
112 int h = gr_get_height(surface);
113 int x = (screen_width_ - w) / 2 + kSplitOffset;
114 int y = (screen_height_ - h) / 2;
115
Luke Song7f386dc2017-07-13 15:10:35 -0700116 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
117 gr_blit(surface, 0, 0, w, h, x, y);
Todd Poynore5d1b622018-06-01 11:09:58 -0700118 if (kSplitScreen) {
119 x += screen_width_ - 2 * kSplitOffset;
120 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
121 gr_blit(surface, 0, 0, w, h, x, y);
122 }
Luke Song7f386dc2017-07-13 15:10:35 -0700123
Todd Poynore5d1b622018-06-01 11:09:58 -0700124 return y + h;
Luke Song7f386dc2017-07-13 15:10:35 -0700125}
126
127int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700128 if (!graphics_available) return 0;
129 int str_len_px = gr_measure(font, str);
Luke Song7f386dc2017-07-13 15:10:35 -0700130
Todd Poynore5d1b622018-06-01 11:09:58 -0700131 if (x < 0) x = (screen_width_ - str_len_px) / 2;
132 if (y < 0) y = (screen_height_ - char_height_) / 2;
133 gr_text(font, x + kSplitOffset, y, str, false /* bold */);
134 if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
Luke Song7f386dc2017-07-13 15:10:35 -0700135
Todd Poynore5d1b622018-06-01 11:09:58 -0700136 return y + char_height_;
Luke Song7f386dc2017-07-13 15:10:35 -0700137}
138
139void HealthdDraw::determine_xy(const animation::text_field& field,
140 const int length, int* x, int* y) {
141 *x = field.pos_x;
Jack Wuc1b17112021-09-29 22:27:56 +0800142 screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
143 screen_height_ = gr_fb_height();
Luke Song7f386dc2017-07-13 15:10:35 -0700144
145 int str_len_px = length * field.font->char_width;
146 if (field.pos_x == CENTER_VAL) {
147 *x = (screen_width_ - str_len_px) / 2;
148 } else if (field.pos_x >= 0) {
149 *x = field.pos_x;
150 } else { // position from max edge
151 *x = screen_width_ + field.pos_x - str_len_px - kSplitOffset;
152 }
153
154 *y = field.pos_y;
155
156 if (field.pos_y == CENTER_VAL) {
157 *y = (screen_height_ - field.font->char_height) / 2;
158 } else if (field.pos_y >= 0) {
159 *y = field.pos_y;
160 } else { // position from max edge
161 *y = screen_height_ + field.pos_y - field.font->char_height;
162 }
163}
164
165void HealthdDraw::draw_clock(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700166 static constexpr char CLOCK_FORMAT[] = "%H:%M";
167 static constexpr int CLOCK_LENGTH = 6;
Luke Song7f386dc2017-07-13 15:10:35 -0700168
Todd Poynore5d1b622018-06-01 11:09:58 -0700169 const animation::text_field& field = anim->text_clock;
Luke Song7f386dc2017-07-13 15:10:35 -0700170
Todd Poynore5d1b622018-06-01 11:09:58 -0700171 if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
172 field.font->char_height == 0)
173 return;
Luke Song7f386dc2017-07-13 15:10:35 -0700174
Todd Poynore5d1b622018-06-01 11:09:58 -0700175 time_t rawtime;
176 time(&rawtime);
177 tm* time_info = localtime(&rawtime);
Luke Song7f386dc2017-07-13 15:10:35 -0700178
Todd Poynore5d1b622018-06-01 11:09:58 -0700179 char clock_str[CLOCK_LENGTH];
180 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
181 if (length != CLOCK_LENGTH - 1) {
182 LOGE("Could not format time\n");
183 return;
184 }
Luke Song7f386dc2017-07-13 15:10:35 -0700185
Todd Poynore5d1b622018-06-01 11:09:58 -0700186 int x, y;
187 determine_xy(field, length, &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700188
Todd Poynore5d1b622018-06-01 11:09:58 -0700189 LOGV("drawing clock %s %d %d\n", clock_str, x, y);
190 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
191 draw_text(field.font, x, y, clock_str);
Luke Song7f386dc2017-07-13 15:10:35 -0700192}
193
194void HealthdDraw::draw_percent(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700195 if (!graphics_available) return;
196 int cur_level = anim->cur_level;
197 if (anim->cur_status == BATTERY_STATUS_FULL) {
198 cur_level = 100;
199 }
Luke Song7f386dc2017-07-13 15:10:35 -0700200
kentsouba61ea42018-07-17 17:49:34 +0800201 if (cur_level < 0) return;
Luke Song7f386dc2017-07-13 15:10:35 -0700202
Todd Poynore5d1b622018-06-01 11:09:58 -0700203 const animation::text_field& field = anim->text_percent;
204 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
205 return;
206 }
Luke Song7f386dc2017-07-13 15:10:35 -0700207
Todd Poynore5d1b622018-06-01 11:09:58 -0700208 std::string str = base::StringPrintf("%d%%", cur_level);
Luke Song7f386dc2017-07-13 15:10:35 -0700209
Todd Poynore5d1b622018-06-01 11:09:58 -0700210 int x, y;
211 determine_xy(field, str.size(), &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700212
Todd Poynore5d1b622018-06-01 11:09:58 -0700213 LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
214 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
215 draw_text(field.font, x, y, str.c_str());
Luke Song7f386dc2017-07-13 15:10:35 -0700216}
217
218void HealthdDraw::draw_battery(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700219 if (!graphics_available) return;
220 const animation::frame& frame = anim->frames[anim->cur_frame];
Luke Song7f386dc2017-07-13 15:10:35 -0700221
Todd Poynore5d1b622018-06-01 11:09:58 -0700222 if (anim->num_frames != 0) {
223 draw_surface_centered(frame.surface);
224 LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
225 frame.disp_time);
226 }
227 draw_clock(anim);
228 draw_percent(anim);
Luke Song7f386dc2017-07-13 15:10:35 -0700229}
230
231void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
232 int y;
233 if (surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700234 draw_surface_centered(surf_unknown);
235 } else if (sys_font) {
236 gr_color(0xa4, 0xc6, 0x39, 255);
237 y = draw_text(sys_font, -1, -1, "Charging!");
238 draw_text(sys_font, -1, y + 25, "?\?/100");
Luke Song7f386dc2017-07-13 15:10:35 -0700239 } else {
Todd Poynore5d1b622018-06-01 11:09:58 -0700240 LOGW("Charging, level unknown\n");
Luke Song7f386dc2017-07-13 15:10:35 -0700241 }
242}
Xiaohui Niua40d8722021-08-31 16:22:25 +0800243
244std::unique_ptr<HealthdDraw> HealthdDraw::Create(animation *anim) {
245 if (gr_init() < 0) {
246 LOGE("gr_init failed\n");
247 return nullptr;
248 }
249 return std::unique_ptr<HealthdDraw>(new HealthdDraw(anim));
250}