• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "pasteboard_common.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 
21 #include "pasteboard_hilog.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
GetAppBundleManager(void)25 sptr<AppExecFwk::IBundleMgr> PasteBoardCommon::GetAppBundleManager(void)
26 {
27     auto sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28     if (sysAbilityMgr == nullptr) {
29         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, " Failed to get system ability manager.");
30         return nullptr;
31     }
32     auto remoteObject = sysAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
33     if (remoteObject == nullptr) {
34         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, " Failed to get bundle mgr service.");
35         return nullptr;
36     }
37     return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
38 }
39 
GetApiTargetVersionForSelf(void)40 int32_t PasteBoardCommon::GetApiTargetVersionForSelf(void)
41 {
42     if (apiTargetVersion_ > 0) {
43         return apiTargetVersion_;
44     }
45     static constexpr int32_t API_VERSION_MOD = 1000;
46     sptr<AppExecFwk::IBundleMgr> bundleMgrProxy = GetAppBundleManager();
47     if (bundleMgrProxy == nullptr) {
48         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "Failed to get bundle manager proxy.");
49         return 0;
50     }
51 
52     AppExecFwk::BundleInfo bundleInfo;
53     auto flags = AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION;
54     auto ret = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(flags), bundleInfo);
55     if (ret != ERR_OK) {
56         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "GetBundleInfoForSelf: bundleName get failed %{public}d.", ret);
57         return 0;
58     }
59     int32_t targetApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
60     apiTargetVersion_ = targetApiVersion;
61     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_COMMON, "Got target API version %{public}d.", targetApiVersion);
62     return targetApiVersion;
63 }
64 } // namespace MiscServices
65 } // namespace OHOS
66