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>
42 #include "appcore_base.h"
43 #include "appcore_base_private.h"
44 #include "appcore_watchdog.h"
46 #define PATH_LOCALE "locale"
47 #define RESOURCED_FREEZER_PATH "/Org/Tizen/ResourceD/Freezer"
48 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
49 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
50 #define SQLITE_FLUSH_MAX (1024 * 1024)
52 typedef struct _appcore_base_context {
62 } appcore_base_context;
64 typedef struct _appcore_base_event_node {
66 appcore_base_event_cb cb;
68 } appcore_base_event_node;
70 typedef struct _appcore_base_rotation {
74 enum appcore_base_rm rm;
77 } appcore_base_rotation;
84 static appcore_base_context __context;
85 static GList *__events;
86 static GDBusConnection *__bus;
87 static guint __suspend_dbus_handler_initialized;
88 static char *__locale_dir;
89 static appcore_base_rotation __rotation;
91 appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
93 static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
94 char *profile_name = NULL;
96 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
99 system_info_get_platform_string("http://tizen.org/feature/profile",
101 if (profile_name == NULL)
104 switch (*profile_name) {
107 profile = TIZEN_PROFILE_MOBILE;
111 profile = TIZEN_PROFILE_WEARABLE;
115 profile = TIZEN_PROFILE_TV;
119 profile = TIZEN_PROFILE_IVI;
122 profile = TIZEN_PROFILE_COMMON;
131 static void __invoke_callback(void *event, int type)
133 GList *iter = __events;
136 appcore_base_event_node *node = iter->data;
138 if (node->type == type)
139 node->cb(event, node->data);
140 iter = g_list_next(iter);
144 static bool __exist_callback(int type)
146 GList *iter = __events;
149 appcore_base_event_node *node = iter->data;
151 if (node->type == type)
154 iter = g_list_next(iter);
160 static enum appcore_base_rm __get_rm(sensor_data_t data)
163 enum appcore_base_rm rm;
165 if (data.value_count <= 0) {
166 _ERR("Failed to get sensor data");
167 return APPCORE_BASE_RM_UNKNOWN;
170 event = data.values[0];
172 case AUTO_ROTATION_DEGREE_0:
173 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
175 case AUTO_ROTATION_DEGREE_90:
176 rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
178 case AUTO_ROTATION_DEGREE_180:
179 rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
181 case AUTO_ROTATION_DEGREE_270:
182 rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
185 rm = APPCORE_BASE_RM_UNKNOWN;
192 static void __lock_cb(keynode_t *node, void *user_data)
196 enum appcore_base_rm rm;
198 __rotation.lock = !vconf_keynode_get_bool(node);
199 if (__rotation.lock) {
200 _DBG("Rotation locked");
201 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
203 _DBG("Rotation unlocked");
204 r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
206 _ERR("Failed to get sensor data");
211 if (rm == APPCORE_BASE_RM_UNKNOWN) {
212 _ERR("Unknown mode");
217 if (__rotation.rm == rm)
220 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
222 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
225 static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
226 sensor_data_t *data, void *user_data)
228 enum appcore_base_rm rm;
236 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
239 rm = __get_rm(*data);
240 if (rm == APPCORE_BASE_RM_UNKNOWN) {
241 _ERR("Unknown mode");
245 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
247 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
250 static void __fini_rotation(void)
252 if (!__rotation.initialized)
255 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
256 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
257 sensord_stop(__rotation.conn);
258 sensord_disconnect(__rotation.conn);
261 __rotation.initialized = false;
264 static void __init_rotation(void)
270 if (__rotation.initialized)
273 sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
274 __rotation.conn = sensord_connect(sensor);
275 if (__rotation.conn < 0) {
276 _ERR("Failed to connect sensord");
280 r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
281 SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
283 _ERR("Failed to register auto rotation change event");
284 sensord_disconnect(__rotation.conn);
288 r = sensord_start(__rotation.conn, 0);
290 _ERR("Failed to start sensord");
291 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
292 sensord_disconnect(__rotation.conn);
297 vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
298 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
300 __rotation.lock = !lock;
301 __rotation.initialized = true;
304 static void __charger_status_changed_cb(keynode_t *keynode, void *user_data)
306 if (TIZEN_FEATURE_CHARGER_STATUS) {
307 __rotation.charger_status = vconf_keynode_get_int(keynode);
308 if (__rotation.charger_status) {
315 _DBG("charger status(%d)", __rotation.charger_status);
319 static void __unregister_rotation_changed_event(void)
325 if (__rotation.ref > 1)
329 if (TIZEN_FEATURE_CHARGER_STATUS) {
330 vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
331 __charger_status_changed_cb);
337 static void __register_rotation_changed_event(void)
339 if (__rotation.ref) {
344 if (TIZEN_FEATURE_CHARGER_STATUS) {
345 vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
346 &__rotation.charger_status);
347 vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
348 __charger_status_changed_cb, NULL);
349 if (__rotation.charger_status)
358 static void __on_low_memory(keynode_t *key, void *data)
362 val = vconf_keynode_get_int(key);
364 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
365 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
370 static void __on_low_battery(keynode_t *key, void *data)
374 val = vconf_keynode_get_int(key);
376 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
377 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
380 static void __destroy_lang_info(gpointer data)
382 struct lang_info_s *info = (struct lang_info_s *)data;
388 g_list_free_full(info->list, free);
394 static struct lang_info_s *__create_lang_info(const char *lang)
396 struct lang_info_s *info;
398 info = calloc(1, sizeof(struct lang_info_s));
400 _ERR("Out of memory");
404 info->parent = strdup(lang);
405 if (info->parent == NULL) {
406 _ERR("Out of memory");
414 static gint __compare_langs(gconstpointer a, gconstpointer b)
422 static char *__get_string_before(const char *str, const char *delim)
428 dup_str = strdup(str);
432 token = strtok(dup_str, delim);
438 new_str = strdup(token);
444 static GHashTable *__get_lang_table(void)
448 struct dirent *dentry;
450 struct stat stat_buf;
453 struct lang_info_s *info;
455 if (__locale_dir == NULL || __locale_dir[0] == '\0')
458 table = g_hash_table_new_full(g_str_hash, g_str_equal,
459 NULL, __destroy_lang_info);
461 _ERR("Out of memory");
465 dp = opendir(__locale_dir);
467 g_hash_table_destroy(table);
471 while ((dentry = readdir(dp)) != NULL) {
472 if (!strcmp(dentry->d_name, ".") ||
473 !strcmp(dentry->d_name, ".."))
476 snprintf(buf, sizeof(buf), "%s/%s",
477 __locale_dir, dentry->d_name);
478 ret = stat(buf, &stat_buf);
479 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
482 parent_lang = __get_string_before(dentry->d_name, "_");
483 if (parent_lang == NULL) {
484 _ERR("Out of memory");
488 info = g_hash_table_lookup(table, parent_lang);
490 info = __create_lang_info(parent_lang);
495 g_hash_table_insert(table, info->parent, info);
497 info->list = g_list_append(info->list, strdup(dentry->d_name));
505 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
507 struct lang_info_s *info;
509 char *parent_lang = NULL;
515 list = g_list_append(list, strdup(lang));
517 extract_lang = __get_string_before(lang, ".");
518 if (extract_lang == NULL)
521 found = g_list_find_custom(list, extract_lang, __compare_langs);
523 list = g_list_remove_link(list, found);
524 list = g_list_concat(list, found);
528 parent_lang = __get_string_before(extract_lang, "_");
529 if (parent_lang == NULL)
532 info = g_hash_table_lookup(table, parent_lang);
536 found = g_list_find_custom(info->list, extract_lang, __compare_langs);
538 info->list = g_list_remove_link(info->list, found);
539 list = g_list_concat(list, found);
543 found = g_list_find_custom(info->list, parent_lang, __compare_langs);
545 info->list = g_list_remove_link(info->list, found);
546 list = g_list_concat(list, found);
550 found = g_list_first(info->list);
552 info->list = g_list_remove_link(info->list, found);
553 list = g_list_concat(list, found);
565 static GList *__split_language(const char *lang)
571 dup_lang = strdup(lang);
572 if (dup_lang == NULL) {
573 _ERR("Out of memory");
577 token = strtok(dup_lang, ":");
578 while (token != NULL) {
579 list = g_list_append(list, strdup(token));
580 token = strtok(NULL, ":");
587 static GList *__append_default_langs(GList *list)
589 const char *langs[] = {"en_US", "en_GB", "en"};
593 for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
594 found = g_list_find_custom(list, langs[i], __compare_langs);
596 list = g_list_append(list, strdup(langs[i]));
602 static char *__get_language(const char *lang)
606 GList *lang_list = NULL;
609 char buf[LINE_MAX] = {'\0'};
612 list = __split_language(lang);
616 table = __get_lang_table();
618 g_list_free_full(list, free);
622 iter = g_list_first(list);
624 language = (char *)iter->data;
625 lang_list = __append_langs(language, lang_list, table);
626 iter = g_list_next(iter);
628 g_list_free_full(list, free);
629 g_hash_table_destroy(table);
631 lang_list = __append_default_langs(lang_list);
632 iter = g_list_first(lang_list);
634 language = (char *)iter->data;
636 if (buf[0] == '\0') {
637 snprintf(buf, sizeof(buf), "%s", language);
639 n = sizeof(buf) - strlen(buf) - 1;
640 strncat(buf, ":", n);
641 n = sizeof(buf) - strlen(buf) - 1;
642 strncat(buf, language, n);
645 iter = g_list_next(iter);
647 g_list_free_full(lang_list, free);
652 static void __update_lang(void)
658 lang = vconf_get_str(VCONFKEY_LANGSET);
660 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
661 language = __get_language(lang);
663 _DBG("*****language(%s)", language);
664 setenv("LANGUAGE", language, 1);
667 setenv("LANGUAGE", lang, 1);
669 setenv("LANG", lang, 1);
670 setenv("LC_MESSAGES", lang, 1);
671 r = setlocale(LC_ALL, "");
673 r = setlocale(LC_ALL, "en_US.UTF-8");
675 _DBG("*****appcore setlocale=%s\n", r);
677 _DBG("*****appcore setlocale=\"C\"");
678 setenv("LC_ALL", "C", 1);
679 r = setlocale(LC_ALL, "");
681 _ERR("failed to setlocale");
688 static void __update_region(void)
693 region = vconf_get_str(VCONFKEY_REGIONFORMAT);
695 setenv("LC_CTYPE", region, 1);
696 setenv("LC_NUMERIC", region, 1);
697 setenv("LC_TIME", region, 1);
698 setenv("LC_COLLATE", region, 1);
699 setenv("LC_MONETARY", region, 1);
700 setenv("LC_PAPER", region, 1);
701 setenv("LC_NAME", region, 1);
702 setenv("LC_ADDRESS", region, 1);
703 setenv("LC_TELEPHONE", region, 1);
704 setenv("LC_MEASUREMENT", region, 1);
705 setenv("LC_IDENTIFICATION", region, 1);
706 r = setlocale(LC_ALL, "");
708 _DBG("*****appcore setlocale=%s\n", r);
710 _DBG("*****appcore setlocale=\"C\"");
711 setenv("LC_ALL", "C", 1);
712 r = setlocale(LC_ALL, "");
714 _ERR("failed to setlocale");
721 static void __on_language_change(keynode_t *key, void *data)
726 g_source_remove(__context.sid);
730 val = vconf_keynode_get_str(key);
733 __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
736 static void __on_region_change(keynode_t *key, void *data)
741 name = vconf_keynode_get_name(key);
745 if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
746 strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
749 val = vconf_get_str(VCONFKEY_REGIONFORMAT);
752 __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
756 static gboolean __invoke_lang_change(gpointer data)
762 lang = getenv("LANG");
764 return G_SOURCE_REMOVE;
766 __invoke_callback((void *)lang, APPCORE_BASE_EVENT_LANG_CHANGE);
768 return G_SOURCE_REMOVE;
771 static void __verify_language(void)
774 const char *env_lang;
776 env_lang = getenv("LANG");
780 lang = vconf_get_str(VCONFKEY_LANGSET);
784 if (strcmp(env_lang, lang) != 0) {
785 _INFO("LANG(%s), LANGSET(%s)", env_lang, lang);
786 __context.sid = g_idle_add(__invoke_lang_change, NULL);
792 static gboolean __flush_memory(gpointer data)
794 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
796 if (__context.ops.trim_memory)
797 __context.ops.trim_memory(__context.data);
801 if (!__context.allowed_bg && !__context.suspended_state) {
802 _DBG("[__SUSPEND__] flush case");
803 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
804 __context.suspended_state = true;
810 static void __add_suspend_timer(void)
812 __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
815 static void __remove_suspend_timer(void)
817 if (__context.tid > 0) {
818 g_source_remove(__context.tid);
823 static void __on_receive_suspend_signal(GDBusConnection *connection,
824 const gchar *sender_name,
825 const gchar *object_path,
826 const gchar *interface_name,
827 const gchar *signal_name,
828 GVariant *parameters,
831 gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
835 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
836 g_variant_get(parameters, "(ii)", &status, &pid);
837 if (pid == getpid() && status == 0) {
838 if (!__context.allowed_bg && __context.suspended_state) {
839 __remove_suspend_timer();
840 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
841 __context.suspended_state = false;
842 __add_suspend_timer();
848 static int __init_suspend_dbus_handler(void)
852 if (__suspend_dbus_handler_initialized)
856 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
858 _ERR("Failed to connect to the D-BUS daemon: %s",
865 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
868 RESOURCED_FREEZER_INTERFACE,
869 RESOURCED_FREEZER_SIGNAL,
870 RESOURCED_FREEZER_PATH,
872 G_DBUS_SIGNAL_FLAGS_NONE,
873 __on_receive_suspend_signal,
876 if (__suspend_dbus_handler_initialized == 0) {
877 _ERR("g_dbus_connection_signal_subscribe() is failed.");
881 _DBG("[__SUSPEND__] suspend signal initialized");
886 static void __fini_suspend_dbus_handler(void)
891 if (__suspend_dbus_handler_initialized) {
892 g_dbus_connection_signal_unsubscribe(__bus,
893 __suspend_dbus_handler_initialized);
894 __suspend_dbus_handler_initialized = 0;
897 g_object_unref(__bus);
901 static gboolean __init_suspend(gpointer data)
903 __init_suspend_dbus_handler();
904 return G_SOURCE_REMOVE;
907 static int __get_locale_resource_dir(char *locale_dir, int size)
909 const char *res_path;
911 res_path = aul_get_app_resource_path();
912 if (res_path == NULL) {
913 _ERR("Failed to get resource path");
917 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
918 if (access(locale_dir, R_OK) != 0)
919 _DBG("%s does not exist", locale_dir);
924 static int __get_app_name(const char *appid, char **name)
926 char *name_token = NULL;
931 /* com.vendor.name -> name */
932 name_token = strrchr(appid, '.');
933 if (name_token == NULL)
938 *name = strdup(name_token);
945 static int __set_i18n(const char *domain, const char *dir)
949 if (domain == NULL) {
957 __locale_dir = strdup(dir);
963 r = setlocale(LC_ALL, "");
964 /* if locale is not set properly, try to set "en_US" again */
966 r = setlocale(LC_ALL, "en_US.UTF-8");
968 _DBG("*****appcore setlocale=%s\n", r);
971 _ERR("appcore: setlocale() error");
972 _DBG("*****appcore setlocale=\"C\"");
973 setenv("LC_ALL", "C", 1);
974 r = setlocale(LC_ALL, "");
976 _ERR("failed to setlocale");
979 r = bindtextdomain(domain, dir);
981 _ERR("appcore: bindtextdomain() error");
983 r = textdomain(domain);
985 _ERR("appcore: textdomain() error");
990 EXPORT_API int appcore_base_on_set_i18n(void)
993 char locale_dir[PATH_MAX];
994 char appid[PATH_MAX];
997 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
999 _ERR("Failed to get application ID - pid(%d)", getpid());
1003 r = __get_app_name(appid, &name);
1007 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
1013 r = __set_i18n(name, locale_dir);
1024 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
1026 return __set_i18n(domain_name, dir_name);
1029 static void __set_default_events(void)
1033 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1034 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1036 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1037 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1040 static void __unset_default_events(void)
1044 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1045 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1047 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1048 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1051 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
1056 __context.ops = ops;
1057 __context.argc = argc;
1058 __context.argv = argv;
1059 __context.data = data;
1061 __context.suspended_state = false;
1062 __context.allowed_bg = false;
1064 if (__context.ops.init)
1065 __context.ops.init(argc, argv, data);
1067 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1068 g_idle_add(__init_suspend, NULL);
1070 if (!__context.dirty) {
1071 __context.dirty = true;
1073 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1074 if (__exist_callback(i)) {
1075 if (__context.ops.set_event)
1076 __context.ops.set_event(i, __context.data);
1081 __verify_language();
1082 __set_default_events();
1083 if (__context.ops.set_i18n)
1084 __context.ops.set_i18n(__context.data);
1086 appcore_watchdog_enable();
1088 if (__context.ops.create) {
1089 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1090 r = __context.ops.create(__context.data);
1091 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1093 aul_status_update(STATUS_DYING);
1098 if (__context.ops.run)
1099 __context.ops.run(__context.data);
1104 EXPORT_API void appcore_base_fini(void)
1108 aul_status_update(STATUS_DYING);
1109 appcore_watchdog_disable();
1110 if (__context.ops.terminate) {
1111 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1112 __context.ops.terminate(__context.data);
1113 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1116 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1117 if (__exist_callback(i)) {
1118 if (__context.ops.unset_event)
1119 __context.ops.unset_event(i, __context.data);
1123 __unset_default_events();
1125 if (__context.sid) {
1126 g_source_remove(__context.sid);
1130 g_list_free_full(__events, free);
1133 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1134 __fini_suspend_dbus_handler();
1138 __locale_dir = NULL;
1141 __context.dirty = false;
1143 if (__context.ops.finish)
1144 __context.ops.finish();
1147 EXPORT_API int appcore_base_flush_memory(void)
1149 if (__context.ops.trim_memory)
1150 __context.ops.trim_memory(__context.data);
1155 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1162 _DBG("[APP %d] AUL event: AUL_START", getpid());
1163 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1164 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1165 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1166 _DBG("[__SUSPEND__] allowed background");
1167 __context.allowed_bg = true;
1168 __remove_suspend_timer();
1172 if (__context.ops.control) {
1173 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1174 __context.ops.control(b, __context.data);
1175 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1179 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1180 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1181 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1182 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1183 _DBG("[__SUSPEND__] allowed background");
1184 __context.allowed_bg = true;
1185 __remove_suspend_timer();
1190 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1191 aul_status_update(STATUS_DYING);
1192 if (!__context.allowed_bg)
1193 __remove_suspend_timer();
1195 if (__context.ops.exit)
1196 __context.ops.exit(__context.data);
1198 case AUL_TERMINATE_BGAPP:
1199 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", getpid());
1200 if (!__context.allowed_bg)
1201 __remove_suspend_timer();
1204 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1205 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1206 if (!__context.allowed_bg &&
1207 __context.suspended_state) {
1208 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1209 __remove_suspend_timer();
1210 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1211 __context.suspended_state = false;
1216 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1217 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1218 if (!__context.allowed_bg &&
1219 !__context.suspended_state) {
1220 __remove_suspend_timer();
1221 __flush_memory(NULL);
1225 case AUL_UPDATE_REQUESTED:
1226 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1227 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1230 _DBG("[APP %d] AUL event: %d", getpid(), type);
1238 EXPORT_API int appcore_base_on_create(void)
1242 r = aul_launch_init(__context.ops.receive, NULL);
1244 _ERR("Aul init failed: %d", r);
1248 r = aul_launch_argv_handler(__context.argc, __context.argv);
1250 _ERR("Aul argv handler failed: %d", r);
1257 EXPORT_API int appcore_base_on_control(bundle *b)
1262 EXPORT_API int appcore_base_on_terminate()
1269 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1272 case APPCORE_BASE_EVENT_LOW_BATTERY:
1273 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1275 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1276 __register_rotation_changed_event();
1278 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1287 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1290 case APPCORE_BASE_EVENT_LOW_BATTERY:
1291 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1293 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1294 __unregister_rotation_changed_event();
1296 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1303 EXPORT_API int appcore_base_on_trim_memory(void)
1305 int (*sqlite3_free_heap_memory)(int);
1307 sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
1308 "sqlite3_release_memory");
1309 if (sqlite3_free_heap_memory)
1310 sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
1317 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1318 appcore_base_event_cb cb, void *data)
1320 appcore_base_event_node *node;
1322 if (__context.dirty && !__exist_callback(event)) {
1323 if (__context.ops.set_event)
1324 __context.ops.set_event(event, __context.data);
1327 node = malloc(sizeof(appcore_base_event_node));
1335 __events = g_list_append(__events, node);
1340 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1342 appcore_base_event_node *node = handle;
1343 enum appcore_base_event event;
1345 if (!node || !g_list_find(__events, node))
1349 __events = g_list_remove(__events, node);
1351 if (__context.dirty && !__exist_callback(event)) {
1352 if (__context.ops.unset_event)
1353 __context.ops.unset_event(event, __context.data);
1359 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1361 __invoke_callback(event, type);
1365 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1370 if (!__rotation.ref)
1373 *curr = __rotation.rm;
1377 EXPORT_API bool appcore_base_is_bg_allowed(void)
1379 return __context.allowed_bg;
1382 EXPORT_API bool appcore_base_is_suspended(void)
1384 return __context.suspended_state;
1387 EXPORT_API void appcore_base_toggle_suspended_state(void)
1389 __context.suspended_state ^= __context.suspended_state;
1392 EXPORT_API void appcore_base_exit(void)
1394 if (__context.ops.exit)
1395 __context.ops.exit(__context.data);
1398 EXPORT_API void appcore_base_add_suspend_timer(void)
1400 __add_suspend_timer();
1403 EXPORT_API void appcore_base_remove_suspend_timer(void)
1405 __remove_suspend_timer();
1408 static int __on_receive(aul_type type, bundle *b, void *data)
1410 return appcore_base_on_receive(type, b);
1413 static int __on_create(void *data)
1415 return appcore_base_on_create();
1418 static int __on_control(bundle *b, void *data)
1420 return appcore_base_on_control(b);
1423 static int __on_terminate(void *data)
1425 return appcore_base_on_terminate();
1428 static int __on_set_i18n(void *data)
1430 return appcore_base_on_set_i18n();
1433 static void __on_set_event(enum appcore_base_event event, void *data)
1435 appcore_base_on_set_event(event);
1438 static void __on_unset_event(enum appcore_base_event event, void *data)
1440 appcore_base_on_unset_event(event);
1443 static void __on_trim_memory(void *data)
1445 appcore_base_on_trim_memory();
1448 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1450 appcore_base_ops ops;
1452 ops.create = __on_create;
1453 ops.control = __on_control;
1454 ops.terminate = __on_terminate;
1455 ops.receive = __on_receive;
1456 ops.set_i18n = __on_set_i18n;
1461 ops.set_event = __on_set_event;
1462 ops.unset_event = __on_unset_event;
1463 ops.trim_memory = __on_trim_memory;