diff --git a/README.md b/README.md index 5337ed0a11e283e7d581904c0c6043e8595ebe14..f28be986a2664edae4fb546d7b98beeabcd843b9 100644 --- a/README.md +++ b/README.md @@ -44,26 +44,26 @@ Taking app usage interface as an example, the main exposed interfaces are as fol -

queryBundleActiveStates(begin:number, end:number, callback:AsyncCallback<Array<BundleActiveState>>):void

+

queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

Queries the event collection of all applications through time interval.

-

queryBundleStateInfos(begin:number, end:number, callback:AsyncCallback<BundleStateInfoResponse>):void

+

queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void

Uses the start and end time to query the application usage time statistics.

-

queryCurrentBundleActiveStates(begin:number, end:number, callback:AsyncCallback<Array<BundleActiveState>>):void

+

queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

Queries the event collection of the current application through the time interval.

-

queryBundleStateInfoByInterval(byInterval:intervalType, begin:number, end:number, callback:AsyncCallback<Array<BundleStateInfo>>):void

+

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void

Queries application usage duration statistics by time interval.

-

queryAppUsagePriorityGroup(callback:AsyncCallback<number>):void

+

queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void

Queries (returns) the priority group used by the current caller application.

-

isIdleState(bundleName:string, callback:AsyncCallback<boolean>):void

+

isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void

Judges whether the application of the specified bundle name is currently idle.

diff --git a/README_ZH.md b/README_ZH.md index 8692a540c44828671e0b47ea8f1e7ae83c9d1633..dad77280943569d746dabf1efa95d53753eebc2d 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -41,26 +41,26 @@ -

queryBundleActiveStates(begin:number, end:number, callback:AsyncCallback<Array<BundleActiveState>>):void

+

queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

通过指定起始和结束时间查询所有应用的事件集合。

-

queryBundleStateInfos(begin:number, end:number, callback:AsyncCallback<BundleStateInfoResponse>):void

+

queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void

通过指定起始和结束时间查询应用使用时长统计信息。

-

queryCurrentBundleActiveStates(begin:number, end:number, callback:AsyncCallback<Array<BundleActiveState>>):void

+

queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void

通过指定起始和结束时间查询当前应用的事件集合。

-

queryBundleStateInfoByInterval(byInterval:intervalType, begin:number, end:number, callback:AsyncCallback<Array<BundleStateInfo>>):void

+

queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void

通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息。

-

queryAppUsagePriorityGroup(callback:AsyncCallback<number>):void

+

queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void

查询(返回)当前调用者应用的使用优先级群组。

-

isIdleState(bundleName:string, callback:AsyncCallback<boolean>):void

+

isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void

判断指定Bundle Name的应用当前是否是空闲状态。

diff --git a/services/common/include/bundle_active_constant.h b/services/common/include/bundle_active_constant.h index 2e3bd8c1bfed63c7b52c341cefd83c765f409b31..7a188b21f0cdd983793edab57cb85da22b04531d 100644 --- a/services/common/include/bundle_active_constant.h +++ b/services/common/include/bundle_active_constant.h @@ -92,10 +92,10 @@ const std::string BUNDLE_ACTIVE_DB_SCREEN_ON_DURATION = "screenOnDuration"; const std::string REFRESH_DATABASE_RUNNER_NAME = "RefreshDatabase"; const std::string BUNDLE_ACTIVE_DATABASE_DIR = "/data/system_ce/bundle_usage/"; const std::string BUNDLE_ACTIVE_VERSION_FILE = "/version"; -const std::string BUNDLE_ACTIVE_DATABASE_SUFFIX = ".db"; const std::string DATABASE_FILE_TABLE_NAME = "table"; const std::string SQLITE_MASTER_NAME = "name"; -const std::string DATABASE_TYPE[] = {"daily.db", "weekly.db", "monthly.db", "yearly.db", "event.db", "usageGroup.db"}; +const std::string DATABASE_TYPE[] = {"daily", "weekly", "monthly", "yearly", "event", "usageGroup"}; +const std::string SUFFIX_TYPE[] = {".db", ".db-shm", ".db-wal"}; } // namespace DeviceUsageStats } // namespace OHOS #endif // BUNDLE_ACTIVE_CONSTANT_H \ No newline at end of file diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index f52a995c3d5046667ff34c34aa071c930d259774..cc564294be16572f3819115475cb09c43a20fcd8 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -82,6 +82,7 @@ private: unsigned int databaseType); int32_t CreateDatabasePath(); int64_t GetSystemTimeMs(); + void CheckDatabaseFile(unsigned int databaseType); private: std::vector databaseFiles_; diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 4433bcc69d9d1c9875ee86288519ee5e634d265f..c8f92502eefff6e83b7e5c0cac3e268e5e2dd401 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -45,7 +45,7 @@ BundleActiveUsageDatabase::BundleActiveUsageDatabase() currentVersion_ = BUNDLE_ACTIVE_CURRENT_VERSION; versionDirectoryPath_ = BUNDLE_ACTIVE_DATABASE_DIR + BUNDLE_ACTIVE_VERSION_FILE; for (uint32_t i = 0; i < sizeof(DATABASE_TYPE)/sizeof(DATABASE_TYPE[0]); i++) { - databaseFiles_.push_back(DATABASE_TYPE[i]); + databaseFiles_.push_back(DATABASE_TYPE[i] + SUFFIX_TYPE[0]); } eventTableName_ = UNKNOWN_TABLE_NAME; durationTableName_ = UNKNOWN_TABLE_NAME; @@ -415,6 +415,30 @@ shared_ptr BundleActiveUsageDatabase::GetBundleActiveRdbSto return rdbStore; } +void BundleActiveUsageDatabase::CheckDatabaseFile(unsigned int databaseType) +{ + std::string databaseFileName = databaseFiles_.at(databaseType); + std::string dbFile; + for (uint32_t i = 0; i < sizeof(SUFFIX_TYPE) / sizeof(SUFFIX_TYPE[0]); i++) { + dbFile = BUNDLE_ACTIVE_DATABASE_DIR + DATABASE_TYPE[databaseType] + SUFFIX_TYPE[i]; + if ((access(dbFile.c_str(), F_OK) != 0) + && (bundleActiveRdbStoreCache_.find(databaseFileName) != bundleActiveRdbStoreCache_.end())) { + bundleActiveRdbStoreCache_.erase(databaseFileName); + std::string rdbStorePath = BUNDLE_ACTIVE_DATABASE_DIR + DATABASE_TYPE[databaseType] + SUFFIX_TYPE[0]; + RdbHelper::DeleteRdbStore(rdbStorePath); + if (databaseType >= 0 && databaseType < sortedTableArray_.size()) { + sortedTableArray_.at(databaseType).clear(); + } else if (databaseType == EVENT_DATABASE_INDEX) { + eventTableName_ = UNKNOWN_TABLE_NAME; + } else if (databaseType == APP_GROUP_DATABASE_INDEX) { + durationTableName_ = UNKNOWN_TABLE_NAME; + bundleHistoryTableName_ = UNKNOWN_TABLE_NAME; + } + return; + } + } +} + int32_t BundleActiveUsageDatabase::CreateEventLogTable(unsigned int databaseType, int64_t currentTimeMillis) { shared_ptr rdbStore = GetBundleActiveRdbStore(databaseType); @@ -539,15 +563,15 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int userId, BUNDLE_ACTIVE_LOGE("userHistory is nullptr"); return; } - if (bundleHistoryTableName_ == UNKNOWN_TABLE_NAME) { - CreateBundleHistoryTable(APP_GROUP_DATABASE_INDEX); - bundleHistoryTableName_ = BUNDLE_HISTORY_LOG_TABLE; - } shared_ptr rdbStore = GetBundleActiveRdbStore(APP_GROUP_DATABASE_INDEX); if (rdbStore == nullptr) { - BUNDLE_ACTIVE_LOGE("rdbStore is nullptr"); return; } + CheckDatabaseFile(APP_GROUP_DATABASE_INDEX); + if (bundleHistoryTableName_ == UNKNOWN_TABLE_NAME) { + CreateBundleHistoryTable(APP_GROUP_DATABASE_INDEX); + bundleHistoryTableName_ = BUNDLE_HISTORY_LOG_TABLE; + } int32_t changeRow = BUNDLE_ACTIVE_FAIL; int64_t outRowId = BUNDLE_ACTIVE_FAIL; NativeRdb::ValuesBucket valuesBucket; @@ -633,6 +657,7 @@ shared_ptr>> BundleActiveUsag void BundleActiveUsageDatabase::PutDurationData(int64_t bootBasedDuration, int64_t screenOnDuration) { lock_guard lock(databaseMutex_); + CheckDatabaseFile(APP_GROUP_DATABASE_INDEX); if (durationTableName_ == UNKNOWN_TABLE_NAME) { CreateDurationTable(APP_GROUP_DATABASE_INDEX); durationTableName_ = DURATION_LOG_TABLE; @@ -775,11 +800,15 @@ shared_ptr BundleActiveUsageDatabase::GetCurrentUsageDa void BundleActiveUsageDatabase::FlushEventInfo(unsigned int databaseType, BundleActivePeriodStats &stats) { + shared_ptr rdbStore = GetBundleActiveRdbStore(databaseType); + if (rdbStore == nullptr) { + BUNDLE_ACTIVE_LOGE("rdbStore is nullptr"); + return; + } if (eventTableName_ == UNKNOWN_TABLE_NAME) { CreateEventLogTable(databaseType, stats.beginTime_); } int64_t eventTableTime = ParseStartTime(eventTableName_); - shared_ptr rdbStore = GetBundleActiveRdbStore(databaseType); int64_t outRowId = BUNDLE_ACTIVE_FAIL; NativeRdb::ValuesBucket valuesBucket; for (int32_t i = 0; i < stats.events_.Size(); i++) { @@ -987,8 +1016,10 @@ void BundleActiveUsageDatabase::UpdateUsageData(int32_t databaseType, BundleActi BUNDLE_ACTIVE_LOGE("databaseType is invalid : %{public}d", databaseType); return; } + CheckDatabaseFile(databaseType); if (databaseType == DAILY_DATABASE_INDEX) { if (stats.events_.Size() != 0) { + CheckDatabaseFile(EVENT_DATABASE_INDEX); FlushEventInfo(EVENT_DATABASE_INDEX, stats); } }