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