#include "shared/device-notifier.h"
#include "shared/apps.h"
-
-#define METHOD_LOW_NOTI_ON "BatteryLowNotiOn"
-#define METHOD_LOW_NOTI_UPDATE "BatteryLowNotiUpdate"
-#define METHOD_LOW_NOTI_OFF "BatteryLowNotiOff"
-#define METHOD_CRITICAL_NOTI_ON "BatteryCriticalNotiOn"
-#define METHOD_CRITICAL_NOTI_UPDATE "BatteryCriticalNotiUpdate"
-#define METHOD_CRITICAL_NOTI_OFF "BatteryCriticalNotiOff"
-
#define REMOVE_POPUP "remove_battery_popups"
-enum event_noti_type {
- NOTI_NONE = -1,
- NOTI_UPDATE = 0,
-};
-
static struct display_plugin *disp_plgn;
static struct battery_status *battery;
static guint abnormal_timer;
-static int noti_low = NOTI_NONE;
-static int noti_crit = NOTI_NONE;
-
-static bool check_remove_condition(const char *prev, const char *cur)
-{
- int prev_len;
- int cur_len;
- bool ret = false;
-
- prev_len = strlen(prev);
- cur_len = strlen(cur);
-
- if (!prev_len || !cur_len)
- return false;
-
- if (!strncmp(prev, EVT_VAL_BATTERY_LEVEL_CRITICAL,prev_len)) {
- if (!strncmp(cur, EVT_VAL_BATTERY_LEVEL_LOW, cur_len)
- || !strncmp(cur, EVT_VAL_BATTERY_LEVEL_HIGH, cur_len)
- || !strncmp(cur, EVT_VAL_BATTERY_LEVEL_FULL, cur_len))
- ret = true;
- } else if (!strncmp(prev, EVT_VAL_BATTERY_LEVEL_LOW, prev_len)) {
- if (!strncmp(cur, EVT_VAL_BATTERY_LEVEL_HIGH, cur_len)
- || !strncmp(cur, EVT_VAL_BATTERY_LEVEL_FULL, cur_len))
- ret = true;
- }
-
- return ret;
-}
-
-static void low_noti_cb(GVariant *var, void *user_data, GError *err)
-{
-
- if (!var) {
- _E("Invalid parameter.");
- return;
- }
-
- if (!g_variant_get_safe(var, "(i)", ¬i_low)) {
- _E("Failed to notify low: no message(%s)", g_variant_get_type_string(var));
- goto out;
- }
-
- _D("Inserted battery low noti : %d", noti_low);
-
-out:
- g_variant_unref(var);
-}
-
-static void critical_noti_cb(GVariant *var, void *user_data, GError *err)
-{
- int id = 0;
-
- if (!var)
- return;
-
- if (!g_variant_get_safe(var, "(i)", &id)) {
- _E("Failed to notify critical: no message(%s)", g_variant_get_type_string(var));
- goto out;
- }
-
- noti_crit = id;
- _D("Inserted battery critical noti : %d", noti_crit);
-
-out:
- g_variant_unref(var);
-}
-
-static int launch_lowbat_noti(int capacity, int option)
-{
- int ret = -EINVAL;
- char batcap[10] = {0,};
- char notiid[10] = {0,};
- int noti_id;
- const char *pa[2];
- int retry;
- char *noti_type = NULL;
- static int prev_level = BATTERY_NORMAL;
-
- snprintf(batcap, sizeof(batcap), "%d", capacity);
- pa[0] = batcap;
-
- if (option == BAT_OPT_WARNING) {
- if (noti_crit > 0) {
- /* remove waring notiid*/
- noti_id = noti_crit;
- for (retry = RETRY_MAX; retry > 0; retry--) {
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- METHOD_LOW_NOTI_OFF,
- g_variant_new("(i)", noti_id));
- if (ret == 0) {
- _D("Deleted battery critical noti.");
- break;
- } else
- _E("Failed to call dbus method (err: %d)", ret);
- }
- }
- memset(notiid, 0x0, sizeof(notiid));
- snprintf(notiid, sizeof(notiid), "%d", noti_low);
- pa[1] = notiid;
- if (prev_level == battery_info.critical || noti_low == NOTI_UPDATE)
- noti_type = METHOD_LOW_NOTI_UPDATE;
- else {
- noti_type = METHOD_LOW_NOTI_ON;
- syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
- DEVICED_DISPLAY_ATTR_TUPLE2_CURRENT_STATE,
- SYSCOMMON_DEVICED_DISPLAY_STATE_ON, DEVICED_EVENT_BATTERY_CAPACITY_LOW);
- }
- prev_level = battery_info.warning;
- if (battery->charge_now)
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- noti_type,
- g_variant_new("(ss)", pa[0], pa[1]));
- else
- ret = gdbus_call_async_with_reply(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- noti_type,
- g_variant_new("(ss)", pa[0], pa[1]),
- low_noti_cb, -1, NULL);
- } else if (option == BAT_OPT_CRITICAL) {
- if (noti_low > 0) {
- /* remove waring notiid*/
- noti_id = noti_low;
- for (retry = RETRY_MAX; retry > 0; retry--) {
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- METHOD_CRITICAL_NOTI_OFF,
- g_variant_new("(i)", noti_id));
- if (ret == 0) {
- _D("Deleted battery low noti");
- break;
- } else
- _E("Failed to call dbus method (err: %d)", ret);
- }
- }
- memset(notiid, 0x0, sizeof(notiid));
- snprintf(notiid, sizeof(notiid), "%d", noti_crit);
- pa[1] = notiid;
- if (noti_crit == NOTI_UPDATE)
- noti_type = METHOD_CRITICAL_NOTI_UPDATE;
- else {
- noti_type = METHOD_CRITICAL_NOTI_ON;
- syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
- DEVICED_DISPLAY_ATTR_TUPLE2_CURRENT_STATE,
- SYSCOMMON_DEVICED_DISPLAY_STATE_ON, DEVICED_EVENT_BATTERY_CAPACITY_LOW);
- }
- prev_level = battery_info.critical;
- if (battery->charge_now)
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- noti_type,
- g_variant_new("(ss)", pa[0], pa[1]));
- else
- ret = gdbus_call_async_with_reply(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- noti_type,
- g_variant_new("(ss)", pa[0], pa[1]),
- critical_noti_cb, -1, NULL);
-
- } else
- prev_level = battery_info.normal;
-
- _D("Requested type(%s) id(%s)", noti_type, notiid);
- return ret;
-}
-
-static void clean_lowbat_noti(const char *prev, const char *str)
-{
- int noti_id;
- int retry;
- int ret;
- bool condition;
-
- if (!prev || !str) {
- _E("There is no data.");
- return;
- }
-
- if (prev == str && !battery->charge_now)
- return;
-
- condition = check_remove_condition(prev, str);
- _D("Condition: %d", condition);
-
- if (condition || battery->charge_now) {
- if (noti_crit > 1) {
- /* remove critical notiid */
- noti_id = noti_crit;
- for (retry = RETRY_MAX; retry > 0; retry--) {
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- METHOD_CRITICAL_NOTI_OFF,
- g_variant_new("(i)", noti_id));
- if (ret == 0) {
- _D("Deleted battery critical noti.");
- noti_crit = NOTI_NONE;
- break;
- } else
- _E("Failed to call dbus method: %d", ret);
- }
- }
-
- if (noti_low > 1) {
- /* remove waring notiid*/
- noti_id = noti_low;
- for (retry = RETRY_MAX; retry > 0; retry--) {
- ret = gdbus_call_async(POPUP_BUS_NAME,
- POPUP_PATH_BATTERY,
- POPUP_INTERFACE_BATTERY,
- METHOD_LOW_NOTI_OFF,
- g_variant_new("(i)", noti_id));
- if (ret == 0) {
- _D("Deleted battery low noti.");
- noti_low = NOTI_NONE;
- break;
- } else
- _E("Failed to call dbus method: %d", ret);
- }
- }
- }
- _D("Noti_crit(%d) noti_low(%d).", noti_crit, noti_low);
-}
-
static void remove_health_popup(void)
{
int ret;
return;
_D("Add plugins for battery notification.");
- plugin->lowbat_noti_launch = launch_lowbat_noti;
- plugin->lowbat_noti_clean = clean_lowbat_noti;
-
plugin->remove_health_popup = remove_health_popup;
plugin->check_power_supply_noti = check_power_supply_noti;