Fix appcore_base_flush_memory API
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
index c8617d5..cc311a9 100644 (file)
@@ -45,6 +45,7 @@
 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
+#define SQLITE_FLUSH_MAX (1024 * 1024)
 
 typedef struct _appcore_base_context {
        appcore_base_ops ops;
@@ -618,8 +619,15 @@ static void __update_lang(void)
                r = setlocale(LC_ALL, "");
                if (r == NULL) {
                        r = setlocale(LC_ALL, "en_US.UTF-8");
-                       if (r != NULL)
+                       if (r != NULL) {
                                _DBG("*****appcore setlocale=%s\n", r);
+                       } else {
+                               _DBG("*****appcore setlocale=\"C\"");
+                               setenv("LC_ALL", "C", 1);
+                               r = setlocale(LC_ALL, "");
+                               if (r == NULL)
+                                       _ERR("failed to setlocale");
+                       }
                }
                free(lang);
        }
@@ -644,8 +652,15 @@ static void __update_region(void)
                setenv("LC_MEASUREMENT", region, 1);
                setenv("LC_IDENTIFICATION", region, 1);
                r = setlocale(LC_ALL, "");
-               if (r != NULL)
+               if (r != NULL) {
                        _DBG("*****appcore setlocale=%s\n", r);
+               } else {
+                       _DBG("*****appcore setlocale=\"C\"");
+                       setenv("LC_ALL", "C", 1);
+                       r = setlocale(LC_ALL, "");
+                       if (r == NULL)
+                               _ERR("failed to setlocale");
+               }
 
                free(region);
        }
@@ -895,10 +910,17 @@ static int __set_i18n(const char *domain, const char *dir)
        /* if locale is not set properly, try to set "en_US" again */
        if (r == NULL) {
                r = setlocale(LC_ALL, "en_US.UTF-8");
-               _DBG("*****appcore setlocale=%s\n", r);
+               if (r != NULL)
+                       _DBG("*****appcore setlocale=%s\n", r);
        }
-       if (r == NULL)
+       if (r == NULL) {
                _ERR("appcore: setlocale() error");
+               _DBG("*****appcore setlocale=\"C\"");
+               setenv("LC_ALL", "C", 1);
+               r = setlocale(LC_ALL, "");
+               if (r == NULL)
+                       _ERR("failed to setlocale");
+       }
 
        r = bindtextdomain(domain, dir);
        if (r == NULL)
@@ -950,6 +972,28 @@ EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_na
        return __set_i18n(domain_name, dir_name);
 }
 
+static void __set_default_events(void)
+{
+       int r;
+
+       vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
+       r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
+       if (r == 0)
+               vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
+       vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
+}
+
+static void __unset_default_events(void)
+{
+       int r;
+
+       vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
+       r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
+       if (r == 0)
+               vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
+       vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
+}
+
 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
 {
        int i;
@@ -981,6 +1025,7 @@ EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
        }
 
        __verify_language();
+       __set_default_events();
        if (__context.ops.set_i18n)
                __context.ops.set_i18n(__context.data);
 
@@ -1018,6 +1063,8 @@ EXPORT_API void appcore_base_fini(void)
                }
        }
 
+       __unset_default_events();
+
        if (__context.sid) {
                g_source_remove(__context.sid);
                __context.sid = 0;
@@ -1042,33 +1089,25 @@ EXPORT_API void appcore_base_fini(void)
 
 EXPORT_API int appcore_base_flush_memory(void)
 {
+       int (*sqlite3_free_heap_memory)(int);
+
+       sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
+                       "sqlite3_release_memory");
+       if (sqlite3_free_heap_memory)
+               sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
+
        malloc_trim(0);
        return 0;
 }
 
 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
 {
-       int ret;
-       const char **tep_path;
-       int len = 0;
-       int i;
        const char *bg;
        int dummy = 0;
 
        switch (type) {
        case AUL_START:
                _DBG("[APP %d]     AUL event: AUL_START", getpid());
-               tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
-               if (tep_path) {
-                       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);
-                               }
-                       }
-               }
-
                if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
                        bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
                        if (bg && !strcmp(bg, "ALLOWED_BG")) {
@@ -1147,6 +1186,7 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
 EXPORT_API int appcore_base_on_create(void)
 {
        int r;
+
        r = aul_launch_init(__context.ops.receive, NULL);
        if (r < 0) {
                _ERR("Aul init failed: %d", r);
@@ -1176,28 +1216,13 @@ EXPORT_API int appcore_base_on_terminate()
 
 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
 {
-       int r;
-
        switch (event) {
-       case APPCORE_BASE_EVENT_LOW_MEMORY:
-               vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
-               break;
        case APPCORE_BASE_EVENT_LOW_BATTERY:
                vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
                break;
-       case APPCORE_BASE_EVENT_LANG_CHANGE:
-               vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
-               break;
        case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
                __register_rotation_changed_event();
                break;
-       case APPCORE_BASE_EVENT_REGION_CHANGE:
-               r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
-               if (r < 0)
-                       break;
-
-               vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
-               break;
        case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
                break;
 
@@ -1209,27 +1234,13 @@ EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
 
 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
 {
-       int r;
-
        switch (event) {
-       case APPCORE_BASE_EVENT_LOW_MEMORY:
-               vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
-               break;
        case APPCORE_BASE_EVENT_LOW_BATTERY:
                vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
                break;
-       case APPCORE_BASE_EVENT_LANG_CHANGE:
-               vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
-               break;
        case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
                __unregister_rotation_changed_event();
                break;
-       case APPCORE_BASE_EVENT_REGION_CHANGE:
-               r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
-               if (r < 0)
-                       break;
-               vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
-               break;
        case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
                break;
        default:
@@ -1318,6 +1329,16 @@ EXPORT_API void appcore_base_exit(void)
                __context.ops.exit(__context.data);
 }
 
+EXPORT_API void appcore_base_add_suspend_timer(void)
+{
+       __add_suspend_timer();
+}
+
+EXPORT_API void appcore_base_remove_suspend_timer(void)
+{
+       __remove_suspend_timer();
+}
+
 static int __on_receive(aul_type type, bundle *b, void *data)
 {
        return appcore_base_on_receive(type, b);