1 /* 2 * Copyright (c) 2022 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 BUNDLE_ACTIVE_USAGE_DATABASE_H 17 #define BUNDLE_ACTIVE_USAGE_DATABASE_H 18 19 #include <vector> 20 #include <string> 21 #include <mutex> 22 23 #include "rdb_errno.h" 24 #include "rdb_helper.h" 25 #include "rdb_open_callback.h" 26 #include "rdb_store.h" 27 #include "rdb_store_config.h" 28 #include "result_set.h" 29 #include "values_bucket.h" 30 31 #include "bundle_active_period_stats.h" 32 #include "bundle_active_calendar.h" 33 #include "bundle_active_package_history.h" 34 #include "bundle_active_module_record.h" 35 36 namespace OHOS { 37 namespace DeviceUsageStats { 38 class BundleActiveUsageDatabase { 39 public: 40 BundleActiveUsageDatabase(); 41 ~BundleActiveUsageDatabase(); 42 void InitDatabaseTableInfo(int64_t currentTime); 43 void InitUsageGroupDatabase(const int32_t databaseType, const bool forModuleRecords); 44 void UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats); 45 void UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats); 46 std::shared_ptr<BundleActivePeriodStats> GetCurrentUsageData(int32_t databaseType, int32_t userId); 47 void RenewTableTime(int64_t timeDiffMillis); 48 int32_t GetOptimalIntervalType(int64_t beginTime, int64_t endTime); 49 void RemoveOldData(int64_t currentTime); 50 std::vector<BundleActivePackageStats> QueryDatabaseUsageStats(int32_t databaseType, int64_t beginTime, 51 int64_t endTime, int32_t userId); 52 std::vector<BundleActiveEvent> QueryDatabaseEvents(int64_t beginTime, int64_t endTime, int32_t userId, 53 std::string bundleName); 54 void PutDurationData(int64_t bootBasedDuration, int64_t screenOnDuration); 55 std::pair<int64_t, int64_t> GetDurationData(); 56 void PutBundleHistoryData(int32_t userId, std::shared_ptr<std::map<std::string, 57 std::shared_ptr<BundleActivePackageHistory>>> userHistory); 58 std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>> 59 GetBundleHistoryData(int32_t userId); 60 void OnPackageUninstalled(const int32_t userId, const std::string& bundleName); 61 void ChangeToDebug(); 62 void UpdateModuleData(const int32_t userId, 63 std::map<std::string, std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords, const int64_t timeStamp); 64 void RemoveFormData(const int32_t userId, const std::string bundleName, 65 const std::string moduleName, const std::string formName, const int32_t formDimension, 66 const int64_t formId); 67 void LoadModuleData(const int32_t userId, std::map<std::string, 68 std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords); 69 void LoadFormData(const int32_t userId, std::map<std::string, 70 std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords); 71 void QueryDeviceEventStats(int32_t eventId, int64_t beginTime, int64_t endTime, 72 std::map<std::string, BundleActiveEventStats>& eventStats, int32_t userId); 73 void QueryNotificationEventStats(int32_t eventId, int64_t beginTime, int64_t endTime, 74 std::map<std::string, BundleActiveEventStats>& notificationEventStats, int32_t userId); 75 76 private: 77 void CheckDatabaseVersion(); 78 void HandleTableInfo(uint32_t databaseType); 79 std::shared_ptr<NativeRdb::RdbStore> GetBundleActiveRdbStore(uint32_t databaseType); 80 int64_t ParseStartTime(const std::string &tableName); 81 int32_t NearIndexOnOrAfterCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 82 int32_t NearIndexOnOrBeforeCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 83 int32_t RenameTableName(uint32_t databaseType, int64_t tableOldTime, int64_t tableNewTime); 84 std::string GetTableIndexSql(uint32_t databaseType, int64_t tableTime, bool createFlag, 85 int32_t indexFlag = BUNDLE_ACTIVE_DB_INDEX_NORMAL); 86 int32_t SetNewIndexWhenTimeChanged(uint32_t databaseType, int64_t tableOldTime, 87 int64_t tableNewTime, std::shared_ptr<NativeRdb::RdbStore> rdbStore); 88 void FlushPackageInfo(uint32_t databaseType, const BundleActivePeriodStats &stats); 89 void FlushEventInfo(uint32_t databaseType, BundleActivePeriodStats &stats); 90 void DeleteExcessiveTableData(uint32_t databaseType); 91 int32_t DeleteInvalidTable(uint32_t databaseType, int64_t tableTimeMillis); 92 std::unique_ptr<std::vector<int64_t>> GetOverdueTableCreateTime(uint32_t databaseType, 93 int64_t currentTimeMillis); 94 int32_t CreatePackageLogTable(uint32_t databaseType, int64_t currentTimeMillis); 95 int32_t CreateEventLogTable(uint32_t databaseType, int64_t currentTimeMillis); 96 int32_t CreateDurationTable(uint32_t databaseType); 97 int32_t CreateBundleHistoryTable(uint32_t databaseType); 98 int32_t CreateModuleRecordTable(uint32_t databaseType, int64_t timeStamp); 99 int32_t CreateFormRecordTable(uint32_t databaseType, int64_t timeStamp); 100 std::unique_ptr<NativeRdb::ResultSet> QueryStatsInfoByStep(uint32_t databaseType, 101 const std::string &sql, const std::vector<std::string> &selectionArgs); 102 void DeleteUninstalledInfo(const int32_t userId, const std::string& bundleName, const std::string& tableName, 103 uint32_t databaseType); 104 int32_t CreateDatabasePath(); 105 int64_t GetSystemTimeMs(); 106 void CheckDatabaseFile(uint32_t databaseType); 107 void UpdateFormData(const int32_t userId, const std::string bundleName, 108 const std::string moduleName, const BundleActiveFormRecord& formRecord, 109 std::shared_ptr<NativeRdb::RdbStore> rdbStore); 110 int32_t JudgeQueryCondition(const int64_t beginTime, const int64_t endTime, const int64_t eventTableTime); 111 std::string GetSystemEventName(const int32_t userId); 112 113 private: 114 std::vector<std::string> databaseFiles_; 115 std::vector<std::vector<int64_t>> sortedTableArray_; 116 std::map<std::string, std::shared_ptr<NativeRdb::RdbStore>> bundleActiveRdbStoreCache_; 117 std::shared_ptr<BundleActiveCalendar> calendar_; 118 std::string eventTableName_; 119 std::string durationTableName_; 120 std::string bundleHistoryTableName_; 121 std::string moduleRecordsTableName_; 122 std::string formRecordsTableName_; 123 std::string versionDirectoryPath_; 124 std::string versionFile_; 125 uint32_t currentVersion_; 126 std::mutex databaseMutex_; 127 std::int64_t eventBeginTime_; 128 bool debugDatabase_; 129 }; 130 } // namespace DeviceUsageStats 131 } // namespace OHOS 132 #endif // BUNDLE_ACTIVE_USAGE_DATABASE_H 133 134