diff --git a/system/core/healthd/healthd_mode_charger.cpp b/system/core/healthd/healthd_mode_charger.cpp index 33d83f80ff..6b584dc2d2 100644 --- a/system/core/healthd/healthd_mode_charger.cpp +++ b/system/core/healthd/healthd_mode_charger.cpp @@ -77,6 +77,13 @@ char *locale; #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) +/* if w0 < w1, return a negative number */ +#define GET_CENTER_POS(w0, w1) (((w0) - (w1)) / 2) + +#ifndef CHARGER_CAPACITY_TEXT +#define CHARGER_CAPACITY_TEXT /* remove it if not want to display capacity */ +#endif + struct key_state { bool pending; bool down; @@ -92,6 +99,16 @@ struct frame { gr_surface surface; }; +struct capacity_text { + const char *name; + int text_count; + int width; + int height; + int cheight; + + gr_surface surface; +}; + struct animation { bool run; @@ -174,6 +191,41 @@ static struct animation battery_animation = { .capacity = 0, }; +static struct capacity_text batt_cap_text[] = { + { + .name = "charger/capacity_text_top", + .text_count = 11, + .width = 18, /* width and height will be update after load resource */ + .height = 352, + .cheight = 32, + .surface = NULL, + }, + { + .name = "charger/capacity_text_bottom", + .text_count = 11, + .width = 18, /* width and height will be update after load resource */ + .height = 352, + .cheight = 32, + .surface = NULL, + }, + { + .name = "charger/capacity_text_right", + .text_count = 11, + .width = 32, /* width and height will be update after load resource */ + .height = 198, + .cheight = 18, + .surface = NULL, + }, + { + .name = "charger/capacity_text_left", + .text_count = 11, + .width = 32, /* width and height will be update after load resource */ + .height = 198, + .cheight = 18, + .surface = NULL, + }, +}; + static struct charger charger_state; static struct healthd_config *healthd_config; static struct android::BatteryProperties *batt_prop; @@ -261,6 +313,11 @@ static int request_suspend(bool /*enable*/) } #endif +static bool outside(int x, int y) +{ + return x < 0 || x >= gr_fb_width() || y < 0 || y >= gr_fb_height(); +} + static int draw_text(const char *str, int x, int y) { int str_len_px = gr_measure(str); @@ -279,7 +336,12 @@ static void android_green(void) gr_color(0xa4, 0xc6, 0x39, 255); } -static int draw_charger_capacity(struct charger *charger, int x, int y, int w) +#ifdef CHARGER_CAPACITY_TEXT + +#ifdef CHARGER_CAPACITY_TEXT_USE_FONT +#define SPACING 10 +static int draw_charger_capacity(struct charger *charger, + int x, int y, int w, int h) { int clen; int font_w; @@ -288,13 +350,142 @@ static int draw_charger_capacity(struct charger *charger, int x, int y, int w) clen = snprintf(str, sizeof(str) - 1, "%d%%", batt_anim->capacity); font_w = clen * gr_get_font_cwidth(); - if (font_w < w) - x += (w - font_w) / 2; + x += GET_CENTER_POS(w, font_w); gr_color(255, 255, 255, 255); LOGV("drawing capacity %s xpos=%d ypos=%d\n", str, x, y); - return draw_text(str, x, y) + 10; + return draw_text(str, x, y) + SPACING; } +#else /* default use images */ +#define SPACING 20 +static int draw_charger_capacity(struct charger *charger, + int x, int y, int w, int h) +{ + int c_w; + int c_h; + int c_x; + int c_y; + int clen; + int i; + int index[4] = {0}; + struct animation *batt_anim = charger->batt_anim; + int capacity = batt_anim->capacity; + struct capacity_text *cap_text; + + if (capacity == 100) { + index[0] = 1; + index[1] = 0; + index[2] = 0; + index[3] = 10; /* '%' */ + clen = 4; + } else if (capacity > 9) { + index[0] = capacity / 10; + index[1] = capacity % 10; + index[2] = 10; + clen = 3; + } else { + index[0] = capacity % 10; + index[1] = 10; + clen = 2; + } + + if (w <= h) { +#ifdef CHARGER_CAPACITY_TEXT_ROTATE_180 /* on the bottom */ + cap_text = &batt_cap_text[1]; + if (!cap_text->surface) + return y; + + c_w = cap_text->width; + c_h = cap_text->cheight; + c_x = x + GET_CENTER_POS(w, c_w * clen); + c_y = GET_CENTER_POS(gr_fb_height(), h + c_h + SPACING) + h + SPACING; + + if (outside(c_x, c_y) || outside(c_x + c_w * clen, c_y + c_h)) { + LOGE("drawing outside %dx%d+%d+%d\n", c_x, c_y, c_w * clen, c_h); + return y; + } + + for (i = clen - 1; i >= 0; i--) { + LOGV("drawing text %d,%dx%d+%d+%d\n", index[i], c_w, c_h, c_x, c_y); + gr_blit(cap_text->surface, 0, c_h * index[i], c_w, c_h, c_x, c_y); + c_x += c_w; + } + + return c_y - h - SPACING; +#else /* default on the top */ + cap_text = &batt_cap_text[0]; + if (!cap_text->surface) + return y; + + c_w = cap_text->width; + c_h = cap_text->cheight; + c_x = x + GET_CENTER_POS(w, c_w * clen); + c_y = GET_CENTER_POS(gr_fb_height(), h + c_h + SPACING); + + if (outside(c_x, c_y) || outside(c_x + c_w * clen, c_y + c_h)) { + LOGE("drawing outside %dx%d+%d+%d\n", c_x, c_y, c_w * clen, c_h); + return y; + } + + for (i = 0; i < clen; i++) { + LOGV("drawing text %d,%dx%d+%d+%d\n", index[i], c_w, c_h, c_x, c_y); + gr_blit(cap_text->surface, 0, c_h * index[i], c_w, c_h, c_x, c_y); + c_x += c_w; + } + + return c_y + c_h + SPACING; +#endif + } else { +#ifdef CHARGER_CAPACITY_TEXT_ROTATE_180 /* on the left */ + cap_text = &batt_cap_text[3]; + if (!cap_text->surface) + return x; + + c_w = cap_text->width; + c_h = cap_text->cheight; + c_x = GET_CENTER_POS(gr_fb_width(), w + c_w + SPACING); + c_y = y + GET_CENTER_POS(h, c_h * clen); + + if (outside(c_x, c_y) || outside(c_x + c_w, c_y + c_h * clen)) { + LOGE("drawing outside %dx%d+%d+%d\n", c_x, c_y, c_w, c_h * clen); + return x; + } + + for (i = clen - 1; i >= 0; i--) { + LOGV("drawing text %d,%dx%d+%d+%d\n", index[i], c_w, c_h, c_x, c_y); + gr_blit(cap_text->surface, 0, c_h * index[i], c_w, c_h, c_x, c_y); + c_y += c_h; + } + + return c_x + c_w + SPACING; +#else /* the capacity text display on the right of charging icon by default */ + cap_text = &batt_cap_text[2]; + if (!cap_text->surface) + return x; + + c_w = cap_text->width; + c_h = cap_text->cheight; + c_x = GET_CENTER_POS(gr_fb_width(), w + c_w + SPACING) + w + SPACING; + c_y = y + GET_CENTER_POS(h, c_h * clen); + + if (outside(c_x, c_y) || outside (c_x + c_w, c_y + c_h * clen)) { + LOGE("drawing outside %dx%d+%d+%d\n", c_x, c_y, c_w, c_h * clen); + return x; + } + + for (i = 0; i < clen; i++) { + LOGV("drawing text %d,%dx%d+%d+%d\n", index[i], c_w, c_h, c_x, c_y); + gr_blit(cap_text->surface, 0, c_h * index[i], c_w, c_h, c_x, c_y); + c_y += c_h; + } + + return c_x - w - SPACING; +#endif + } +} +#endif + +#endif /* returns the last y-offset of where the surface ends */ static int draw_surface_centered(struct charger *charger, gr_surface surface) @@ -309,8 +500,13 @@ static int draw_surface_centered(struct charger *charger, gr_surface surface) x = (gr_fb_width() - w) / 2 ; y = (gr_fb_height() - h) / 2 ; +#ifdef CHARGER_CAPACITY_TEXT /* drawing capacity text */ - y = draw_charger_capacity(charger, x, y, w); + if (w <= h) + y = draw_charger_capacity(charger, x, y, w, h); + else /* capacity text display on the right of battery icon */ + x = draw_charger_capacity(charger, x, y, w, h); +#endif LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y); gr_blit(surface, 0, 0, w, h, x, y); @@ -727,6 +923,23 @@ void healthd_mode_charger_init(struct healthd_config* config) } } +#ifdef CHARGER_CAPACITY_TEXT + for (i = 0; i < 4; i++) { + struct capacity_text *cap_text = &batt_cap_text[i]; + + ret = res_create_display_surface(cap_text->name, &cap_text->surface); + if (ret < 0) { + LOGE("Cannot load image %s\n", cap_text->name); + cap_text->surface = NULL; + break; + } + cap_text->width = gr_get_width(cap_text->surface); + cap_text->height = gr_get_height(cap_text->surface); + if (cap_text->text_count > 0) + cap_text->cheight = cap_text->height / cap_text->text_count; + } +#endif + ev_sync_key_state(set_key_callback, charger); charger->next_screen_transition = -1; diff --git a/system/core/healthd/images/capacity_text_bottom.png b/system/core/healthd/images/capacity_text_bottom.png new file mode 100644 index 0000000000..8e62b75dae Binary files /dev/null and b/system/core/healthd/images/capacity_text_bottom.png differ diff --git a/system/core/healthd/images/capacity_text_left.png b/system/core/healthd/images/capacity_text_left.png new file mode 100644 index 0000000000..91aebd7d09 Binary files /dev/null and b/system/core/healthd/images/capacity_text_left.png differ diff --git a/system/core/healthd/images/capacity_text_right.png b/system/core/healthd/images/capacity_text_right.png new file mode 100644 index 0000000000..1fb084dc5e Binary files /dev/null and b/system/core/healthd/images/capacity_text_right.png differ diff --git a/system/core/healthd/images/capacity_text_top.png b/system/core/healthd/images/capacity_text_top.png new file mode 100644 index 0000000000..0f2eba8df9 Binary files /dev/null and b/system/core/healthd/images/capacity_text_top.png differ