Add low and critical battery popup
[platform/core/system/system-popup.git] / src / battery / battery-wearable.c
old mode 100755 (executable)
new mode 100644 (file)
index ff8ef29..8ac5591
 
 #include "popup-common.h"
 
-#define DEVICED_PATH_SYSNOTI        "/Org/Tizen/System/DeviceD/SysNoti"
-#define DEVICED_INTERFACE_SYSNOTI   "org.tizen.system.deviced.SysNoti"
 #define SIGNAL_CHARGEERR_RESPONSE   "ChargeErrResponse"
 
-static const struct popup_ops lowbattery_warning_ops;
-static const struct popup_ops lowbattery_critical_ops;
 static const struct popup_ops lowbattery_poweroff_ops;
+static const struct popup_ops lowbattery_critical_ops;
+static const struct popup_ops lowbattery_warning_ops;
 static const struct popup_ops charge_error_low_ops;
 static const struct popup_ops charge_error_high_ops;
 static const struct popup_ops battery_disconnected_ops;
@@ -34,14 +32,14 @@ static int lowbattery_launch(bundle *b, const struct popup_ops *ops);
 
 static int remove_other_lowbattery_popups(const struct popup_ops *ops)
 {
-       if (ops != &lowbattery_warning_ops)
-               unload_simple_popup(&lowbattery_warning_ops);
+       if (ops != &lowbattery_poweroff_ops)
+               unload_simple_popup(&lowbattery_poweroff_ops);
 
        if (ops != &lowbattery_critical_ops)
                unload_simple_popup(&lowbattery_critical_ops);
 
-       if (ops != &lowbattery_poweroff_ops)
-               unload_simple_popup(&lowbattery_poweroff_ops);
+       if (ops != &lowbattery_warning_ops)
+               unload_simple_popup(&lowbattery_warning_ops);
 
        return 0;
 }
@@ -71,7 +69,25 @@ static void charger_status_changed(keynode_t *key, void *data)
 
        if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
                                charger_status_changed) < 0)
-               _E("Failed to release vconf key handler");
+               _E("Failed to release vconf key handler.");
+
+       unload_simple_popup(ops);
+
+       terminate_if_no_popup();
+}
+
+static void battery_status_changed(keynode_t *key, void *data)
+{
+       int status;
+       const struct popup_ops *ops = data;
+
+       status = vconf_keynode_get_int(key);
+       if (status < VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
+               return;
+
+       if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
+                               battery_status_changed) < 0)
+               _E("Failed to release vconf key handler.");
 
        unload_simple_popup(ops);
 
@@ -84,18 +100,33 @@ static void unregister_charger_status_handler(void)
                                charger_status_changed);
 }
 
+static void unregister_battery_status_handler(void)
+{
+       vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
+                               battery_status_changed);
+}
+
 static void register_charger_status_handler(const struct popup_ops *ops)
 {
        if (vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
                                charger_status_changed, (void *)ops) < 0)
-               _E("Failed to register vconf key handler");
+               _E("Failed to register vconf key handler.");
+}
+
+static void register_battery_status_handler(const struct popup_ops *ops)
+{
+       if (vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
+                               battery_status_changed, (void *)ops) < 0)
+               _E("Failed to register vconf key handler.");
 }
 
 static int lowbattery_launch(bundle *b, const struct popup_ops *ops)
 {
        unregister_charger_status_handler();
+       unregister_battery_status_handler();
        remove_other_lowbattery_popups(ops);
        register_charger_status_handler(ops);
+       register_battery_status_handler(ops);
 
        return 0;
 }
@@ -103,40 +134,51 @@ static int lowbattery_launch(bundle *b, const struct popup_ops *ops)
 static void lowbattery_terminate(const struct popup_ops *ops)
 {
        unregister_charger_status_handler();
+       unregister_battery_status_handler();
 }
 
 static void poweroff_clicked(const struct popup_ops *ops)
 {
-       _I("Power off the phone");
-       unload_simple_popup(ops);
+       static int bPowerOff = 0;
+       char *param[1];
+       int ret;
+
+       if (bPowerOff == 1)
+               return;
+       bPowerOff = 1;
 
-       if (vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
-                               VCONFKEY_SYSMAN_POWER_OFF_DIRECT) != 0)
-               _E("Failed to request poweroff to deviced");
+       unload_simple_popup(ops);
 
-       terminate_if_no_popup();
+       param[0] = POWEROFF_OPERATION_OFF;
+       ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
+                       POWEROFF_OBJECT_PATH,
+                       POWEROFF_INTERFACE_NAME,
+                       POWEROFF_METHOD,
+                       "s", param);
+       if (ret < 0)
+               _E("Failed to request poweroff to deviced: %d", ret);
 }
 
 static void charge_error_ok_clicked(const struct popup_ops *ops)
 {
-       _I("OK is selected");
+       _I("OK is selected.");
        unload_simple_popup(ops);
 
        if (broadcast_dbus_signal(DEVICED_PATH_SYSNOTI,
                                DEVICED_INTERFACE_SYSNOTI,
                                SIGNAL_CHARGEERR_RESPONSE,
                                NULL, NULL) < 0)
-               _E("Failed to send signal");
+               _E("Failed to send signal.");
 
        terminate_if_no_popup();
 }
 
 static int remove_battery_popups(bundle *b, const struct popup_ops *ops)
 {
-       _I("Remove battery related popups");
+       _I("Remove battery related popups.");
+       unload_simple_popup(&lowbattery_poweroff_ops);
        unload_simple_popup(&lowbattery_critical_ops);
        unload_simple_popup(&lowbattery_warning_ops);
-       unload_simple_popup(&lowbattery_poweroff_ops);
        unload_simple_popup(&charge_error_low_ops);
        unload_simple_popup(&charge_error_high_ops);
        unload_simple_popup(&battery_disconnected_ops);
@@ -144,30 +186,33 @@ static int remove_battery_popups(bundle *b, const struct popup_ops *ops)
        return 0;
 }
 
-static const struct popup_ops lowbattery_warning_ops = {
-       .name           = "lowbattery_warning",
+static const struct popup_ops lowbattery_poweroff_ops = {
+       .name           = "lowbattery_poweroff",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
-       .content        = "IDS_COM_POP_BATTERYLOW",
+       .content        = "IDS_COM_POP_LOW_BATTERY_PHONE_WILL_SHUT_DOWN",
        .left_text      = "IDS_COM_SK_OK",
+       .left           = poweroff_clicked,
        .pre            = lowbattery_launch,
-       .terminate      = lowbattery_terminate,
+       .terminate  = lowbattery_terminate,
 };
 
 static const struct popup_ops lowbattery_critical_ops = {
        .name           = "lowbattery_critical",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
        .content        = "IDS_COM_POP_BATTERYLOW",
        .left_text      = "IDS_COM_SK_OK",
        .pre            = lowbattery_launch,
-       .terminate      = lowbattery_terminate,
+       .terminate  = lowbattery_terminate,
 };
 
-static const struct popup_ops lowbattery_poweroff_ops = {
-       .name           = "lowbattery_poweroff",
+static const struct popup_ops lowbattery_warning_ops = {
+       .name           = "lowbattery_warning",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
-       .content        = "IDS_COM_POP_LOW_BATTERY_PHONE_WILL_SHUT_DOWN",
+       .content        = "IDS_COM_POP_BATTERYLOW",
        .left_text      = "IDS_COM_SK_OK",
-       .left           = poweroff_clicked,
        .pre            = lowbattery_launch,
        .terminate  = lowbattery_terminate,
 };
@@ -179,6 +224,7 @@ static const struct popup_ops battery_remove_ops = {
 
 static const struct popup_ops charge_error_low_ops = {
        .name           = "chargeerrlow",//"charge_error_low",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
        .content        = "IDS_QP_BODY_CHARGING_PAUSED_BATTERY_TEMPERATURE_TOO_LOW",
        .left_text      = "IDS_COM_SK_OK",
@@ -188,6 +234,7 @@ static const struct popup_ops charge_error_low_ops = {
 
 static const struct popup_ops charge_error_high_ops = {
        .name           = "chargeerrhigh",//"charge_error_high",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
        .content        = "IDS_QP_BODY_CHARGING_PAUSED_BATTERY_TEMPERATURE_TOO_HIGH",
        .left_text      = "IDS_COM_SK_OK",
@@ -197,6 +244,7 @@ static const struct popup_ops charge_error_high_ops = {
 
 static const struct popup_ops battery_disconnected_ops = {
        .name           = "battdisconnect",//"battery_disconnected",
+       .pattern        = FEEDBACK_PATTERN_LOWBATT,
        .show   = load_simple_popup,
        .content        = "IDS_COM_POP_BATTERY_DISCONNECTED_ABB",
        .left_text      = "IDS_COM_SK_OK",
@@ -205,9 +253,10 @@ static const struct popup_ops battery_disconnected_ops = {
 /* Constructor to register lowbattery button */
 static __attribute__ ((constructor)) void battery_register_popup(void)
 {
-       register_popup(&lowbattery_warning_ops);
-       register_popup(&lowbattery_critical_ops);
        register_popup(&lowbattery_poweroff_ops);
+       register_popup(&lowbattery_critical_ops);
+       register_popup(&lowbattery_warning_ops);
+       register_popup(&battery_remove_ops);
        register_popup(&charge_error_low_ops);
        register_popup(&charge_error_high_ops);
        register_popup(&battery_disconnected_ops);