From 988b9b0a7f3355146b515a31338a06f1739ef93c Mon Sep 17 00:00:00 2001 From: Michal Pawluk Date: Fri, 21 Aug 2015 11:29:26 +0200 Subject: [PATCH] [SAMPLE APP][EVENT] Application's initialization section added Change-Id: Idcb61f0fd85c575f619c896bfc4291b7ecd5c6b6 Signed-off-by: Michal Pawluk --- .../html/mobile_n/event_sd_mn.htm | 166 ++++++++++++++++++++- 1 file changed, 164 insertions(+), 2 deletions(-) diff --git a/org.tizen.sampledescriptions/html/mobile_n/event_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/event_sd_mn.htm index ed4af18..f635442 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/event_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/event_sd_mn.htm @@ -132,7 +132,7 @@ struct __viewdata {    Evas_Object *custom_ev_list;    Elm_Genlist_Item_Class *custom_ev_itc;    /* The structure of callbacks handlers used by the Controller module to establish interaction with the Model module. */ -   viewcallbacks_s *callbacks; +   viewcallbacks_s callbacks; }; typedef struct __viewdata viewdata_s; @@ -197,6 +197,168 @@ typedef struct __system_ev_info custom_ev_info_s;

Application initialization

+

+ The entire application's life-cycle is implemented in the main source file, using a common Tizen application structure: +

+ +
+int main(int argc, char *argv[])
+{
+   appdata_s ad = {{0,},};
+   int ret = 0;
+
+   ui_app_lifecycle_callback_s event_callback;
+   app_event_handler_h handlers[5] = {NULL, };
+
+   event_callback.create = __create_app;
+   event_callback.terminate = __terminate_app;
+   event_callback.pause = __pause_app;
+   event_callback.resume = __resume_app;
+   event_callback.app_control = __control_app;
+
+   ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, __ui_app_low_battery, &ad);
+   ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, __ui_app_low_memory, &ad);
+   ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, __ui_app_orient_changed, &ad);
+   ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, __ui_app_lang_changed, &ad);
+   ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, __ui_app_region_changed, &ad);
+
+   ret = ui_app_main(argc, argv, &event_callback, &ad);
+   if (ret != APP_ERROR_NONE)
+      controller_log(DLOG_ERROR, "Function ui_app_main() failed with error = %d", ret);
+
+   return ret;
+}
+
+ +

+ The application's initilization procedure is executed in __create_app() callback function, which is invoked on application's startup. +

+ +
+static bool __create_app(void *data)
+{
+   appdata_s *ad = (appdata_s *)data;
+
+   return controller_init(&ad->view);
+}
+
+ +

+ Finally, the controller_init() function is invoked, which controls the entire initialization process + (see the code snippet below for details). This function is responsible for attaching callback functions invoked by the View module in order to perform required tasks: +

+ The first and the third callback function is called when the user performs appropriate action from the application's UI. The second callback function is called during the application's UI creation. + The application's UI is afterwards created by the view_create_base_gui() function. Its source code is not listed + within this documentation, as it is not a subject of this document. At the end of the initialization phase, a callback function is attached to each of the available system events within the + __add_system_event_handlers() function. +

+ +
+bool controller_init(viewdata_s *vd)
+{
+   vd->callbacks.do_publish_cb = __controller_event_do_publish_cb;
+   vd->callbacks.get_system_info_cb = __controller_event_get_system_info_cb;
+   vd->callbacks.set_custom_info_cb = __controller_event_set_custom_info_cb;
+
+   if (!view_create_base_gui(vd))
+      return false;
+
+   __add_system_event_handlers();
+
+   return true;
+}
+
+ +
+static void __controller_event_do_publish_cb(const char *event_name)
+{
+   controller_log(DLOG_INFO, "Event publishing: '%s'.", event_name);
+
+   model_publish_event(event_name);
+}
+
+ +

+ The __controller_event_get_system_info_cb() is invoked by the View module as long as the function returns + true value. Each time the function is called with incremented index + value. As a result, the system's event information ev_info, stored at specified + index, is returned. The requested data structure is obtained with the + model_get_system_event_info() function. +

+ +
+static bool __controller_event_get_system_info_cb(int index, void **ev_info)
+{
+   *ev_info = NULL;
+
+   if (index >= model_get_system_events_count())
+      return false;
+
+   system_ev_info_s *ev_info_tmp = NULL;
+   if (!model_get_system_event_info(index, &ev_info_tmp))
+      return false;
+
+   *ev_info = (system_ev_info_s *)ev_info_tmp;
+
+   return true;
+}
+
+ +

+ The __controller_event_set_custom_info_cb() callback function is invoked by the View model when the user requests to register + custom event. If the registration procedure succeeds (see the __controller_register_custom_event() function's implementation for + details), the reference to the event's information structure is returned via the ev_info argument. +

+ +
+static bool __controller_event_set_custom_info_cb(const char *event_name, void **ev_info)
+{
+   *ev_info = NULL;
+
+   custom_ev_info_s *ev_info_tmp = NULL;
+   if (!__controller_register_custom_event(event_name, &ev_info_tmp))
+      return false;
+
+   *ev_info = (custom_ev_info_s *)ev_info_tmp;
+
+   return true;
+}
+
+ +
+bool __controller_register_custom_event(const char *event_name, custom_ev_info_s **ev_info)
+{
+   *ev_info = NULL;
+   bool name_exists = false;
+
+   /* The event's name, assigned by the user, must be unique, so we have to check if it does not exist yet. */
+   if (!model_check_event_exists(event_name, &name_exists))
+      return false;
+
+   /* If there is already an event registered with the given name, then the function fails. */
+   if (name_exists) {
+      controller_log(DLOG_WARN, "Custom event '%s' already registered.", event_name);
+      return false;
+   }
+
+   /* Otherwise new event's information structure is created. */
+   if (!model_create_custom_event_info(event_name, ev_info))
+      return false;
+
+   /* Finally, the event's callback function is assigned, to be invoked when the event occurs. */
+   if (!model_add_custom_event_handler(*ev_info, __custom_event_cb, (void *)(*ev_info)))
+      return false;
+
+   controller_log(DLOG_INFO, "Custom event registered: '%s'.", (*ev_info)->name);
+
+   return true;
+}
+
+

Application termination

System events

@@ -227,4 +389,4 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga - \ No newline at end of file + -- 2.7.4