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 "background_task_listener.h"
17 #include "standby_service_impl.h"
18 #include "standby_service_log.h"
19 
20 namespace OHOS {
21 namespace DevStandbyMgr {
BackgroundTaskListener()22 BackgroundTaskListener::BackgroundTaskListener()
23 {
24     bgTaskListenerImpl_ = std::make_unique<BgTaskListenerImpl>();
25 }
26 
StartListener()27 ErrCode BackgroundTaskListener::StartListener()
28 {
29     if (!bgTaskListenerImpl_) {
30         STANDBYSERVICE_LOGE("backgroundTaskListener is nullptr");
31         return ERR_BGTASK_LISTENER_NULL;
32     }
33     if (BackgroundTaskMgrHelper::SubscribeBackgroundTask(*bgTaskListenerImpl_) != OHOS::ERR_OK) {
34         STANDBYSERVICE_LOGE("SubscribeBackgroundTask failed");
35         return ERR_REGISTER_BACKGROUND_TASK_FAILED;
36     }
37     STANDBYSERVICE_LOGI("backgroundTaskListener start listener");
38     return ERR_OK;
39 }
40 
StopListener()41 ErrCode BackgroundTaskListener::StopListener()
42 {
43     if (!bgTaskListenerImpl_) {
44         STANDBYSERVICE_LOGE("backgroundTaskListener is nullptr");
45         return ERR_BGTASK_LISTENER_NULL;
46     }
47     if (BackgroundTaskMgrHelper::UnsubscribeBackgroundTask(*bgTaskListenerImpl_) != OHOS::ERR_OK) {
48         STANDBYSERVICE_LOGE("UnsubscribeBackgroundTask failed");
49         return ERR_UNREGISTER_BACKGROUND_TASK_FAILED;
50     }
51     STANDBYSERVICE_LOGI("backgroundTaskListener stop listener");
52     return ERR_OK;
53 }
54 
BgTaskListenerImpl()55 BackgroundTaskListener::BgTaskListenerImpl::BgTaskListenerImpl()
56 {
57     handler_ = StandbyServiceImpl::GetInstance()->GetHandler();
58 }
59 
OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)60 void BackgroundTaskListener::BgTaskListenerImpl::OnAppTransientTaskStart(const
61     std::shared_ptr<TransientTaskAppInfo>& info)
62 {
63     STANDBYSERVICE_LOGD("Transient start called, uid is %{public}d, bundle name is %{public}s",
64         info->GetUid(), info->GetPackageName().c_str());
65     OnTaskStatusChanged(TRANSIENT_TASK, true, info->GetUid(), info->GetPid(), info->GetPackageName());
66 }
67 
OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)68 void BackgroundTaskListener::BgTaskListenerImpl::OnAppTransientTaskEnd(const
69     std::shared_ptr<TransientTaskAppInfo>& info)
70 {
71     STANDBYSERVICE_LOGD("Transient stop called, uid is %{public}d, bundle name is %{public}s",
72         info->GetUid(), info->GetPackageName().c_str());
73     OnTaskStatusChanged(TRANSIENT_TASK, false, info->GetUid(), info->GetPid(), info->GetPackageName());
74 }
75 
OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)76 void BackgroundTaskListener::BgTaskListenerImpl::OnContinuousTaskStart(
77     const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
78 {
79     STANDBYSERVICE_LOGD("Continuous start called, uid is %{public}d", continuousTaskCallbackInfo->GetCreatorUid());
80     OnTaskStatusChanged(CONTINUOUS_TASK, true, continuousTaskCallbackInfo->GetCreatorUid(),
81         continuousTaskCallbackInfo->GetCreatorPid(), "");
82 }
83 
OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)84 void BackgroundTaskListener::BgTaskListenerImpl::OnContinuousTaskStop(
85     const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
86 {
87     STANDBYSERVICE_LOGD("Continuous stop called, uid is %{public}d", continuousTaskCallbackInfo->GetCreatorUid());
88     OnTaskStatusChanged(CONTINUOUS_TASK, false, continuousTaskCallbackInfo->GetCreatorUid(),
89         continuousTaskCallbackInfo->GetCreatorPid(), "");
90 }
91 
OnTaskStatusChanged(const std::string & type,bool started,int32_t uid,int32_t pid,const std::string & bundleName)92 void BackgroundTaskListener::BgTaskListenerImpl::OnTaskStatusChanged(const std::string& type, bool started,
93     int32_t uid, int32_t pid, const std::string& bundleName)
94 {
95     StandbyMessage standbyMessage {StandbyMessageType::BG_TASK_STATUS_CHANGE};
96     standbyMessage.want_ = AAFwk::Want {};
97     standbyMessage.want_->SetParam(BG_TASK_TYPE, type);
98     standbyMessage.want_->SetParam(BG_TASK_STATUS, started);
99     standbyMessage.want_->SetParam(BG_TASK_UID, uid);
100     standbyMessage.want_->SetParam(BG_TASK_BUNDLE_NAME, bundleName);
101     StandbyServiceImpl::GetInstance()->DispatchEvent(standbyMessage);
102 }
103 } // OHOS
104 } // DevStandbyMgr