• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <algorithm>
17 #include <iterator>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include "application_configuration_manager.h"
22 #include "configuration_utils.h"
23 #include "unicode/locid.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 
operator <(SetLevel lhs,SetLevel rhs)28 bool operator<(SetLevel lhs, SetLevel rhs)
29 {
30     return static_cast<uint8_t>(lhs) < static_cast<uint8_t>(rhs);
31 };
32 
operator >(SetLevel lhs,SetLevel rhs)33 bool operator>(SetLevel lhs, SetLevel rhs)
34 {
35     return static_cast<uint8_t>(lhs) > static_cast<uint8_t>(rhs);
36 };
37 
GetInstance()38 ApplicationConfigurationManager& ApplicationConfigurationManager::GetInstance()
39 {
40     static ApplicationConfigurationManager instance;
41     return instance;
42 }
43 
SetLanguageSetLevel(SetLevel languageSetLevel)44 void ApplicationConfigurationManager::SetLanguageSetLevel(SetLevel languageSetLevel)
45 {
46     languageSetLevel_ = languageSetLevel;
47 }
48 
GetLanguageSetLevel() const49 SetLevel ApplicationConfigurationManager::GetLanguageSetLevel() const
50 {
51     return languageSetLevel_;
52 }
53 
SetfontSetLevel(SetLevel fontSetLevel)54 void ApplicationConfigurationManager::SetfontSetLevel(SetLevel fontSetLevel)
55 {
56     fontSetLevel_ = fontSetLevel;
57 }
58 
GetFontSetLevel() const59 SetLevel ApplicationConfigurationManager::GetFontSetLevel() const
60 {
61     return fontSetLevel_;
62 }
63 
SetColorModeSetLevel(SetLevel colorModeSetLevel,const std::string & value)64 std::string ApplicationConfigurationManager::SetColorModeSetLevel(SetLevel colorModeSetLevel, const std::string &value)
65 {
66     colorModeVal_[static_cast<uint8_t>(colorModeSetLevel)] = value;
67     for (int i = static_cast<uint8_t>(SetLevel::SetLevelCount) - 1; i >= 0; i--) {
68         if (!colorModeVal_[i].empty() &&
69             colorModeVal_[i].compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) != 0) {
70             colorModeSetLevel_ = static_cast<SetLevel>(i);
71             break;
72         }
73     }
74 
75     return colorModeVal_[static_cast<uint8_t>(colorModeSetLevel_)];
76 }
77 
GetColorMode()78 std::string ApplicationConfigurationManager::GetColorMode()
79 {
80     for (int i = static_cast<uint8_t>(SetLevel::SetLevelCount) - 1; i >= 0; i--) {
81         if (!colorModeVal_[i].empty() &&
82             colorModeVal_[i].compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) != 0) {
83             colorModeSetLevel_ = static_cast<SetLevel>(i);
84             break;
85         }
86     }
87 
88     return colorModeVal_[static_cast<uint8_t>(colorModeSetLevel_)];
89 }
90 
GetColorModeSetLevel() const91 SetLevel ApplicationConfigurationManager::GetColorModeSetLevel() const
92 {
93     return colorModeSetLevel_;
94 }
95 
ColorModeHasSetByApplication() const96 bool ApplicationConfigurationManager::ColorModeHasSetByApplication() const
97 {
98     return !colorModeVal_[static_cast<uint8_t>(SetLevel::Application)].empty();
99 }
100 
AddIgnoreContext(std::shared_ptr<Context> context,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)101 void ApplicationConfigurationManager::AddIgnoreContext(
102     std::shared_ptr<Context> context, std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
103 {
104     ignoreContext_.insert(std::make_pair(context, resourceManager));
105 }
106 
DeleteIgnoreContext(std::shared_ptr<Context> context)107 void ApplicationConfigurationManager::DeleteIgnoreContext(std::shared_ptr<Context> context)
108 {
109     ignoreContext_.erase(context);
110 }
111 
GetIgnoreContext()112 std::vector<std::shared_ptr<Context>> ApplicationConfigurationManager::GetIgnoreContext()
113 {
114     std::vector<std::shared_ptr<Context>> keys;
115     std::transform(ignoreContext_.begin(), ignoreContext_.end(), std::back_inserter(keys),
116         [](const auto& pair) {
117             return pair.first;
118         });
119     return keys;
120 }
121 
GetIgnoreResource()122 std::vector<std::shared_ptr<Global::Resource::ResourceManager>> ApplicationConfigurationManager::GetIgnoreResource()
123 {
124     std::vector<std::shared_ptr<Global::Resource::ResourceManager>> values;
125     std::transform(ignoreContext_.begin(), ignoreContext_.end(), std::back_inserter(values),
126         [](const auto& pair) {
127             return pair.second;
128         });
129     return values;
130 }
131 
GetUpdatedLocale(const std::string & systemLocale,const std::string & systemLanguage)132 std::string ApplicationConfigurationManager::GetUpdatedLocale(const std::string& systemLocale,
133     const std::string& systemLanguage)
134 {
135     if (systemLocale.empty() || systemLanguage.empty()) {
136         return "";
137     }
138 
139     UErrorCode status = U_ZERO_ERROR;
140     icu::Locale locale = icu::Locale::forLanguageTag(systemLocale.c_str(), status);
141     if (U_FAILURE(status)) {
142         return "";
143     }
144 
145     icu::Locale language = icu::Locale::forLanguageTag(systemLanguage.c_str(), status);
146     if (U_FAILURE(status)) {
147         return "";
148     }
149 
150     std::string extendParamTag;
151     size_t pos = systemLocale.find("-u-");
152     if (pos != std::string::npos) {
153         extendParamTag = systemLocale.substr(pos);
154     }
155 
156     std::string languageTag = language.getLanguage();
157     std::string scriptTag = language.getScript();
158     std::string regionTag = locale.getCountry();
159 
160     std::string effectiveLocale = languageTag;
161     std::string splitor = "-";
162     if (!scriptTag.empty()) {
163         effectiveLocale += splitor + scriptTag;
164     }
165     if (!regionTag.empty()) {
166         effectiveLocale += splitor + regionTag;
167     }
168     if (!extendParamTag.empty()) {
169         effectiveLocale += extendParamTag;
170     }
171     return effectiveLocale;
172 }
173 } // namespace AbilityRuntime
174 } // namespace OHOS