From 579f45fb14f33a8fdbd025351392615222d37b79 Mon Sep 17 00:00:00 2001 From: zhangwt3652 Date: Thu, 28 Aug 2025 16:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=92=AD=E6=8E=A7=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E5=87=86=E7=A1=AE=E6=8B=89=E8=B5=B7=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangwt3652 --- services/session/BUILD.gn | 1 + services/session/server/avsession_item.cpp | 75 ++++++++++++++++++- services/session/server/avsession_item.h | 6 ++ services/session/server/avsession_service.cpp | 36 ++++++++- .../server/test/avsession_item_test.cpp | 34 +++++++++ 5 files changed, 145 insertions(+), 7 deletions(-) diff --git a/services/session/BUILD.gn b/services/session/BUILD.gn index 11f1e516..c9448158 100644 --- a/services/session/BUILD.gn +++ b/services/session/BUILD.gn @@ -553,6 +553,7 @@ av_session_ohos_library("avsession_item") { external_deps = [ "ability_base:base", "ability_base:want", + "ability_runtime:app_context", "ability_runtime:app_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", diff --git a/services/session/server/avsession_item.cpp b/services/session/server/avsession_item.cpp index f73003b9..05335b21 100644 --- a/services/session/server/avsession_item.cpp +++ b/services/session/server/avsession_item.cpp @@ -509,6 +509,8 @@ int32_t AVSessionItem::SetAVMetaData(const AVMetaData& meta) auto ret = remoteSource_->SetAVMetaData(meta); CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVMetaData failed"); } + SLOGI("andy, call AVSessionItem::SetAVMetaData"); + GetCurrentAppIndexForSession(); #ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL ReportMetadataChange(meta); #endif @@ -626,6 +628,57 @@ void AVSessionItem::CheckIfSendCapsule(const AVPlaybackState& state) } } +void AVSessionItem::GetCurrentAppIndexForSession() +{ + SLOGI("andy, call AVSessionItem::GetCurrentAppIndexForSession"); + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + CHECK_AND_RETURN_LOG(samgr != nullptr, "andy, GetSystemAbilityManager return nullptr"); + sptr object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + CHECK_AND_RETURN_LOG(object != nullptr, "andy, GetSystemAbility return nullptr"); + sptr bms = iface_cast(object); + CHECK_AND_RETURN_LOG(bms != nullptr, "andy, GetBundleMgr return nullptr"); + + std::string bundleName; + int32_t appIndex = 0; + bms->GetNameAndIndexForUid(GetUid(), bundleName, appIndex); + SLOGI("andy, AVSessionItem GetNameAndIndexForUid name: %{public}s appIndex:%{public}d", + bundleName.c_str(), appIndex); + { + std::lock_guard lockGuard(avsessionItemLock_); + appIndex_ = appIndex; + } +} + +AbilityRuntime::WantAgent::WantAgent AVSessionItem::CreateWantAgentWithIndex( + const AbilityRuntime::WantAgent::WantAgent& ability, int32_t index) +{ + SLOGI("andy, CreateWantAgentWithIndex with index %{public}d", index); + std::shared_ptr want = std::make_shared(); + std::shared_ptr launchWantAgent = + std::make_shared(ability); + int res = AVSESSION_SUCCESS; + if (want != nullptr && launchWantAgent != nullptr) { + res = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(launchWantAgent, want); + } + if (res == AVSESSION_SUCCESS && want != nullptr) { + AAFwk::WantParams params = want->GetParams(); + params.SetParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY, AAFwk::Integer::Box(index)); + want->SetParams(params); + } + std::vector flags = { + AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG + }; + std::vector> wants{ want }; + AbilityRuntime::WantAgent::WantAgentInfo wantAgentInfo( + 0, + AbilityRuntime::WantAgent::WantAgentConstant::OperationType::START_ABILITY, + flags, + wants, + nullptr + ); + return *AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(wantAgentInfo, GetUid()); +} + int32_t AVSessionItem::SetAVPlaybackState(const AVPlaybackState& state) { { @@ -683,12 +736,17 @@ int32_t AVSessionItem::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAge { launchAbility_ = ability; std::shared_ptr want = std::make_shared(); - std::shared_ptr launWantAgent = + std::shared_ptr launchWantAgent = std::make_shared(ability); int res = AVSESSION_SUCCESS; - if (want != nullptr && launWantAgent != nullptr) { - res = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(launWantAgent, want); + if (want != nullptr && launchWantAgent != nullptr) { + res = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(launchWantAgent, want); + AAFwk::WantParams params = want->GetParams(); + if (!params.HasParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY) && appIndex_ != 0) { + launchAbility_ = CreateWantAgentWithIndex(ability, appIndex_); + } } + std::string errMsg = "Get want failed."; std::string bundleName = ""; std::string abilityName = ""; @@ -1959,6 +2017,11 @@ void AVSessionItem::ReadMetaDataAVQueueImg(std::shared_ptr& a { std::string avQueueFileDir = AVSessionUtils::GetFixedPathName(userId_); std::string avQueueFileName = GetBundleName() + "_" + metaData_.GetAVQueueId() + AVSessionUtils::GetFileSuffix(); + if (appIndex_ != 0) { + avQueueFileName = GetBundleName() + "_" + std::to_string(appIndex_) + "_" + metaData_.GetAVQueueId() + + AVSessionUtils::GetFileSuffix(); + } + SLOGI("andy, AVSessionItem::ReadMetaDataAVQueueImg, avQueueFileName: %{public}s", avQueueFileName.c_str()); AVSessionUtils::ReadImageFromFile(avQueuePixelMap, avQueueFileDir, avQueueFileName); } @@ -2021,6 +2084,12 @@ void AVSessionItem::SetSupportCommand(std::vector cmds) } } +int32_t AVSessionItem::GetAppIndex() +{ + std::lock_guard lockGuard(avsessionItemLock_); + return appIndex_; +} + AbilityRuntime::WantAgent::WantAgent AVSessionItem::GetLaunchAbility() { return launchAbility_; diff --git a/services/session/server/avsession_item.h b/services/session/server/avsession_item.h index 289ae436..be628ec7 100644 --- a/services/session/server/avsession_item.h +++ b/services/session/server/avsession_item.h @@ -261,6 +261,8 @@ public: void SetSupportCommand(std::vector cmds); + int32_t GetAppIndex(); + #ifdef CASTPLUS_CAST_ENGINE_ENABLE int32_t RegisterListenerStreamToCast(const std::pair& serviceNameStatePair, DeviceInfo deviceInfo); @@ -381,6 +383,9 @@ private: bool SearchSpidInCapability(const std::string& deviceId); void CheckIfSendCapsule(const AVPlaybackState& state); void CheckSupportColdStartExtra(const AAFwk::WantParams& extras); + void GetCurrentAppIndexForSession(); + AbilityRuntime::WantAgent::WantAgent CreateWantAgentWithIndex(const AbilityRuntime::WantAgent::WantAgent& ability, + int32_t index); using HandlerFuncType = std::function; std::map cmdHandlers = { @@ -453,6 +458,7 @@ private: friend class AVSessionDumper; int64_t playingTime_ = 0; std::string lyricTitle_ = ""; + int32_t appIndex_ = 0; std::shared_ptr remoteSource_; std::shared_ptr remoteSink_; diff --git a/services/session/server/avsession_service.cpp b/services/session/server/avsession_service.cpp index 8a2a95dc..85f1fc40 100644 --- a/services/session/server/avsession_service.cpp +++ b/services/session/server/avsession_service.cpp @@ -1881,7 +1881,14 @@ bool AVSessionService::InsertSessionItemToCJSONAndPrint(const std::string& tag, cJSON* newValue = cJSON_CreateObject(); CHECK_AND_RETURN_RET_LOG(newValue != nullptr, false, "newValue get null"); cJSON_AddStringToObject(newValue, "sessionId", sessionId.c_str()); - cJSON_AddStringToObject(newValue, "bundleName", elementName.GetBundleName().c_str()); + // andy + std::string bundleName = elementName.GetBundleName(); + sptr session = GetContainer().GetSessionById(sessionId); + if ((session != nullptr) && (session->GetAppIndex() != 0)) { + bundleName = bundleName + "_" + std::to_string(session->GetAppIndex()); + } + SLOGI("andy, AVSessionService::InsertSessionItemToCJSONAndPrint, bundleName=%{public}s", bundleName.c_str()); + cJSON_AddStringToObject(newValue, "bundleName", bundleName.c_str()); cJSON_AddStringToObject(newValue, "abilityName", elementName.GetAbilityName().c_str()); cJSON_AddStringToObject(newValue, "sessionType", sessionType.c_str()); cJSON_AddStringToObject(newValue, "sessionTag", tag.c_str()); @@ -1920,14 +1927,22 @@ void AVSessionService::SaveSessionInfoInFile(const std::string& tag, const std:: valuesArray = cJSON_CreateArray(); CHECK_AND_RETURN_LOG(valuesArray != nullptr, "create array json fail"); } - if (oldSortContent.find(elementName.GetBundleName()) == string::npos) { + // andy + std::string bundleName = elementName.GetBundleName(); + sptr session = GetContainer().GetSessionById(sessionId); + if ((session != nullptr) && (session->GetAppIndex() != 0)) { + bundleName = bundleName + "_" + std::to_string(session->GetAppIndex()); + } + SLOGI("andy, AVSessionService::SaveSessionInfoInFile, bundleName=%{public}s", bundleName.c_str()); + + if (oldSortContent.find(bundleName) == string::npos) { auto callback = [this](std::string bundleName, int32_t userId) { SLOGI("recv delete bundle:%{public}s at user:%{public}d", bundleName.c_str(), userId); DeleteAVQueueInfoRecord(bundleName, userId); DeleteHistoricalRecord(bundleName, userId); NotifyHistoricalRecordChange(bundleName, userId); }; - if (!BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(elementName.GetBundleName(), + if (!BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback, GetUsersManager().GetCurrentUserId())) { SLOGE("SubscribeBundleStatusEvent failed"); } @@ -1943,7 +1958,7 @@ void AVSessionService::SaveSessionInfoInFile(const std::string& tag, const std:: SLOGI("not contain bundleName or abilityName, pass"); continue; } - if (strcmp(elementName.GetBundleName().c_str(), bundleNameItem->valuestring) == 0 && + if (strcmp(bundleName.c_str(), bundleNameItem->valuestring) == 0 && strcmp(elementName.GetAbilityName().c_str(), abilityNameItem->valuestring) == 0) { cJSON_DeleteItemFromArray(valuesArray, i); } @@ -2303,7 +2318,13 @@ void AVSessionService::AddAvQueueInfoToFile(AVSessionItem& session) { // check is this session support playmusiclist intent std::lock_guard lockGuard(sessionServiceLock_); + // andy std::string bundleName = session.GetBundleName(); + if (session.GetAppIndex() != 0) { + bundleName = bundleName + "_" + std::to_string(session.GetAppIndex()); + } + SLOGI("andy, AVSessionService::AddAvQueueInfoToFile, bundleName=%{public}s", bundleName.c_str()); + std::string supportModule; std::string profile; if (!BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile)) { @@ -2967,6 +2988,13 @@ void AVSessionService::HandleSystemKeyColdStart(const AVControlCommand &command, sptr object; int32_t ret = 0; std::string bundleName = coldStartDescriptors[0].elementName_.GetBundleName(); + // andy + sptr session = GetContainer().GetSessionById(coldStartDescriptors[0].sessionId_); + if ((session != nullptr) && (session->GetAppIndex() != 0)) { + bundleName = bundleName + "_" + std::to_string(session->GetAppIndex()); + } + SLOGI("andy, AVSessionService::HandleSystemKeyColdStart, bundleName=%{public}s", bundleName.c_str()); + if (deviceId.length() == 0) { ret = StartAVPlayback(bundleName, ""); } else { diff --git a/services/session/server/test/avsession_item_test.cpp b/services/session/server/test/avsession_item_test.cpp index 50818685..236d5eb1 100644 --- a/services/session/server/test/avsession_item_test.cpp +++ b/services/session/server/test/avsession_item_test.cpp @@ -29,6 +29,8 @@ #include "migrate_avsession_constant.h" #include "system_ability_definition.h" #include "avsession_service.h" +#include "int_wrapper.h" +#include "want_agent_helper.h" using namespace testing::ext; using namespace OHOS::AVSession; @@ -768,5 +770,37 @@ HWTEST_F(AVsessionItemTest, AVSessionItem_CheckUseAVMetaData_001, TestSize.Level EXPECT_EQ(avQueueImg->GetInnerImgBuffer().size(), 0); SLOGI("AVSessionItem_CheckUseAVMetaData_001 end!"); } + +/** + * @tc.name: AVSessionItem_GetCurrentAppIndexForSession_001 + * @tc.desc: Test GetCurrentAppIndexForSession sets appIndex_ correctly. + * @tc.type: FUNC + * @tc.require: #ICWCWU + */ +HWTEST_F(AVsessionItemTest, AVSessionItem_GetCurrentAppIndexForSession_001, TestSize.Level0) +{ + SLOGI("AVSessionItem_GetCurrentAppIndexForSession_001 begin!"); + ASSERT_TRUE(g_AVSessionItem != nullptr); + g_AVSessionItem->GetCurrentAppIndexForSession(); + int32_t appIndex = g_AVSessionItem->GetAppIndex(); + EXPECT_EQ(appIndex, 0); + SLOGI("AVSessionItem_GetCurrentAppIndexForSession_001 end!"); +} + +/** + * @tc.name: AVSessionItem_GetAppIndex_001 + * @tc.desc: Test GetAppIndex returns appIndex_ value. + * @tc.type: FUNC + * @tc.require: #ICWCWU + */ +HWTEST_F(AVsessionItemTest, AVSessionItem_GetAppIndex_001, TestSize.Level0) +{ + SLOGI("AVSessionItem_GetAppIndex_001 begin!"); + ASSERT_TRUE(g_AVSessionItem != nullptr); + g_AVSessionItem->appIndex_ = 1; + int32_t appIndex = g_AVSessionItem->GetAppIndex(); + EXPECT_EQ(appIndex, 1); + SLOGI("AVSessionItem_GetAppIndex_001 end!"); +} } //AVSession } //OHOS -- Gitee