blob: 9a47f6b2bfadee762cfce6cff710d10e51233734 [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
Yifan Hong97eecdc2019-07-03 11:07:37 -070021#include "charger.sysprop.h"
Luke Song7f386dc2017-07-13 15:10:35 -070022#include "healthd_draw.h"
23
24#define LOGE(x...) KLOG_ERROR("charger", x);
Todd Poynore5d1b622018-06-01 11:09:58 -070025#define LOGW(x...) KLOG_WARNING("charger", x);
Luke Song7f386dc2017-07-13 15:10:35 -070026#define LOGV(x...) KLOG_DEBUG("charger", x);
27
Yifan Hong97eecdc2019-07-03 11:07:37 -070028static bool get_split_screen() {
29 return android::sysprop::ChargerProperties::draw_split_screen().value_or(false);
30}
31
32static int get_split_offset() {
33 int64_t value = android::sysprop::ChargerProperties::draw_split_offset().value_or(0);
34 if (value < static_cast<int64_t>(std::numeric_limits<int>::min())) {
35 LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
36 std::numeric_limits<int>::min());
37 value = std::numeric_limits<int>::min();
38 }
39 if (value > static_cast<int64_t>(std::numeric_limits<int>::max())) {
40 LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
41 std::numeric_limits<int>::max());
42 value = std::numeric_limits<int>::max();
43 }
44 return static_cast<int>(value);
45}
46
Luke Song7f386dc2017-07-13 15:10:35 -070047HealthdDraw::HealthdDraw(animation* anim)
Yifan Hong97eecdc2019-07-03 11:07:37 -070048 : kSplitScreen(get_split_screen()), kSplitOffset(get_split_offset()) {
Todd Poynore5d1b622018-06-01 11:09:58 -070049 graphics_available = true;
50 sys_font = gr_sys_font();
51 if (sys_font == nullptr) {
52 LOGW("No system font, screen fallback text not available\n");
53 } else {
54 gr_font_size(sys_font, &char_width_, &char_height_);
55 }
56
57 screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
58 screen_height_ = gr_fb_height();
59
60 int res;
61 if (!anim->text_clock.font_file.empty() &&
62 (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
63 LOGE("Could not load time font (%d)\n", res);
64 }
65 if (!anim->text_percent.font_file.empty() &&
66 (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
67 LOGE("Could not load percent font (%d)\n", res);
68 }
Luke Song7f386dc2017-07-13 15:10:35 -070069}
70
71HealthdDraw::~HealthdDraw() {}
72
73void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -070074 if (!graphics_available) return;
75 clear_screen();
Luke Song7f386dc2017-07-13 15:10:35 -070076
Todd Poynore5d1b622018-06-01 11:09:58 -070077 /* try to display *something* */
Ken Tsou6c7ece72019-02-15 10:50:58 +080078 if (batt_anim->cur_status == BATTERY_STATUS_UNKNOWN || batt_anim->cur_level < 0 ||
79 batt_anim->num_frames == 0)
Todd Poynore5d1b622018-06-01 11:09:58 -070080 draw_unknown(surf_unknown);
81 else
82 draw_battery(batt_anim);
83 gr_flip();
Luke Song7f386dc2017-07-13 15:10:35 -070084}
85
Todd Poynore5d1b622018-06-01 11:09:58 -070086void HealthdDraw::blank_screen(bool blank) {
87 if (!graphics_available) return;
88 gr_fb_blank(blank);
89}
Luke Song7f386dc2017-07-13 15:10:35 -070090
91void HealthdDraw::clear_screen(void) {
Todd Poynore5d1b622018-06-01 11:09:58 -070092 if (!graphics_available) return;
93 gr_color(0, 0, 0, 255);
94 gr_clear();
Luke Song7f386dc2017-07-13 15:10:35 -070095}
96
97int HealthdDraw::draw_surface_centered(GRSurface* surface) {
Todd Poynore5d1b622018-06-01 11:09:58 -070098 if (!graphics_available) return 0;
Luke Song7f386dc2017-07-13 15:10:35 -070099
Todd Poynore5d1b622018-06-01 11:09:58 -0700100 int w = gr_get_width(surface);
101 int h = gr_get_height(surface);
102 int x = (screen_width_ - w) / 2 + kSplitOffset;
103 int y = (screen_height_ - h) / 2;
104
Luke Song7f386dc2017-07-13 15:10:35 -0700105 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
106 gr_blit(surface, 0, 0, w, h, x, y);
Todd Poynore5d1b622018-06-01 11:09:58 -0700107 if (kSplitScreen) {
108 x += screen_width_ - 2 * kSplitOffset;
109 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
110 gr_blit(surface, 0, 0, w, h, x, y);
111 }
Luke Song7f386dc2017-07-13 15:10:35 -0700112
Todd Poynore5d1b622018-06-01 11:09:58 -0700113 return y + h;
Luke Song7f386dc2017-07-13 15:10:35 -0700114}
115
116int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700117 if (!graphics_available) return 0;
118 int str_len_px = gr_measure(font, str);
Luke Song7f386dc2017-07-13 15:10:35 -0700119
Todd Poynore5d1b622018-06-01 11:09:58 -0700120 if (x < 0) x = (screen_width_ - str_len_px) / 2;
121 if (y < 0) y = (screen_height_ - char_height_) / 2;
122 gr_text(font, x + kSplitOffset, y, str, false /* bold */);
123 if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
Luke Song7f386dc2017-07-13 15:10:35 -0700124
Todd Poynore5d1b622018-06-01 11:09:58 -0700125 return y + char_height_;
Luke Song7f386dc2017-07-13 15:10:35 -0700126}
127
128void HealthdDraw::determine_xy(const animation::text_field& field,
129 const int length, int* x, int* y) {
130 *x = field.pos_x;
131
132 int str_len_px = length * field.font->char_width;
133 if (field.pos_x == CENTER_VAL) {
134 *x = (screen_width_ - str_len_px) / 2;
135 } else if (field.pos_x >= 0) {
136 *x = field.pos_x;
137 } else { // position from max edge
138 *x = screen_width_ + field.pos_x - str_len_px - kSplitOffset;
139 }
140
141 *y = field.pos_y;
142
143 if (field.pos_y == CENTER_VAL) {
144 *y = (screen_height_ - field.font->char_height) / 2;
145 } else if (field.pos_y >= 0) {
146 *y = field.pos_y;
147 } else { // position from max edge
148 *y = screen_height_ + field.pos_y - field.font->char_height;
149 }
150}
151
152void HealthdDraw::draw_clock(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700153 static constexpr char CLOCK_FORMAT[] = "%H:%M";
154 static constexpr int CLOCK_LENGTH = 6;
Luke Song7f386dc2017-07-13 15:10:35 -0700155
Todd Poynore5d1b622018-06-01 11:09:58 -0700156 const animation::text_field& field = anim->text_clock;
Luke Song7f386dc2017-07-13 15:10:35 -0700157
Todd Poynore5d1b622018-06-01 11:09:58 -0700158 if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
159 field.font->char_height == 0)
160 return;
Luke Song7f386dc2017-07-13 15:10:35 -0700161
Todd Poynore5d1b622018-06-01 11:09:58 -0700162 time_t rawtime;
163 time(&rawtime);
164 tm* time_info = localtime(&rawtime);
Luke Song7f386dc2017-07-13 15:10:35 -0700165
Todd Poynore5d1b622018-06-01 11:09:58 -0700166 char clock_str[CLOCK_LENGTH];
167 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
168 if (length != CLOCK_LENGTH - 1) {
169 LOGE("Could not format time\n");
170 return;
171 }
Luke Song7f386dc2017-07-13 15:10:35 -0700172
Todd Poynore5d1b622018-06-01 11:09:58 -0700173 int x, y;
174 determine_xy(field, length, &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700175
Todd Poynore5d1b622018-06-01 11:09:58 -0700176 LOGV("drawing clock %s %d %d\n", clock_str, x, y);
177 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
178 draw_text(field.font, x, y, clock_str);
Luke Song7f386dc2017-07-13 15:10:35 -0700179}
180
181void HealthdDraw::draw_percent(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700182 if (!graphics_available) return;
183 int cur_level = anim->cur_level;
184 if (anim->cur_status == BATTERY_STATUS_FULL) {
185 cur_level = 100;
186 }
Luke Song7f386dc2017-07-13 15:10:35 -0700187
kentsouba61ea42018-07-17 17:49:34 +0800188 if (cur_level < 0) return;
Luke Song7f386dc2017-07-13 15:10:35 -0700189
Todd Poynore5d1b622018-06-01 11:09:58 -0700190 const animation::text_field& field = anim->text_percent;
191 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
192 return;
193 }
Luke Song7f386dc2017-07-13 15:10:35 -0700194
Todd Poynore5d1b622018-06-01 11:09:58 -0700195 std::string str = base::StringPrintf("%d%%", cur_level);
Luke Song7f386dc2017-07-13 15:10:35 -0700196
Todd Poynore5d1b622018-06-01 11:09:58 -0700197 int x, y;
198 determine_xy(field, str.size(), &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700199
Todd Poynore5d1b622018-06-01 11:09:58 -0700200 LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
201 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
202 draw_text(field.font, x, y, str.c_str());
Luke Song7f386dc2017-07-13 15:10:35 -0700203}
204
205void HealthdDraw::draw_battery(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700206 if (!graphics_available) return;
207 const animation::frame& frame = anim->frames[anim->cur_frame];
Luke Song7f386dc2017-07-13 15:10:35 -0700208
Todd Poynore5d1b622018-06-01 11:09:58 -0700209 if (anim->num_frames != 0) {
210 draw_surface_centered(frame.surface);
211 LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
212 frame.disp_time);
213 }
214 draw_clock(anim);
215 draw_percent(anim);
Luke Song7f386dc2017-07-13 15:10:35 -0700216}
217
218void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
219 int y;
220 if (surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700221 draw_surface_centered(surf_unknown);
222 } else if (sys_font) {
223 gr_color(0xa4, 0xc6, 0x39, 255);
224 y = draw_text(sys_font, -1, -1, "Charging!");
225 draw_text(sys_font, -1, y + 25, "?\?/100");
Luke Song7f386dc2017-07-13 15:10:35 -0700226 } else {
Todd Poynore5d1b622018-06-01 11:09:58 -0700227 LOGW("Charging, level unknown\n");
Luke Song7f386dc2017-07-13 15:10:35 -0700228 }
229}
Xiaohui Niua40d8722021-08-31 16:22:25 +0800230
231std::unique_ptr<HealthdDraw> HealthdDraw::Create(animation *anim) {
232 if (gr_init() < 0) {
233 LOGE("gr_init failed\n");
234 return nullptr;
235 }
236 return std::unique_ptr<HealthdDraw>(new HealthdDraw(anim));
237}