Check watch viewer state when resume 90/135790/3
authorHyunho Kang <hhstark.kang@samsung.com>
Mon, 26 Jun 2017 11:56:34 +0000 (20:56 +0900)
committerHyunho Kang <hhstark.kang@samsung.com>
Tue, 27 Jun 2017 09:42:29 +0000 (18:42 +0900)
Resume watch app only if viewer state is resumed.

Change-Id: I56dc876868caf2a3565412be17ad76b772d01358
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
src/appcore-watch.c

index e20b43f..74ebf1a 100755 (executable)
@@ -37,6 +37,7 @@
 #include <widget_app.h>
 #include <screen_connector_provider.h>
 #include <pkgmgr-info.h>
+#include <aul_app_com.h>
 
 #include "appcore-watch.h"
 #include "appcore-watch-log.h"
@@ -67,6 +68,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"
 
 static Ecore_Timer *watch_tick = NULL;
 static alarm_id_t alarm_id = 0;
@@ -75,6 +77,7 @@ static watch_app_ambient_tick_type_e ambient_tick_type
 
 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;
 
 /**
  * Appcore internal system event
@@ -111,6 +114,18 @@ enum watch_event {
        WE_MAX
 };
 
+enum watch_visible_state {
+       WVS_UNKNOWN,
+       WVS_PAUSE,
+       WVS_RESUME
+};
+
+enum viewer_visible_state{
+       WATCH_CONTROL_UNKNOWN,
+       WATCH_CONTROL_PAUSE,
+       WATCH_CONTROL_RESUME
+};
+
 static enum watch_core_event to_ae[SE_MAX] = {
        WATCH_CORE_EVENT_UNKNOWN,       /* SE_UNKNOWN */
        WATCH_CORE_EVENT_LOW_MEMORY,    /* SE_LOWMEM */
@@ -150,9 +165,10 @@ struct watch_priv {
        const char *name;
        pid_t pid;
        enum watch_state state;
+       enum watch_visible_state watch_visibility;
+       enum viewer_visible_state viewer_visibility;
        int ambient_mode;
        int ambient_mode_skip_resume;
-
        struct watchcore_ops *ops;
 };
 
@@ -390,6 +406,8 @@ static int __set_data(struct watch_priv *watch, const char *appid,
        watch->appid = strdup(appid);
        watch->name = __get_domain_name(appid);
        watch->pid = getpid();
+       watch->viewer_visibility = WATCH_CONTROL_UNKNOWN;
+       watch->watch_visibility = WVS_UNKNOWN;
 
        return 0;
 }
@@ -837,6 +855,23 @@ static void __get_timeinfo(struct watch_time_s *timeinfo)
        ucal_close(cal);
 }
 
+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);
+       priv.viewer_visibility = *state;
+       _D("set viewer state to %d", *state);
+
+       if (*state == WATCH_CONTROL_RESUME && priv.watch_visibility == WVS_RESUME)
+               __do_app(WE_RESUME, &priv, NULL);
+       else if (*state == WATCH_CONTROL_PAUSE)
+               __do_app(WE_PAUSE, &priv, NULL);
+
+       return 0;
+}
+
 int watch_core_init(const char *name, const struct watch_ops *ops,
                int argc, char **argv)
 {
@@ -874,6 +909,12 @@ int watch_core_init(const char *name, const struct watch_ops *ops,
                goto err;
        }
 
+       r = aul_app_com_create(priv.appid, NULL, __viewer_state_handler, NULL, &__conn_viewer_state);
+       if (r < 0) {
+               _E("failed to create app com viewer state endpoint : %s", priv.appid);
+               goto err;
+       }
+
        core.ops = ops;
        core.init = 1;
 
@@ -961,7 +1002,7 @@ static Eina_Bool __watch_core_time_tick(void *data)
                sec = __get_next_tick_sec();
                watch_tick = ecore_timer_add(sec, __watch_core_time_tick, NULL);
 
-               _D("next time tick: %f, type %d, ticks %d", sec, app_tick_type, app_tick_resolution);
+               _D("next time tick: %f, type %d, ticks %d", sec, app_tick_resolution, app_tick_type);
                priv.ops->time_tick(&timeinfo, priv.ops->data);
        }
 
@@ -1074,6 +1115,7 @@ static int __widget_pause(const char *id, void *data)
 {
        _D("widget_pause");
 
+       priv.watch_visibility = WVS_PAUSE;
        __do_app(WE_PAUSE, &priv, NULL);
 
        return WIDGET_ERROR_NONE;
@@ -1084,8 +1126,12 @@ static int __widget_pause(const char *id, void *data)
 static int __widget_resume(const char *id, void *data)
 {
        _D("widget_resume");
+       priv.watch_visibility = WVS_RESUME;
 
-       __do_app(WE_RESUME, &priv, NULL);
+       if (priv.viewer_visibility == WATCH_CONTROL_RESUME)
+               __do_app(WE_RESUME, &priv, NULL);
+       else
+               _D("viewer is paused, do not resume watch");
 
        return WIDGET_ERROR_NONE;
 }
@@ -1174,10 +1220,16 @@ static Eina_Bool __visibility_cb(void *data, int type, void *event)
 
        _D("visibility %d %d", (unsigned int)ev->win, (unsigned int)ev->fully_obscured);
 
-       if (ev->fully_obscured)
+       if (ev->fully_obscured) {
+               priv.watch_visibility = WVS_PAUSE;
                __do_app(WE_PAUSE, &priv, NULL);
-       else
-               __do_app(WE_RESUME, &priv, NULL);
+       } else {
+               priv.watch_visibility = WVS_RESUME;
+               if (priv.viewer_visibility == WATCH_CONTROL_RESUME)
+                       __do_app(WE_RESUME, &priv, NULL);
+               else
+                       _D("viewer is paused, do not resume watch");
+       }
 
        return ECORE_CALLBACK_RENEW;
 }
@@ -1273,6 +1325,12 @@ static void __after_loop(struct watch_priv *watch)
        if (watch->ops && watch->ops->terminate)
                watch->ops->terminate(watch->ops->data);
 
+       if (__conn_viewer_state) {
+               if (aul_app_com_leave(__conn_viewer_state) < 0)
+                       _E("failed to leave app com endpoint __conn_viewer_state");
+               __conn_viewer_state = NULL;
+       }
+
        screen_connector_provider_fini();
 
        elm_shutdown();