[SAMPLE APP][EVENT] Application's initialization section added
authorMichal Pawluk <m.pawluk@samsung.com>
Fri, 21 Aug 2015 09:29:26 +0000 (11:29 +0200)
committerMichal Pawluk <m.pawluk@samsung.com>
Mon, 24 Aug 2015 08:50:20 +0000 (10:50 +0200)
Change-Id: Idcb61f0fd85c575f619c896bfc4291b7ecd5c6b6
Signed-off-by: Michal Pawluk <m.pawluk@samsung.com>
org.tizen.sampledescriptions/html/mobile_n/event_sd_mn.htm

index ed4af18..f635442 100644 (file)
@@ -132,7 +132,7 @@ struct __viewdata {
 &nbsp;&nbsp;&nbsp;Evas_Object *custom_ev_list;
 &nbsp;&nbsp;&nbsp;Elm_Genlist_Item_Class *custom_ev_itc;
 &nbsp;&nbsp;&nbsp;/* The structure of callbacks handlers used by the Controller module to establish interaction with the Model module. */
-&nbsp;&nbsp;&nbsp;viewcallbacks_s *callbacks;
+&nbsp;&nbsp;&nbsp;viewcallbacks_s callbacks;
 };
 
 typedef struct __viewdata viewdata_s;
@@ -197,6 +197,168 @@ typedef struct __system_ev_info custom_ev_info_s;
 
 <h3 id="app-init">Application initialization</h3>
 
+   <p>
+  The entire application's life-cycle is implemented in the main source file, using a common Tizen application structure:
+  </p>
+
+<pre class="prettyprint">
+int main(int argc, char *argv[])
+{
+&nbsp;&nbsp;&nbsp;appdata_s ad = {{0,},};
+&nbsp;&nbsp;&nbsp;int ret = 0;
+
+&nbsp;&nbsp;&nbsp;ui_app_lifecycle_callback_s event_callback;
+&nbsp;&nbsp;&nbsp;app_event_handler_h handlers[5] = {NULL, };
+
+&nbsp;&nbsp;&nbsp;event_callback.create = __create_app;
+&nbsp;&nbsp;&nbsp;event_callback.terminate = __terminate_app;
+&nbsp;&nbsp;&nbsp;event_callback.pause = __pause_app;
+&nbsp;&nbsp;&nbsp;event_callback.resume = __resume_app;
+&nbsp;&nbsp;&nbsp;event_callback.app_control = __control_app;
+
+&nbsp;&nbsp;&nbsp;ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, __ui_app_low_battery, &ad);
+&nbsp;&nbsp;&nbsp;ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, __ui_app_low_memory, &ad);
+&nbsp;&nbsp;&nbsp;ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, __ui_app_orient_changed, &ad);
+&nbsp;&nbsp;&nbsp;ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, __ui_app_lang_changed, &ad);
+&nbsp;&nbsp;&nbsp;ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, __ui_app_region_changed, &ad);
+
+&nbsp;&nbsp;&nbsp;ret = ui_app_main(argc, argv, &event_callback, &ad);
+&nbsp;&nbsp;&nbsp;if (ret != APP_ERROR_NONE)
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller_log(DLOG_ERROR, "Function ui_app_main() failed with error = %d", ret);
+
+&nbsp;&nbsp;&nbsp;return ret;
+}
+</pre>
+
+  <p>
+  The application's initilization procedure is executed in <span style="font-family: Courier New,Courier,monospace">__create_app()</span> callback function, which is invoked on application's startup.
+  </p>
+
+<pre class="prettyprint">
+static bool __create_app(void *data)
+{
+&nbsp;&nbsp;&nbsp;appdata_s *ad = (appdata_s *)data;
+
+&nbsp;&nbsp;&nbsp;return controller_init(&ad->view);
+}
+</pre>
+
+  <p>
+  Finally, the <span style="font-family: Courier New,Courier,monospace">controller_init()</span> 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:
+    <ul>
+         <li><span style="font-family: Courier New,Courier,monospace">__controller_event_do_publish_cb()</span> - publish the custom event registered by the user;</li>
+         <li><span style="font-family: Courier New,Courier,monospace">__controller_event_get_system_info_cb()</span> - query system's event information to populate the list with relevant data;</li>
+         <li><span style="font-family: Courier New,Courier,monospace">__controller_event_set_custom_info_cb()</span> - register event handler to be invoked on custom event occurance.</li>
+       </ul>
+  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 <span style="font-family: Courier New,Courier,monospace">view_create_base_gui()</span> 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
+  <span style="font-family: Courier New,Courier,monospace">__add_system_event_handlers()</span> function.
+  </p>
+
+<pre class="prettyprint">
+bool controller_init(viewdata_s *vd)
+{
+&nbsp;&nbsp;&nbsp;vd->callbacks.do_publish_cb = __controller_event_do_publish_cb;
+&nbsp;&nbsp;&nbsp;vd->callbacks.get_system_info_cb = __controller_event_get_system_info_cb;
+&nbsp;&nbsp;&nbsp;vd->callbacks.set_custom_info_cb = __controller_event_set_custom_info_cb;
+
+&nbsp;&nbsp;&nbsp;if (!view_create_base_gui(vd))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;__add_system_event_handlers();
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<pre class="prettyprint">
+static void __controller_event_do_publish_cb(const char *event_name)
+{
+&nbsp;&nbsp;&nbsp;controller_log(DLOG_INFO, "Event publishing: '%s'.", event_name);
+
+&nbsp;&nbsp;&nbsp;model_publish_event(event_name);
+}
+</pre>
+
+  <p>
+  The <span style="font-family: Courier New,Courier,monospace">__controller_event_get_system_info_cb()</span> is invoked by the View module as long as the function returns
+  <span style="font-family: Courier New,Courier,monospace">true</span> value. Each time the function is called with incremented <span style="font-family: Courier New,Courier,monospace">index</span>
+  value. As a result, the system's event information <span style="font-family: Courier New,Courier,monospace">ev_info</span>, stored at specified
+  <span style="font-family: Courier New,Courier,monospace">index</span>, is returned. The requested data structure is obtained with the
+  <span style="font-family: Courier New,Courier,monospace">model_get_system_event_info()</span> function.
+  </p>
+
+<pre class="prettyprint">
+static bool __controller_event_get_system_info_cb(int index, void **ev_info)
+{
+&nbsp;&nbsp;&nbsp;*ev_info = NULL;
+
+&nbsp;&nbsp;&nbsp;if (index >= model_get_system_events_count())
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;system_ev_info_s *ev_info_tmp = NULL;
+&nbsp;&nbsp;&nbsp;if (!model_get_system_event_info(index, &ev_info_tmp))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;*ev_info = (system_ev_info_s *)ev_info_tmp;
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+  <p>
+  The <span style="font-family: Courier New,Courier,monospace">__controller_event_set_custom_info_cb()</span> callback function is invoked by the View model when the user requests to register
+  custom event. If the registration procedure succeeds (see the <span style="font-family: Courier New,Courier,monospace">__controller_register_custom_event()</span> function's implementation for
+  details), the reference to the event's information structure is returned via the <span style="font-family: Courier New,Courier,monospace">ev_info</span> argument.
+  </p>
+
+<pre class="prettyprint">
+static bool __controller_event_set_custom_info_cb(const char *event_name, void **ev_info)
+{
+&nbsp;&nbsp;&nbsp;*ev_info = NULL;
+
+&nbsp;&nbsp;&nbsp;custom_ev_info_s *ev_info_tmp = NULL;
+&nbsp;&nbsp;&nbsp;if (!__controller_register_custom_event(event_name, &ev_info_tmp))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;*ev_info = (custom_ev_info_s *)ev_info_tmp;
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
+<pre class="prettyprint">
+bool __controller_register_custom_event(const char *event_name, custom_ev_info_s **ev_info)
+{
+&nbsp;&nbsp;&nbsp;*ev_info = NULL;
+&nbsp;&nbsp;&nbsp;bool name_exists = false;
+
+&nbsp;&nbsp;&nbsp;/* The event's name, assigned by the user, must be unique, so we have to check if it does not exist yet. */
+&nbsp;&nbsp;&nbsp;if (!model_check_event_exists(event_name, &name_exists))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;/* If there is already an event registered with the given name, then the function fails. */
+&nbsp;&nbsp;&nbsp;if (name_exists) {
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller_log(DLOG_WARN, "Custom event '%s' already registered.", event_name);
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+&nbsp;&nbsp;&nbsp;}
+
+&nbsp;&nbsp;&nbsp;/* Otherwise new event's information structure is created. */
+&nbsp;&nbsp;&nbsp;if (!model_create_custom_event_info(event_name, ev_info))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;/* Finally, the event's callback function is assigned, to be invoked when the event occurs. */
+&nbsp;&nbsp;&nbsp;if (!model_add_custom_event_handler(*ev_info, __custom_event_cb, (void *)(*ev_info)))
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
+
+&nbsp;&nbsp;&nbsp;controller_log(DLOG_INFO, "Custom event registered: '%s'.", (*ev_info)->name);
+
+&nbsp;&nbsp;&nbsp;return true;
+}
+</pre>
+
 <h3 id="app-finit">Application termination</h3>
 
 <h3 id="ev-system">System events</h3>
@@ -227,4 +389,4 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
 </script>
 
 </body>
-</html>
\ No newline at end of file
+</html>