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_drawable.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "bundle_resource_image_info.h"
20 #include "parameter.h"
21
22 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
23 #include "drawable_descriptor.h"
24 #endif
25
26 namespace OHOS {
27 namespace AppExecFwk {
28
GetIconResourceByTheme(const uint32_t iconId,const int32_t density,std::shared_ptr<Global::Resource::ResourceManager> resourceManager,ResourceInfo & resourceInfo)29 bool BundleResourceDrawable::GetIconResourceByTheme(
30 const uint32_t iconId,
31 const int32_t density,
32 std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
33 ResourceInfo &resourceInfo)
34 {
35 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
36 if (resourceManager == nullptr) {
37 LOG_E(BMS_TAG_DEFAULT, "resourceManager is nullptr");
38 return false;
39 }
40 BundleResourceImageInfo info;
41 OHOS::Ace::Napi::DrawableDescriptor::DrawableType drawableType;
42 std::string themeMask = resourceManager->GetThemeMask();
43 std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
44 std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
45 Global::Resource::RState state = resourceManager->GetThemeIcons(iconId, foregroundInfo, backgroundInfo, density,
46 resourceInfo.abilityName_);
47 if (state == Global::Resource::SUCCESS) {
48 LOG_I(BMS_TAG_DEFAULT, "bundleName:%{public}s find theme resource", resourceInfo.bundleName_.c_str());
49 if (backgroundInfo.second == 0) {
50 return ProcessForegroundIcon(foregroundInfo, resourceInfo);
51 }
52 auto drawableDescriptor = Ace::Napi::DrawableDescriptorFactory::Create(foregroundInfo, backgroundInfo,
53 themeMask, drawableType, resourceManager);
54 if ((drawableDescriptor != nullptr) && (drawableDescriptor->GetPixelMap() != nullptr)) {
55 // init foreground
56 resourceInfo.foreground_.resize(foregroundInfo.second);
57 for (size_t index = 0; index < foregroundInfo.second; ++index) {
58 resourceInfo.foreground_[index] = foregroundInfo.first[index];
59 }
60 // init background
61 resourceInfo.background_.resize(backgroundInfo.second);
62 for (size_t index = 0; index < backgroundInfo.second; ++index) {
63 resourceInfo.background_[index] = backgroundInfo.first[index];
64 }
65 return info.ConvertToString(drawableDescriptor->GetPixelMap(), resourceInfo.icon_);
66 }
67 LOG_W(BMS_TAG_DEFAULT, "bundleName:%{public}s drawableDescriptor or pixelMap is nullptr, need create again",
68 resourceInfo.bundleName_.c_str());
69 }
70 return false;
71 #else
72 return false;
73 #endif
74 }
75
GetIconResourceByHap(const uint32_t iconId,const int32_t density,std::shared_ptr<Global::Resource::ResourceManager> resourceManager,ResourceInfo & resourceInfo)76 bool BundleResourceDrawable::GetIconResourceByHap(
77 const uint32_t iconId,
78 const int32_t density,
79 std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
80 ResourceInfo &resourceInfo)
81 {
82 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
83 if (resourceManager == nullptr) {
84 LOG_E(BMS_TAG_DEFAULT, "resourceManager is nullptr");
85 return false;
86 }
87 BundleResourceImageInfo info;
88 OHOS::Ace::Napi::DrawableDescriptor::DrawableType drawableType;
89 Global::Resource::RState state = Global::Resource::SUCCESS;
90 auto drawableDescriptor = Ace::Napi::DrawableDescriptorFactory::Create(
91 iconId, resourceManager, state, drawableType, density);
92 if ((drawableDescriptor == nullptr) || (state != Global::Resource::SUCCESS)) {
93 LOG_E(BMS_TAG_DEFAULT, "bundleName:%{public}s drawableDescriptor is nullptr, errCode:%{public}d",
94 resourceInfo.bundleName_.c_str(), static_cast<int32_t>(state));
95 return false;
96 }
97 return info.ConvertToString(drawableDescriptor->GetPixelMap(), resourceInfo.icon_);
98 #else
99 return false;
100 #endif
101 }
102
ProcessForegroundIcon(std::pair<std::unique_ptr<uint8_t[]>,size_t> & foregroundInfo,ResourceInfo & resourceInfo)103 bool BundleResourceDrawable::ProcessForegroundIcon(
104 std::pair<std::unique_ptr<uint8_t[]>, size_t> &foregroundInfo,
105 ResourceInfo &resourceInfo)
106 {
107 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
108 LOG_I(BMS_TAG_DEFAULT, "-n %{public}s theme exist, but background not exist", resourceInfo.bundleName_.c_str());
109 // init foreground
110 resourceInfo.foreground_.resize(foregroundInfo.second);
111 for (size_t index = 0; index < foregroundInfo.second; ++index) {
112 resourceInfo.foreground_[index] = foregroundInfo.first[index];
113 }
114 // encode base64
115 BundleResourceImageInfo bundleResourceImageInfo;
116 return bundleResourceImageInfo.ConvertToBase64(std::move(foregroundInfo.first), foregroundInfo.second,
117 resourceInfo.icon_);
118 #else
119 return false;
120 #endif
121 }
122
123 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
GetBadgedIconResource(const std::shared_ptr<Media::PixelMap> layeredPixelMap,const std::shared_ptr<Media::PixelMap> badgedPixelMap,ResourceInfo & resourceInfo)124 bool BundleResourceDrawable::GetBadgedIconResource(
125 const std::shared_ptr<Media::PixelMap> layeredPixelMap,
126 const std::shared_ptr<Media::PixelMap> badgedPixelMap,
127 ResourceInfo &resourceInfo)
128 {
129 if ((layeredPixelMap == nullptr) || (badgedPixelMap == nullptr)) {
130 LOG_E(BMS_TAG_DEFAULT, "bundleName:%{public}s layered or badge pixelMap are nullptr",
131 resourceInfo.bundleName_.c_str());
132 return false;
133 }
134 Ace::Napi::LayeredDrawableDescriptor layeredDrawableDescriptor;
135 std::shared_ptr<Media::PixelMap> compositePixelMap;
136 if (!layeredDrawableDescriptor.GetCompositePixelMapWithBadge(layeredPixelMap, badgedPixelMap, compositePixelMap)) {
137 LOG_E(BMS_TAG_DEFAULT, "bundleName:%{public}s GetCompositePixelMapWithBadge failed",
138 resourceInfo.bundleName_.c_str());
139 return false;
140 }
141 if (compositePixelMap == nullptr) {
142 LOG_E(BMS_TAG_DEFAULT, "bundleName:%{public}s compositePixelMap is nullptr",
143 resourceInfo.bundleName_.c_str());
144 return false;
145 }
146 BundleResourceImageInfo info;
147 return info.ConvertToString(compositePixelMap, resourceInfo.icon_);
148 }
149 #endif
150 } // AppExecFwk
151 } // OHOS
152