From 43449014b08c04fdb90713e011d0ae5ebee075a3 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 29 Aug 2024 20:10:24 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=A6=82=E7=8E=87?= =?UTF-8?q?=E5=9B=A0=E6=97=B6=E5=BA=8F=E9=97=AE=E9=A2=98=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=90=8E=E4=BA=8B=E4=BB=B6=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- .../innerkits/include/bundle_active_event.h | 3 +++ .../innerkits/src/bundle_active_event.cpp | 8 ++++++ services/common/include/bundle_active_core.h | 5 ++++ services/common/src/bundle_active_core.cpp | 27 +++++++++++++++++++ .../src/bundle_active_report_handler.cpp | 5 ++++ test/unittest/package_usage_test.cpp | 24 +++++++++++++++++ 6 files changed, 72 insertions(+) diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 964ed45..1531f6e 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -133,6 +133,9 @@ public: * @return return true if event reported by bundle usage. */ static bool IsBundleEvent(const int32_t eventId); + + static bool IsAppStateEvent(const int32_t& eventId); + }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_event.cpp b/interfaces/innerkits/src/bundle_active_event.cpp index 2eb1a1f..88037c2 100644 --- a/interfaces/innerkits/src/bundle_active_event.cpp +++ b/interfaces/innerkits/src/bundle_active_event.cpp @@ -208,6 +208,14 @@ bool BundleActiveEvent::IsBundleEvent(const int32_t eventId) } return false; } + +bool BundleActiveEvent::IsAppStateEvent(const int32_t eventId) +{ + if (eventId == ABILITY_BACKGROUND || eventId == ABILITY_FOREGROUND || eventId == ABILITY_STOP) { + return true; + } + return false; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index a9ea52e..6bbb347 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -242,6 +242,7 @@ public: int32_t currentUsedUser_; void OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo); + bool isUninstalledApp(const int32_t& uid); private: void NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo); @@ -249,6 +250,8 @@ private: void RemoveObserverDeathRecipient(const sptr &observer); void OnObserverDied(const wptr &remote); void OnObserverDiedInner(const wptr &remote); + void AddbundleUninstalledUid(const int32_t uid); + void DelayRemovebundleUninstalledUid(const int32_t uid); int64_t flushInterval_; static const int64_t TIME_CHANGE_THRESHOLD_MILLIS = TWO_SECONDS; const int32_t DEFAULT_USER_ID = -1; @@ -269,6 +272,8 @@ private: std::map, sptr> recipientMap_; void ObtainSystemEventName(BundleActiveEvent& event); bool debugCore_; + ffrt::mutex bundleUninstalledMutex_; + std::set bundleUninstalledSet_; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 1bb14cd..6e85c9f 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -28,6 +28,7 @@ namespace OHOS { 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 +const int32_t BUNDLE_UNINSTALL_DELAY_TIME = 60 * 1000 * 1000; #endif // OS_ACCOUNT_PART_ENABLED BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() @@ -238,10 +239,36 @@ void BundleActiveCore::OnBundleUninstalled(const int32_t userId, const std::stri if (service == nullptr) { return; } + AddbundleUninstalledUid(uid); + DelayRemovebundleUninstalledUid(uid); service->DeleteUninstalledBundleStats(bundleName, uid, appIndex); bundleGroupController_->OnBundleUninstalled(userId, bundleName, uid, appIndex); } +void BundleActiveCore::AddUidTobundleUninstalledUidSet(const int32_t uid) +{ + std::lock_guard lock(bundleUninstalledMutex_); + bundleUninstalledSet_.insert(uid); +} + +void BundleActiveCore::DelayRemoveUidTobundleUninstalledSet(const int32_t uid) +{ + auto bundleActiveCore = shared_from_this(); + ffrt::submit([bundleActiveCore, uid]() { + std::lock_guard lock(bundleUninstalledMutex_); + bundleActiveCore->bundleUninstalledSet_.erase(uid); + }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); +} + +bool BundleActiveCore::isUninstalledApp(const int32_t& uid) +{ + std::lock_guard lock(bundleUninstalledMutex_); + if (bundleUninstalledSet_.find(uid) != bundleUninstalledSet_.end()) { + return true; + } + return false; +} + void BundleActiveCore::OnStatsChanged(const int32_t userId) { if (!handler_.expired()) { diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 52785c8..db03602 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -106,6 +106,11 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, 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_); break; } diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index 8c97e15..eeae093 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -834,6 +834,30 @@ HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_003, Function | MediumT EXPECT_EQ(bundleActiveReportHandler->HasEvent(0), true); } +/* + * @tc.name: BundleActiveReportHandlerTest_004 + * @tc.desc: Send Uninstalled APP Event + * @tc.type: FUNC + * @tc.require: IAHDJW + */ +HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_004, Function | MediumTest | Level0) +{ + auto bundleActiveReportHandler = std::make_shared(); + bundleActiveReportHandler->Init(bundleActiveCore_); + int32_t userId = 100; + std::string bundleName = "test"; + int32_t uid = 100010; + int32_t appIndex = 1; + bundleActiveCore_->OnBundleUninstalled(userId, bundleName, uid, appIndex); + EXPECT_TRUE(bundleActiveCore_->isUninstalledApp(uid)); + BundleActiveReportHandlerObject tmpObject; + tmpObject.event_.eventId_ = tmpObject.event_.ABILITY_STOP; + tmpObject.event_.uid_ = uid; + auto handlerObject = std::make_shared(tmpObject); + bundleActiveReportHandler->SendEvent(0, handlerObject); + SUCCEED(); +} + /* * @tc.name: BundleActiveGroupHandler_001 * @tc.desc: SendEvent -- Gitee From 039944c5bd49959a25c3717d64f581e48964baf3 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 29 Aug 2024 20:27:32 +0800 Subject: [PATCH 2/7] push Signed-off-by: fengyang --- services/common/include/bundle_active_core.h | 2 +- services/common/src/bundle_active_core.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 6bbb347..1565259 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -251,7 +251,7 @@ private: void OnObserverDied(const wptr &remote); void OnObserverDiedInner(const wptr &remote); void AddbundleUninstalledUid(const int32_t uid); - void DelayRemovebundleUninstalledUid(const int32_t uid); + void DelayRemoveBundleUninstalledUid(const int32_t uid); int64_t flushInterval_; static const int64_t TIME_CHANGE_THRESHOLD_MILLIS = TWO_SECONDS; const int32_t DEFAULT_USER_ID = -1; diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 6e85c9f..941ea81 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -28,8 +28,8 @@ namespace OHOS { 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 -const int32_t BUNDLE_UNINSTALL_DELAY_TIME = 60 * 1000 * 1000; #endif // OS_ACCOUNT_PART_ENABLED +constexpr int32_t BUNDLE_UNINSTALL_DELAY_TIME = 60 * 1000 * 1000; BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() { @@ -240,22 +240,22 @@ void BundleActiveCore::OnBundleUninstalled(const int32_t userId, const std::stri return; } AddbundleUninstalledUid(uid); - DelayRemovebundleUninstalledUid(uid); + DelayRemoveBundleUninstalledUid(uid); service->DeleteUninstalledBundleStats(bundleName, uid, appIndex); bundleGroupController_->OnBundleUninstalled(userId, bundleName, uid, appIndex); } -void BundleActiveCore::AddUidTobundleUninstalledUidSet(const int32_t uid) +void BundleActiveCore::AddbundleUninstalledUid(const int32_t uid) { std::lock_guard lock(bundleUninstalledMutex_); bundleUninstalledSet_.insert(uid); } -void BundleActiveCore::DelayRemoveUidTobundleUninstalledSet(const int32_t uid) +void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) { auto bundleActiveCore = shared_from_this(); ffrt::submit([bundleActiveCore, uid]() { - std::lock_guard lock(bundleUninstalledMutex_); + std::lock_guard lock(bundleActiveCore->bundleUninstalledMutex_); bundleActiveCore->bundleUninstalledSet_.erase(uid); }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); } -- Gitee From 64ba9471d800fd7d3b45fd7aaca9d2f39c2de418 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 29 Aug 2024 20:30:40 +0800 Subject: [PATCH 3/7] push Signed-off-by: fengyang --- interfaces/innerkits/include/bundle_active_event.h | 2 +- interfaces/innerkits/src/bundle_active_event.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 1531f6e..55a1c77 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -134,7 +134,7 @@ public: */ static bool IsBundleEvent(const int32_t eventId); - static bool IsAppStateEvent(const int32_t& eventId); + static bool IsAppStateEvent(const int32_t& eventId) }; } // namespace DeviceUsageStats diff --git a/interfaces/innerkits/src/bundle_active_event.cpp b/interfaces/innerkits/src/bundle_active_event.cpp index 88037c2..4f77968 100644 --- a/interfaces/innerkits/src/bundle_active_event.cpp +++ b/interfaces/innerkits/src/bundle_active_event.cpp @@ -209,7 +209,7 @@ bool BundleActiveEvent::IsBundleEvent(const int32_t eventId) return false; } -bool BundleActiveEvent::IsAppStateEvent(const int32_t eventId) +bool BundleActiveEvent::IsAppStateEvent(const int32_t& eventId) { if (eventId == ABILITY_BACKGROUND || eventId == ABILITY_FOREGROUND || eventId == ABILITY_STOP) { return true; -- Gitee From 3953e7d34c1d82241e0f89683f89517f0d204d6d Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 29 Aug 2024 20:41:37 +0800 Subject: [PATCH 4/7] push Signed-off-by: fengyang --- interfaces/innerkits/include/bundle_active_event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 55a1c77..1531f6e 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -134,7 +134,7 @@ public: */ static bool IsBundleEvent(const int32_t eventId); - static bool IsAppStateEvent(const int32_t& eventId) + static bool IsAppStateEvent(const int32_t& eventId); }; } // namespace DeviceUsageStats -- Gitee From 552eb1b573a4417c1eaa56e289279c4153d67e7e Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 30 Aug 2024 09:45:20 +0800 Subject: [PATCH 5/7] push Signed-off-by: fengyang --- test/unittest/package_usage_test.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index eeae093..83ca4a6 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -848,13 +848,21 @@ HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_004, Function | MediumT std::string bundleName = "test"; int32_t uid = 100010; int32_t appIndex = 1; - bundleActiveCore_->OnBundleUninstalled(userId, bundleName, uid, appIndex); - EXPECT_TRUE(bundleActiveCore_->isUninstalledApp(uid)); + int64_t timeNow = bundleActiveCore_->CheckTimeChangeAndGetWallTime(userId); BundleActiveReportHandlerObject tmpObject; tmpObject.event_.eventId_ = tmpObject.event_.ABILITY_STOP; tmpObject.event_.uid_ = uid; + tmpObject.event_.timeStamp_ = timeNow; auto handlerObject = std::make_shared(tmpObject); bundleActiveReportHandler->SendEvent(0, handlerObject); + auto service = bundleActiveCore_->GetUserDataAndInitializeIfNeeded(userId, timeNow, false); + EXPECT_EQ(service->currentStats_[0]->endTime_, timeNow); + bundleActiveCore_->OnBundleUninstalled(userId, bundleName, uid, appIndex); + EXPECT_TRUE(bundleActiveCore_->isUninstalledApp(uid)); + timeNow = timeNow + 100; + tmpObject.event_.timeStamp_ = timeNow; + bundleActiveReportHandler->SendEvent(0, handlerObject); + EXPECT_NE(service->currentStats_[0]->endTime_, timeNow); SUCCEED(); } -- Gitee From 66dfdf0b19b5d4f722ab557b81ed31661eed7259 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 30 Aug 2024 11:18:41 +0800 Subject: [PATCH 6/7] push Signed-off-by: fengyang --- interfaces/innerkits/include/bundle_active_event.h | 2 -- services/common/src/bundle_active_core.cpp | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 1531f6e..894f7a7 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -133,9 +133,7 @@ public: * @return return true if event reported by bundle usage. */ static bool IsBundleEvent(const int32_t eventId); - static bool IsAppStateEvent(const int32_t& eventId); - }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 941ea81..1f6861d 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -257,7 +257,7 @@ void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) ffrt::submit([bundleActiveCore, uid]() { std::lock_guard lock(bundleActiveCore->bundleUninstalledMutex_); bundleActiveCore->bundleUninstalledSet_.erase(uid); - }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); + }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); } bool BundleActiveCore::isUninstalledApp(const int32_t& uid) -- Gitee From 5b3c9b6d06fceba83ad9053afb81b30efa512f78 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 30 Aug 2024 15:54:08 +0800 Subject: [PATCH 7/7] push Signed-off-by: fengyang --- interfaces/innerkits/include/bundle_active_event.h | 2 +- interfaces/innerkits/src/bundle_active_event.cpp | 2 +- services/common/include/bundle_active_core.h | 2 +- services/common/src/bundle_active_core.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 894f7a7..4a3d691 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -133,7 +133,7 @@ public: * @return return true if event reported by bundle usage. */ static bool IsBundleEvent(const int32_t eventId); - static bool IsAppStateEvent(const int32_t& eventId); + static bool IsAppStateEvent(const int32_t eventId); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_event.cpp b/interfaces/innerkits/src/bundle_active_event.cpp index 4f77968..88037c2 100644 --- a/interfaces/innerkits/src/bundle_active_event.cpp +++ b/interfaces/innerkits/src/bundle_active_event.cpp @@ -209,7 +209,7 @@ bool BundleActiveEvent::IsBundleEvent(const int32_t eventId) return false; } -bool BundleActiveEvent::IsAppStateEvent(const int32_t& eventId) +bool BundleActiveEvent::IsAppStateEvent(const int32_t eventId) { if (eventId == ABILITY_BACKGROUND || eventId == ABILITY_FOREGROUND || eventId == ABILITY_STOP) { return true; diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 1565259..eec0a7f 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -242,7 +242,7 @@ public: int32_t currentUsedUser_; void OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo); - bool isUninstalledApp(const int32_t& uid); + bool isUninstalledApp(const int32_t uid); private: void NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 1f6861d..d172557 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -260,7 +260,7 @@ void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); } -bool BundleActiveCore::isUninstalledApp(const int32_t& uid) +bool BundleActiveCore::isUninstalledApp(const int32_t uid) { std::lock_guard lock(bundleUninstalledMutex_); if (bundleUninstalledSet_.find(uid) != bundleUninstalledSet_.end()) { -- Gitee