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 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_LISTENER_H 17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_LISTENER_H 18 19 #ifdef STANDBY_SERVICE_UNIT_TEST 20 #define WEAK_FUNC __attribute__((weak)) 21 #else 22 #define WEAK_FUNC 23 #endif // STANDBY_SERVICE_UNIT_TEST 24 25 #include <memory> 26 #include <vector> 27 28 #include "background_task_mgr_helper.h" 29 #include "background_task_subscriber.h" 30 #include "imessage_listener.h" 31 #include "ipc_skeleton.h" 32 #include "iremote_object.h" 33 #include "istate_manager_adapter.h" 34 #include "resource_callback_info.h" 35 #include "single_instance.h" 36 #include "standby_service_errors.h" 37 38 namespace OHOS { 39 namespace DevStandbyMgr { 40 using namespace OHOS::BackgroundTaskMgr; 41 class BackgroundTaskListener : public std::enable_shared_from_this<BackgroundTaskListener>, public IMesssageListener { 42 public: 43 BackgroundTaskListener(); 44 ~BackgroundTaskListener() override = default; 45 46 /** 47 * @brief Register background task observer. 48 */ 49 ErrCode StartListener() override; 50 51 /** 52 * @brief Unregister background task observer. 53 */ 54 ErrCode StopListener() override; 55 private: 56 class BgTaskListenerImpl : public OHOS::BackgroundTaskMgr::BackgroundTaskSubscriber { 57 public: 58 BgTaskListenerImpl(); 59 60 /** 61 * Called back when a continuous task start. 62 * 63 * @param info Transient task app info. 64 */ 65 virtual void OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info) override; 66 67 /** 68 * Called back when the app does not have transient task. 69 * 70 * @param info App info transient task . 71 */ 72 virtual void OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info) override; 73 74 /** 75 * Called back when a continuous task start. 76 * 77 * @param info Continuous task app info. 78 */ 79 virtual void OnContinuousTaskStart( 80 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 81 82 /** 83 * Called back when a continuous task end. 84 * 85 * @param info Continuous task info. 86 */ 87 virtual void OnContinuousTaskStop( 88 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo) override; 89 90 private: 91 /** 92 * @brief dispatch BG_TASK_STATUS_CHANGE event when background task started or stopped 93 */ 94 void OnTaskStatusChanged(const std::string& type, bool started, int32_t uid, int32_t pid, 95 const std::string& bundleName); 96 private: 97 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 98 }; 99 100 private: 101 std::unique_ptr<BackgroundTaskSubscriber> bgTaskListenerImpl_ {nullptr}; 102 }; 103 } 104 } 105 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_BACKGROUND_TASK_LISTENER_H