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