diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 6f878ee94a8642453b84e3f1543be7d445e77811..accab744cb18eeb7122d839e4dff3ce0bf303887 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -750,36 +750,43 @@ ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTo void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) { std::lock_guard lock(deathRecipientMutex_); - if (!observer || !(observer->AsObject())) { + if (!observer) { BUNDLE_ACTIVE_LOGI("observer nullptr."); return; } + auto object = observer->AsObject(); + if (!object) { + BUNDLE_ACTIVE_LOGI("observer->AsObject() nullptr."); + return; + } auto it = recipientMap_.find(observer->AsObject()); if (it != recipientMap_.end()) { BUNDLE_ACTIVE_LOGI("This death recipient has been added."); return; - } else { - sptr deathRecipient = new (std::nothrow) RemoteDeathRecipient( - [this](const wptr &remote) { this->OnObserverDied(remote); }); - if (!deathRecipient) { - BUNDLE_ACTIVE_LOGI("create death recipient failed"); - return ; - } - observer->AsObject()->AddDeathRecipient(deathRecipient); - recipientMap_.emplace(observer->AsObject(), deathRecipient); } + sptr deathRecipient = new (std::nothrow) RemoteDeathRecipient( + [this](const wptr &remote) { this->OnObserverDied(remote); }); + if (!deathRecipient) { + BUNDLE_ACTIVE_LOGI("create death recipient failed"); + return ; + } + observer->AsObject()->AddDeathRecipient(deathRecipient); + recipientMap_.emplace(observer->AsObject(), deathRecipient); } void BundleActiveCore::RemoveObserverDeathRecipient(const sptr &observer) { std::lock_guard lock(deathRecipientMutex_); - if (!observer || !(observer->AsObject())) { + if (!observer) { + return; + } + auto object = observer->AsObject(); + if (!object) { return ; } auto iter = recipientMap_.find(observer->AsObject()); if (iter != recipientMap_.end()) { iter->first->RemoveDeathRecipient(iter->second); recipientMap_.erase(iter); - return ; } } @@ -802,7 +809,14 @@ void BundleActiveCore::OnObserverDiedInner(const wptr &remote) return; } for (const auto& item : groupChangeObservers_) { - if ((item.second) && ((item.second)->AsObject()) && ((item.second)->AsObject() == objectProxy)) { + if (!(item.second)) { + continue; + } + auto object = (item.second)->AsObject(); + if (!object) { + continue; + } + if (object == objectProxy) { groupChangeObservers_.erase(item.first); break; } diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index a0b50486b14fcb568dabb350e7e367cf9143e838..8ef02fcf3f895ea2dcbaae51776d2840f2ae1c87 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -625,7 +625,8 @@ ErrCode BundleActiveService::QueryNotificationEventStats(int64_t beginTime, int6 void BundleActiveService::QueryModuleRecordInfos(BundleActiveModuleRecord& moduleRecord) { - if (!GetBundleMgrProxy()) { + ErrCode errCode = GetBundleMgrProxy(); + if (errCode != ERR_OK) { return; } ApplicationInfo appInfo; diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index dbbd6db3be030c19310f05af454ac4553a5b21a7..f3da15696c7e659a4914473b89d8d40f60b063fc 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -79,11 +79,11 @@ private: std::mutex mutex_; bool GetBundleMgrProxy(); std::weak_ptr activeGroupHandler_; - uint32_t EventToGroupReason(const int32_t eventId); int64_t timeoutForDirectlyUse_; int64_t timeoutForNotifySeen_; int64_t timeoutForSystemInteraction_; int64_t timeoutCalculated_ = 0; + std::map eventIdMatchReason_; sptr sptrBundleMgr_; bool calculationTimeOut(const std::shared_ptr& oneBundleHistory, const int64_t bootBasedTimeStamp); diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index aceddcdeb38126eabb89161e7b3806fb19e18d60..d096cd91f55cb48f62af142db9116d48627941c9 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -38,6 +38,14 @@ BundleActiveGroupController::BundleActiveGroupController(const bool debug) screenTimeLevel_ = {0, 0, debug ? TWO_MINUTE : ONE_HOUR, debug ? FOUR_MINUTE : TWO_HOUR}; bootTimeLevel_ = {0, debug ? TWO_MINUTE : TWELVE_HOUR, debug ? FOUR_MINUTE : TWENTY_FOUR_HOUR, debug ? SIXTEEN_MINUTE : FOURTY_EIGHT_HOUR}; + eventIdMatchReason_ = { + {BundleActiveEvent::ABILITY_FOREGROUND, GROUP_EVENT_REASON_FOREGROUND}, + {BundleActiveEvent::ABILITY_BACKGROUND, GROUP_EVENT_REASON_BACKGROUND}, + {BundleActiveEvent::SYSTEM_INTERACTIVE, GROUP_EVENT_REASON_SYSTEM}, + {BundleActiveEvent::USER_INTERACTIVE, GROUP_EVENT_REASON_USER_INTERACTION}, + {BundleActiveEvent::NOTIFICATION_SEEN, GROUP_EVENT_REASON_NOTIFY_SEEN}, + {BundleActiveEvent::LONG_TIME_TASK_STARTTED, GROUP_EVENT_REASON_LONG_TIME_TASK_STARTTED}, + }; } void BundleActiveGroupController::RestoreDurationToDatabase() @@ -195,26 +203,6 @@ bool BundleActiveGroupController::calculationTimeOut( - lastGroupCalculatedTimeStamp > timeoutCalculated_; } -uint32_t BundleActiveGroupController::EventToGroupReason(const int32_t eventId) -{ - switch (eventId) { - case BundleActiveEvent::ABILITY_FOREGROUND: - return GROUP_EVENT_REASON_FOREGROUND; - case BundleActiveEvent::ABILITY_BACKGROUND: - return GROUP_EVENT_REASON_BACKGROUND; - case BundleActiveEvent::SYSTEM_INTERACTIVE: - return GROUP_EVENT_REASON_SYSTEM; - case BundleActiveEvent::USER_INTERACTIVE: - return GROUP_EVENT_REASON_USER_INTERACTION; - case BundleActiveEvent::NOTIFICATION_SEEN: - return GROUP_EVENT_REASON_NOTIFY_SEEN; - case BundleActiveEvent::LONG_TIME_TASK_STARTTED: - return GROUP_EVENT_REASON_LONG_TIME_TASK_STARTTED; - default: - return 0; - } -} - void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp, const int32_t userId) { @@ -227,19 +215,15 @@ void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, co return; } int32_t eventId = event.eventId_; - if (eventId == BundleActiveEvent::ABILITY_FOREGROUND || - eventId == BundleActiveEvent::ABILITY_BACKGROUND || - eventId == BundleActiveEvent::SYSTEM_INTERACTIVE || - eventId == BundleActiveEvent::USER_INTERACTIVE || - eventId == BundleActiveEvent::NOTIFICATION_SEEN || - eventId == BundleActiveEvent::LONG_TIME_TASK_STARTTED) { + auto item = eventIdMatchReason_.find(eventId); + if (item != eventIdMatchReason_.end()) { std::shared_ptr bundleUsageHistory = bundleUserHistory_->GetUsageHistoryForBundle( event.bundleName_, userId, bootBasedTimeStamp, true); if (bundleUsageHistory == nullptr) { return; } int64_t timeUntilNextCheck; - uint32_t eventReason = EventToGroupReason(eventId); + uint32_t eventReason = item->second; switch (eventId) { case BundleActiveEvent::NOTIFICATION_SEEN: bundleUserHistory_->ReportUsage(bundleUsageHistory, event.bundleName_, ACTIVE_GROUP_DAILY, diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 9a4c4addffcbd22c4d89e2545516573dedae81cd..c7d6f12a367c55dde9187dab23723f7b5f19fa0f 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -185,6 +185,16 @@ HWTEST_F(DeviceUsageStatisticsTest, int32_t errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); EXPECT_EQ(errCode, 0); EXPECT_EQ(results.size(), 0); + + results.clear(); + maxNum = 0; + errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); + EXPECT_NE(errCode, 0); + + results.clear(); + maxNum = 1001; + errCode = BundleActiveClient::GetInstance().QueryModuleUsageRecords(maxNum, results, DEFAULT_USERID); + EXPECT_NE(errCode, 0); } /* @@ -218,6 +228,9 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_SetAppGroup_001, F DEFAULT_GROUP = (result == DEFAULT_GROUP) ? (result + 10) : DEFAULT_GROUP; result = BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, COMMON_USERID); EXPECT_EQ(result, DEFAULT_ERRCODE); + + result = BundleActiveClient::GetInstance().SetAppGroup(g_defaultBundleName, DEFAULT_GROUP, -1); + EXPECT_NE(result, DEFAULT_ERRCODE); } /* @@ -293,8 +306,7 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryNotificationE * @tc.type: FUNC * @tc.require: SR000H0G4F AR000H2US8 */ -HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveGroupMap_001, Function - | MediumTest | Level0) +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveGroupMap_001, Function | MediumTest | Level0) { int64_t minInterval = DeviceUsageStatsGroupMap::groupIntervalMap_ .at(DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET); @@ -319,8 +331,7 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveGroupM * @tc.type: FUNC * @tc.require: issuesI5SOZY */ -HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_DeathRecipient_001, Function - | MediumTest | Level0) +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_DeathRecipient_001, Function | MediumTest | Level0) { auto deathTest = std::make_shared(); EXPECT_TRUE(deathTest!=nullptr);