1 /* 2 * Copyright (c) 2022-2024 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 BUNDLE_ACTIVE_GROUP_HANDLER_H 17 #define BUNDLE_ACTIVE_GROUP_HANDLER_H 18 19 #include "ffrt.h" 20 #include <map> 21 #include <queue> 22 #include "ibundle_active_service.h" 23 #include "bundle_active_group_controller.h" 24 #include "bundle_active_group_common.h" 25 26 namespace OHOS { 27 namespace DeviceUsageStats { 28 class BundleActiveGroupHandlerObject { 29 public: 30 std::string bundleName_; 31 int32_t userId_; 32 int32_t uid_; 33 BundleActiveGroupHandlerObject(); 34 BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject& orig); ~BundleActiveGroupHandlerObject()35 ~BundleActiveGroupHandlerObject() {} 36 }; 37 38 class BundleActiveGroupHandler { 39 public: 40 explicit BundleActiveGroupHandler(const bool debug); 41 ~BundleActiveGroupHandler() = default; 42 /** 43 * Process the event. Developers should override this method. 44 * 45 * @param event The event should be processed. 46 */ 47 void ProcessEvent(const int32_t& eventId, const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj); 48 void SendEvent(const int32_t& eventId, const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, 49 const int64_t& delayTime = 0); 50 void SendCheckBundleMsg(const int32_t& eventId, const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, 51 const int64_t& delayTime = 0); 52 std::string GetMsgKey(const int32_t& eventId, const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, 53 const int64_t& delayTime); 54 void RemoveEvent(const int32_t& eventId); 55 void RemoveCheckBundleMsg(const std::string& msgKey); 56 void PostSyncTask(const std::function<void()>& fuc); 57 void PostTask(const std::function<void()>& fuc); 58 void Init(); 59 void DeInit(); 60 static const int32_t MSG_CHECK_DEFAULT_BUNDLE_STATE; 61 static const int32_t MSG_ONE_TIME_CHECK_BUNDLE_STATE; 62 static const int32_t MSG_CHECK_IDLE_STATE; 63 static const int32_t MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE; 64 static const int32_t MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE; 65 int64_t checkIdleInterval_; 66 67 private: 68 bool isInited_ = false; 69 ffrt::mutex taskHandlerMutex_; 70 ffrt::mutex checkBundleTaskMutex_; 71 std::shared_ptr<ffrt::queue> ffrtQueue_; 72 std::map<int32_t, std::queue<ffrt::task_handle>> taskHandlerMap_; 73 std::map<std::string, ffrt::task_handle> checkBundleTaskMap_; 74 }; 75 } // namespace DeviceUsageStats 76 } // namespace OHOS 77 #endif // BUNDLE_ACTIVE_GROUP_HANDLER_H 78 79