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/osal/resource_adapter_impl.h"
17
18 #include <dirent.h>
19
20 #include "adapter/ohos/entrance/ace_container.h"
21 #include "adapter/ohos/osal/resource_convertor.h"
22 #include "adapter/ohos/osal/resource_theme_style.h"
23 #include "base/utils/system_properties.h"
24 #include "base/utils/utils.h"
25 #include "core/components/theme/theme_attributes.h"
26
27 namespace OHOS::Ace {
28 namespace {
29
30 constexpr uint32_t OHOS_THEME_ID = 125829872; // ohos_theme
31
CheckThemeId(int32_t & themeId)32 void CheckThemeId(int32_t& themeId)
33 {
34 if (themeId >= 0) {
35 return;
36 }
37 themeId = OHOS_THEME_ID;
38 }
39
40 const char* PATTERN_MAP[] = {
41 THEME_PATTERN_BUTTON,
42 THEME_PATTERN_CHECKBOX,
43 THEME_PATTERN_DATA_PANEL,
44 THEME_PATTERN_RADIO,
45 THEME_PATTERN_SWIPER,
46 THEME_PATTERN_SWITCH,
47 THEME_PATTERN_TOOLBAR,
48 THEME_PATTERN_TOGGLE,
49 THEME_PATTERN_TOAST,
50 THEME_PATTERN_DIALOG,
51 THEME_PATTERN_DRAG_BAR,
52 THEME_PATTERN_SEMI_MODAL,
53 // append
54 THEME_PATTERN_BADGE,
55 THEME_PATTERN_CALENDAR,
56 THEME_PATTERN_CAMERA,
57 THEME_PATTERN_CLOCK,
58 THEME_PATTERN_COUNTER,
59 THEME_PATTERN_DIVIDER,
60 THEME_PATTERN_FOCUS_ANIMATION,
61 THEME_PATTERN_GRID,
62 THEME_PATTERN_IMAGE,
63 THEME_PATTERN_LIST,
64 THEME_PATTERN_LIST_ITEM,
65 THEME_PATTERN_MARQUEE,
66 THEME_PATTERN_NAVIGATION_BAR,
67 THEME_PATTERN_PICKER,
68 THEME_PATTERN_PIECE,
69 THEME_PATTERN_POPUP,
70 THEME_PATTERN_PROGRESS,
71 THEME_PATTERN_QRCODE,
72 THEME_PATTERN_RATING,
73 THEME_PATTERN_REFRESH,
74 THEME_PATTERN_SCROLL_BAR,
75 THEME_PATTERN_SEARCH,
76 THEME_PATTERN_SELECT,
77 THEME_PATTERN_SLIDER,
78 THEME_PATTERN_STEPPER,
79 THEME_PATTERN_TAB,
80 THEME_PATTERN_TEXT,
81 THEME_PATTERN_TEXTFIELD,
82 THEME_PATTERN_TEXT_OVERLAY,
83 THEME_PATTERN_VIDEO,
84 THEME_PATTERN_ICON,
85 THEME_PATTERN_INDEXER,
86 THEME_PATTERN_APP_BAR,
87 };
88
IsDirExist(const std::string & path)89 bool IsDirExist(const std::string& path)
90 {
91 char realPath[PATH_MAX] = { 0x00 };
92 CHECK_NULL_RETURN_NOLOG(realpath(path.c_str(), realPath), false);
93 DIR* dir = opendir(realPath);
94 CHECK_NULL_RETURN_NOLOG(dir, false);
95 closedir(dir);
96 return true;
97 }
98
ParseDimensionUnit(const std::string & unit)99 DimensionUnit ParseDimensionUnit(const std::string& unit)
100 {
101 if (unit == "px") {
102 return DimensionUnit::PX;
103 } else if (unit == "fp") {
104 return DimensionUnit::FP;
105 } else if (unit == "lpx") {
106 return DimensionUnit::LPX;
107 } else if (unit == "%") {
108 return DimensionUnit::PERCENT;
109 } else {
110 return DimensionUnit::VP;
111 }
112 };
113
114 } // namespace
115
Create()116 RefPtr<ResourceAdapter> ResourceAdapter::Create()
117 {
118 return AceType::MakeRefPtr<ResourceAdapterImpl>();
119 }
120
Init(const ResourceInfo & resourceInfo)121 void ResourceAdapterImpl::Init(const ResourceInfo& resourceInfo)
122 {
123 std::string resPath = resourceInfo.GetPackagePath();
124 std::string hapPath = resourceInfo.GetHapPath();
125 auto resConfig = ConvertConfigToGlobal(resourceInfo.GetResourceConfiguration());
126 std::shared_ptr<Global::Resource::ResourceManager> newResMgr(Global::Resource::CreateResourceManager());
127 std::string resIndexPath = hapPath.empty() ? (resPath + "resources.index") : hapPath;
128 auto resRet = newResMgr->AddResource(resIndexPath.c_str());
129 auto configRet = newResMgr->UpdateResConfig(*resConfig);
130 LOGI("AddRes result=%{public}d, UpdateResConfig result=%{public}d, ori=%{public}d, dpi=%{public}f, "
131 "device=%{public}d, colorMode=%{publid}d, inputDevice=%{public}d",
132 resRet, configRet, resConfig->GetDirection(), resConfig->GetScreenDensity(), resConfig->GetDeviceType(),
133 resConfig->GetColorMode(), resConfig->GetInputDevice());
134 sysResourceManager_ = newResMgr;
135 {
136 std::unique_lock<std::shared_mutex> lock(resourceMutex_);
137 resourceManager_ = sysResourceManager_;
138 }
139 packagePathStr_ = (hapPath.empty() || IsDirExist(resPath)) ? resPath : std::string();
140 resConfig_ = resConfig;
141 }
142
Reload()143 void ResourceAdapterImpl::Reload()
144 {
145 std::string resIndexPath = packagePathStr_ + "resources.index";
146 auto resRet = sysResourceManager_->AddResource(resIndexPath.c_str());
147 auto configRet = sysResourceManager_->UpdateResConfig(*resConfig_);
148 LOGI("UICast result=%{public}d, UpdateResConfig result=%{public}d", resRet, configRet);
149 }
150
UpdateConfig(const ResourceConfiguration & config)151 void ResourceAdapterImpl::UpdateConfig(const ResourceConfiguration& config)
152 {
153 auto resConfig = ConvertConfigToGlobal(config);
154 LOGI("UpdateConfig ori=%{public}d, dpi=%{public}f, device=%{public}d, "
155 "colorMode=%{publid}d, inputDevice=%{public}d",
156 resConfig->GetDirection(), resConfig->GetScreenDensity(), resConfig->GetDeviceType(),
157 resConfig->GetColorMode(), resConfig->GetInputDevice());
158 if (sysResourceManager_) {
159 sysResourceManager_->UpdateResConfig(*resConfig);
160 }
161 for (auto& resMgr : resourceManagers_) {
162 resMgr.second->UpdateResConfig(*resConfig);
163 }
164 resConfig_ = resConfig;
165 }
166
GetTheme(int32_t themeId)167 RefPtr<ThemeStyle> ResourceAdapterImpl::GetTheme(int32_t themeId)
168 {
169 CheckThemeId(themeId);
170 auto theme = AceType::MakeRefPtr<ResourceThemeStyle>(AceType::Claim(this));
171 constexpr char OHFlag[] = "ohos_"; // fit with resource/base/theme.json and pattern.json
172 {
173 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
174 if (resourceManager_) {
175 auto ret = resourceManager_->GetThemeById(themeId, theme->rawAttrs_);
176 for (size_t i = 0; i < sizeof(PATTERN_MAP) / sizeof(PATTERN_MAP[0]); i++) {
177 ResourceThemeStyle::RawAttrMap attrMap;
178 std::string patternTag = PATTERN_MAP[i];
179 std::string patternName = std::string(OHFlag) + PATTERN_MAP[i];
180 ret = resourceManager_->GetPatternByName(patternName.c_str(), attrMap);
181 LOGD("theme pattern[%{public}s, %{public}s], attr size=%{public}zu", patternTag.c_str(),
182 patternName.c_str(), attrMap.size());
183 if (attrMap.empty()) {
184 continue;
185 }
186 theme->patternAttrs_[patternTag] = attrMap;
187 }
188 LOGI("themeId=%{public}d, ret=%{public}d, attr size=%{public}zu, pattern size=%{public}zu", themeId, ret,
189 theme->rawAttrs_.size(), theme->patternAttrs_.size());
190 }
191 }
192
193 if (theme->patternAttrs_.empty() && theme->rawAttrs_.empty()) {
194 LOGW("theme resource get failed, use default theme config.");
195 return nullptr;
196 }
197
198 theme->ParseContent();
199 theme->patternAttrs_.clear();
200 return theme;
201 }
202
GetColor(uint32_t resId)203 Color ResourceAdapterImpl::GetColor(uint32_t resId)
204 {
205 uint32_t result = 0;
206 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
207 CHECK_NULL_RETURN_NOLOG(resourceManager_, Color(result));
208 auto state = resourceManager_->GetColorById(resId, result);
209 if (state != Global::Resource::SUCCESS) {
210 LOGE("GetColor error, id=%{public}u", resId);
211 }
212 return Color(result);
213 }
214
GetColorByName(const std::string & resName)215 Color ResourceAdapterImpl::GetColorByName(const std::string& resName)
216 {
217 uint32_t result = 0;
218 auto actualResName = GetActualResourceName(resName);
219 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
220 CHECK_NULL_RETURN_NOLOG(resourceManager_, Color(result));
221 auto state = resourceManager_->GetColorByName(actualResName.c_str(), result);
222 if (state != Global::Resource::SUCCESS) {
223 LOGE("GetColor error, resName=%{public}s", resName.c_str());
224 }
225 return Color(result);
226 }
227
GetDimension(uint32_t resId)228 Dimension ResourceAdapterImpl::GetDimension(uint32_t resId)
229 {
230 if (Container::IsCurrentUseNewPipeline()) {
231 float dimensionFloat = 0.0f;
232 std::string unit;
233 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
234 if (resourceManager_) {
235 auto state = resourceManager_->GetFloatById(resId, dimensionFloat, unit);
236 if (state != Global::Resource::SUCCESS) {
237 LOGE("NG: GetDimension error, id=%{public}u", resId);
238 }
239 }
240 return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
241 }
242
243 float dimensionFloat = 0.0f;
244
245 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
246 CHECK_NULL_RETURN_NOLOG(resourceManager_, Dimension(static_cast<double>(dimensionFloat)));
247 auto state = resourceManager_->GetFloatById(resId, dimensionFloat);
248 if (state != Global::Resource::SUCCESS) {
249 LOGE("GetDimension error, id=%{public}u", resId);
250 }
251 return Dimension(static_cast<double>(dimensionFloat));
252 }
253
GetDimensionByName(const std::string & resName)254 Dimension ResourceAdapterImpl::GetDimensionByName(const std::string& resName)
255 {
256 float dimensionFloat = 0.0f;
257 auto actualResName = GetActualResourceName(resName);
258 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
259 CHECK_NULL_RETURN_NOLOG(resourceManager_, Dimension());
260 std::string unit;
261 auto state = resourceManager_->GetFloatByName(actualResName.c_str(), dimensionFloat, unit);
262 if (state != Global::Resource::SUCCESS) {
263 LOGE("GetDimension error, resName=%{public}s", resName.c_str());
264 }
265 return Dimension(static_cast<double>(dimensionFloat), ParseDimensionUnit(unit));
266 }
267
GetString(uint32_t resId)268 std::string ResourceAdapterImpl::GetString(uint32_t resId)
269 {
270 std::string strResult = "";
271 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
272 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResult);
273 auto state = resourceManager_->GetStringById(resId, strResult);
274 if (state != Global::Resource::SUCCESS) {
275 LOGE("GetString error, id=%{public}u", resId);
276 }
277 return strResult;
278 }
279
GetStringByName(const std::string & resName)280 std::string ResourceAdapterImpl::GetStringByName(const std::string& resName)
281 {
282 std::string strResult = "";
283 auto actualResName = GetActualResourceName(resName);
284 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
285 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResult);
286 auto state = resourceManager_->GetStringByName(actualResName.c_str(), strResult);
287 if (state != Global::Resource::SUCCESS) {
288 LOGD("GetString error, resName=%{public}s", resName.c_str());
289 }
290 return strResult;
291 }
292
GetPluralString(uint32_t resId,int quantity)293 std::string ResourceAdapterImpl::GetPluralString(uint32_t resId, int quantity)
294 {
295 std::string strResult = "";
296 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
297 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResult);
298 auto state = resourceManager_->GetPluralStringById(resId, quantity, strResult);
299 if (state != Global::Resource::SUCCESS) {
300 LOGE("GetPluralString error, id=%{public}u", resId);
301 }
302 return strResult;
303 }
304
GetPluralStringByName(const std::string & resName,int quantity)305 std::string ResourceAdapterImpl::GetPluralStringByName(const std::string& resName, int quantity)
306 {
307 std::string strResult = "";
308 auto actualResName = GetActualResourceName(resName);
309 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
310 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResult);
311 auto state = resourceManager_->GetPluralStringByName(actualResName.c_str(), quantity, strResult);
312 if (state != Global::Resource::SUCCESS) {
313 LOGE("GetPluralString error, resName=%{public}s", resName.c_str());
314 }
315 return strResult;
316 }
317
GetStringArray(uint32_t resId) const318 std::vector<std::string> ResourceAdapterImpl::GetStringArray(uint32_t resId) const
319 {
320 std::vector<std::string> strResults;
321 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
322 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResults);
323 auto state = resourceManager_->GetStringArrayById(resId, strResults);
324 if (state != Global::Resource::SUCCESS) {
325 LOGE("GetStringArray error, id=%{public}u", resId);
326 }
327 return strResults;
328 }
329
GetStringArrayByName(const std::string & resName) const330 std::vector<std::string> ResourceAdapterImpl::GetStringArrayByName(const std::string& resName) const
331 {
332 std::vector<std::string> strResults;
333 auto actualResName = GetActualResourceName(resName);
334 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
335 CHECK_NULL_RETURN_NOLOG(resourceManager_, strResults);
336 auto state = resourceManager_->GetStringArrayByName(actualResName.c_str(), strResults);
337 if (state != Global::Resource::SUCCESS) {
338 LOGE("GetStringArray error, resName=%{public}s", resName.c_str());
339 }
340 return strResults;
341 }
342
GetDouble(uint32_t resId)343 double ResourceAdapterImpl::GetDouble(uint32_t resId)
344 {
345 float result = 0.0f;
346 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
347 CHECK_NULL_RETURN_NOLOG(resourceManager_, static_cast<double>(result));
348 auto state = resourceManager_->GetFloatById(resId, result);
349 if (state != Global::Resource::SUCCESS) {
350 LOGE("GetDouble error, id=%{public}u", resId);
351 }
352 return static_cast<double>(result);
353 }
354
GetDoubleByName(const std::string & resName)355 double ResourceAdapterImpl::GetDoubleByName(const std::string& resName)
356 {
357 float result = 0.0f;
358 auto actualResName = GetActualResourceName(resName);
359 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
360 CHECK_NULL_RETURN_NOLOG(resourceManager_, static_cast<double>(result));
361 auto state = resourceManager_->GetFloatByName(actualResName.c_str(), result);
362 if (state != Global::Resource::SUCCESS) {
363 LOGE("GetDouble error, resName=%{public}s", resName.c_str());
364 }
365 return static_cast<double>(result);
366 }
367
GetInt(uint32_t resId)368 int32_t ResourceAdapterImpl::GetInt(uint32_t resId)
369 {
370 int32_t result = 0;
371 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
372 CHECK_NULL_RETURN_NOLOG(resourceManager_, result);
373 auto state = resourceManager_->GetIntegerById(resId, result);
374 if (state != Global::Resource::SUCCESS) {
375 LOGE("GetInt error, id=%{public}u", resId);
376 }
377 return result;
378 }
379
GetIntByName(const std::string & resName)380 int32_t ResourceAdapterImpl::GetIntByName(const std::string& resName)
381 {
382 int32_t result = 0;
383 auto actualResName = GetActualResourceName(resName);
384 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
385 CHECK_NULL_RETURN_NOLOG(resourceManager_, result);
386 auto state = resourceManager_->GetIntegerByName(actualResName.c_str(), result);
387 if (state != Global::Resource::SUCCESS) {
388 LOGE("GetInt error, resName=%{public}s", resName.c_str());
389 }
390 return result;
391 }
392
GetIntArray(uint32_t resId) const393 std::vector<uint32_t> ResourceAdapterImpl::GetIntArray(uint32_t resId) const
394 {
395 std::vector<int> intVectorResult;
396 {
397 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
398 if (resourceManager_) {
399 auto state = resourceManager_->GetIntArrayById(resId, intVectorResult);
400 if (state != Global::Resource::SUCCESS) {
401 LOGE("GetIntArray error, id=%{public}u", resId);
402 }
403 }
404 }
405
406 std::vector<uint32_t> result;
407 std::transform(
408 intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); });
409 return result;
410 }
411
GetIntArrayByName(const std::string & resName) const412 std::vector<uint32_t> ResourceAdapterImpl::GetIntArrayByName(const std::string& resName) const
413 {
414 std::vector<int> intVectorResult;
415 auto actualResName = GetActualResourceName(resName);
416 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
417 CHECK_NULL_RETURN_NOLOG(resourceManager_, {});
418 auto state = resourceManager_->GetIntArrayByName(actualResName.c_str(), intVectorResult);
419 if (state != Global::Resource::SUCCESS) {
420 LOGE("GetIntArray error, resName=%{public}s", resName.c_str());
421 }
422
423 std::vector<uint32_t> result;
424 std::transform(
425 intVectorResult.begin(), intVectorResult.end(), result.begin(), [](int x) { return static_cast<uint32_t>(x); });
426 return result;
427 }
428
GetBoolean(uint32_t resId) const429 bool ResourceAdapterImpl::GetBoolean(uint32_t resId) const
430 {
431 bool result = false;
432 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
433 CHECK_NULL_RETURN_NOLOG(resourceManager_, result);
434 auto state = resourceManager_->GetBooleanById(resId, result);
435 if (state != Global::Resource::SUCCESS) {
436 LOGE("GetBoolean error, id=%{public}u", resId);
437 }
438 return result;
439 }
440
GetBooleanByName(const std::string & resName) const441 bool ResourceAdapterImpl::GetBooleanByName(const std::string& resName) const
442 {
443 bool result = false;
444 auto actualResName = GetActualResourceName(resName);
445 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
446 CHECK_NULL_RETURN_NOLOG(resourceManager_, result);
447 auto state = resourceManager_->GetBooleanByName(actualResName.c_str(), result);
448 if (state != Global::Resource::SUCCESS) {
449 LOGE("GetBoolean error, resName=%{public}s", resName.c_str());
450 }
451 return result;
452 }
453
GetMediaPath(uint32_t resId)454 std::string ResourceAdapterImpl::GetMediaPath(uint32_t resId)
455 {
456 CHECK_NULL_RETURN_NOLOG(resourceManager_, "");
457 std::string mediaPath = "";
458 {
459 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
460 auto state = resourceManager_->GetMediaById(resId, mediaPath);
461 if (state != Global::Resource::SUCCESS) {
462 LOGE("GetMediaById error, id=%{public}u, errorCode=%{public}u", resId, state);
463 return "";
464 }
465 }
466 if (SystemProperties::GetUnZipHap()) {
467 return "file:///" + mediaPath;
468 }
469 auto pos = mediaPath.find_last_of('.');
470 if (pos == std::string::npos) {
471 LOGE("GetMediaById error, return mediaPath[%{private}s] format error", mediaPath.c_str());
472 return "";
473 }
474 return "resource:///" + std::to_string(resId) + mediaPath.substr(pos);
475 }
476
GetMediaPathByName(const std::string & resName)477 std::string ResourceAdapterImpl::GetMediaPathByName(const std::string& resName)
478 {
479 std::string mediaPath = "";
480 auto actualResName = GetActualResourceName(resName);
481 {
482 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
483 CHECK_NULL_RETURN_NOLOG(resourceManager_, "");
484 auto state = resourceManager_->GetMediaByName(actualResName.c_str(), mediaPath);
485 if (state != Global::Resource::SUCCESS) {
486 LOGE("GetMediaPathByName error, resName=%{public}s, errorCode=%{public}u", resName.c_str(), state);
487 return "";
488 }
489 }
490 if (SystemProperties::GetUnZipHap()) {
491 return "file:///" + mediaPath;
492 }
493 auto pos = mediaPath.find_last_of('.');
494 if (pos == std::string::npos) {
495 LOGE("GetMediaById error, return mediaPath[%{private}s] format error", mediaPath.c_str());
496 return "";
497 }
498 return "resource:///" + actualResName + mediaPath.substr(pos);
499 }
500
GetRawfile(const std::string & fileName)501 std::string ResourceAdapterImpl::GetRawfile(const std::string& fileName)
502 {
503 // as web component not support resource format: resource://RAWFILE/{fileName}, use old format
504 if (!packagePathStr_.empty()) {
505 std::string outPath;
506 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
507 CHECK_NULL_RETURN_NOLOG(resourceManager_, "");
508 // Adapt to the input like: "file:///index.html?a=1", before the new solution comes.
509 auto it = std::find_if(fileName.begin(), fileName.end(), [](char c) {
510 return (c == '#') || (c == '?');
511 });
512 std::string params;
513 std::string newFileName = fileName;
514 if (it != fileName.end()) {
515 newFileName = std::string(fileName.begin(), it);
516 params = std::string(it, fileName.end());
517 }
518 auto state = resourceManager_->GetRawFilePathByName(newFileName, outPath);
519 if (state != Global::Resource::SUCCESS) {
520 LOGE("GetRawfile error, filename:%{public}s, error:%{public}u", fileName.c_str(), state);
521 return "";
522 }
523 return "file:///" + outPath + params;
524 }
525 return "resource://RAWFILE/" + fileName;
526 }
527
GetRawFileData(const std::string & rawFile,size_t & len,std::unique_ptr<uint8_t[]> & dest)528 bool ResourceAdapterImpl::GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest)
529 {
530 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
531 CHECK_NULL_RETURN_NOLOG(resourceManager_, false);
532 auto rawFileObj = std::make_unique<OHOS::Global::Resource::ResourceManager::RawFile>();
533 auto state = resourceManager_->GetRawFileFromHap(rawFile, rawFileObj);
534 if (state != Global::Resource::SUCCESS || !rawFileObj || !rawFileObj->buffer) {
535 LOGE("GetRawFileFromHap error, raw filename:%{public}s, error:%{public}u", rawFile.c_str(), state);
536 return false;
537 }
538 len = rawFileObj->length;
539 dest.swap(rawFileObj->buffer);
540 return true;
541 }
542
GetMediaData(uint32_t resId,size_t & len,std::unique_ptr<uint8_t[]> & dest)543 bool ResourceAdapterImpl::GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest)
544 {
545 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
546 CHECK_NULL_RETURN_NOLOG(resourceManager_, false);
547 auto state = resourceManager_->GetMediaDataById(resId, len, dest);
548 if (state != Global::Resource::SUCCESS) {
549 LOGE("GetMediaDataById error, id=%{public}u, error:%{public}u", resId, state);
550 return false;
551 }
552 return true;
553 }
554
GetMediaData(const std::string & resName,size_t & len,std::unique_ptr<uint8_t[]> & dest)555 bool ResourceAdapterImpl::GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest)
556 {
557 std::shared_lock<std::shared_mutex> lock(resourceMutex_);
558 CHECK_NULL_RETURN_NOLOG(resourceManager_, false);
559 auto state = resourceManager_->GetMediaDataByName(resName.c_str(), len, dest);
560 if (state != Global::Resource::SUCCESS) {
561 LOGE("GetMediaDataByName error, res=%{public}s, error:%{public}u", resName.c_str(), state);
562 return false;
563 }
564 return true;
565 }
566
UpdateResourceManager(const std::string & bundleName,const std::string & moduleName)567 void ResourceAdapterImpl::UpdateResourceManager(const std::string& bundleName, const std::string& moduleName)
568 {
569 std::unique_lock<std::shared_mutex> lcok(resourceMutex_);
570 if (bundleName.empty() || moduleName.empty()) {
571 resourceManager_ = sysResourceManager_;
572 return;
573 }
574
575 auto resourceMgrIter = resourceManagers_.find({ bundleName, moduleName });
576 if (resourceMgrIter != resourceManagers_.end()) {
577 resourceManager_ = resourceMgrIter->second;
578 return;
579 } else {
580 auto container = Container::Current();
581 CHECK_NULL_VOID(container);
582 auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
583 CHECK_NULL_VOID(aceContainer);
584 auto context = aceContainer->GetAbilityContextByModule(bundleName, moduleName);
585 CHECK_NULL_VOID(context);
586 resourceManagers_[{ bundleName, moduleName }] = context->GetResourceManager();
587 resourceManager_ = context->GetResourceManager();
588 if (resourceManager_) {
589 resourceManager_->UpdateResConfig(*resConfig_);
590 }
591 }
592 }
593
GetActualResourceName(const std::string & resName) const594 std::string ResourceAdapterImpl::GetActualResourceName(const std::string& resName) const
595 {
596 auto index = resName.find_last_of('.');
597 if (index == std::string::npos) {
598 LOGE("GetActualResourceName error, incorrect resName format.");
599 return "";
600 }
601 return resName.substr(index + 1, resName.length() - index - 1);
602 }
603
604 } // namespace OHOS::Ace
605