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 HIVIEW_PLUGINS_USAGE_EVENT_REPORT_INCLUDE_USAGE_EVENT_REPORT_H
17 #define HIVIEW_PLUGINS_USAGE_EVENT_REPORT_INCLUDE_USAGE_EVENT_REPORT_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "event_loop.h"
23 #include "ffrt.h"
24 #include "fold_app_usage_event_factory.h"
25 #include "fold_event_cacher.h"
26 #include "plugin.h"
27 #include "shutdown/iasync_shutdown_callback.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 class UsageEventReport : public Plugin {
32 public:
33     UsageEventReport();
~UsageEventReport()34     ~UsageEventReport() {}
35     void OnLoad() override;
36     void OnUnload() override;
37     bool OnEvent(std::shared_ptr<Event>& event) override;
38     bool IsRunning();
39     void TimeOut();
40     static void SaveEventToDb();
41 
42 private:
43     void Init();
44     void InitCallback();
45     void Start();
46     void Stop();
47     void RunTask();
48     void ReportDailyEvent();
49     void ReportFoldAppUsageEvent();
50     void ReportTimeOutEvent();
51     void ReportSysUsageEvent();
52     void DeletePluginStatsEvents();
53     static void StartServiceByOption(const std::string& opt);
54     static void SavePluginStatsEvents();
55     static void SaveSysUsageEvent();
56     void InitFoldEventCacher(const std::string& workPath);
57 
58 private:
59     sptr<PowerMgr::IAsyncShutdownCallback> callback_;
60     uint64_t timeOutCnt_;
61     ffrt::mutex runningMutex_;
62     bool isRunning_;
63     static uint64_t lastSysReportTime_;
64     static uint64_t lastReportTime_;
65     static uint64_t nextReportTime_;
66     static std::string workPath_;
67     std::unique_ptr<FoldEventCacher> foldEventCacher_;
68     std::unique_ptr<FoldAppUsageEventFactory> foldAppUsageFactory_;
69 }; // UsageEventReport
70 } // namespace HiviewDFX
71 } // namespace OHOS
72 #endif // HIVIEW_PLUGINS_USAGE_EVENT_REPORT_INCLUDE_USAGE_EVENT_REPORT_H
73