diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index d5fc211c87984202f932245ddca4afaadd1e17df..99d372871cc18455cd47ee84e5f707994b009c0f 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -125,6 +125,7 @@ public: // when service stop, call it to unregister commen event and shutdown call back. void UnRegisterSubscriber(); int64_t GetSystemTimeMs(); + int currentUsedUser_; private: static const int64_t FLUSH_INTERVAL = TWO_MINUTE; @@ -142,7 +143,6 @@ private: void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; void RestoreAllData(); - int lastUsedUser_; }; } } diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 1d431ec773eba40d739eba0ecf080536e880662f..11ff64a7dec545c24d2748e459b857e0f0134a3f 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -23,6 +23,7 @@ namespace OHOS { namespace DeviceUsageStats { +const std::string LAUNCHER_BUNDLE_NAME = "com.ohos.launcher"; BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() { userId_ = -1; @@ -51,7 +52,7 @@ BundleActiveCore::BundleActiveCore() { systemTimeShot_ = -1; realTimeShot_ = -1; - lastUsedUser_ = -1; + currentUsedUser_ = -1; } BundleActiveCore::~BundleActiveCore() @@ -223,9 +224,8 @@ void BundleActiveCore::OnStatsChanged(const int userId) std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr); - if (handler_.lock()->HasInnerEvent(static_cast(BundleActiveReportHandler::MSG_FLUSH_TO_DISK)) == - false) { - BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event"); + if (handler_.lock()->HasInnerEvent(static_cast(userId)) == false) { + BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event for user %{public}d", userId); handler_.lock()->SendEvent(event, FLUSH_INTERVAL); } } @@ -281,7 +281,7 @@ void BundleActiveCore::RestoreToDatabaseLocked(const int userId) } if (!handler_.expired()) { BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); - handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, userId); } } @@ -344,7 +344,7 @@ int64_t BundleActiveCore::CheckTimeChangeAndGetWallTime(int userId) it->second->LoadActiveStats(actualSystemTime, true, true); if (!handler_.expired()) { BUNDLE_ACTIVE_LOGI("CheckTimeChangeAndGetWallTime remove flush to disk event"); - handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, userId); } } realTimeShot_ = actualRealTime; @@ -376,10 +376,10 @@ void BundleActiveCore::OnUserRemoved(const int userId) void BundleActiveCore::OnUserSwitched(const int userId) { sptr timer = MiscServices::TimeServiceClient::GetInstance(); - auto it = userStatServices_.find(lastUsedUser_); + auto it = userStatServices_.find(currentUsedUser_); if (it != userStatServices_.end()) { if (it != userStatServices_.end()) { - BUNDLE_ACTIVE_LOGI("restore old user id %{public}d data when switch user", lastUsedUser_); + BUNDLE_ACTIVE_LOGI("restore old user id %{public}d data when switch user", currentUsedUser_); BundleActiveEvent event; event.eventId_ = BundleActiveEvent::FLUSH; int64_t actualRealTime = timer->GetBootTimeMs(); @@ -389,6 +389,10 @@ void BundleActiveCore::OnUserSwitched(const int userId) it->second->RestoreStats(true); } } + if (!handler_.expired()) { + BUNDLE_ACTIVE_LOGI("OnUserSwitched remove flush to disk event"); + handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, currentUsedUser_); + } std::vector activatedOsAccountIds; GetAllActiveUser(activatedOsAccountIds); if (activatedOsAccountIds.size() == 0) { @@ -397,9 +401,9 @@ void BundleActiveCore::OnUserSwitched(const int userId) } for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { BUNDLE_ACTIVE_LOGI("start to period check for userId %{public}d", activatedOsAccountIds[i]); - bundleGroupController_->OnUserSwitched(activatedOsAccountIds[i]); + bundleGroupController_->OnUserSwitched(activatedOsAccountIds[i], currentUsedUser_); } - lastUsedUser_ = userId; + currentUsedUser_ = userId; OnStatsChanged(userId); } @@ -409,18 +413,25 @@ int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) if (userId == 0 || userId == -1) { return -1; } - if (lastUsedUser_ == -1) { - lastUsedUser_ = userId; - BUNDLE_ACTIVE_LOGI("last used id change to %{public}d", lastUsedUser_); + if (currentUsedUser_ == -1) { + currentUsedUser_ = userId; + BUNDLE_ACTIVE_LOGI("last used id change to %{public}d", currentUsedUser_); } + + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); + if (event.bundleName_ == LAUNCHER_BUNDLE_NAME) { + BUNDLE_ACTIVE_LOGI("launcher event, only update app group"); + bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId); + return 0; + } + BUNDLE_ACTIVE_LOGI("report event called bundle name %{public}s time %{public}lld userId %{public}d, " "eventid %{public}d, in lock range", event.bundleName_.c_str(), event.timeStamp_, userId, event.eventId_); - sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == -1) { return -1; } - int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); ConvertToSystemTimeLocked(event); std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow); if (service == nullptr) { diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index 90c03cb291af3d569251447c90284deb2c9e986f..ee2f45c3d3dd46ea63c44921d6f983f026b1178b 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -84,7 +84,7 @@ public: int IsBundleIdle(const std::string& bundleName, const int userId); int QueryPackageGroup(const int userId, const std::string& bundleName); void ShutDown(const int64_t bootBasedTimeStamp, const int userId); - void OnUserSwitched(const int userId); + void OnUserSwitched(const int userId, const int lastUsedUser); private: std::mutex mutex_; diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 97b32975d019f353732c15f573b9b6a7e261d0d5..92783290468dc26ad8515d1f58b74a4d9aa11e20 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -50,8 +50,11 @@ void BundleActiveGroupController::OnUserRemoved(const int userId) } } -void BundleActiveGroupController::OnUserSwitched(const int userId) +void BundleActiveGroupController::OnUserSwitched(const int userId, const int lastUsedUser) { + BUNDLE_ACTIVE_LOGI("last time check for user %{public}d", lastUsedUser); + CheckEachBundleState(lastUsedUser); + bundleUserHistory_->WriteBundleUsage(lastUsedUser); std::lock_guard lock(mutex_); if (!activeGroupHandler_.expired()) { activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); @@ -282,14 +285,12 @@ void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleN bootBasedTimeStampAdjusted) { newGroup = ACTIVE_GROUP_ALIVE; groupReason = oneBundleHistory->reasonInGroup_; - groupReason = (newGroup == oldGroup) ? oneBundleHistory->reasonInGroup_ : GROUP_CONTROL_REASON_USAGE | - GROUP_EVENT_REASON_ALIVE_NOT_TIMEOUT; + groupReason = GROUP_CONTROL_REASON_USAGE | GROUP_EVENT_REASON_ALIVE_NOT_TIMEOUT; notTimeout = true; } else if (newGroup >= ACTIVE_GROUP_DAILY && oneBundleHistory->bundleDailyTimeoutTimeStamp_ > bootBasedTimeStampAdjusted) { newGroup = ACTIVE_GROUP_DAILY; - groupReason = (newGroup == oldGroup) ? oneBundleHistory->reasonInGroup_ : GROUP_CONTROL_REASON_USAGE | - GROUP_EVENT_REASON_ALIVE_TIMEOUT; + groupReason = GROUP_CONTROL_REASON_USAGE | GROUP_EVENT_REASON_ALIVE_TIMEOUT; notTimeout = true; } if (oldGroup < newGroup || notTimeout) { diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index ff45231868fdf2db32b783bbc47f0aaf3623e08a..2b4474873dafc6ecda277f8d80ba055a0bf5706b 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -54,19 +54,18 @@ void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointe break; } case MSG_ONE_TIME_CHECK_BUNDLE_STATE: { - std::vector osAccountInfos; - OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::GetAllActiveUser failed"); + std::vector activatedOsAccountIds; + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { + BUNDLE_ACTIVE_LOGI("query activated account failed"); return; } - if (osAccountInfos.size() == 0) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::GetAllActiveUser size is 0"); + if (activatedOsAccountIds.size() == 0) { + BUNDLE_ACTIVE_LOGI("GetAllActiveUser size is 0"); return; } - for (uint32_t i = 0; i < osAccountInfos.size(); i++) { - bundleActiveGroupController_->CheckEachBundleState(osAccountInfos[i].GetLocalId()); - bundleActiveGroupController_->RestoreToDatabase(osAccountInfos[i].GetLocalId()); + for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { + bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); + bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); } RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE); break; diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index e1d59b5179dc0cb95a23200d4b7d94fe5e27b6b4..3d0f0f90f67710bf9a3abdd4c8cbd0513a0aee03 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -197,6 +197,10 @@ void BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int if (oneBundleHistory == nullptr) { return; } + if (oneBundleHistory->currentGroup_ == newGroup || oneBundleHistory->reasonInGroup_ == groupReason) { + BUNDLE_ACTIVE_LOGI("%{public}s group and reason is same as before, not update", bundleName.c_str()); + return; + } oneBundleHistory->currentGroup_ = newGroup; oneBundleHistory->reasonInGroup_ = groupReason; int64_t setTimeStamp = GetBootBasedTimeStamp(bootBasedTimeStamp); diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index ad2714dedaff5416d998b5e04be44b9c2cb3d975..f0fcdaa7349a379b56d23c8659f815d64ea689f2 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -36,11 +36,10 @@ public: void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; void Init(const std::shared_ptr& bundleActiveCore); static const int MSG_REPORT_EVENT = 0; - static const int MSG_REPORT_EVENT_TO_ALL_USER = 1; - static const int MSG_FLUSH_TO_DISK = 2; - static const int MSG_REMOVE_USER = 3; - static const int MSG_BUNDLE_UNINSTALLED = 4; - static const int MSG_SWITCH_USER = 5; + static const int MSG_FLUSH_TO_DISK = 1; + static const int MSG_REMOVE_USER = 2; + static const int MSG_BUNDLE_UNINSTALLED = 3; + static const int MSG_SWITCH_USER = 4; private: std::shared_ptr bundleActiveCore_; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 3fc4a797a813b1bd92f892c6656dd1769b14b6e3..e11680911999efc7e87570fbf612aebc2b479156 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -42,16 +42,16 @@ void BundleActiveReportHandler::ProcessEvent(const AppExecFwk::InnerEvent::Point bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); break; } - case MSG_REPORT_EVENT_TO_ALL_USER: { - auto ptrToHandlerobj = event->GetSharedObject(); - BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; - bundleActiveCore_->ReportEventToAllUserId(tmpHandlerobj.event_); - break; - } case MSG_FLUSH_TO_DISK: { BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); auto ptrToHandlerobj = event->GetSharedObject(); BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; + 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, tmpHandlerobj.userId_); + return; + } bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_); break; } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 9682bbc69ee390a4378825e5fddc1a7680ff7cb6..931d54f32992f3fb2f2507a321b15b1a25c12251 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -71,6 +71,7 @@ void BundleActiveUserService::RenewTableTime(int64_t oldTime, int64_t newTime) void BundleActiveUserService::NotifyStatsChanged() { + BUNDLE_ACTIVE_LOGI("NotifyStatsChanged stat change is %{public}d, user is %{public}d", statsChanged_, userId_); if (!statsChanged_) { BUNDLE_ACTIVE_LOGI("NotifyStatsChanged() set stats changed to true"); statsChanged_ = true; @@ -85,8 +86,9 @@ void BundleActiveUserService::NotifyNewUpdate() void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) { - BUNDLE_ACTIVE_LOGI("ReportEvent, B time is %{public}lld, E time is %{public}lld", - currentStats_[0]->beginTime_, dailyExpiryDate_.GetMilliseconds()); + BUNDLE_ACTIVE_LOGI("ReportEvent, B time is %{public}lld, E time is %{public}lld, userId is %{public}d," + "event is %{public}d", + currentStats_[0]->beginTime_, dailyExpiryDate_.GetMilliseconds(), userId_, event.eventId_); if (event.timeStamp_ >= dailyExpiryDate_.GetMilliseconds()) { BUNDLE_ACTIVE_LOGI("ReportEvent later than daily expire, renew data in memory"); RenewStatsInMemory(event.timeStamp_); @@ -131,7 +133,9 @@ void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) break; } } - NotifyStatsChanged(); + if (event.eventId_ != BundleActiveEvent::FLUSH) { + NotifyStatsChanged(); + } } void BundleActiveUserService::ReportForShutdown(const BundleActiveEvent& event) @@ -170,6 +174,7 @@ void BundleActiveUserService::RestoreStats(bool forced) } currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->events_.Clear(); statsChanged_ = false; + BUNDLE_ACTIVE_LOGI("change statsChanged_ to %{public}d user is %{public}d", statsChanged_, userId_); } } @@ -260,6 +265,9 @@ void BundleActiveUserService::RenewStatsInMemory(const int64_t timeStamp) if (continueAbilities.find(continueBundleName) != continueAbilities.end()) { for (std::map::iterator it = continueAbilities[continueBundleName].begin(); it != continueAbilities[continueBundleName].end(); it++) { + if (it->second == BundleActiveEvent::ABILITY_BACKGROUND) { + continue; + } (*itInterval)->Update(continueBundleName, "", beginTime, it->second, it->first); } }