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