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