blob: 3da8bda3b8b36781b2efef8e2c07a2b41f78b72c [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
23#define LOGE(x...) KLOG_ERROR("charger", x);
Todd Poynore5d1b622018-06-01 11:09:58 -070024#define LOGW(x...) KLOG_WARNING("charger", x);
Luke Song7f386dc2017-07-13 15:10:35 -070025#define LOGV(x...) KLOG_DEBUG("charger", x);
26
27HealthdDraw::HealthdDraw(animation* anim)
28 : kSplitScreen(HEALTHD_DRAW_SPLIT_SCREEN),
29 kSplitOffset(HEALTHD_DRAW_SPLIT_OFFSET) {
Todd Poynore5d1b622018-06-01 11:09:58 -070030 int ret = gr_init();
Luke Song7f386dc2017-07-13 15:10:35 -070031
Todd Poynore5d1b622018-06-01 11:09:58 -070032 if (ret < 0) {
33 LOGE("gr_init failed\n");
34 graphics_available = false;
35 return;
36 }
Luke Song7f386dc2017-07-13 15:10:35 -070037
Todd Poynore5d1b622018-06-01 11:09:58 -070038 graphics_available = true;
39 sys_font = gr_sys_font();
40 if (sys_font == nullptr) {
41 LOGW("No system font, screen fallback text not available\n");
42 } else {
43 gr_font_size(sys_font, &char_width_, &char_height_);
44 }
45
46 screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
47 screen_height_ = gr_fb_height();
48
49 int res;
50 if (!anim->text_clock.font_file.empty() &&
51 (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
52 LOGE("Could not load time font (%d)\n", res);
53 }
54 if (!anim->text_percent.font_file.empty() &&
55 (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
56 LOGE("Could not load percent font (%d)\n", res);
57 }
Luke Song7f386dc2017-07-13 15:10:35 -070058}
59
60HealthdDraw::~HealthdDraw() {}
61
62void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -070063 if (!graphics_available) return;
64 clear_screen();
Luke Song7f386dc2017-07-13 15:10:35 -070065
Todd Poynore5d1b622018-06-01 11:09:58 -070066 /* try to display *something* */
Ken Tsou6c7ece72019-02-15 10:50:58 +080067 if (batt_anim->cur_status == BATTERY_STATUS_UNKNOWN || batt_anim->cur_level < 0 ||
68 batt_anim->num_frames == 0)
Todd Poynore5d1b622018-06-01 11:09:58 -070069 draw_unknown(surf_unknown);
70 else
71 draw_battery(batt_anim);
72 gr_flip();
Luke Song7f386dc2017-07-13 15:10:35 -070073}
74
Todd Poynore5d1b622018-06-01 11:09:58 -070075void HealthdDraw::blank_screen(bool blank) {
76 if (!graphics_available) return;
77 gr_fb_blank(blank);
78}
Luke Song7f386dc2017-07-13 15:10:35 -070079
80void HealthdDraw::clear_screen(void) {
Todd Poynore5d1b622018-06-01 11:09:58 -070081 if (!graphics_available) return;
82 gr_color(0, 0, 0, 255);
83 gr_clear();
Luke Song7f386dc2017-07-13 15:10:35 -070084}
85
86int HealthdDraw::draw_surface_centered(GRSurface* surface) {
Todd Poynore5d1b622018-06-01 11:09:58 -070087 if (!graphics_available) return 0;
Luke Song7f386dc2017-07-13 15:10:35 -070088
Todd Poynore5d1b622018-06-01 11:09:58 -070089 int w = gr_get_width(surface);
90 int h = gr_get_height(surface);
91 int x = (screen_width_ - w) / 2 + kSplitOffset;
92 int y = (screen_height_ - h) / 2;
93
Luke Song7f386dc2017-07-13 15:10:35 -070094 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
95 gr_blit(surface, 0, 0, w, h, x, y);
Todd Poynore5d1b622018-06-01 11:09:58 -070096 if (kSplitScreen) {
97 x += screen_width_ - 2 * kSplitOffset;
98 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
99 gr_blit(surface, 0, 0, w, h, x, y);
100 }
Luke Song7f386dc2017-07-13 15:10:35 -0700101
Todd Poynore5d1b622018-06-01 11:09:58 -0700102 return y + h;
Luke Song7f386dc2017-07-13 15:10:35 -0700103}
104
105int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700106 if (!graphics_available) return 0;
107 int str_len_px = gr_measure(font, str);
Luke Song7f386dc2017-07-13 15:10:35 -0700108
Todd Poynore5d1b622018-06-01 11:09:58 -0700109 if (x < 0) x = (screen_width_ - str_len_px) / 2;
110 if (y < 0) y = (screen_height_ - char_height_) / 2;
111 gr_text(font, x + kSplitOffset, y, str, false /* bold */);
112 if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
Luke Song7f386dc2017-07-13 15:10:35 -0700113
Todd Poynore5d1b622018-06-01 11:09:58 -0700114 return y + char_height_;
Luke Song7f386dc2017-07-13 15:10:35 -0700115}
116
117void HealthdDraw::determine_xy(const animation::text_field& field,
118 const int length, int* x, int* y) {
119 *x = field.pos_x;
120
121 int str_len_px = length * field.font->char_width;
122 if (field.pos_x == CENTER_VAL) {
123 *x = (screen_width_ - str_len_px) / 2;
124 } else if (field.pos_x >= 0) {
125 *x = field.pos_x;
126 } else { // position from max edge
127 *x = screen_width_ + field.pos_x - str_len_px - kSplitOffset;
128 }
129
130 *y = field.pos_y;
131
132 if (field.pos_y == CENTER_VAL) {
133 *y = (screen_height_ - field.font->char_height) / 2;
134 } else if (field.pos_y >= 0) {
135 *y = field.pos_y;
136 } else { // position from max edge
137 *y = screen_height_ + field.pos_y - field.font->char_height;
138 }
139}
140
141void HealthdDraw::draw_clock(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700142 static constexpr char CLOCK_FORMAT[] = "%H:%M";
143 static constexpr int CLOCK_LENGTH = 6;
Luke Song7f386dc2017-07-13 15:10:35 -0700144
Todd Poynore5d1b622018-06-01 11:09:58 -0700145 const animation::text_field& field = anim->text_clock;
Luke Song7f386dc2017-07-13 15:10:35 -0700146
Todd Poynore5d1b622018-06-01 11:09:58 -0700147 if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
148 field.font->char_height == 0)
149 return;
Luke Song7f386dc2017-07-13 15:10:35 -0700150
Todd Poynore5d1b622018-06-01 11:09:58 -0700151 time_t rawtime;
152 time(&rawtime);
153 tm* time_info = localtime(&rawtime);
Luke Song7f386dc2017-07-13 15:10:35 -0700154
Todd Poynore5d1b622018-06-01 11:09:58 -0700155 char clock_str[CLOCK_LENGTH];
156 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
157 if (length != CLOCK_LENGTH - 1) {
158 LOGE("Could not format time\n");
159 return;
160 }
Luke Song7f386dc2017-07-13 15:10:35 -0700161
Todd Poynore5d1b622018-06-01 11:09:58 -0700162 int x, y;
163 determine_xy(field, length, &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700164
Todd Poynore5d1b622018-06-01 11:09:58 -0700165 LOGV("drawing clock %s %d %d\n", clock_str, x, y);
166 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
167 draw_text(field.font, x, y, clock_str);
Luke Song7f386dc2017-07-13 15:10:35 -0700168}
169
170void HealthdDraw::draw_percent(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700171 if (!graphics_available) return;
172 int cur_level = anim->cur_level;
173 if (anim->cur_status == BATTERY_STATUS_FULL) {
174 cur_level = 100;
175 }
Luke Song7f386dc2017-07-13 15:10:35 -0700176
kentsouba61ea42018-07-17 17:49:34 +0800177 if (cur_level < 0) return;
Luke Song7f386dc2017-07-13 15:10:35 -0700178
Todd Poynore5d1b622018-06-01 11:09:58 -0700179 const animation::text_field& field = anim->text_percent;
180 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
181 return;
182 }
Luke Song7f386dc2017-07-13 15:10:35 -0700183
Todd Poynore5d1b622018-06-01 11:09:58 -0700184 std::string str = base::StringPrintf("%d%%", cur_level);
Luke Song7f386dc2017-07-13 15:10:35 -0700185
Todd Poynore5d1b622018-06-01 11:09:58 -0700186 int x, y;
187 determine_xy(field, str.size(), &x, &y);
Luke Song7f386dc2017-07-13 15:10:35 -0700188
Todd Poynore5d1b622018-06-01 11:09:58 -0700189 LOGV("drawing percent %s %d %d\n", str.c_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, str.c_str());
Luke Song7f386dc2017-07-13 15:10:35 -0700192}
193
194void HealthdDraw::draw_battery(const animation* anim) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700195 if (!graphics_available) return;
196 const animation::frame& frame = anim->frames[anim->cur_frame];
Luke Song7f386dc2017-07-13 15:10:35 -0700197
Todd Poynore5d1b622018-06-01 11:09:58 -0700198 if (anim->num_frames != 0) {
199 draw_surface_centered(frame.surface);
200 LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
201 frame.disp_time);
202 }
203 draw_clock(anim);
204 draw_percent(anim);
Luke Song7f386dc2017-07-13 15:10:35 -0700205}
206
207void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
208 int y;
209 if (surf_unknown) {
Todd Poynore5d1b622018-06-01 11:09:58 -0700210 draw_surface_centered(surf_unknown);
211 } else if (sys_font) {
212 gr_color(0xa4, 0xc6, 0x39, 255);
213 y = draw_text(sys_font, -1, -1, "Charging!");
214 draw_text(sys_font, -1, y + 25, "?\?/100");
Luke Song7f386dc2017-07-13 15:10:35 -0700215 } else {
Todd Poynore5d1b622018-06-01 11:09:58 -0700216 LOGW("Charging, level unknown\n");
Luke Song7f386dc2017-07-13 15:10:35 -0700217 }
218}