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 OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_TIMED_COLLECT_H
17 #define OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_TIMED_COLLECT_H
18 
19 #ifdef PREFERENCES_ENABLE
20 #include "device_timed_collect_tool.h"
21 #endif
22 #include "icollect_plugin.h"
23 
24 #include <mutex>
25 #include <set>
26 
27 namespace OHOS {
28 struct TimeInfo {
29     bool normal = false;
30     bool awake = false;
31 };
32 class DeviceTimedCollect : public ICollectPlugin {
33 public:
34     explicit DeviceTimedCollect(const sptr<IReport>& report);
35     ~DeviceTimedCollect() = default;
36 
37     int32_t OnStart() override;
38     int32_t OnStop() override;
39     void Init(const std::list<SaProfile>& saProfiles) override;
40     int32_t AddCollectEvent(const OnDemandEvent& event) override;
41     int32_t RemoveUnusedEvent(const OnDemandEvent& event) override;
42 private:
43     void SaveTimedEvent(const OnDemandEvent& onDemandEvent);
44     void SaveTimedInfos(const OnDemandEvent& onDemandEvent, int32_t interval);
45     void ReportEventByTimeInfo(int32_t interval, bool persistence);
46     void PostDelayTaskByTimeInfo(std::function<void()> callback, int32_t interval, int32_t disTime);
47     int64_t CalculateDelayTime(const std::string& timeString);
48     void PostPersistenceLoopTasks();
49     void PostNonPersistenceLoopTasks();
50 
51     void PostPersistenceLoopTaskLocked(int32_t interval);
52     void PostNonPersistenceLoopTaskLocked(int32_t interval);
53     void PostPersistenceTimedTaskLocked(std::string timeString, int64_t timeGap);
54     void PostNonPersistenceTimedTaskLocked(std::string timeString, int64_t timeGap);
55 
56     void ProcessPersistenceTasks();
57     void ProcessPersistenceLoopTask(int64_t disTime, int64_t triggerTime, std::string strInterval);
58     void ProcessPersistenceTimedTask(int64_t disTime, std::string timeString);
59 
60     void PostPersistenceDelayTask(std::function<void()> loopTask,
61         int32_t interval, int32_t disTime);
62 
63     void RemoveNonPersistenceLoopTask(int32_t interval);
64     void RemovePersistenceLoopTask(int32_t interval);
65     void RemoveTimesInfo(const OnDemandEvent& onDemandEvent, int32_t interval);
66     std::set<int32_t> nonPersitenceLoopEventSet_;
67     std::set<int32_t> persitenceLoopEventSet_;
68     std::mutex nonPersitenceLoopEventSetLock_;
69     std::mutex nonPersitenceTimedEventSetLock;
70     std::mutex persitenceLoopEventSetLock_;
71     std::mutex persitenceTimedEventSetLock_;
72     std::map<int32_t, std::function<void()>> nonPersitenceLoopTasks_;
73     std::map<int32_t, std::function<void()>> persitenceLoopTasks_;
74     std::mutex timeInfosLock_;
75     std::map<int32_t, TimeInfo> timeInfos_;
76 #ifdef PREFERENCES_ENABLE
77     std::shared_ptr<PreferencesUtil> preferencesUtil_;
78 #endif
79 };
80 } // namespace OHOS
81 #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_TIMED_COLLECT_H
82