1 /*
2 * Copyright (c) 2021 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 "adapter/ohos/entrance/ace_application_info.h"
17
18 #include <dirent.h>
19 #include <iostream>
20 #include <sys/stat.h>
21
22 #include "contrib/minizip/unzip.h"
23 #include "ohos/init_data.h"
24 #include "unicode/locid.h"
25
26 #include "base/i18n/localization.h"
27 #include "base/log/ace_trace.h"
28 #include "base/log/log.h"
29 #include "base/resource/ace_res_config.h"
30 #include "base/resource/ace_res_data_struct.h"
31 #include "base/utils/utils.h"
32 #include "core/common/ace_engine.h"
33
34 namespace OHOS::Ace::Platform {
AceApplicationInfoImpl()35 AceApplicationInfoImpl::AceApplicationInfoImpl() {}
36 AceApplicationInfoImpl::~AceApplicationInfoImpl() = default;
37
SetJsEngineParam(const std::string & key,const std::string & value)38 void AceApplicationInfoImpl::SetJsEngineParam(const std::string& key, const std::string& value)
39 {
40 jsEngineParams_[key] = value;
41 }
42
GetJsEngineParam(const std::string & key) const43 std::string AceApplicationInfoImpl::GetJsEngineParam(const std::string& key) const
44 {
45 std::string value;
46 auto iter = jsEngineParams_.find(key);
47 if (iter != jsEngineParams_.end()) {
48 value = iter->second;
49 }
50 return value;
51 }
52
ChangeLocale(const std::string & language,const std::string & countryOrRegion)53 void AceApplicationInfoImpl::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
54 {
55 std::string languageLower = language;
56 std::transform(language.begin(), language.end(), languageLower.begin(), ::tolower);
57
58 std::string countryOrRegionUpper = countryOrRegion;
59 std::transform(countryOrRegion.begin(), countryOrRegion.end(), countryOrRegionUpper.begin(), ::tolower);
60
61 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
62 resourceManager_->GetResConfig(*resConfig);
63
64 auto localeInfo = resConfig->GetLocaleInfo();
65 CHECK_NULL_VOID(localeInfo);
66 auto script = Localization::ComputeScript(language, countryOrRegion);
67 resConfig->SetLocaleInfo(languageLower.c_str(), script.c_str(), countryOrRegionUpper.c_str());
68 resourceManager_->UpdateResConfig(*resConfig);
69
70 SetLocale(languageLower, countryOrRegionUpper, (script.empty()) ? "" : script, "");
71 }
72
SetLocale(const std::string & language,const std::string & countryOrRegion,const std::string & script,const std::string & keywordsAndValues)73 void AceApplicationInfoImpl::SetLocale(const std::string& language, const std::string& countryOrRegion,
74 const std::string& script, const std::string& keywordsAndValues)
75 {
76 language_ = language;
77 countryOrRegion_ = countryOrRegion;
78 script_ = script;
79 keywordsAndValues_ = keywordsAndValues;
80
81 localeTag_ = language;
82 if (!script_.empty()) {
83 localeTag_.append("-" + script_);
84 }
85 if (!countryOrRegion_.empty()) {
86 localeTag_.append("-" + countryOrRegion_);
87 }
88
89 icu::Locale locale(language_.c_str(), countryOrRegion.c_str());
90 isRightToLeft_ = SystemProperties::GetRtlEnabled().value_or(locale.isRightToLeft());
91
92 auto languageList = Localization::GetLanguageList(language_);
93 if (languageList.size() == 1) {
94 Localization::SetLocale(language_, countryOrRegion_, script, languageList.front(), keywordsAndValues_);
95 } else {
96 auto selectLanguage = AceResConfig::GetLocaleFallback(localeTag_, languageList);
97 Localization::SetLocale(language_, countryOrRegion_, script, selectLanguage.front(), keywordsAndValues_);
98 }
99 }
100
GetBundleInfo(const std::string & packageName,AceBundleInfo & bundleInfo)101 bool AceApplicationInfoImpl::GetBundleInfo(const std::string& packageName, AceBundleInfo& bundleInfo)
102 {
103 return false;
104 }
105
GetInstance()106 AceApplicationInfoImpl& AceApplicationInfoImpl::GetInstance()
107 {
108 static AceApplicationInfoImpl instance;
109 return instance;
110 }
111
112 } // namespace OHOS::Ace::Platform
113
114 namespace OHOS::Ace {
115
GetInstance()116 AceApplicationInfo& AceApplicationInfo::GetInstance()
117 {
118 return Platform::AceApplicationInfoImpl::GetInstance();
119 }
120
121 } // namespace OHOS::Ace
122