• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_resource_drawable_utils.h"
17 
18 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
19 #include "drawable_descriptor.h"
20 #include "js_drawable_descriptor.h"
21 #include "resource_manager.h"
22 #endif
23 #include "parameters.h"
24 namespace OHOS {
25 namespace AppExecFwk {
26 constexpr const char* DRAWABLE_ICON_SIZE = "const.bms.drawableIconSize";
27 constexpr int32_t DECODE_SIZE = 0;
28 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
29 std::shared_ptr<Global::Resource::ResourceManager> BundleResourceDrawableUtils::resourceManager_ = nullptr;
30 std::mutex BundleResourceDrawableUtils::resMutex_;
31 
InitResourceManager()32 void BundleResourceDrawableUtils::InitResourceManager()
33 {
34     std::lock_guard<std::mutex> lock(resMutex_);
35     if (resourceManager_ == nullptr) {
36         std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
37         if (resConfig == nullptr) {
38             return;
39         }
40         resourceManager_ =
41             std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager(
42                 "bundleName", "moduleName", "", std::vector<std::string>(), *resConfig));
43     }
44 }
45 #endif
46 
ConvertToDrawableDescriptor(napi_env env,const std::vector<uint8_t> & foreground,const std::vector<uint8_t> & background)47 napi_value BundleResourceDrawableUtils::ConvertToDrawableDescriptor(napi_env env,
48     const std::vector<uint8_t> &foreground, const std::vector<uint8_t> &background)
49 {
50 #ifdef BUNDLE_FRAMEWORK_GRAPHICS
51     if (foreground.empty() && background.empty()) {
52         return nullptr;
53     }
54     InitResourceManager();
55     size_t lenForeground = foreground.size();
56     std::unique_ptr<uint8_t[]> foregroundPtr = std::make_unique<uint8_t[]>(lenForeground);
57     for (size_t index = 0; index < lenForeground; ++index) {
58         foregroundPtr[index] = foreground[index];
59     }
60     int32_t decodeSize = OHOS::system::GetIntParameter(DRAWABLE_ICON_SIZE, DECODE_SIZE);
61     if (background.empty()) {
62         // base-icon
63         std::unique_ptr<Ace::Napi::DrawableDescriptor> drawableDescriptor =
64             std::make_unique<Ace::Napi::DrawableDescriptor>(std::move(foregroundPtr), lenForeground);
65         drawableDescriptor->SetDecodeSize(decodeSize, decodeSize);
66         return Ace::Napi::JsDrawableDescriptor::ToNapi(env, drawableDescriptor.release(),
67             Ace::Napi::DrawableDescriptor::DrawableType::BASE);
68     }
69     // layered-icon
70     size_t lenBackground = background.size();
71     std::unique_ptr<uint8_t[]> backgroundPtr = std::make_unique<uint8_t[]>(lenBackground);
72     for (size_t index = 0; index < lenBackground; ++index) {
73         backgroundPtr[index] = background[index];
74     }
75     std::unique_ptr<uint8_t[]> jsonBuf;
76     std::string themeMask = (resourceManager_ == nullptr) ? "" : resourceManager_->GetThemeMask();
77 
78     std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundPair;
79     foregroundPair.first = std::move(foregroundPtr);
80     foregroundPair.second = lenForeground;
81     std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundPair;
82     backgroundPair.first = std::move(backgroundPtr);
83     backgroundPair.second = lenBackground;
84     std::unique_ptr<Ace::Napi::DrawableDescriptor> drawableDescriptor =
85         std::make_unique<Ace::Napi::LayeredDrawableDescriptor>(std::move(jsonBuf), 0, resourceManager_, themeMask, 1,
86         foregroundPair, backgroundPair);
87     drawableDescriptor->SetDecodeSize(decodeSize, decodeSize);
88     return Ace::Napi::JsDrawableDescriptor::ToNapi(env, drawableDescriptor.release(),
89         Ace::Napi::DrawableDescriptor::DrawableType::LAYERED);
90 #else
91     return nullptr;
92 #endif
93 }
94 }
95 }
96