From d8b197576a6a61dde5d2a6f163c11a90b1e551a7 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Tue, 1 Mar 2022 20:33:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E6=B5=8B=E8=AF=95=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/common/include/bundle_active_log.h | 2 +- .../common/include/bundle_active_service.h | 5 +- .../src/bundle_active_app_state_obsever.cpp | 2 +- ...bundle_active_continuous_task_observer.cpp | 2 +- services/common/src/bundle_active_core.cpp | 2 + services/common/src/bundle_active_service.cpp | 112 ++++++++++-------- .../include/bundle_active_group_controller.h | 1 + .../include/bundle_active_group_handler.h | 2 +- .../src/bundle_active_group_controller.cpp | 11 ++ .../include/bundle_active_event.h | 2 +- .../src/bundle_active_package_stats.cpp | 2 +- .../src/bundle_active_period_stats.cpp | 6 +- .../src/bundle_active_user_service.cpp | 6 +- 13 files changed, 90 insertions(+), 65 deletions(-) diff --git a/services/common/include/bundle_active_log.h b/services/common/include/bundle_active_log.h index d2056ed..3a24733 100644 --- a/services/common/include/bundle_active_log.h +++ b/services/common/include/bundle_active_log.h @@ -19,7 +19,7 @@ #include "hilog/log.h" #define LOG_TAG_BUNDLE_ACTIVE "BUNDLE_ACTIVE" -#define LOG_TAG_DOMAIN_ID_BUNDLE_ACTIVE 0xD001700 +#define LOG_TAG_DOMAIN_ID_BUNDLE_ACTIVE 0xD001701 static constexpr OHOS::HiviewDFX::HiLogLabel BUNDLE_ACTIVE_LOG_LABEL = { LOG_CORE, diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 3b5ee1b..c0ec217 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -54,8 +54,6 @@ public: protected: void OnStart() override; void OnStop() override; - void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; private: std::shared_ptr bundleActiveCore_; @@ -64,7 +62,10 @@ private: std::shared_ptr continuousTaskObserver_; sptr sptrBundleMgr_; sptr shutdownCallback_; + std::shared_ptr runner_; + std::shared_ptr handler_; int ConvertIntervalType(const int intervalType); + void InitNecessaryState(); bool GetBundleMgrProxy(); bool CheckBundleIsSystemAppAndHasPermission(const int uid, const int userId); void InitAppStateSubscriber(const std::shared_ptr& reportHandler); diff --git a/services/common/src/bundle_active_app_state_obsever.cpp b/services/common/src/bundle_active_app_state_obsever.cpp index 2e458c1..cf613c6 100644 --- a/services/common/src/bundle_active_app_state_obsever.cpp +++ b/services/common/src/bundle_active_app_state_obsever.cpp @@ -48,7 +48,7 @@ void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData tmpHandlerObject.event_.bundleName_ = abilityStateData.bundleName; tmpHandlerObject.event_.abilityName_ = abilityStateData.abilityName; tmpHandlerObject.event_.abilityId_ = abilityStateData.abilityName; - tmpHandlerObject.event_.ContinuousTaskAbilityName_ = ""; + tmpHandlerObject.event_.continuousTaskAbilityName_ = ""; tmpHandlerObject.userId_ = userId; sptr timer = MiscServices::TimeServiceClient::GetInstance(); tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); diff --git a/services/common/src/bundle_active_continuous_task_observer.cpp b/services/common/src/bundle_active_continuous_task_observer.cpp index 4db0837..459524f 100644 --- a/services/common/src/bundle_active_continuous_task_observer.cpp +++ b/services/common/src/bundle_active_continuous_task_observer.cpp @@ -92,7 +92,7 @@ void BundleActiveContinuousTaskObserver::ReportContinuousTaskEvent( tmpHandlerObject.event_.bundleName_ = bundleName; tmpHandlerObject.event_.abilityName_ = ""; tmpHandlerObject.event_.abilityId_ = abilityId; - tmpHandlerObject.event_.ContinuousTaskAbilityName_ = abiliytName; + tmpHandlerObject.event_.continuousTaskAbilityName_ = abiliytName; tmpHandlerObject.userId_ = userId; sptr timer = MiscServices::TimeServiceClient::GetInstance(); tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index d0f7505..5f09345 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -31,6 +31,7 @@ BundleActiveReportHandlerObject::BundleActiveReportHandlerObject(const BundleAct 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_; } @@ -195,6 +196,7 @@ void BundleActiveCore::OnBundleUninstalled(const int userId, const std::string& return; } service->DeleteUninstalledBundleStats(bundleName); + bundleGroupController_->OnBundleUninstalled(userId, bundleName); } void BundleActiveCore::OnStatsChanged() diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 9d7728a..9b7bfd2 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -28,6 +28,7 @@ namespace DeviceUsageStats { static const int PERIOD_BEST_JS = 0; static const int PERIOD_YEARLY_JS = 4; static const int PERIOD_BEST_SERVICE = 4; +static const int DELAY_TIME = 2000; REGISTER_SYSTEM_ABILITY_BY_ID(BundleActiveService, DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, true); using namespace OHOS::Security::AccessToken; using AccessTokenKit = OHOS::Security::AccessToken::AccessTokenKit; @@ -36,68 +37,77 @@ const std::string NEEDED_PERMISSION = "ohos.permission.BUNDLE_ACTIVE_INFO"; void BundleActiveService::OnStart() { BUNDLE_ACTIVE_LOGI("BundleActiveService::OnStart() called"); + runner_ = AppExecFwk::EventRunner::Create("device_usage_stats_init_handler"); + if (runner_ == nullptr) { + BUNDLE_ACTIVE_LOGI("BundleActiveService runner create failed!"); + return; + } + handler_ = std::make_shared(runner_); + if (handler_ == nullptr) { + BUNDLE_ACTIVE_LOGI("BundleActiveService handler create failed!"); + return; + } + + InitNecessaryState(); int ret = Publish(this); if (!ret) { BUNDLE_ACTIVE_LOGE("[Server] OnStart, Register SystemAbility[1907] FAIL."); return; } - AddSystemAbilityListener(POWER_MANAGER_SERVICE_ID); - AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - AddSystemAbilityListener(APP_MGR_SERVICE_ID); - AddSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID); BUNDLE_ACTIVE_LOGI("[Server] OnStart, Register SystemAbility[1907] SUCCESS."); return; } -void BundleActiveService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +void BundleActiveService::InitNecessaryState() { - BUNDLE_ACTIVE_LOGI("BundleActiveService::OnAddSystemAbility called, SAID is %{public}d", systemAbilityId); - if (systemAbilityId == POWER_MANAGER_SERVICE_ID) { - if (bundleActiveCore_ == nullptr) { - bundleActiveCore_ = std::make_shared(); - bundleActiveCore_->Init(); + sptr systemAbilityManager + = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr + || systemAbilityManager->CheckSystemAbility(APP_MGR_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(TIME_SERVICE_ID) == nullptr) { + BUNDLE_ACTIVE_LOGI("request system service is not ready yet!"); + auto task = [this]() { this->InitNecessaryState(); }; + handler_->PostTask(task, DELAY_TIME); + return; + } + + if (bundleActiveCore_ == nullptr) { + bundleActiveCore_ = std::make_shared(); + bundleActiveCore_->Init(); + } + if (reportHandler_ == nullptr) { + std::string threadName = "bundle_active_report_handler"; + auto runner = AppExecFwk::EventRunner::Create(threadName); + if (runner == nullptr) { + BUNDLE_ACTIVE_LOGE("report handler is null"); + return; } + reportHandler_ = std::make_shared(runner); if (reportHandler_ == nullptr) { - std::string threadName = "bundle_active_report_handler"; - auto runner = AppExecFwk::EventRunner::Create(threadName); - if (runner == nullptr) { - BUNDLE_ACTIVE_LOGE("report handler is null"); - return; - } - reportHandler_ = std::make_shared(runner); - if (reportHandler_ == nullptr) { - return; - } - reportHandler_->Init(bundleActiveCore_); - } - if (reportHandler_ != nullptr && bundleActiveCore_ != nullptr) { - BUNDLE_ACTIVE_LOGI("core and handler is not null"); - bundleActiveCore_->SetHandler(reportHandler_); - } else { return; } - shutdownCallback_ = new BundleActiveShutdownCallbackService(bundleActiveCore_); - auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); - powerManagerClient.RegisterShutdownCallback(shutdownCallback_); - InitAppStateSubscriber(reportHandler_); - InitContinuousSubscriber(reportHandler_); - } - if (systemAbilityId == APP_MGR_SERVICE_ID) { - if (reportHandler_ != nullptr) { - SubscribeAppState(); - } + reportHandler_->Init(bundleActiveCore_); } - if (systemAbilityId == BACKGROUND_TASK_MANAGER_SERVICE_ID) { - if (reportHandler_ != nullptr) { - SubscribeContinuousTask(); - } - } - if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) { - bundleActiveCore_->InitBundleGroupController(); + if (reportHandler_ != nullptr && bundleActiveCore_ != nullptr) { + BUNDLE_ACTIVE_LOGI("core and handler is not null"); + bundleActiveCore_->SetHandler(reportHandler_); + } else { + return; } + shutdownCallback_ = new BundleActiveShutdownCallbackService(bundleActiveCore_); + auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); + powerManagerClient.RegisterShutdownCallback(shutdownCallback_); + InitAppStateSubscriber(reportHandler_); + InitContinuousSubscriber(reportHandler_); + bundleActiveCore_->InitBundleGroupController(); + SubscribeAppState(); + SubscribeContinuousTask(); } - OHOS::sptr BundleActiveService::GetAppManagerInstance() { OHOS::sptr systemAbilityManager = @@ -154,10 +164,6 @@ bool BundleActiveService::SubscribeContinuousTask() return true; } -void BundleActiveService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) -{ -} - void BundleActiveService::OnStop() { if (shutdownCallback_ != nullptr) { @@ -181,7 +187,7 @@ int BundleActiveService::ReportEvent(std::string& bundleName, std::string& abili tmpHandlerObject.event_.abilityName_ = abilityName; tmpHandlerObject.event_.abilityId_ = abilityId; tmpHandlerObject.event_.eventId_ = eventId; - tmpHandlerObject.event_.ContinuousTaskAbilityName_ = continuousTask; + tmpHandlerObject.event_.continuousTaskAbilityName_ = continuousTask; tmpHandlerObject.userId_ = userId; sptr timer = MiscServices::TimeServiceClient::GetInstance(); tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); @@ -204,8 +210,12 @@ bool BundleActiveService::IsBundleIdle(const std::string& bundleName) 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); - bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId); - if (isSystemAppAndHasPermission == true) { + if (!GetBundleMgrProxy()) { + BUNDLE_ACTIVE_LOGE("Get bundle manager proxy failed!"); + return true; + } + bool bundleIsSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(callingUid); + if (bundleIsSystemApp == true) { result = bundleActiveCore_->IsBundleIdle(bundleName, userId); } } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index a8a2ec1..c0589b7 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -73,6 +73,7 @@ public: void CheckIdleStatsOneTime(); void PeriodCheckBundleState(const int userId); 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, const int64_t bootBasedTimeStamp, const bool& resetTimeout); diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 2bf694b..cb9ec0d 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 = THREE_HOUR; + static const int CHECK_IDLE_INTERVAL = TEN_MINUTE; 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 9a4ed7c..ca79fea 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -60,6 +60,17 @@ void BundleActiveGroupController::SetHandlerAndCreateUserHistory( activeGroupHandler_ = groupHandler; } +void BundleActiveGroupController::OnBundleUninstalled(const int userId, const std::string bundleName) +{ + std::lock_guard lock(mutex_); + auto oneUserHistory = bundleUserHistory_->GetUserHistory(userId, false); + if (oneUserHistory == nullptr) { + return; + } + oneUserHistory->erase(bundleName); + bundleUserHistory_->WriteBundleUsage(userId); +} + bool BundleActiveGroupController::GetBundleMgrProxy() { if (!sptrBundleMgr_) { diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index e0d6a8f..d438384 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -42,7 +42,7 @@ public: static const int NOTIFICATION_SEEN = 17; inline static const std::string DEVICE_EVENT_PACKAGE_NAME = "openharmony"; std::string bundleName_; - std::string ContinuousTaskAbilityName_; + std::string continuousTaskAbilityName_; std::string abilityName_; std::string abilityId_; int64_t timeStamp_; diff --git a/services/packageusage/src/bundle_active_package_stats.cpp b/services/packageusage/src/bundle_active_package_stats.cpp index b081c3d..01048a2 100644 --- a/services/packageusage/src/bundle_active_package_stats.cpp +++ b/services/packageusage/src/bundle_active_package_stats.cpp @@ -189,7 +189,7 @@ void BundleActivePackageStats::UpdateLongTimeTask(const std::string& longTimeTas switch (eventId) { case BundleActiveEvent::LONG_TIME_TASK_STARTTED: - if (AnyLongTimeTaskStarted()) { + if (!AnyLongTimeTaskStarted()) { lastContiniousTaskUsed_ = timeStamp; } longTimeTasks_[longTimeTaskName] = eventId; diff --git a/services/packageusage/src/bundle_active_period_stats.cpp b/services/packageusage/src/bundle_active_period_stats.cpp index a3eb481..e9f0b71 100644 --- a/services/packageusage/src/bundle_active_period_stats.cpp +++ b/services/packageusage/src/bundle_active_period_stats.cpp @@ -47,7 +47,7 @@ BundleActiveEvent BundleActivePeriodStats::BuildEvent(std::string bundleName, st BundleActiveEvent newEvent; newEvent.bundleName_ = bundleName; if (!timeTaskName.empty()) { - newEvent.ContinuousTaskAbilityName_ = timeTaskName; + newEvent.continuousTaskAbilityName_ = timeTaskName; } return newEvent; } @@ -75,8 +75,8 @@ void BundleActivePeriodStats::Update(const std::string bundleName, const std::st void BundleActivePeriodStats::AddEvent(BundleActiveEvent event) { event.bundleName_ = GetCachedString(event.bundleName_); - if (!event.ContinuousTaskAbilityName_.empty()) { - event.ContinuousTaskAbilityName_ = GetCachedString(event.ContinuousTaskAbilityName_); + if (!event.continuousTaskAbilityName_.empty()) { + event.continuousTaskAbilityName_ = GetCachedString(event.continuousTaskAbilityName_); } events_.Insert(event); if (event.timeStamp_ > endTime_) { diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 7e4efcf..cdc1639 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -123,7 +123,7 @@ void BundleActiveUserService::ReportEvent(const BundleActiveEvent& event) it->UpdateKeyguardHidden(event.timeStamp_); break; default: - it->Update(event.bundleName_, event.ContinuousTaskAbilityName_, event.timeStamp_, event.eventId_, + it->Update(event.bundleName_, event.continuousTaskAbilityName_, event.timeStamp_, event.eventId_, event.abilityId_); if (incrementBundleLaunch) { BUNDLE_ACTIVE_LOGI(" BundleActiveUserService::ReportEvent increase bundle started count"); @@ -150,7 +150,7 @@ void BundleActiveUserService::ReportForFlushAndShutdown(const BundleActiveEvent& RenewStatsInMemory(event.timeStamp_); } for (auto it : currentStats_) { - it->Update(event.bundleName_, event.ContinuousTaskAbilityName_, event.timeStamp_, event.eventId_, + it->Update(event.bundleName_, event.continuousTaskAbilityName_, event.timeStamp_, event.eventId_, event.abilityId_); } BUNDLE_ACTIVE_LOGI("BundleActiveUserService::ReportForFlushAndShutdown called notify"); @@ -257,7 +257,7 @@ void BundleActiveUserService::RenewStatsInMemory(const int64_t timeStamp) if (continueServices.find(continueBundleName) != continueServices.end()) { for (std::map::iterator it = continueServices[continueBundleName].begin(); it != continueServices[continueBundleName].end(); it++) { - (*itInterval)->Update(continueBundleName, it->first, beginTime, it->second, 0); + (*itInterval)->Update(continueBundleName, it->first, beginTime, it->second, ""); } } NotifyStatsChanged(); -- Gitee