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 #include "app_mgr_constants.h"
19 #include "app_mgr_helper.h"
20 #include "standby_service_impl.h"
21 #include "standby_service_log.h"
22
23 namespace OHOS {
24 namespace DevStandbyMgr {
AppStateObserver(const std::shared_ptr<AppExecFwk::EventHandler> & handler)25 AppStateObserver::AppStateObserver(const std::shared_ptr<AppExecFwk::EventHandler>& handler): handler_(handler) {}
26
OnProcessDied(const AppExecFwk::ProcessData & processData)27 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
28 {
29 STANDBYSERVICE_LOGD("process died, uid : %{public}d, pid : %{public}d", processData.uid, processData.pid);
30 if (!this->CheckAlivedApp(processData.bundleName)) {
31 auto uid = processData.uid;
32 auto bundleName = processData.bundleName;
33 handler_->PostTask([uid, bundleName]() {
34 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(uid, bundleName, false);
35 });
36 }
37 handler_->PostTask([uid = processData.uid, pid = processData.pid, bundleName = processData.bundleName]() {
38 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(uid, pid, bundleName, false);
39 });
40 }
41
CheckAlivedApp(const std::string & bundleName)42 bool AppStateObserver::CheckAlivedApp(const std::string &bundleName)
43 {
44 STANDBYSERVICE_LOGD("start check app alive or not");
45 bool isRunning {true};
46 if (!AppMgrHelper::GetInstance()->GetAppRunningStateByBundleName(bundleName, isRunning)) {
47 STANDBYSERVICE_LOGW("connect to app mgr service failed");
48 return true;
49 }
50 return isRunning;
51 }
52
OnProcessCreated(const AppExecFwk::ProcessData & processData)53 void AppStateObserver::OnProcessCreated(const AppExecFwk::ProcessData &processData)
54 {
55 handler_->PostTask([uid = processData.uid, pid = processData.pid, bundleName = processData.bundleName]() {
56 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(uid, pid, bundleName, true);
57 });
58 }
59
OnApplicationStateChanged(const AppExecFwk::AppStateData & appStateData)60 void AppStateObserver::OnApplicationStateChanged(const AppExecFwk::AppStateData &appStateData)
61 {
62 if (!(appStateData.uid > 0 && appStateData.bundleName.size() > 0)) {
63 STANDBYSERVICE_LOGD("%{public}s : validate app state data failed!", __func__);
64 return;
65 }
66 auto uid = appStateData.uid;
67 auto bundleName = appStateData.bundleName;
68 auto state = appStateData.state;
69 STANDBYSERVICE_LOGD("app is terminated, uid: %{public}d, bunddlename: %{public}s", uid, bundleName.c_str());
70 if (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED) || state ==
71 static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END)) {
72 handler_->PostTask([uid, bundleName]() {
73 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(uid, bundleName, false);
74 });
75 }
76 }
77
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)78 void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData)
79 {
80 if (!(appStateData.uid > 0 && appStateData.bundleName.size() > 0)) {
81 STANDBYSERVICE_LOGD("%{public}s : validate app state data failed!", __func__);
82 return;
83 }
84 auto pid = appStateData.pid;
85 auto bundleName = appStateData.bundleName;
86 auto state = appStateData.state;
87 auto isFocused = appStateData.isFocused;
88 STANDBYSERVICE_LOGD("fg app changed, state: %{public}d, bunddlename: %{public}s", state, bundleName.c_str());
89 if (state == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) && isFocused) {
90 handler_->PostTask([pid, bundleName]() {
91 StandbyMessage message(StandbyMessageType::FG_APPLICATION_CHANGED);
92 message.want_ = AAFwk::Want{};
93 message.want_->SetParam("cur_foreground_app_pid", pid);
94 message.want_->SetParam("cur_foreground_app_name", bundleName);
95 StandbyServiceImpl::GetInstance()->DispatchEvent(message);
96 });
97 }
98 }
99
OnPageShow(const AppExecFwk::PageStateData & pageStateData)100 void AppStateObserver::OnPageShow(const AppExecFwk::PageStateData &pageStateData)
101 {
102 std::string bundleName = pageStateData.bundleName;
103 std::string moduleName = pageStateData.moduleName;
104 std::string abilityName = pageStateData.abilityName;
105 std::string pageName = pageStateData.pageName;
106 std::string targetBundleName = pageStateData.targetBundleName;
107 std::string targetModuleName = pageStateData.targetModuleName;
108 STANDBYSERVICE_LOGD("PageStateData: page show, pageName: %{public}s, bundlename: %{public}s",
109 pageName.c_str(),
110 bundleName.c_str());
111 handler_->PostTask([bundleName, moduleName, abilityName, pageName, targetBundleName, targetModuleName]() {
112 StandbyMessage message(StandbyMessageType::PAGE_SHOW);
113 message.want_ = AAFwk::Want{};
114 message.want_->SetParam("bundleName", bundleName);
115 message.want_->SetParam("moduleName", moduleName);
116 message.want_->SetParam("abilityName", abilityName);
117 message.want_->SetParam("pageName", pageName);
118 message.want_->SetParam("targetBundleName", targetBundleName);
119 message.want_->SetParam("targetModuleName", targetModuleName);
120 StandbyServiceImpl::GetInstance()->DispatchEvent(message);
121 });
122 }
123
OnPageHide(const AppExecFwk::PageStateData & pageStateData)124 void AppStateObserver::OnPageHide(const AppExecFwk::PageStateData &pageStateData)
125 {
126 std::string bundleName = pageStateData.bundleName;
127 std::string moduleName = pageStateData.moduleName;
128 std::string abilityName = pageStateData.abilityName;
129 std::string pageName = pageStateData.pageName;
130 std::string targetBundleName = pageStateData.targetBundleName;
131 std::string targetModuleName = pageStateData.targetModuleName;
132 STANDBYSERVICE_LOGD("PageStateData: page hide, pageName: %{public}s, bundlename: %{public}s",
133 pageName.c_str(),
134 bundleName.c_str());
135 handler_->PostTask([bundleName, moduleName, abilityName, pageName, targetBundleName, targetModuleName]() {
136 StandbyMessage message(StandbyMessageType::PAGE_HIDE);
137 message.want_ = AAFwk::Want{};
138 message.want_->SetParam("bundleName", bundleName);
139 message.want_->SetParam("moduleName", moduleName);
140 message.want_->SetParam("abilityName", abilityName);
141 message.want_->SetParam("pageName", pageName);
142 message.want_->SetParam("targetBundleName", targetBundleName);
143 message.want_->SetParam("targetModuleName", targetModuleName);
144 StandbyServiceImpl::GetInstance()->DispatchEvent(message);
145 });
146 }
147 } // namespace DevStandbyMgr
148 } // namespace OHOS