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 }  // namespace DevStandbyMgr
100 }  // namespace OHOS