Adds new api for sending event to ambient-viewer 87/226387/5
authorInkyun Kil <inkyun.kil@samsung.com>
Mon, 2 Mar 2020 07:18:14 +0000 (16:18 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Tue, 3 Mar 2020 05:00:30 +0000 (14:00 +0900)
Change-Id: Ia64723db52747c201c959158465d102c0d2e0147
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
include/watch_app_internal.h [new file with mode: 0644]
include/watch_base.h
packaging/appcore-watch.spec
src/base/watch_base.c
src/efl_base/watch_app_main.c

diff --git a/include/watch_app_internal.h b/include/watch_app_internal.h
new file mode 100644 (file)
index 0000000..5a4f821
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef __TIZEN_APPFW_WATCH_APP_INTERNAL_H__
+#define __TIZEN_APPFW_WATCH_APP_INTERNAL_H__
+
+#include <bundle.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Enumeration for event.
+ * @remark This function only for internal applications
+ */
+typedef enum {
+       WATCH_APP_AMBIENT_EVENT_PREPARE,
+       WATCH_APP_AMBIENT_EVENT_READY,
+       WATCH_APP_AMBIENT_EVENT_CHANGED
+} watch_app_ambient_event_e;
+
+/**
+ * @brief Sends event with bundle data to the @a receiver.
+ * @remark This function only for internal applications
+ * @param[in] receiver application id of receiver
+ * @param[in] event ambient event type
+ * @param[in] data extra bundle data
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ */
+int watch_app_ambient_send_event(const char *receiver, watch_app_ambient_event_e event, bundle *data);
+
+/**
+ * @brief Gets extra bundle data from ambient-viewer.
+ * @remark This function only for internal applications
+ * @param[out] data extra bundle data
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ */
+int watch_app_ambient_get_extra(bundle **extra);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TIZEN_APPFW_WATCH_APP_INTERNAL_H__ */
\ No newline at end of file
index 22bed01..79c8fac 100755 (executable)
@@ -21,6 +21,7 @@
 #include <time.h>
 #include <app_control.h>
 #include <app_common.h>
+#include <bundle.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -101,6 +102,12 @@ typedef enum {
        WATCH_BASE_AMBIENT_TICK_EVERY_DAY
 } watch_base_ambient_tick_type_e;
 
+typedef enum {
+       WATCH_BASE_AMBIENT_EVENT_PREPARE,
+       WATCH_BASE_AMBIENT_EVENT_READY,
+       WATCH_BASE_AMBIENT_EVENT_CHANGED
+} watch_base_ambient_event_e;
+
 int watch_base_set_ambient_tick_type(watch_base_ambient_tick_type_e type);
 int watch_base_get_ambient_tick_type(watch_base_ambient_tick_type_e *type);
 int watch_base_time_get_current_time(watch_base_time_h *watch_base_time);
@@ -118,6 +125,8 @@ int watch_base_time_get_utc_time(watch_base_time_h watch_base_time, struct tm *u
 int watch_base_time_get_utc_timestamp(watch_base_time_h watch_base_time, time_t *utc_timestamp);
 int watch_base_time_get_time_zone(watch_base_time_h watch_base_time, char **time_zone_id);
 int watch_base_time_get_dst_status(watch_base_time_h watch_base_time, bool *status);
+int watch_base_ambient_send_event(const char *receiver, watch_base_ambient_event_e event, bundle *extra);
+int watch_base_ambient_get_extra(bundle **extra);
 
 #ifdef __cplusplus
 }
index e36ad03..b1ed145 100644 (file)
@@ -133,6 +133,7 @@ cp capi-appfw-watch-application.pc %{buildroot}%{_libdir}/pkgconfig
 %{_includedir}/appcore-watch/watch_app.h
 %{_includedir}/appcore-watch/watch_app_extension.h
 %{_includedir}/appcore-watch/watch_app_efl.h
+%{_includedir}/appcore-watch/watch_app_internal.h
 %{_libdir}/pkgconfig/capi-appfw-watch-application.pc
 %{_libdir}/libappcore-watch.so
 
index 74b657c..5aecff7 100755 (executable)
@@ -112,6 +112,8 @@ struct watch_base_context {
        bool ambient_mode_skip_resume;
        bool bg_launch;
        guint pause_timer;
+       aul_app_com_connection_h conn;
+       bundle *extra;
        void *data;
 };
 
@@ -507,6 +509,55 @@ static void __vconf_time_changed_cb(keynode_t *key, void *data)
 }
 /* LCOV_EXCL_STOP */
 
+int __on_change_signal(const char *endpoint, aul_app_com_result_e e,
+       bundle *envelope, void *user_data)
+{
+       char *extra;
+       char *mode;
+       watch_base_ambient_changed_cb ambient_changed_cb;
+
+       _W("__on_change_signal");
+
+       bundle_get_str(envelope, "__AMBIENT_MODE__", &mode);
+       bundle_get_str(envelope, "__AMBIENT_EXTRA__", &extra);
+       __context.extra = bundle_decode((bundle_raw *)extra, strlen(extra));
+
+       ambient_changed_cb = __context.callback.ambient_changed;
+
+       if (ambient_changed_cb != NULL)
+               ambient_changed_cb((bool)atoi(mode), __context.data);
+
+       return 0;
+}
+
+static void __set_ambient_changed_cb()
+{
+       if (__context.conn) {
+               _D("Already set ambient changed cb");
+               return;
+       }
+
+       _D("Set ambient changed cb");
+
+       if (aul_app_com_create("watch.ambientchange", NULL, __on_change_signal,
+               NULL, &__context.conn) != AUL_R_OK) {
+               _E("Failed to listen 'watch.ambientchange' signal");
+       }
+}
+
+static void __unset_ambient_changed_cb()
+{
+       if (__context.conn) {
+               if (aul_app_com_leave(__context.conn) < 0)
+                       _E("failed to leave app com disable");
+
+               __context.conn = NULL;
+       }
+
+       if (__context.extra)
+               bundle_free(__context.extra);
+}
+
 static int __on_ui_base_create(void *data)
 {
        watch_base_create_cb create_cb;
@@ -922,6 +973,10 @@ EXPORT_API int watch_base_init(int argc, char **argv,
                                __FUNCTION__, "failed to get the appid");
        }
 
+       if (!__context.conn) {
+               __set_ambient_changed_cb();
+       }
+
        /* override methods */
        ops.base.create = __on_ui_base_create;
        ops.base.control = __on_ui_base_control;
@@ -964,12 +1019,14 @@ EXPORT_API int watch_base_init(int argc, char **argv,
        __context.ambient_mode_skip_resume = false;
        __context.data = user_data;
        __context.state = WATCH_BASE_STATE_CREATING;
+       __context.extra = NULL;
 
        return appcore_ui_base_init(ops, argc, argv, NULL, 0);
 }
 
 EXPORT_API void watch_base_fini(void)
 {
+       __unset_ambient_changed_cb();
        free(__context.appid);
        __context.appid = NULL;
        appcore_ui_base_fini();
@@ -1100,3 +1157,75 @@ EXPORT_API int watch_base_get_time_tick_frequency(int *ticks, watch_base_time_ti
                return APP_ERROR_INVALID_PARAMETER;
        return __get_time_tick_frequency(ticks, type);
 }
+
+EXPORT_API int watch_base_ambient_send_event(const char *receiver,
+               watch_base_ambient_event_e event, bundle *extra)
+{
+       char buf[32];
+       char endpoint[APPID_BUFFER_MAX];
+       char appid_buf[APPID_BUFFER_MAX] = {0, };
+       int ret;
+       bundle *envelope;
+
+       if (!receiver) {
+               _E("receiver should not be null");
+               return APP_ERROR_INVALID_PARAMETER;
+       }
+
+       if (extra == NULL) {
+               _E("extra data is null");
+               envelope = bundle_create();
+       } else {
+               envelope = bundle_dup(extra);
+       }
+
+       if (envelope == NULL) {
+               _E("out of memory");
+               return APP_ERROR_OUT_OF_MEMORY;
+       }
+
+       snprintf(buf, sizeof(buf), "%d", event);
+       ret = bundle_add_str(envelope, "__APP_AMBIENT_EVENT__", buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("bundle_add_str returns false");
+               bundle_free(envelope);
+               return -1;
+       }
+
+       if (aul_app_get_appid_bypid(
+               getpid(), appid_buf, sizeof(appid_buf)) != AUL_R_OK) {
+               _E("Failed to get appid (%d)", getpid());
+               bundle_free(envelope);
+               return -1;
+       }
+
+       ret = bundle_add_str(envelope, "__APP_AMBIENT_SENDER__", appid_buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("bundle_add_str returns false");
+               bundle_free(envelope);
+               return -1;
+       }
+
+       snprintf(endpoint, sizeof(endpoint), "send_extra_data:%s", receiver);
+
+       ret = aul_app_com_send(endpoint, envelope);
+       if (ret != AUL_R_OK) {
+               _E("Failed aul_app_com_send");
+               bundle_free(envelope);
+               return -1;
+       }
+
+       bundle_free(envelope);
+       return APP_ERROR_NONE;
+}
+
+EXPORT_API int watch_base_ambient_get_extra(bundle **extra)
+{
+       if (__context.extra == NULL) {
+               _E("Watch app not receive a bundle yet.");
+               return APP_ERROR_INVALID_CONTEXT;
+       }
+
+       *extra = bundle_dup(__context.extra);
+       return APP_ERROR_NONE;
+}
\ No newline at end of file
index c3c3529..3ed00d5 100755 (executable)
@@ -22,6 +22,7 @@
 #include "watch_app_log.h"
 #include "watch_base.h"
 #include "watch_app.h"
+#include "watch_app_internal.h"
 
 #ifdef LOG_TAG
 #undef LOG_TAG
@@ -366,3 +367,14 @@ EXPORT_API int watch_app_get_time_tick_frequency(int *ticks,
        return watch_base_get_time_tick_frequency(ticks,
                        (watch_base_time_tick_resolution_e *)type);
 }
+
+EXPORT_API int watch_app_ambient_send_event(const char *receiver,
+               watch_app_ambient_event_e event, bundle *extra)
+{
+       return watch_base_ambient_send_event(receiver, event, extra);
+}
+
+EXPORT_API int watch_app_ambient_get_extra(bundle **extra)
+{
+       return watch_base_ambient_get_extra(extra);
+}
\ No newline at end of file