diff --git a/init/device_usage_statistics_service.cfg b/init/device_usage_statistics_service.cfg index c197a766062cea3140d3e620420fd3f8333ac90c..2154baa1e2dff29beb89622ab33639c0a52f9337 100644 --- a/init/device_usage_statistics_service.cfg +++ b/init/device_usage_statistics_service.cfg @@ -9,8 +9,8 @@ "services" : [{ "name" : "device_usage_stats_service", "path" : ["/system/bin/sa_main", "/system/profile/device_usage_stats_service.xml"], - "uid" : "root", - "gid" : ["root", "shell"] + "uid" : "system", + "gid" : ["system", "shell"] } ] } \ No newline at end of file diff --git a/services/common/include/bundle_active_constant.h b/services/common/include/bundle_active_constant.h index 2e3bd8c1bfed63c7b52c341cefd83c765f409b31..721da942d9d5384e6bb4d4aab6a3564c23386298 100644 --- a/services/common/include/bundle_active_constant.h +++ b/services/common/include/bundle_active_constant.h @@ -59,7 +59,8 @@ const int BOOT_BASED_DURATION_COLUMN_INDEX = 0; const int SCREEN_ON_DURATION_COLUMN_INDEX = 1; const int DURATION_FLAG_COLUMN_INDEX = 2; const int TWO_SECONDS = 2 * 1000; -const int64_t SIX_DAY_IN_MILLIS_MAX = 1 * 6 * 10 * 60 * 1000; +const int64_t SIX_DAY_IN_MILLIS_MAX = 6 * 24 * 60 * 60 * 1000; +const int64_t THIRTY_MINUTE = 30 * 60 * 1000; const int64_t LAST_TIME_IN_MILLIS_MIN = 0; const int64_t EVENT_TIME_IN_MILLIS_MIN = 0; const int64_t EVENT_BEGIN_TIME_INITIAL_VALUE = -1; diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index d5fc211c87984202f932245ddca4afaadd1e17df..bd1601e14853a6b1ac3706ecafe7acd89fc8f846 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -125,9 +125,10 @@ 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; + static const int64_t FLUSH_INTERVAL = THIRTY_MINUTE; static const int64_t TIME_CHANGE_THRESHOLD_MILLIS = TWO_SECONDS; const int DEFAULT_USER_ID = -1; std::map visibleActivities_; @@ -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 ef0ade27dacedeaab9e8c1caa532483d8f7a06ea..65d56b81a3db6bad20aa01bd479662bf18aca839 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -52,7 +52,7 @@ BundleActiveCore::BundleActiveCore() { systemTimeShot_ = -1; realTimeShot_ = -1; - lastUsedUser_ = -1; + currentUsedUser_ = -1; } BundleActiveCore::~BundleActiveCore() @@ -224,8 +224,7 @@ 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) { + 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); } @@ -282,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); } } @@ -345,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; @@ -377,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(); @@ -402,9 +401,10 @@ 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], lastUsedUser_); + bundleGroupController_->OnUserSwitched(activatedOsAccountIds[i], currentUsedUser_); } - lastUsedUser_ = userId; + currentUsedUser_ = userId; + OnStatsChanged(userId); } int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) @@ -413,9 +413,9 @@ 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(); diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index ee2f45c3d3dd46ea63c44921d6f983f026b1178b..63d3b05d282e140e7a206e8329aa1bab6395b93a 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -43,7 +43,7 @@ public: using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag; OHOS::AppExecFwk::ApplicationFlag flag = OHOS::AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO; bool bundleGroupEnable_ = true; - bool debug_ = true; + bool debug_ = false; const int LEVEL_GROUP[4] = { ACTIVE_GROUP_ALIVE, ACTIVE_GROUP_DAILY, @@ -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, const int lastUsedUser); + void OnUserSwitched(const int userId, const int currentUsedUser); private: std::mutex mutex_; diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index f498c65773227982a51b42a1ec4e026f89079212..737a9db6ec3f13fc123d17e973eb90c1c321a344 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -48,7 +48,7 @@ public: static const int MSG_CHECK_BUNDLE_STATE = 0; static const int MSG_ONE_TIME_CHECK_BUNDLE_STATE = 1; static const int MSG_CHECK_IDLE_STATE = 2; - static const int CHECK_IDLE_INTERVAL = ONE_MINUTE; + static const int CHECK_IDLE_INTERVAL = THREE_HOUR; private: std::shared_ptr bundleActiveGroupController_; diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 92783290468dc26ad8515d1f58b74a4d9aa11e20..0f5ae55ceaa65a81f2c12a0fa22aa136b2693af4 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -50,11 +50,11 @@ void BundleActiveGroupController::OnUserRemoved(const int userId) } } -void BundleActiveGroupController::OnUserSwitched(const int userId, const int lastUsedUser) +void BundleActiveGroupController::OnUserSwitched(const int userId, const int currentUsedUser) { - BUNDLE_ACTIVE_LOGI("last time check for user %{public}d", lastUsedUser); - CheckEachBundleState(lastUsedUser); - bundleUserHistory_->WriteBundleUsage(lastUsedUser); + BUNDLE_ACTIVE_LOGI("last time check for user %{public}d", currentUsedUser); + CheckEachBundleState(currentUsedUser); + bundleUserHistory_->WriteBundleUsage(currentUsedUser); std::lock_guard lock(mutex_); if (!activeGroupHandler_.expired()) { activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); diff --git a/services/packageusage/include/bundle_active_calendar.h b/services/packageusage/include/bundle_active_calendar.h index 2e7a2f4e57d002bcb980e07a1d497afc0df07654..745d56ecbb1dce48b28479f919ce15d09ad613a9 100644 --- a/services/packageusage/include/bundle_active_calendar.h +++ b/services/packageusage/include/bundle_active_calendar.h @@ -23,10 +23,10 @@ namespace DeviceUsageStats { class BundleActiveCalendar { public: static const int64_t ONE_SECOND_MILLISECONDS = 1000; - static const int64_t DAY_MILLISECONDS = (int64_t)1 * 1 * 10 * 60 * 1000; - static const int64_t WEEK_MILLISECONDS = (int64_t)1 * 1 * 20 * 60 * 1000; - static const int64_t MONTH_MILLISECONDS = (int64_t)1 * 1 * 30 * 60 * 1000; - static const int64_t YEAR_MILLISECONDS = (int64_t)1 * 1 * 40 * 60 * 1000; + static const int64_t DAY_MILLISECONDS = (int64_t)1 * 24 * 60 * 60 * 1000; + static const int64_t WEEK_MILLISECONDS = (int64_t)7 * 24 * 60 * 60 * 1000; + static const int64_t MONTH_MILLISECONDS = (int64_t)30 * 24 * 60 * 60 * 1000; + static const int64_t YEAR_MILLISECONDS = (int64_t)365 * 24 * 60 * 60 * 1000; BundleActiveCalendar(const int64_t timeStamp); BundleActiveCalendar() { 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 670e854793f3379a04eb61af62595a103e7a7fe9..931d54f32992f3fb2f2507a321b15b1a25c12251 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -86,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_); @@ -132,7 +133,9 @@ void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) break; } } - NotifyStatsChanged(); + if (event.eventId_ != BundleActiveEvent::FLUSH) { + NotifyStatsChanged(); + } } void BundleActiveUserService::ReportForShutdown(const BundleActiveEvent& event)