1 /*
2 * Copyright (c) 2022 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_graphics_client_impl.h"
17
18 #include <unistd.h>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_constants.h"
22 #include "bundle_file_util.h"
23 #include "bundle_mgr_interface.h"
24 #include "bundle_mgr_proxy.h"
25 #include "image_source.h"
26 #include "if_system_ability_manager.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
GetBundleMgr()32 sptr<IBundleMgr> BundleGraphicsClientImpl::GetBundleMgr()
33 {
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (bundleMgr_ == nullptr) {
36 auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (systemAbilityManager == nullptr) {
38 APP_LOGE("GetBundleMgr GetSystemAbilityManager is null");
39 return nullptr;
40 }
41 auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
42 if (bundleMgrSa == nullptr) {
43 APP_LOGE("GetBundleMgr GetSystemAbility is null");
44 return nullptr;
45 }
46 auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
47 if (bundleMgr == nullptr) {
48 APP_LOGE("GetBundleMgr iface_cast get null");
49 }
50 bundleMgr_ = bundleMgr;
51 }
52
53 return bundleMgr_;
54 }
55
LoadImageFile(const uint8_t * data,size_t len)56 std::shared_ptr<Media::PixelMap> BundleGraphicsClientImpl::LoadImageFile(const uint8_t *data, size_t len)
57 {
58 APP_LOGI("begin");
59 uint32_t errorCode = 0;
60 Media::SourceOptions opts;
61 std::unique_ptr<Media::ImageSource> imageSource = Media::ImageSource::CreateImageSource(data, len, opts, errorCode);
62 if ((errorCode != 0) || (imageSource == nullptr)) {
63 APP_LOGE("create image source failed err %{public}d", errorCode);
64 return nullptr;
65 }
66
67 Media::DecodeOptions decodeOpts;
68 auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode);
69 if (errorCode != 0) {
70 APP_LOGE("create pixelmap failed err %{public}d", errorCode);
71 return nullptr;
72 }
73 APP_LOGI("end");
74 return std::shared_ptr<Media::PixelMap>(std::move(pixelMapPtr));
75 }
76
GetAbilityPixelMapIcon(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::shared_ptr<Media::PixelMap> & pixelMap)77 ErrCode BundleGraphicsClientImpl::GetAbilityPixelMapIcon(const std::string &bundleName,
78 const std::string &moduleName, const std::string &abilityName, std::shared_ptr<Media::PixelMap> &pixelMap)
79 {
80 APP_LOGI("begin");
81 auto iBundleMgr = GetBundleMgr();
82 if (iBundleMgr == nullptr) {
83 APP_LOGE("can not get iBundleMgr");
84 return ERR_APPEXECFWK_SERVICE_NOT_READY;
85 }
86 std::unique_ptr<uint8_t[]> mediaDataPtr = nullptr;
87 size_t len = 0;
88 ErrCode ret = iBundleMgr->GetMediaData(bundleName, moduleName, abilityName, mediaDataPtr, len);
89 if (ret != ERR_OK) {
90 APP_LOGE("get media data failed");
91 return ret;
92 }
93 if (mediaDataPtr == nullptr || len == 0) {
94 return ERR_APPEXECFWK_PARCEL_ERROR;
95 }
96 auto pixelMapPtr = LoadImageFile(mediaDataPtr.get(), len);
97 if (pixelMapPtr == nullptr) {
98 APP_LOGE("loadImageFile failed");
99 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
100 }
101 pixelMap = std::move(pixelMapPtr);
102 return ERR_OK;
103 }
104 } // AppExecFwk
105 } // OHOS