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 "ui/base/ace_type.h"
17 #include "ui/resource/resource_parser.h"
18
19 #include "base/utils/system_properties.h"
20 #include "core/common/container.h"
21 #include "core/common/resource/resource_manager.h"
22 #include "core/common/resource/resource_wrapper.h"
23
24 namespace OHOS::Ace::Kit {
CreateResourceWrapper(const ResourceInfo & info)25 static RefPtr<Ace::ResourceAdapter> CreateResourceWrapper(const ResourceInfo& info)
26 {
27 auto bundleName = info.bundleName;
28 auto moduleName = info.moduleName;
29
30 RefPtr<Ace::ResourceAdapter> resourceAdapter = nullptr;
31 if (bundleName.has_value() && moduleName.has_value()) {
32 auto resourceObject = AceType::MakeRefPtr<Ace::ResourceObject>(
33 bundleName.value_or(""), moduleName.value_or(""), Container::CurrentIdSafely());
34 resourceAdapter = Ace::ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject);
35 } else {
36 resourceAdapter = Ace::ResourceManager::GetInstance().GetResourceAdapter(Container::CurrentIdSafely());
37 }
38 return resourceAdapter;
39 }
40
GetDimension(const ResourceInfo & resourceInfo,Ace::Dimension & dimension)41 bool ResourceParser::GetDimension(const ResourceInfo& resourceInfo, Ace::Dimension& dimension)
42 {
43 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
44 if (!resourceWrapper) {
45 return false;
46 }
47
48 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
49 dimension = resourceWrapper->GetDimensionByName(resourceInfo.params[0]);
50 } else {
51 dimension = resourceWrapper->GetDimension(resourceInfo.resId);
52 }
53 return true;
54 }
55
GetColor(const ResourceInfo & resourceInfo,Ace::Color & color)56 bool ResourceParser::GetColor(const ResourceInfo& resourceInfo, Ace::Color& color)
57 {
58 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
59 if (!resourceWrapper) {
60 return false;
61 }
62
63 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
64 color = resourceWrapper->GetColorByName(resourceInfo.params[0]);
65 } else {
66 color = resourceWrapper->GetColor(resourceInfo.resId);
67 }
68 return true;
69 }
70
GetString(const ResourceInfo & resourceInfo,std::string & str)71 bool ResourceParser::GetString(const ResourceInfo& resourceInfo, std::string& str)
72 {
73 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
74 if (!resourceWrapper) {
75 return false;
76 }
77
78 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
79 str = resourceWrapper->GetStringByName(resourceInfo.params[0]);
80 } else {
81 str = resourceWrapper->GetString(resourceInfo.resId);
82 }
83 return true;
84 }
85
GetMediaPath(const ResourceInfo & resourceInfo,std::string & mediaPath)86 bool ResourceParser::GetMediaPath(const ResourceInfo& resourceInfo, std::string& mediaPath)
87 {
88 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
89 if (!resourceWrapper) {
90 return false;
91 }
92
93 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
94 mediaPath = resourceWrapper->GetMediaPathByName(resourceInfo.params[0]);
95 } else {
96 mediaPath = resourceWrapper->GetMediaPath(resourceInfo.resId);
97 }
98 return true;
99 }
100
GetInt(const ResourceInfo & resourceInfo,int32_t & intRes)101 bool ResourceParser::GetInt(const ResourceInfo& resourceInfo, int32_t& intRes)
102 {
103 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
104 if (!resourceWrapper) {
105 return false;
106 }
107
108 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
109 intRes = resourceWrapper->GetIntByName(resourceInfo.params[0]);
110 } else {
111 intRes = resourceWrapper->GetInt(resourceInfo.resId);
112 }
113 return true;
114 }
115
GetDouble(const ResourceInfo & resourceInfo,double & doubleRes)116 bool ResourceParser::GetDouble(const ResourceInfo& resourceInfo, double& doubleRes)
117 {
118 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
119 if (!resourceWrapper) {
120 return false;
121 }
122
123 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
124 doubleRes = resourceWrapper->GetDoubleByName(resourceInfo.params[0]);
125 } else {
126 doubleRes = resourceWrapper->GetDouble(resourceInfo.resId);
127 }
128 return true;
129 }
130
GetPluralString(const ResourceInfo & resourceInfo,int count,std::string & str)131 bool ResourceParser::GetPluralString(const ResourceInfo& resourceInfo, int count, std::string& str)
132 {
133 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
134 if (!resourceWrapper) {
135 return false;
136 }
137
138 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
139 str = resourceWrapper->GetPluralStringByName(resourceInfo.params[0], count);
140 } else {
141 str = resourceWrapper->GetPluralString(resourceInfo.resId, count);
142 }
143 return true;
144 }
145
GetBoolean(const ResourceInfo & resourceInfo,bool & boolRes)146 bool ResourceParser::GetBoolean(const ResourceInfo& resourceInfo, bool& boolRes)
147 {
148 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
149 if (!resourceWrapper) {
150 return false;
151 }
152
153 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
154 boolRes = resourceWrapper->GetBooleanByName(resourceInfo.params[0]);
155 } else {
156 boolRes = resourceWrapper->GetBoolean(resourceInfo.resId);
157 }
158 return true;
159 }
160
GetIntArray(const ResourceInfo & resourceInfo,std::vector<uint32_t> & intArray)161 bool ResourceParser::GetIntArray(const ResourceInfo& resourceInfo, std::vector<uint32_t>& intArray)
162 {
163 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
164 if (!resourceWrapper) {
165 return false;
166 }
167
168 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
169 intArray = resourceWrapper->GetIntArrayByName(resourceInfo.params[0]);
170 } else {
171 intArray = resourceWrapper->GetIntArray(resourceInfo.resId);
172 }
173 return true;
174 }
175
GetStringArray(const ResourceInfo & resourceInfo,std::vector<std::string> & strArray)176 bool ResourceParser::GetStringArray(const ResourceInfo& resourceInfo, std::vector<std::string>& strArray)
177 {
178 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
179 if (!resourceWrapper) {
180 return false;
181 }
182
183 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
184 strArray = resourceWrapper->GetStringArrayByName(resourceInfo.params[0]);
185 } else {
186 strArray = resourceWrapper->GetStringArray(resourceInfo.resId);
187 }
188 return true;
189 }
190
GetSymbol(const ResourceInfo & resourceInfo,uint32_t & symbolRes)191 bool ResourceParser::GetSymbol(const ResourceInfo& resourceInfo, uint32_t& symbolRes)
192 {
193 auto resourceWrapper = CreateResourceWrapper(resourceInfo);
194 if (!resourceWrapper) {
195 return false;
196 }
197
198 if (resourceInfo.resId == UNKNOWN_RESOURCE_ID) {
199 symbolRes = resourceWrapper->GetSymbolByName(resourceInfo.params[0].c_str());
200 } else {
201 symbolRes = resourceWrapper->GetSymbolById(resourceInfo.resId);
202 }
203 return true;
204 }
205
206 } // namespace OHOS::Ace::Kit
207