[system/core] healthd: charger: use images to display current capacity text

Add images to make up the value of capacity to make the size of
text bigger.

Also adds support display capacity text on four direction:
 - if the resolution of charging animation image is horizontal,
   the capacity text display on the top of charging animation image,
   but on the bottom if CHARGER_CAPACITY_TEXT_ROTATE_180 is selected;
 - and if the resolution of charging animation image is vertical,
   the capacity text display on the right of charing animation image,
   but on the left if CHARGER_CAPACITY_TEXT_ROTATE_180 is selected.

Signed-off-by: Firefly <service@t-firefly.com>
This commit is contained in:
Firefly
2015-08-31 10:42:47 +08:00
committed by djw
parent 9a8107a0ed
commit cd64a7531b
5 changed files with 218 additions and 5 deletions

View File

@ -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;

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB