1 /*
2  * Copyright (c) 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_SREVICE_FOLD_EVENT_CACHER_H
17 #define HIVIEW_PLUGINS_USAGE_EVENT_REPORT_SREVICE_FOLD_EVENT_CACHER_H
18 
19 #include <memory>
20 #include <map>
21 #include <vector>
22 
23 #include "app_mgr_interface.h"
24 #include "fold_app_usage_db_helper.h"
25 #include "sys_event.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class FoldEventCacher {
30 public:
31     FoldEventCacher(const std::string& workPath);
~FoldEventCacher()32     ~FoldEventCacher() {}
33 
34     void ProcessEvent(std::shared_ptr<SysEvent> event);
35     void TimeOut();
36 private:
37     void ProcessForegroundEvent(std::shared_ptr<SysEvent> event, AppEventRecord& appEventRecord);
38     void ProcessBackgroundEvent(std::shared_ptr<SysEvent> event, AppEventRecord& appEventRecord);
39     void ProcessSceenStatusChangedEvent(std::shared_ptr<SysEvent> event);
40     void CountLifeCycleDuration(AppEventRecord& appEventRecord);
41     void UpdateFoldStatus(int status);
42     void UpdateVhMode(int mode);
43     int GetStartIndex(const std::string& bundleName);
44     void CalCulateDuration(uint64_t dayStartTime, std::vector<AppEventRecord>& events,
45         std::map<int, uint64_t>& durations);
46     bool CanCalcDuration(uint32_t preId, uint32_t id);
47     void Accumulative(int foldStatus, uint64_t duration, std::map<int, uint64_t>& durations);
48     int64_t GetFoldStatusDuration(const int foldStatus, std::map<int, uint64_t>& durations);
49     void ProcessCountDurationEvent(AppEventRecord& appEventRecord, std::map<int, uint64_t>& durations);
50 
51 private:
52     std::unique_ptr<FoldAppUsageDbHelper> dbHelper_;
53     std::map<std::string, std::string> foregroundApps_;
54     int foldStatus_ = 0;
55     int vhMode_ = 0;
56     uint64_t timelyStart_ = 0;
57 };
58 } // namespace HiviewDFX
59 } // namespace OHOS
60 #endif // HIVIEW_PLUGINS_USAGE_EVENT_REPORT_SREVICE_EVENT_CACHER_H
61