Check watch viewer state when resume 79/150579/3
authorHyunho Kang <hhstark.kang@samsung.com>
Mon, 26 Jun 2017 11:56:34 +0000 (20:56 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 18 Sep 2017 04:54:38 +0000 (13:54 +0900)
Resume watch app only if viewer state is resumed.

Change-Id: I56dc876868caf2a3565412be17ad76b772d01358
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/watch_app_main.c

index 079c173..893ab50 100755 (executable)
@@ -48,6 +48,7 @@
 #include <app_control_internal.h>
 #include <bundle_internal.h>
 #include <pkgmgr-info.h>
+#include <aul_app_com.h>
 
 #include "appcore-watch-log.h"
 #include "watch_app_private.h"
@@ -70,6 +71,7 @@
 #define ONE_DAY_IN_SEC         86400
 #define METADATA_TICK_PER_SECOND       "http://developer.samsung.com/tizen/metadata/tickpersecond"
 #define METADATA_MINUTE_TICK           "http://developer.samsung.com/tizen/metadata/minutetick"
+#define WATCH_CONTROL_STATE "watch,state"
 
 typedef enum {
        WATCH_APP_STATE_NOT_RUNNING = 0, /* The application has been launched or was running but was terminated */
@@ -92,14 +94,25 @@ struct _watch_time_s {
        char *timezone;
 };
 
+enum watch_visible_state {
+       WVS_UNKNOWN,
+       WVS_PAUSE,
+       WVS_RESUME
+};
+
+enum viewer_visible_state {
+       WATCH_CONTROL_UNKNOWN,
+       WATCH_CONTROL_PAUSE,
+       WATCH_CONTROL_RESUME
+};
+
 #define WATCH_APP_EVENT_MAX 5
 static Ecore_Timer *watch_tick = NULL;
 static alarm_id_t alarm_id = 0;
-static watch_app_ambient_tick_type_e ambient_tick_type
-                                       = WATCH_APP_AMBIENT_TICK_EVERY_MINUTE;
-
+static watch_app_ambient_tick_type_e ambient_tick_type = WATCH_APP_AMBIENT_TICK_EVERY_MINUTE;
 static watch_app_time_tick_resolution_e app_tick_type = WATCH_APP_TIME_TICKS_PER_SECOND;
 static int app_tick_resolution = 1;
+static aul_app_com_connection_h __conn_viewer_state;
 
 struct app_event_handler {
        app_event_type_e type;
@@ -119,6 +132,8 @@ struct watch_app_context {
        watch_app_lifecycle_callback_s callback;
        int width;
        int height;
+       enum watch_visible_state watch_visibility;
+       enum viewer_visible_state viewer_visibility;
        bool ambient_mode;
        bool ambient_mode_skip_resume;
        void *data;
@@ -515,6 +530,7 @@ static int __on_pause(void *data)
 {
        _W("_watch_core_pause");
 
+       __context.watch_visibility = WVS_PAUSE;
        /* Handling the ambient mode */
        if (__context.ambient_mode)
                __context.ambient_mode_skip_resume = 1;
@@ -534,6 +550,12 @@ static int __on_resume(void *data)
 {
        _W("_watch_core_resume");
 
+       __context.watch_visibility = WVS_RESUME;
+       if (__context.viewer_visibility != WATCH_CONTROL_RESUME) {
+               _D("Viewer is paused, do not resume watch");
+               return APP_ERROR_NONE;
+       }
+
        appcore_efl_base_on_resume();
 
        if (__context.ambient_mode) {
@@ -620,6 +642,26 @@ static void __on_window_show(int type, void *event, void *data)
        appcore_efl_base_resume();
 }
 
+static void __on_window_visibility(int type, void *event, void *data)
+{
+       Ecore_Wl_Event_Window_Visibility_Change *ev = event;
+
+       _D("visibility %u %u",
+                       (unsigned int)ev->win,
+                       (unsigned int)ev->fully_obscured);
+
+       if (ev->fully_obscured) {
+               __context.watch_visibility = WVS_PAUSE;
+               __on_pause(NULL);
+       } else {
+               __context.watch_visibility = WVS_RESUME;
+               if (__context.viewer_visibility == WATCH_CONTROL_RESUME)
+                       __on_resume(NULL);
+               else
+                       _D("Viewer is paused, do not resume watch");
+       }
+}
+
 static int __set_ambient_tick_type(watch_app_ambient_tick_type_e type)
 {
        _D("set ambient tick type : %d", type);
@@ -670,6 +712,26 @@ static int __get_time_tick_frequency(int *ticks, watch_app_time_tick_resolution_
        return 0;
 }
 
+static int __viewer_state_handler(const char *widget_id, aul_app_com_result_e e,
+               bundle *envelope, void *user_data)
+{
+       int *state = NULL;
+       size_t state_sz = 0;
+
+       bundle_get_byte(envelope, WATCH_CONTROL_STATE, (void **)&state,
+                       &state_sz);
+       __context.viewer_visibility = *state;
+       _D("set viewer state to %d", *state);
+
+       if (*state == WATCH_CONTROL_RESUME &&
+                       __context.watch_visibility == WVS_RESUME)
+               __on_resume(NULL);
+       else if (*state == WATCH_CONTROL_PAUSE)
+               __on_pause(NULL);
+
+       return 0;
+}
+
 EXPORT_API int watch_app_main(int argc, char **argv,
                watch_app_lifecycle_callback_s *callback, void *user_data)
 {
@@ -688,6 +750,7 @@ EXPORT_API int watch_app_main(int argc, char **argv,
        ops.ui_base.pause = __on_pause;
        ops.ui_base.resume = __on_resume;
        ops.ui_base.window.show = __on_window_show;
+       ops.ui_base.window.visibility = __on_window_visibility;
 
        kb = bundle_import_from_argv(argc, argv);
        if (kb) {
@@ -723,9 +786,20 @@ EXPORT_API int watch_app_main(int argc, char **argv,
                return watch_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__,
                                "failed to get the appid");
 
+       ret = aul_app_com_create(__context.appid, NULL, __viewer_state_handler,
+                       NULL, &__conn_viewer_state);
+       if (ret < 0) {
+               free(__context.appid);
+               __context.appid = NULL;
+               return watch_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__,
+                               "Failed to create app com");
+       }
+
        __context.callback = *callback;
        __context.width = width;
        __context.height = height;
+       __context.viewer_visibility = WATCH_CONTROL_UNKNOWN;
+       __context.watch_visibility = WVS_UNKNOWN;
        __context.ambient_mode = false;
        __context.ambient_mode_skip_resume = false;
        __context.data = user_data;
@@ -734,6 +808,8 @@ EXPORT_API int watch_app_main(int argc, char **argv,
        ret = appcore_efl_base_init(ops, argc, argv, NULL, 0);
        free(__context.appid);
        __context.appid = NULL;
+       aul_app_com_leave(__conn_viewer_state);
+       __conn_viewer_state = NULL;
 
        if (ret < 0) {
                __context.state = WATCH_APP_STATE_NOT_RUNNING;