!12032 【需求】悬浮窗拉起主窗-查询前台悬浮窗

Merge pull request !12032 from aodongbiao/master
This commit is contained in:
openharmony_ci 2025-01-11 12:01:36 +00:00 committed by Gitee
commit 92b437a2b1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
13 changed files with 314 additions and 1 deletions

View File

@ -532,6 +532,8 @@ public:
void RegisterCreateSubSessionListener(int32_t persistentId, const NotifyCreateSubSessionFunc& func);
void RegisterBindDialogTargetListener(const sptr<SceneSession>& session, NotifyBindDialogSessionFunc&& func);
void UnregisterSpecificSessionCreateListener(int32_t persistentId);
WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) override;
/*
* Window Lifecycle

View File

@ -80,6 +80,8 @@ public:
WSError NotifyAppUseControlList(ControlAppType type, int32_t userId,
const std::vector<AppUseControlInfo>& controlList) override;
WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override;
WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) override;
};
} // namespace OHOS::Rosen

View File

@ -359,6 +359,8 @@ public:
DragResizeType& dragResizeType) override { return WMError::WM_OK; }
WMError ShiftAppWindowPointerEvent(int32_t sourcePersistentId,
int32_t targetPersistentId) override { return WMError::WM_OK; }
WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) override { return WMError::WM_OK; }
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_INTERFACE_H

View File

@ -84,6 +84,7 @@ public:
TRANS_ID_UI_EXTENSION_CREATION_CHECK,
TRANS_ID_NOTIFY_APP_USE_CONTROL_LIST,
TRANS_ID_MINIMIZE_MAIN_SESSION,
TRANS_ID_HAS_FLOAT_FOREGROUND,
};
/*
@ -162,6 +163,20 @@ public:
* @permission application requires SA permission or SystemApp permission
*/
virtual WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) = 0;
/**
* @brief Query if there is float type window foreground of an abilityToken
*
* This function is used to query if there is float type window foreground of an ability
*
* @caller SA
* @permission SA permission
*
* @param abilityToken token of ability
* @param hasOrNot result for output
*/
virtual WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) = 0;
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_LITE_INTERFACE_H

View File

@ -90,7 +90,9 @@ public:
WSError NotifyAppUseControlList(ControlAppType type, int32_t userId,
const std::vector<AppUseControlInfo>& controlList) override;
WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override;
WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) override;
private:
template<typename T>
WSError GetParcelableInfos(MessageParcel& reply, std::vector<T>& parcelableInfos);

View File

@ -77,6 +77,7 @@ private:
int HandleGetAccessibilityWindowInfo(MessageParcel& data, MessageParcel& reply);
int HandleNotifyAppUseControlList(MessageParcel& data, MessageParcel& reply);
int HandleMinimizeMainSession(MessageParcel& data, MessageParcel& reply);
int HandleHasFloatingWindowForeground(MessageParcel& data, MessageParcel& reply);
int ProcessRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option);
};

View File

@ -12313,4 +12313,31 @@ WMError SceneSessionManager::ShiftAppWindowPointerEvent(int32_t sourcePersistent
return WMError::WM_OK;
}, __func__);
}
WMError SceneSessionManager::HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken, bool& hasOrNot)
{
if (!abilityToken) {
TLOGE(WmsLogTag::WMS_SYSTEM, "AbilityToken is null");
return WMError::WM_ERROR_NULLPTR;
}
if (!SessionPermission::IsSACalling() && !SessionPermission::IsShellCall()) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Permission denied, only for SA");
return WMError::WM_ERROR_INVALID_PERMISSION;
}
return taskScheduler_->PostSyncTask([this, &abilityToken, &hasOrNot, where = __func__] {
std::shared_lock<std::shared_mutex> lock(sceneSessionMapMutex_);
for (const auto& [_, session] : sceneSessionMap_) {
if (session && session->GetAbilityToken() == abilityToken &&
session->GetWindowType() == WindowType::WINDOW_TYPE_FLOAT && session->IsSessionForeground()) {
TLOGNI(WmsLogTag::WMS_SYSTEM, "%{public}s found", where);
hasOrNot = true;
return WMError::WM_OK;
}
}
TLOGNI(WmsLogTag::WMS_SYSTEM, "%{public}s not found", where);
hasOrNot = false;
return WMError::WM_OK;
}, __func__);
}
} // namespace OHOS::Rosen

View File

@ -289,4 +289,11 @@ WMError SceneSessionManagerLite::MinimizeMainSession(const std::string& bundleNa
{
return SceneSessionManager::GetInstance().MinimizeMainSession(bundleName, appIndex, userId);
}
WMError SceneSessionManagerLite::HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot)
{
return SceneSessionManager::GetInstance().HasFloatingWindowForeground(abilityToken,
hasOrNot);
}
} // namespace OHOS::Rosen

View File

@ -1471,4 +1471,44 @@ WMError SceneSessionManagerLiteProxy::MinimizeMainSession(
}
return static_cast<WMError>(ret);
}
WMError SceneSessionManagerLiteProxy::HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot)
{
if (!abilityToken) {
TLOGE(WmsLogTag::WMS_SYSTEM, "AbilityToken is null");
return WMError::WM_ERROR_INVALID_PARAM;
}
MessageParcel data;
MessageParcel reply;
MessageOption option;
if (!data.WriteInterfaceToken(GetDescriptor())) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Write interfaceToken failed");
return WMError::WM_ERROR_IPC_FAILED;
}
if (!data.WriteRemoteObject(abilityToken)) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Write abilityToken failed");
return WMError::WM_ERROR_IPC_FAILED;
}
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Remote is null");
return WMError::WM_ERROR_IPC_FAILED;
}
if (remote->SendRequest(static_cast<uint32_t>(SceneSessionManagerLiteMessage::TRANS_ID_HAS_FLOAT_FOREGROUND),
data, reply, option) != ERR_NONE) {
TLOGE(WmsLogTag::WMS_SYSTEM, "SendRequest failed");
return WMError::WM_ERROR_IPC_FAILED;
}
if (!reply.ReadBool(hasOrNot)) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Read result failed");
return WMError::WM_ERROR_IPC_FAILED;
}
uint32_t ret = 0;
if (!reply.ReadUint32(ret)) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Read ret failed");
return WMError::WM_ERROR_IPC_FAILED;
}
return static_cast<WMError>(ret);
}
} // namespace OHOS::Rosen

View File

@ -131,6 +131,8 @@ int SceneSessionManagerLiteStub::ProcessRemoteRequest(uint32_t code, MessageParc
return HandleNotifyAppUseControlList(data, reply);
case static_cast<uint32_t>(SceneSessionManagerLiteMessage::TRANS_ID_MINIMIZE_MAIN_SESSION):
return HandleMinimizeMainSession(data, reply);
case static_cast<uint32_t>(SceneSessionManagerLiteMessage::TRANS_ID_HAS_FLOAT_FOREGROUND):
return HandleHasFloatingWindowForeground(data, reply);
default:
WLOGFE("Failed to find function handler!");
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
@ -910,4 +912,25 @@ int SceneSessionManagerLiteStub::HandleMinimizeMainSession(MessageParcel& data,
reply.WriteInt32(static_cast<int32_t>(ret));
return ERR_NONE;
}
int SceneSessionManagerLiteStub::HandleHasFloatingWindowForeground(MessageParcel& data, MessageParcel& reply)
{
TLOGD(WmsLogTag::WMS_SYSTEM, "in");
sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
if (!abilityToken) {
TLOGE(WmsLogTag::WMS_SYSTEM, "AbilityToken is null.");
return ERR_INVALID_DATA;
}
bool hasOrNot = false;
WMError errCode = HasFloatingWindowForeground(abilityToken, hasOrNot);
if (!reply.WriteBool(hasOrNot)) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Write hasOrNot failed.");
return ERR_INVALID_DATA;
}
if (!reply.WriteUint32(static_cast<uint32_t>(errCode))) {
TLOGE(WmsLogTag::WMS_SYSTEM, "Write errCode failed.");
return ERR_INVALID_DATA;
}
return ERR_NONE;
}
} // namespace OHOS::Rosen

View File

@ -207,6 +207,8 @@ class MockSceneSessionManagerLiteStub : public SceneSessionManagerLiteStub {
const std::vector<AppUseControlInfo>& controlList) override { return WSError::WS_OK; }
WMError MinimizeMainSession(const std::string& bundleName,
int32_t appIndex, int32_t userId) override { return WMError::WM_OK; }
WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasFloatingShowing) override { return WMError::WM_OK; }
};
class SceneSessionManagerLiteStubTest : public testing::Test {

View File

@ -1211,6 +1211,194 @@ HWTEST_F(SceneSessionManagerTest12, IsGetWindowLayoutInfoNeeded03, Function | Sm
sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
ASSERT_EQ(true, ssm_->IsGetWindowLayoutInfoNeeded(sceneSession));
}
/**
* @tc.name: HasFloatingWindowForeground01
* @tc.desc: test HasFloatingWindowForeground with null abilityToken
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground01, Function | SmallTest | Level3)
{
bool hasFloatWindowForeground = false;
WMError result = ssm_->HasFloatingWindowForeground(nullptr, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_ERROR_NULLPTR);
EXPECT_EQ(hasFloatWindowForeground, false);
}
/**
* @tc.name: HasFloatingWindowForeground02
* @tc.desc: test HasFloatingWindowForeground with not existed abilityToken
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground02, Function | SmallTest | Level3)
{
SessionInfo sessionInfo;
sessionInfo.sessionState_ = SessionState::STATE_ACTIVE;
sessionInfo.persistentId_ = 1;
sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
sceneSession->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession->SetAbilityToken(token1);
sceneSession->SetSessionState(SessionState::STATE_ACTIVE);
ssm_->sceneSessionMap_.insert({ sceneSession->GetPersistentId(), sceneSession });
bool hasFloatWindowForeground = false;
sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
WMError result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, false);
}
/**
* @tc.name: HasFloatingWindowForeground03
* @tc.desc: test HasFloatingWindowForeground with existed foreground float window
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground03, Function | SmallTest | Level3)
{
// create first test sceneSession
SessionInfo sessionInfo1;
sessionInfo1.sessionState_ = SessionState::STATE_ACTIVE;
sessionInfo1.persistentId_ = 1;
sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession1->SetAbilityToken(token1);
sceneSession1->SetSessionState(SessionState::STATE_ACTIVE);
// create second test sceneSession
SessionInfo sessionInfo2;
sessionInfo2.sessionState_ = SessionState::STATE_FOREGROUND;
sessionInfo2.persistentId_ = 2;
sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession2->SetAbilityToken(token2);
sceneSession2->SetSessionState(SessionState::STATE_FOREGROUND);
ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
bool hasFloatWindowForeground = false;
WMError result = ssm_->HasFloatingWindowForeground(token1, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, true);
hasFloatWindowForeground = false;
result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, true);
}
/**
* @tc.name: HasFloatingWindowForeground04
* @tc.desc: test HasFloatingWindowForeground with existed background float window
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground04, Function | SmallTest | Level3)
{
// create first test sceneSession
SessionInfo sessionInfo1;
sessionInfo1.sessionState_ = SessionState::STATE_INACTIVE;
sessionInfo1.persistentId_ = 1;
sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession1->SetAbilityToken(token1);
sceneSession1->SetSessionState(SessionState::STATE_INACTIVE);
// create second test sceneSession
SessionInfo sessionInfo2;
sessionInfo2.sessionState_ = SessionState::STATE_BACKGROUND;
sessionInfo2.persistentId_ = 2;
sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession2->SetAbilityToken(token2);
sceneSession2->SetSessionState(SessionState::STATE_BACKGROUND);
ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
bool hasFloatWindowForeground = false;
WMError result = ssm_->HasFloatingWindowForeground(token1, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, false);
hasFloatWindowForeground = false;
result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, false);
}
/**
* @tc.name: HasFloatingWindowForeground05
* @tc.desc: test HasFloatingWindowForeground with existed forground toast window
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground05, Function | SmallTest | Level3)
{
// create first test sceneSession
SessionInfo sessionInfo;
sessionInfo.sessionState_ = SessionState::STATE_INACTIVE;
sessionInfo.persistentId_ = 1;
sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
sptr<IRemoteObject> token = sptr<MockIRemoteObject>::MakeSptr();
sceneSession->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession->SetAbilityToken(token);
sceneSession->SetSessionState(SessionState::STATE_INACTIVE);
ssm_->sceneSessionMap_.insert({ sceneSession->GetPersistentId(), sceneSession });
bool hasFloatWindowForeground = false;
WMError result = ssm_->HasFloatingWindowForeground(token, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, false);
}
/**
* @tc.name: HasFloatingWindowForeground06
* @tc.desc: test HasFloatingWindowForeground with other foreground float window
* @tc.type: FUNC
*/
HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground06, Function | SmallTest | Level3)
{
// create first test sceneSession
SessionInfo sessionInfo1;
sessionInfo1.sessionState_ = SessionState::STATE_ACTIVE;
sessionInfo1.persistentId_ = 1;
sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession1->SetAbilityToken(token1);
sceneSession1->SetSessionState(SessionState::STATE_ACTIVE);
// create second test sceneSession
SessionInfo sessionInfo2;
sessionInfo2.sessionState_ = SessionState::STATE_BACKGROUND;
sessionInfo2.persistentId_ = 2;
sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
sceneSession2->SetAbilityToken(token2);
sceneSession2->SetSessionState(SessionState::STATE_BACKGROUND);
ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
bool hasFloatWindowForeground = false;
WMError result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
EXPECT_EQ(result, WMError::WM_OK);
EXPECT_EQ(hasFloatWindowForeground, false);
}
}
} // namespace Rosen
} // namespace OHOS

View File

@ -266,6 +266,8 @@ public:
virtual WMError NotifyWatchFocusActiveChange(bool isActive) { return WMError::WM_OK; }
virtual WMError ShiftAppWindowPointerEvent(int32_t sourcePersistentId,
int32_t targetPersistentId) { return WMError::WM_ERROR_DEVICE_NOT_SUPPORT; }
virtual WMError HasFloatingWindowForeground(const sptr<IRemoteObject>& abilityToken,
bool& hasOrNot) { return WMError::WM_OK; }
};
}
}