• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (bundleMgr_ == nullptr) {
35         auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36         if (systemAbilityManager == nullptr) {
37             APP_LOGE("GetBundleMgr GetSystemAbilityManager is null");
38             return nullptr;
39         }
40         auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
41         if (bundleMgrSa == nullptr) {
42             APP_LOGE("GetBundleMgr GetSystemAbility is null");
43             return nullptr;
44         }
45         auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
46         if (bundleMgr == nullptr) {
47             APP_LOGE("GetBundleMgr iface_cast get null");
48         }
49         bundleMgr_ = bundleMgr;
50     }
51 
52     return bundleMgr_;
53 }
54 
LoadImageFile(const uint8_t * data,size_t len)55 std::shared_ptr<Media::PixelMap> BundleGraphicsClientImpl::LoadImageFile(const uint8_t *data, size_t len)
56 {
57     APP_LOGI("begin LoadImageFile");
58     uint32_t errorCode = 0;
59     Media::SourceOptions opts;
60     std::unique_ptr<Media::ImageSource> imageSource = Media::ImageSource::CreateImageSource(data, len, opts, errorCode);
61     if ((errorCode != 0) || (imageSource == nullptr)) {
62         APP_LOGE("failed to create image source err is %{public}d", errorCode);
63         return nullptr;
64     }
65 
66     Media::DecodeOptions decodeOpts;
67     auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode);
68     if (errorCode != 0) {
69         APP_LOGE("failed to create pixelmap err %{public}d", errorCode);
70         return nullptr;
71     }
72     APP_LOGI("LoadImageFile finish");
73     return std::shared_ptr<Media::PixelMap>(std::move(pixelMapPtr));
74 }
75 
GetAbilityPixelMapIcon(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::shared_ptr<Media::PixelMap> & pixelMap)76 ErrCode BundleGraphicsClientImpl::GetAbilityPixelMapIcon(const std::string &bundleName,
77     const std::string &moduleName, const std::string &abilityName, std::shared_ptr<Media::PixelMap> &pixelMap)
78 {
79     APP_LOGI("begin GetAbilityPixelMapIcon");
80     auto iBundleMgr = GetBundleMgr();
81     if (iBundleMgr == nullptr) {
82         APP_LOGE("can not get iBundleMgr");
83         return ERR_APPEXECFWK_SERVICE_NOT_READY;
84     }
85     std::unique_ptr<uint8_t[]> mediaDataPtr = nullptr;
86     size_t len = 0;
87     ErrCode ret = iBundleMgr->GetMediaData(bundleName, moduleName, abilityName, mediaDataPtr, len);
88     if (ret != ERR_OK) {
89         APP_LOGE("get media data failed");
90         return ret;
91     }
92     if (mediaDataPtr == nullptr || len == 0) {
93         return ERR_APPEXECFWK_PARCEL_ERROR;
94     }
95     auto pixelMapPtr = LoadImageFile(mediaDataPtr.get(), len);
96     if (pixelMapPtr == nullptr) {
97         APP_LOGE("loadImageFile failed");
98         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
99     }
100     pixelMap = std::move(pixelMapPtr);
101     return ERR_OK;
102 }
103 }  // AppExecFwk
104 }  // OHOS