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