1 /*
2 * Copyright (c) 2023 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 "bundle_resource_parser.h"
17
18 #include "bundle_resource_configuration.h"
19 #include "bundle_resource_image_info.h"
20 #include "bundle_resource_drawable.h"
21 #include "bundle_service_constants.h"
22 #include "bundle_util.h"
23 #include "json_util.h"
24
25 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
26 #include "image_source.h"
27 #include "pixel_map.h"
28 #endif
29
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 constexpr const char* TYPE_JSON = "json";
34 constexpr const char* TYPE_PNG = "png";
35 constexpr const char* FOREGROUND = "foreground";
36 constexpr const char* BACKGROUND = "background";
37 constexpr char CHAR_COLON = ':';
38 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
39 constexpr const char* OHOS_CLONE_APP_BADGE_RESOURCE = "clone_app_badge_";
40 constexpr int8_t BADGE_SIZE = 62;
41 #endif
42 struct LayeredImage {
43 std::string foreground;
44 std::string background;
45 };
46
from_json(const nlohmann::json & jsonObject,LayeredImage & layeredImage)47 void from_json(const nlohmann::json &jsonObject, LayeredImage &layeredImage)
48 {
49 int32_t parseResult = 0;
50 const auto &jsonObjectEnd = jsonObject.end();
51 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, FOREGROUND, layeredImage.foreground,
52 false, parseResult);
53
54 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, BACKGROUND, layeredImage.background,
55 false, parseResult);
56 }
57
58 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
GetBadgeResource(const std::string & resourceName,std::shared_ptr<Media::PixelMap> & badgePixelMap)59 bool GetBadgeResource(const std::string &resourceName, std::shared_ptr<Media::PixelMap> &badgePixelMap)
60 {
61 std::shared_ptr<Global::Resource::ResourceManager> resMgr(Global::Resource::CreateResourceManager());
62 if (resMgr == nullptr) {
63 APP_LOGE("resMgr is nullptr");
64 return false;
65 }
66 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
67 if (resConfig == nullptr) {
68 APP_LOGE("resConfig is nullptr");
69 return false;
70 }
71 resMgr->UpdateResConfig(*resConfig);
72
73 std::unique_ptr<uint8_t[]> badgeResourceData;
74 size_t badgeResourceDataLength = 0;
75 auto ret = resMgr->GetMediaDataByName(resourceName.c_str(), badgeResourceDataLength, badgeResourceData);
76 if (ret != Global::Resource::RState::SUCCESS) {
77 APP_LOGE("get (%{public}s) failed, errorCode:%{public}d", resourceName.c_str(), static_cast<int32_t>(ret));
78 return false;
79 }
80
81 Media::SourceOptions opts;
82 uint32_t errorCode = 0;
83 std::unique_ptr<Media::ImageSource> imageSource =
84 Media::ImageSource::CreateImageSource(badgeResourceData.get(), badgeResourceDataLength, opts, errorCode);
85 Media::DecodeOptions decodeOpts;
86 decodeOpts.desiredPixelFormat = Media::PixelFormat::BGRA_8888;
87 decodeOpts.desiredSize.width = BADGE_SIZE;
88 decodeOpts.desiredSize.height = BADGE_SIZE;
89 if (imageSource) {
90 auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode);
91 badgePixelMap = std::shared_ptr<Media::PixelMap>(pixelMapPtr.release());
92 }
93 if (errorCode != 0 || (badgePixelMap == nullptr)) {
94 APP_LOGE("get badge failed, errorCode:%{public}u", errorCode);
95 return false;
96 }
97 return true;
98 }
99 #endif
100 }
101
BundleResourceParser()102 BundleResourceParser::BundleResourceParser()
103 {
104 }
105
~BundleResourceParser()106 BundleResourceParser::~BundleResourceParser()
107 {
108 }
109
ParseResourceInfo(const int32_t userId,ResourceInfo & resourceInfo)110 bool BundleResourceParser::ParseResourceInfo(const int32_t userId, ResourceInfo &resourceInfo)
111 {
112 return ParseResourceInfoWithSameHap(userId, resourceInfo);
113 }
114
ParseResourceInfos(const int32_t userId,std::vector<ResourceInfo> & resourceInfos)115 bool BundleResourceParser::ParseResourceInfos(const int32_t userId, std::vector<ResourceInfo> &resourceInfos)
116 {
117 APP_LOGD("start");
118 if (resourceInfos.empty()) {
119 APP_LOGE("resourceInfos is empty");
120 return false;
121 }
122 // same module need parse together
123 std::map<std::string, std::shared_ptr<Global::Resource::ResourceManager>> resourceManagerMap;
124 size_t size = resourceInfos.size();
125 for (size_t index = 0; index < size; ++index) {
126 if (!resourceInfos[index].iconNeedParse_ && !resourceInfos[index].labelNeedParse_) {
127 APP_LOGI("%{public}s no need parse", resourceInfos[index].bundleName_.c_str());
128 continue;
129 }
130 if ((index > 0) && !IsNeedToParseResourceInfo(resourceInfos[index], resourceInfos[0])) {
131 continue;
132 }
133 auto resourceManager = resourceManagerMap[resourceInfos[index].moduleName_];
134 if (resourceManager == nullptr) {
135 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
136 if (resConfig == nullptr) {
137 APP_LOGE("resConfig is nullptr");
138 continue;
139 }
140 resourceManager =
141 std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager(
142 resourceInfos[index].bundleName_, resourceInfos[index].moduleName_,
143 resourceInfos[index].hapPath_, resourceInfos[index].overlayHapPaths_, *resConfig, 0, userId));
144 (void)BundleResourceConfiguration::InitResourceGlobalConfig(
145 resourceInfos[index].hapPath_, resourceInfos[index].overlayHapPaths_, resourceManager,
146 resourceInfos[index].iconNeedParse_, resourceInfos[index].labelNeedParse_);
147 resourceManagerMap[resourceInfos[index].moduleName_] = resourceManager;
148 }
149
150 if (!ParseResourceInfoByResourceManager(resourceManager, resourceInfos[index])) {
151 APP_LOGW_NOFUNC("ParseResourceInfo fail key:%{public}s", resourceInfos[index].GetKey().c_str());
152 }
153 }
154 if ((resourceInfos[0].labelNeedParse_ && resourceInfos[0].label_.empty()) ||
155 (resourceInfos[0].iconNeedParse_ && resourceInfos[0].icon_.empty())) {
156 APP_LOGE_NOFUNC("ParseResourceInfos fail -n %{public}s -m %{public}s",
157 resourceInfos[0].bundleName_.c_str(), resourceInfos[0].moduleName_.c_str());
158 return false;
159 }
160 APP_LOGD("end");
161 return true;
162 }
163
IsNeedToParseResourceInfo(const ResourceInfo & newResourceInfo,const ResourceInfo & oldResourceInfo)164 bool BundleResourceParser::IsNeedToParseResourceInfo(
165 const ResourceInfo &newResourceInfo, const ResourceInfo &oldResourceInfo)
166 {
167 if (ServiceConstants::ALLOW_MULTI_ICON_BUNDLE.find(newResourceInfo.bundleName_) !=
168 ServiceConstants::ALLOW_MULTI_ICON_BUNDLE.end()) {
169 return true;
170 }
171 // same labelId and iconId no need to parse again
172 if (newResourceInfo.moduleName_ == oldResourceInfo.moduleName_) {
173 if ((newResourceInfo.labelId_ == oldResourceInfo.labelId_) &&
174 (newResourceInfo.iconId_ == oldResourceInfo.iconId_)) {
175 return false;
176 }
177 }
178 if ((newResourceInfo.labelId_ <= 0) && (newResourceInfo.iconId_ <= 0)) {
179 // no need to process icon and label
180 APP_LOGW("key:%{public}s label and icon both equal 0", newResourceInfo.GetKey().c_str());
181 return false;
182 }
183 return true;
184 }
185
ParseResourceInfoWithSameHap(const int32_t userId,ResourceInfo & resourceInfo)186 bool BundleResourceParser::ParseResourceInfoWithSameHap(const int32_t userId, ResourceInfo &resourceInfo)
187 {
188 if (resourceInfo.hapPath_.empty()) {
189 APP_LOGE("resourceInfo.hapPath_ is empty");
190 return false;
191 }
192 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
193 if (resConfig == nullptr) {
194 APP_LOGE("resConfig is nullptr");
195 return false;
196 }
197 std::shared_ptr<Global::Resource::ResourceManager> resourceManager =
198 std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager(
199 resourceInfo.bundleName_, resourceInfo.moduleName_,
200 resourceInfo.hapPath_, resourceInfo.overlayHapPaths_, *resConfig, 0, userId));
201 if (resourceManager == nullptr) {
202 APP_LOGE("resourceManager is nullptr");
203 return false;
204 }
205 if (!BundleResourceConfiguration::InitResourceGlobalConfig(resourceInfo.hapPath_, resourceManager)) {
206 APP_LOGE("InitResourceGlobalConfig failed, key:%{public}s", resourceInfo.GetKey().c_str());
207 return false;
208 }
209 if (!ParseResourceInfoByResourceManager(resourceManager, resourceInfo)) {
210 APP_LOGE_NOFUNC("ParseResourceInfo fail key:%{public}s", resourceInfo.GetKey().c_str());
211 return false;
212 }
213 return true;
214 }
215
ParseLabelResourceByPath(const std::string & hapPath,const uint32_t labelId,std::string & label)216 bool BundleResourceParser::ParseLabelResourceByPath(
217 const std::string &hapPath, const uint32_t labelId, std::string &label)
218 {
219 if (hapPath.empty()) {
220 APP_LOGE("hapPath is empty");
221 return false;
222 }
223 // allow label resource parse failed, then label is bundleName
224 if (labelId <= 0) {
225 APP_LOGW("labelId is 0");
226 return true;
227 }
228 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
229 if (resourceManager == nullptr) {
230 APP_LOGE("resourceManager is nullptr");
231 return false;
232 }
233 if (!BundleResourceConfiguration::InitResourceGlobalConfig(hapPath, resourceManager)) {
234 APP_LOGE("InitResourceGlobalConfig failed, key:%{private}s", hapPath.c_str());
235 return false;
236 }
237 if (!ParseLabelResourceByResourceManager(resourceManager, labelId, label)) {
238 APP_LOGE("ParseLabelResource fail label %{public}d", labelId);
239 return false;
240 }
241 return true;
242 }
243
ParseIconResourceByPath(const std::string & hapPath,const uint32_t iconId,ResourceInfo & resourceInfo)244 bool BundleResourceParser::ParseIconResourceByPath(const std::string &hapPath, const uint32_t iconId,
245 ResourceInfo &resourceInfo)
246 {
247 if (hapPath.empty()) {
248 APP_LOGE("hapPath is empty");
249 return false;
250 }
251 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
252 if (resourceManager == nullptr) {
253 APP_LOGE("resourceManager is nullptr");
254 return false;
255 }
256 if (!BundleResourceConfiguration::InitResourceGlobalConfig(hapPath, resourceManager)) {
257 APP_LOGE("InitResourceGlobalConfig failed, hapPath:%{private}s", hapPath.c_str());
258 return false;
259 }
260 resourceInfo.iconId_ = iconId;
261 if (!ParseIconResourceByResourceManager(resourceManager, resourceInfo)) {
262 APP_LOGE("failed, iconId %{public}d", iconId);
263 return false;
264 }
265 return true;
266 }
267
ParseResourceInfoByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,ResourceInfo & resourceInfo,bool usingThemeResource)268 bool BundleResourceParser::ParseResourceInfoByResourceManager(
269 const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
270 ResourceInfo &resourceInfo, bool usingThemeResource)
271 {
272 if (resourceManager == nullptr) {
273 APP_LOGE("resourceManager is nullptr");
274 return false;
275 }
276 bool ans = true;
277 if (resourceInfo.labelNeedParse_ && !ParseLabelResourceByResourceManager(
278 resourceManager, resourceInfo.labelId_, resourceInfo.label_)) {
279 APP_LOGE_NOFUNC("ParseLabelResource fail key %{public}s", resourceInfo.GetKey().c_str());
280 ans = false;
281 }
282
283 if (resourceInfo.iconNeedParse_ && !ParseIconResourceByResourceManager(resourceManager, resourceInfo,
284 usingThemeResource)) {
285 APP_LOGE_NOFUNC("ParseIconResource fail key %{public}s", resourceInfo.GetKey().c_str());
286 ans = false;
287 }
288
289 return ans;
290 }
291
ParseLabelResourceByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const uint32_t labelId,std::string & label)292 bool BundleResourceParser::ParseLabelResourceByResourceManager(
293 const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
294 const uint32_t labelId, std::string &label)
295 {
296 if (resourceManager == nullptr) {
297 APP_LOGE("resourceManager is nullptr");
298 return false;
299 }
300 if (labelId <= 0) {
301 APP_LOGW_NOFUNC("ParseLabelResource labelId invalid label is bundleName");
302 return false;
303 }
304 auto ret = resourceManager->GetStringById(labelId, label);
305 if (ret != OHOS::Global::Resource::RState::SUCCESS) {
306 APP_LOGE("GetStringById failed %{public}d, labelId %{public}d",
307 static_cast<int32_t>(ret), labelId);
308 return false;
309 }
310 return true;
311 }
312
ParseIconResourceByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,ResourceInfo & resourceInfo,bool usingThemeResource)313 bool BundleResourceParser::ParseIconResourceByResourceManager(
314 const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
315 ResourceInfo &resourceInfo, bool usingThemeResource)
316 {
317 if (resourceManager == nullptr) {
318 APP_LOGE("resourceManager is nullptr");
319 return false;
320 }
321 if (resourceInfo.iconId_ <= 0) {
322 APP_LOGE_NOFUNC("iconId is 0 or less than 0");
323 return false;
324 }
325 if (usingThemeResource) {
326 // Firstly, check if the bundle theme resource exists, density 0
327 BundleResourceDrawable drawable;
328 if (drawable.GetIconResourceByTheme(resourceInfo.iconId_, 0, resourceManager, resourceInfo) &&
329 !resourceInfo.foreground_.empty()) {
330 return true;
331 }
332 APP_LOGI_NOFUNC("%{public}s not exist theme", resourceInfo.GetKey().c_str());
333 }
334 // parse json
335 std::string type;
336 size_t len;
337 std::unique_ptr<uint8_t[]> jsonBuf;
338 Global::Resource::RState state = resourceManager->GetDrawableInfoById(resourceInfo.iconId_, type, len, jsonBuf, 0);
339 if (state != Global::Resource::SUCCESS) {
340 APP_LOGE("%{public}s failed to get id:%{public}d, err:%{public}d", resourceInfo.bundleName_.c_str(),
341 resourceInfo.iconId_, state);
342 return false;
343 }
344 transform(type.begin(), type.end(), type.begin(), ::tolower);
345 if (type == TYPE_PNG) {
346 resourceInfo.foreground_.resize(len);
347 for (size_t index = 0; index < len; ++index) {
348 resourceInfo.foreground_[index] = jsonBuf[index];
349 }
350 BundleResourceImageInfo bundleResourceImageInfo;
351 // encode base64
352 return bundleResourceImageInfo.ConvertToBase64(std::move(jsonBuf), len, resourceInfo.icon_);
353 }
354 APP_LOGI_NOFUNC("%{public}s icon is not png, parse by drawable descriptor", resourceInfo.GetKey().c_str());
355 BundleResourceDrawable drawable;
356 if (!drawable.GetIconResourceByHap(resourceInfo.iconId_, 0, resourceManager, resourceInfo)) {
357 APP_LOGE("key:%{public}s parse failed with hap iconId:%{public}d",
358 resourceInfo.GetKey().c_str(), resourceInfo.iconId_);
359 return false;
360 }
361 if (type == TYPE_JSON) {
362 return ParseForegroundAndBackgroundResource(resourceManager,
363 std::string(reinterpret_cast<char*>(jsonBuf.get()), len), 0, resourceInfo);
364 } else {
365 resourceInfo.foreground_.resize(len);
366 for (size_t index = 0; index < len; ++index) {
367 resourceInfo.foreground_[index] = jsonBuf[index];
368 }
369 }
370 return true;
371 }
372
ParseIconIdFromJson(const std::string & jsonBuff,uint32_t & foregroundId,uint32_t & backgroundId)373 bool BundleResourceParser::ParseIconIdFromJson(
374 const std::string &jsonBuff, uint32_t &foregroundId, uint32_t &backgroundId)
375 {
376 nlohmann::json jsonObject = nlohmann::json::parse(jsonBuff, nullptr, false);
377 if (jsonObject.is_discarded()) {
378 APP_LOGE("failed to parse jsonBuff %{public}s", jsonBuff.c_str());
379 return false;
380 }
381 const auto &jsonObjectStart = jsonObject.begin();
382 if ((jsonObjectStart == jsonObject.end()) || !jsonObjectStart.value().is_object()) {
383 APP_LOGE("not object, failed to parse jsonBuff %{public}s", jsonBuff.c_str());
384 return false;
385 }
386 LayeredImage layerImage = jsonObjectStart.value().get<LayeredImage>();
387 if (layerImage.foreground.empty() && layerImage.background.empty()) {
388 APP_LOGE("foreground background empty, buffer %{public}s", jsonBuff.c_str());
389 return false;
390 }
391 auto pos = layerImage.foreground.find(CHAR_COLON);
392 if (pos != std::string::npos) {
393 int32_t foregroundLength = static_cast<int32_t>(layerImage.foreground.length());
394 foregroundId = static_cast<uint32_t>(
395 atoi(layerImage.foreground.substr(pos + 1, foregroundLength - pos - 1).c_str()));
396 }
397 pos = layerImage.background.find(CHAR_COLON);
398 if (pos != std::string::npos) {
399 int32_t backgroundLength = static_cast<int32_t>(layerImage.background.length());
400 backgroundId = static_cast<uint32_t>(atoi(layerImage.background.substr(pos + 1,
401 backgroundLength - pos - 1).c_str()));
402 }
403 APP_LOGD("succeed, foregroundId:%{public}u, backgroundId:%{public}u", foregroundId, backgroundId);
404 return true;
405 }
406
GetMediaDataById(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const uint32_t iconId,const int32_t density,std::vector<uint8_t> & data)407 bool BundleResourceParser::GetMediaDataById(
408 const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
409 const uint32_t iconId, const int32_t density, std::vector<uint8_t> &data)
410 {
411 if (resourceManager == nullptr) {
412 APP_LOGE("resourceManager is nullptr");
413 return false;
414 }
415 std::string type;
416 size_t len;
417 std::unique_ptr<uint8_t[]> jsonBuf;
418 Global::Resource::RState state = resourceManager->GetDrawableInfoById(iconId, type, len, jsonBuf, density);
419 if (state != Global::Resource::SUCCESS) {
420 APP_LOGE("Failed get drawable info, iconId %{public}u", iconId);
421 return false;
422 }
423 data.resize(len);
424 for (size_t index = 0; index < len; ++index) {
425 data[index] = jsonBuf[index];
426 }
427 return true;
428 }
429
ParseForegroundAndBackgroundResource(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const std::string & jsonBuff,const int32_t density,ResourceInfo & resourceInfo)430 bool BundleResourceParser::ParseForegroundAndBackgroundResource(
431 const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
432 const std::string &jsonBuff,
433 const int32_t density,
434 ResourceInfo &resourceInfo)
435 {
436 APP_LOGI_NOFUNC("key:%{public}s start parse layered-image", resourceInfo.GetKey().c_str());
437 if (resourceManager == nullptr) {
438 APP_LOGE("resourceManager is nullptr");
439 return false;
440 }
441 uint32_t foregroundId = 0;
442 uint32_t backgroundId = 0;
443 if (!ParseIconIdFromJson(jsonBuff, foregroundId, backgroundId)) {
444 APP_LOGE("parse from json failed, iconId:%{public}d,buffer:%{public}s", resourceInfo.iconId_, jsonBuff.c_str());
445 return false;
446 }
447 // parse foreground
448 bool ans = true;
449 if (!GetMediaDataById(resourceManager, foregroundId, density, resourceInfo.foreground_)) {
450 APP_LOGE("parse foreground failed iconId %{public}u", foregroundId);
451 ans = false;
452 }
453 // parse background
454 if (!GetMediaDataById(resourceManager, backgroundId, density, resourceInfo.background_)) {
455 APP_LOGE("parse background failed iconId:%{public}u", backgroundId);
456 ans = false;
457 }
458 APP_LOGD("foreground size:%{public}zu background size:%{public}zu",
459 resourceInfo.foreground_.size(), resourceInfo.background_.size());
460 return ans;
461 }
462
ParserCloneResourceInfo(const int32_t appIndex,std::vector<ResourceInfo> & resourceInfos)463 bool BundleResourceParser::ParserCloneResourceInfo(
464 const int32_t appIndex, std::vector<ResourceInfo> &resourceInfos)
465 {
466 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
467 // 1. get badge resource media
468 std::string resourceName = OHOS_CLONE_APP_BADGE_RESOURCE + std::to_string(appIndex);
469 APP_LOGI("parse clone info appIndex:%{public}d resourceName:%{public}s start", appIndex, resourceName.c_str());
470 std::shared_ptr<Media::PixelMap> badgePixelMap;
471 if (!GetBadgeResource(resourceName, badgePixelMap) || (badgePixelMap == nullptr)) {
472 APP_LOGE("resourceName:%{public}s get failed", resourceName.c_str());
473 return false;
474 }
475 bool ans = true;
476 // 2. base64 to pixelMap
477 for (auto &resourceInfo : resourceInfos) {
478 uint32_t errorCode = 0;
479 Media::SourceOptions opts;
480 std::unique_ptr<Media::ImageSource> imageSource =
481 Media::ImageSource::CreateImageSource(resourceInfo.icon_, opts, errorCode); // base64 to image
482 Media::DecodeOptions decodeOpts;
483 decodeOpts.desiredPixelFormat = Media::PixelFormat::BGRA_8888;
484 std::shared_ptr<Media::PixelMap> baseIconResource;
485 if (imageSource) {
486 auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode);
487 baseIconResource = std::shared_ptr<Media::PixelMap>(pixelMapPtr.release());
488 }
489 if ((errorCode != 0) || (baseIconResource == nullptr)) {
490 APP_LOGW("get base icon resource failed, key:%{public}s", resourceInfo.GetKey().c_str());
491 ans = false;
492 continue;
493 }
494 // base icon and badge icon resource
495 BundleResourceDrawable drawable;
496 if (!drawable.GetBadgedIconResource(baseIconResource, badgePixelMap, resourceInfo)) {
497 APP_LOGE("get badge failed, key:%{public}s", resourceInfo.GetKey().c_str());
498 ans = false;
499 }
500 }
501 APP_LOGI("parse clone resource info appIndex:%{public}d end", appIndex);
502 return ans;
503 #else
504 APP_LOGI("not support pixel map");
505 return false;
506 #endif
507 }
508
ParseResourceInfosNoTheme(const int32_t userId,std::vector<ResourceInfo> & resourceInfos)509 bool BundleResourceParser::ParseResourceInfosNoTheme(
510 const int32_t userId, std::vector<ResourceInfo> &resourceInfos)
511 {
512 APP_LOGD("start");
513 if (resourceInfos.empty()) {
514 APP_LOGE("resourceInfos is empty");
515 return false;
516 }
517 // same module need parse together
518 std::map<std::string, std::shared_ptr<Global::Resource::ResourceManager>> resourceManagerMap;
519 size_t size = resourceInfos.size();
520 for (size_t index = 0; index < size; ++index) {
521 if (!resourceInfos[index].iconNeedParse_ && !resourceInfos[index].labelNeedParse_) {
522 APP_LOGI("%{public}s no need parse", resourceInfos[index].bundleName_.c_str());
523 continue;
524 }
525 if ((index > 0) && !IsNeedToParseResourceInfo(resourceInfos[index], resourceInfos[0])) {
526 continue;
527 }
528 auto resourceManager = resourceManagerMap[resourceInfos[index].moduleName_];
529 if (resourceManager == nullptr) {
530 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
531 if (resConfig == nullptr) {
532 APP_LOGE("resConfig is nullptr");
533 continue;
534 }
535 resourceManager =
536 std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager(
537 resourceInfos[index].bundleName_, resourceInfos[index].moduleName_,
538 resourceInfos[index].hapPath_, resourceInfos[index].overlayHapPaths_, *resConfig, 0, userId));
539 resourceManagerMap[resourceInfos[index].moduleName_] = resourceManager;
540 if (!BundleResourceConfiguration::InitResourceGlobalConfig(
541 resourceInfos[index].hapPath_, resourceInfos[index].overlayHapPaths_, resourceManager,
542 resourceInfos[index].iconNeedParse_, resourceInfos[index].labelNeedParse_)) {
543 APP_LOGW("InitResourceGlobalConfig failed, key:%{public}s", resourceInfos[index].GetKey().c_str());
544 }
545 }
546
547 if (!ParseResourceInfoByResourceManager(resourceManager, resourceInfos[index], false)) {
548 APP_LOGW_NOFUNC("ParseResourceInfo fail key:%{public}s", resourceInfos[index].GetKey().c_str());
549 }
550 }
551 if ((resourceInfos[0].labelNeedParse_ && resourceInfos[0].label_.empty()) ||
552 (resourceInfos[0].iconNeedParse_ && resourceInfos[0].icon_.empty())) {
553 APP_LOGE_NOFUNC("ParseResourceInfos fail -n %{public}s -m %{public}s",
554 resourceInfos[0].bundleName_.c_str(), resourceInfos[0].moduleName_.c_str());
555 return false;
556 }
557 APP_LOGD("end");
558 return true;
559 }
560
ParseIconResourceInfosWithTheme(const int32_t userId,std::vector<ResourceInfo> & resourceInfos)561 bool BundleResourceParser::ParseIconResourceInfosWithTheme(
562 const int32_t userId, std::vector<ResourceInfo> &resourceInfos)
563 {
564 APP_LOGD("start");
565 if (resourceInfos.empty()) {
566 APP_LOGE("resourceInfos is empty");
567 return false;
568 }
569 // same module need parse together
570 std::map<std::string, std::shared_ptr<Global::Resource::ResourceManager>> resourceManagerMap;
571 size_t size = resourceInfos.size();
572 for (size_t index = 0; index < size; ++index) {
573 auto resourceManager = resourceManagerMap[resourceInfos[index].moduleName_];
574 if (resourceManager == nullptr) {
575 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
576 if (resConfig == nullptr) {
577 APP_LOGE("resConfig is nullptr");
578 continue;
579 }
580 resourceManager =
581 std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager(
582 resourceInfos[index].bundleName_, resourceInfos[index].moduleName_,
583 resourceInfos[index].hapPath_, resourceInfos[index].overlayHapPaths_, *resConfig, 0, userId));
584 resourceManagerMap[resourceInfos[index].moduleName_] = resourceManager;
585 // no add path when theme exist
586 if (!BundleResourceConfiguration::InitResourceGlobalConfig(resourceManager)) {
587 APP_LOGW("InitResourceGlobalConfig failed, key:%{public}s", resourceInfos[index].GetKey().c_str());
588 }
589 }
590
591 if (!ParseIconResourceByResourceManager(resourceManager, resourceInfos[index])) {
592 APP_LOGW_NOFUNC("ParseResourceInfo fail key:%{public}s", resourceInfos[index].GetKey().c_str());
593 }
594 }
595 if (resourceInfos[0].icon_.empty()) {
596 APP_LOGE_NOFUNC("ParseResourceInfos fail -n %{public}s -m %{public}s",
597 resourceInfos[0].bundleName_.c_str(), resourceInfos[0].moduleName_.c_str());
598 return false;
599 }
600 APP_LOGD("end");
601 return true;
602 }
603 } // AppExecFwk
604 } // OHOS