2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <sys/types.h>
30 #include <linux/limits.h>
37 #include <bundle_internal.h>
38 #include <sensor_internal.h>
40 #include <system_info.h>
41 #include "appcore_base.h"
42 #include "appcore_base_private.h"
44 #define PATH_LOCALE "locale"
45 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
46 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
47 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
48 #define SQLITE_FLUSH_MAX (1024 * 1024)
50 typedef struct _appcore_base_context {
60 } appcore_base_context;
62 typedef struct _appcore_base_event_node {
64 appcore_base_event_cb cb;
66 } appcore_base_event_node;
68 typedef struct _appcore_base_rotation {
72 enum appcore_base_rm rm;
75 } appcore_base_rotation;
82 static appcore_base_context __context;
83 static GList *__events;
84 static GDBusConnection *__bus;
85 static guint __suspend_dbus_handler_initialized;
86 static char *__locale_dir;
87 static appcore_base_rotation __rotation;
89 appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
91 static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
92 char *profile_name = NULL;
94 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
97 system_info_get_platform_string("http://tizen.org/feature/profile",
99 if (profile_name == NULL)
102 switch (*profile_name) {
105 profile = TIZEN_PROFILE_MOBILE;
109 profile = TIZEN_PROFILE_WEARABLE;
113 profile = TIZEN_PROFILE_TV;
117 profile = TIZEN_PROFILE_IVI;
120 profile = TIZEN_PROFILE_COMMON;
129 static void __invoke_callback(void *event, int type)
131 GList *iter = __events;
134 appcore_base_event_node *node = iter->data;
136 if (node->type == type)
137 node->cb(event, node->data);
138 iter = g_list_next(iter);
142 static bool __exist_callback(int type)
144 GList *iter = __events;
147 appcore_base_event_node *node = iter->data;
149 if (node->type == type)
152 iter = g_list_next(iter);
158 static enum appcore_base_rm __get_rm(sensor_data_t data)
161 enum appcore_base_rm rm;
163 if (data.value_count <= 0) {
164 _ERR("Failed to get sensor data");
165 return APPCORE_BASE_RM_UNKNOWN;
168 event = data.values[0];
170 case AUTO_ROTATION_DEGREE_0:
171 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
173 case AUTO_ROTATION_DEGREE_90:
174 rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
176 case AUTO_ROTATION_DEGREE_180:
177 rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
179 case AUTO_ROTATION_DEGREE_270:
180 rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
183 rm = APPCORE_BASE_RM_UNKNOWN;
190 static void __lock_cb(keynode_t *node, void *user_data)
194 enum appcore_base_rm rm;
196 __rotation.lock = !vconf_keynode_get_bool(node);
197 if (__rotation.lock) {
198 _DBG("Rotation locked");
199 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
201 _DBG("Rotation unlocked");
202 r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
204 _ERR("Failed to get sensor data");
209 if (rm == APPCORE_BASE_RM_UNKNOWN) {
210 _ERR("Unknown mode");
215 if (__rotation.rm == rm)
218 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
220 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
223 static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
224 sensor_data_t *data, void *user_data)
226 enum appcore_base_rm rm;
234 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
237 rm = __get_rm(*data);
238 if (rm == APPCORE_BASE_RM_UNKNOWN) {
239 _ERR("Unknown mode");
243 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
245 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
248 static void __fini_rotation(void)
250 if (!__rotation.initialized)
253 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
254 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
255 sensord_stop(__rotation.conn);
256 sensord_disconnect(__rotation.conn);
259 __rotation.initialized = false;
262 static void __init_rotation(void)
268 if (__rotation.initialized)
271 sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
272 __rotation.conn = sensord_connect(sensor);
273 if (__rotation.conn < 0) {
274 _ERR("Failed to connect sensord");
278 r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
279 SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
281 _ERR("Failed to register auto rotation change event");
282 sensord_disconnect(__rotation.conn);
286 r = sensord_start(__rotation.conn, 0);
288 _ERR("Failed to start sensord");
289 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
290 sensord_disconnect(__rotation.conn);
295 vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
296 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
298 __rotation.lock = !lock;
299 __rotation.initialized = true;
302 static void __charger_status_changed_cb(keynode_t *keynode, void *user_data)
304 if (TIZEN_FEATURE_CHARGER_STATUS) {
305 __rotation.charger_status = vconf_keynode_get_int(keynode);
306 if (__rotation.charger_status) {
313 _DBG("charger status(%d)", __rotation.charger_status);
317 static void __unregister_rotation_changed_event(void)
323 if (__rotation.ref > 1)
327 if (TIZEN_FEATURE_CHARGER_STATUS) {
328 vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
329 __charger_status_changed_cb);
335 static void __register_rotation_changed_event(void)
337 if (__rotation.ref) {
342 if (TIZEN_FEATURE_CHARGER_STATUS) {
343 vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
344 &__rotation.charger_status);
345 vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
346 __charger_status_changed_cb, NULL);
347 if (__rotation.charger_status)
356 static void __on_low_memory(keynode_t *key, void *data)
360 val = vconf_keynode_get_int(key);
362 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
363 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
368 static void __on_low_battery(keynode_t *key, void *data)
372 val = vconf_keynode_get_int(key);
374 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
375 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
378 static void __destroy_lang_info(gpointer data)
380 struct lang_info_s *info = (struct lang_info_s *)data;
386 g_list_free_full(info->list, free);
392 static struct lang_info_s *__create_lang_info(const char *lang)
394 struct lang_info_s *info;
396 info = calloc(1, sizeof(struct lang_info_s));
398 _ERR("Out of memory");
402 info->parent = strdup(lang);
403 if (info->parent == NULL) {
404 _ERR("Out of memory");
412 static gint __compare_langs(gconstpointer a, gconstpointer b)
420 static char *__get_string_before(const char *str, const char *delim)
426 dup_str = strdup(str);
430 token = strtok(dup_str, delim);
436 new_str = strdup(token);
442 static GHashTable *__get_lang_table(void)
446 struct dirent *dentry;
448 struct stat stat_buf;
451 struct lang_info_s *info;
453 if (__locale_dir == NULL || __locale_dir[0] == '\0')
456 table = g_hash_table_new_full(g_str_hash, g_str_equal,
457 NULL, __destroy_lang_info);
459 _ERR("Out of memory");
463 dp = opendir(__locale_dir);
465 g_hash_table_destroy(table);
469 while ((dentry = readdir(dp)) != NULL) {
470 if (!strcmp(dentry->d_name, ".") ||
471 !strcmp(dentry->d_name, ".."))
474 snprintf(buf, sizeof(buf), "%s/%s",
475 __locale_dir, dentry->d_name);
476 ret = stat(buf, &stat_buf);
477 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
480 parent_lang = __get_string_before(dentry->d_name, "_");
481 if (parent_lang == NULL) {
482 _ERR("Out of memory");
486 info = g_hash_table_lookup(table, parent_lang);
488 info = __create_lang_info(parent_lang);
493 g_hash_table_insert(table, info->parent, info);
495 info->list = g_list_append(info->list, strdup(dentry->d_name));
503 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
505 struct lang_info_s *info;
507 char *parent_lang = NULL;
513 list = g_list_append(list, strdup(lang));
515 extract_lang = __get_string_before(lang, ".");
516 if (extract_lang == NULL)
519 found = g_list_find_custom(list, extract_lang, __compare_langs);
521 list = g_list_remove_link(list, found);
522 list = g_list_concat(list, found);
526 parent_lang = __get_string_before(extract_lang, "_");
527 if (parent_lang == NULL)
530 info = g_hash_table_lookup(table, parent_lang);
534 found = g_list_find_custom(info->list, extract_lang, __compare_langs);
536 info->list = g_list_remove_link(info->list, found);
537 list = g_list_concat(list, found);
541 found = g_list_find_custom(info->list, parent_lang, __compare_langs);
543 info->list = g_list_remove_link(info->list, found);
544 list = g_list_concat(list, found);
548 found = g_list_first(info->list);
550 info->list = g_list_remove_link(info->list, found);
551 list = g_list_concat(list, found);
563 static GList *__split_language(const char *lang)
569 dup_lang = strdup(lang);
570 if (dup_lang == NULL) {
571 _ERR("Out of memory");
575 token = strtok(dup_lang, ":");
576 while (token != NULL) {
577 list = g_list_append(list, strdup(token));
578 token = strtok(NULL, ":");
585 static GList *__append_default_langs(GList *list)
587 const char *langs[] = {"en_US", "en_GB", "en"};
591 for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
592 found = g_list_find_custom(list, langs[i], __compare_langs);
594 list = g_list_append(list, strdup(langs[i]));
600 static char *__get_language(const char *lang)
604 GList *lang_list = NULL;
607 char buf[LINE_MAX] = {'\0'};
610 list = __split_language(lang);
614 table = __get_lang_table();
616 g_list_free_full(list, free);
620 iter = g_list_first(list);
622 language = (char *)iter->data;
623 lang_list = __append_langs(language, lang_list, table);
624 iter = g_list_next(iter);
626 g_list_free_full(list, free);
627 g_hash_table_destroy(table);
629 lang_list = __append_default_langs(lang_list);
630 iter = g_list_first(lang_list);
632 language = (char *)iter->data;
634 if (buf[0] == '\0') {
635 snprintf(buf, sizeof(buf), "%s", language);
637 n = sizeof(buf) - strlen(buf) - 1;
638 strncat(buf, ":", n);
639 n = sizeof(buf) - strlen(buf) - 1;
640 strncat(buf, language, n);
643 iter = g_list_next(iter);
645 g_list_free_full(lang_list, free);
650 static void __update_lang(void)
656 lang = vconf_get_str(VCONFKEY_LANGSET);
658 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
659 language = __get_language(lang);
661 _DBG("*****language(%s)", language);
662 setenv("LANGUAGE", language, 1);
665 setenv("LANGUAGE", lang, 1);
667 setenv("LANG", lang, 1);
668 setenv("LC_MESSAGES", lang, 1);
669 r = setlocale(LC_ALL, "");
671 r = setlocale(LC_ALL, "en_US.UTF-8");
673 _DBG("*****appcore setlocale=%s\n", r);
675 _DBG("*****appcore setlocale=\"C\"");
676 setenv("LC_ALL", "C", 1);
677 r = setlocale(LC_ALL, "");
679 _ERR("failed to setlocale");
686 static void __update_region(void)
691 region = vconf_get_str(VCONFKEY_REGIONFORMAT);
693 setenv("LC_CTYPE", region, 1);
694 setenv("LC_NUMERIC", region, 1);
695 setenv("LC_TIME", region, 1);
696 setenv("LC_COLLATE", region, 1);
697 setenv("LC_MONETARY", region, 1);
698 setenv("LC_PAPER", region, 1);
699 setenv("LC_NAME", region, 1);
700 setenv("LC_ADDRESS", region, 1);
701 setenv("LC_TELEPHONE", region, 1);
702 setenv("LC_MEASUREMENT", region, 1);
703 setenv("LC_IDENTIFICATION", region, 1);
704 r = setlocale(LC_ALL, "");
706 _DBG("*****appcore setlocale=%s\n", r);
708 _DBG("*****appcore setlocale=\"C\"");
709 setenv("LC_ALL", "C", 1);
710 r = setlocale(LC_ALL, "");
712 _ERR("failed to setlocale");
719 static void __on_language_change(keynode_t *key, void *data)
724 g_source_remove(__context.sid);
728 val = vconf_keynode_get_str(key);
731 __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
734 static void __on_region_change(keynode_t *key, void *data)
739 name = vconf_keynode_get_name(key);
743 if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
744 strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
747 val = vconf_get_str(VCONFKEY_REGIONFORMAT);
750 __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
754 static gboolean __invoke_lang_change(gpointer data)
760 lang = getenv("LANG");
762 return G_SOURCE_REMOVE;
764 __invoke_callback((void *)lang, APPCORE_BASE_EVENT_LANG_CHANGE);
766 return G_SOURCE_REMOVE;
769 static void __verify_language(void)
772 const char *env_lang;
774 env_lang = getenv("LANG");
778 lang = vconf_get_str(VCONFKEY_LANGSET);
782 if (strcmp(env_lang, lang) != 0) {
783 _INFO("LANG(%s), LANGSET(%s)", env_lang, lang);
784 __context.sid = g_idle_add(__invoke_lang_change, NULL);
790 static gboolean __flush_memory(gpointer data)
792 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
794 appcore_base_flush_memory();
797 if (!__context.allowed_bg && !__context.suspended_state) {
798 _DBG("[__SUSPEND__] flush case");
799 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
800 __context.suspended_state = true;
806 static void __add_suspend_timer(void)
808 __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
811 static void __remove_suspend_timer(void)
813 if (__context.tid > 0) {
814 g_source_remove(__context.tid);
819 static void __on_receive_suspend_signal(GDBusConnection *connection,
820 const gchar *sender_name,
821 const gchar *object_path,
822 const gchar *interface_name,
823 const gchar *signal_name,
824 GVariant *parameters,
827 gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
831 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
832 g_variant_get(parameters, "(ii)", &status, &pid);
833 if (pid == getpid() && status == 0) {
834 if (!__context.allowed_bg && __context.suspended_state) {
835 __remove_suspend_timer();
836 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
837 __context.suspended_state = false;
838 __add_suspend_timer();
844 static int __init_suspend_dbus_handler(void)
848 if (__suspend_dbus_handler_initialized)
852 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
854 _ERR("Failed to connect to the D-BUS daemon: %s",
861 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
864 RESOURCED_FREEZER_INTERFACE,
865 RESOURCED_FREEZER_SIGNAL,
866 RESOURCED_FREEZER_PATH,
868 G_DBUS_SIGNAL_FLAGS_NONE,
869 __on_receive_suspend_signal,
872 if (__suspend_dbus_handler_initialized == 0) {
873 _ERR("g_dbus_connection_signal_subscribe() is failed.");
877 _DBG("[__SUSPEND__] suspend signal initialized");
882 static void __fini_suspend_dbus_handler(void)
887 if (__suspend_dbus_handler_initialized) {
888 g_dbus_connection_signal_unsubscribe(__bus,
889 __suspend_dbus_handler_initialized);
890 __suspend_dbus_handler_initialized = 0;
893 g_object_unref(__bus);
897 static gboolean __init_suspend(gpointer data)
899 __init_suspend_dbus_handler();
900 return G_SOURCE_REMOVE;
903 static int __get_locale_resource_dir(char *locale_dir, int size)
905 const char *res_path;
907 res_path = aul_get_app_resource_path();
908 if (res_path == NULL) {
909 _ERR("Failed to get resource path");
913 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
914 if (access(locale_dir, R_OK) != 0)
915 _DBG("%s does not exist", locale_dir);
920 static int __get_app_name(const char *appid, char **name)
922 char *name_token = NULL;
927 /* com.vendor.name -> name */
928 name_token = strrchr(appid, '.');
929 if (name_token == NULL)
934 *name = strdup(name_token);
941 static int __set_i18n(const char *domain, const char *dir)
945 if (domain == NULL) {
953 __locale_dir = strdup(dir);
959 r = setlocale(LC_ALL, "");
960 /* if locale is not set properly, try to set "en_US" again */
962 r = setlocale(LC_ALL, "en_US.UTF-8");
964 _DBG("*****appcore setlocale=%s\n", r);
967 _ERR("appcore: setlocale() error");
968 _DBG("*****appcore setlocale=\"C\"");
969 setenv("LC_ALL", "C", 1);
970 r = setlocale(LC_ALL, "");
972 _ERR("failed to setlocale");
975 r = bindtextdomain(domain, dir);
977 _ERR("appcore: bindtextdomain() error");
979 r = textdomain(domain);
981 _ERR("appcore: textdomain() error");
986 EXPORT_API int appcore_base_on_set_i18n(void)
989 char locale_dir[PATH_MAX];
990 char appid[PATH_MAX];
993 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
995 _ERR("Failed to get application ID - pid(%d)", getpid());
999 r = __get_app_name(appid, &name);
1003 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
1009 r = __set_i18n(name, locale_dir);
1020 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
1022 return __set_i18n(domain_name, dir_name);
1025 static void __set_default_events(void)
1029 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1030 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1032 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1033 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1036 static void __unset_default_events(void)
1040 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1041 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1043 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1044 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1047 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
1052 __context.ops = ops;
1053 __context.argc = argc;
1054 __context.argv = argv;
1055 __context.data = data;
1057 __context.suspended_state = false;
1058 __context.allowed_bg = false;
1060 if (__context.ops.init)
1061 __context.ops.init(argc, argv, data);
1063 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1064 g_idle_add(__init_suspend, NULL);
1066 if (!__context.dirty) {
1067 __context.dirty = true;
1069 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1070 if (__exist_callback(i)) {
1071 if (__context.ops.set_event)
1072 __context.ops.set_event(i, __context.data);
1077 __verify_language();
1078 __set_default_events();
1079 if (__context.ops.set_i18n)
1080 __context.ops.set_i18n(__context.data);
1082 if (__context.ops.create) {
1083 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1084 r = __context.ops.create(__context.data);
1085 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1087 aul_status_update(STATUS_DYING);
1092 if (__context.ops.run)
1093 __context.ops.run(__context.data);
1098 EXPORT_API void appcore_base_fini(void)
1102 aul_status_update(STATUS_DYING);
1103 if (__context.ops.terminate) {
1104 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1105 __context.ops.terminate(__context.data);
1106 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1109 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1110 if (__exist_callback(i)) {
1111 if (__context.ops.unset_event)
1112 __context.ops.unset_event(i, __context.data);
1116 __unset_default_events();
1118 if (__context.sid) {
1119 g_source_remove(__context.sid);
1123 g_list_free_full(__events, free);
1126 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1127 __fini_suspend_dbus_handler();
1131 __locale_dir = NULL;
1134 __context.dirty = false;
1136 if (__context.ops.finish)
1137 __context.ops.finish();
1140 EXPORT_API int appcore_base_flush_memory(void)
1142 int (*sqlite3_free_heap_memory)(int);
1143 void (*elm_free_all_cache)(void);
1145 elm_free_all_cache = dlsym(RTLD_DEFAULT, "elm_cache_all_flush");
1146 if (elm_free_all_cache)
1147 elm_free_all_cache();
1149 sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
1150 "sqlite3_release_memory");
1151 if (sqlite3_free_heap_memory)
1152 sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
1158 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1165 _DBG("[APP %d] AUL event: AUL_START", getpid());
1166 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1167 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1168 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1169 _DBG("[__SUSPEND__] allowed background");
1170 __context.allowed_bg = true;
1171 __remove_suspend_timer();
1175 if (__context.ops.control) {
1176 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1177 __context.ops.control(b, __context.data);
1178 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1182 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1183 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1184 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1185 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1186 _DBG("[__SUSPEND__] allowed background");
1187 __context.allowed_bg = true;
1188 __remove_suspend_timer();
1193 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1194 aul_status_update(STATUS_DYING);
1195 if (!__context.allowed_bg)
1196 __remove_suspend_timer();
1198 if (__context.ops.exit)
1199 __context.ops.exit(__context.data);
1201 case AUL_TERMINATE_BGAPP:
1202 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", getpid());
1203 if (!__context.allowed_bg)
1204 __remove_suspend_timer();
1207 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1208 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1209 if (!__context.allowed_bg &&
1210 __context.suspended_state) {
1211 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1212 __remove_suspend_timer();
1213 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1214 __context.suspended_state = false;
1219 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1220 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1221 if (!__context.allowed_bg &&
1222 !__context.suspended_state) {
1223 __remove_suspend_timer();
1224 __flush_memory(NULL);
1228 case AUL_UPDATE_REQUESTED:
1229 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1230 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1233 _DBG("[APP %d] AUL event: %d", getpid(), type);
1241 EXPORT_API int appcore_base_on_create(void)
1245 r = aul_launch_init(__context.ops.receive, NULL);
1247 _ERR("Aul init failed: %d", r);
1251 r = aul_launch_argv_handler(__context.argc, __context.argv);
1253 _ERR("Aul argv handler failed: %d", r);
1260 EXPORT_API int appcore_base_on_control(bundle *b)
1265 EXPORT_API int appcore_base_on_terminate()
1272 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1275 case APPCORE_BASE_EVENT_LOW_BATTERY:
1276 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1278 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1279 __register_rotation_changed_event();
1281 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1290 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1293 case APPCORE_BASE_EVENT_LOW_BATTERY:
1294 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1296 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1297 __unregister_rotation_changed_event();
1299 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1306 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1307 appcore_base_event_cb cb, void *data)
1309 appcore_base_event_node *node;
1311 if (__context.dirty && !__exist_callback(event)) {
1312 if (__context.ops.set_event)
1313 __context.ops.set_event(event, __context.data);
1316 node = malloc(sizeof(appcore_base_event_node));
1324 __events = g_list_append(__events, node);
1329 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1331 appcore_base_event_node *node = handle;
1332 enum appcore_base_event event;
1334 if (!node || !g_list_find(__events, node))
1338 __events = g_list_remove(__events, node);
1340 if (__context.dirty && !__exist_callback(event)) {
1341 if (__context.ops.unset_event)
1342 __context.ops.unset_event(event, __context.data);
1348 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1350 __invoke_callback(event, type);
1354 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1359 if (!__rotation.ref)
1362 *curr = __rotation.rm;
1366 EXPORT_API bool appcore_base_is_bg_allowed(void)
1368 return __context.allowed_bg;
1371 EXPORT_API bool appcore_base_is_suspended(void)
1373 return __context.suspended_state;
1376 EXPORT_API void appcore_base_toggle_suspended_state(void)
1378 __context.suspended_state ^= __context.suspended_state;
1381 EXPORT_API void appcore_base_exit(void)
1383 if (__context.ops.exit)
1384 __context.ops.exit(__context.data);
1387 EXPORT_API void appcore_base_add_suspend_timer(void)
1389 __add_suspend_timer();
1392 EXPORT_API void appcore_base_remove_suspend_timer(void)
1394 __remove_suspend_timer();
1397 static int __on_receive(aul_type type, bundle *b, void *data)
1399 return appcore_base_on_receive(type, b);
1402 static int __on_create(void *data)
1404 return appcore_base_on_create();
1407 static int __on_control(bundle *b, void *data)
1409 return appcore_base_on_control(b);
1412 static int __on_terminate(void *data)
1414 return appcore_base_on_terminate();
1417 static int __on_set_i18n(void *data)
1419 return appcore_base_on_set_i18n();
1422 static void __on_set_event(enum appcore_base_event event, void *data)
1424 return appcore_base_on_set_event(event);
1427 static void __on_unset_event(enum appcore_base_event event, void *data)
1429 return appcore_base_on_unset_event(event);
1432 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1434 appcore_base_ops ops;
1436 ops.create = __on_create;
1437 ops.control = __on_control;
1438 ops.terminate = __on_terminate;
1439 ops.receive = __on_receive;
1440 ops.set_i18n = __on_set_i18n;
1445 ops.set_event = __on_set_event;
1446 ops.unset_event = __on_unset_event;