diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 5464548fdc4cb98683a4719612120e3afe075c5f..928e667f5814ae4afff19b83eeb2169ae8ced883 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -116,10 +116,11 @@ public: const int64_t timeStamp); // when received a USER_REMOVED commen event, call it to remove data. void OnUserRemoved(const int userId); + void OnUserSwitched(); // force set app group. void SetBundleGroup(const std::string& bundleName, const int newGroup, const int userId); // get all user in device - void GetAllActiveUser(std::vector &osAccountInfos); + void GetAllActiveUser(std::vector& activatedOsAccountIds); // when service stop, call it to unregister commen event and shutdown call back. void UnRegisterSubscriber(); int64_t GetSystemTimeMs(); @@ -140,6 +141,7 @@ 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 842ebfa6cd2964d94d8c162d0b15e8e16c4e0ee8..31b768e4d609928663ca265fd5983534451c7697 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -45,6 +45,7 @@ BundleActiveCore::BundleActiveCore() { systemTimeShot_ = -1; realTimeShot_ = -1; + lastUsedUser_ = -1; } BundleActiveCore::~BundleActiveCore() @@ -77,12 +78,11 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); bundleActiveReportHandler_.lock()->SendEvent(event); } - } else if (action == CommonEventSupport::COMMON_EVENT_USER_ADDED) { + } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { int32_t userId = data.GetCode(); - BUNDLE_ACTIVE_LOGI("OnReceiveEvent receive add user event, user id is %{public}d", userId); - if (!activeGroupController_.expired() && userId >= 0) { - activeGroupController_.lock()->PeriodCheckBundleState(userId); - } + BUNDLE_ACTIVE_LOGI("OnReceiveEvent receive switched user event, user id is %{public}d", userId); + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_SWITCH_USER); + bundleActiveReportHandler_.lock()->SendEvent(event); } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { int32_t userId = data.GetWant().GetIntParam("userId", 0); @@ -109,7 +109,7 @@ void BundleActiveCore::RegisterSubscriber() matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_ADDED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED); @@ -157,11 +157,15 @@ void BundleActiveCore::InitBundleGroupController() BUNDLE_ACTIVE_LOGI("Init Set group controller and handler done"); } RegisterSubscriber(); - std::vector osAccountInfos; - GetAllActiveUser(osAccountInfos); + std::vector activatedOsAccountIds; bundleGroupController_->bundleGroupEnable_ = true; - for (uint32_t i = 0; i < osAccountInfos.size(); i++) { - bundleGroupController_->PeriodCheckBundleState(osAccountInfos[i].GetLocalId()); + GetAllActiveUser(activatedOsAccountIds); + if (activatedOsAccountIds.size() == 0) { + BUNDLE_ACTIVE_LOGI("query activated account failed, no account activated"); + return; + } + for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { + bundleGroupController_->PeriodCheckBundleState(activatedOsAccountIds[i]); } } @@ -352,9 +356,37 @@ void BundleActiveCore::OnUserRemoved(const int userId) bundleGroupController_->OnUserRemoved(userId); } +void BundleActiveCore::OnUserSwitched() +{ + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + auto it = userStatServices_.find(lastUsedUser_); + if (it != userStatServices_.end()) { + if (it != userStatServices_.end()) { + BundleActiveEvent event; + event.eventId_ = BundleActiveEvent::FLUSH; + int64_t actualRealTime = timer->GetBootTimeMs(); + event.timeStamp_ = (actualRealTime - realTimeShot_) + systemTimeShot_; + event.abilityId_ = ""; + it->second->ReportEvent(event); + it->second->RestoreStats(true); + } + } + std::vector activatedOsAccountIds; + GetAllActiveUser(activatedOsAccountIds); + if (activatedOsAccountIds.size() == 0) { + BUNDLE_ACTIVE_LOGI("query activated account failed, no account activated"); + return; + } + 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]); + } +} + int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) { std::lock_guard lock(mutex_); + lastUsedUser_ = userId; if (userId == 0) { return -1; } @@ -468,14 +500,13 @@ int BundleActiveCore::IsBundleIdle(const std::string& bundleName, const int user return bundleGroupController_->IsBundleIdle(bundleName, userId); } -void BundleActiveCore::GetAllActiveUser(std::vector &osAccountInfos) +void BundleActiveCore::GetAllActiveUser(std::vector& activatedOsAccountIds) { - OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGI("GetAllActiveUser failed"); + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds) != ERR_OK) { + BUNDLE_ACTIVE_LOGI("query activated account failed"); return; } - if (osAccountInfos.size() == 0) { + if (activatedOsAccountIds.size() == 0) { BUNDLE_ACTIVE_LOGI("GetAllActiveUser size is 0"); return; } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index 1de660fc0c4a8a0f29a6fd540407a3ec577e894a..67632020262716fde8046e027b883beb1963e9f4 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -84,6 +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); + void OnUserSwitched(const int userId); 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 f89d4bf08a94e3bfd1a4e55ec13d40f18c4b9515..519311f47333923321dccdb69892355d83bf9704 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -45,6 +45,18 @@ void BundleActiveGroupController::OnUserRemoved(const int userId) { std::lock_guard lock(mutex_); bundleUserHistory_->userHistory_.erase(userId); + if (!activeGroupHandler_.expired()) { + activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + } +} + +void BundleActiveGroupController::OnUserSwitched(const int userId) +{ + std::lock_guard lock(mutex_); + if (!activeGroupHandler_.expired()) { + activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + } + PeriodCheckBundleState(userId); } void BundleActiveGroupController::OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp) diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index bc9ea44107018c56147b28fc1a0706073fd5393a..ad2714dedaff5416d998b5e04be44b9c2cb3d975 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -39,8 +39,8 @@ public: 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_DEVICE_SHUTDOWN = 4; - static const int MSG_BUNDLE_UNINSTALLED = 5; + static const int MSG_BUNDLE_UNINSTALLED = 4; + static const int MSG_SWITCH_USER = 5; private: std::shared_ptr bundleActiveCore_; diff --git a/services/packageusage/include/bundle_active_user_service.h b/services/packageusage/include/bundle_active_user_service.h index 6aa13e79464ec01e237502bc8d9580eb69d15800..2bce5f34f9952224ef37e5c54a53702fbecffbe9 100644 --- a/services/packageusage/include/bundle_active_user_service.h +++ b/services/packageusage/include/bundle_active_user_service.h @@ -71,7 +71,8 @@ private: BundleActiveCalendar::YEAR_MILLISECONDS}; void NotifyStatsChanged(); void NotifyNewUpdate(); - void printstat(); + void PrintInMemPackageStats(const int idx); + void PrintInMemEventStats(); }; } } diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index d02a40d18cdee7cbb6443cd1c4810529c6789aa5..33b734d06dbd952647057e5e421bc409a328da1a 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -61,10 +61,6 @@ void BundleActiveReportHandler::ProcessEvent(const AppExecFwk::InnerEvent::Point bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); break; } - case MSG_DEVICE_SHUTDOWN: { - bundleActiveCore_->ShutDown(); - break; - } case MSG_BUNDLE_UNINSTALLED: { BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); auto ptrToHandlerobj = event->GetSharedObject(); @@ -72,6 +68,9 @@ void BundleActiveReportHandler::ProcessEvent(const AppExecFwk::InnerEvent::Point bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_); break; } + case MSG_SWITCH_USER: { + bundleActiveCore_->OnUserSwitched(); + } default: { break; } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 4686d761c353be675076c5f65eec21066476da10..ed0dd0f6b0a2a782ff3c0b707c88017bab79dddb 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -304,7 +304,12 @@ std::vector BundleActiveUserService::QueryPackageStats return result; } int64_t truncatedEndTime = std::min(currentStats->beginTime_, endTime); + BUNDLE_ACTIVE_LOGI("Query package data in db from %{public}lld to %{public}lld, current begin %{public}lld", + beginTime, truncatedEndTime, currentStats->beginTime_); result = database_.QueryDatabaseUsageStats(intervalType, beginTime, truncatedEndTime, userId); + BUNDLE_ACTIVE_LOGI("Query package data in db result size is %{public}d", + static_cast(result.size())); + PrintInMemPackageStats(intervalType); // if we need a in-memory stats, combine current stats with result from database. if (currentStats->endTime_ != 0 && endTime > currentStats->beginTime_) { BUNDLE_ACTIVE_LOGI("QueryPackageStats need in memory stats"); @@ -339,9 +344,10 @@ std::vector BundleActiveUserService::QueryEvents(const int64_ if (beginTime >= currentStats->endTime_) { return result; } - BUNDLE_ACTIVE_LOGI("QueryEvents bundle name is %{public}s", bundleName.c_str()); + BUNDLE_ACTIVE_LOGI("Query event bundle name is %{public}s", bundleName.c_str()); result = database_.QueryDatabaseEvents(beginTime, endTime, userId, bundleName); - BUNDLE_ACTIVE_LOGI("event database query size is %{public}d", result.size()); + BUNDLE_ACTIVE_LOGI("Query event data in db result size is %{public}d", result.size()); + PrintInMemEventStats(); // if we need a in-memory stats, combine current stats with result from database. if (currentStats->endTime_ != 0 && endTime > currentStats->beginTime_) { BUNDLE_ACTIVE_LOGI("QueryEvents need in memory stats"); @@ -358,17 +364,25 @@ std::vector BundleActiveUserService::QueryEvents(const int64_ return result; } -void BundleActiveUserService::printstat() +void BundleActiveUserService::PrintInMemPackageStats(const int idx) { - BUNDLE_ACTIVE_LOGI("printstat called"); - int idx = 0; + BUNDLE_ACTIVE_LOGI("PrintInMemPackageStats called"); for (auto it : currentStats_[idx]->bundleStats_) { - BUNDLE_ACTIVE_LOGI("bundle name is %{public}s", it.first.c_str()); - int64_t lasttimeused = it.second->lastTimeUsed_; - int64_t totalusedtime = it.second->totalInFrontTime_; - BUNDLE_ACTIVE_LOGI("event stat is, totaltime is %{public}lld, lasttimeused is %{public}lld", - totalusedtime, lasttimeused); + BUNDLE_ACTIVE_LOGI("In mem, bundle name is %{public}s", it.first.c_str()); + int64_t lastTimeUsed = it.second->lastTimeUsed_; + int64_t totalUsedTime = it.second->totalInFrontTime_; + int64_t lastTimeContinuousTaskUsed = it.second->lastContiniousTaskUsed_; + int64_t totalTimeContinuousTaskUsed = it.second->totalContiniousTaskUsedTime_; + BUNDLE_ACTIVE_LOGI("In mem, event stat is, totaltime is %{public}lld, lastTimeUsed is %{public}lld" + "total continuous task is %{public}lld, lastTimeContinuousTaskUsed is %{public}lld", + totalUsedTime, lastTimeUsed, totalTimeContinuousTaskUsed, lastTimeContinuousTaskUsed); } +} + +void BundleActiveUserService::PrintInMemEventStats() +{ + BUNDLE_ACTIVE_LOGI("PrintInMemEventStats called"); + int idx = 0; int size = static_cast(currentStats_[idx]->events_.events_.size()); for (int i = 0; i < size; i++) { std::string abilityId = currentStats_[idx]->events_.events_[i].abilityId_; @@ -376,7 +390,7 @@ void BundleActiveUserService::printstat() std::string bundlename = currentStats_[idx]->events_.events_[i].bundleName_; int eventid = currentStats_[idx]->events_.events_[i].eventId_; int64_t timestamp = currentStats_[idx]->events_.events_[i].timeStamp_; - BUNDLE_ACTIVE_LOGI("event stat is, abilityid is %{public}s, abilityname is %{public}s, " + BUNDLE_ACTIVE_LOGI("In mem, event stat is, abilityid is %{public}s, abilityname is %{public}s, " "bundlename is %{public}s, eventid is %{public}d, timestamp is %{public}lld", abilityId.c_str(), abilityname.c_str(), bundlename.c_str(), eventid, timestamp); }