diff --git a/services/common/include/bundle_active_bundle_mgr_helper.h b/services/common/include/bundle_active_bundle_mgr_helper.h index 8deb25c08a835f464de7b596cfaa4e0620c78993..c59fcb4780f5f06cc97e21cc885dda058dd75172 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 1e57e84883db9718ce07e7146c949b89811d5382..7e8c60e136c48236cdddcc1b32d040e89e5d2854 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 e9c7f29eaba3e7a7d8371d860d4f6986d1d8442c..b19678d20733d62064d13dfa7c549f259c148a9d 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -91,7 +91,6 @@ private: ~BundleActiveGroupController() = default; ffrt::mutex mutex_; ffrt::mutex initMutex_; - bool GetBundleMgrProxy(); std::shared_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 eadc901f9b11d4a90ebf89666b801cacb905b4f9..60570a3b7f27c427728ff49bde9fa1b50f5ca314 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 { @@ -184,34 +185,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"); @@ -228,12 +201,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 c0aedde61c086a365506511dc3a3e36f8a7847d0..bcfa474b5781d480d1e057c1111acedd70755ac0 100644 --- a/test/unittest/bundle_active_total_test.cpp +++ b/test/unittest/bundle_active_total_test.cpp @@ -144,7 +144,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); }