From 27a9e182d6f6a71480d3007b69295267c387297a Mon Sep 17 00:00:00 2001 From: houdisheng Date: Sat, 5 Mar 2022 19:28:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?codex=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0debug=E6=97=A5=E5=BF=97=EF=BC=8C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E8=B0=83=E6=95=B4=EF=BC=8C?= =?UTF-8?q?review=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- .../innerkits/src/bundle_active_proxy.cpp | 21 ++--- services/common/include/bundle_active_core.h | 5 +- .../src/bundle_active_app_state_obsever.cpp | 2 +- services/common/src/bundle_active_core.cpp | 87 +++++++++++-------- services/common/src/bundle_active_service.cpp | 65 +++++++------- services/common/src/bundle_active_stub.cpp | 16 ++-- .../src/bundle_active_usage_database.cpp | 6 +- .../include/bundle_active_group_common.h | 31 +++---- .../include/bundle_active_group_controller.h | 8 +- .../include/bundle_active_group_handler.h | 5 +- .../include/bundle_active_package_history.h | 14 ++- .../include/bundle_active_user_history.h | 4 +- .../src/bundle_active_group_controller.cpp | 36 ++++---- .../src/bundle_active_group_handler.cpp | 6 +- .../src/bundle_active_user_history.cpp | 18 ++-- .../include/bundle_active_event.h | 11 ++- .../include/bundle_active_event_stats.h | 8 +- .../packageusage/src/bundle_active_event.cpp | 6 -- .../src/bundle_active_event_list.cpp | 6 +- .../src/bundle_active_stats_combiner.cpp | 6 +- .../src/bundle_active_user_service.cpp | 50 ++++++----- 21 files changed, 227 insertions(+), 184 deletions(-) diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 068794f..a550088 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -51,7 +51,7 @@ bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName) data.WriteString(bundleName); Remote() -> SendRequest(IS_BUNDLE_IDLE, data, reply, option); int32_t result = reply.ReadInt32(); - BUNDLE_ACTIVE_LOGI("BundleActiveProxy::IsBundleIdle result is %{public}d", result); + BUNDLE_ACTIVE_LOGI("result is %{public}d", result); return result; } @@ -78,8 +78,8 @@ std::vector BundleActiveProxy::QueryPackageStats(const } result.push_back(*tmp); } - for (int i = 0; i < result.size(); i++) { - BUNDLE_ACTIVE_LOGI("BundleActiveProxy::QueryPackageStats result idx is %{public}d, bundleName_ is %{public}s, " + for (uint32_t i = 0; i < result.size(); i++) { + BUNDLE_ACTIVE_LOGI("QueryPackageStats result idx is %{public}d, bundleName_ is %{public}s, " "lastTimeUsed_ is %{public}lld, lastContiniousTaskUsed_ is %{public}lld, " "totalInFrontTime_ is %{public}lld, totalContiniousTaskUsedTime_ is %{public}lld", i + 1, result[i].bundleName_.c_str(), result[i].lastTimeUsed_, result[i].lastContiniousTaskUsed_, @@ -109,8 +109,8 @@ std::vector BundleActiveProxy::QueryEvents(const int64_t begi } result.push_back(*tmp); } - for (int i = 0; i < result.size(); i++) { - BUNDLE_ACTIVE_LOGI("BundleActiveProxy::QueryEvents event id is %{public}d, bundle name is %{public}s, " + for (uint32_t i = 0; i < result.size(); i++) { + BUNDLE_ACTIVE_LOGI("QueryEvents event id is %{public}d, bundle name is %{public}s, " "time stamp is %{public}lld", result[i].eventId_, result[i].bundleName_.c_str(), result[i].timeStamp_); } return result; @@ -153,8 +153,8 @@ std::vector BundleActiveProxy::QueryCurrentPackageStat } result.push_back(*tmp); } - for (int i = 0; i < result.size(); i++) { - BUNDLE_ACTIVE_LOGI("BundleActiveProxy::QueryPackageStats result idx is %{public}d, bundleName_ is %{public}s, " + for (uint32_t i = 0; i < result.size(); i++) { + BUNDLE_ACTIVE_LOGI("QueryPackageStats result idx is %{public}d, bundleName_ is %{public}s, " "lastTimeUsed_ is %{public}lld, lastContiniousTaskUsed_ is %{public}lld, " "totalInFrontTime_ is %{public}lld, totalContiniousTaskUsedTime_ is %{public}lld", i + 1, result[i].bundleName_.c_str(), result[i].lastTimeUsed_, result[i].lastContiniousTaskUsed_, @@ -184,8 +184,9 @@ std::vector BundleActiveProxy::QueryCurrentEvents(const int64 } result.push_back(*tmp); } - for (int i = 0; i < result.size(); i++) { - BUNDLE_ACTIVE_LOGI("event id is %{public}d, bundle name is %{public}s, time stamp is %{public}lld", + for (uint32_t i = 0; i < result.size(); i++) { + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents event id is %{public}d, bundle name is %{public}s," + "time stamp is %{public}lld", result[i].eventId_, result[i].bundleName_.c_str(), result[i].timeStamp_); } return result; @@ -201,7 +202,7 @@ int BundleActiveProxy::QueryPackageGroup() } Remote() -> SendRequest(QUERY_BUNDLE_GROUP, data, reply, option); int32_t packageGroup = reply.ReadInt32(); - BUNDLE_ACTIVE_LOGI("BundleActiveProxy::QueryPackageGroup result is %{public}d", packageGroup); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup result is %{public}d", packageGroup); return packageGroup; } } diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index a9b6868..51a5264 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -36,7 +36,10 @@ public: BundleActiveEvent event_; int userId_; std::string bundleName_; - BundleActiveReportHandlerObject() {}; + BundleActiveReportHandlerObject() { + userId_ = -1; + bundleName_ = ""; + }; BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig); ~BundleActiveReportHandlerObject() {}; }; diff --git a/services/common/src/bundle_active_app_state_obsever.cpp b/services/common/src/bundle_active_app_state_obsever.cpp index cf613c6..1bebdd5 100644 --- a/services/common/src/bundle_active_app_state_obsever.cpp +++ b/services/common/src/bundle_active_app_state_obsever.cpp @@ -24,7 +24,7 @@ namespace DeviceUsageStats { void BundleActiveAppStateObserver::Init(const std::shared_ptr& reportHandler) { if (reportHandler != nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveAppStateObserver::Init report handler is not null, init success"); + BUNDLE_ACTIVE_LOGI("Init report handler is not null, init success"); reportHandler_ = reportHandler; } } diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 665887b..07cceba 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -30,7 +30,6 @@ BundleActiveReportHandlerObject::BundleActiveReportHandlerObject(const BundleAct event_.abilityId_ = orig.event_.abilityId_; event_.timeStamp_ = orig.event_.timeStamp_; event_.eventId_ = orig.event_.eventId_; - event_.isidle_ = orig.event_.isidle_; event_.continuousTaskAbilityName_ = orig.event_.continuousTaskAbilityName_; userId_ = orig.userId_; bundleName_ = orig.bundleName_; @@ -38,6 +37,8 @@ BundleActiveReportHandlerObject::BundleActiveReportHandlerObject(const BundleAct BundleActiveCore::BundleActiveCore() { + systemTimeShot_ = -1; + realTimeShot_ = -1; } BundleActiveCore::~BundleActiveCore() @@ -48,14 +49,14 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da { std::lock_guard lock(mutex_); std::string action = data.GetWant().GetAction(); - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent action is %{public}s", action.c_str()); + BUNDLE_ACTIVE_LOGI("OnReceiveEvent action is %{public}s", action.c_str()); auto want = data.GetWant(); if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF || action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) { if (!activeGroupController_.expired()) { sptr timer = MiscServices::TimeServiceClient::GetInstance(); bool isScreenOn = activeGroupController_.lock()->IsScreenOn(); - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent Screen state changed " + BUNDLE_ACTIVE_LOGI("OnReceiveEvent Screen state changed " "received, screen state chante to %{public}d", isScreenOn); activeGroupController_.lock()->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); } @@ -72,8 +73,7 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da } } else if (action == CommonEventSupport::COMMON_EVENT_USER_ADDED) { int32_t userId = data.GetCode(); - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent receive add user event, " - "user id is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("OnReceiveEvent receive add user event, user id is %{public}d", userId); if (!activeGroupController_.expired() && userId >= 0) { activeGroupController_.lock()->PeriodCheckBundleState(userId); } @@ -84,16 +84,13 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", action.c_str(), userId, bundleName.c_str()); if (!bundleActiveReportHandler_.expired()) { - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent gggggg"); BundleActiveReportHandlerObject tmpHandlerObject; tmpHandlerObject.bundleName_ = bundleName; tmpHandlerObject.userId_ = userId; std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent fffff"); auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, handlerobjToPtr); - BUNDLE_ACTIVE_LOGI("BundleActiveCommonEventSubscriber::OnReceiveEvent hhhhh"); bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, handlerobjToPtr); } @@ -137,7 +134,7 @@ void BundleActiveCore::Init() void BundleActiveCore::InitBundleGroupController() { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::InitBundleGroupController called"); + BUNDLE_ACTIVE_LOGI("InitBundleGroupController called"); std::string threadName = "bundle_active_group_handler"; auto runner = AppExecFwk::EventRunner::Create(threadName); if (runner == nullptr) { @@ -151,13 +148,13 @@ void BundleActiveCore::InitBundleGroupController() if (bundleGroupController_ != nullptr && bundleGroupHandler_ != nullptr) { bundleGroupHandler_->Init(bundleGroupController_); bundleGroupController_->SetHandlerAndCreateUserHistory(bundleGroupHandler_, realTimeShot_); - BUNDLE_ACTIVE_LOGI("BundleActiveCore::Init Set group controller and handler done"); + BUNDLE_ACTIVE_LOGI("Init Set group controller and handler done"); } RegisterSubscriber(); std::vector osAccountInfos; GetAllActiveUser(osAccountInfos); bundleGroupController_->bundleGroupEnable_ = true; - for (int i = 0; i < osAccountInfos.size(); i++) { + for (uint32_t i = 0; i < osAccountInfos.size(); i++) { bundleGroupController_->PeriodCheckBundleState(osAccountInfos[i].GetLocalId()); } } @@ -170,7 +167,7 @@ void BundleActiveCore::SetHandler(const std::shared_ptr BundleActiveCore::GetUserDataAndInitializeIfNeeded(const int userId, const int64_t timeStamp) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::GetUserDataAndInitializeIfNeeded called"); + BUNDLE_ACTIVE_LOGI("GetUserDataAndInitializeIfNeeded called"); std::map>::iterator it = userStatServices_.find(userId); if (it == userStatServices_.end()) { BUNDLE_ACTIVE_LOGI("first initialize user service"); @@ -188,9 +185,12 @@ std::shared_ptr BundleActiveCore::GetUserDataAndInitial } void BundleActiveCore::OnBundleUninstalled(const int userId, const std::string& bundleName) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::OnBundleUninstalled CALLED"); + BUNDLE_ACTIVE_LOGI("OnBundleUninstalled CALLED"); std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); + if (timeNow == -1) { + return; + } auto service = GetUserDataAndInitializeIfNeeded(userId, timeNow); if (service == nullptr) { return; @@ -202,7 +202,7 @@ void BundleActiveCore::OnBundleUninstalled(const int userId, const std::string& void BundleActiveCore::OnStatsChanged(const int userId) { if (!handler_.expired()) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::OnStatsChanged send flush to disk event"); + BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event"); BundleActiveReportHandlerObject tmpHandlerObject; tmpHandlerObject.userId_ = userId; std::shared_ptr handlerobjToPtr = @@ -230,14 +230,14 @@ void BundleActiveCore::RestoreAllData() bundleGroupController_->RestoreDurationToDatabase(); } if (!handler_.expired()) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::RestoreToDatabaseLocked remove flush to disk event"); + BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } void BundleActiveCore::RestoreToDatabase(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::RestoreToDatabase called"); + BUNDLE_ACTIVE_LOGI("RestoreToDatabase called"); sptr timer = MiscServices::TimeServiceClient::GetInstance(); BundleActiveEvent event; event.eventId_ = BundleActiveEvent::FLUSH; @@ -247,15 +247,12 @@ void BundleActiveCore::RestoreToDatabase(const int userId) if (it != userStatServices_.end()) { it->second->ReportEvent(event); } - if (bundleGroupController_ != nullptr) { - bundleGroupController_->RestoreDurationToDatabase(); - } RestoreToDatabaseLocked(userId); } void BundleActiveCore::RestoreToDatabaseLocked(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::RestoreToDatabaseLocked called"); + BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked called"); auto it = userStatServices_.find(userId); if (it != userStatServices_.end()) { it->second->RestoreStats(false); @@ -264,15 +261,14 @@ void BundleActiveCore::RestoreToDatabaseLocked(const int userId) bundleGroupController_->RestoreDurationToDatabase(); } if (!handler_.expired()) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::RestoreToDatabaseLocked remove flush to disk event"); + BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } void BundleActiveCore::ShutDown() { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::ShutDown called"); - std::lock_guard lock(mutex_); + BUNDLE_ACTIVE_LOGI("ShutDown called"); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t timeStamp = timer->GetBootTimeMs(); BundleActiveEvent event(BundleActiveEvent::SHUTDOWN, timeStamp); @@ -284,7 +280,7 @@ void BundleActiveCore::ShutDown() void BundleActiveCore::OnStatsReload() { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::OnStatsReload called"); + BUNDLE_ACTIVE_LOGI("OnStatsReload called"); bundleGroupController_->CheckIdleStatsOneTime(); } @@ -294,12 +290,15 @@ void BundleActiveCore::OnSystemUpdate(int userId) int64_t BundleActiveCore::CheckTimeChangeAndGetWallTime(int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::CheckTimeChangeAndGetWallTime called, userId is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("CheckTimeChangeAndGetWallTime called, userId is %{public}d", userId); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t actualSystemTime = GetSystemTimeMs(); int64_t actualRealTime = timer->GetBootTimeMs(); int64_t expectedSystemTime = (actualRealTime - realTimeShot_) + systemTimeShot_; int64_t diffSystemTime = actualSystemTime - expectedSystemTime; + if (actualSystemTime == -1 || actualRealTime == -1) { + return -1; + } BUNDLE_ACTIVE_LOGI("asystime is %{public}lld, artime is %{public}lld, esystime is %{public}lld, " "diff is %{public}lld", actualSystemTime, actualRealTime, expectedSystemTime, diffSystemTime); @@ -316,7 +315,7 @@ int64_t BundleActiveCore::CheckTimeChangeAndGetWallTime(int userId) it->second->RenewTableTime(expectedSystemTime, actualSystemTime); it->second->LoadActiveStats(actualSystemTime, true, true); if (!handler_.expired()) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::RestoreToDatabaseLocked remove flush to disk event"); + BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } @@ -328,13 +327,13 @@ int64_t BundleActiveCore::CheckTimeChangeAndGetWallTime(int userId) void BundleActiveCore::ConvertToSystemTimeLocked(BundleActiveEvent& event) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::ConvertToSystemTimeLocked called"); + BUNDLE_ACTIVE_LOGI("ConvertToSystemTimeLocked called"); event.timeStamp_ = std::max((int64_t)0, event.timeStamp_ - realTimeShot_) + systemTimeShot_; } void BundleActiveCore::OnUserRemoved(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::OnUserRemoved called"); + BUNDLE_ACTIVE_LOGI("OnUserRemoved called"); std::lock_guard lock(mutex_); auto it = userStatServices_.find(userId); if (it == userStatServices_.end()) { @@ -352,11 +351,14 @@ int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) if (userId == 0) { return -1; } - BUNDLE_ACTIVE_LOGI("BundleActiveCore::ReportEvent called"); + BUNDLE_ACTIVE_LOGI("ReportEvent called"); 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); @@ -371,8 +373,11 @@ int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) int BundleActiveCore::ReportEventToAllUserId(BundleActiveEvent& event) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::ReportEventToAllUserId called"); + BUNDLE_ACTIVE_LOGI("ReportEventToAllUserId called"); int64_t timeNow = CheckTimeChangeAndGetWallTime(); + if (timeNow == -1) { + return -1; + } if (userStatServices_.empty()) { std::shared_ptr service = GetUserDataAndInitializeIfNeeded(DEFAULT_USER_ID, timeNow); } @@ -384,7 +389,7 @@ int BundleActiveCore::ReportEventToAllUserId(BundleActiveEvent& event) BUNDLE_ACTIVE_LOGE("get user data service failed!"); return -1; } - BUNDLE_ACTIVE_LOGI("BundleActiveCore::ReportEventToAllUserId SERVICE user ID IS userId %{public}d", + BUNDLE_ACTIVE_LOGI("ReportEventToAllUserId SERVICE user ID IS userId %{public}d", service->userId_); service->ReportForShutdown(event); return 0; @@ -395,19 +400,22 @@ int BundleActiveCore::ReportEventToAllUserId(BundleActiveEvent& event) std::vector BundleActiveCore::QueryPackageStats(const int userId, const int intervalType, const int64_t beginTime, const int64_t endTime, std::string bundleName) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::QueryPackageStats called"); + BUNDLE_ACTIVE_LOGI("QueryPackageStats called"); std::lock_guard lock(mutex_); std::vector result; int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); - BUNDLE_ACTIVE_LOGI("BundleActiveCore::QueryPackageStats begin time is %{public}lld, end time is %{public}lld, " + if (timeNow == -1) { + return result; + } + BUNDLE_ACTIVE_LOGI("QueryPackageStats begin time is %{public}lld, end time is %{public}lld, " "intervaltype is %{public}d", beginTime, endTime, intervalType); if (beginTime > timeNow || beginTime >= endTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::QueryPackageStats time span illegal"); + BUNDLE_ACTIVE_LOGI("QueryPackageStats time span illegal"); return result; } std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow); if (service == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::QueryPackageStats service is null, failed"); + BUNDLE_ACTIVE_LOGI("QueryPackageStats service is null, failed"); return result; } result = service->QueryPackageStats(intervalType, beginTime, endTime, userId, bundleName); @@ -417,10 +425,13 @@ std::vector BundleActiveCore::QueryPackageStats(const std::vector BundleActiveCore::QueryEvents(const int userId, const int64_t beginTime, const int64_t endTime, std::string bundleName) { - BUNDLE_ACTIVE_LOGI("BundleActiveCore::QueryEvents called"); + BUNDLE_ACTIVE_LOGI("QueryEvents called"); std::vector result; std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); + if (timeNow == -1) { + return result; + } if (beginTime > timeNow || beginTime >= endTime) { return result; } @@ -454,11 +465,11 @@ void BundleActiveCore::GetAllActiveUser(std::vector appManager = GetAppManagerInstance(); if (appStateObserver_ == nullptr) { - BUNDLE_ACTIVE_LOGE("BundleActiveService::SubscribeAppState appstateobserver is null, return"); + BUNDLE_ACTIVE_LOGE("SubscribeAppState appstateobserver is null, return"); return false; } int32_t err = appManager->RegisterApplicationStateObserver(appStateObserver_.get()); @@ -153,7 +158,7 @@ bool BundleActiveService::SubscribeAppState() bool BundleActiveService::SubscribeContinuousTask() { if (continuousTaskObserver_ == nullptr) { - BUNDLE_ACTIVE_LOGE("BundleActiveService::SubscribeContinuousTask continuousTaskObserver_ is null, return"); + BUNDLE_ACTIVE_LOGE("SubscribeContinuousTask continuousTaskObserver_ is null, return"); return false; } if (OHOS::BackgroundTaskMgr::BackgroundTaskMgrHelper::SubscribeBackgroundTask(*continuousTaskObserver_) @@ -172,14 +177,8 @@ void BundleActiveService::OnStop() powerManagerClient.UnRegisterShutdownCallback(shutdownCallback_); delete shutdownCallback_; shutdownCallback_ = nullptr; - } else { - shutdownCallback_ = new BundleActiveShutdownCallbackService(bundleActiveCore_); + return; } - auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); - powerManagerClient.UnRegisterShutdownCallback(shutdownCallback_); - bundleActiveCore_->UnRegisterSubscriber(); - delete shutdownCallback_; - shutdownCallback_ = nullptr; BUNDLE_ACTIVE_LOGI("[Server] OnStop"); } @@ -215,7 +214,7 @@ bool BundleActiveService::IsBundleIdle(const std::string& bundleName) int result = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); if (ret == ERR_OK && userId != -1) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::IsBundleIdle user id is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("IsBundleIdle user id is %{public}d", userId); if (!GetBundleMgrProxy()) { BUNDLE_ACTIVE_LOGE("Get bundle manager proxy failed!"); return true; @@ -234,17 +233,17 @@ bool BundleActiveService::IsBundleIdle(const std::string& bundleName) std::vector BundleActiveService::QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageStats stats called, intervaltype is %{public}d", + BUNDLE_ACTIVE_LOGI("QueryPackageStats stats called, intervaltype is %{public}d", intervalType); std::vector result; // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageStats UID is %{public}d", callingUid); + BUNDLE_ACTIVE_LOGI("QueryPackageStats UID is %{public}d", callingUid); // get userid int userId = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); if (ret == ERR_OK && userId != -1) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageStats user id is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("QueryPackageStats user id is %{public}d", userId); bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId); if (isSystemAppAndHasPermission == true) { int convertedIntervalType = ConvertIntervalType(intervalType); @@ -256,16 +255,16 @@ std::vector BundleActiveService::QueryPackageStats(con std::vector BundleActiveService::QueryEvents(const int64_t beginTime, const int64_t endTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryEvents stats called"); + BUNDLE_ACTIVE_LOGI("QueryEvents stats called"); std::vector result; // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryEvents UID is %{public}d", callingUid); + BUNDLE_ACTIVE_LOGI("QueryEvents UID is %{public}d", callingUid); // get userid int userId = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); if (ret == ERR_OK && userId != -1) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryEvents userid is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("QueryEvents userid is %{public}d", userId); bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId); if (isSystemAppAndHasPermission == true) { result = bundleActiveCore_->QueryEvents(userId, beginTime, endTime, ""); @@ -283,7 +282,7 @@ void BundleActiveService::SetBundleGroup(const std::string& bundleName, int newG std::vector BundleActiveService::QueryCurrentPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentPackageStats stats called"); + BUNDLE_ACTIVE_LOGI("QueryCurrentPackageStats stats called"); std::vector result; // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); @@ -292,7 +291,7 @@ std::vector BundleActiveService::QueryCurrentPackageSt int userId = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); if (ret == ERR_OK && userId != -1) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentPackageStats userid is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("QueryCurrentPackageStats userid is %{public}d", userId); if (!GetBundleMgrProxy()) { BUNDLE_ACTIVE_LOGE("BundleActiveGroupController::CheckEachBundleState get bundle manager proxy failed!"); return result; @@ -306,51 +305,51 @@ std::vector BundleActiveService::QueryCurrentPackageSt bundleName); } } - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentPackageStats result size is %{public}d", result.size()); + BUNDLE_ACTIVE_LOGI("QueryCurrentPackageStats result size is %{public}d", result.size()); return result; } std::vector BundleActiveService::QueryCurrentEvents(const int64_t beginTime, const int64_t endTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentEvents stats called"); + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents stats called"); std::vector result; // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentEvents UID is %{public}d", callingUid); + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents UID is %{public}d", callingUid); // get userid int userId = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); if (ret == ERR_OK && userId != -1) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentEvents userid is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents userid is %{public}d", userId); if (!GetBundleMgrProxy()) { - BUNDLE_ACTIVE_LOGE("BundleActiveService::QueryCurrentEvents get bundle manager proxy failed!"); + BUNDLE_ACTIVE_LOGE("QueryCurrentEvents get bundle manager proxy failed!"); return result; } std::string bundleName = ""; sptrBundleMgr_->GetBundleNameForUid(callingUid, bundleName); bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId); if (!bundleName.empty() && isSystemAppAndHasPermission == true) { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentEvents buindle name is %{public}s", + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents buindle name is %{public}s", bundleName.c_str()); result = bundleActiveCore_->QueryEvents(userId, beginTime, endTime, bundleName); } } - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryCurrentEvents result size is %{public}d", result.size()); + BUNDLE_ACTIVE_LOGI("QueryCurrentEvents result size is %{public}d", result.size()); return result; } int BundleActiveService::QueryPackageGroup() { - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageGroup stats called"); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup stats called"); // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageGroup UID is %{public}d", callingUid); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup UID is %{public}d", callingUid); // get userid int userId = -1; int result = -1; OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageGroup user id is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup user id is %{public}d", userId); if (ret == ERR_OK && userId != -1) { if (!GetBundleMgrProxy()) { BUNDLE_ACTIVE_LOGE("BundleActiveGroupController::CheckEachBundleState get bundle manager proxy failed!"); @@ -359,12 +358,12 @@ int BundleActiveService::QueryPackageGroup() std::string bundleName = ""; // get bundle name sptrBundleMgr_->GetBundleNameForUid(callingUid, bundleName); - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageGroup bundlename is %{public}s", bundleName.c_str()); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup bundlename is %{public}s", bundleName.c_str()); if (!bundleName.empty()) { result = bundleActiveCore_->QueryPackageGroup(userId, bundleName); } } - BUNDLE_ACTIVE_LOGI("BundleActiveService::QueryPackageGroup result is %{public}d", result); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup result is %{public}d", result); return result; } diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index 99beeea..02c5beb 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -45,13 +45,13 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me std::vector result; int size = 0; int intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("BundleActiveStub::OnRemoteRequest QUERY_USAGE_STATS intervaltype is %{public}d", + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS intervaltype is %{public}d", intervalType); int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); result = QueryPackageStats(intervalType, beginTime, endTime); - size = result.size(); - BUNDLE_ACTIVE_LOGI("BundleActiveStub::OnRemoteRequest QUERY_USAGE_STATS result size is %{public}d", size); + size = static_cast(result.size()); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS result size is %{public}d", size); reply.WriteInt32(size); for (int i = 0; i < size; i++) { bool tmp = result[i].Marshalling(reply); @@ -67,7 +67,7 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); result = QueryEvents(beginTime, endTime); - size = result.size(); + size = static_cast(result.size()); reply.WriteInt32(size); for (int i = 0; i < size; i++) { bool tmp = result[i].Marshalling(reply); @@ -88,13 +88,13 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me std::vector result; int size = 0; int intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("BundleActiveStub::OnRemoteRequest QUERY_CURRENT_USAGE_STATS intervaltype " + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS intervaltype " "is %{public}d", intervalType); int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); result = QueryPackageStats(intervalType, beginTime, endTime); - size = result.size(); - BUNDLE_ACTIVE_LOGI("BundleActiveStub::OnRemoteRequest QUERY_CURRENT_USAGE_STATS result size " + size = static_cast(result.size()); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_CURRENT_USAGE_STATS result size " "is %{public}d", size); reply.WriteInt32(size); for (int i = 0; i < size; i++) { @@ -111,7 +111,7 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); result = QueryCurrentEvents(beginTime, endTime); - size = result.size(); + size = static_cast(result.size()); reply.WriteInt32(size); for (int i = 0; i < size; i++) { bool tmp = result[i].Marshalling(reply); diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 9824a4b..da42029 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -559,7 +559,7 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int userId, valuesBucket.PutLong(BUNDLE_ACTIVE_DB_LAST_BOOT_FROM_USED_TIME, iter->second->lastBootFromUsedTimeStamp_); valuesBucket.PutLong(BUNDLE_ACTIVE_DB_LAST_SCREEN_USED_TIME, iter->second->lastScreenUsedTimeStamp_); valuesBucket.PutInt(BUNDLE_ACTIVE_DB_CURRENT_GROUP, iter->second->currentGroup_); - valuesBucket.PutInt(BUNDLE_ACTIVE_DB_REASON_IN_GROUP, iter->second->reasonInGroup_); + valuesBucket.PutInt(BUNDLE_ACTIVE_DB_REASON_IN_GROUP, static_cast(iter->second->reasonInGroup_)); valuesBucket.PutLong(BUNDLE_ACTIVE_DB_BUNDLE_ALIVE_TIMEOUT_TIME, iter->second->bundleAliveTimeoutTimeStamp_); valuesBucket.PutLong(BUNDLE_ACTIVE_DB_BUNDLE_DAILY_TIMEOUT_TIME, iter->second->bundleDailyTimeoutTimeStamp_); rdbStore->Update(changeRow, BUNDLE_HISTORY_LOG_TABLE, valuesBucket, "userId = ? and bundleName = ?", @@ -601,6 +601,7 @@ shared_ptr>> BundleActiveUsag shared_ptr>> userUsageHistory = make_shared>>(); shared_ptr usageHistory; + int currentBundleGroupReason = 0; for (int32_t i = 0; i < tableRowNumber; i++) { bundleActiveResult->GoToRow(i); bundleActiveResult->GetString(BUNDLE_NAME_COLUMN_INDEX, bundleName); @@ -608,7 +609,8 @@ shared_ptr>> BundleActiveUsag bundleActiveResult->GetLong(LAST_BOOT_FROM_USED_TIME_COLUMN_INDEX, usageHistory->lastBootFromUsedTimeStamp_); bundleActiveResult->GetLong(LAST_SCREEN_USED_TIME_COLUMN_INDEX, usageHistory->lastScreenUsedTimeStamp_); bundleActiveResult->GetInt(CURRENT_GROUP_COLUMN_INDEX, usageHistory->currentGroup_); - bundleActiveResult->GetInt(REASON_IN_GROUP_COLUMN_INDEX, usageHistory->reasonInGroup_); + bundleActiveResult->GetInt(REASON_IN_GROUP_COLUMN_INDEX, currentBundleGroupReason); + usageHistory->reasonInGroup_ = static_cast(currentBundleGroupReason); bundleActiveResult->GetLong(BUNDLE_ALIVE_TIMEOUT_TIME_COLUMN_INDEX, usageHistory->bundleAliveTimeoutTimeStamp_); bundleActiveResult->GetLong(BUNDLE_DAILY_TIMEOUT_TIME_COLUMN_INDEX, diff --git a/services/packagegroup/include/bundle_active_group_common.h b/services/packagegroup/include/bundle_active_group_common.h index f530520..d3fdef4 100644 --- a/services/packagegroup/include/bundle_active_group_common.h +++ b/services/packagegroup/include/bundle_active_group_common.h @@ -30,6 +30,7 @@ const int ACTIVE_GROUP_NEVER = 60; const int64_t FIVE_SECOND = 5 * 1000; const int64_t ONE_MINUTE = 60 * 1000; const int64_t TWO_MINUTE = 2 * ONE_MINUTE; +const int64_t THREE_MINUTE = 3 * ONE_MINUTE; const int64_t FOUR_MINUTE = 4 * ONE_MINUTE; const int64_t TEN_MINUTE = 10 * ONE_MINUTE; const int64_t SIXTEEN_MINUTE = 16 * ONE_MINUTE; @@ -43,21 +44,21 @@ const int64_t DEFAULT_EVENT_TIMEOUT = ONE_HOUR; const int64_t DEFAULT_NOTIFY_EVENT_TIMEOUT = 12 * ONE_HOUR; const int64_t DEFAULT_SYSTEevent__TIMEOUT = 2 * ONE_HOUR; const int64_t DEFAULT_LONT_TIME_TASK_START_EVENT_TIMEOUT = 30 * ONE_MINUTE; -const int GROUP_CONTROL_REASON_MASK = 0xFF00; -const int GROUP_CONTROL_REASON_DEFAULT = 0x0100; -const int GROUP_CONTROL_REASON_TIMEOUT = 0x0200; -const int GROUP_CONTROL_REASON_USAGE = 0x0300; -const int GROUP_CONTROL_REASON_FORCED = 0x0400; -const int GROUP_CONTROL_REASON_CALCULATED = 0x0500; -const int GROUP_EVENT_REASON_MASK = 0x00FF; -const int GROUP_EVENT_REASON_SYSTEM = 0x0001; -const int GROUP_EVENT_REASON_NOTIFY_SEEN = 0x0002; -const int GROUP_EVENT_REASON_USER_INTERACTION = 0x0003; -const int GROUP_EVENT_REASON_FOREGROUND = 0x0004; -const int GROUP_EVENT_REASON_BACKGROUND = 0x0005; -const int GROUP_EVENT_REASON_ALIVE_TIMEOUT = 0x0006; -const int GROUP_EVENT_REASON_LONG_TIME_TASK_STARTTED = 0x0007; -const int GROUP_EVENT_REASON_CALCULATED_RESTORED = 0x0008; +const uint32_t GROUP_CONTROL_REASON_MASK = 0xFF00; +const uint32_t GROUP_CONTROL_REASON_DEFAULT = 0x0100; +const uint32_t GROUP_CONTROL_REASON_TIMEOUT = 0x0200; +const uint32_t GROUP_CONTROL_REASON_USAGE = 0x0300; +const uint32_t GROUP_CONTROL_REASON_FORCED = 0x0400; +const uint32_t GROUP_CONTROL_REASON_CALCULATED = 0x0500; +const uint32_t GROUP_EVENT_REASON_MASK = 0x00FF; +const uint32_t GROUP_EVENT_REASON_SYSTEM = 0x0001; +const uint32_t GROUP_EVENT_REASON_NOTIFY_SEEN = 0x0002; +const uint32_t GROUP_EVENT_REASON_USER_INTERACTION = 0x0003; +const uint32_t GROUP_EVENT_REASON_FOREGROUND = 0x0004; +const uint32_t GROUP_EVENT_REASON_BACKGROUND = 0x0005; +const uint32_t GROUP_EVENT_REASON_ALIVE_TIMEOUT = 0x0006; +const uint32_t GROUP_EVENT_REASON_LONG_TIME_TASK_STARTTED = 0x0007; +const uint32_t GROUP_EVENT_REASON_CALCULATED_RESTORED = 0x0008; } } } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index c0589b7..1de660f 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -42,7 +42,7 @@ public: using BundleFlag = OHOS::AppExecFwk::BundleFlag; using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag; OHOS::AppExecFwk::ApplicationFlag flag = OHOS::AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO; - bool bundleGroupEnable_; + bool bundleGroupEnable_ = true; bool debug_ = true; const int LEVEL_GROUP[4] = { ACTIVE_GROUP_ALIVE, @@ -75,7 +75,7 @@ public: void OnUserRemoved(const int userId); void OnBundleUninstalled(const int userId, const std::string bundleName); void OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp); - void SetBundleGroup(const std::string& bundleName, const int userId, int newGroup, int reason, + void SetBundleGroup(const std::string& bundleName, const int userId, int newGroup, uint32_t reason, const int64_t bootBasedTimeStamp, const bool& resetTimeout); void RestoreToDatabase(const int userId); void RestoreDurationToDatabase(); @@ -89,8 +89,8 @@ private: std::mutex mutex_; bool GetBundleMgrProxy(); std::weak_ptr activeGroupHandler_; - int EventToGroupReason(const int eventId); - int64_t timeoutForDirectlyUse_ = debug_ ? ONE_MINUTE : ONE_HOUR; + uint32_t EventToGroupReason(const int eventId); + int64_t timeoutForDirectlyUse_ = debug_ ? THREE_MINUTE : ONE_HOUR; int64_t timeoutForNotifySeen_ = debug_ ? ONE_MINUTE : TWELVE_HOUR; int64_t timeoutForSystemInteraction_ = debug_ ? ONE_MINUTE : TEN_MINUTE; int64_t timeoutCalculated_; diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index a954957..3427c0a 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -29,7 +29,10 @@ class BundleActiveGroupHandlerObject { public: std::string bundleName_; int userId_; - BundleActiveGroupHandlerObject() {}; + BundleActiveGroupHandlerObject() { + bundleName_ = ""; + userId_ = -1; + }; BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject& orig); ~BundleActiveGroupHandlerObject() {}; }; diff --git a/services/packagegroup/include/bundle_active_package_history.h b/services/packagegroup/include/bundle_active_package_history.h index c78c304..d08e4d3 100644 --- a/services/packagegroup/include/bundle_active_package_history.h +++ b/services/packagegroup/include/bundle_active_package_history.h @@ -17,7 +17,7 @@ #define BUNDLE_ACTIVE_USAGE_HISTORY_H #include "ibundle_active_service.h" - +#include "bundle_active_group_common.h" namespace OHOS { namespace DeviceUsageStats { class BundleActivePackageHistory { @@ -27,10 +27,18 @@ public: int64_t lastGroupCalculatedTimeStamp_; int lastCalculatedGroup_; int currentGroup_; - int reasonInGroup_; + uint32_t reasonInGroup_; int64_t bundleAliveTimeoutTimeStamp_; int64_t bundleDailyTimeoutTimeStamp_; - BundleActivePackageHistory() {}; + BundleActivePackageHistory() { + lastBootFromUsedTimeStamp_ = 0; + lastScreenUsedTimeStamp_ = 0; + lastGroupCalculatedTimeStamp_ = 0; + currentGroup_ = DeviceUsageStatsGroupConst::ACTIVE_GROUP_NEVER; + reasonInGroup_ = DeviceUsageStatsGroupConst::GROUP_CONTROL_REASON_DEFAULT; + bundleAliveTimeoutTimeStamp_ = 0; + bundleDailyTimeoutTimeStamp_ = 0; + }; ~BundleActivePackageHistory() {}; }; } diff --git a/services/packagegroup/include/bundle_active_user_history.h b/services/packagegroup/include/bundle_active_user_history.h index edfe564..5449c0f 100644 --- a/services/packagegroup/include/bundle_active_user_history.h +++ b/services/packagegroup/include/bundle_active_user_history.h @@ -47,10 +47,10 @@ public: int64_t GetBootBasedTimeStamp(int64_t bootBasedTimeStamp); int64_t GetScreenOnTimeStamp(int64_t bootBasedTimeStamp); void ReportUsage(std::shared_ptr oneBundleUsageHistory, const std::string& bundleName, - const int newGroup, const int groupReason, const int64_t bootBasedTimeStamp, + const int newGroup, const uint32_t groupReason, const int64_t bootBasedTimeStamp, const int64_t timeUntilNextCheck); void SetBundleGroup(const std::string& bundleName, const int userId, const int64_t bootBasedTimeStamp, - int newGroup, int groupReason, const bool& resetTimeout); + int newGroup, uint32_t groupReason, const bool& resetTimeout); int GetLevelIndex(const std::string& bundleName, const int userId, const int64_t bootBasedTimeStamp, const int64_t screenTimeLevel[4], const int64_t bootFromTimeLevel[4]); void WriteDeviceDuration(); diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 070c8e3..c550135 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -52,7 +52,7 @@ void BundleActiveGroupController::SetHandlerAndCreateUserHistory( const std::shared_ptr& groupHandler, const int64_t bootFromTimeStamp) { if (bundleUserHistory_ == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::SetHandlerAndCreateUserHistory bundleUserHistory_ is null, " + BUNDLE_ACTIVE_LOGI("SetHandlerAndCreateUserHistory bundleUserHistory_ is null, " "called constructor, bootstamp is %{public}lld", bootFromTimeStamp); bundleUserHistory_ = std::make_shared(bootFromTimeStamp); } @@ -96,7 +96,7 @@ bool BundleActiveGroupController::GetBundleMgrProxy() void BundleActiveGroupController::PeriodCheckBundleState(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::PeriodCheckBundleState called"); + BUNDLE_ACTIVE_LOGI("PeriodCheckBundleState called"); if (!activeGroupHandler_.expired()) { BundleActiveGroupHandlerObject tmpGroupHandlerObj; tmpGroupHandlerObj.userId_ = userId; @@ -110,10 +110,10 @@ void BundleActiveGroupController::PeriodCheckBundleState(const int userId) bool BundleActiveGroupController::CheckEachBundleState(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::CheckEachBundleState called, userid is %{public}d", userId); + BUNDLE_ACTIVE_LOGI("CheckEachBundleState called, userid is %{public}d", userId); std::vector allBundlesForUser; if (!GetBundleMgrProxy()) { - BUNDLE_ACTIVE_LOGE("BundleActiveGroupController::CheckEachBundleState get bundle manager proxy failed!"); + BUNDLE_ACTIVE_LOGE("CheckEachBundleState get bundle manager proxy failed!"); return false; } sptrBundleMgr_->GetApplicationInfos(flag, userId, allBundlesForUser); @@ -126,6 +126,7 @@ bool BundleActiveGroupController::CheckEachBundleState(const int userId) for (auto oneBundleName : bundleNamesOfUser) { CheckAndUpdateGroup(oneBundleName, userId, bootBasedTimeStamp); } + //bundleUserHistory_->printdata(userId); return true; } @@ -160,7 +161,7 @@ bool BundleActiveGroupController::calculationTimeOut( - lastGroupCalculatedTimeStamp > timeoutCalculated_; } -int BundleActiveGroupController::EventToGroupReason(const int eventId) +uint32_t BundleActiveGroupController::EventToGroupReason(const int eventId) { switch (eventId) { case BundleActiveEvent::ABILITY_FOREGROUND: @@ -183,7 +184,7 @@ int BundleActiveGroupController::EventToGroupReason(const int eventId) void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp, const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::ReportEvent called"); + BUNDLE_ACTIVE_LOGI("ReportEvent called"); if (bundleGroupEnable_ == false) { return; } @@ -243,8 +244,8 @@ void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleN if (oneBundleHistory == nullptr) { return; } - int groupReason = oneBundleHistory->reasonInGroup_; - int oldGroupControlReason = groupReason & GROUP_CONTROL_REASON_MASK; + uint32_t groupReason = oneBundleHistory->reasonInGroup_; + uint32_t oldGroupControlReason = groupReason & GROUP_CONTROL_REASON_MASK; if (oldGroupControlReason == GROUP_CONTROL_REASON_FORCED) { return; } @@ -271,13 +272,13 @@ void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleN GROUP_EVENT_REASON_ALIVE_TIMEOUT; } if (oldGroup < newGroup) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::CheckAndUpdateGroup called SetBundleGroup"); + BUNDLE_ACTIVE_LOGI("CheckAndUpdateGroup called SetBundleGroup"); bundleUserHistory_->SetBundleGroup(bundleName, userId, bootBasedTimeStamp, newGroup, groupReason, false); } } void BundleActiveGroupController::SetBundleGroup(const std::string& bundleName, const int userId, int newGroup, - int reason, const int64_t bootBasedTimeStamp, const bool& resetTimeout) + uint32_t reason, const int64_t bootBasedTimeStamp, const bool& resetTimeout) { std::lock_guard lock(mutex_); if (IsBundleInstalled(bundleName, userId) == false) { @@ -290,6 +291,9 @@ void BundleActiveGroupController::SetBundleGroup(const std::string& bundleName, int64_t bootBasedTimeStampAdjusted = bundleUserHistory_->GetBootBasedTimeStamp(bootBasedTimeStamp); if (newGroup > ACTIVE_GROUP_ALIVE && oneBundleHistory->bundleAliveTimeoutTimeStamp_ > bootBasedTimeStampAdjusted) { + BUNDLE_ACTIVE_LOGI("%{public}s should be decreased, but time out in alive is not expire! now is %{public}lld," + "timeout is %{public}lld", + bundleName.c_str(), bootBasedTimeStampAdjusted, oneBundleHistory->bundleAliveTimeoutTimeStamp_); newGroup = ACTIVE_GROUP_ALIVE; reason = oneBundleHistory->reasonInGroup_; } else if (newGroup > ACTIVE_GROUP_DAILY && oneBundleHistory->bundleDailyTimeoutTimeStamp_ > @@ -316,11 +320,11 @@ int BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, con if (oneBundleHistory == nullptr) { return 1; } else if (oneBundleHistory->currentGroup_ >= ACTIVE_GROUP_RARE) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::IsBundleIdle, bundle group is %{public}d", + BUNDLE_ACTIVE_LOGI("IsBundleIdle, bundle group is %{public}d", oneBundleHistory->currentGroup_); return 1; } else { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::IsBundleIdle, bundle group is %{public}d", + BUNDLE_ACTIVE_LOGI("IsBundleIdle, bundle group is %{public}d", oneBundleHistory->currentGroup_); return 0; } @@ -328,7 +332,7 @@ int BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, con int BundleActiveGroupController::QueryPackageGroup(const int userId, const std::string& bundleName) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::QueryPackageGroup called"); + BUNDLE_ACTIVE_LOGI("QueryPackageGroup called"); sptr timer = MiscServices::TimeServiceClient::GetInstance(); if (IsBundleInstalled(bundleName, userId) == false) { return -1; @@ -348,7 +352,7 @@ bool BundleActiveGroupController::IsBundleInstalled(const std::string& bundleNam ApplicationInfo bundleInfo; if (sptrBundleMgr_ != nullptr && sptrBundleMgr_->GetApplicationInfo( bundleName, ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, bundleInfo) == false) { - BUNDLE_ACTIVE_LOGE("BundleActiveGroupController::IsBundleInstalled bundle is not installed!"); + BUNDLE_ACTIVE_LOGE("IsBundleInstalled bundle is not installed!"); return false; } return true; @@ -356,14 +360,14 @@ bool BundleActiveGroupController::IsBundleInstalled(const std::string& bundleNam void BundleActiveGroupController::ShutDown(const int64_t bootBasedTimeStamp) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::ShutDown called"); + BUNDLE_ACTIVE_LOGI("ShutDown called"); bundleUserHistory_->UpdateBootBasedAndScreenTime(false, bootBasedTimeStamp, true); } bool BundleActiveGroupController::IsScreenOn() { bool result = PowerMgrClient::GetInstance().IsScreenOn(); - BUNDLE_ACTIVE_LOGI("BundleActiveGroupController::IsScreenOn() is %{public}d", result); + BUNDLE_ACTIVE_LOGI("IsScreenOn() is %{public}d", result); return result; } } diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index ce1e089..ff45231 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -33,9 +33,9 @@ BundleActiveGroupHandler::BundleActiveGroupHandler void BundleActiveGroupHandler::Init(const std::shared_ptr& bundleActiveController) { - BUNDLE_ACTIVE_LOGI("BundleActiveGroupHandler::Init called"); + BUNDLE_ACTIVE_LOGI("Init called"); if (bundleActiveController == nullptr) { - BUNDLE_ACTIVE_LOGE("BundleActiveGroupHandler::Init failed bundleActiveController is null"); + BUNDLE_ACTIVE_LOGE("Init failed bundleActiveController is null"); } bundleActiveGroupController_ = bundleActiveController; } @@ -64,7 +64,7 @@ void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointe BUNDLE_ACTIVE_LOGI("BundleActiveCore::GetAllActiveUser size is 0"); return; } - for (int i = 0; i < osAccountInfos.size(); i++) { + for (uint32_t i = 0; i < osAccountInfos.size(); i++) { bundleActiveGroupController_->CheckEachBundleState(osAccountInfos[i].GetLocalId()); bundleActiveGroupController_->RestoreToDatabase(osAccountInfos[i].GetLocalId()); } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index 3399f2b..f739ac6 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -29,10 +29,10 @@ void BundleActiveUserHistory::WriteDeviceDuration() void BundleActiveUserHistory::WriteBundleUsage(const int userId) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserHistory::WriteBundleUsage called"); + BUNDLE_ACTIVE_LOGI("WriteBundleUsage called"); auto userHistory = GetUserHistory(userId, false); if (userHistory == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserHistory::WriteBundleUsage called, no existed user history, return"); + BUNDLE_ACTIVE_LOGI("WriteBundleUsage called, no existed user history, return"); return; } database_.PutBundleHistoryData(userId, userHistory); @@ -62,6 +62,8 @@ int BundleActiveUserHistory::GetLevelIndex(const string& bundleName, const int u } int64_t screenDiff = GetScreenOnTimeStamp(bootBasedTimeStamp) - oneBundleHistory->lastScreenUsedTimeStamp_; int64_t bootFromDiff = GetBootBasedTimeStamp(bootBasedTimeStamp) - oneBundleHistory->lastBootFromUsedTimeStamp_; + BUNDLE_ACTIVE_LOGI("screendiff is %{public}lld, bootfromdiff is %{public}lld, bundle name is %{public}s", + screenDiff, bootFromDiff, bundleName.c_str()); for (int i = 3; i >= 0; i--) { if (screenDiff >= screenTimeLevel[i] && bootFromDiff >= bootFromTimeLevel[i]) { return i; @@ -92,11 +94,11 @@ shared_ptr>> BundleActiveUser shared_ptr>> usageHistoryInserted = database_.GetBundleHistoryData(userId); if (usageHistoryInserted == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserHistory::GetUserHistory READ FROM DATABASE FAILD"); + BUNDLE_ACTIVE_LOGI("GetUserHistory READ FROM DATABASE FAILD"); usageHistoryInserted = make_shared>>(); } - BUNDLE_ACTIVE_LOGI("BundleActiveUserHistory::GetUserHistory usageHistoryInserted not null"); + BUNDLE_ACTIVE_LOGI("GetUserHistory usageHistoryInserted not null"); userHistory_[userId] = usageHistoryInserted; } return userHistory_[userId]; @@ -139,7 +141,7 @@ shared_ptr BundleActiveUserHistory::GetUsageHistoryF } void BundleActiveUserHistory::ReportUsage(shared_ptr oneBundleUsageHistory, - const string& bundleName, const int newGroup, const int groupReason, const int64_t bootBasedTimeStamp, + const string& bundleName, const int newGroup, const uint32_t groupReason, const int64_t bootBasedTimeStamp, const int64_t timeUntilNextCheck) { if (timeUntilNextCheck > bootBasedTimeStamp) { @@ -166,8 +168,10 @@ void BundleActiveUserHistory::ReportUsage(shared_ptr } void BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int userId, - const int64_t bootBasedTimeStamp, int newGroup, int groupReason, const bool& resetTimeout) + const int64_t bootBasedTimeStamp, int newGroup, uint32_t groupReason, const bool& resetTimeout) { + BUNDLE_ACTIVE_LOGI("set %{public}s to group %{public}d, reason is %{public}d", + bundleName.c_str(), newGroup, groupReason); shared_ptr>> userBundleHistory = GetUserHistory(userId, false); if (userBundleHistory == nullptr) { @@ -207,7 +211,7 @@ void BundleActiveUserHistory::UpdateBootBasedAndScreenTime(const bool& isScreenO void BundleActiveUserHistory::printdata(int userId) { auto oneUserHistory = GetUserHistory(userId, false); - BUNDLE_ACTIVE_LOGI("BundleActiveUserHistory::printdata screen is %{public}d", isScreenOn_); + BUNDLE_ACTIVE_LOGI("printdata screen is %{public}d", isScreenOn_); if (oneUserHistory == nullptr) { return; } diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index d438384..a7f5dad 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -47,10 +47,16 @@ public: std::string abilityId_; int64_t timeStamp_; int eventId_; - bool isidle_; public: - BundleActiveEvent() {}; + BundleActiveEvent() { + bundleName_ = ""; + continuousTaskAbilityName_ = ""; + abilityName_ = ""; + abilityId_ = ""; + timeStamp_ = 0; + eventId_ = 0; + }; BundleActiveEvent(const BundleActiveEvent& orig); BundleActiveEvent(int eventId, int64_t timeStamp); std::string GetBundleName(); @@ -58,7 +64,6 @@ public: std::string GetAbilityId(); int64_t GetTimeStamp(); int GetEventId(); - bool GetIsIdle(); virtual bool Marshalling(Parcel &parcel) const override; std::shared_ptr Unmarshalling(Parcel &parcel); }; diff --git a/services/packageusage/include/bundle_active_event_stats.h b/services/packageusage/include/bundle_active_event_stats.h index a2f3900..18ec8f3 100644 --- a/services/packageusage/include/bundle_active_event_stats.h +++ b/services/packageusage/include/bundle_active_event_stats.h @@ -28,7 +28,13 @@ public: int64_t lastEventTime_; int64_t totalTime_; int count_; - BundleActiveEventStats() {}; + BundleActiveEventStats() { + beginTimeStamp_ = 0; + endTimeStamp_ = 0; + lastEventTime_ = 0; + totalTime_ = 0; + count_ = 0; + }; BundleActiveEventStats(const BundleActiveEventStats& orig); int GetEventId(); int GetFirstTimeStamp(); diff --git a/services/packageusage/src/bundle_active_event.cpp b/services/packageusage/src/bundle_active_event.cpp index c29ebc3..9cb7e68 100644 --- a/services/packageusage/src/bundle_active_event.cpp +++ b/services/packageusage/src/bundle_active_event.cpp @@ -24,7 +24,6 @@ BundleActiveEvent::BundleActiveEvent (const BundleActiveEvent& orig) abilityId_ = orig.abilityId_; timeStamp_ = orig.timeStamp_; eventId_ = orig.eventId_; - isidle_ = orig.isidle_; } BundleActiveEvent::BundleActiveEvent(int eventId, int64_t timeStamp) @@ -61,11 +60,6 @@ int BundleActiveEvent::GetEventId() return eventId_; } -bool BundleActiveEvent::GetIsIdle() -{ - return isidle_; -} - bool BundleActiveEvent::Marshalling(Parcel &parcel) const { if (parcel.WriteString(bundleName_) && diff --git a/services/packageusage/src/bundle_active_event_list.cpp b/services/packageusage/src/bundle_active_event_list.cpp index 86d0c3d..51c081b 100644 --- a/services/packageusage/src/bundle_active_event_list.cpp +++ b/services/packageusage/src/bundle_active_event_list.cpp @@ -33,7 +33,7 @@ void BundleActiveEventList::Clear() void BundleActiveEventList::Insert(BundleActiveEvent event) { - int size = events_.size(); + uint32_t size = events_.size(); if (size == 0 || event.timeStamp_ >= events_.back().timeStamp_) { events_.push_back(event); return; @@ -44,7 +44,7 @@ void BundleActiveEventList::Insert(BundleActiveEvent event) int BundleActiveEventList::FindBestIndex(const int64_t timeStamp) { - int size = events_.size(); + int size = static_cast(events_.size()); int result = size; int lo = 0; int hi = size - 1; @@ -63,7 +63,7 @@ int BundleActiveEventList::FindBestIndex(const int64_t timeStamp) void BundleActiveEventList::Merge(const BundleActiveEventList& right) { - int size = right.events_.size(); + int size = static_cast(right.events_.size()); for (int i = 0; i < size; i++) { Insert(right.events_[i]); } diff --git a/services/packageusage/src/bundle_active_stats_combiner.cpp b/services/packageusage/src/bundle_active_stats_combiner.cpp index 88ddd97..4e63f90 100644 --- a/services/packageusage/src/bundle_active_stats_combiner.cpp +++ b/services/packageusage/src/bundle_active_stats_combiner.cpp @@ -21,7 +21,7 @@ void BundleActiveStatsCombiner::combine( const std::shared_ptr& stats, std::vector& accumulatedResult, int64_t beginTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveStatsCombiner::combine called"); + BUNDLE_ACTIVE_LOGI("BundleActivePackageStats combine called"); for (auto it : stats->bundleStats_) { if (it.second != nullptr) { accumulatedResult.push_back(*(it.second)); @@ -33,9 +33,9 @@ void BundleActiveStatsCombiner::combine(const std::shared_ptr std::vector& accumulatedResult, int64_t beginTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveStatsCombiner::combine called"); + BUNDLE_ACTIVE_LOGI("BundleActiveEvent combine called"); int startIndex = stats->events_.FindBestIndex(beginTime); - int size = stats->events_.events_.size(); + int size = static_cast(stats->events_.events_.size()); for (int i = startIndex; i < size; i++) { accumulatedResult.push_back(stats->events_.events_[i]); } diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 386804d..b2584ae 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -21,7 +21,7 @@ namespace DeviceUsageStats { void BundleActiveUserService::Init(const int64_t timeStamp) { database_.InitDatabaseTableInfo(timeStamp); - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::Init called"); + BUNDLE_ACTIVE_LOGI("Init called"); LoadActiveStats(timeStamp, false, false); std::shared_ptr currentDailyStats = currentStats_[BundleActivePeriodStats::PERIOD_DAILY]; if (currentDailyStats != nullptr) { @@ -29,10 +29,10 @@ void BundleActiveUserService::Init(const int64_t timeStamp) startupEvent.bundleName_ = BundleActiveEvent::DEVICE_EVENT_PACKAGE_NAME; currentDailyStats->AddEvent(startupEvent); for (auto it : currentDailyStats->events_.events_) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::Init event id is %{public}d, time stamp is %{public}lld", + BUNDLE_ACTIVE_LOGI("Init event id is %{public}d, time stamp is %{public}lld", it.eventId_, it.timeStamp_); } - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::Init currentDailyStats begintime is %{public}lld, " + BUNDLE_ACTIVE_LOGI("Init currentDailyStats begintime is %{public}lld, " "expire time is %{public}lld", currentDailyStats->beginTime_, dailyExpiryDate_.GetMilliseconds()); } } @@ -63,8 +63,8 @@ void BundleActiveUserService::DeleteUninstalledBundleStats(const std::string& bu void BundleActiveUserService::RenewTableTime(int64_t oldTime, int64_t newTime) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::RenewTableTime called"); - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::RenewTableTime called current event size is %{public}d", + BUNDLE_ACTIVE_LOGI("RenewTableTime called"); + BUNDLE_ACTIVE_LOGI("RenewTableTime called current event size is %{public}d", currentStats_[0]->events_.Size()); database_.RenewTableTime(newTime - oldTime); } @@ -72,7 +72,7 @@ void BundleActiveUserService::RenewTableTime(int64_t oldTime, int64_t newTime) void BundleActiveUserService::NotifyStatsChanged() { if (!statsChanged_) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::NotifyStatsChanged() set stats changed to true"); + BUNDLE_ACTIVE_LOGI("NotifyStatsChanged() set stats changed to true"); statsChanged_ = true; listener_.OnStatsChanged(userId_); } @@ -137,7 +137,7 @@ void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) void BundleActiveUserService::ReportForShutdown(const BundleActiveEvent& event) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::ReportForShutdown() called"); + BUNDLE_ACTIVE_LOGI("ReportForShutdown() called"); if (event.eventId_ != BundleActiveEvent::SHUTDOWN) { return; } @@ -152,17 +152,19 @@ void BundleActiveUserService::ReportForShutdown(const BundleActiveEvent& event) it->Update(event.bundleName_, event.continuousTaskAbilityName_, event.timeStamp_, event.eventId_, event.abilityId_); } - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::ReportForShutdown called notify"); + BUNDLE_ACTIVE_LOGI("ReportForShutdown called notify"); NotifyStatsChanged(); } void BundleActiveUserService::RestoreStats(bool forced) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::RestoreStats() called, userId is %{public}d", userId_); + BUNDLE_ACTIVE_LOGI("RestoreStats() called, userId is %{public}d", userId_); if (statsChanged_ || forced) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::RestoreStats() stat changed is true"); - for (int i = 0; i < currentStats_.size(); i++) { - database_.UpdateUsageData(i, *(currentStats_[i])); + BUNDLE_ACTIVE_LOGI("RestoreStats() stat changed is true"); + for (uint32_t i = 0; i < currentStats_.size(); i++) { + if (currentStats_[i] != nullptr) { + database_.UpdateUsageData(i, *(currentStats_[i])); + } if (i == 0) { BUNDLE_ACTIVE_LOGI("RESOTRE EVENT SIZE IS %{public}d, USER ID IS %{public}d", currentStats_[i]->events_.Size(), userId_); @@ -175,11 +177,11 @@ void BundleActiveUserService::RestoreStats(bool forced) void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const bool& force, const bool& timeChanged) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::LoadActiveStats called"); + BUNDLE_ACTIVE_LOGI("LoadActiveStats called"); BundleActiveCalendar tmpCalendar(0); tmpCalendar.SetMilliseconds(timeStamp); tmpCalendar.TruncateTo(BundleActivePeriodStats::PERIOD_DAILY); - for (int intervalType = 0; intervalType < PERIOD_LENGTH.size(); intervalType++) { + for (uint32_t intervalType = 0; intervalType < PERIOD_LENGTH.size(); intervalType++) { if (!force && currentStats_[intervalType] != nullptr && currentStats_[intervalType]->beginTime_ == tmpCalendar.GetMilliseconds()) { continue; @@ -187,7 +189,7 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo std::shared_ptr stats = database_.GetCurrentUsageData(intervalType, userId_); currentStats_[intervalType].reset(); // 当前interval stat置空 if (stats != nullptr) { // 找出最近的stats - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::LoadActiveStats inter type is %{public}d, " + BUNDLE_ACTIVE_LOGI("LoadActiveStats inter type is %{public}d, " "bundle size is %{public}d", intervalType, stats->bundleStats_.size()); // 如果当前时间在stats的统计时间范围内,则可以从数据库加载数据 BUNDLE_ACTIVE_LOGI("interval type is %{public}d, database stat BEGIN time is %{public}lld, " @@ -200,7 +202,7 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo if (currentStats_[intervalType] != nullptr) { continue; } - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::LoadActiveStats [Server]create new interval stats!"); + BUNDLE_ACTIVE_LOGI("LoadActiveStats [Server]create new interval stats!"); currentStats_[intervalType] = std::make_shared(); currentStats_[intervalType]->userId_ = userId_; currentStats_[intervalType]->beginTime_ = tmpCalendar.GetMilliseconds(); @@ -218,7 +220,7 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo dailyExpiryDate_.TruncateToDay(); } listener_.OnStatsReload(); - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::LoadActiveStats current expire time is %{public}lld, " + BUNDLE_ACTIVE_LOGI("LoadActiveStats current expire time is %{public}lld, " "begin time is %{public}lld", dailyExpiryDate_.GetMilliseconds(), tmpCalendar.GetMilliseconds()); } @@ -284,7 +286,7 @@ std::vector BundleActiveUserService::QueryPackageStats intervalType = BundleActivePeriodStats::PERIOD_DAILY; } } - if (intervalType < 0 || intervalType >= currentStats_.size()) { + if (intervalType < 0 || intervalType >= static_cast(currentStats_.size())) { return result; } auto currentStats = currentStats_[intervalType]; @@ -306,7 +308,7 @@ std::vector BundleActiveUserService::QueryPackageStats result = database_.QueryDatabaseUsageStats(intervalType, beginTime, truncatedEndTime, userId); // if we need a in-memory stats, combine current stats with result from database. if (currentStats->endTime_ != 0 && endTime > currentStats->beginTime_) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::QueryPackageStats need in memory stats"); + BUNDLE_ACTIVE_LOGI("QueryPackageStats need in memory stats"); for (auto it : currentStats->bundleStats_) { if (bundleName.empty()) { if ((it.second->totalInFrontTime_ != 0 || it.second->totalContiniousTaskUsedTime_ != 0) && @@ -328,7 +330,7 @@ std::vector BundleActiveUserService::QueryPackageStats std::vector BundleActiveUserService::QueryEvents(const int64_t beginTime, const int64_t endTime, const int userId, const std::string& bundleName) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::QueryEvents called"); + BUNDLE_ACTIVE_LOGI("QueryEvents called"); std::vector result; auto currentStats = currentStats_[BundleActivePeriodStats::PERIOD_DAILY]; if (currentStats == nullptr) { @@ -338,12 +340,12 @@ std::vector BundleActiveUserService::QueryEvents(const int64_ if (beginTime >= currentStats->endTime_) { return result; } - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::QueryEvents bundle name is %{public}s", bundleName.c_str()); + BUNDLE_ACTIVE_LOGI("QueryEvents 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()); // if we need a in-memory stats, combine current stats with result from database. if (currentStats->endTime_ != 0 && endTime > currentStats->beginTime_) { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::QueryEvents need in memory stats"); + BUNDLE_ACTIVE_LOGI("QueryEvents need in memory stats"); int eventBeginIdx = currentStats->events_.FindBestIndex(beginTime); int eventSize = currentStats->events_.Size(); for (int i = eventBeginIdx; i < eventSize; i++) { @@ -359,7 +361,7 @@ std::vector BundleActiveUserService::QueryEvents(const int64_ void BundleActiveUserService::printstat() { - BUNDLE_ACTIVE_LOGI("BundleActiveUserService::printstat called"); + BUNDLE_ACTIVE_LOGI("printstat called"); int idx = 0; for (auto it : currentStats_[idx]->bundleStats_) { BUNDLE_ACTIVE_LOGI("bundle name is %{public}s", it.first.c_str()); @@ -368,7 +370,7 @@ void BundleActiveUserService::printstat() BUNDLE_ACTIVE_LOGI("event stat is, totaltime is %{public}lld, lasttimeused is %{public}lld", totalusedtime, lasttimeused); } - int size = currentStats_[idx]->events_.events_.size(); + 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_; std::string abilityname = currentStats_[idx]->events_.events_[i].abilityName_; -- Gitee From c83fcd91beac219637b17971bf5cb7c368aa4296 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Sat, 5 Mar 2022 19:30:48 +0800 Subject: [PATCH 2/4] =?UTF-8?q?codex=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0debug=E6=97=A5=E5=BF=97=EF=BC=8C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E8=B0=83=E6=95=B4=EF=BC=8C?= =?UTF-8?q?review=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/common/src/bundle_active_core.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 07cceba..0fba16f 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -268,6 +268,7 @@ void BundleActiveCore::RestoreToDatabaseLocked(const int userId) void BundleActiveCore::ShutDown() { + std::lock_guard lock(mutex_); BUNDLE_ACTIVE_LOGI("ShutDown called"); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t timeStamp = timer->GetBootTimeMs(); -- Gitee From fd8c6ec4ff9d0147f63f7628d2f8c1a01aa129e5 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Sat, 5 Mar 2022 19:48:25 +0800 Subject: [PATCH 3/4] adjust postion of constructor Signed-off-by: houdisheng --- services/common/include/bundle_active_core.h | 5 +---- services/common/src/bundle_active_core.cpp | 6 ++++++ .../include/bundle_active_group_handler.h | 5 +---- .../include/bundle_active_package_history.h | 10 +--------- .../src/bundle_active_group_controller.cpp | 6 +++++- .../packagegroup/src/bundle_active_user_history.cpp | 11 +++++++++++ services/packageusage/include/bundle_active_event.h | 9 +-------- .../packageusage/include/bundle_active_event_stats.h | 8 +------- services/packageusage/src/bundle_active_event.cpp | 10 ++++++++++ .../packageusage/src/bundle_active_event_stats.cpp | 9 +++++++++ 10 files changed, 46 insertions(+), 33 deletions(-) diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 51a5264..5464548 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -36,10 +36,7 @@ public: BundleActiveEvent event_; int userId_; std::string bundleName_; - BundleActiveReportHandlerObject() { - userId_ = -1; - bundleName_ = ""; - }; + BundleActiveReportHandlerObject(); BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig); ~BundleActiveReportHandlerObject() {}; }; diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 0fba16f..25fe675 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -23,6 +23,12 @@ namespace OHOS { namespace DeviceUsageStats { +BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() +{ + userId_ = -1; + bundleName_ = ""; +} + BundleActiveReportHandlerObject::BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig) { event_.bundleName_ = orig.event_.bundleName_; diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 3427c0a..f498c65 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -29,10 +29,7 @@ class BundleActiveGroupHandlerObject { public: std::string bundleName_; int userId_; - BundleActiveGroupHandlerObject() { - bundleName_ = ""; - userId_ = -1; - }; + BundleActiveGroupHandlerObject(); BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject& orig); ~BundleActiveGroupHandlerObject() {}; }; diff --git a/services/packagegroup/include/bundle_active_package_history.h b/services/packagegroup/include/bundle_active_package_history.h index d08e4d3..ff25c7a 100644 --- a/services/packagegroup/include/bundle_active_package_history.h +++ b/services/packagegroup/include/bundle_active_package_history.h @@ -30,15 +30,7 @@ public: uint32_t reasonInGroup_; int64_t bundleAliveTimeoutTimeStamp_; int64_t bundleDailyTimeoutTimeStamp_; - BundleActivePackageHistory() { - lastBootFromUsedTimeStamp_ = 0; - lastScreenUsedTimeStamp_ = 0; - lastGroupCalculatedTimeStamp_ = 0; - currentGroup_ = DeviceUsageStatsGroupConst::ACTIVE_GROUP_NEVER; - reasonInGroup_ = DeviceUsageStatsGroupConst::GROUP_CONTROL_REASON_DEFAULT; - bundleAliveTimeoutTimeStamp_ = 0; - bundleDailyTimeoutTimeStamp_ = 0; - }; + BundleActivePackageHistory(); ~BundleActivePackageHistory() {}; }; } diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index c550135..f89d4bf 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -23,6 +23,11 @@ namespace OHOS { namespace DeviceUsageStats { using namespace DeviceUsageStatsGroupConst; +BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject() +{ + bundleName_ = ""; + userId_ = -1; +} void BundleActiveGroupController::RestoreDurationToDatabase() { @@ -126,7 +131,6 @@ bool BundleActiveGroupController::CheckEachBundleState(const int userId) for (auto oneBundleName : bundleNamesOfUser) { CheckAndUpdateGroup(oneBundleName, userId, bootBasedTimeStamp); } - //bundleUserHistory_->printdata(userId); return true; } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index f739ac6..c06e6dc 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -22,6 +22,17 @@ namespace DeviceUsageStats { using namespace DeviceUsageStatsGroupConst; using namespace std; +BundleActivePackageHistory::BundleActivePackageHistory() +{ + lastBootFromUsedTimeStamp_ = 0; + lastScreenUsedTimeStamp_ = 0; + lastGroupCalculatedTimeStamp_ = 0; + currentGroup_ = DeviceUsageStatsGroupConst::ACTIVE_GROUP_NEVER; + reasonInGroup_ = DeviceUsageStatsGroupConst::GROUP_CONTROL_REASON_DEFAULT; + bundleAliveTimeoutTimeStamp_ = 0; + bundleDailyTimeoutTimeStamp_ = 0; +}; + void BundleActiveUserHistory::WriteDeviceDuration() { database_.PutDurationData(bootBasedDuration_, ScreenOnDuration_); diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index a7f5dad..4e66b23 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -49,14 +49,7 @@ public: int eventId_; public: - BundleActiveEvent() { - bundleName_ = ""; - continuousTaskAbilityName_ = ""; - abilityName_ = ""; - abilityId_ = ""; - timeStamp_ = 0; - eventId_ = 0; - }; + BundleActiveEvent(); BundleActiveEvent(const BundleActiveEvent& orig); BundleActiveEvent(int eventId, int64_t timeStamp); std::string GetBundleName(); diff --git a/services/packageusage/include/bundle_active_event_stats.h b/services/packageusage/include/bundle_active_event_stats.h index 18ec8f3..77d9e2d 100644 --- a/services/packageusage/include/bundle_active_event_stats.h +++ b/services/packageusage/include/bundle_active_event_stats.h @@ -28,13 +28,7 @@ public: int64_t lastEventTime_; int64_t totalTime_; int count_; - BundleActiveEventStats() { - beginTimeStamp_ = 0; - endTimeStamp_ = 0; - lastEventTime_ = 0; - totalTime_ = 0; - count_ = 0; - }; + BundleActiveEventStats(); BundleActiveEventStats(const BundleActiveEventStats& orig); int GetEventId(); int GetFirstTimeStamp(); diff --git a/services/packageusage/src/bundle_active_event.cpp b/services/packageusage/src/bundle_active_event.cpp index 9cb7e68..bc6c81f 100644 --- a/services/packageusage/src/bundle_active_event.cpp +++ b/services/packageusage/src/bundle_active_event.cpp @@ -17,6 +17,16 @@ namespace OHOS { namespace DeviceUsageStats { +BundleActiveEvent::BundleActiveEvent() +{ + bundleName_ = ""; + continuousTaskAbilityName_ = ""; + abilityName_ = ""; + abilityId_ = ""; + timeStamp_ = 0; + eventId_ = 0; +} + BundleActiveEvent::BundleActiveEvent (const BundleActiveEvent& orig) { bundleName_ = orig.bundleName_; diff --git a/services/packageusage/src/bundle_active_event_stats.cpp b/services/packageusage/src/bundle_active_event_stats.cpp index 02d527c..4dfa187 100644 --- a/services/packageusage/src/bundle_active_event_stats.cpp +++ b/services/packageusage/src/bundle_active_event_stats.cpp @@ -17,6 +17,15 @@ namespace OHOS { namespace DeviceUsageStats { +BundleActiveEventStats::BundleActiveEventStats() +{ + beginTimeStamp_ = 0; + endTimeStamp_ = 0; + lastEventTime_ = 0; + totalTime_ = 0; + count_ = 0; +} + BundleActiveEventStats::BundleActiveEventStats(const BundleActiveEventStats& orig) { eventId_ = orig.eventId_; -- Gitee From aea74466fb7bf46908c46e4d906c38953e621bf6 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Sat, 5 Mar 2022 20:04:47 +0800 Subject: [PATCH 4/4] revise print data content Signed-off-by: houdisheng --- services/packagegroup/src/bundle_active_user_history.cpp | 9 +++++---- services/packageusage/src/bundle_active_user_service.cpp | 5 ++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index c06e6dc..5179f8f 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -73,8 +73,9 @@ int BundleActiveUserHistory::GetLevelIndex(const string& bundleName, const int u } int64_t screenDiff = GetScreenOnTimeStamp(bootBasedTimeStamp) - oneBundleHistory->lastScreenUsedTimeStamp_; int64_t bootFromDiff = GetBootBasedTimeStamp(bootBasedTimeStamp) - oneBundleHistory->lastBootFromUsedTimeStamp_; - BUNDLE_ACTIVE_LOGI("screendiff is %{public}lld, bootfromdiff is %{public}lld, bundle name is %{public}s", - screenDiff, bootFromDiff, bundleName.c_str()); + BUNDLE_ACTIVE_LOGI("screendiff is %{public}lld, bootfromdiff is %{public}lld, bundle name is %{public}s," + "userid is %{public}d", + screenDiff, bootFromDiff, bundleName.c_str(), userId); for (int i = 3; i >= 0; i--) { if (screenDiff >= screenTimeLevel[i] && bootFromDiff >= bootFromTimeLevel[i]) { return i; @@ -181,8 +182,8 @@ void BundleActiveUserHistory::ReportUsage(shared_ptr void BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int userId, const int64_t bootBasedTimeStamp, int newGroup, uint32_t groupReason, const bool& resetTimeout) { - BUNDLE_ACTIVE_LOGI("set %{public}s to group %{public}d, reason is %{public}d", - bundleName.c_str(), newGroup, groupReason); + BUNDLE_ACTIVE_LOGI("set %{public}s to group %{public}d, reason is %{public}d, userId is %{public}d", + bundleName.c_str(), newGroup, groupReason, userId); shared_ptr>> userBundleHistory = GetUserHistory(userId, false); if (userBundleHistory == nullptr) { diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index b2584ae..4686d76 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -85,11 +85,10 @@ void BundleActiveUserService::NotifyNewUpdate() void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) { - BUNDLE_ACTIVE_LOGI(" BundleActiveUserService::ReportEvent later than daily expire, B time is %{public}lld," - "E time is %{public}lld", + BUNDLE_ACTIVE_LOGI("ReportEvent, B time is %{public}lld, E time is %{public}lld", currentStats_[0]->beginTime_, dailyExpiryDate_.GetMilliseconds()); if (event.timeStamp_ >= dailyExpiryDate_.GetMilliseconds()) { - BUNDLE_ACTIVE_LOGI(" BundleActiveUserService::ReportEvent later than daily expire, renew data in memory"); + BUNDLE_ACTIVE_LOGI("ReportEvent later than daily expire, renew data in memory"); RenewStatsInMemory(event.timeStamp_); } std::shared_ptr currentDailyStats = currentStats_[BundleActivePeriodStats::PERIOD_DAILY]; -- Gitee