Fix Coding rule non-compliance
[apps/native/volume-app.git] / src / key_event.c
index 4d0b8b9..20c88bd 100755 (executable)
@@ -21,6 +21,7 @@
 #include <bluetooth.h>
 #include <bluetooth_internal.h>
 #include <bluetooth_extension.h>
+#include <device/display.h>
 
 #include "main.h"
 #include "_util_log.h"
 #include "timer.h"
 #include "key_event.h"
 #include "bt.h"
+#include "earphone.h"
 
 #define VCONFKEY_ALARM_VOLUME_POPUP_ENABLE  "memory/alarm/volume_popup_enable"
 #define VOLUME_INPUT_WIN_NAME "volumekey-input-window"
+#define SAFETY_LIMIT 9
 
 struct _key_event_s_info {
        Ecore_X_Window input_win;
@@ -68,8 +71,8 @@ static struct _key_event_s_info key_event_info = {
 static Eina_Bool _key_release_cb(void *data, int type, void *event);
 static Eina_Bool _key_press_cb(void *data, int type, void *event);
 static volume_error_e _volume_popup_check_in_alarm_type(sound_type_e sound_type);
-static volume_error_e _volume_down_key_press(sound_type_e sound_type, int sound, bool bt_opened);
-static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, bool bt_opened);
+static volume_error_e _volume_down_key_press(int status, sound_type_e sound_type, int sound, bool bt_opened, int earphone_connected);
+static volume_error_e _volume_up_key_press(int status, sound_type_e sound_type, int sound, bool bt_opened, int earphone_connected);
 static volume_error_e _mute_key_press();
 
 Ecore_Event_Handler* volume_key_event_handler_volume_up_get(void)
@@ -89,10 +92,10 @@ Eina_Bool volume_key_event_is_pressing_get(void)
 
 void volume_key_event_handler_add(void)
 {
-       if(!key_event_info.handler_volume_up)
+       if (!key_event_info.handler_volume_up)
                key_event_info.handler_volume_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
 
-       if(!key_event_info.handler_volume_down)
+       if (!key_event_info.handler_volume_down)
                key_event_info.handler_volume_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
 }
 
@@ -100,7 +103,7 @@ void volume_key_event_handler_del(void)
 {
        ret_if(key_event_info.handler_volume_up == NULL);
        ecore_event_handler_del(key_event_info.handler_volume_up);
-       key_event_info.handler_volume_up= NULL;
+       key_event_info.handler_volume_up = NULL;
 
        ret_if(key_event_info.handler_volume_down == NULL);
        ecore_event_handler_del(key_event_info.handler_volume_down);
@@ -114,6 +117,7 @@ static volume_error_e _mute_key_press()
        int status = 0;
        int sound = 0;
        int error = 0;
+       int earphone_connected = 0;
        bool bt_opened = false;
        sound_type_e sound_type = 0;
 
@@ -124,12 +128,14 @@ static volume_error_e _mute_key_press()
        _D("sound status : %d", sound);
 
        error = bt_ag_is_sco_opened(&bt_opened);
-       if(error != BT_ERROR_NONE)
+       if (error != BT_ERROR_NONE)
                _E("bt_ag_is_sco_opened return [%d]", error);
 
-       _D("BT state %d", bt_opened);
+       earphone_connected = earphone_get_earphone_is_connected();
 
-       volume_control_show_view(status, sound_type, sound, bt_opened);
+       _D("BT state %d, Earphone state: %d", bt_opened, earphone_connected);
+
+       volume_control_show_view(status, sound_type, sound, bt_opened, earphone_connected);
 
        if (sound_type == SOUND_TYPE_MEDIA) {
                if (key_event_info.is_mute == EINA_FALSE) {
@@ -168,7 +174,7 @@ static volume_error_e _mute_key_press()
        }
 }
 
-static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, bool bt_opened)
+static volume_error_e _volume_up_key_press(int status, sound_type_e sound_type, int sound, bool bt_opened, int earphone_connected)
 {
        int sound_step = 0;
        int sound_level = 0;
@@ -197,8 +203,7 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
                                _D("mute -> vib.");
                                volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
                                volume_sound_vib_play();
-                       }
-                       else {
+                       } else {
                                _D("vib -> sound");
                                volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
                                volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
@@ -206,8 +211,7 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
                                volume_view_slider_value_set(sound_level+1);
                                _D("new sound value: %d", sound_level+1);
                        }
-               }
-               else {
+               } else {
                        /*adjust the sound level normally */
                        if (sound_level != sound_step) {
                                volume_sound_level_set(sound_type, sound_level+1);
@@ -215,14 +219,12 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
                                _D("new sound value: %d", sound_level+1);
                        }
                }
-       }
-       else if (sound_type == SOUND_TYPE_NOTIFICATION) {
+       } else if (sound_type == SOUND_TYPE_NOTIFICATION) {
                if (!sound) {
                        /* No sound in notification type. */
                        volume_view_slider_value_set(0);
                        elm_object_disabled_set(volume_view_slider_get(), EINA_TRUE);
-               }
-               else {
+               } else {
                        /*adjust the sound level normally */
                        if (sound_level != sound_step) {
                                volume_sound_level_set(sound_type, sound_level+1);
@@ -230,9 +232,8 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
                                _D("new sound value: %d", sound_level+1);
                        }
                }
-       }
+       } else if (sound_type == SOUND_TYPE_CALL && bt_opened) {
        /* Sound type is not ringtone. Need to adjust sound level */
-       else if (sound_type == SOUND_TYPE_CALL && bt_opened) {
                int bt_vol = 0;
                if (bt_ag_get_speaker_gain(&bt_vol) != BT_ERROR_NONE)
                        _E("Getting bt volume is failed");
@@ -240,13 +241,42 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
                _D("BT VOLUME : %d", bt_vol);
 
                if (bt_vol != sound_step) {
-                       if(bt_ag_notify_speaker_gain(bt_vol+1) != BT_ERROR_NONE)
+                       if (bt_ag_notify_speaker_gain(bt_vol+1) != BT_ERROR_NONE)
                                _E("Setting bt volume is failed");
                        volume_view_slider_value_set(bt_vol+1);
                        _D("New BT VOLUME : %d", bt_vol+1);
                }
-       }
-       else {
+       } else {
+               if (sound_type == SOUND_TYPE_MEDIA && (bt_opened || earphone_connected) && sound_level + 1 > SAFETY_LIMIT) {
+                       if (volume_control_get_safety_limit()) {
+                               display_state_e state;
+                               volume_sound_level_set(sound_type, SAFETY_LIMIT);
+                               volume_view_slider_value_set(SAFETY_LIMIT);
+                               if (VOLUME_ERROR_OK != volume_view_send_warning_signal(EINA_TRUE))
+                                       _E("Failed to set warning status");
+
+                               if (VOLUME_ERROR_OK != volume_view_open_warning_popup()) {
+                                       _E("Failed to open waring popup!!");
+                                       return VOLUME_ERROR_FAIL;
+                               }
+                               if (device_display_get_state(&state) != DEVICE_ERROR_NONE)
+                                       _E("Failed to get display state");
+
+                               if (state == DISPLAY_STATE_SCREEN_OFF || state == DISPLAY_STATE_SCREEN_DIM) {
+                                       device_display_change_state(DISPLAY_STATE_NORMAL);
+                                       if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_WAKEUP))
+                                               _E("Failed to play vibration");
+                               }
+                               if (status == LOCK_AND_MEDIA) {
+                                       /* Check LCD */
+                                       volume_view_window_show(sound_type);
+                               }
+                               volume_timer_del(TYPE_TIMER_SU);
+                               volume_timer_del(TYPE_TIMER_SD);
+                               volume_timer_add(10.0, TYPE_TIMER_WARNING_POPUP);
+                               return VOLUME_ERROR_OK;
+                       }
+               }
                if (sound_level != sound_step) {
                        volume_sound_level_set(sound_type, sound_level+1);
                        volume_view_slider_value_set(sound_level+1);
@@ -269,7 +299,7 @@ static volume_error_e _volume_up_key_press(sound_type_e sound_type, int sound, b
        return VOLUME_ERROR_OK;
 }
 
-static volume_error_e _volume_down_key_press(sound_type_e sound_type, int sound, bool bt_opened)
+static volume_error_e _volume_down_key_press(int status, sound_type_e sound_type, int sound, bool bt_opened, int earphone_connected)
 {
        key_event_info.is_mute = EINA_FALSE;
 
@@ -285,7 +315,7 @@ static volume_error_e _volume_down_key_press(sound_type_e sound_type, int sound,
        if (elm_object_disabled_get(volume_view_slider_get()))
                elm_object_disabled_set(volume_view_slider_get(), EINA_FALSE);
 
-       if(sound_type == SOUND_TYPE_RINGTONE){
+       if (sound_type == SOUND_TYPE_RINGTONE) {
                if (!sound)
                        /* Check sound status change case. */
                        _D("Do nothing.");
@@ -301,14 +331,12 @@ static volume_error_e _volume_down_key_press(sound_type_e sound_type, int sound,
                                }
                        }
                }
-       }
-       else if (sound_type == SOUND_TYPE_NOTIFICATION) {
-               if(!sound) {
+       } else if (sound_type == SOUND_TYPE_NOTIFICATION) {
+               if (!sound) {
                        /* No sound in notification type. */
                        volume_view_slider_value_set(0);
                        elm_object_disabled_set(volume_view_slider_get(), EINA_TRUE);
-               }
-               else {
+               } else {
                        /*adjust the sound level normally */
                        if (val != 0) {
                                volume_sound_level_set(sound_type, val-1);
@@ -316,29 +344,54 @@ static volume_error_e _volume_down_key_press(sound_type_e sound_type, int sound,
                                _D("new sound value: %d", val-1);
                        }
                }
-       }
-       else if(sound_type == SOUND_TYPE_CALL && bt_opened) {
+       } else if (sound_type == SOUND_TYPE_CALL && bt_opened) {
                int bt_vol = 0;
-               if(bt_ag_get_speaker_gain(&bt_vol) != BT_ERROR_NONE)
+               if (bt_ag_get_speaker_gain(&bt_vol) != BT_ERROR_NONE)
                        _E("Getting bt volume is failed");
 
                _D("BT VOLUME : %d", bt_vol);
-               if(bt_ag_notify_speaker_gain(bt_vol-1) != BT_ERROR_NONE)
+               if (bt_ag_notify_speaker_gain(bt_vol-1) != BT_ERROR_NONE)
                        _E("Setting bt volume is failed");
                volume_view_slider_value_set(bt_vol-1);
 
                _D("New BT VOLUME : %d", bt_vol-1);
-       }
+       } else {
        /* Sound type is not ringtone. Need to adjust sound level */
-       else {
-               if (val != 0) {
+               if (volume_control_get_safety_limit() && sound_type == SOUND_TYPE_MEDIA
+                       && (bt_opened || earphone_connected) && val > SAFETY_LIMIT) {
+                       display_state_e state;
+                       volume_sound_level_set(sound_type, SAFETY_LIMIT);
+                       volume_view_slider_value_set(SAFETY_LIMIT);
+                       if (VOLUME_ERROR_OK != volume_view_send_warning_signal(EINA_TRUE))
+                               _E("Failed to set warning status");
+
+                       if (VOLUME_ERROR_OK != volume_view_open_warning_popup()) {
+                               _E("Failed to open waring popup!!");
+                               return VOLUME_ERROR_FAIL;
+                       }
+                       if (device_display_get_state(&state) != DEVICE_ERROR_NONE)
+                               _E("Failed to get display state");
+
+                       if (state == DISPLAY_STATE_SCREEN_OFF || state == DISPLAY_STATE_SCREEN_DIM) {
+                               device_display_change_state(DISPLAY_STATE_NORMAL);
+                               if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_WAKEUP))
+                                       _E("Failed to play vibration");
+                       }
+                       if (status == LOCK_AND_MEDIA)
+                               /* Check LCD */
+                               volume_view_window_show(sound_type);
+
+                       volume_timer_del(TYPE_TIMER_SU);
+                       volume_timer_del(TYPE_TIMER_SD);
+                       volume_timer_add(10.0, TYPE_TIMER_WARNING_POPUP);
+               } else if (val != 0) {
                        volume_sound_level_set(sound_type, val - 1);
                        volume_view_slider_value_set(val - 1);
                        _D("new sound value: %d", val-1);
                }
        }
 
-       if(sound_type != SOUND_TYPE_ALARM)
+       if (sound_type != SOUND_TYPE_ALARM)
                volume_sound_play();
 
        volume_timer_del(TYPE_TIMER_SD);
@@ -352,15 +405,15 @@ static volume_error_e _volume_popup_check_in_alarm_type(sound_type_e sound_type)
 {
        int is_enabled = 0;
 
-       if(sound_type == SOUND_TYPE_ALARM) {
+       if (sound_type == SOUND_TYPE_ALARM) {
                _D("Sound type is Alarm Type");
-               if(vconf_get_bool(VCONFKEY_ALARM_VOLUME_POPUP_ENABLE, &is_enabled) < 0) {
+               if (vconf_get_bool(VCONFKEY_ALARM_VOLUME_POPUP_ENABLE, &is_enabled) < 0) {
                        _E("Failed to get vconfkey : VCONFKEY_ALARM_VOLUME_POPUP_ENABLE");
                        return VOLUME_ERROR_FAIL;
                }
                _D("volume popup enabled in alarm type : %d", is_enabled);
 
-               if(!is_enabled) {
+               if (!is_enabled) {
                        _D("alarm type but vconf for the volume popup is disabled");
                        return VOLUME_ERROR_FAIL;
                }
@@ -376,6 +429,7 @@ static Eina_Bool _key_press_cb(void *data, int type, void *event)
        int key_status = 0;
        int status = 0;
        int error = 0;
+       int earphone_connected = 0;
        bool bt_opened = false;
        sound_type_e sound_type = 0;
        Evas_Object *win = NULL;
@@ -389,17 +443,27 @@ static Eina_Bool _key_press_cb(void *data, int type, void *event)
        win = volume_view_win_get();
        retv_if(!win, ECORE_CALLBACK_CANCEL);
 
-       if(!strncmp(ev->keyname, KEY_CANCEL, strlen(KEY_CANCEL)) || !strncmp(ev->keyname, KEY_BACK, strlen(KEY_BACK))) {
+       if (volume_view_warning_popup_get()) {
+               _D("Safety warning popup is exist");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (!volume_control_get_safety_limit() && volume_control_get_time_for_safety_limit()) {
+               _D("Already passed 20 hour after checking warning popup, open warning again");
+               volume_control_set_safety_limit(EINA_TRUE);
+       }
+
+       if (!strncmp(ev->keyname, KEY_CANCEL, strlen(KEY_CANCEL)) || !strncmp(ev->keyname, KEY_BACK, strlen(KEY_BACK))) {
                _D("%s is pressed", ev->keyname);
                return ECORE_CALLBACK_CANCEL;
        }
 
-       if(volume_view_is_slider_touching_get()) {
+       if (volume_view_is_slider_touching_get()) {
                _E("Failed to show volume : is_slider_touching is EINA_TRUE");
                return ECORE_CALLBACK_CANCEL;
        }
 
-       if(vconf_get_int(VCONFKEY_STARTER_USE_VOLUME_KEY, &key_status) < 0) {
+       if (vconf_get_int(VCONFKEY_STARTER_USE_VOLUME_KEY, &key_status) < 0) {
                _E("Failed to get vconf : VCONFKEY_STATER_USE_VOLUME_KEY");
                return ECORE_CALLBACK_CANCEL;
        }
@@ -407,18 +471,18 @@ static Eina_Bool _key_press_cb(void *data, int type, void *event)
 
 #ifndef FEATURE_SDK
        int is_call = -1;
-       if(!strncmp(ev->keyname, KEY_MUTE, strlen(KEY_MUTE))) {
+       if (!strncmp(ev->keyname, KEY_MUTE, strlen(KEY_MUTE))) {
                _D("MUTE key is pressed");
-               if(VOLUME_ERROR_OK != _mute_key_press())
+               if (VOLUME_ERROR_OK != _mute_key_press())
                        _E("Failed to press MUTE key");
                return ECORE_CALLBACK_CANCEL;
        }
 
-       if(vconf_get_int(VCONFKEY_TELEPHONY_CALL_STATE, &is_call) < 0 || is_call < 0) {
+       if (vconf_get_int(VCONFKEY_TELEPHONY_CALL_STATE, &is_call) < 0 || is_call < 0) {
                _E("Failed to get call state vconf");
                return ECORE_CALLBACK_CANCEL;
        }
-       if(is_call>0) {
+       if (is_call > 0) {
                _D("Call is active");
                return ECORE_CALLBACK_CANCEL;
        }
@@ -431,28 +495,28 @@ static Eina_Bool _key_press_cb(void *data, int type, void *event)
        _D("sound status : %d", sound);
 
        error = bt_ag_is_sco_opened(&bt_opened);
-       if(error != BT_ERROR_NONE)
+       if (error != BT_ERROR_NONE)
                _E("bt_ag_is_sco_opened return [%d]", error);
 
-       _D("BT state %d", bt_opened);
+       earphone_connected = earphone_get_earphone_is_connected();
+       _D("module state - bt: %d, earphone: %d", bt_opened, earphone_connected);
 
-       if(VOLUME_ERROR_OK != _volume_popup_check_in_alarm_type(sound_type)) {
+       if (VOLUME_ERROR_OK != _volume_popup_check_in_alarm_type(sound_type)) {
                _E("Failed to set volume popup");
                return ECORE_CALLBACK_CANCEL;
        }
 
-       volume_control_show_view(status, sound_type, sound, bt_opened);
+       volume_control_show_view(status, sound_type, sound, bt_opened, earphone_connected);
 
        key_event_info.is_pressing = EINA_TRUE;
 
        volume_timer_del(TYPE_TIMER_POPUP);
 
        if (!strncmp(ev->keyname, KEY_VOLUMEUP, strlen(KEY_VOLUMEUP))) {
-               if(VOLUME_ERROR_OK != _volume_up_key_press(sound_type, sound, bt_opened))
+               if (VOLUME_ERROR_OK != _volume_up_key_press(status, sound_type, sound, bt_opened, earphone_connected))
                        _E("Failed to press volume up key");
-       }
-       else if (!strncmp(ev->keyname, KEY_VOLUMEDOWN, strlen(KEY_VOLUMEDOWN))) {
-               if(VOLUME_ERROR_OK != _volume_down_key_press(sound_type, sound, bt_opened))
+       } else if (!strncmp(ev->keyname, KEY_VOLUMEDOWN, strlen(KEY_VOLUMEDOWN))) {
+               if (VOLUME_ERROR_OK != _volume_down_key_press(status, sound_type, sound, bt_opened, earphone_connected))
                        _E("Failed to press volume down key");
        }
 
@@ -470,16 +534,16 @@ static Eina_Bool _key_release_cb(void *data, int type, void *event)
 
        key_event_info.is_pressing = EINA_FALSE;
 
-       if(!strncmp(ev->keyname, KEY_CANCEL, strlen(KEY_CANCEL))) {
+       if (!strncmp(ev->keyname, KEY_CANCEL, strlen(KEY_CANCEL))) {
                _D("%s is released", ev->keyname);
-               if(VOLUME_ERROR_OK != volume_control_hide_view())
+               if (VOLUME_ERROR_OK != volume_control_hide_view())
                        _E("Failed to close volume");
-               if(VOLUME_ERROR_OK != volume_control_cache_flush())
+               if (VOLUME_ERROR_OK != volume_control_cache_flush())
                        _E("Failed to flush cache");
                return ECORE_CALLBACK_CANCEL;
        }
 
-       if(!strncmp(ev->keyname, KEY_BACK, strlen(KEY_BACK))) {
+       if (!strncmp(ev->keyname, KEY_BACK, strlen(KEY_BACK))) {
                _D("BACK Key is released");
                return ECORE_CALLBACK_CANCEL;
        }
@@ -487,16 +551,17 @@ static Eina_Bool _key_release_cb(void *data, int type, void *event)
        if (!strncmp(ev->keyname, KEY_VOLUMEUP, strlen(KEY_VOLUMEUP))) {
                _D("up key released and del timer");
                volume_timer_del(TYPE_TIMER_SU);
-       }
-       else if (!strncmp(ev->keyname, KEY_VOLUMEDOWN, strlen(KEY_VOLUMEDOWN))) {
+       } else if (!strncmp(ev->keyname, KEY_VOLUMEDOWN, strlen(KEY_VOLUMEDOWN))) {
                _D("down key released and del timer");
                volume_timer_del(TYPE_TIMER_SD);
        }
 
        volume_timer_del(TYPE_TIMER_POPUP);
 
-       if(volume_view_is_slider_touching_get() == EINA_FALSE)
-               volume_timer_add(3.0, TYPE_TIMER_POPUP);
+       if (volume_view_is_slider_touching_get() == EINA_FALSE) {
+               if (!volume_view_warning_popup_get())
+                       volume_timer_add(3.0, TYPE_TIMER_POPUP);
+       }
 
        _D("key release fini");
        return ECORE_CALLBACK_CANCEL;