From 7e75febbb2c0fc2838b785f4c0429e1fb5188c77 Mon Sep 17 00:00:00 2001 From: houdisheng Date: Thu, 10 Mar 2022 15:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=B8=BB=E5=B9=B2=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- BUILD.gn | 3 --- bundle.json | 1 - .../common/include/bundle_active_service.h | 1 + services/common/src/bundle_active_core.cpp | 7 ++++-- services/common/src/bundle_active_service.cpp | 23 ++++++++++++++++++- .../src/bundle_active_usage_database.cpp | 9 +++++++- .../include/bundle_active_package_history.h | 1 + .../src/bundle_active_user_history.cpp | 2 ++ 8 files changed, 39 insertions(+), 8 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index cf37d4b..9505c91 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -34,7 +34,6 @@ ohos_shared_library("usagestatsinner") { ] external_deps = [ - "dmsfwk_standard:zuri", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", @@ -74,7 +73,6 @@ ohos_shared_library("bundlestate") { ] external_deps = [ - "dmsfwk_standard:zuri", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "napi:ace_napi", @@ -131,7 +129,6 @@ ohos_shared_library("usagestatservice") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", - "dmsfwk_standard:zuri", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/bundle.json b/bundle.json index 374597e..8dbbd21 100644 --- a/bundle.json +++ b/bundle.json @@ -31,7 +31,6 @@ "want", "appexecfwk_base", "appexecfwk_core", - "zuri", "base", "time_service", "utils", diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index c0ec217..64732cd 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -66,6 +66,7 @@ private: std::shared_ptr handler_; int ConvertIntervalType(const int intervalType); void InitNecessaryState(); + void InitService(); 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_core.cpp b/services/common/src/bundle_active_core.cpp index 052dbfa..7e9ff88 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -218,13 +218,16 @@ void BundleActiveCore::OnBundleUninstalled(const int userId, const std::string& void BundleActiveCore::OnStatsChanged(const int userId) { if (!handler_.expired()) { - BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event"); BundleActiveReportHandlerObject tmpHandlerObject; tmpHandlerObject.userId_ = userId; std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr); - handler_.lock()->SendEvent(event, FLUSH_INTERVAL); + if (handler_.lock()->HasInnerEvent(static_cast(BundleActiveReportHandler::MSG_FLUSH_TO_DISK)) == + false) { + BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event"); + handler_.lock()->SendEvent(event, FLUSH_INTERVAL); + } } } diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 5673785..652450d 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -75,6 +75,24 @@ void BundleActiveService::InitNecessaryState() return; } + if (systemAbilityManager == nullptr + || systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr + || systemAbilityManager->GetSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID) == nullptr) { + BUNDLE_ACTIVE_LOGI("request system service object is not ready yet!"); + auto task = [this]() { this->InitNecessaryState(); }; + handler_->PostTask(task, DELAY_TIME); + return; + } + + InitService(); +} + +void BundleActiveService::InitService() +{ if (bundleActiveCore_ == nullptr) { bundleActiveCore_ = std::make_shared(); bundleActiveCore_->Init(); @@ -118,6 +136,9 @@ OHOS::sptr BundleActiveService::GetAppManagerInstance OHOS::sptr systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); OHOS::sptr object = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID); + if (!object) { + return nullptr; + } return OHOS::iface_cast(object); } @@ -141,7 +162,7 @@ bool BundleActiveService::SubscribeAppState() { BUNDLE_ACTIVE_LOGI("SubscribeAppState called"); sptr appManager = GetAppManagerInstance(); - if (appStateObserver_ == nullptr) { + if (appStateObserver_ == nullptr || appManager == nullptr) { BUNDLE_ACTIVE_LOGE("SubscribeAppState appstateobserver is null, return"); return false; } diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 091dd41..4433bcc 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -552,8 +552,11 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int userId, int64_t outRowId = BUNDLE_ACTIVE_FAIL; NativeRdb::ValuesBucket valuesBucket; vector queryCondition; + int updatedcount = 0; + int unupdatedcount = 0; for (auto iter = userHistory->begin(); iter != userHistory->end(); iter++) { - if (iter->second == nullptr) { + if (iter->second == nullptr || !iter->second->isChanged_) { + unupdatedcount++; continue; } queryCondition.push_back(to_string(userId)); @@ -576,7 +579,11 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int userId, } valuesBucket.Clear(); queryCondition.clear(); + iter->second->isChanged_ = false; + updatedcount++; } + BUNDLE_ACTIVE_LOGI("PutBundleHistoryData, update %{public}d bundles, keep %{public}d bundles group", + updatedcount, unupdatedcount); } shared_ptr>> BundleActiveUsageDatabase::GetBundleHistoryData( diff --git a/services/packagegroup/include/bundle_active_package_history.h b/services/packagegroup/include/bundle_active_package_history.h index ff25c7a..bfced55 100644 --- a/services/packagegroup/include/bundle_active_package_history.h +++ b/services/packagegroup/include/bundle_active_package_history.h @@ -30,6 +30,7 @@ public: uint32_t reasonInGroup_; int64_t bundleAliveTimeoutTimeStamp_; int64_t bundleDailyTimeoutTimeStamp_; + bool isChanged_; BundleActivePackageHistory(); ~BundleActivePackageHistory() {}; }; diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index 5055d7c..255cc49 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -32,6 +32,7 @@ BundleActivePackageHistory::BundleActivePackageHistory() bundleAliveTimeoutTimeStamp_ = 0; bundleDailyTimeoutTimeStamp_ = 0; lastCalculatedGroup_ = ACTIVE_GROUP_NEVER; + isChanged_ = false; }; void BundleActiveUserHistory::WriteDeviceDuration() @@ -202,6 +203,7 @@ void BundleActiveUserHistory::SetBundleGroup(const string& bundleName, const int oneBundleHistory->bundleAliveTimeoutTimeStamp_ = setTimeStamp; oneBundleHistory->bundleDailyTimeoutTimeStamp_ = setTimeStamp; } + oneBundleHistory->isChanged_ = true; } void BundleActiveUserHistory::UpdateBootBasedAndScreenTime(const bool& isScreenOn, const int64_t bootBasedTimeStamp, -- Gitee