From 0dee8baaac8b55b1c86634569df42d9dddaebff6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 12 Sep 2024 21:39:57 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E5=AE=89=E8=A3=85=E5=8D=B8=E8=BD=BD=E5=90=8C?= =?UTF-8?q?=E4=B8=80=E5=BA=94=E7=94=A8=EF=BC=8C=E6=9C=AA=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E5=88=B0=E4=BA=8B=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- .../bundle_active_common_event_subscriber.h | 1 + services/common/include/bundle_active_core.h | 3 ++ services/common/src/bundle_active_core.cpp | 47 +++++++++++++++++-- .../include/bundle_active_report_handler.h | 1 + .../src/bundle_active_report_handler.cpp | 16 +++++++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/services/common/include/bundle_active_common_event_subscriber.h b/services/common/include/bundle_active_common_event_subscriber.h index e54fbc0..d76d6f5 100644 --- a/services/common/include/bundle_active_common_event_subscriber.h +++ b/services/common/include/bundle_active_common_event_subscriber.h @@ -50,6 +50,7 @@ public: void HandleLockEvent(const std::string& action, const int32_t userId); private: + void HandleOtherEvent(const std::string& action, const int32_t userId); ffrt::mutex mutex_; std::weak_ptr activeGroupController_; std::weak_ptr bundleActiveReportHandler_; diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index eec0a7f..8fd5f3e 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -243,6 +243,8 @@ public: int32_t currentUsedUser_; void OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo); bool isUninstalledApp(const int32_t uid); + void OnBundleInstalled(const int32_t userId, const std::string& bundleName, const int32_t uid, + const int32_t appIndex); private: void NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo); @@ -265,6 +267,7 @@ private: ffrt::mutex mutex_; ffrt::recursive_mutex callbackMutex_; std::map> userStatServices_; + std::map taskMap_; void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; void RestoreAllData(); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index d172557..d19f951 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -29,7 +29,7 @@ namespace DeviceUsageStats { #ifndef OS_ACCOUNT_PART_ENABLED const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part #endif // OS_ACCOUNT_PART_ENABLED -constexpr int32_t BUNDLE_UNINSTALL_DELAY_TIME = 60 * 1000 * 1000; +constexpr int32_t BUNDLE_UNINSTALL_DELAY_TIME = 5 * 1000 * 1000; BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() { @@ -113,10 +113,32 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, handlerobjToPtr); } - } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { + } else { + HandleOtherEvent(data); + } +} + +void BundleActiveCommonEventSubscriber::HandleOtherEvent(const CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { int32_t userId = data.GetWant().GetIntParam("userId", 0); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); HandleLockEvent(action, userId); + } else if (action == COMMON_EVENT_PACKAGE_ADDED) { + int32_t userId = data.GetWant().GetIntParam("userId", 0); + std::string bundleName = data.GetWant().GetElement().GetBundleName(); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", + action.c_str(), userId, bundleName.c_str()); + if (!bundleActiveReportHandler_.expired()) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); + tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); + tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, + handlerobjToPtr); + } } } @@ -150,6 +172,7 @@ void BundleActiveCore::RegisterSubscriber() matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(COMMON_EVENT_UNLOCK_SCREEN); matchingSkills.AddEvent(COMMON_EVENT_LOCK_SCREEN); CommonEventSubscribeInfo subscriberInfo(matchingSkills); @@ -245,6 +268,19 @@ void BundleActiveCore::OnBundleUninstalled(const int32_t userId, const std::stri bundleGroupController_->OnBundleUninstalled(userId, bundleName, uid, appIndex); } +void BundleActiveCore::OnBundleInstalled(const int32_t userId, const std::string& bundleName, + const int32_t uid, const int32_t appIndex) +{ + BUNDLE_ACTIVE_LOGD("OnBundleInstalled CALLED"); + std::lock_guard lock(bundleUninstalledMutex_); + bundleUninstalledSet_.erase(uid); + if (taskMap_.find(uid) == taskMap_.end()) { + return; + } + ffrt::skip(taskMap_[uid]); + taskMap_.erase(uid); +} + void BundleActiveCore::AddbundleUninstalledUid(const int32_t uid) { std::lock_guard lock(bundleUninstalledMutex_); @@ -254,10 +290,15 @@ void BundleActiveCore::AddbundleUninstalledUid(const int32_t uid) void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) { auto bundleActiveCore = shared_from_this(); - ffrt::submit([bundleActiveCore, uid]() { + auto task = ffrt::submit_h([bundleActiveCore, uid]() { std::lock_guard lock(bundleActiveCore->bundleUninstalledMutex_); bundleActiveCore->bundleUninstalledSet_.erase(uid); + if (taskMap_.find(uid) == taskMap_.end()) { + return; + } + taskMap_.erase(uid); }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); + taskMap_[uid] = task; } bool BundleActiveCore::isUninstalledApp(const int32_t uid) diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 6f26bb5..df99dd4 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -48,6 +48,7 @@ public: static const int32_t MSG_SWITCH_USER; private: + void ProcessOtherEvent(const int32_t& eventId, const std::shared_ptr& handlerobj); bool isInited_ = false; ffrt::mutex taskHandlerMutex_; std::shared_ptr ffrtQueue_; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index db03602..128f293 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -140,6 +140,22 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); break; } + default: { + ProcessOtherEvent(eventId, handlerobj); + break; + } + } +} + +void BundleActiveReportHandler::ProcessOtherEvent(const int32_t& eventId, + const std::shared_ptr& handlerobj) +{ + switch (eventId) { + case MSG_BUNDLE_INSTALLED: { + bundleActiveCore_->OnBundleInstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, + tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); + break; + } default: { break; } -- Gitee From 3989a76216c147adb263aae2b0b95d7124548320 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 13 Sep 2024 14:28:13 +0800 Subject: [PATCH 2/6] push Signed-off-by: fengyang --- .../include/bundle_active_common_event_subscriber.h | 2 +- services/common/src/bundle_active_core.cpp | 13 +++++++++---- .../include/bundle_active_report_handler.h | 3 ++- .../src/bundle_active_report_handler.cpp | 3 ++- test/unittest/package_usage_test.cpp | 4 ++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/services/common/include/bundle_active_common_event_subscriber.h b/services/common/include/bundle_active_common_event_subscriber.h index d76d6f5..141692f 100644 --- a/services/common/include/bundle_active_common_event_subscriber.h +++ b/services/common/include/bundle_active_common_event_subscriber.h @@ -50,7 +50,7 @@ public: void HandleLockEvent(const std::string& action, const int32_t userId); private: - void HandleOtherEvent(const std::string& action, const int32_t userId); + void HandleOtherEvent(const CommonEventData &data); ffrt::mutex mutex_; std::weak_ptr activeGroupController_; std::weak_ptr bundleActiveReportHandler_; diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index d19f951..6c90297 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -23,6 +23,8 @@ #include "bundle_active_group_common.h" #include "bundle_active_bundle_mgr_helper.h" #include "bundle_active_constant.h" +#include "ffrt_inner.h" + namespace OHOS { namespace DeviceUsageStats { @@ -125,7 +127,7 @@ void BundleActiveCommonEventSubscriber::HandleOtherEvent(const CommonEventData & int32_t userId = data.GetWant().GetIntParam("userId", 0); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); HandleLockEvent(action, userId); - } else if (action == COMMON_EVENT_PACKAGE_ADDED) { + } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { int32_t userId = data.GetWant().GetIntParam("userId", 0); std::string bundleName = data.GetWant().GetElement().GetBundleName(); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", @@ -273,6 +275,9 @@ void BundleActiveCore::OnBundleInstalled(const int32_t userId, const std::string { BUNDLE_ACTIVE_LOGD("OnBundleInstalled CALLED"); std::lock_guard lock(bundleUninstalledMutex_); + if (bundleUninstalledSet_.find(uid) == bundleUninstalledSet_.end()) { + return; + } bundleUninstalledSet_.erase(uid); if (taskMap_.find(uid) == taskMap_.end()) { return; @@ -293,12 +298,12 @@ void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) auto task = ffrt::submit_h([bundleActiveCore, uid]() { std::lock_guard lock(bundleActiveCore->bundleUninstalledMutex_); bundleActiveCore->bundleUninstalledSet_.erase(uid); - if (taskMap_.find(uid) == taskMap_.end()) { + if (bundleActiveCore->taskMap_.find(uid) == bundleActiveCore->taskMap_.end()) { return; } - taskMap_.erase(uid); + bundleActiveCore->taskMap_.erase(uid); }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); - taskMap_[uid] = task; + taskMap_[uid] = std::move(task); } bool BundleActiveCore::isUninstalledApp(const int32_t uid) diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index df99dd4..87d931c 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -46,9 +46,10 @@ public: static const int32_t MSG_REMOVE_USER; static const int32_t MSG_BUNDLE_UNINSTALLED; static const int32_t MSG_SWITCH_USER; + static const int32_t MSG_BUNDLE_INSTALLED; private: - void ProcessOtherEvent(const int32_t& eventId, const std::shared_ptr& handlerobj); + void ProcessOtherEvent(const int32_t& eventId, const BundleActiveReportHandlerObject& tmpHandlerobj); bool isInited_ = false; ffrt::mutex taskHandlerMutex_; std::shared_ptr ffrtQueue_; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 128f293..906746c 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -26,6 +26,7 @@ const int32_t BundleActiveReportHandler::MSG_FLUSH_TO_DISK = 1; const int32_t BundleActiveReportHandler::MSG_REMOVE_USER = 2; const int32_t BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED = 3; const int32_t BundleActiveReportHandler::MSG_SWITCH_USER = 4; +const int32_t BundleActiveReportHandler::MSG_BUNDLE_INSTALLED = 5; void BundleActiveReportHandler::Init(const std::shared_ptr& bundleActiveCore) { @@ -148,7 +149,7 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, } void BundleActiveReportHandler::ProcessOtherEvent(const int32_t& eventId, - const std::shared_ptr& handlerobj) + const BundleActiveReportHandlerObject& tmpHandlerobj) { switch (eventId) { case MSG_BUNDLE_INSTALLED: { diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index 83ca4a6..70676ec 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -863,6 +863,10 @@ HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_004, Function | MediumT tmpObject.event_.timeStamp_ = timeNow; bundleActiveReportHandler->SendEvent(0, handlerObject); EXPECT_NE(service->currentStats_[0]->endTime_, timeNow); + bundleActiveCore_->OnBundleInstalled(userId, bundleName, uid, appIndex); + EXPECT_FALSE(bundleActiveCore_->isUninstalledApp(uid)); + bundleActiveReportHandler->SendEvent(0, handlerObject); + EXPECT_EQ(service->currentStats_[0]->endTime_, timeNow); SUCCEED(); } -- Gitee From c750180552dbf7c466b7d5cfd8c1c909cad49dc3 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 14 Sep 2024 09:14:17 +0800 Subject: [PATCH 3/6] push Signed-off-by: fengyang --- services/packageusage/src/bundle_active_report_handler.cpp | 2 +- test/unittest/package_usage_test.cpp | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 906746c..893c720 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -142,7 +142,7 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, break; } default: { - ProcessOtherEvent(eventId, handlerobj); + ProcessOtherEvent(eventId, tmpHandlerobj); break; } } diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index 70676ec..eabb8c1 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -865,8 +865,6 @@ HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_004, Function | MediumT EXPECT_NE(service->currentStats_[0]->endTime_, timeNow); bundleActiveCore_->OnBundleInstalled(userId, bundleName, uid, appIndex); EXPECT_FALSE(bundleActiveCore_->isUninstalledApp(uid)); - bundleActiveReportHandler->SendEvent(0, handlerObject); - EXPECT_EQ(service->currentStats_[0]->endTime_, timeNow); SUCCEED(); } -- Gitee From 20b4c3ca8ea718821a55904be5af5770e25cb1e9 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 20 Sep 2024 15:33:35 +0800 Subject: [PATCH 4/6] push Signed-off-by: fengyang --- services/common/src/bundle_active_core.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 6c90297..72e4eb0 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -138,7 +138,11 @@ void BundleActiveCommonEventSubscriber::HandleOtherEvent(const CommonEventData & tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, handlerobjToPtr); } } -- Gitee From d2090f4b2a358f303bba473cdf59584a3e3a6370 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 21 Sep 2024 18:18:32 +0800 Subject: [PATCH 5/6] push Signed-off-by: fengyang --- .../bundle_active_common_event_subscriber.h | 8 +- services/common/src/bundle_active_core.cpp | 145 +++++++++++------- .../include/bundle_active_report_handler.h | 6 + .../src/bundle_active_report_handler.cpp | 90 +++++++---- 4 files changed, 158 insertions(+), 91 deletions(-) diff --git a/services/common/include/bundle_active_common_event_subscriber.h b/services/common/include/bundle_active_common_event_subscriber.h index 141692f..c1e0b78 100644 --- a/services/common/include/bundle_active_common_event_subscriber.h +++ b/services/common/include/bundle_active_common_event_subscriber.h @@ -47,10 +47,14 @@ public: bundleActiveReportHandler_(bundleActiveReportHandler) {} ~BundleActiveCommonEventSubscriber() = default; void OnReceiveEvent(const CommonEventData &data) override; - void HandleLockEvent(const std::string& action, const int32_t userId); private: - void HandleOtherEvent(const CommonEventData &data); + void HandleScreenEvent(); + void HandleUserRemoveEvent(const CommonEventData &data); + void HandleUserSwitchEvent(const CommonEventData &data); + void HandlePackageRemoveEvent(const CommonEventData &data); + void HandlePackageAddEvent(const CommonEventData &data); + void HandleLockEvent(const CommonEventData &data); ffrt::mutex mutex_; std::weak_ptr activeGroupController_; std::weak_ptr bundleActiveReportHandler_; diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 72e4eb0..880c385 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -78,78 +78,113 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da std::string action = data.GetWant().GetAction(); 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("screen state change to %{public}d", isScreenOn); - activeGroupController_.lock()->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); - } + HandleScreenEvent(); } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - if (!bundleActiveReportHandler_.expired()) { - int32_t userId = data.GetCode(); - BUNDLE_ACTIVE_LOGI("remove user id %{public}d", userId); - BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); - } + HandleUserRemoveEvent(data); } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + HandleUserSwitchEvent(data); + } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || + action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { + HandlePackageRemoveEvent(data); + } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { + HandleLockEvent(data); + } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { + HandlePackageAddEvent(data); + } +} + +void BundleActiveCommonEventSubscriber::HandleScreenEvent() +{ + if (!bundleActiveReportHandler_.expired()) { + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + auto groupController = activeGroupController_.lock(); + if (groupController == nullptr) { + return; + } + bool isScreenOn = groupController->IsScreenOn(); + BUNDLE_ACTIVE_LOGI("screen state change to %{public}d", isScreenOn); + groupController->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); + } +} + +void BundleActiveCommonEventSubscriber::HandleUserRemoveEvent(const CommonEventData &data) +{ + if (!bundleActiveReportHandler_.expired()) { + int32_t userId = data.GetCode(); + BUNDLE_ACTIVE_LOGI("remove user id %{public}d", userId); + BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); + } +} + +void BundleActiveCommonEventSubscriber::HandleUserSwitchEvent(const CommonEventData &data) +{ + if (!bundleActiveReportHandler_.expired()) { int32_t userId = data.GetCode(); BUNDLE_ACTIVE_LOGI("OnReceiveEvent receive switched user event, user id is %{public}d", userId); BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); - } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || - action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { - int32_t userId = data.GetWant().GetIntParam("userId", 0); - std::string bundleName = data.GetWant().GetElement().GetBundleName(); - BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", - action.c_str(), userId, bundleName.c_str()); - if (!bundleActiveReportHandler_.expired()) { - BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); - tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); - tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, - handlerobjToPtr); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; } - } else { - HandleOtherEvent(data); + reportHandler->SendEvent(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); } } -void BundleActiveCommonEventSubscriber::HandleOtherEvent(const CommonEventData &data) +void BundleActiveCommonEventSubscriber::HandlePackageRemoveEvent(const CommonEventData &data) { - std::string action = data.GetWant().GetAction(); - if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { - int32_t userId = data.GetWant().GetIntParam("userId", 0); - BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); - HandleLockEvent(action, userId); - } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { - int32_t userId = data.GetWant().GetIntParam("userId", 0); - std::string bundleName = data.GetWant().GetElement().GetBundleName(); - BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", - action.c_str(), userId, bundleName.c_str()); - if (!bundleActiveReportHandler_.expired()) { - BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); - tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); - tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - auto reportHandler = bundleActiveReportHandler_.lock(); - if (reportHandler == nullptr) { - return; - } - reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, - handlerobjToPtr); + int32_t userId = data.GetWant().GetIntParam("userId", 0); + std::string bundleName = data.GetWant().GetElement().GetBundleName(); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", + action.c_str(), userId, bundleName.c_str()); + if (!bundleActiveReportHandler_.expired()) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); + tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); + tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, + handlerobjToPtr); + } +} + +void BundleActiveCommonEventSubscriber::HandlePackageAddEvent(const CommonEventData &data) +{ + int32_t userId = data.GetWant().GetIntParam("userId", 0); + std::string bundleName = data.GetWant().GetElement().GetBundleName(); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", + action.c_str(), userId, bundleName.c_str()); + if (!bundleActiveReportHandler_.expired()) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); + tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); + tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, + handlerobjToPtr); } } -void BundleActiveCommonEventSubscriber::HandleLockEvent(const std::string& action, const int32_t userId) +void BundleActiveCommonEventSubscriber::HandleLockEvent(const CommonEventData &data) { + int32_t userId = data.GetWant().GetIntParam("userId", 0); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); if (bundleActiveReportHandler_.expired()) { return; } diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 87d931c..8da3bd9 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -50,6 +50,12 @@ public: private: void ProcessOtherEvent(const int32_t& eventId, const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessReportEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessFlushToDiskEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessRmoveUserEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessUserSwitchEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessBundleUninstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessBundleInstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); bool isInited_ = false; ffrt::mutex taskHandlerMutex_; std::shared_ptr ffrtQueue_; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 893c720..1def73d 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -106,62 +106,84 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, BundleActiveReportHandlerObject tmpHandlerobj = *handlerobj; switch (eventId) { case MSG_REPORT_EVENT: { - BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); - if (BundleActiveEvent::IsAppStateEvent(tmpHandlerobj.event_.eventId_) && - bundleActiveCore_->isUninstalledApp(tmpHandlerobj.event_.uid_)) { - BUNDLE_ACTIVE_LOGE("not report uninstall app event"); - return; - } - bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); + ProcessReportEvent(tmpHandlerobj); break; } case MSG_FLUSH_TO_DISK: { - BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); - if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) { - BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return", - tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_); - RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); - return; - } - bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_); + ProcessFlushToDiskEvent(tmpHandlerobj); break; } case MSG_REMOVE_USER: { - bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); + ProcessRmoveUserEvent(tmpHandlerobj); + break; + } + case MSG_SWITCH_USER: { + ProcessUserSwitchEvent(tmpHandlerobj); break; } case MSG_BUNDLE_UNINSTALLED: { - BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); - bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, - tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); + ProcessBundleUninstallEvent(tmpHandlerobj); break; } - case MSG_SWITCH_USER: { - BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED"); - bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); + case MSG_BUNDLE_INSTALLED: { + ProcessBundleInstallEvent(tmpHandlerobj); break; } default: { - ProcessOtherEvent(eventId, tmpHandlerobj); break; - } + } } } -void BundleActiveReportHandler::ProcessOtherEvent(const int32_t& eventId, - const BundleActiveReportHandlerObject& tmpHandlerobj) +void BundleActiveReportHandler::ProcessReportEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) { - switch (eventId) { - case MSG_BUNDLE_INSTALLED: { - bundleActiveCore_->OnBundleInstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, - tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); - break; - } - default: { - break; + BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); + if (BundleActiveEvent::IsAppStateEvent(tmpHandlerobj.event_.eventId_) && + bundleActiveCore_->isUninstalledApp(tmpHandlerobj.event_.uid_)) { + BUNDLE_ACTIVE_LOGE("not report uninstall app event"); + return; } + bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessFlushToDiskEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); + if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) { + BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return", + tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_); + RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + return; } + bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessRmoveUserEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("Remove user"); + bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessUserSwitchEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED"); + bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); } + +void BundleActiveReportHandler::ProcessBundleUninstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); + bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, + tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); +} + +void BundleActiveReportHandler::ProcessBundleInstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_INSTALLED CALLED"); + bundleActiveCore_->OnBundleInstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, + tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); +} + } // namespace DeviceUsageStats } // namespace OHOS -- Gitee From 78297c534c4962d5b58a464807f494e0984856d6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 23 Sep 2024 10:16:10 +0800 Subject: [PATCH 6/6] push Signed-off-by: fengyang --- services/common/src/bundle_active_core.cpp | 3 +++ services/packageusage/include/bundle_active_report_handler.h | 3 +-- services/packageusage/src/bundle_active_report_handler.cpp | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 880c385..fdad002 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -141,6 +141,7 @@ void BundleActiveCommonEventSubscriber::HandleUserSwitchEvent(const CommonEventD void BundleActiveCommonEventSubscriber::HandlePackageRemoveEvent(const CommonEventData &data) { + std::string action = data.GetWant().GetAction(); int32_t userId = data.GetWant().GetIntParam("userId", 0); std::string bundleName = data.GetWant().GetElement().GetBundleName(); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", @@ -162,6 +163,7 @@ void BundleActiveCommonEventSubscriber::HandlePackageRemoveEvent(const CommonEve void BundleActiveCommonEventSubscriber::HandlePackageAddEvent(const CommonEventData &data) { + std::string action = data.GetWant().GetAction(); int32_t userId = data.GetWant().GetIntParam("userId", 0); std::string bundleName = data.GetWant().GetElement().GetBundleName(); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", @@ -183,6 +185,7 @@ void BundleActiveCommonEventSubscriber::HandlePackageAddEvent(const CommonEventD void BundleActiveCommonEventSubscriber::HandleLockEvent(const CommonEventData &data) { + std::string action = data.GetWant().GetAction(); int32_t userId = data.GetWant().GetIntParam("userId", 0); BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); if (bundleActiveReportHandler_.expired()) { diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 8da3bd9..7065b42 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -49,8 +49,7 @@ public: static const int32_t MSG_BUNDLE_INSTALLED; private: - void ProcessOtherEvent(const int32_t& eventId, const BundleActiveReportHandlerObject& tmpHandlerobj); - void ProcessReportEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessReportEvent(BundleActiveReportHandlerObject& tmpHandlerobj); void ProcessFlushToDiskEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); void ProcessRmoveUserEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); void ProcessUserSwitchEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 1def73d..c30c422 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -131,11 +131,11 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, } default: { break; - } + } } } -void BundleActiveReportHandler::ProcessReportEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +void BundleActiveReportHandler::ProcessReportEvent(BundleActiveReportHandlerObject& tmpHandlerobj) { BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); if (BundleActiveEvent::IsAppStateEvent(tmpHandlerobj.event_.eventId_) && -- Gitee