Merge "charger: minui error handling fixups"

This commit is contained in:
Tao Bao 2018-07-30 17:32:32 +00:00 committed by Gerrit Code Review
commit bc31fc62ce
2 changed files with 118 additions and 90 deletions

View file

@ -21,26 +21,38 @@
#include "healthd_draw.h" #include "healthd_draw.h"
#define LOGE(x...) KLOG_ERROR("charger", x); #define LOGE(x...) KLOG_ERROR("charger", x);
#define LOGW(x...) KLOG_WARNING("charger", x);
#define LOGV(x...) KLOG_DEBUG("charger", x); #define LOGV(x...) KLOG_DEBUG("charger", x);
HealthdDraw::HealthdDraw(animation* anim) HealthdDraw::HealthdDraw(animation* anim)
: kSplitScreen(HEALTHD_DRAW_SPLIT_SCREEN), : kSplitScreen(HEALTHD_DRAW_SPLIT_SCREEN),
kSplitOffset(HEALTHD_DRAW_SPLIT_OFFSET) { kSplitOffset(HEALTHD_DRAW_SPLIT_OFFSET) {
gr_init(); int ret = gr_init();
gr_font_size(gr_sys_font(), &char_width_, &char_height_);
if (ret < 0) {
LOGE("gr_init failed\n");
graphics_available = false;
return;
}
graphics_available = true;
sys_font = gr_sys_font();
if (sys_font == nullptr) {
LOGW("No system font, screen fallback text not available\n");
} else {
gr_font_size(sys_font, &char_width_, &char_height_);
}
screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1); screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
screen_height_ = gr_fb_height(); screen_height_ = gr_fb_height();
int res; int res;
if (!anim->text_clock.font_file.empty() && if (!anim->text_clock.font_file.empty() &&
(res = gr_init_font(anim->text_clock.font_file.c_str(), (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
&anim->text_clock.font)) < 0) {
LOGE("Could not load time font (%d)\n", res); LOGE("Could not load time font (%d)\n", res);
} }
if (!anim->text_percent.font_file.empty() && if (!anim->text_percent.font_file.empty() &&
(res = gr_init_font(anim->text_percent.font_file.c_str(), (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
&anim->text_percent.font)) < 0) {
LOGE("Could not load percent font (%d)\n", res); LOGE("Could not load percent font (%d)\n", res);
} }
} }
@ -48,6 +60,7 @@ HealthdDraw::HealthdDraw(animation* anim)
HealthdDraw::~HealthdDraw() {} HealthdDraw::~HealthdDraw() {}
void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) { void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
if (!graphics_available) return;
clear_screen(); clear_screen();
/* try to display *something* */ /* try to display *something* */
@ -58,14 +71,20 @@ void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unkn
gr_flip(); gr_flip();
} }
void HealthdDraw::blank_screen(bool blank) { gr_fb_blank(blank); } void HealthdDraw::blank_screen(bool blank) {
if (!graphics_available) return;
gr_fb_blank(blank);
}
void HealthdDraw::clear_screen(void) { void HealthdDraw::clear_screen(void) {
if (!graphics_available) return;
gr_color(0, 0, 0, 255); gr_color(0, 0, 0, 255);
gr_clear(); gr_clear();
} }
int HealthdDraw::draw_surface_centered(GRSurface* surface) { int HealthdDraw::draw_surface_centered(GRSurface* surface) {
if (!graphics_available) return 0;
int w = gr_get_width(surface); int w = gr_get_width(surface);
int h = gr_get_height(surface); int h = gr_get_height(surface);
int x = (screen_width_ - w) / 2 + kSplitOffset; int x = (screen_width_ - w) / 2 + kSplitOffset;
@ -83,13 +102,13 @@ int HealthdDraw::draw_surface_centered(GRSurface* surface) {
} }
int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) { int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
if (!graphics_available) return 0;
int str_len_px = gr_measure(font, str); int str_len_px = gr_measure(font, str);
if (x < 0) x = (screen_width_ - str_len_px) / 2; if (x < 0) x = (screen_width_ - str_len_px) / 2;
if (y < 0) y = (screen_height_ - char_height_) / 2; if (y < 0) y = (screen_height_ - char_height_) / 2;
gr_text(font, x + kSplitOffset, y, str, false /* bold */); gr_text(font, x + kSplitOffset, y, str, false /* bold */);
if (kSplitScreen) if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
return y + char_height_; return y + char_height_;
} }
@ -124,7 +143,7 @@ void HealthdDraw::draw_clock(const animation* anim) {
const animation::text_field& field = anim->text_clock; const animation::text_field& field = anim->text_clock;
if (field.font == nullptr || field.font->char_width == 0 || if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
field.font->char_height == 0) field.font->char_height == 0)
return; return;
@ -148,6 +167,7 @@ void HealthdDraw::draw_clock(const animation* anim) {
} }
void HealthdDraw::draw_percent(const animation* anim) { void HealthdDraw::draw_percent(const animation* anim) {
if (!graphics_available) return;
int cur_level = anim->cur_level; int cur_level = anim->cur_level;
if (anim->cur_status == BATTERY_STATUS_FULL) { if (anim->cur_status == BATTERY_STATUS_FULL) {
cur_level = 100; cur_level = 100;
@ -156,8 +176,7 @@ void HealthdDraw::draw_percent(const animation* anim) {
if (cur_level <= 0) return; if (cur_level <= 0) return;
const animation::text_field& field = anim->text_percent; const animation::text_field& field = anim->text_percent;
if (field.font == nullptr || field.font->char_width == 0 || if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
field.font->char_height == 0) {
return; return;
} }
@ -172,12 +191,13 @@ void HealthdDraw::draw_percent(const animation* anim) {
} }
void HealthdDraw::draw_battery(const animation* anim) { void HealthdDraw::draw_battery(const animation* anim) {
if (!graphics_available) return;
const animation::frame& frame = anim->frames[anim->cur_frame]; const animation::frame& frame = anim->frames[anim->cur_frame];
if (anim->num_frames != 0) { if (anim->num_frames != 0) {
draw_surface_centered(frame.surface); draw_surface_centered(frame.surface);
LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
frame.min_level, frame.disp_time); frame.disp_time);
} }
draw_clock(anim); draw_clock(anim);
draw_percent(anim); draw_percent(anim);
@ -187,9 +207,11 @@ void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
int y; int y;
if (surf_unknown) { if (surf_unknown) {
draw_surface_centered(surf_unknown); draw_surface_centered(surf_unknown);
} else { } else if (sys_font) {
gr_color(0xa4, 0xc6, 0x39, 255); gr_color(0xa4, 0xc6, 0x39, 255);
y = draw_text(gr_sys_font(), -1, -1, "Charging!"); y = draw_text(sys_font, -1, -1, "Charging!");
draw_text(gr_sys_font(), -1, y + 25, "?\?/100"); draw_text(sys_font, -1, y + 25, "?\?/100");
} else {
LOGW("Charging, level unknown\n");
} }
} }

View file

@ -70,6 +70,12 @@ class HealthdDraw {
const bool kSplitScreen; const bool kSplitScreen;
// Pixels to offset graphics towards center split. // Pixels to offset graphics towards center split.
const int kSplitOffset; const int kSplitOffset;
// system text font, may be nullptr
const GRFont* sys_font;
// true if minui init'ed OK, false if minui init failed
bool graphics_available;
}; };
#endif // HEALTHD_DRAW_H #endif // HEALTHD_DRAW_H