• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_callback.h"
18 #include "i18n_service_ability_load_manager.h"
19 
20 namespace OHOS {
21 namespace Global {
22 namespace I18n {
23 static constexpr int32_t I18N_LOAD_SA_TIMEOUT_MS = 1000;
24 #ifdef DEVICE_WATCH
25 static constexpr int32_t I18N_LOAD_SA_TRY_TIMES = 8;
26 #else
27 static constexpr int32_t I18N_LOAD_SA_TRY_TIMES = 4;
28 #endif
29 
I18nServiceAbilityLoadManager()30 I18nServiceAbilityLoadManager::I18nServiceAbilityLoadManager()
31 {
32 }
33 
~I18nServiceAbilityLoadManager()34 I18nServiceAbilityLoadManager::~I18nServiceAbilityLoadManager()
35 {
36 }
37 
GetI18nServiceAbility(int32_t systemAbilityId)38 sptr<IRemoteObject> I18nServiceAbilityLoadManager::GetI18nServiceAbility(int32_t systemAbilityId)
39 {
40     sptr<ISystemAbilityManager> samgr = nullptr;
41     for (int32_t times = 0; times < I18N_LOAD_SA_TRY_TIMES; times++) {
42         samgr = LoadI18nServiceAbility(systemAbilityId);
43         if (samgr != nullptr) {
44             break;
45         }
46         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::GetI18nServiceAbility: Load i18n service failed, "
47             "times is %{public}d.", times);
48     }
49     if (samgr == nullptr) {
50         return nullptr;
51     }
52     return samgr->GetSystemAbility(systemAbilityId, "");
53 }
54 
LoadI18nServiceAbility(int32_t systemAbilityId)55 sptr<ISystemAbilityManager> I18nServiceAbilityLoadManager::LoadI18nServiceAbility(int32_t systemAbilityId)
56 {
57     InitLoadState();
58     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (samgr == nullptr) {
60         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility can't get samgr");
61         return nullptr;
62     }
63     sptr<I18nServiceAbilityLoadCallback> i18nSaLoadCallback = new I18nServiceAbilityLoadCallback();
64     if (i18nSaLoadCallback == nullptr) {
65         HILOG_ERROR_I18N(
66             "I18nServiceAbilityLoadManager::LoadI18nServiceAbility create i18nSaLoadCallback failed.");
67         return nullptr;
68     }
69     int32_t ret = samgr->LoadSystemAbility(systemAbilityId, i18nSaLoadCallback);
70     if (ret != ERR_OK) {
71         HILOG_ERROR_I18N(
72             "I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility LoadSystemAbility failed.");
73         return nullptr;
74     }
75     bool status = WaitLoadStateChange(systemAbilityId);
76     if (!status) {
77         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility wait overtime.");
78         return nullptr;
79     }
80     return samgr;
81 }
82 
InitLoadState()83 void I18nServiceAbilityLoadManager::InitLoadState()
84 {
85     std::unique_lock<std::mutex> lock(loadStateMutex);
86     loadState = false;
87 }
88 
WaitLoadStateChange(int32_t systemAbilityId)89 bool I18nServiceAbilityLoadManager::WaitLoadStateChange(int32_t systemAbilityId)
90 {
91     std::unique_lock<std::mutex> lock(loadStateMutex);
92     auto isLoadSuccess = loadStateCondition.wait_for(lock, std::chrono::milliseconds(I18N_LOAD_SA_TIMEOUT_MS), [this] {
93         return loadState;
94     });
95     if (!isLoadSuccess) {
96         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::WaitLoadStateChange timeout.");
97     }
98     return isLoadSuccess;
99 }
100 
UnloadI18nService(int32_t systemAbilityId)101 bool I18nServiceAbilityLoadManager::UnloadI18nService(int32_t systemAbilityId)
102 {
103     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104     if (samgr == nullptr) {
105         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService can't get samgr.");
106         return false;
107     }
108     int32_t ret = samgr->UnloadSystemAbility(systemAbilityId);
109     if (ret != ERR_OK) {
110         HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService sa unload failed.");
111         return false;
112     }
113     return true;
114 }
115 
LoadSystemAbilitySuccess()116 void I18nServiceAbilityLoadManager::LoadSystemAbilitySuccess()
117 {
118     std::unique_lock<std::mutex> lock(loadStateMutex);
119     loadState = true;
120     loadStateCondition.notify_one();
121 }
122 
LoadSystemAbilityFail()123 void I18nServiceAbilityLoadManager::LoadSystemAbilityFail()
124 {
125     std::unique_lock<std::mutex> lock(loadStateMutex);
126     loadState = false;
127     loadStateCondition.notify_one();
128 }
129 } // namespace I18n
130 } // namespace Global
131 } // namespace OHOS