From d39b71be080881c557a42a81400b3a18ad5827d6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 12 May 2025 18:56:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=86=85=E5=AD=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- .../include/bundle_active_bundle_mgr_helper.h | 9 ++++- .../src/bundle_active_bundle_mgr_helper.cpp | 14 ++++--- .../include/bundle_active_group_controller.h | 1 - .../src/bundle_active_group_controller.cpp | 37 ++----------------- test/unittest/bundle_active_total_test.cpp | 2 +- 5 files changed, 21 insertions(+), 42 deletions(-) diff --git a/services/common/include/bundle_active_bundle_mgr_helper.h b/services/common/include/bundle_active_bundle_mgr_helper.h index 8deb25c..c59fcb4 100644 --- a/services/common/include/bundle_active_bundle_mgr_helper.h +++ b/services/common/include/bundle_active_bundle_mgr_helper.h @@ -27,6 +27,13 @@ namespace OHOS { namespace DeviceUsageStats { +struct BundleActiveApplication { + std::string bundleName; + int32_t uid; + bool isLauncherApp; + BundleActiveApplication(std::string bundleName_, int32_t uid_, bool isLauncherApp_) + : bundleName{std::move(bundleName_)}, uid{uid_}, isLauncherApp{isLauncherApp_} {} +}; class BundleActiveBundleMgrHelper : public DelayedSingleton { public: /** @@ -60,7 +67,7 @@ public: const int userId, AppExecFwk::ApplicationInfo &appInfo); bool GetApplicationInfos(const AppExecFwk::ApplicationFlag flag, - const int userId, std::vector &appInfos); + const int userId, std::vector &bundleActiveApplicationInfos); bool IsLauncherApp(const std::string &bundleName, const int32_t userId); diff --git a/services/common/src/bundle_active_bundle_mgr_helper.cpp b/services/common/src/bundle_active_bundle_mgr_helper.cpp index 1e57e84..7e8c60e 100644 --- a/services/common/src/bundle_active_bundle_mgr_helper.cpp +++ b/services/common/src/bundle_active_bundle_mgr_helper.cpp @@ -70,7 +70,7 @@ bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, } bool BundleActiveBundleMgrHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag flag, - const int userId, std::vector &appInfos) + const int userId, std::vector &bundleActiveApplicationInfos) { BUNDLE_ACTIVE_LOGI("start get application infos"); std::lock_guard lock(connectionMutex_); @@ -79,10 +79,14 @@ bool BundleActiveBundleMgrHelper::GetApplicationInfos(const AppExecFwk::Applicat return false; } BUNDLE_ACTIVE_LOGI("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr); - if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfos(flag, userId, appInfos)) { - return true; + std::vector appInfos; + if (bundleMgr_ == nullptr || !bundleMgr_->GetApplicationInfos(flag, userId, appInfos)) { + return false; } - return false; + for (auto appInfo : appInfos) { + bundleActiveApplicationInfos.emplace_back(appInfo.bundleName, appInfo.uid, appInfo.isLauncherApp); + } + return true; } bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, @@ -152,7 +156,7 @@ void BundleActiveBundleMgrHelper::InitLauncherAppMap() BUNDLE_ACTIVE_LOGI("init laucherAppMap"); isInitLauncherAppMap_ = true; InitSystemEvent(); - std::vector appInfos; + std::vector appInfos; if (!GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, AppExecFwk::Constants::ALL_USERID, appInfos)) { BUNDLE_ACTIVE_LOGE("Init Launcher App Map by BMS failed"); diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index d587f93..1356c5d 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -86,7 +86,6 @@ public: private: ffrt::mutex mutex_; - bool GetBundleMgrProxy(); std::weak_ptr activeGroupHandler_; int64_t timeoutForDirectlyUse_; int64_t timeoutForNotifySeen_; diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 3ce28c4..ba09b73 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -21,6 +21,7 @@ #include "ibundle_active_service.h" #include "bundle_active_group_controller.h" #include "bundle_active_util.h" +#include "bundle_active_bundle_mgr_helper.h" namespace OHOS { namespace DeviceUsageStats { @@ -145,34 +146,6 @@ void BundleActiveGroupController::DeleteUsageGroupCache( } } -bool BundleActiveGroupController::GetBundleMgrProxy() -{ - if (!sptrBundleMgr_) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - BUNDLE_ACTIVE_LOGE("Failed to get system ability mgr."); - return false; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - BUNDLE_ACTIVE_LOGE("Failed to get bundle manager service."); - return false; - } - sptrBundleMgr_ = iface_cast(remoteObject); - if (!sptrBundleMgr_) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, sptrBundleMgr_"); - return false; - } - auto object = sptrBundleMgr_->AsObject(); - if (!object) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability"); - return false; - } - } - return true; -} - void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId) { BUNDLE_ACTIVE_LOGI("PeriodCheckBundleState called"); @@ -189,12 +162,8 @@ void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId) bool BundleActiveGroupController::CheckEachBundleState(const int32_t userId) { BUNDLE_ACTIVE_LOGI("CheckEachBundleState called, userid is %{public}d", userId); - std::vector allBundlesForUser; - if (!GetBundleMgrProxy()) { - BUNDLE_ACTIVE_LOGE("CheckEachBundleState get bundle manager proxy failed!"); - return false; - } - sptrBundleMgr_->GetApplicationInfos(flag, userId, allBundlesForUser); + std::vector allBundlesForUser; + BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfos(flag, userId, allBundlesForUser); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); for (auto oneBundle : allBundlesForUser) { diff --git a/test/unittest/bundle_active_total_test.cpp b/test/unittest/bundle_active_total_test.cpp index 3f8c01e..2f31e7f 100644 --- a/test/unittest/bundle_active_total_test.cpp +++ b/test/unittest/bundle_active_total_test.cpp @@ -146,7 +146,7 @@ HWTEST_F(BundleActiveTotalTest, BundleActiveBundleMgrHelperTest_001, Function | HWTEST_F(BundleActiveTotalTest, BundleActiveBundleMgrHelperTest_002, Function | MediumTest | Level0) { AppExecFwk::ApplicationFlag flag = AppExecFwk::GET_BASIC_APPLICATION_INFO; - std::vector appInfos; + std::vector appInfos; bool result = BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfos(flag, 0, appInfos); EXPECT_TRUE(result); } -- Gitee