1 /* 2 * Copyright (c) 2023 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 #include "app_mgr_helper.h" 16 17 #include "iservice_registry.h" 18 #include "system_ability_definition.h" 19 #include "standby_service_log.h" 20 #include "standby_service_errors.h" 21 22 namespace OHOS { 23 namespace DevStandbyMgr { AppMgrHelper()24AppMgrHelper::AppMgrHelper() {} 25 ~AppMgrHelper()26AppMgrHelper::~AppMgrHelper() {} 27 GetInstance()28std::shared_ptr<AppMgrHelper> AppMgrHelper::GetInstance() 29 { 30 return DelayedSingleton<AppMgrHelper>::GetInstance(); 31 } 32 GetAllRunningProcesses(std::vector<AppExecFwk::RunningProcessInfo> & allAppProcessInfos)33bool WEAK_FUNC AppMgrHelper::GetAllRunningProcesses(std::vector<AppExecFwk::RunningProcessInfo>& allAppProcessInfos) 34 { 35 std::lock_guard<std::mutex> lock(connectMutex_); 36 if (!Connect()) { 37 return false; 38 } 39 if (appMgrProxy_->GetAllRunningProcesses(allAppProcessInfos) != ERR_OK) { 40 return false; 41 } 42 return true; 43 } 44 GetForegroundApplications(std::vector<AppExecFwk::AppStateData> & fgApps)45bool WEAK_FUNC AppMgrHelper::GetForegroundApplications(std::vector<AppExecFwk::AppStateData> &fgApps) 46 { 47 std::lock_guard<std::mutex> lock(connectMutex_); 48 if (!Connect()) { 49 return false; 50 } 51 if (appMgrProxy_->GetForegroundApplications(fgApps) != ERR_OK) { 52 return false; 53 } 54 return true; 55 } 56 GetAppRunningStateByBundleName(const std::string & bundleName,bool & isRunning)57bool WEAK_FUNC AppMgrHelper::GetAppRunningStateByBundleName(const std::string &bundleName, bool& isRunning) 58 { 59 std::lock_guard<std::mutex> lock(connectMutex_); 60 if (!Connect()) { 61 return false; 62 } 63 isRunning = appMgrProxy_->GetAppRunningStateByBundleName(bundleName); 64 return true; 65 } 66 SubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> & observer)67bool WEAK_FUNC AppMgrHelper::SubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> &observer) 68 { 69 std::lock_guard<std::mutex> lock(connectMutex_); 70 if (!Connect()) { 71 return false; 72 } 73 if (appMgrProxy_->RegisterApplicationStateObserver(observer) != ERR_OK) { 74 return false; 75 } 76 return true; 77 } 78 UnsubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> & observer)79bool WEAK_FUNC AppMgrHelper::UnsubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> &observer) 80 { 81 std::lock_guard<std::mutex> lock(connectMutex_); 82 if (!Connect()) { 83 return false; 84 } 85 if (appMgrProxy_->UnregisterApplicationStateObserver(observer) != ERR_OK) { 86 return false; 87 } 88 return true; 89 } 90 Connect()91bool WEAK_FUNC AppMgrHelper::Connect() 92 { 93 if (appMgrProxy_ != nullptr) { 94 return true; 95 } 96 97 sptr<ISystemAbilityManager> systemAbilityManager = 98 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 99 if (systemAbilityManager == nullptr) { 100 STANDBYSERVICE_LOGE("failed to get SystemAbilityManager"); 101 return false; 102 } 103 104 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID); 105 if (remoteObject == nullptr) { 106 STANDBYSERVICE_LOGE("failed to get App Manager Service"); 107 return false; 108 } 109 110 appMgrProxy_ = iface_cast<AppExecFwk::IAppMgr>(remoteObject); 111 if (!appMgrProxy_ || !appMgrProxy_->AsObject()) { 112 STANDBYSERVICE_LOGE("failed to get app mgr proxy"); 113 return false; 114 } 115 return true; 116 } 117 } // namespace DevStandbyMgr 118 } // namespace OHOS