2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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.
18 #include <aul_app_lifecycle.h>
19 #include <aul_watchdog.h>
20 #include <bundle_internal.h>
26 #include <linux/limits.h>
29 #include <sensor_internal.h>
33 #include <sys/types.h>
34 #include <system_info.h>
49 #include "app-core-cpp/app_core_base.hh"
50 #include "common/glib_private.hh"
51 #include "common/log_private.hh"
53 extern "C" void aul_finalize();
56 AppCoreBase* AppCoreBase::context_ = nullptr;
69 TizenProfile TizenProfileGet() {
70 static TizenProfile profile = TizenProfile::Unknown;
71 if (__builtin_expect(profile != TizenProfile::Unknown, 1))
74 char* profile_name = nullptr;
75 system_info_get_platform_string("http://tizen.org/feature/profile",
77 if (profile_name == nullptr)
80 switch (*profile_name) {
83 profile = TizenProfile::Mobile;
87 profile = TizenProfile::Wearable;
91 profile = TizenProfile::Tv;
95 profile = TizenProfile::Ivi;
98 profile = TizenProfile::Common;
106 constexpr const char PATH_LOCALE[] = "locale";
107 constexpr int SQLITE_FLUSH_MAX = 1024 * 1024;
108 constexpr const char RESOURCED_FREEZER_PATH[] =
109 "/Org/Tizen/ResourceD/Freezer";
110 constexpr const char RESOURCED_FREEZER_INTERFACE[] =
111 "org.tizen.resourced.freezer";
112 constexpr const char RESOURCED_FREEZER_SIGNAL[] =
119 tizen_cpp::AppCoreBase::RotationState rm;
125 GDBusConnection* __bus;
126 guint __suspend_dbus_handler_initialized;
127 AppCoreBase::DisplayState __display_state = AppCoreBase::DISPLAY_STATE_UNKNOWN;
131 class AppCoreBase::EventBase::Impl {
133 friend class AppCoreBase;
134 Type type_ = IEvent::Type::START;
135 std::string str_val_;
139 class AppCoreBase::Impl {
141 explicit Impl(AppCoreBase* parent) : parent_(parent) {
142 if (TizenProfileGet() & TizenProfile::Wearable)
143 feature_ |= FEATURE_CHARGER_STATUS;
144 if (!(TizenProfileGet() & TizenProfile::Tv))
145 feature_ |= FEATURE_BACKGROUND_MANAGEMENT;
149 void UnregisterRotationChangedEvent();
150 void RegisterRotationChangedEvent();
151 std::string GetAppName(const char* appid);
152 std::string GetLocaleResourceDir();
155 std::list<std::string> SplitLanguage(const std::string& lang);
156 std::string GetLanguage(std::string lang);
157 void AppendDefaultLangs(std::vector<std::string>& lang_set);
158 std::string GetStringBefore(const char* str, const char* delim);
159 std::map<std::string, std::set<std::string>> GetLangTable();
160 void AppendLangs(const std::string& lang, std::vector<std::string>& lang_set,
161 std::map<std::string, std::set<std::string>>& table);
163 void OnFreezerSignal();
166 void InvokeCallback(T event, IEvent::Type type) {
167 for (auto& i : events_) {
168 if (i->GetType() != type)
171 if (i->GetVal(event) != event ||
172 type == IEvent::Type::START ||
173 type == IEvent::Type::UPDATE_REQUESTED) {
180 static void InitSuspendDbusHandler(gpointer data);
181 static gboolean InitSuspendCb(gpointer data);
182 static gboolean InvokeLangChangeCb(gpointer data);
183 static void ReceiveSuspendSignalCb(GDBusConnection*, const gchar*,
184 const gchar*, const gchar*, const gchar*, GVariant*, gpointer);
185 static void OnLowBatteryCb(keynode_t* key, void* data);
186 static void LockCb(keynode_t* node, void* user_data);
187 static void AutoRotationChangedCb(sensor_t sensor, unsigned int event_type,
188 sensor_data_t* data, void* user_data);
189 static void ChargerStatusChangedCb(keynode_t* keynode, void* user_data);
190 static void LanguageChangeCb(keynode_t* key, void* data);
191 static void RegionChangeCb(keynode_t* key, void* data);
192 static void LowMemoryCb(keynode_t* key, void* data);
195 RotationState GetRm(sensor_data_t data);
196 void VerifyLanguage();
197 void SetDefaultEvents();
198 void UnsetDefaultEvents();
201 friend class AppCoreBase;
202 AppCoreBase* parent_ = nullptr;
205 char** argv_ = nullptr;
206 bool suspended_state_ = false;
207 bool allowed_bg_ = false;
209 unsigned int tid_ = 0;
210 std::list<std::shared_ptr<EventBase>> events_;
211 std::string locale_dir_;
214 IAppCore* core_delegator_ = nullptr;
215 IMainLoop* loop_delegator_ = nullptr;
218 AppCoreBase::EventBase::EventBase(Type type)
219 : impl_(std::make_unique<EventBase::Impl>()) {
223 AppCoreBase::EventBase::~EventBase() = default;
225 IAppCore::IEvent::Type AppCoreBase::EventBase::GetType() const {
229 std::string AppCoreBase::EventBase::GetVal(std::string cur) const {
230 return impl_->str_val_;
233 int AppCoreBase::EventBase::GetVal(int cur) const {
237 void AppCoreBase::EventBase::SetVal(std::string val) {
238 impl_->str_val_ = std::move(val);
241 void AppCoreBase::EventBase::SetVal(int val) {
242 impl_->val_ = std::move(val);
245 AppCoreBase::AppCoreBase()
246 : impl_(std::make_unique<AppCoreBase::Impl>(this)) {
247 if (context_ != nullptr) {
248 _E("Context is already initialized");
254 AppCoreBase::~AppCoreBase() {
259 AppCoreBase* AppCoreBase::GetContext() {
260 if (context_ == nullptr) {
261 _E("Context is not initialized.");
268 void AppCoreBase::Impl::ChangeLang() {
269 const char* lang = getenv("LANG");
273 InvokeCallback(lang, IEvent::Type::LANG_CHANGE);
276 void AppCoreBase::RaiseEvent(int event, IEvent::Type type) {
277 impl_->InvokeCallback(event, type);
280 void AppCoreBase::RaiseEvent(const std::string& event, IEvent::Type type) {
281 impl_->InvokeCallback(event, type);
284 void AppCoreBase::Impl::OnFreezerSignal() {
285 if (!allowed_bg_ && suspended_state_) {
286 parent_->RemoveSuspendTimer();
287 InvokeCallback(SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND,
288 IEvent::Type::SUSPENDED_STATE_CHANGE);
289 suspended_state_ = false;
290 parent_->AddSuspendTimer();
294 AppCoreBase::RotationState AppCoreBase::Impl::GetRm(sensor_data_t data) {
295 if (data.value_count <= 0) {
296 _E("Failed to get sensor data");
297 return ROTATION_UNKNOWN;
300 int event = data.values[0];
302 case AUTO_ROTATION_DEGREE_0:
303 return ROTATION_PORTRAIT_NORMAL;
304 case AUTO_ROTATION_DEGREE_90:
305 return ROTATION_LANDSCAPE_NORMAL;
306 case AUTO_ROTATION_DEGREE_180:
307 return ROTATION_PORTRAIT_REVERSE;
308 case AUTO_ROTATION_DEGREE_270:
309 return ROTATION_LANDSCAPE_REVERSE;
311 return ROTATION_UNKNOWN;
315 void AppCoreBase::Impl::InitSuspendDbusHandler(gpointer data) {
316 if (__suspend_dbus_handler_initialized)
319 if (__bus == nullptr) {
320 GError* err = nullptr;
321 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err);
322 if (__bus == nullptr) {
323 _E("Failed to connect to the D-BUS daemon: %s", err->message);
329 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
330 __bus, nullptr, RESOURCED_FREEZER_INTERFACE, RESOURCED_FREEZER_SIGNAL,
331 RESOURCED_FREEZER_PATH, nullptr, G_DBUS_SIGNAL_FLAGS_NONE,
332 ReceiveSuspendSignalCb, data, nullptr);
334 if (__suspend_dbus_handler_initialized == 0) {
335 _E("g_dbus_connection_signal_subscribe() is failed.");
339 _D("[__SUSPEND__] suspend signal initialized");
342 gboolean AppCoreBase::Impl::InitSuspendCb(gpointer data) {
343 InitSuspendDbusHandler(data);
344 return G_SOURCE_REMOVE;
347 gboolean AppCoreBase::Impl::InvokeLangChangeCb(gpointer data) {
348 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
349 base->impl_->sid_ = 0;
350 base->impl_->ChangeLang();
351 return G_SOURCE_REMOVE;
354 void AppCoreBase::Impl::ReceiveSuspendSignalCb(GDBusConnection*, const gchar*,
355 const gchar*, const gchar*, const gchar* signal_name, GVariant* parameters,
356 gpointer user_data) {
357 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
360 g_variant_get(parameters, "(ii)", &status, &pid);
361 if (pid == getpid() && status == 0) {
362 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
363 base->impl_->OnFreezerSignal();
368 void AppCoreBase::Impl::LanguageChangeCb(keynode_t* key, void* user_data) {
369 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
370 if (base->impl_->sid_) {
371 GLib::SourceRemove(base->impl_->sid_);
372 base->impl_->sid_ = 0;
375 char* val = vconf_keynode_get_str(key);
379 base->impl_->UpdateLang();
380 base->impl_->InvokeCallback(val, IEvent::Type::LANG_CHANGE);
383 void AppCoreBase::Impl::RegionChangeCb(keynode_t* key, void* user_data) {
384 const char* name = vconf_keynode_get_name(key);
388 if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
389 strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
392 char* val = vconf_get_str(VCONFKEY_REGIONFORMAT);
395 std::unique_ptr<char, decltype(std::free)*> region_auto(val, std::free);
397 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
398 base->impl_->UpdateRegion();
399 base->impl_->InvokeCallback(val, IEvent::Type::REGION_CHANGE);
402 void AppCoreBase::Impl::LowMemoryCb(keynode_t* key, void* user_data) {
403 int val = vconf_keynode_get_int(key);
404 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
405 base->impl_->InvokeCallback(val, IEvent::Type::LOW_MEMORY);
406 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
410 void AppCoreBase::Impl::ChargerStatusChangedCb(keynode_t* keynode,
412 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
413 if (base->impl_->feature_ & FEATURE_CHARGER_STATUS) {
414 __rotation.charger_status = vconf_keynode_get_int(keynode);
415 if (__rotation.ref) {
416 if (__rotation.charger_status) {
417 base->impl_->InitRotation();
419 base->impl_->FiniRotation();
423 _D("charger status(%d)", __rotation.charger_status);
427 void AppCoreBase::Impl::LockCb(keynode_t* node, void* user_data) {
428 AppCoreBase::RotationState rm;
429 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
431 __rotation.lock = !vconf_keynode_get_bool(node);
432 if (__rotation.lock) {
433 _D("Rotation locked");
434 rm = ROTATION_PORTRAIT_NORMAL;
436 _D("Rotation unlocked");
438 bool r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
440 _E("Failed to get sensor data");
444 rm = base->impl_->GetRm(data);
445 if (rm == ROTATION_UNKNOWN) {
451 if (__rotation.rm == rm)
454 _D("Rotation: %d -> %d", __rotation.rm, rm);
456 base->impl_->InvokeCallback(__rotation.rm,
457 IEvent::Type::DEVICE_ORIENTATION_CHANGED);
460 void AppCoreBase::Impl::AutoRotationChangedCb(sensor_t sensor,
461 unsigned int event_type, sensor_data_t* data, void* user_data) {
468 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
471 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
472 AppCoreBase::RotationState rm = base->impl_->GetRm(*data);
473 if (rm == ROTATION_UNKNOWN) {
478 _D("Rotation: %d -> %d", __rotation.rm, rm);
480 base->impl_->InvokeCallback(__rotation.rm,
481 IEvent::Type::DEVICE_ORIENTATION_CHANGED);
484 void AppCoreBase::Impl::InitRotation() {
485 if (__rotation.initialized)
488 sensor_t sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
489 __rotation.conn = sensord_connect(sensor);
490 if (__rotation.conn < 0) {
491 _E("Failed to connect sensord");
495 bool r = sensord_register_event(__rotation.conn,
496 AUTO_ROTATION_CHANGE_STATE_EVENT, SENSOR_INTERVAL_NORMAL, 0,
497 AutoRotationChangedCb, parent_);
499 _E("Failed to register auto rotation change event");
500 sensord_disconnect(__rotation.conn);
504 r = sensord_start(__rotation.conn, 0);
506 _E("Failed to start sensord");
507 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
508 sensord_disconnect(__rotation.conn);
513 vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
514 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, LockCb,
517 __rotation.lock = !lock;
518 __rotation.initialized = true;
521 void AppCoreBase::Impl::FiniRotation() {
522 if (!__rotation.initialized)
525 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, LockCb);
526 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
527 sensord_stop(__rotation.conn);
528 sensord_disconnect(__rotation.conn);
531 __rotation.initialized = false;
534 void AppCoreBase::Impl::VerifyLanguage() {
535 const char* env_lang = getenv("LANG");
536 if (env_lang == nullptr)
539 char* lang = vconf_get_str(VCONFKEY_LANGSET);
543 std::unique_ptr<char, decltype(std::free)*> lang_auto(lang, std::free);
545 if (strcmp(env_lang, lang) != 0) {
546 _I("LANG(%s), LANGSET(%s)", env_lang, lang);
547 sid_ = GLib::IdleAdd(InvokeLangChangeCb, parent_);
551 void AppCoreBase::Impl::SetDefaultEvents() {
552 vconf_notify_key_changed(VCONFKEY_LANGSET, LanguageChangeCb, parent_);
553 int r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, RegionChangeCb,
556 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, RegionChangeCb,
560 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, LowMemoryCb, parent_);
563 void AppCoreBase::Impl::UnsetDefaultEvents() {
564 vconf_ignore_key_changed(VCONFKEY_LANGSET, LanguageChangeCb);
565 int r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, RegionChangeCb);
567 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, RegionChangeCb);
569 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, LowMemoryCb);
572 int AppCoreBase::OnReceive(aul_type type, tizen_base::Bundle b) {
575 _D("[APP %d] AUL event: AUL_START", getpid());
576 if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT) {
577 std::string bg = b.GetString(AUL_K_ALLOWED_BG);
578 if (bg == "ALLOWED_BG") {
579 _D("[__SUSPEND__] allowed background");
580 impl_->allowed_bg_ = true;
581 RemoveSuspendTimer();
585 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
586 if (impl_->core_delegator_)
587 impl_->core_delegator_->OnControl(std::move(b));
589 OnControl(std::move(b));
590 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
593 _D("[APP %d] AUL event: AUL_RESUME", getpid());
594 if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT) {
595 std::string bg = b.GetString(AUL_K_ALLOWED_BG);
596 if (bg == "ALLOWED_BG") {
597 _D("[__SUSPEND__] allowed background");
598 impl_->allowed_bg_ = true;
599 RemoveSuspendTimer();
604 _D("[APP %d] AUL event: AUL_TERMINATE", getpid());
605 aul_status_update(STATUS_DYING);
606 if (!impl_->allowed_bg_)
607 RemoveSuspendTimer();
611 case AUL_TERMINATE_INST:
612 case AUL_TERMINATE_BG_INST:
613 case AUL_TERMINATE_BGAPP:
614 _D("[APP %d] AUL event: %d", getpid(), type);
615 if (!impl_->allowed_bg_)
616 RemoveSuspendTimer();
619 _D("[APP %d] AUL event: AUL_WAKE", getpid());
620 if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT) {
621 if (!impl_->allowed_bg_ && impl_->suspended_state_) {
622 RemoveSuspendTimer();
623 int suspend = SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
624 impl_->InvokeCallback(suspend, IEvent::Type::SUSPENDED_STATE_CHANGE);
625 impl_->suspended_state_ = false;
630 _D("[APP %d] AUL event: AUL_SUSPEND", getpid());
631 if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT) {
632 if (!impl_->allowed_bg_ && !impl_->suspended_state_) {
633 RemoveSuspendTimer();
638 case AUL_UPDATE_REQUESTED:
639 _D("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
640 impl_->InvokeCallback(0, IEvent::Type::UPDATE_REQUESTED);
643 _D("[APP %d] AUL event: %d", getpid(), type);
651 int AppCoreBase::OnCreate() {
652 int ret = aul_launch_init([](aul_type type, bundle* b, void* data) -> int {
653 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
654 if (base->impl_->core_delegator_) {
655 return base->impl_->core_delegator_->OnReceive(type,
656 b ? tizen_base::Bundle(b, false, false) : tizen_base::Bundle());
658 return base->OnReceive(type,
659 b ? tizen_base::Bundle(b, false, false) : tizen_base::Bundle());
661 if (ret < 0 && ret != AUL_R_ECANCELED) {
662 _E("aul_launch_init() is failed. error(%d)", ret);
666 ret = aul_launch_argv_handler(impl_->argc_, impl_->argv_);
668 _E("aul_launch_argv_handler() is failed. error(%d)", ret);
675 int AppCoreBase::OnControl(tizen_base::Bundle b) {
679 int AppCoreBase::OnTerminate() {
684 void AppCoreBase::SetCoreDelegator(IAppCore* delegator) {
685 impl_->core_delegator_ = delegator;
688 void AppCoreBase::SetLoopDelegator(IMainLoop* delegator) {
689 impl_->loop_delegator_ = delegator;
692 std::string AppCoreBase::Impl::GetAppName(const char* appid) {
693 if (appid == nullptr)
696 /* com.vendor.name -> name */
697 const char* name_token = strrchr(appid, '.');
698 if (name_token == nullptr)
705 std::string AppCoreBase::Impl::GetLocaleResourceDir() {
706 const char* res_path = aul_get_app_resource_path();
707 if (res_path == nullptr) {
708 _E("Failed to get resource path");
712 return std::string(res_path) + PATH_LOCALE;
715 int AppCoreBase::OnSetI18n() {
716 char appid[PATH_MAX];
717 int ret = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
719 _E("aul_app_get_appid_bypid() is failed. error(%d)", ret);
723 std::string name = impl_->GetAppName(appid);
727 std::string locale_dir = impl_->GetLocaleResourceDir();
728 if (locale_dir.empty())
731 return SetI18n(move(name), move(locale_dir));
734 void AppCoreBase::Impl::OnLowBatteryCb(keynode_t* key, void* data) {
735 int val = vconf_keynode_get_int(key);
736 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
737 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
738 base->impl_->InvokeCallback(val, IEvent::Type::LOW_BATTERY);
742 void AppCoreBase::Impl::UnregisterRotationChangedEvent() {
747 if (__rotation.ref == 0) {
749 if (feature_ & FEATURE_CHARGER_STATUS) {
750 vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
751 ChargerStatusChangedCb);
756 void AppCoreBase::Impl::RegisterRotationChangedEvent() {
757 if (__rotation.ref == 0) {
758 if (feature_ & FEATURE_CHARGER_STATUS) {
759 vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS, &__rotation.charger_status);
760 vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
761 ChargerStatusChangedCb, parent_);
762 if (__rotation.charger_status)
772 int AppCoreBase::OnSetEvent(IEvent::Type event) {
774 case IEvent::Type::LOW_BATTERY:
775 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
776 impl_->OnLowBatteryCb, this);
778 case IEvent::Type::DEVICE_ORIENTATION_CHANGED:
779 impl_->RegisterRotationChangedEvent();
781 case IEvent::Type::SUSPENDED_STATE_CHANGE:
790 int AppCoreBase::OnUnsetEvent(IEvent::Type event) {
792 case IEvent::Type::LOW_BATTERY:
793 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
794 impl_->OnLowBatteryCb);
796 case IEvent::Type::DEVICE_ORIENTATION_CHANGED:
797 impl_->UnregisterRotationChangedEvent();
799 case IEvent::Type::SUSPENDED_STATE_CHANGE:
808 int AppCoreBase::OnTrimMemory() {
809 int (*sqlite3_free_heap_memory)(int);
811 sqlite3_free_heap_memory = reinterpret_cast<
812 decltype(sqlite3_free_heap_memory)>(
813 dlsym(RTLD_DEFAULT, "sqlite3_release_memory"));
814 if (sqlite3_free_heap_memory)
815 sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
821 void AppCoreBase::AddEvent(std::shared_ptr<EventBase> event) {
823 for (auto& ev : impl_->events_) {
824 if (ev->GetType() == event->GetType()) {
831 if (impl_->core_delegator_)
832 impl_->core_delegator_->OnSetEvent(event->GetType());
834 OnSetEvent(event->GetType());
837 impl_->events_.push_back(move(event));
840 bool AppCoreBase::RemoveEvent(std::shared_ptr<EventBase> event) {
842 impl_->events_.remove_if([&](const std::shared_ptr<EventBase>& ptr)->bool {
843 if (event.get() == ptr.get()) {
853 void AppCoreBase::FlushMemory() {
854 if (impl_->core_delegator_)
855 impl_->core_delegator_->OnTrimMemory();
860 if (!impl_->allowed_bg_ && !impl_->suspended_state_) {
861 _D("[__SUSPEND__] flush case");
862 int suspend = SUSPENDED_STATE_WILL_ENTER_SUSPEND;
863 impl_->InvokeCallback(suspend, IEvent::Type::SUSPENDED_STATE_CHANGE);
864 impl_->suspended_state_ = true;
868 AppCoreBase::RotationState AppCoreBase::GetRotationState() {
870 throw std::runtime_error("invalid rotation state");
872 return __rotation.rm;
875 bool AppCoreBase::IsBgAllowed() {
876 return impl_->allowed_bg_;
879 bool AppCoreBase::IsSuspended() {
880 return impl_->suspended_state_;
883 void AppCoreBase::ToggleSuspendedState() {
884 impl_->suspended_state_ = !impl_->suspended_state_;
887 std::list<std::string> AppCoreBase::Impl::SplitLanguage(
888 const std::string& lang) {
889 std::istringstream ss(lang);
890 std::list<std::string> li;
893 while (std::getline(ss, token, ':'))
899 void AppCoreBase::Impl::AppendDefaultLangs(std::vector<std::string>& langs) {
900 langs.push_back("en_US");
901 langs.push_back("en_GB");
902 langs.push_back("en");
905 std::string AppCoreBase::Impl::GetStringBefore(const char* cstr,
907 if (cstr == nullptr || delim == nullptr)
910 auto str = std::string(cstr);
911 auto idx = str.find(delim);
912 return str.substr(0, idx);
915 std::map<std::string, std::set<std::string>> AppCoreBase::Impl::GetLangTable() {
916 if (locale_dir_.empty())
919 DIR* dp = opendir(locale_dir_.c_str());
923 std::map<std::string, std::set<std::string>> table;
924 struct dirent *dentry;
925 while ((dentry = readdir(dp)) != nullptr) {
926 if (!strcmp(dentry->d_name, ".") ||
927 !strcmp(dentry->d_name, ".."))
930 std::string buf = locale_dir_ + "/" + dentry->d_name;
931 struct stat stat_buf;
932 int ret = stat(buf.c_str(), &stat_buf);
933 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
936 std::string parent_lang = GetStringBefore(dentry->d_name, "_");
937 if (parent_lang.empty()) {
942 table[parent_lang].insert(dentry->d_name);
949 void AppCoreBase::Impl::AppendLangs(const std::string& lang,
950 std::vector<std::string>& langs,
951 std::map<std::string, std::set<std::string>>& table) {
955 langs.push_back(lang);
956 std::string extract_lang = GetStringBefore(lang.c_str(), ".");
957 if (extract_lang.empty())
960 if (std::find(langs.begin(), langs.end(), extract_lang) != langs.end())
963 std::string parent_lang = GetStringBefore(extract_lang.c_str(), "_");
964 if (parent_lang.empty())
967 if (table.find(parent_lang) == table.end())
970 auto it = table[parent_lang].find(extract_lang);
971 if (it != table[parent_lang].end()) {
972 std::string value = *it;
973 if (std::find(langs.begin(), langs.end(), value) == langs.end())
974 langs.push_back(std::move(value));
976 table[parent_lang].erase(it);
980 it = table[parent_lang].find(parent_lang);
981 if (it != table[parent_lang].end()) {
982 std::string value = *it;
983 if (std::find(langs.begin(), langs.end(), value) == langs.end())
984 langs.push_back(std::move(value));
986 table[parent_lang].erase(parent_lang);
990 if (!table[parent_lang].empty()) {
991 auto i = table[parent_lang].begin();
992 std::string value = *i;
993 if (std::find(langs.begin(), langs.end(), value) == langs.end())
994 langs.push_back(std::move(value));
996 table[parent_lang].erase(i);
1000 std::string AppCoreBase::Impl::GetLanguage(std::string lang) {
1001 std::list<std::string> l = SplitLanguage(lang);
1005 auto table = GetLangTable();
1009 std::vector<std::string> langs;
1011 AppendLangs(i, langs, table);
1013 AppendDefaultLangs(langs);
1015 for (auto& i : langs) {
1025 void AppCoreBase::Impl::UpdateLang() {
1026 char* lang = vconf_get_str(VCONFKEY_LANGSET);
1027 if (lang == nullptr)
1030 std::unique_ptr<char, decltype(std::free)*> lang_auto(lang, std::free);
1031 std::string language = GetLanguage(lang);
1032 if (!language.empty()) {
1033 _D("*****language(%s)", language.c_str());
1034 setenv("LANGUAGE", language.c_str(), 1);
1036 setenv("LANGUAGE", lang, 1);
1038 setenv("LANG", lang, 1);
1039 setenv("LC_MESSAGES", lang, 1);
1040 setenv("LC_ALL", lang, 1);
1041 char* r = setlocale(LC_ALL, "");
1043 r = setlocale(LC_ALL, "en_US.UTF-8");
1045 _D("*****appcore setlocale=%s\n", r);
1047 _D("*****appcore setlocale=\"C\"");
1048 setenv("LC_ALL", "C", 1);
1049 r = setlocale(LC_ALL, "");
1051 _E("failed to setlocale");
1056 void AppCoreBase::Impl::UpdateRegion() {
1057 char* region = vconf_get_str(VCONFKEY_REGIONFORMAT);
1058 if (region == nullptr)
1061 std::unique_ptr<char, decltype(std::free)*> region_auto(region, std::free);
1062 setenv("LC_CTYPE", region, 1);
1063 setenv("LC_NUMERIC", region, 1);
1064 setenv("LC_TIME", region, 1);
1065 setenv("LC_COLLATE", region, 1);
1066 setenv("LC_MONETARY", region, 1);
1067 setenv("LC_PAPER", region, 1);
1068 setenv("LC_NAME", region, 1);
1069 setenv("LC_ADDRESS", region, 1);
1070 setenv("LC_TELEPHONE", region, 1);
1071 setenv("LC_MEASUREMENT", region, 1);
1072 setenv("LC_IDENTIFICATION", region, 1);
1073 char* r = setlocale(LC_ALL, "");
1075 _D("*****appcore setlocale=%s\n", r);
1077 _D("*****appcore setlocale=\"C\"");
1078 setenv("LC_ALL", "C", 1);
1079 r = setlocale(LC_ALL, "");
1081 _E("failed to setlocale");
1085 int AppCoreBase::SetI18n(std::string domain_name, std::string dir_name) {
1086 if (domain_name.empty()) {
1091 if (!dir_name.empty())
1092 impl_->locale_dir_ = dir_name;
1094 impl_->UpdateLang();
1095 impl_->UpdateRegion();
1097 char* r = setlocale(LC_ALL, "");
1098 /* if locale is not set properly, try to set "en_US" again */
1100 r = setlocale(LC_ALL, "en_US.UTF-8");
1102 _E("appcore: setlocale() error");
1103 _D("*****appcore setlocale=\"C\"");
1104 setenv("LC_ALL", "C", 1);
1105 r = setlocale(LC_ALL, "");
1107 _E("failed to setlocale");
1112 _D("*****appcore setlocale=%s\n", r);
1114 r = bindtextdomain(domain_name.c_str(), dir_name.c_str());
1116 _E("appcore: bindtextdomain() error");
1118 r = textdomain(domain_name.c_str());
1120 _E("appcore: textdomain() error");
1125 void AppCoreBase::Exit() {
1126 aul_status_update(STATUS_DYING);
1127 if (impl_->loop_delegator_)
1128 impl_->loop_delegator_->OnLoopExit();
1133 void AppCoreBase::AddSuspendTimer() {
1134 impl_->tid_ = GLib::TimeoutAdd(5000, [](gpointer data) -> gboolean {
1135 AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
1136 base->FlushMemory();
1141 void AppCoreBase::RemoveSuspendTimer() {
1142 if (impl_->tid_ > 0) {
1143 GLib::SourceRemove(impl_->tid_);
1148 void AppCoreBase::SetDisplayState(DisplayState state) {
1149 __display_state = state;
1152 AppCoreBase::DisplayState AppCoreBase::GetDisplayState() {
1153 return __display_state;
1156 int AppCoreBase::EnableWatchdog() {
1157 _I("[__APPCORE_WATCHDOG__] enable");
1158 return aul_watchdog_enable();
1161 int AppCoreBase::DisableWatchdog() {
1162 _I("[__APPCORE_WATCHDOG__] disable");
1163 return aul_watchdog_disable();
1166 int AppCoreBase::KickWatchdog() {
1167 _I("[__APPCORE_WATCHDOG__] kick");
1168 return aul_watchdog_kick();
1171 void AppCoreBase::Run(int argc, char** argv) {
1176 void AppCoreBase::Init(int argc, char** argv) {
1178 impl_->suspended_state_ = false;
1179 impl_->allowed_bg_ = false;
1180 impl_->argc_ = argc;
1181 impl_->argv_ = argv;
1182 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:OPS_INIT");
1183 if (impl_->loop_delegator_)
1184 impl_->loop_delegator_->OnLoopInit(argc, argv);
1186 OnLoopInit(argc, argv);
1187 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1189 if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT)
1190 GLib::IdleAdd(Impl::InitSuspendCb, this);
1192 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:SET_SYSTEM_EVENT");
1193 if (!impl_->dirty_) {
1194 impl_->dirty_ = true;
1196 for (auto& e : impl_->events_) {
1197 if (impl_->core_delegator_)
1198 impl_->core_delegator_->OnSetEvent(e->GetType());
1200 OnSetEvent(e->GetType());
1203 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1205 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:VERIFY_LANG");
1206 impl_->VerifyLanguage();
1207 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1209 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:SET_DEFAULT_EVENTS");
1210 impl_->SetDefaultEvents();
1211 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1213 if (impl_->core_delegator_)
1214 impl_->core_delegator_->OnSetI18n();
1218 aul_app_lifecycle_update_state(AUL_APP_LIFECYCLE_STATE_INITIALIZED);
1220 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1222 if (impl_->core_delegator_)
1223 create = impl_->core_delegator_->OnCreate();
1225 create = OnCreate();
1226 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1228 aul_app_lifecycle_update_state(AUL_APP_LIFECYCLE_STATE_CREATED);
1232 if (impl_->loop_delegator_)
1233 impl_->loop_delegator_->OnLoopRun();
1238 void AppCoreBase::Fini() {
1242 void AppCoreBase::Dispose() {
1243 aul_status_update(STATUS_DYING);
1245 aul_app_lifecycle_update_state(AUL_APP_LIFECYCLE_STATE_DESTROYED);
1247 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1248 if (impl_->core_delegator_)
1249 impl_->core_delegator_->OnTerminate();
1252 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1254 for (auto& e : impl_->events_) {
1255 if (impl_->core_delegator_)
1256 impl_->core_delegator_->OnUnsetEvent(e->GetType());
1258 OnUnsetEvent(e->GetType());
1261 impl_->UnsetDefaultEvents();
1263 GLib::SourceRemove(impl_->sid_);
1267 RemoveSuspendTimer();
1268 impl_->dirty_ = false;
1269 if (impl_->loop_delegator_)
1270 impl_->loop_delegator_->OnLoopFinish();
1275 void AppCoreBase::SetFeature(int feature) {
1276 impl_->feature_ = feature;
1279 int AppCoreBase::GetFeature() const {
1280 return impl_->feature_;
1283 } // namespace tizen_cpp