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
18#include "power_log.h"
19#include <app_mgr_interface.h>
20#include <if_system_ability_manager.h>
21#include <iservice_registry.h>
22
23namespace OHOS {
24namespace PowerMgr {
25static constexpr uint32_t APP_MGR_SERVICE_ID = 501;
26sptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::appManagerInstance_ = nullptr;
27
28sptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::GetAppManagerInstance()
29{
30    if (appManagerInstance_) {
31        return appManagerInstance_;
32    }
33
34    sptr<OHOS::ISystemAbilityManager> abilityMgr =
35        OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36    if (abilityMgr == nullptr) {
37        POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager");
38        return nullptr;
39    }
40    sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
41    if (remoteObject == nullptr) {
42        POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID);
43        return nullptr;
44    }
45    sptr<OHOS::AppExecFwk::IAppMgr> appMgrProxy = iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
46    if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) {
47        POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager proxy");
48        return nullptr;
49    }
50    appManagerInstance_ = appMgrProxy;
51    return appManagerInstance_;
52}
53
54void AppManagerUtils::GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData)
55{
56    auto appMgr = GetAppManagerInstance();
57    if (!appMgr) {
58        return;
59    }
60    int32_t ret = appMgr->GetForegroundApplications(appsData);
61    POWER_HILOGI(
62        FEATURE_UTIL, "GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size());
63}
64
65bool AppManagerUtils::IsForegroundApplication(const std::string& appName)
66{
67    if (appName.empty()) {
68        POWER_HILOGW(FEATURE_UTIL, "IsForegroundApplication: app name is empty");
69        return false;
70    }
71
72    bool IsForeground = false;
73    std::vector<OHOS::AppExecFwk::AppStateData> appsData;
74    GetForegroundApplications(appsData);
75    for (const auto& curApp : appsData) {
76        if (curApp.bundleName == appName) {
77            IsForeground = true;
78            break;
79        }
80    }
81    POWER_HILOGI(FEATURE_UTIL, "IsForegroundApplication, ret: %{public}u", static_cast<uint32_t>(IsForeground));
82    return IsForeground;
83}
84
85} // namespace PowerMgr
86} // namespace OHOS