summaryrefslogtreecommitdiffstats
path: root/healthd/healthd_mode_charger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'healthd/healthd_mode_charger.cpp')
-rw-r--r--healthd/healthd_mode_charger.cpp347
1 files changed, 252 insertions, 95 deletions
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index 66f927136..fb17f2da1 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -30,6 +30,9 @@
#include <time.h>
#include <unistd.h>
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+
#include <sys/socket.h>
#include <linux/netlink.h>
@@ -44,10 +47,14 @@
#include <suspend/autosuspend.h>
#endif
+#include "animation.h"
+#include "AnimationParser.h"
#include "minui/minui.h"
#include <healthd/healthd.h>
+using namespace android;
+
char *locale;
#ifndef max
@@ -67,8 +74,6 @@ char *locale;
#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
-#define BATTERY_FULL_THRESH 95
-
#define LAST_KMSG_PATH "/proc/last_kmsg"
#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops"
#define LAST_KMSG_MAX_SZ (32 * 1024)
@@ -77,34 +82,14 @@ char *locale;
#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
+static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt";
+
struct key_state {
bool pending;
bool down;
int64_t timestamp;
};
-struct frame {
- int disp_time;
- int min_capacity;
- bool level_only;
-
- GRSurface* surface;
-};
-
-struct animation {
- bool run;
-
- struct frame *frames;
- int cur_frame;
- int num_frames;
-
- int cur_cycle;
- int num_cycles;
-
- /* current capacity being animated */
- int capacity;
-};
-
struct charger {
bool have_battery_state;
bool charger_connected;
@@ -119,54 +104,83 @@ struct charger {
int boot_min_cap;
};
-static struct frame batt_anim_frames[] = {
+static const struct animation BASE_ANIMATION = {
+ .text_clock = {
+ .pos_x = 0,
+ .pos_y = 0,
+
+ .color_r = 255,
+ .color_g = 255,
+ .color_b = 255,
+ .color_a = 255,
+
+ .font = nullptr,
+ },
+ .text_percent = {
+ .pos_x = 0,
+ .pos_y = 0,
+
+ .color_r = 255,
+ .color_g = 255,
+ .color_b = 255,
+ .color_a = 255,
+ },
+
+ .run = false,
+
+ .frames = nullptr,
+ .cur_frame = 0,
+ .num_frames = 0,
+ .first_frame_repeats = 2,
+
+ .cur_cycle = 0,
+ .num_cycles = 3,
+
+ .cur_level = 0,
+ .cur_status = BATTERY_STATUS_UNKNOWN,
+};
+
+
+static struct animation::frame default_animation_frames[] = {
{
.disp_time = 750,
- .min_capacity = 0,
- .level_only = false,
+ .min_level = 0,
+ .max_level = 19,
.surface = NULL,
},
{
.disp_time = 750,
- .min_capacity = 20,
- .level_only = false,
+ .min_level = 0,
+ .max_level = 39,
.surface = NULL,
},
{
.disp_time = 750,
- .min_capacity = 40,
- .level_only = false,
+ .min_level = 0,
+ .max_level = 59,
.surface = NULL,
},
{
.disp_time = 750,
- .min_capacity = 60,
- .level_only = false,
+ .min_level = 0,
+ .max_level = 79,
.surface = NULL,
},
{
.disp_time = 750,
- .min_capacity = 80,
- .level_only = true,
+ .min_level = 80,
+ .max_level = 95,
.surface = NULL,
},
{
.disp_time = 750,
- .min_capacity = BATTERY_FULL_THRESH,
- .level_only = false,
+ .min_level = 0,
+ .max_level = 100,
.surface = NULL,
},
};
-static struct animation battery_animation = {
- .run = false,
- .frames = batt_anim_frames,
- .cur_frame = 0,
- .num_frames = ARRAY_SIZE(batt_anim_frames),
- .cur_cycle = 0,
- .num_cycles = 3,
- .capacity = 0,
-};
+static struct animation battery_animation = BASE_ANIMATION;
static struct charger charger_state;
static struct healthd_config *healthd_config;
@@ -273,8 +287,79 @@ static void android_green(void)
gr_color(0xa4, 0xc6, 0x39, 255);
}
+// Negative x or y coordinates position the text away from the opposite edge that positive ones do.
+void determine_xy(const animation::text_field& field, const int length, int* x, int* y)
+{
+ *x = field.pos_x;
+ *y = field.pos_y;
+
+ int str_len_px = length * field.font->char_width;
+ if (field.pos_x == CENTER_VAL) {
+ *x = (gr_fb_width() - str_len_px) / 2;
+ } else if (field.pos_x >= 0) {
+ *x = field.pos_x;
+ } else { // position from max edge
+ *x = gr_fb_width() + field.pos_x - str_len_px;
+ }
+
+ if (field.pos_y == CENTER_VAL) {
+ *y = (gr_fb_height() - field.font->char_height) / 2;
+ } else if (field.pos_y >= 0) {
+ *y = field.pos_y;
+ } else { // position from max edge
+ *y = gr_fb_height() + field.pos_y - field.font->char_height;
+ }
+}
+
+static void draw_clock(const animation& anim)
+{
+ static constexpr char CLOCK_FORMAT[] = "%H:%M";
+ static constexpr int CLOCK_LENGTH = 6;
+
+ const animation::text_field& field = anim.text_clock;
+
+ if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) return;
+
+ time_t rawtime;
+ time(&rawtime);
+ struct tm* time_info = localtime(&rawtime);
+
+ char clock_str[CLOCK_LENGTH];
+ size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
+ if (length != CLOCK_LENGTH - 1) {
+ LOGE("Could not format time\n");
+ return;
+ }
+
+ int x, y;
+ determine_xy(field, length, &x, &y);
+
+ LOGV("drawing clock %s %d %d\n", clock_str, x, y);
+ gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+ gr_text(field.font, x, y, clock_str, false);
+}
+
+static void draw_percent(const animation& anim)
+{
+ if (anim.cur_level <= 0 || anim.cur_status != BATTERY_STATUS_CHARGING) return;
+
+ const animation::text_field& field = anim.text_percent;
+ if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
+ return;
+ }
+
+ std::string str = base::StringPrintf("%d%%", anim.cur_level);
+
+ int x, y;
+ determine_xy(field, str.size(), &x, &y);
+
+ LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
+ gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+ gr_text(field.font, x, y, str.c_str(), false);
+}
+
/* returns the last y-offset of where the surface ends */
-static int draw_surface_centered(struct charger* /*charger*/, GRSurface* surface)
+static int draw_surface_centered(GRSurface* surface)
{
int w;
int h;
@@ -295,7 +380,7 @@ static void draw_unknown(struct charger *charger)
{
int y;
if (charger->surf_unknown) {
- draw_surface_centered(charger, charger->surf_unknown);
+ draw_surface_centered(charger->surf_unknown);
} else {
android_green();
y = draw_text("Charging!", -1, -1);
@@ -303,17 +388,19 @@ static void draw_unknown(struct charger *charger)
}
}
-static void draw_battery(struct charger *charger)
+static void draw_battery(const struct charger* charger)
{
- struct animation *batt_anim = charger->batt_anim;
- struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
+ const struct animation& anim = *charger->batt_anim;
+ const struct animation::frame& frame = anim.frames[anim.cur_frame];
- if (batt_anim->num_frames != 0) {
- draw_surface_centered(charger, frame->surface);
+ if (anim.num_frames != 0) {
+ draw_surface_centered(frame.surface);
LOGV("drawing frame #%d min_cap=%d time=%d\n",
- batt_anim->cur_frame, frame->min_capacity,
- frame->disp_time);
+ anim.cur_frame, frame.min_level,
+ frame.disp_time);
}
+ draw_clock(anim);
+ draw_percent(anim);
}
static void redraw_screen(struct charger *charger)
@@ -323,7 +410,7 @@ static void redraw_screen(struct charger *charger)
clear_screen();
/* try to display *something* */
- if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
+ if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0)
draw_unknown(charger);
else
draw_battery(charger);
@@ -342,16 +429,33 @@ static void reset_animation(struct animation *anim)
anim->run = false;
}
+static void init_status_display(struct animation* anim)
+{
+ int res;
+
+ if (!anim->text_clock.font_file.empty()) {
+ if ((res =
+ gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
+ LOGE("Could not load time font (%d)\n", res);
+ }
+ }
+
+ if (!anim->text_percent.font_file.empty()) {
+ if ((res =
+ gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
+ LOGE("Could not load percent font (%d)\n", res);
+ }
+ }
+}
+
static void update_screen_state(struct charger *charger, int64_t now)
{
struct animation *batt_anim = charger->batt_anim;
int disp_time;
- if (!batt_anim->run || now < charger->next_screen_transition)
- return;
+ if (!batt_anim->run || now < charger->next_screen_transition) return;
if (!minui_inited) {
-
if (healthd_config && healthd_config->screen_on) {
if (!healthd_config->screen_on(batt_prop)) {
LOGV("[%" PRId64 "] leave screen off\n", now);
@@ -365,6 +469,7 @@ static void update_screen_state(struct charger *charger, int64_t now)
gr_init();
gr_font_size(gr_sys_font(), &char_width, &char_height);
+ init_status_display(batt_anim);
#ifndef CHARGER_DISABLE_INIT_BLANK
gr_fb_blank(true);
@@ -373,7 +478,7 @@ static void update_screen_state(struct charger *charger, int64_t now)
}
/* animation is over, blank screen and leave */
- if (batt_anim->cur_cycle == batt_anim->num_cycles) {
+ if (batt_anim->num_cycles > 0 && batt_anim->cur_cycle == batt_anim->num_cycles) {
reset_animation(batt_anim);
charger->next_screen_transition = -1;
gr_fb_blank(true);
@@ -389,21 +494,24 @@ static void update_screen_state(struct charger *charger, int64_t now)
if (batt_anim->cur_frame == 0) {
LOGV("[%" PRId64 "] animation starting\n", now);
- if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
- int i;
+ if (batt_prop) {
+ batt_anim->cur_level = batt_prop->batteryLevel;
+ batt_anim->cur_status = batt_prop->batteryStatus;
+ if (batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
+ /* find first frame given current battery level */
+ for (int i = 0; i < batt_anim->num_frames; i++) {
+ if (batt_anim->cur_level >= batt_anim->frames[i].min_level &&
+ batt_anim->cur_level <= batt_anim->frames[i].max_level) {
+ batt_anim->cur_frame = i;
+ break;
+ }
+ }
- /* find first frame given current capacity */
- for (i = 1; i < batt_anim->num_frames; i++) {
- if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity)
- break;
+ // repeat the first frame first_frame_repeats times
+ disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time *
+ batt_anim->first_frame_repeats;
}
- batt_anim->cur_frame = i - 1;
-
- /* show the first frame for twice as long */
- disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
}
- if (batt_prop)
- batt_anim->capacity = batt_prop->batteryLevel;
}
/* unblank the screen on first cycle */
@@ -416,8 +524,8 @@ static void update_screen_state(struct charger *charger, int64_t now)
/* if we don't have anim frames, we only have one image, so just bump
* the cycle counter and exit
*/
- if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
- LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now);
+ if (batt_anim->num_frames == 0 || batt_anim->cur_level < 0) {
+ LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now);
charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
batt_anim->cur_cycle++;
return;
@@ -432,12 +540,11 @@ static void update_screen_state(struct charger *charger, int64_t now)
if (charger->charger_connected) {
batt_anim->cur_frame++;
- /* if the frame is used for level-only, that is only show it when it's
- * the current level, skip it during the animation.
- */
while (batt_anim->cur_frame < batt_anim->num_frames &&
- batt_anim->frames[batt_anim->cur_frame].level_only)
+ (batt_anim->cur_level < batt_anim->frames[batt_anim->cur_frame].min_level ||
+ batt_anim->cur_level > batt_anim->frames[batt_anim->cur_frame].max_level)) {
batt_anim->cur_frame++;
+ }
if (batt_anim->cur_frame >= batt_anim->num_frames) {
batt_anim->cur_cycle++;
batt_anim->cur_frame = 0;
@@ -521,7 +628,7 @@ static void process_key(struct charger *charger, int code, int64_t now)
LOGW("[%" PRId64 "] booting from charger mode\n", now);
property_set("sys.boot_from_charger_mode", "1");
} else {
- if (charger->batt_anim->capacity >= charger->boot_min_cap) {
+ if (charger->batt_anim->cur_level >= charger->boot_min_cap) {
LOGW("[%" PRId64 "] rebooting\n", now);
android_reboot(ANDROID_RB_RESTART, 0, 0);
} else {
@@ -672,6 +779,52 @@ static void charger_event_handler(uint32_t /*epevents*/)
ev_dispatch();
}
+animation* init_animation()
+{
+ bool parse_success;
+
+ std::string content;
+ if (base::ReadFileToString(animation_desc_path, &content)) {
+ parse_success = parse_animation_desc(content, &battery_animation);
+ } else {
+ LOGW("Could not open animation description at %s\n", animation_desc_path);
+ parse_success = false;
+ }
+
+ if (!parse_success) {
+ LOGW("Could not parse animation description. Using default animation.\n");
+ battery_animation = BASE_ANIMATION;
+ battery_animation.animation_file.assign("charger/battery_scale");
+ battery_animation.frames = default_animation_frames;
+ battery_animation.num_frames = ARRAY_SIZE(default_animation_frames);
+ }
+ if (battery_animation.fail_file.empty()) {
+ battery_animation.fail_file.assign("charger/battery_fail");
+ }
+
+ LOGV("Animation Description:\n");
+ LOGV(" animation: %d %d '%s' (%d)\n",
+ battery_animation.num_cycles, battery_animation.first_frame_repeats,
+ battery_animation.animation_file.c_str(), battery_animation.num_frames);
+ LOGV(" fail_file: '%s'\n", battery_animation.fail_file.c_str());
+ LOGV(" clock: %d %d %d %d %d %d '%s'\n",
+ battery_animation.text_clock.pos_x, battery_animation.text_clock.pos_y,
+ battery_animation.text_clock.color_r, battery_animation.text_clock.color_g,
+ battery_animation.text_clock.color_b, battery_animation.text_clock.color_a,
+ battery_animation.text_clock.font_file.c_str());
+ LOGV(" percent: %d %d %d %d %d %d '%s'\n",
+ battery_animation.text_percent.pos_x, battery_animation.text_percent.pos_y,
+ battery_animation.text_percent.color_r, battery_animation.text_percent.color_g,
+ battery_animation.text_percent.color_b, battery_animation.text_percent.color_a,
+ battery_animation.text_percent.font_file.c_str());
+ for (int i = 0; i < battery_animation.num_frames; i++) {
+ LOGV(" frame %.2d: %d %d %d\n", i, battery_animation.frames[i].disp_time,
+ battery_animation.frames[i].min_level, battery_animation.frames[i].max_level);
+ }
+
+ return &battery_animation;
+}
+
void healthd_mode_charger_init(struct healthd_config* config)
{
int ret;
@@ -689,35 +842,39 @@ void healthd_mode_charger_init(struct healthd_config* config)
healthd_register_event(epollfd, charger_event_handler);
}
- ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
+ struct animation* anim = init_animation();
+ charger->batt_anim = anim;
+
+ ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown);
if (ret < 0) {
- LOGE("Cannot load battery_fail image\n");
- charger->surf_unknown = NULL;
+ LOGE("Cannot load custom battery_fail image. Reverting to built in.\n");
+ ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
+ if (ret < 0) {
+ LOGE("Cannot load built in battery_fail image\n");
+ charger->surf_unknown = NULL;
+ }
}
- charger->batt_anim = &battery_animation;
-
GRSurface** scale_frames;
int scale_count;
int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text
// chunk). We are using hard-coded frame.disp_time instead.
- ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_fps,
- &scale_frames);
+ ret = res_create_multi_display_surface(anim->animation_file.c_str(),
+ &scale_count, &scale_fps, &scale_frames);
if (ret < 0) {
LOGE("Cannot load battery_scale image\n");
- charger->batt_anim->num_frames = 0;
- charger->batt_anim->num_cycles = 1;
- } else if (scale_count != charger->batt_anim->num_frames) {
+ anim->num_frames = 0;
+ anim->num_cycles = 1;
+ } else if (scale_count != anim->num_frames) {
LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
- scale_count, charger->batt_anim->num_frames);
- charger->batt_anim->num_frames = 0;
- charger->batt_anim->num_cycles = 1;
+ scale_count, anim->num_frames);
+ anim->num_frames = 0;
+ anim->num_cycles = 1;
} else {
- for (i = 0; i < charger->batt_anim->num_frames; i++) {
- charger->batt_anim->frames[i].surface = scale_frames[i];
+ for (i = 0; i < anim->num_frames; i++) {
+ anim->frames[i].surface = scale_frames[i];
}
}
-
ev_sync_key_state(set_key_callback, charger);
charger->next_screen_transition = -1;