1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "i18n_hilog.h"
17 #include "i18n_service_ability_load_manager.h"
18 #include "locale_config.h"
19 #include "mem_mgr_client.h"
20 #include "mem_mgr_proxy.h"
21 #include "multi_users.h"
22 #include "preferred_language.h"
23 #include "preferences.h"
24 #include "preferences_helper.h"
25 #include "system_ability_definition.h"
26 #include "i18n_service_ability.h"
27
28 namespace OHOS {
29 namespace Global {
30 namespace I18n {
31 REGISTER_SYSTEM_ABILITY_BY_ID(I18nServiceAbility, I18N_SA_ID, false);
32 static const std::string UNLOAD_TASK = "i18n_service_unload";
33 static const uint32_t DELAY_MILLISECONDS_FOR_UNLOAD_SA = 10000;
34
I18nServiceAbility(int32_t saId, bool runOnCreate)35 I18nServiceAbility::I18nServiceAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
36 {
37 HILOG_INFO_I18N("I18nServiceAbility object init success.");
38 }
39
~I18nServiceAbility()40 I18nServiceAbility::~I18nServiceAbility()
41 {
42 HILOG_INFO_I18N("I18nServiceAbility object release.");
43 }
44
SetSystemLanguage(const std::string &language)45 I18nErrorCode I18nServiceAbility::SetSystemLanguage(const std::string &language)
46 {
47 return LocaleConfig::SetSystemLanguage(language);
48 }
49
SetSystemRegion(const std::string ®ion)50 I18nErrorCode I18nServiceAbility::SetSystemRegion(const std::string ®ion)
51 {
52 return LocaleConfig::SetSystemRegion(region);
53 }
54
SetSystemLocale(const std::string &locale)55 I18nErrorCode I18nServiceAbility::SetSystemLocale(const std::string &locale)
56 {
57 return LocaleConfig::SetSystemLocale(locale);
58 }
59
Set24HourClock(const std::string &flag)60 I18nErrorCode I18nServiceAbility::Set24HourClock(const std::string &flag)
61 {
62 return LocaleConfig::Set24HourClock(flag);
63 }
64
SetUsingLocalDigit(bool flag)65 I18nErrorCode I18nServiceAbility::SetUsingLocalDigit(bool flag)
66 {
67 return LocaleConfig::SetUsingLocalDigit(flag);
68 }
69
AddPreferredLanguage(const std::string &language, int32_t index)70 I18nErrorCode I18nServiceAbility::AddPreferredLanguage(const std::string &language, int32_t index)
71 {
72 return PreferredLanguage::AddPreferredLanguage(language, index);
73 }
74
RemovePreferredLanguage(int32_t index)75 I18nErrorCode I18nServiceAbility::RemovePreferredLanguage(int32_t index)
76 {
77 return PreferredLanguage::RemovePreferredLanguage(index);
78 }
79
UnloadI18nServiceAbility()80 void I18nServiceAbility::UnloadI18nServiceAbility()
81 {
82 if (handler != nullptr) {
83 handler->RemoveTask(UNLOAD_TASK);
84 }
85 auto task = [this]() {
86 auto i18nSaLoadManager = DelayedSingleton<I18nServiceAbilityLoadManager>::GetInstance();
87 if (i18nSaLoadManager != nullptr) {
88 HILOG_INFO_I18N("I18nServiceAbility::UnloadI18nServiceAbility start to unload i18n sa.");
89 i18nSaLoadManager->UnloadI18nService(I18N_SA_ID);
90 }
91 };
92 if (handler != nullptr) {
93 handler->PostTask(task, UNLOAD_TASK, DELAY_MILLISECONDS_FOR_UNLOAD_SA);
94 }
95 }
96
OnStart(const SystemAbilityOnDemandReason& startReason)97 void I18nServiceAbility::OnStart(const SystemAbilityOnDemandReason& startReason)
98 {
99 HILOG_INFO_I18N("I18nServiceAbility start.");
100 i18nServiceEvent = new I18nServiceEvent();
101 if (i18nServiceEvent != nullptr) {
102 i18nServiceEvent->SubscriberEvent();
103 i18nServiceEvent->CheckStartReason(startReason);
104 }
105 #ifdef SUPPORT_MULTI_USER
106 MultiUsers::InitMultiUser();
107 #endif
108 bool status = Publish(this);
109 if (status) {
110 HILOG_INFO_I18N("I18nServiceAbility Publish success.");
111 } else {
112 HILOG_INFO_I18N("I18nServiceAbility Publish failed.");
113 }
114 handler = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
115 UnloadI18nServiceAbility();
116 int pid = getpid();
117 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 1, I18N_SA_ID);
118 }
119
OnStop()120 void I18nServiceAbility::OnStop()
121 {
122 int pid = getpid();
123 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 0, I18N_SA_ID);
124 if (i18nServiceEvent != nullptr) {
125 delete i18nServiceEvent;
126 i18nServiceEvent = nullptr;
127 }
128 HILOG_INFO_I18N("I18nServiceAbility Stop.");
129 }
130 } // namespace I18n
131 } // namespace Global
132 } // namespace OHOS