1 /* 2 * Copyright (c) 2022-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 #ifndef HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H 16 #define HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H 17 18 #include <memory> 19 #include <mutex> 20 #include <unordered_map> 21 22 #include "app_event_observer.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 class AppEventHandler; 27 class OsEventListener; 28 namespace HiAppEvent { 29 class AppStateCallback; 30 } 31 using HiAppEvent::AppEventObserver; 32 using HiAppEvent::AppStateCallback; 33 using HiAppEvent::ReportConfig; 34 35 class AppEventObserverMgr { 36 public: 37 AppEventObserverMgr(); 38 ~AppEventObserverMgr(); 39 static AppEventObserverMgr& GetInstance(); 40 41 int64_t RegisterObserver(std::shared_ptr<AppEventObserver> observer); 42 int64_t RegisterObserver(const std::string& observerName, const ReportConfig& config = {}); 43 int UnregisterObserver(int64_t observerSeq); 44 int UnregisterObserver(const std::string& observerName); 45 void HandleEvents(std::vector<std::shared_ptr<AppEventPack>>& events); 46 void HandleTimeout(); 47 void HandleBackground(); 48 void HandleClearUp(); 49 50 int SetReportConfig(int64_t observerSeq, const ReportConfig& config); 51 int GetReportConfig(int64_t observerSeq, ReportConfig& config); 52 53 private: 54 void CreateEventHandler(); 55 void DestroyEventHandler(); 56 void RegisterAppStateCallback(); 57 void UnregisterAppStateCallback(); 58 int64_t InitObserver(std::shared_ptr<AppEventObserver> observer); 59 bool InitObserverFromListener(std::shared_ptr<AppEventObserver> observer, bool sendFlag); 60 void UnregisterOsEventListener(); 61 62 private: 63 std::unordered_map<int64_t, std::shared_ptr<AppEventObserver>> observers_; 64 std::unordered_map<int64_t, ReportConfig> configs_; 65 std::shared_ptr<AppEventHandler> handler_; 66 std::shared_ptr<AppStateCallback> appStateCallback_; 67 std::mutex observerMutex_; 68 std::shared_ptr<OsEventListener> listener_; 69 70 private: 71 static std::mutex instanceMutex_; 72 }; 73 } // namespace HiviewDFX 74 } // namespace OHOS 75 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H 76