• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 namespace OHOS {
26 namespace HiviewDFX {
27 struct FoldAppUsageInfo {
28     std::string package;
29     std::string version;
30     uint32_t foldVer = 0; // usage duration when screen in fold-vertiacal status
31     uint32_t foldVerSplit = 0; // usage duration when screen in fold-vertiacal status and split-screen window
32     uint32_t foldVerFloating = 0; // usage duration when screen in fold-vertiacal status and floating window
33     uint32_t foldVerMidscene = 0; // usage duration when screen in fold-vertiacal status and midscene window
34     uint32_t foldHor = 0; // usage duration when screen in fold-horizon status
35     uint32_t foldHorSplit = 0; // usage duration when screen in fold-horizon status and split-screen window
36     uint32_t foldHorFloating = 0; // usage duration when screen in fold-horizon status and floating window
37     uint32_t foldHorMidscene = 0; // usage duration when screen in fold-horizon status and midscene window
38     uint32_t expdVer = 0; // usage duration when screen in expand-vertiacal status
39     uint32_t expdVerSplit = 0; // usage duration when screen in expand-vertiacal status and split-screen window
40     uint32_t expdVerFloating = 0; // usage duration when screen in expand-vertiacal status and floating window
41     uint32_t expdVerMidscene = 0; // usage duration when screen in expand-vertiacal status and midscene window
42     uint32_t expdHor = 0; // usage duration when screen in expand-horizon status
43     uint32_t expdHorSplit = 0; // usage duration when screen in expand-horizon status and split-screen window
44     uint32_t expdHorFloating = 0; // usage duration when screen in expand-horizon status and floating window
45     uint32_t expdHorMidscene = 0; // usage duration when screen in expand-horizon status and midscene window
46     uint32_t gVer = 0; // usage duration when screen in g-vertiacal status
47     uint32_t gVerSplit = 0; // usage duration when screen in g-vertiacal status and split-screen window
48     uint32_t gVerFloating = 0; // usage duration when screen in g-vertiacal status and floating window
49     uint32_t gVerMidscene = 0; // usage duration when screen in g-vertiacal status and midscene window
50     uint32_t gHor = 0; // usage duration when screen in g-horizon status
51     uint32_t gHorSplit = 0; // usage duration when screen in g-horizon status and split-screen window
52     uint32_t gHorFloating = 0; // usage duration when screen in g-horizon status and floating window
53     uint32_t gHorMidscene = 0; // usage duration when screen in g-horizon status and midscene window
54     uint32_t startNum = 1;
55     std::string date;
56     uint32_t usage = 0;
57 
58     FoldAppUsageInfo& operator+=(const FoldAppUsageInfo& info);
59     uint32_t GetAppUsage() const;
60 };
61 
62 struct FoldAppUsageRawEvent {
63     int64_t id = 0;
64     int rawId = 0;
65     std::string package;
66     std::string version;
67     int64_t ts = 0;
68     int64_t happenTime = 0;
69     int screenStatusBefore = 0;
70     int screenStatusAfter = 0;
71 };
72 
73 struct AppEventRecord {
74     int rawid = 0;
75     int64_t ts = 0;
76     std::string bundleName = "";
77     int preFoldStatus = 0;
78     int foldStatus = 0;
79     std::string versionName = "";
80     int64_t happenTime = 0;
81 };
82 
83 class FoldAppUsageDbHelper {
84 public:
85     FoldAppUsageDbHelper(const std::string& workPath);
86     ~FoldAppUsageDbHelper();
87 
88 public:
89     void QueryStatisticEventsInPeriod(uint64_t startTime, uint64_t endTime,
90         std::unordered_map<std::string, FoldAppUsageInfo> &infos);
91     void QueryForegroundAppsInfo(uint64_t startTime, uint64_t endTime, int screenStatus, FoldAppUsageInfo &info);
92     int DeleteEventsByTime(uint64_t clearDataTime);
93     void QueryFinalAppInfo(uint64_t endTime, FoldAppUsageRawEvent& event);
94     int AddAppEvent(const AppEventRecord& appEventRecord, const std::map<int, uint64_t>& durations = {});
95     int QueryRawEventIndex(const std::string& bundleName, int rawId);
96     void QueryAppEventRecords(int startIndex, int64_t dayStartTime, const std::string& bundleName,
97         std::vector<AppEventRecord>& records);
98 
99 private:
100     void CreateDbStore(const std::string& dbPath, const std::string& dbName);
101     int CreateAppEventsTable(const std::string& table);
102 
103 private:
104     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
105     std::string dbPath_;
106     std::mutex dbMutex_;
107 };
108 } // namespace HiviewDFX
109 } // namespace OHOS
110 #endif // HIVIEW_PLUGINS_USAGE_EVENT_REPORT_FOLD_APP_USAGE_DB_HELPER_H