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 "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 {
35
AceApplicationInfoImpl()36 AceApplicationInfoImpl::AceApplicationInfoImpl() {}
37
38 AceApplicationInfoImpl::~AceApplicationInfoImpl() = default;
39
SetJsEngineParam(const std::string & key,const std::string & value)40 void AceApplicationInfoImpl::SetJsEngineParam(const std::string& key, const std::string& value)
41 {
42 jsEngineParams_[key] = value;
43 }
44
GetJsEngineParam(const std::string & key) const45 std::string AceApplicationInfoImpl::GetJsEngineParam(const std::string& key) const
46 {
47 std::string value;
48 auto iter = jsEngineParams_.find(key);
49 if (iter != jsEngineParams_.end()) {
50 value = iter->second;
51 }
52 return value;
53 }
54
ChangeLocale(const std::string & language,const std::string & countryOrRegion)55 void AceApplicationInfoImpl::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
56 {
57 std::string languageLower = language;
58 std::transform(language.begin(), language.end(), languageLower.begin(), ::tolower);
59
60 std::string countryOrRegionUpper = countryOrRegion;
61 std::transform(countryOrRegion.begin(), countryOrRegion.end(), countryOrRegionUpper.begin(), ::tolower);
62
63 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
64 resourceManager_->GetResConfig(*resConfig);
65
66 auto localeInfo = resConfig->GetLocaleInfo();
67 CHECK_NULL_VOID(localeInfo);
68 auto script = Localization::ComputeScript(language, countryOrRegion);
69 resConfig->SetLocaleInfo(languageLower.c_str(), script.c_str(), countryOrRegionUpper.c_str());
70 resourceManager_->UpdateResConfig(*resConfig);
71
72 SetLocale(languageLower, countryOrRegionUpper, (script.empty()) ? "" : script, "");
73 }
74
SetLocale(const std::string & language,const std::string & countryOrRegion,const std::string & script,const std::string & keywordsAndValues)75 void AceApplicationInfoImpl::SetLocale(const std::string& language, const std::string& countryOrRegion,
76 const std::string& script, const std::string& keywordsAndValues)
77 {
78 language_ = language;
79 countryOrRegion_ = countryOrRegion;
80 script_ = script;
81 keywordsAndValues_ = keywordsAndValues;
82
83 localeTag_ = language;
84 if (!script_.empty()) {
85 localeTag_.append("-" + script_);
86 }
87 if (!countryOrRegion_.empty()) {
88 localeTag_.append("-" + countryOrRegion_);
89 }
90
91 icu::Locale locale(language_.c_str(), countryOrRegion.c_str());
92 isRightToLeft_ = locale.isRightToLeft();
93
94 auto languageList = Localization::GetLanguageList(language_);
95 if (languageList.size() == 1) {
96 Localization::SetLocale(language_, countryOrRegion_, script, languageList.front(), keywordsAndValues_);
97 } else {
98 auto selectLanguage = AceResConfig::GetLocaleFallback(localeTag_, languageList);
99 Localization::SetLocale(language_, countryOrRegion_, script, selectLanguage.front(), keywordsAndValues_);
100 }
101 }
102
GetBundleInfo(const std::string & packageName,AceBundleInfo & bundleInfo)103 bool AceApplicationInfoImpl::GetBundleInfo(const std::string& packageName, AceBundleInfo& bundleInfo)
104 {
105 return false;
106 }
107
GetInstance()108 AceApplicationInfoImpl& AceApplicationInfoImpl::GetInstance()
109 {
110 static AceApplicationInfoImpl instance;
111 return instance;
112 }
113
114 } // namespace OHOS::Ace::Platform
115
116 namespace OHOS::Ace {
117
GetInstance()118 AceApplicationInfo& AceApplicationInfo::GetInstance()
119 {
120 return Platform::AceApplicationInfoImpl::GetInstance();
121 }
122
123 } // namespace OHOS::Ace
124