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