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 BUNDLE_ACTIVE_USAGE_DATABASE_H 17 #define BUNDLE_ACTIVE_USAGE_DATABASE_H 18 19 #ifdef DEVICE_USAGE_UNIT_TEST 20 #define WEAK_FUNC __attribute__((weak)) 21 #else 22 #define WEAK_FUNC 23 #endif 24 25 #include <vector> 26 #include <string> 27 #include <mutex> 28 29 #include "rdb_errno.h" 30 #include "rdb_helper.h" 31 #include "rdb_open_callback.h" 32 #include "rdb_store.h" 33 #include "rdb_store_config.h" 34 #include "result_set.h" 35 #include "values_bucket.h" 36 #include "ffrt.h" 37 38 #include "bundle_active_period_stats.h" 39 #include "bundle_active_calendar.h" 40 #include "bundle_active_package_history.h" 41 #include "bundle_active_module_record.h" 42 43 namespace OHOS { 44 namespace DeviceUsageStats { 45 class BundleActiveUsageDatabase { 46 public: 47 BundleActiveUsageDatabase(); 48 ~BundleActiveUsageDatabase(); 49 void InitDatabaseTableInfo(int64_t currentTime); 50 void InitUsageGroupDatabase(const int32_t databaseType, const bool forModuleRecords); 51 void UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats); 52 void UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats); 53 std::shared_ptr<BundleActivePeriodStats> GetCurrentUsageData(int32_t databaseType, int32_t userId); 54 void RenewTableTime(int64_t timeDiffMillis); 55 int32_t GetOptimalIntervalType(int64_t beginTime, int64_t endTime); 56 void RemoveOldData(int64_t currentTime); 57 std::vector<BundleActivePackageStats> QueryDatabaseUsageStats(int32_t databaseType, int64_t beginTime, 58 int64_t endTime, int32_t userId, const std::string& bundleName = ""); 59 std::vector<BundleActiveEvent> QueryDatabaseEvents(int64_t beginTime, int64_t endTime, int32_t userId, 60 std::string bundleName); 61 void PutDurationData(int64_t bootBasedDuration, int64_t screenOnDuration); 62 std::pair<int64_t, int64_t> GetDurationData(); 63 void PutBundleHistoryData(int32_t userId, std::shared_ptr<std::map<std::string, 64 std::shared_ptr<BundleActivePackageHistory>>> userHistory); 65 std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>> 66 GetBundleHistoryData(int32_t userId); 67 void OnPackageUninstalled(const int32_t userId, const std::string& bundleName, 68 const int32_t uid, const int32_t appIndex); 69 void ChangeToDebug(); 70 void UpdateModuleData(const int32_t userId, 71 std::map<std::string, std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords, const int64_t timeStamp); 72 void RemoveFormData(const int32_t userId, const std::string bundleName, 73 const std::string moduleName, const std::string formName, const int32_t formDimension, 74 const int64_t formId, const int32_t uid); 75 void LoadModuleData(const int32_t userId, std::map<std::string, 76 std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords); 77 void LoadFormData(const int32_t userId, std::map<std::string, 78 std::shared_ptr<BundleActiveModuleRecord>>& moduleRecords); 79 void QueryDeviceEventStats(int32_t eventId, int64_t beginTime, int64_t endTime, 80 std::map<std::string, BundleActiveEventStats>& eventStats, int32_t userId); 81 void QueryNotificationEventStats(int32_t eventId, int64_t beginTime, int64_t endTime, 82 std::map<std::string, BundleActiveEventStats>& notificationEventStats, int32_t userId); 83 void DeleteExcessiveEventTableData(int32_t deleteDays); 84 85 private: 86 void CheckDatabaseVersion(); 87 void HandleTableInfo(uint32_t databaseType); 88 std::shared_ptr<NativeRdb::RdbStore> GetBundleActiveRdbStore(uint32_t databaseType); 89 int64_t ParseStartTime(const std::string &tableName); 90 int32_t NearIndexOnOrAfterCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 91 int32_t NearIndexOnOrBeforeCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 92 int32_t RenameTableName(uint32_t databaseType, int64_t tableOldTime, int64_t tableNewTime); 93 std::string GetTableIndexSql(uint32_t databaseType, int64_t tableTime, bool createFlag, 94 int32_t indexFlag = BUNDLE_ACTIVE_DB_INDEX_NORMAL); 95 int32_t SetNewIndexWhenTimeChanged(uint32_t databaseType, int64_t tableOldTime, 96 int64_t tableNewTime, std::shared_ptr<NativeRdb::RdbStore> rdbStore); 97 void FlushPackageInfo(uint32_t databaseType, const BundleActivePeriodStats &stats); 98 void FlushEventInfo(uint32_t databaseType, BundleActivePeriodStats &stats); 99 void DeleteExcessiveTableData(uint32_t databaseType); 100 int32_t DeleteInvalidTable(uint32_t databaseType, int64_t tableTimeMillis); 101 std::unique_ptr<std::vector<int64_t>> GetOverdueTableCreateTime(uint32_t databaseType, 102 int64_t currentTimeMillis); 103 int32_t CreatePackageLogTable(uint32_t databaseType, int64_t currentTimeMillis); 104 int32_t CreateEventLogTable(uint32_t databaseType, int64_t currentTimeMillis); 105 int32_t CreateDurationTable(uint32_t databaseType); 106 int32_t CreateBundleHistoryTable(uint32_t databaseType); 107 int32_t CreateModuleRecordTable(uint32_t databaseType, int64_t timeStamp); 108 int32_t CreateFormRecordTable(uint32_t databaseType, int64_t timeStamp); 109 std::shared_ptr<NativeRdb::ResultSet> QueryStatsInfoByStep(uint32_t databaseType, 110 const std::string &sql, const std::vector<std::string> &selectionArgs); 111 void DeleteUninstalledInfo(const int32_t userId, const std::string& bundleName, const int32_t uid, 112 const std::string& tableName, uint32_t databaseType, const int32_t appIndex); 113 int32_t CreateDatabasePath(); 114 void CheckDatabaseFile(uint32_t databaseType); 115 void UpdateFormData(const int32_t userId, const std::string bundleName, 116 const std::string moduleName, const BundleActiveFormRecord& formRecord, 117 std::shared_ptr<NativeRdb::RdbStore> rdbStore, 118 std::vector<NativeRdb::ValuesBucket>& formValueBuckets); 119 void BatchInsert(std::shared_ptr<NativeRdb::RdbStore> rdbStore, 120 std::vector<NativeRdb::ValuesBucket>& rawContactValues, const std::string& tableName); 121 int32_t JudgeQueryCondition(const int64_t beginTime, const int64_t endTime, const int64_t eventTableTime); 122 std::string GetSystemEventName(const int32_t userId); 123 int32_t ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, int64_t tableNewTime, 124 std::shared_ptr<NativeRdb::RdbStore> rdbStore); 125 void GetQuerySqlCommand(const int64_t beginTime, 126 const int64_t endTime, const int32_t databaseType, 127 const int32_t index, const int32_t startIndex, const int32_t endIndex, const int32_t userId, 128 std::vector<std::string> &queryCondition, std::string &queryPackageSql); 129 void GetCurrentBundleStats(int64_t currentPackageTime, 130 std::shared_ptr<NativeRdb::ResultSet>& bundleActiveResult, 131 std::shared_ptr<BundleActivePeriodStats>& intervalStats); 132 bool GetDbIndex(const int64_t beginTime, const int64_t endTime, 133 const int32_t databaseType, int32_t &startIndex, int32_t &endIndex); 134 135 private: 136 int32_t GetOldDbVersion(); 137 int32_t GetVersionByFileInput(const std::string& FileVersionInput); 138 void CreateRecordTable(const int64_t timeStamp); 139 void HandleAllTableName(const uint32_t databaseType, std::vector<std::vector<std::string>>& allTableName); 140 void UpgradeDatabase(const int32_t oldVersion, const int32_t curVersion); 141 void UpdateOldDataUid(const std::shared_ptr<NativeRdb::RdbStore> store, const std::string& tableName, 142 const int32_t userId, std::map<std::string, int32_t>& bundleNameUidMap); 143 void SupportAppTwin(); 144 void AddRdbColumn(const std::shared_ptr<NativeRdb::RdbStore> store, const std::string& tableName, 145 const std::string& columnName, const std::string& columnType); 146 void SupportFirstUseTime(); 147 void UpdateFirstUseTime(const std::shared_ptr<NativeRdb::RdbStore> store, 148 const std::string& tableName, const int32_t userId); 149 std::map<std::string, int64_t> GetAllBundleFirstUseTime(const int32_t userId); 150 std::vector<std::string> databaseFiles_; 151 std::vector<std::vector<int64_t>> sortedTableArray_; 152 std::map<std::string, std::shared_ptr<NativeRdb::RdbStore>> bundleActiveRdbStoreCache_; 153 std::shared_ptr<BundleActiveCalendar> calendar_; 154 void CheckDatabaseFileAndTable(); 155 std::string eventTableName_; 156 std::string durationTableName_; 157 std::string bundleHistoryTableName_; 158 std::string moduleRecordsTableName_; 159 std::string formRecordsTableName_; 160 std::string versionFile_; 161 uint32_t currentVersion_; 162 ffrt::mutex databaseMutex_; 163 std::int64_t eventBeginTime_; 164 bool debugDatabase_; 165 }; 166 } // namespace DeviceUsageStats 167 } // namespace OHOS 168 #endif // BUNDLE_ACTIVE_USAGE_DATABASE_H 169 170