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 "preferred_language.h"
22 #include "preferences.h"
23 #include "preferences_helper.h"
24 #include "system_ability_definition.h"
25 #include "i18n_service_ability.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace I18n {
30 REGISTER_SYSTEM_ABILITY_BY_ID(I18nServiceAbility, I18N_SA_ID, false);
31 static const std::string UNLOAD_TASK = "i18n_service_unload";
32 static const uint32_t DELAY_MILLISECONDS_FOR_UNLOAD_SA = 10000;
33 
I18nServiceAbility(int32_t saId,bool runOnCreate)34 I18nServiceAbility::I18nServiceAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
35 {
36     HILOG_INFO_I18N("I18nServiceAbility object init success.");
37 }
38 
~I18nServiceAbility()39 I18nServiceAbility::~I18nServiceAbility()
40 {
41     HILOG_INFO_I18N("I18nServiceAbility object release.");
42 }
43 
SetSystemLanguage(const std::string & language)44 I18nErrorCode I18nServiceAbility::SetSystemLanguage(const std::string &language)
45 {
46     return LocaleConfig::SetSystemLanguage(language);
47 }
48 
SetSystemRegion(const std::string & region)49 I18nErrorCode I18nServiceAbility::SetSystemRegion(const std::string &region)
50 {
51     return LocaleConfig::SetSystemRegion(region);
52 }
53 
SetSystemLocale(const std::string & locale)54 I18nErrorCode I18nServiceAbility::SetSystemLocale(const std::string &locale)
55 {
56     return LocaleConfig::SetSystemLocale(locale);
57 }
58 
Set24HourClock(const std::string & flag)59 I18nErrorCode I18nServiceAbility::Set24HourClock(const std::string &flag)
60 {
61     return LocaleConfig::Set24HourClock(flag);
62 }
63 
SetUsingLocalDigit(bool flag)64 I18nErrorCode I18nServiceAbility::SetUsingLocalDigit(bool flag)
65 {
66     return LocaleConfig::SetUsingLocalDigit(flag);
67 }
68 
AddPreferredLanguage(const std::string & language,int32_t index)69 I18nErrorCode I18nServiceAbility::AddPreferredLanguage(const std::string &language, int32_t index)
70 {
71     return PreferredLanguage::AddPreferredLanguage(language, index);
72 }
73 
RemovePreferredLanguage(int32_t index)74 I18nErrorCode I18nServiceAbility::RemovePreferredLanguage(int32_t index)
75 {
76     return PreferredLanguage::RemovePreferredLanguage(index);
77 }
78 
UnloadI18nServiceAbility()79 void I18nServiceAbility::UnloadI18nServiceAbility()
80 {
81     if (handler != nullptr) {
82         handler->RemoveTask(UNLOAD_TASK);
83     }
84     auto task = [this]() {
85         auto i18nSaLoadManager = DelayedSingleton<I18nServiceAbilityLoadManager>::GetInstance();
86         if (i18nSaLoadManager != nullptr) {
87             HILOG_INFO_I18N("I18nServiceAbility::UnloadI18nServiceAbility start to unload i18n sa.");
88             i18nSaLoadManager->UnloadI18nService(I18N_SA_ID);
89         }
90     };
91     if (handler != nullptr) {
92         handler->PostTask(task, UNLOAD_TASK, DELAY_MILLISECONDS_FOR_UNLOAD_SA);
93     }
94 }
95 
OnStart()96 void I18nServiceAbility::OnStart()
97 {
98     HILOG_INFO_I18N("I18nServiceAbility start.");
99     bool status = Publish(this);
100     if (status) {
101         HILOG_INFO_I18N("I18nServiceAbility Publish success.");
102     } else {
103         HILOG_INFO_I18N("I18nServiceAbility Publish failed.");
104     }
105     handler = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
106     UnloadI18nServiceAbility();
107     int pid = getpid();
108     Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 1, I18N_SA_ID);
109 }
110 
OnStop()111 void I18nServiceAbility::OnStop()
112 {
113     int pid = getpid();
114     Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 0, I18N_SA_ID);
115     HILOG_INFO_I18N("I18nServiceAbility Stop.");
116 }
117 } // namespace I18n
118 } // namespace Global
119 } // namespace OHOS