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
24 namespace OHOS {
25 namespace AbilityRuntime {
26
operator <(SetLevel lhs,SetLevel rhs)27 bool operator<(SetLevel lhs, SetLevel rhs)
28 {
29 return static_cast<uint8_t>(lhs) < static_cast<uint8_t>(rhs);
30 };
31
operator >(SetLevel lhs,SetLevel rhs)32 bool operator>(SetLevel lhs, SetLevel rhs)
33 {
34 return static_cast<uint8_t>(lhs) > static_cast<uint8_t>(rhs);
35 };
36
GetInstance()37 ApplicationConfigurationManager& ApplicationConfigurationManager::GetInstance()
38 {
39 static ApplicationConfigurationManager instance;
40 return instance;
41 }
42
SetLanguageSetLevel(SetLevel languageSetLevel)43 void ApplicationConfigurationManager::SetLanguageSetLevel(SetLevel languageSetLevel)
44 {
45 languageSetLevel_ = languageSetLevel;
46 }
47
GetLanguageSetLevel() const48 SetLevel ApplicationConfigurationManager::GetLanguageSetLevel() const
49 {
50 return languageSetLevel_;
51 }
52
SetfontSetLevel(SetLevel fontSetLevel)53 void ApplicationConfigurationManager::SetfontSetLevel(SetLevel fontSetLevel)
54 {
55 fontSetLevel_ = fontSetLevel;
56 }
57
GetFontSetLevel() const58 SetLevel ApplicationConfigurationManager::GetFontSetLevel() const
59 {
60 return fontSetLevel_;
61 }
62
SetColorModeSetLevel(SetLevel colorModeSetLevel,const std::string & value)63 std::string ApplicationConfigurationManager::SetColorModeSetLevel(SetLevel colorModeSetLevel, const std::string &value)
64 {
65 colorModeVal_[static_cast<uint8_t>(colorModeSetLevel)] = value;
66 for (int i = static_cast<uint8_t>(SetLevel::SetLevelCount) - 1; i >= 0; i--) {
67 if (!colorModeVal_[i].empty() &&
68 colorModeVal_[i].compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) != 0) {
69 colorModeSetLevel_ = static_cast<SetLevel>(i);
70 break;
71 }
72 }
73
74 return colorModeVal_[static_cast<uint8_t>(colorModeSetLevel_)];
75 }
76
GetColorMode()77 std::string ApplicationConfigurationManager::GetColorMode()
78 {
79 for (int i = static_cast<uint8_t>(SetLevel::SetLevelCount) - 1; i >= 0; i--) {
80 if (!colorModeVal_[i].empty() &&
81 colorModeVal_[i].compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) != 0) {
82 colorModeSetLevel_ = static_cast<SetLevel>(i);
83 break;
84 }
85 }
86
87 return colorModeVal_[static_cast<uint8_t>(colorModeSetLevel_)];
88 }
89
GetColorModeSetLevel() const90 SetLevel ApplicationConfigurationManager::GetColorModeSetLevel() const
91 {
92 return colorModeSetLevel_;
93 }
94
ColorModeHasSetByApplication() const95 bool ApplicationConfigurationManager::ColorModeHasSetByApplication() const
96 {
97 return !colorModeVal_[static_cast<uint8_t>(SetLevel::Application)].empty();
98 }
99
AddIgnoreContext(std::shared_ptr<Context> context,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)100 void ApplicationConfigurationManager::AddIgnoreContext(
101 std::shared_ptr<Context> context, std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
102 {
103 ignoreContext_.insert(std::make_pair(context, resourceManager));
104 }
105
DeleteIgnoreContext(std::shared_ptr<Context> context)106 void ApplicationConfigurationManager::DeleteIgnoreContext(std::shared_ptr<Context> context)
107 {
108 ignoreContext_.erase(context);
109 }
110
GetIgnoreContext()111 std::vector<std::shared_ptr<Context>> ApplicationConfigurationManager::GetIgnoreContext()
112 {
113 std::vector<std::shared_ptr<Context>> keys;
114 std::transform(ignoreContext_.begin(), ignoreContext_.end(), std::back_inserter(keys),
115 [](const auto& pair) {
116 return pair.first;
117 });
118 return keys;
119 }
120
GetIgnoreResource()121 std::vector<std::shared_ptr<Global::Resource::ResourceManager>> ApplicationConfigurationManager::GetIgnoreResource()
122 {
123 std::vector<std::shared_ptr<Global::Resource::ResourceManager>> values;
124 std::transform(ignoreContext_.begin(), ignoreContext_.end(), std::back_inserter(values),
125 [](const auto& pair) {
126 return pair.second;
127 });
128 return values;
129 }
130 } // namespace AbilityRuntime
131 } // namespace OHOS