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 
16 #include "app_state_observer.h"
17 
18 #undef MMI_LOG_DOMAIN
19 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "AppStateObserver"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 std::mutex mutex_;
27 } // namespace
AppObserverManager()28 AppObserverManager::AppObserverManager() {}
~AppObserverManager()29 AppObserverManager::~AppObserverManager() {}
30 
OnProcessStateChanged(const AppExecFwk::ProcessData & processData)31 void ApplicationStateObserver::OnProcessStateChanged(const AppExecFwk::ProcessData &processData)
32 {
33     CALL_DEBUG_ENTER;
34     std::lock_guard<std::mutex> guard(mutex_);
35     MMI_HILOGD("Process state change app name:%{public}s, uid:%{public}d, state:%{public}d",
36         processData.bundleName.c_str(),
37         processData.uid,
38         processData.state);
39     std::vector<AppExecFwk::AppStateData> list {};
40     GetForegroundApplicationInfo(list);
41 }
42 
GetAppMgr()43 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> ApplicationStateObserver::GetAppMgr()
44 {
45     if (appManager_) {
46         return appManager_;
47     }
48 
49     OHOS::sptr<ISystemAbilityManager> systemAbilityManager =
50         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51     if (!systemAbilityManager) {
52         MMI_HILOGE("Get system ability manager failed");
53         return nullptr;
54     }
55     OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->CheckSystemAbility(OHOS::APP_MGR_SERVICE_ID);
56     appManager_ = OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(object);
57     return appManager_;
58 }
59 
GetForegroundApplicationInfo(std::vector<AppExecFwk::AppStateData> & list)60 int32_t ApplicationStateObserver::GetForegroundApplicationInfo(std::vector<AppExecFwk::AppStateData> &list)
61 {
62     CALL_DEBUG_ENTER;
63     auto appMgr = GetAppMgr();
64     CHKPR(appMgr, RET_ERR);
65     int32_t ret = appMgr->GetForegroundApplications(list);
66     if (ret == RET_OK) {
67         MMI_HILOGD("GetForegroundApplications success");
68         APP_OBSERVER_MGR->SetForegroundAppData(list);
69     }
70     return ret;
71 }
72 
GetForegroundAppData()73 std::vector<AppExecFwk::AppStateData> AppObserverManager::GetForegroundAppData()
74 {
75     CALL_DEBUG_ENTER;
76     std::lock_guard<std::mutex> guard(mutex_);
77     MMI_HILOGD("foregroundAppData_.size():%{public}zu", foregroundAppData_.size());
78     return foregroundAppData_;
79 }
80 
SetForegroundAppData(std::vector<AppExecFwk::AppStateData> list)81 void AppObserverManager::SetForegroundAppData(std::vector<AppExecFwk::AppStateData> list)
82 {
83     CALL_DEBUG_ENTER;
84     foregroundAppData_ = list;
85     MMI_HILOGD("foregroundAppData_.size():%{public}zu", foregroundAppData_.size());
86 }
87 
InitAppStateObserver()88 void AppObserverManager::InitAppStateObserver()
89 {
90     CALL_DEBUG_ENTER;
91     if (hasInit_) {
92         MMI_HILOGI("App state observer has init");
93         return;
94     }
95     OHOS::sptr<ISystemAbilityManager> systemAbilityManager =
96         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97     if (!systemAbilityManager) {
98         MMI_HILOGE("Get system ability manager failed");
99         return;
100     }
101     OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->CheckSystemAbility(OHOS::APP_MGR_SERVICE_ID);
102     CHKPV(object);
103     sptr<AppExecFwk::IAppMgr> appMgr = OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(object);
104     CHKPV(appMgr);
105     int32_t ret = appMgr->RegisterApplicationStateObserver(new ApplicationStateObserver());
106     if (ret == RET_OK) {
107         hasInit_ = true;
108         MMI_HILOGI("Register app success");
109     }
110 }
111 } // namespace MMI
112 } // namespace OHOS