Raise the window when getting the resume request
[platform/core/appfw/app-core.git] / src / appcore.c
index 31ce92f..c93d447 100644 (file)
 #include <dlfcn.h>
 #include <vconf.h>
 #include <aul.h>
-#include <tzplatform_config.h>
 #include <bundle_internal.h>
 #include "appcore-internal.h"
+#include <system_info.h>
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
 #include <gio/gio.h>
 
 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
-#endif
+
+int __appcore_init_suspend_dbus_handler(void *data);
+void __appcore_fini_suspend_dbus_handler(void);
 
 #define SQLITE_FLUSH_MAX               (1024*1024)
 
-#define PKGNAME_MAX 256
-#define PATH_APP_ROOT tzplatform_getenv(TZ_USER_APP)
-#define PATH_SYS_RO_APP_ROOT tzplatform_getenv(TZ_SYS_RO_APP)
-#define PATH_SYS_RW_APP_ROOT tzplatform_getenv(TZ_SYS_RW_APP)
-#define PATH_RES "/res"
-#define PATH_LOCALE "/locale"
+#define PATH_LOCALE "locale"
 
 static struct appcore core;
 static pid_t _pid;
@@ -61,6 +57,7 @@ static enum appcore_event to_ae[SE_MAX] = {
        APPCORE_EVENT_LANG_CHANGE,      /* SE_LANGCGH */
        APPCORE_EVENT_REGION_CHANGE,
        APPCORE_EVENT_SUSPENDED_STATE_CHANGE,
+       APPCORE_EVENT_UPDATE_REQUESTED,
 };
 
 static int appcore_event_initialized[SE_MAX] = {0,};
@@ -141,39 +138,21 @@ static struct evt_ops evtops[] = {
         },
 };
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-static GDBusConnection *bus = NULL;
-static guint __suspend_dbus_handler_initialized = 0;
-#endif
+static GDBusConnection *bus;
+static guint __suspend_dbus_handler_initialized;
 
-static int __get_dir_name(char *dirname)
+static int __get_locale_resource_dir(char *locale_dir, int size)
 {
-       char pkg_name[PKGNAME_MAX];
-       int r;
-       int pid;
+       const char *res_path;
 
-       pid = getpid();
-       if (pid < 0)
-               return -1;
-
-       if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
+       res_path = aul_get_app_resource_path();
+       if (res_path == NULL) {
+               _ERR("Failed to get resource path");
                return -1;
+       }
 
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_APP_ROOT, pkg_name);
-       if (r < 0)
-               return -1;
-       if (access(dirname, R_OK) == 0)
-               return 0;
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_SYS_RO_APP_ROOT, pkg_name);
-       if (r < 0)
-               return -1;
-       if (access(dirname, R_OK) == 0)
-               return 0;
-       r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
-                       PATH_SYS_RW_APP_ROOT, pkg_name);
-       if (r < 0)
+       snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
+       if (access(locale_dir, R_OK) != 0)
                return -1;
 
        return 0;
@@ -247,6 +226,17 @@ static int __app_pause(void *data)
        return 0;
 }
 
+static int __app_update_requested(void *data)
+{
+       struct appcore *ac = data;
+
+       _retv_if(ac == NULL || ac->ops == NULL, -1);
+       _retv_if(ac->ops->cb_app == NULL, 0);
+       ac->ops->cb_app(AE_UPDATE_REQUESTED, ac->ops->data, NULL);
+
+       return 0;
+}
+
 static int __sys_do_default(struct appcore *ac, enum sys_event event)
 {
        int r;
@@ -473,7 +463,6 @@ static int __del_vconf_list(void)
        return 0;
 }
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
 static gboolean __flush_memory(gpointer data)
 {
        int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
@@ -507,50 +496,52 @@ static void __remove_suspend_timer(struct appcore *ac)
                ac->tid = 0;
        }
 }
-#endif
 
 static int __aul_handler(aul_type type, bundle *b, void *data)
 {
        int ret;
-       const char *tep_path = NULL;
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
+       const char **tep_path = NULL;
+       int len = 0;
+       int i;
        const char *bg = NULL;
        struct appcore *ac = data;
-#endif
+       int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
 
        switch (type) {
        case AUL_START:
                _DBG("[APP %d]     AUL event: AUL_START", _pid);
-               tep_path = bundle_get_val(b, AUL_TEP_PATH);
+               tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
                if (tep_path) {
-                       ret = aul_check_tep_mount(tep_path);
-                       if (ret == -1) {
-                               _ERR("mount request not completed within 1 sec");
-                               exit(-1);
+                       for (i = 0; i < len; i++) {
+                               ret = aul_check_tep_mount(tep_path[i]);
+                               if (ret == -1) {
+                                       _ERR("mount request not completed within 1 sec");
+                                       exit(-1);
+                               }
                        }
                }
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       ac->allowed_bg = true;
-                       __remove_suspend_timer(data);
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               ac->allowed_bg = true;
+                               __remove_suspend_timer(data);
+                       }
                }
-#endif
 
                __app_reset(data, b);
                break;
        case AUL_RESUME:
                _DBG("[APP %d]     AUL event: AUL_RESUME", _pid);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       ac->allowed_bg = true;
-                       __remove_suspend_timer(data);
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               ac->allowed_bg = true;
+                               __remove_suspend_timer(data);
+                       }
                }
-#endif
 
                if (open.callback) {
                        ret = open.callback(open.cbdata);
@@ -562,19 +553,15 @@ static int __aul_handler(aul_type type, bundle *b, void *data)
                break;
        case AUL_TERMINATE:
                _DBG("[APP %d]     AUL event: AUL_TERMINATE", _pid);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               if (!ac->allowed_bg)
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && !ac->allowed_bg)
                        __remove_suspend_timer(data);
-#endif
 
                __app_terminate(data);
                break;
        case AUL_TERMINATE_BGAPP:
                _DBG("[APP %d]     AUL event: AUL_TERMINATE_BGAPP", _pid);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               if (!ac->allowed_bg)
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && !ac->allowed_bg)
                        __remove_suspend_timer(data);
-#endif
 
                __bgapp_terminate(data);
                break;
@@ -582,24 +569,40 @@ static int __aul_handler(aul_type type, bundle *b, void *data)
                _DBG("[APP %d]     AUL event: AUL_PAUSE", _pid);
                __app_pause(data);
                break;
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
        case AUL_WAKE:
-               _DBG("[APP %d]     AUL event: AUL_WAKE", _pid);
-               if (!ac->allowed_bg && ac->suspended_state) {
-                       int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
-                       __remove_suspend_timer(data);
-                       __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
-                       ac->suspended_state = false;
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       _DBG("[APP %d]     AUL event: AUL_WAKE", _pid);
+                       if (!ac->allowed_bg && ac->suspended_state) {
+                               __remove_suspend_timer(data);
+                               __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
+                               ac->suspended_state = false;
+                       }
+                       if (b) {
+                               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                               if (bg && strcmp(bg, "ALLOWED_BG") == 0) {
+                                       _DBG("[__SUSPEND__] allowed background");
+                                       ac->allowed_bg = true;
+                               }
+                       }
+                       break;
                }
-               break;
+               goto __default__;
        case AUL_SUSPEND:
-               _DBG("[APP %d]     AUL event: AUL_SUSPEND", _pid);
-               if (!ac->allowed_bg && !ac->suspended_state) {
-                       __remove_suspend_timer(data);
-                       __flush_memory((gpointer)ac);
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       _DBG("[APP %d]     AUL event: AUL_SUSPEND", _pid);
+                       ac->allowed_bg = false;
+                       if (!ac->suspended_state) {
+                               __remove_suspend_timer(data);
+                               __flush_memory((gpointer)ac);
+                       }
+                       break;
                }
+               goto __default__;
+       case AUL_UPDATE_REQUESTED:
+               _DBG("[APP %d]     AUL event: AUL_UPDATE_REQUESTED", _pid);
+               __app_update_requested(data);
                break;
-#endif
+__default__:
        default:
                _DBG("[APP %d]     AUL event: %d", _pid, type);
                /* do nothing */
@@ -670,25 +673,23 @@ EXPORT_API int appcore_set_event_callback(enum appcore_event event,
        return 0;
 }
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
 static gboolean __init_suspend(gpointer data)
 {
        int r;
 
-       r = _appcore_init_suspend_dbus_handler(&core);
+       r = __appcore_init_suspend_dbus_handler(&core);
        if (r == -1) {
                _ERR("Initailzing suspended state handler failed");
        }
 
        return FALSE;
 }
-#endif
 
 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
                            int argc, char **argv)
 {
        int r;
-       char dirname[PATH_MAX];
+       char locale_dir[PATH_MAX];
 
        if (core.state != 0) {
                _ERR("Already in use");
@@ -702,8 +703,8 @@ EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
                return -1;
        }
 
-       r = __get_dir_name(dirname);
-       r = set_i18n(name, dirname);
+       r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
+       r = set_i18n(name, locale_dir);
        _retv_if(r == -1, -1);
 
        r = aul_launch_init(__aul_handler, &core);
@@ -726,9 +727,8 @@ EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
 
        _pid = getpid();
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-       g_idle_add(__init_suspend, NULL);
-#endif
+       if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT)
+               g_idle_add(__init_suspend, NULL);
 
        return 0;
  err:
@@ -742,9 +742,10 @@ EXPORT_API void appcore_exit(void)
        if (core.state) {
                __del_vconf_list();
                __clear(&core);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               __remove_suspend_timer(&core);
-#endif
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       __remove_suspend_timer(&core);
+                       __appcore_fini_suspend_dbus_handler();
+               }
        }
        aul_finalize();
 }
@@ -780,7 +781,6 @@ EXPORT_API int appcore_flush_memory(void)
        return 0;
 }
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
 static void __suspend_dbus_signal_handler(GDBusConnection *connection,
                                        const gchar *sender_name,
                                        const gchar *object_path,
@@ -807,7 +807,7 @@ static void __suspend_dbus_signal_handler(GDBusConnection *connection,
        }
 }
 
-int _appcore_init_suspend_dbus_handler(void *data)
+int __appcore_init_suspend_dbus_handler(void *data)
 {
        GError *err = NULL;
 
@@ -844,4 +844,51 @@ int _appcore_init_suspend_dbus_handler(void *data)
 
        return 0;
 }
-#endif
+
+void __appcore_fini_suspend_dbus_handler(void)
+{
+       if (bus == NULL)
+               return;
+
+       if (__suspend_dbus_handler_initialized) {
+               g_dbus_connection_signal_unsubscribe(bus,
+                               __suspend_dbus_handler_initialized);
+               __suspend_dbus_handler_initialized = 0;
+       }
+
+       g_object_unref(bus);
+       bus = NULL;
+}
+
+tizen_profile_t _get_tizen_profile()
+{
+       static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       char *profileName;
+       system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
+       switch (*profileName) {
+       case 'm':
+       case 'M':
+               profile = TIZEN_PROFILE_MOBILE;
+               break;
+       case 'w':
+       case 'W':
+               profile = TIZEN_PROFILE_WEARABLE;
+               break;
+       case 't':
+       case 'T':
+               profile = TIZEN_PROFILE_TV;
+               break;
+       case 'i':
+       case 'I':
+               profile = TIZEN_PROFILE_IVI;
+               break;
+       default: // common or unTIZEN_nown ==> ALL ARE COMMON.
+               profile = TIZEN_PROFILE_COMMON;
+       }
+       free(profileName);
+
+       return profile;
+}