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