From d3fcebac24133cb5d4f95cfd337f959630d22daa Mon Sep 17 00:00:00 2001 From: houdisheng Date: Wed, 16 Aug 2023 17:55:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B6=85=E5=A4=A7=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- .../include/bundle_active_account_helper.h | 1 + services/common/include/bundle_active_core.h | 10 + services/common/include/bundle_active_stub.h | 16 + .../include/bundle_active_usage_database.h | 1 + .../src/bundle_active_account_helper.cpp | 16 + .../src/bundle_active_app_state_obsever.cpp | 5 +- services/common/src/bundle_active_core.cpp | 4 +- services/common/src/bundle_active_stub.cpp | 429 ++++++++++-------- .../src/bundle_active_usage_database.cpp | 47 +- .../src/bundle_active_group_handler.cpp | 17 +- .../include/bundle_active_period_stats.h | 7 + .../src/bundle_active_period_stats.cpp | 11 + 12 files changed, 332 insertions(+), 232 deletions(-) diff --git a/services/common/include/bundle_active_account_helper.h b/services/common/include/bundle_active_account_helper.h index b304aed..ca0d6c2 100644 --- a/services/common/include/bundle_active_account_helper.h +++ b/services/common/include/bundle_active_account_helper.h @@ -26,6 +26,7 @@ namespace DeviceUsageStats { class BundleActiveAccountHelper { public: static ErrCode GetUserId(const int32_t uid, int32_t& userId); + static void GetActiveUserId(std::vector &activatedOsAccountIds); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 43b2101..0aae5c5 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -54,6 +54,16 @@ public: BundleActiveReportHandlerObject(const int32_t userId, const std::string bundleName); BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig); ~BundleActiveReportHandlerObject() {} + + BUNDLE_ACTIVE_LOGI("OnAbilityStateChangeduser id is %{public}d, bundle name is %{public}s, " + "ability name is %{public}s, ability id is %{public}s, event id is %{public}d", + tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(), + tmpHandlerObject.event_.abilityName_.c_str(), abilityId.c_str(), tmpHandlerObject.event_.eventId_); + std::string ToString() + { + std::string result; + result += "userid is " + std::to_string(userId_) + ", " + event_.ToString(); + } }; class BundleActiveReportHandler; diff --git a/services/common/include/bundle_active_stub.h b/services/common/include/bundle_active_stub.h index a3c5983..fb9cca4 100644 --- a/services/common/include/bundle_active_stub.h +++ b/services/common/include/bundle_active_stub.h @@ -34,6 +34,22 @@ public: */ int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) override; +private: + static const std::map> interfaces_; + ErrCode HandleReportEvent(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option); + ErrCode HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleStatsInfoByInterval(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleEvents(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleStatsInfos(MessageParcel& data, MessageParcel& reply); + ErrCode HandleSetAppGroup(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryCurrentBundleEvents(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryModuleUsageRecords(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryAppGroup(MessageParcel& data, MessageParcel& reply); + ErrCode HandleRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply); + ErrCode HandleUnRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryDeviceEventStats(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryNotificationEventStats(MessageParcel& data, MessageParcel& reply); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index 9dddfd0..992c4d1 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -115,6 +115,7 @@ private: std::shared_ptr rdbStore); int32_t JudgeQueryCondition(const int64_t beginTime, const int64_t endTime, const int64_t eventTableTime); std::string GetSystemEventName(const int32_t userId); + int32_t ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, int64_t tableNewTime); private: std::vector databaseFiles_; diff --git a/services/common/src/bundle_active_account_helper.cpp b/services/common/src/bundle_active_account_helper.cpp index 3886931..a8447b1 100644 --- a/services/common/src/bundle_active_account_helper.cpp +++ b/services/common/src/bundle_active_account_helper.cpp @@ -30,6 +30,22 @@ ErrCode BundleActiveAccountHelper::GetUserId(const int32_t uid, int32_t& userId) #endif // OS_ACCOUNT_PART_ENABLED return ret; } + + +void BundleActiveAccountHelper::GetActiveUserId(std::vector &activatedOsAccountIds) +{ + #ifdef OS_ACCOUNT_PART_ENABLED + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { + BUNDLE_ACTIVE_LOGI("query activated account failed"); + return; + } + if (activatedOsAccountIds.size() == 0) { + return; + } + #else // OS_ACCOUNT_PART_ENABLED + activatedOsAccountIds.push_back(DEFAULT_OS_ACCOUNT_ID); + #endif // OS_ACCOUNT_PART_ENABLED +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_app_state_obsever.cpp b/services/common/src/bundle_active_app_state_obsever.cpp index f1b4882..0c7850f 100644 --- a/services/common/src/bundle_active_app_state_obsever.cpp +++ b/services/common/src/bundle_active_app_state_obsever.cpp @@ -70,10 +70,7 @@ void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData default: return; } - BUNDLE_ACTIVE_LOGI("OnAbilityStateChangeduser id is %{public}d, bundle name is %{public}s, " - "ability name is %{public}s, ability id is %{public}s, event id is %{public}d", - tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(), - tmpHandlerObject.event_.abilityName_.c_str(), abilityId.c_str(), tmpHandlerObject.event_.eventId_); + BUNDLE_ACTIVE_LOGI("OnAblityStateChanged %{public}s", tmpHandlerObject.ToString().c_str()); if (reportHandler_ != nullptr) { std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index ca03ef7..06ad2e5 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -70,14 +70,12 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da { std::lock_guard lock(mutex_); std::string action = data.GetWant().GetAction(); - BUNDLE_ACTIVE_LOGD("OnReceiveEvent action is %{public}s", action.c_str()); if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF || action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) { if (!activeGroupController_.expired()) { sptr timer = MiscServices::TimeServiceClient::GetInstance(); bool isScreenOn = activeGroupController_.lock()->IsScreenOn(); - BUNDLE_ACTIVE_LOGI("OnReceiveEvent Screen state changed " - "received, screen state change to %{public}d", isScreenOn); + BUNDLE_ACTIVE_LOGI("screen state change to %{public}d", isScreenOn); activeGroupController_.lock()->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); } } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index ff47330..78bc065 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -33,6 +33,7 @@ namespace { constexpr int32_t EVENT_MAX_SIZE = 100000; constexpr int32_t PACKAGE_MAX_SIZE = 1000; } + int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) { @@ -41,218 +42,282 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me } switch (code) { case static_cast(IBundleActiveServiceInterfaceCode::REPORT_EVENT): { - int32_t userId = data.ReadInt32(); - std::shared_ptr tmpEvent = BundleActiveEvent::UnMarshalling(data); - if (!tmpEvent) { - return -1; - } - int32_t result = ReportEvent(*tmpEvent, userId); - return reply.WriteInt32(result); + return HandleReportEvent(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::IS_BUNDLE_IDLE): { - bool isBundleIdle = false; - std::string bundleName = data.ReadString(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId); - reply.WriteInt32(isBundleIdle); - return reply.WriteInt32(errCode); + return HandleIsBundleIdle(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFO_BY_INTERVAL): { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId); - int32_t size = static_cast(result.size()); - if (size > PACKAGE_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + return HandleQueryBundleStatsInfoByInterval(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_EVENTS): { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId); - int32_t size = static_cast(result.size()); - if (size > EVENT_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + return HandleQueryBundleEvents(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::SET_APP_GROUP): { - std::string bundleName = data.ReadString(); - int32_t newGroup = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = SetAppGroup(bundleName, newGroup, userId); - return reply.WriteInt32(errCode); + return HandleSetAppGroup(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFOS): { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime); - int32_t size = static_cast(result.size()); - if (size > PACKAGE_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + return HandleQueryBundleStatsInfos(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_CURRENT_BUNDLE_EVENTS): { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime); - int32_t size = static_cast(result.size()); - if (size > EVENT_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + return HandleQueryCurrentBundleEvents(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_APP_GROUP): { - int32_t appGroup = -1; - std::string bundleName = data.ReadString(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId); - reply.WriteInt32(appGroup); - return reply.WriteInt32(errCode); + return HandleQueryAppGroup(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_MODULE_USAGE_RECORDS): { - std::vector results; - int32_t maxNum = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId); - int32_t size = static_cast(results.size()); - if (size > PACKAGE_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = results[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; + return HandleQueryModuleUsageRecords(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::REGISTER_APP_GROUP_CALLBACK): { - auto observer = iface_cast(data.ReadRemoteObject()); - if (!observer) { - BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return"); - return false; - } - BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok"); - ErrCode errCode = RegisterAppGroupCallBack(observer); - return reply.WriteInt32(errCode); + return HandleRegisterAppGroupCallBack(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::UNREGISTER_APP_GROUP_CALLBACK): { - auto observer = iface_cast(data.ReadRemoteObject()); - if (!observer) { - BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return"); - return false; - } - ErrCode errCode = UnRegisterAppGroupCallBack(observer); - return reply.WriteInt32(errCode); + return HandleUnRegisterAppGroupCallBack(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_DEVICE_EVENT_STATES): { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId); - int32_t size = static_cast(result.size()); - if (size > EVENT_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (!tmp) { - return 1; - } - } - return size == 0; + return HandleQueryDeviceEventStats(data, reply); } case static_cast(IBundleActiveServiceInterfaceCode::QUERY_NOTIFICATION_NUMBER): { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId); - int32_t size = static_cast(result.size()); - if (size > PACKAGE_MAX_SIZE) { - errCode = ERR_QUERY_RESULT_TOO_LARGE; - reply.WriteInt32(errCode); - return -1; - } - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (!tmp) { - return 1; - } - } - return size == 0; + return HandleQueryNotificationEventStats(data, reply); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } return ERR_OK; } + +ErrCode BundleActiveStub::HandleReportEvent(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + int32_t userId = data.ReadInt32(); + std::shared_ptr tmpEvent = BundleActiveEvent::UnMarshalling(data); + if (!tmpEvent) { + return -1; + } + int32_t result = ReportEvent(*tmpEvent, userId); + return reply.WriteInt32(result); +} + +ErrCode BundleActiveStub::HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply) +{ + bool isBundleIdle = false; + std::string bundleName = data.ReadString(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId); + reply.WriteInt32(isBundleIdle); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryBundleStatsInfoByInterval(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryBundleEvents(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryBundleStatsInfos(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} +ErrCode BundleActiveStub::HandleSetAppGroup(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName = data.ReadString(); + int32_t newGroup = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = SetAppGroup(bundleName, newGroup, userId); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryCurrentBundleEvents(MessageParcel &data, MessageParcel &reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryModuleUsageRecords(MessageParcel &data, MessageParcel &reply) +{ + std::vector results; + int32_t maxNum = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId); + int32_t size = static_cast(results.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = results[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryAppGroup(MessageParcel& data, MessageParcel& reply) +{ + int32_t appGroup = -1; + std::string bundleName = data.ReadString(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId); + reply.WriteInt32(appGroup); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply) +{ + auto observer = iface_cast(data.ReadRemoteObject()); + if (!observer) { + BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return"); + return false; + } + BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok"); + ErrCode errCode = RegisterAppGroupCallBack(observer); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleUnRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply) +{ + auto observer = iface_cast(data.ReadRemoteObject()); + if (!observer) { + BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return"); + return false; + } + ErrCode errCode = UnRegisterAppGroupCallBack(observer); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryDeviceEventStats(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (!tmp) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryNotificationEventStats(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (!tmp) { + return 1; + } + } + return size == 0; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 46cbbfe..643ff0b 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -645,7 +645,6 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int32_t userId, { lock_guard lock(databaseMutex_); if (userHistory == nullptr) { - BUNDLE_ACTIVE_LOGE("userHistory is nullptr"); return; } shared_ptr rdbStore = GetBundleActiveRdbStore(APP_GROUP_DATABASE_INDEX); @@ -691,8 +690,7 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int32_t userId, iter->second->isChanged_ = false; updatedcount++; } - BUNDLE_ACTIVE_LOGI("PutBundleHistoryData, update %{public}d bundles, keep %{public}d bundles group", - updatedcount, unupdatedcount); + BUNDLE_ACTIVE_LOGI("update %{public}d bundles, keep %{public}d bundles group", updatedcount, unupdatedcount); } shared_ptr>> BundleActiveUsageDatabase::GetBundleHistoryData( @@ -832,7 +830,6 @@ shared_ptr BundleActiveUsageDatabase::GetCurrentUsageDa { lock_guard lock(databaseMutex_); if (databaseType < 0 || databaseType >= static_cast(sortedTableArray_.size())) { - BUNDLE_ACTIVE_LOGE("databaseType is invalid, databaseType = %{public}d", databaseType); return nullptr; } @@ -840,10 +837,8 @@ shared_ptr BundleActiveUsageDatabase::GetCurrentUsageDa if (tableNumber == TABLE_NOT_EXIST) { return nullptr; } - shared_ptr intervalStats = make_shared(); - intervalStats->userId_ = userId; int64_t currentPackageTime = sortedTableArray_.at(databaseType).at(tableNumber - 1); - intervalStats->beginTime_ = currentPackageTime; + shared_ptr intervalStats = make_shared(userId, currentPackageTime); string packageTableName = PACKAGE_LOG_TABLE + to_string(currentPackageTime); string queryPackageSql = "select * from " + packageTableName + " where userId = ?"; vector queryCondition; @@ -1012,12 +1007,7 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ return BUNDLE_ACTIVE_FAIL; } if (databaseType >= 0 && databaseType < sortedTableArray_.size()) { - string oldPackageTableName = PACKAGE_LOG_TABLE + to_string(tableOldTime); - string newPackageTableName = PACKAGE_LOG_TABLE + to_string(tableNewTime); - string renamePackageTableNameSql = "alter table " + oldPackageTableName + " rename to " + - newPackageTableName; - int32_t renamePackageTableName = rdbStore->ExecuteSql(renamePackageTableNameSql); - if (renamePackageTableName != NativeRdb::E_OK) { + if (ExecuteRenameTableName(PACKAGE_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); @@ -1025,11 +1015,7 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ return BUNDLE_ACTIVE_FAIL; } } else if (databaseType == EVENT_DATABASE_INDEX) { - string oldEventTableName = EVENT_LOG_TABLE + to_string(tableOldTime); - string newEventTableName = EVENT_LOG_TABLE + to_string(tableNewTime); - string renameEventTableNameSql = "alter table " + oldEventTableName + " rename to " + newEventTableName; - int32_t renameEventTableName = rdbStore->ExecuteSql(renameEventTableNameSql); - if (renameEventTableName != NativeRdb::E_OK) { + if (ExecuteRenameTableName(EVENT_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); @@ -1037,18 +1023,10 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ return BUNDLE_ACTIVE_FAIL; } } else if (databaseType == APP_GROUP_DATABASE_INDEX) { - string oldModuleTableName = MODULE_RECORD_LOG_TABLE + to_string(tableOldTime); - string newModuleTableName = MODULE_RECORD_LOG_TABLE + to_string(tableNewTime); - string renameModuleTableNameSql = "alter table " + oldModuleTableName + " rename to " + newModuleTableName; - int32_t renameModuleTableName = rdbStore->ExecuteSql(renameModuleTableNameSql); - if (renameModuleTableName != NativeRdb::E_OK) { + if (ExecuteRenameTableName(MODULE_RECORD_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } - string oldFormTableName = FORM_RECORD_LOG_TABLE + to_string(tableOldTime); - string newFormTableName = FORM_RECORD_LOG_TABLE + to_string(tableNewTime); - string renameFormTableNameSql = "alter table " + oldFormTableName + " rename to " + newFormTableName; - int32_t renameFormTableName = rdbStore->ExecuteSql(renameFormTableNameSql); - if (renameFormTableName != NativeRdb::E_OK) { + if (ExecuteRenameTableName(FORM_RECORD_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); @@ -1059,6 +1037,19 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ return BUNDLE_ACTIVE_SUCCESS; } +int32_t BundleActiveUsageDatabase::ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, + int64_t tableNewTime) +{ + string oleTableName = tablePrefix + to_string(tableOldTime); + string newTableName = tablePrefix + to_string(tableNewTime); + string renameTableNameSql = "alter table " + oldTableName + " rename to " + newTableName; + int32_t renameTableName = rdbStore->ExecuteSql(renameTableNameSql); + if (renameTableName != NativeRdb::E_OK) { + return BUNDLE_ACTIVE_FAIL; + } + return ERR_OK; +} + int32_t BundleActiveUsageDatabase::GetOptimalIntervalType(int64_t beginTime, int64_t endTime) { lock_guard lock(databaseMutex_); diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index fd897f7..5f960c6 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -14,9 +14,7 @@ */ #include "time_service_client.h" -#ifdef OS_ACCOUNT_PART_ENABLED -#include "os_account_manager.h" -#endif // OS_ACCOUNT_PART_ENABLED +#include "bundle_active_account_helper.h" #include "bundle_active_group_handler.h" namespace OHOS { @@ -70,18 +68,7 @@ void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointe } case MSG_ONE_TIME_CHECK_BUNDLE_STATE: { std::vector activatedOsAccountIds; -#ifdef OS_ACCOUNT_PART_ENABLED - if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { - BUNDLE_ACTIVE_LOGI("query activated account failed"); - return; - } - if (activatedOsAccountIds.size() == 0) { - return; - } -#else // OS_ACCOUNT_PART_ENABLED - activatedOsAccountIds.push_back(DEFAULT_OS_ACCOUNT_ID); - BUNDLE_ACTIVE_LOGI("os account part not enabled, use default id."); -#endif // OS_ACCOUNT_PART_ENABLED + BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds); for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); diff --git a/services/packageusage/include/bundle_active_period_stats.h b/services/packageusage/include/bundle_active_period_stats.h index e2efc70..4627e84 100644 --- a/services/packageusage/include/bundle_active_period_stats.h +++ b/services/packageusage/include/bundle_active_period_stats.h @@ -50,6 +50,13 @@ public: * function: BundleActivePeriodStats,default constructor. */ BundleActivePeriodStats(); + + /* + * function: constructor, provided userId, beginTime. + * parameters: userid, beginTime. + */ + BundleActivePeriodStats(int32_t userId, int64_t beginTime); + /* * function: GetOrCreateUsageStats, get or create bundle usage statistics object of a bundle. * parameters: bundleName diff --git a/services/packageusage/src/bundle_active_period_stats.cpp b/services/packageusage/src/bundle_active_period_stats.cpp index e870739..502a764 100644 --- a/services/packageusage/src/bundle_active_period_stats.cpp +++ b/services/packageusage/src/bundle_active_period_stats.cpp @@ -29,6 +29,17 @@ BundleActivePeriodStats::BundleActivePeriodStats() packetNamesCache_.clear(); } +BundleActivePeriodStats::BundleActivePeriodStats(int32_t userId, int64_t beginTime) +{ + userId_ = userId; + beginTime_ = beginTime; + endTime_ = 0; + lastTimeSaved_ = 0; + bundleStats_.clear(); + events_.events_.clear(); + packetNamesCache_.clear(); +} + std::shared_ptr BundleActivePeriodStats::GetOrCreateUsageStats( const std::string& bundleName) { -- Gitee From fa318383458b262d6ed34a7355cf73513ee9ad52 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Thu, 17 Aug 2023 11:09:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B6=85=E5=A4=A7=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/common/include/bundle_active_core.h | 5 +--- .../include/bundle_active_usage_database.h | 3 ++- .../src/bundle_active_account_helper.cpp | 21 ++++++++-------- .../src/bundle_active_usage_database.cpp | 25 ++++++++----------- .../include/bundle_active_user_service.h | 1 + .../src/bundle_active_user_service.cpp | 20 +++++++++------ 6 files changed, 38 insertions(+), 37 deletions(-) diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 0aae5c5..2b03de8 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -55,14 +55,11 @@ public: BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig); ~BundleActiveReportHandlerObject() {} - BUNDLE_ACTIVE_LOGI("OnAbilityStateChangeduser id is %{public}d, bundle name is %{public}s, " - "ability name is %{public}s, ability id is %{public}s, event id is %{public}d", - tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(), - tmpHandlerObject.event_.abilityName_.c_str(), abilityId.c_str(), tmpHandlerObject.event_.eventId_); std::string ToString() { std::string result; result += "userid is " + std::to_string(userId_) + ", " + event_.ToString(); + return result; } }; diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index 992c4d1..b188aec 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -115,7 +115,8 @@ private: std::shared_ptr rdbStore); int32_t JudgeQueryCondition(const int64_t beginTime, const int64_t endTime, const int64_t eventTableTime); std::string GetSystemEventName(const int32_t userId); - int32_t ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, int64_t tableNewTime); + int32_t ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, int64_t tableNewTime, + std::shared_ptr rdbStore); private: std::vector databaseFiles_; diff --git a/services/common/src/bundle_active_account_helper.cpp b/services/common/src/bundle_active_account_helper.cpp index a8447b1..379de1f 100644 --- a/services/common/src/bundle_active_account_helper.cpp +++ b/services/common/src/bundle_active_account_helper.cpp @@ -34,17 +34,16 @@ ErrCode BundleActiveAccountHelper::GetUserId(const int32_t uid, int32_t& userId) void BundleActiveAccountHelper::GetActiveUserId(std::vector &activatedOsAccountIds) { - #ifdef OS_ACCOUNT_PART_ENABLED - if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { - BUNDLE_ACTIVE_LOGI("query activated account failed"); - return; - } - if (activatedOsAccountIds.size() == 0) { - return; - } - #else // OS_ACCOUNT_PART_ENABLED - activatedOsAccountIds.push_back(DEFAULT_OS_ACCOUNT_ID); - #endif // OS_ACCOUNT_PART_ENABLED +#ifdef OS_ACCOUNT_PART_ENABLED + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { + return; + } + if (activatedOsAccountIds.size() == 0) { + return; + } +#else // OS_ACCOUNT_PART_ENABLED + activatedOsAccountIds.push_back(DEFAULT_OS_ACCOUNT_ID); +#endif // OS_ACCOUNT_PART_ENABLED } } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 643ff0b..8ceceb6 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -1010,18 +1010,10 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ if (ExecuteRenameTableName(PACKAGE_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } - int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); - if (setResult != BUNDLE_ACTIVE_SUCCESS) { - return BUNDLE_ACTIVE_FAIL; - } } else if (databaseType == EVENT_DATABASE_INDEX) { if (ExecuteRenameTableName(EVENT_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } - int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); - if (setResult != BUNDLE_ACTIVE_SUCCESS) { - return BUNDLE_ACTIVE_FAIL; - } } else if (databaseType == APP_GROUP_DATABASE_INDEX) { if (ExecuteRenameTableName(MODULE_RECORD_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; @@ -1029,22 +1021,27 @@ int32_t BundleActiveUsageDatabase::RenameTableName(uint32_t databaseType, int64_ if (ExecuteRenameTableName(FORM_RECORD_LOG_TABLE, tableOldTime, tableNewTime) != ERR_OK) { return BUNDLE_ACTIVE_FAIL; } - int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); - if (setResult != BUNDLE_ACTIVE_SUCCESS) { - return BUNDLE_ACTIVE_FAIL; - } + } + int32_t setResult = SetNewIndexWhenTimeChanged(databaseType, tableOldTime, tableNewTime, rdbStore); + if (setResult != BUNDLE_ACTIVE_SUCCESS) { + return BUNDLE_ACTIVE_FAIL; } return BUNDLE_ACTIVE_SUCCESS; } int32_t BundleActiveUsageDatabase::ExecuteRenameTableName(std::string tablePrefix, int64_t tableOldTime, - int64_t tableNewTime) + int64_t tableNewTime, std::shared_ptr rdbStore) { - string oleTableName = tablePrefix + to_string(tableOldTime); + if (!rdbStore) { + BUNDLE_ACTIVE_LOGE("rdbstore is nullptr"); + return BUNDLE_ACTIVE_FAIL; + } + string oldTableName = tablePrefix + to_string(tableOldTime); string newTableName = tablePrefix + to_string(tableNewTime); string renameTableNameSql = "alter table " + oldTableName + " rename to " + newTableName; int32_t renameTableName = rdbStore->ExecuteSql(renameTableNameSql); if (renameTableName != NativeRdb::E_OK) { + BUNDLE_ACTIVE_LOGE("Rename table failed"); return BUNDLE_ACTIVE_FAIL; } return ERR_OK; diff --git a/services/packageusage/include/bundle_active_user_service.h b/services/packageusage/include/bundle_active_user_service.h index c8c5377..1ad79bc 100644 --- a/services/packageusage/include/bundle_active_user_service.h +++ b/services/packageusage/include/bundle_active_user_service.h @@ -98,6 +98,7 @@ private: int64_t endTime, std::map& systemEventStats); void GetCachedNotificationEvents(std::shared_ptr currentStats, int64_t beginTime, int64_t endTime, std::map& notificationEventStats); + void FlushDataInMem(); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 5369449..7bbbd45 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -249,12 +249,10 @@ void BundleActiveUserService::LoadModuleAndFormStats() database_.LoadFormData(userId_, moduleRecords_); } -void BundleActiveUserService::RenewStatsInMemory(const int64_t timeStamp) + +void BundleActiveUserService::FlushDataInMem() { - std::set continueBundles; - std::map> continueAbilities; - std::map> continueServices; - for (std::vector>::iterator it = currentStats_.begin(); // 更新使用时长 + for (std::vector>::iterator it = currentStats_.begin(); it != currentStats_.end(); ++it) { if (*it == nullptr) { continue; @@ -278,10 +276,18 @@ void BundleActiveUserService::RenewStatsInMemory(const int64_t timeStamp) } (*it)->CommitTime(dailyExpiryDate_.GetMilliseconds() - 1); } +} + +void BundleActiveUserService::RenewStatsInMemory(const int64_t timeStamp) +{ + std::set continueBundles; + std::map> continueAbilities; + std::map> continueServices; + FlushDataInMem();// update stat in memory. RestoreStats(true); database_.RemoveOldData(timeStamp); - LoadActiveStats(timeStamp, false, false); // 新建intervalstat或加载当前数据库数据 - for (std::string continueBundleName : continueBundles) { // 更新所有事件的时间戳到新的begintime + LoadActiveStats(timeStamp, false, false); // create new stats + for (std::string continueBundleName : continueBundles) { // update timestamps of events in memory int64_t beginTime = currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->beginTime_; for (std::vector>::iterator itInterval = currentStats_.begin(); itInterval != currentStats_.end(); ++itInterval) { -- Gitee