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 84 private: 85 void CheckDatabaseVersion(); 86 void HandleTableInfo(uint32_t databaseType); 87 std::shared_ptr<NativeRdb::RdbStore> GetBundleActiveRdbStore(uint32_t databaseType); 88 int64_t ParseStartTime(const std::string &tableName); 89 int32_t NearIndexOnOrAfterCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 90 int32_t NearIndexOnOrBeforeCurrentTime(int64_t currentTime, std::vector<int64_t> &sortedTableArray); 91 int32_t RenameTableName(uint32_t databaseType, int64_t tableOldTime, int64_t tableNewTime); 92 std::string GetTableIndexSql(uint32_t databaseType, int64_t tableTime, bool createFlag, 93 int32_t indexFlag = BUNDLE_ACTIVE_DB_INDEX_NORMAL); 94 int32_t SetNewIndexWhenTimeChanged(uint32_t databaseType, int64_t tableOldTime, 95 int64_t tableNewTime, std::shared_ptr<NativeRdb::RdbStore> rdbStore); 96 void FlushPackageInfo(uint32_t databaseType, const BundleActivePeriodStats &stats); 97 void FlushEventInfo(uint32_t databaseType, BundleActivePeriodStats &stats); 98 void DeleteExcessiveTableData(uint32_t databaseType); 99 int32_t DeleteInvalidTable(uint32_t databaseType, int64_t tableTimeMillis); 100 std::unique_ptr<std::vector<int64_t>> GetOverdueTableCreateTime(uint32_t databaseType, 101 int64_t currentTimeMillis); 102 int32_t CreatePackageLogTable(uint32_t databaseType, int64_t currentTimeMillis); 103 int32_t CreateEventLogTable(uint32_t databaseType, int64_t currentTimeMillis); 104 int32_t CreateDurationTable(uint32_t databaseType); 105 int32_t CreateBundleHistoryTable(uint32_t databaseType); 106 int32_t CreateModuleRecordTable(uint32_t databaseType, int64_t timeStamp); 107 int32_t CreateFormRecordTable(uint32_t databaseType, int64_t timeStamp); 108 std::shared_ptr<NativeRdb::ResultSet> QueryStatsInfoByStep(uint32_t databaseType, 109 const std::string &sql, const std::vector<std::string> &selectionArgs); 110 void DeleteUninstalledInfo(const int32_t userId, const std::string& bundleName, const int32_t uid, 111 const std::string& tableName, uint32_t databaseType, const int32_t appIndex); 112 int32_t CreateDatabasePath(); 113 int64_t GetSystemTimeMs(); 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 int32_t JudgeQueryCondition(const int64_t beginTime, const int64_t endTime, const int64_t eventTableTime); 119 std::string GetSystemEventName(const int32_t userId); 120 int32_t ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, int64_t tableNewTime, 121 std::shared_ptr<NativeRdb::RdbStore> rdbStore); 122 void GetQuerySqlCommand(const int64_t beginTime, 123 const int64_t endTime, const int32_t databaseType, 124 const int32_t index, const int32_t startIndex, const int32_t endIndex, const int32_t userId, 125 std::vector<std::string> &queryCondition, std::string &queryPackageSql); 126 bool GetDbIndex(const int64_t beginTime, const int64_t endTime, 127 const int32_t databaseType, int32_t &startIndex, int32_t &endIndex); 128 129 private: 130 int32_t GetOldDbVersion(); 131 int32_t GetVersionByFileInput(const std::string& FileVersionInput); 132 void CreateRecordTable(const int64_t timeStamp); 133 void HandleAllTableName(const uint32_t databaseType, std::vector<std::vector<std::string>>& allTableName); 134 void UpgradleDatabase(const int32_t oldVersion, const int32_t curVersion); 135 void UpdateOldDataUid(const std::shared_ptr<NativeRdb::RdbStore> store, const std::string& tableName, 136 const int32_t userId, std::map<std::string, int32_t>& bundleNameUidMap); 137 void SupportAppTwin(); 138 void AddRdbColumn(const std::shared_ptr<NativeRdb::RdbStore> store, const std::string& tableName, 139 const std::string& columnName, const std::string& columnType); 140 void CheckDatabaseFileAndTable(); 141 std::vector<std::string> databaseFiles_; 142 std::vector<std::vector<int64_t>> sortedTableArray_; 143 std::map<std::string, std::shared_ptr<NativeRdb::RdbStore>> bundleActiveRdbStoreCache_; 144 std::shared_ptr<BundleActiveCalendar> calendar_; 145 std::string eventTableName_; 146 std::string durationTableName_; 147 std::string bundleHistoryTableName_; 148 std::string moduleRecordsTableName_; 149 std::string formRecordsTableName_; 150 std::string versionFile_; 151 uint32_t currentVersion_; 152 ffrt::mutex databaseMutex_; 153 std::int64_t eventBeginTime_; 154 bool debugDatabase_; 155 }; 156 } // namespace DeviceUsageStats 157 } // namespace OHOS 158 #endif // BUNDLE_ACTIVE_USAGE_DATABASE_H 159 160