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_FOLD_APP_USAGE_DB_HELPER_H
17 #define HIVIEW_PLUGINS_USAGE_EVENT_REPORT_FOLD_APP_USAGE_DB_HELPER_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "rdb_store.h"
24 
25 struct FoldAppUsageInfo {
26     std::string package;
27     std::string version;
28     int32_t foldVer = 0; // usage duration when screen in fold-vertiacal status
29     int32_t foldHor = 0; // usage duration when screen in fold-horizon status
30     int32_t expdVer = 0; // usage duration when screen in expand-vertiacal status
31     int32_t expdHor = 0; // usage duration when screen in expand-horizon status
32     int32_t startNum = 1;
33     std::string date;
34 };
35 
36 struct FoldAppUsageRawEvent {
37     int64_t id = 0;
38     int rawId = 0;
39     std::string package;
40     std::string version;
41     int64_t ts = 0;
42     int64_t happenTime = 0;
43     int screenStatusBefore = 0;
44     int screenStatusAfter = 0;
45 };
46 
47 struct AppEventRecord {
48     int rawid = 0;
49     int64_t ts = 0;
50     std::string bundleName = "";
51     int preFoldStatus = 0;
52     int foldStatus = 0;
53     std::string versionName = "";
54     int64_t happenTime = 0;
55     int64_t foldPortraitTime = 0;
56     int64_t foldLandscapeTime = 0;
57     int64_t expandPortraitTime = 0;
58     int64_t expandLandscapeTime = 0;
59 };
60 
61 namespace OHOS {
62 namespace HiviewDFX {
63 class FoldAppUsageDbHelper {
64 public:
65     FoldAppUsageDbHelper(const std::string& workPath);
66     ~FoldAppUsageDbHelper();
67 
68 public:
69     void QueryStatisticEventsInPeriod(uint64_t startTime, uint64_t endTime,
70         std::unordered_map<std::string, FoldAppUsageInfo> &infos);
71     void QueryForegroundAppsInfo(uint64_t startTime, uint64_t endTime, int screenStatus, FoldAppUsageInfo &info);
72     int DeleteEventsByTime(uint64_t clearDataTime);
73     int QueryFinalScreenStatus(uint64_t endTime);
74     int AddAppEvent(const AppEventRecord& appEventRecord);
75     int QueryRawEventIndex(const std::string& bundleName, int rawId);
76     void QueryAppEventRecords(int startIndex, int64_t dayStartTime, const std::string& bundleName,
77         std::vector<AppEventRecord>& records);
78     std::vector<std::pair<int, std::string>> QueryEventAfterEndTime(uint64_t endTime, uint64_t nowTime);
79 
80 private:
81     void CreateDbStore(const std::string& dbPath, const std::string& dbName);
82     int CreateAppEventsTable(const std::string& table);
83     FoldAppUsageInfo CaculateForegroundAppUsage(const std::vector<FoldAppUsageRawEvent> &events, uint64_t startTime,
84         uint64_t endTime, const std::string &appName, int screenStatus);
85     void ParseEntity(NativeRdb::RowEntity& entity, AppEventRecord& record);
86 
87 private:
88     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
89     std::string dbPath_;
90     std::mutex dbMutex_;
91 };
92 } // namespace HiviewDFX
93 } // namespace OHOS
94 #endif // HIVIEW_PLUGINS_USAGE_EVENT_REPORT_FOLD_APP_USAGE_DB_HELPER_H