blob: 50eee198f4e43ee72ae517adec29264107514aa5 [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 int ret = gr_init();
Luke Song7f386dc2017-07-13 15:10:35 -070050
Todd Poynore5d1b622018-06-01 11:09:58 -070051 if (ret < 0) {
52 LOGE("gr_init failed\n");
53 graphics_available = false;
54 return;
55 }
Luke Song7f386dc2017-07-13 15:10:35 -070056
Todd Poynore5d1b622018-06-01 11:09:58 -070057 graphics_available = true;
58 sys_font = gr_sys_font();
59 if (sys_font == nullptr) {
60 LOGW("No system font, screen fallback text not available\n");
61 } else {
62 gr_font_size(sys_font, &char_width_, &char_height_);
63 }
64
65 screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
66 screen_height_ = gr_fb_height();
67
68 int res;
69 if (!anim->text_clock.font_file.empty() &&
70 (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
71 LOGE("Could not load time font (%d)\n", res);
72 }
73 if (!anim->text_percent.font_file.empty() &&
74 (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
75 LOGE("Could not load percent font (%d)\n", res);
76 }
Luke Song7f386dc2017-07-13 15:10:35 -070077}
78
79HealthdDraw::~HealthdDraw() {}
80
81void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -070082 if (!graphics_available) return;
83 clear_screen();
Luke Song7f386dc2017-07-13 15:10:35 -070084
Todd Poynore5d1b622018-06-01 11:09:58 -070085 /* try to display *something* */
Ken Tsou6c7ece72019-02-15 10:50:58 +080086 if (batt_anim->cur_status == BATTERY_STATUS_UNKNOWN || batt_anim->cur_level < 0 ||
87 batt_anim->num_frames == 0)
Todd Poynore5d1b622018-06-01 11:09:58 -070088 draw_unknown(surf_unknown);
89 else
90 draw_battery(batt_anim);
91 gr_flip();
Luke Song7f386dc2017-07-13 15:10:35 -070092}
93
Todd Poynore5d1b622018-06-01 11:09:58 -070094void HealthdDraw::blank_screen(bool blank) {
95 if (!graphics_available) return;
96 gr_fb_blank(blank);
97}
Luke Song7f386dc2017-07-13 15:10:35 -070098
99void HealthdDraw::clear_screen(void) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700100 if (!graphics_available) return;
101 gr_color(0, 0, 0, 255);
102 gr_clear();
Luke Song7f386dc2017-07-13 15:10:35 -0700103}
104
105int HealthdDraw::draw_surface_centered(GRSurface* surface) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700106 if (!graphics_available) return 0;
Luke Song7f386dc2017-07-13 15:10:35 -0700107
Todd Poynore5d1b622018-06-01 11:09:58 -0700108 int w = gr_get_width(surface);
109 int h = gr_get_height(surface);
110 int x = (screen_width_ - w) / 2 + kSplitOffset;
111 int y = (screen_height_ - h) / 2;
112
Luke Song7f386dc2017-07-13 15:10:35 -0700113 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
114 gr_blit(surface, 0, 0, w, h, x, y);
Todd Poynore5d1b622018-06-01 11:09:58 -0700115 if (kSplitScreen) {
116 x += screen_width_ - 2 * kSplitOffset;
117 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
118 gr_blit(surface, 0, 0, w, h, x, y);
119 }
Luke Song7f386dc2017-07-13 15:10:35 -0700120
Todd Poynore5d1b622018-06-01 11:09:58 -0700121 return y + h;
Luke Song7f386dc2017-07-13 15:10:35 -0700122}
123
124int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700125 if (!graphics_available) return 0;
126 int str_len_px = gr_measure(font, str);
Luke Song7f386dc2017-07-13 15:10:35 -0700127
Todd Poynore5d1b622018-06-01 11:09:58 -0700128 if (x < 0) x = (screen_width_ - str_len_px) / 2;
129 if (y < 0) y = (screen_height_ - char_height_) / 2;
130 gr_text(font, x + kSplitOffset, y, str, false /* bold */);
131 if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
Luke Song7f386dc2017-07-13 15:10:35 -0700132
Todd Poynore5d1b622018-06-01 11:09:58 -0700133 return y + char_height_;
Luke Song7f386dc2017-07-13 15:10:35 -0700134}
135
136void HealthdDraw::determine_xy(const animation::text_field& field,
137 const int length, int* x, int* y) {
138 *x = field.pos_x;
139
140 int str_len_px = length * field.font->char_width;
141 if (field.pos_x == CENTER_VAL) {
142 *x = (screen_width_ - str_len_px) / 2;
143 } else if (field.pos_x >= 0) {
144 *x = field.pos_x;
145 } else { // position from max edge
146 *x = screen_width_ + field.pos_x - str_len_px - kSplitOffset;
147 }
148
149 *y = field.pos_y;
150
151 if (field.pos_y == CENTER_VAL) {
152 *y = (screen_height_ - field.font->char_height) / 2;
153 } else if (field.pos_y >= 0) {
154 *y = field.pos_y;
155 } else { // position from max edge
156 *y = screen_height_ + field.pos_y - field.font->char_height;
157 }
158}
159
160void HealthdDraw::draw_clock(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700161 static constexpr char CLOCK_FORMAT[] = "%H:%M";
162 static constexpr int CLOCK_LENGTH = 6;
Luke Song7f386dc2017-07-13 15:10:35 -0700163
Todd Poynore5d1b622018-06-01 11:09:58 -0700164 const animation::text_field& field = anim->text_clock;
Luke Song7f386dc2017-07-13 15:10:35 -0700165
Todd Poynore5d1b622018-06-01 11:09:58 -0700166 if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
167 field.font->char_height == 0)
168 return;
Luke Song7f386dc2017-07-13 15:10:35 -0700169
Todd Poynore5d1b622018-06-01 11:09:58 -0700170 time_t rawtime;
171 time(&rawtime);
172 tm* time_info = localtime(&rawtime);
Luke Song7f386dc2017-07-13 15:10:35 -0700173
Todd Poynore5d1b622018-06-01 11:09:58 -0700174 char clock_str[CLOCK_LENGTH];
175 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
176 if (length != CLOCK_LENGTH - 1) {
177 LOGE("Could not format time\n");
178 return;
179 }
Luke Song7f386dc2017-07-13 15:10:35 -0700180
Todd Poynore5d1b622018-06-01 11:09:58 -0700181 int x, y;
182 determine_xy(field, length, &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700183
Todd Poynore5d1b622018-06-01 11:09:58 -0700184 LOGV("drawing clock %s %d %d\n", clock_str, x, y);
185 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
186 draw_text(field.font, x, y, clock_str);
Luke Song7f386dc2017-07-13 15:10:35 -0700187}
188
189void HealthdDraw::draw_percent(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700190 if (!graphics_available) return;
191 int cur_level = anim->cur_level;
192 if (anim->cur_status == BATTERY_STATUS_FULL) {
193 cur_level = 100;
194 }
Luke Song7f386dc2017-07-13 15:10:35 -0700195
kentsouba61ea42018-07-17 17:49:34 +0800196 if (cur_level < 0) return;
Luke Song7f386dc2017-07-13 15:10:35 -0700197
Todd Poynore5d1b622018-06-01 11:09:58 -0700198 const animation::text_field& field = anim->text_percent;
199 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
200 return;
201 }
Luke Song7f386dc2017-07-13 15:10:35 -0700202
Todd Poynore5d1b622018-06-01 11:09:58 -0700203 std::string str = base::StringPrintf("%d%%", cur_level);
Luke Song7f386dc2017-07-13 15:10:35 -0700204
Todd Poynore5d1b622018-06-01 11:09:58 -0700205 int x, y;
206 determine_xy(field, str.size(), &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700207
Todd Poynore5d1b622018-06-01 11:09:58 -0700208 LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
209 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
210 draw_text(field.font, x, y, str.c_str());
Luke Song7f386dc2017-07-13 15:10:35 -0700211}
212
213void HealthdDraw::draw_battery(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700214 if (!graphics_available) return;
215 const animation::frame& frame = anim->frames[anim->cur_frame];
Luke Song7f386dc2017-07-13 15:10:35 -0700216
Todd Poynore5d1b622018-06-01 11:09:58 -0700217 if (anim->num_frames != 0) {
218 draw_surface_centered(frame.surface);
219 LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
220 frame.disp_time);
221 }
222 draw_clock(anim);
223 draw_percent(anim);
Luke Song7f386dc2017-07-13 15:10:35 -0700224}
225
226void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
227 int y;
228 if (surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700229 draw_surface_centered(surf_unknown);
230 } else if (sys_font) {
231 gr_color(0xa4, 0xc6, 0x39, 255);
232 y = draw_text(sys_font, -1, -1, "Charging!");
233 draw_text(sys_font, -1, y + 25, "?\?/100");
Luke Song7f386dc2017-07-13 15:10:35 -0700234 } else {
Todd Poynore5d1b622018-06-01 11:09:58 -0700235 LOGW("Charging, level unknown\n");
Luke Song7f386dc2017-07-13 15:10:35 -0700236 }
237}