diff --git a/interfaces/innerkits/include/bundle_active_event.h b/interfaces/innerkits/include/bundle_active_event.h index 964ed454dc5ad45d9d3d8d5c652612e9f3e06c25..4a3d69161c54d5a21261d9da9b8b083b25e28110 100644 --- a/interfaces/innerkits/include/bundle_active_event.h +++ b/interfaces/innerkits/include/bundle_active_event.h @@ -133,6 +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/interfaces/innerkits/src/bundle_active_event.cpp b/interfaces/innerkits/src/bundle_active_event.cpp index 2eb1a1f7e71a13079c3660eba6941e71bd814ef4..88037c299ce5be9d2833efef8460723455459653 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 a9ea52e85468361dc19f8a462c58625c144ebd7f..eec0a7f19e3ce1df78d8ede0f25f1b20f8e64dd9 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 1bb14cda73e3892fb4b2ed0433275ec62c08dd91..d1725575247fafcc60c94b05714f10f45d2cee37 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -29,6 +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; 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::AddbundleUninstalledUid(const int32_t uid) +{ + std::lock_guard lock(bundleUninstalledMutex_); + bundleUninstalledSet_.insert(uid); +} + +void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) +{ + auto bundleActiveCore = shared_from_this(); + ffrt::submit([bundleActiveCore, uid]() { + std::lock_guard lock(bundleActiveCore->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 52785c83c83da8e878fb0b540de3e68c1915db8b..db03602b45fd80341481b0e5241622d3b3231082 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 8c97e159d38dba21295651cdb904f137c28ae509..83ca4a62c0254c9843dd59cffd1f01b493a864d7 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -834,6 +834,38 @@ 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; + 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(); +} + /* * @tc.name: BundleActiveGroupHandler_001 * @tc.desc: SendEvent