15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "app_manager_utils.h" 175ccb8f90Sopenharmony_ci 185ccb8f90Sopenharmony_ci#include "power_log.h" 195ccb8f90Sopenharmony_ci#include <app_mgr_interface.h> 205ccb8f90Sopenharmony_ci#include <if_system_ability_manager.h> 215ccb8f90Sopenharmony_ci#include <iservice_registry.h> 225ccb8f90Sopenharmony_ci 235ccb8f90Sopenharmony_cinamespace OHOS { 245ccb8f90Sopenharmony_cinamespace PowerMgr { 255ccb8f90Sopenharmony_cistatic constexpr uint32_t APP_MGR_SERVICE_ID = 501; 265ccb8f90Sopenharmony_cisptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::appManagerInstance_ = nullptr; 275ccb8f90Sopenharmony_ci 285ccb8f90Sopenharmony_cisptr<OHOS::AppExecFwk::IAppMgr> AppManagerUtils::GetAppManagerInstance() 295ccb8f90Sopenharmony_ci{ 305ccb8f90Sopenharmony_ci if (appManagerInstance_) { 315ccb8f90Sopenharmony_ci return appManagerInstance_; 325ccb8f90Sopenharmony_ci } 335ccb8f90Sopenharmony_ci 345ccb8f90Sopenharmony_ci sptr<OHOS::ISystemAbilityManager> abilityMgr = 355ccb8f90Sopenharmony_ci OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 365ccb8f90Sopenharmony_ci if (abilityMgr == nullptr) { 375ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager"); 385ccb8f90Sopenharmony_ci return nullptr; 395ccb8f90Sopenharmony_ci } 405ccb8f90Sopenharmony_ci sptr<IRemoteObject> remoteObject = abilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID); 415ccb8f90Sopenharmony_ci if (remoteObject == nullptr) { 425ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager service, id=%{public}u", APP_MGR_SERVICE_ID); 435ccb8f90Sopenharmony_ci return nullptr; 445ccb8f90Sopenharmony_ci } 455ccb8f90Sopenharmony_ci sptr<OHOS::AppExecFwk::IAppMgr> appMgrProxy = iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject); 465ccb8f90Sopenharmony_ci if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) { 475ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_UTIL, "Failed to get app manager proxy"); 485ccb8f90Sopenharmony_ci return nullptr; 495ccb8f90Sopenharmony_ci } 505ccb8f90Sopenharmony_ci appManagerInstance_ = appMgrProxy; 515ccb8f90Sopenharmony_ci return appManagerInstance_; 525ccb8f90Sopenharmony_ci} 535ccb8f90Sopenharmony_ci 545ccb8f90Sopenharmony_civoid AppManagerUtils::GetForegroundApplications(std::vector<OHOS::AppExecFwk::AppStateData>& appsData) 555ccb8f90Sopenharmony_ci{ 565ccb8f90Sopenharmony_ci auto appMgr = GetAppManagerInstance(); 575ccb8f90Sopenharmony_ci if (!appMgr) { 585ccb8f90Sopenharmony_ci return; 595ccb8f90Sopenharmony_ci } 605ccb8f90Sopenharmony_ci int32_t ret = appMgr->GetForegroundApplications(appsData); 615ccb8f90Sopenharmony_ci POWER_HILOGI( 625ccb8f90Sopenharmony_ci FEATURE_UTIL, "GetForegroundApplications, ret: %{public}u, num of apps: %{public}zu", ret, appsData.size()); 635ccb8f90Sopenharmony_ci} 645ccb8f90Sopenharmony_ci 655ccb8f90Sopenharmony_cibool AppManagerUtils::IsForegroundApplication(const std::string& appName) 665ccb8f90Sopenharmony_ci{ 675ccb8f90Sopenharmony_ci if (appName.empty()) { 685ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_UTIL, "IsForegroundApplication: app name is empty"); 695ccb8f90Sopenharmony_ci return false; 705ccb8f90Sopenharmony_ci } 715ccb8f90Sopenharmony_ci 725ccb8f90Sopenharmony_ci bool IsForeground = false; 735ccb8f90Sopenharmony_ci std::vector<OHOS::AppExecFwk::AppStateData> appsData; 745ccb8f90Sopenharmony_ci GetForegroundApplications(appsData); 755ccb8f90Sopenharmony_ci for (const auto& curApp : appsData) { 765ccb8f90Sopenharmony_ci if (curApp.bundleName == appName) { 775ccb8f90Sopenharmony_ci IsForeground = true; 785ccb8f90Sopenharmony_ci break; 795ccb8f90Sopenharmony_ci } 805ccb8f90Sopenharmony_ci } 815ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_UTIL, "IsForegroundApplication, ret: %{public}u", static_cast<uint32_t>(IsForeground)); 825ccb8f90Sopenharmony_ci return IsForeground; 835ccb8f90Sopenharmony_ci} 845ccb8f90Sopenharmony_ci 855ccb8f90Sopenharmony_ci} // namespace PowerMgr 865ccb8f90Sopenharmony_ci} // namespace OHOS