• 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 "app_manager_utils.h"
17 #ifdef HAS_ABILITY_RUNTIME_PART
18 #include "bundle_mgr_interface.h"
19 #include "system_ability_definition.h"
20 #endif
21 
22 #include "power_log.h"
23 #include <app_mgr_interface.h>
24 #include "app_mgr_proxy.h"
25 #include <if_system_ability_manager.h>
26 #include <iservice_registry.h>
27 
28 namespace OHOS {
29 namespace PowerMgr {
30 static constexpr uint32_t APP_MGR_SERVICE_ID = 501;
31 sptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::appManagerInstance_ = nullptr;
32 namespace {
33 const int32_t API_VERSION_MOD = 1000;
34 }
35 
GetAppManagerInstance()36 sptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::GetAppManagerInstance()
37 {
38     if (appManagerInstance_) {
39         return appManagerInstance_;
40     }
41 
42     sptr<OHOS::ISystemAbilityManager> abilityMgr =
43         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44     if (abilityMgr == nullptr) {
45         POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager");
46         return nullptr;
47     }
48     sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
49     if (remoteObject == nullptr) {
50         POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID);
51         return nullptr;
52     }
53     sptr<OHOS::AppExecFwk::IAppMgr> appMgrProxy = iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
54     if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) {
55         POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager proxy");
56         return nullptr;
57     }
58     appManagerInstance_ = appMgrProxy;
59     return appManagerInstance_;
60 }
61 
GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData> & appsData)62 void AppManagerUtils::GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData)
63 {
64     auto appMgr = GetAppManagerInstance();
65     if (!appMgr) {
66         return;
67     }
68     int32_t ret = appMgr->GetForegroundApplications(appsData);
69     POWER_HILOGI(
70         FEATURE_UTIL, "GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size());
71 }
72 
IsForegroundApplication(const std::string & appName)73 bool AppManagerUtils::IsForegroundApplication(const std::string& appName)
74 {
75     if (appName.empty()) {
76         POWER_HILOGW(FEATURE_UTIL, "IsForegroundApplication: app name is empty");
77         return false;
78     }
79 
80     bool IsForeground = false;
81     std::vector<OHOS::AppExecFwk::AppStateData> appsData;
82     GetForegroundApplications(appsData);
83     for (const auto& curApp : appsData) {
84         if (curApp.bundleName == appName) {
85             IsForeground = true;
86             break;
87         }
88     }
89     POWER_HILOGI(FEATURE_UTIL, "IsForegroundApplication, ret: %{public}u", static_cast<uint32_t>(IsForeground));
90     return IsForeground;
91 }
92 
GetApiTargetVersion()93 int32_t AppManagerUtils::GetApiTargetVersion()
94 {
95 #ifdef HAS_ABILITY_RUNTIME_PART
96     static int32_t apiTargetVersion = -1;
97     if (apiTargetVersion != -1) {
98         return apiTargetVersion;
99     }
100     sptr<OHOS::ISystemAbilityManager> saManager =
101         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102     if (saManager == nullptr) {
103         POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager");
104         return 0;
105     }
106     sptr<OHOS::IRemoteObject> remoteObject = saManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
107     sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgrProxy = iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
108     if (bundleMgrProxy == nullptr) {
109         POWER_HILOGE(FEATURE_UTIL, "GetApiTargetVersion: bundleMgrProxy is nullptr");
110         return 0;
111     }
112     OHOS::AppExecFwk::BundleInfo bundleInfo;
113     auto ret =
114         bundleMgrProxy->GetBundleInfoForSelf(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo);
115     if (ret != 0) {
116         POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: GetBundleInfoForSelf failed");
117         return 0;
118     }
119     int32_t hapApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD;
120     apiTargetVersion = hapApiVersion;
121     POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: hapApiVersion is %{public}d", hapApiVersion);
122     return hapApiVersion;
123 #else
124     POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion not support");
125     return -1;
126 #endif
127 }
128 
129 } // namespace PowerMgr
130 } // namespace OHOS