!7790 修复多个modal uiextension复用一个sysdialog的问题

Merge pull request !7790 from wanganjie/modal_system
This commit is contained in:
openharmony_ci 2024-08-01 09:00:22 +00:00 committed by Gitee
commit daeb74cd74
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 63 additions and 50 deletions

View File

@ -52,6 +52,7 @@ ohos_shared_library("libmodal_system_ui_extension_client") {
"ability_runtime:ability_connect_callback_stub",
"ability_runtime:ability_manager",
"c_utils:utils",
"ffrt:libffrt",
"hilog:libhilog",
"input:libmmi-client",
"ipc:ipc_single",

View File

@ -15,12 +15,12 @@
#include "modal_system_ui_extension.h"
#include <atomic>
#include <memory>
#include <message_parcel.h>
#include <ability_manager_client.h>
#include <ffrt.h>
#include <iremote_object.h>
#include <message_parcel.h>
#include "window_manager_hilog.h"
@ -32,8 +32,7 @@ namespace {
constexpr int32_t INVALID_USERID = -1;
constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3;
constexpr int32_t VALUE_TYPE_STRING = 9;
std::atomic_bool g_isDialogShow = false;
sptr<IRemoteObject> g_remoteObject = nullptr;
constexpr uint64_t DISCONNECT_ABILITY_DELAY_TIME_MICROSECONDS = 5000000;
} // namespace
ModalSystemUiExtension::ModalSystemUiExtension() {}
@ -45,14 +44,6 @@ ModalSystemUiExtension::~ModalSystemUiExtension()
bool ModalSystemUiExtension::CreateModalUIExtension(const AAFwk::Want& want)
{
dialogConnectionCallback_ = sptr<OHOS::AAFwk::IAbilityConnection>(new DialogAbilityConnection(want));
if (g_isDialogShow) {
AppExecFwk::ElementName element;
dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID);
TLOGI(WmsLogTag::WMS_UIEXT, "dialog has been shown");
return true;
}
auto abilityManagerClient = AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
TLOGE(WmsLogTag::WMS_UIEXT, "AbilityManagerClient is nullptr");
@ -61,6 +52,7 @@ bool ModalSystemUiExtension::CreateModalUIExtension(const AAFwk::Want& want)
AAFwk::Want systemUIWant;
systemUIWant.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.systemdialog");
dialogConnectionCallback_ = sptr<DialogAbilityConnection>::MakeSptr(want);
auto result = abilityManagerClient->ConnectAbility(systemUIWant, dialogConnectionCallback_, INVALID_USERID);
if (result != ERR_OK) {
TLOGE(WmsLogTag::WMS_UIEXT, "ConnectAbility failed, result = %{public}d", result);
@ -70,12 +62,12 @@ bool ModalSystemUiExtension::CreateModalUIExtension(const AAFwk::Want& want)
return true;
}
std::string ModalSystemUiExtension::ToString(const AAFwk::WantParams& wantParams_)
std::string ModalSystemUiExtension::ToString(const AAFwk::WantParams& wantParams)
{
std::string result;
if (wantParams_.Size() != 0) {
if (wantParams.Size() != 0) {
result += "{";
for (auto it: wantParams_.GetParams()) {
for (auto it : wantParams.GetParams()) {
int typeId = AAFwk::WantParams::GetDataType(it.second);
result += "\"" + it.first + "\":";
if (typeId == VALUE_TYPE_STRING && AAFwk::WantParams::GetStringByType(it.second, typeId)[0] != '{') {
@ -83,7 +75,7 @@ std::string ModalSystemUiExtension::ToString(const AAFwk::WantParams& wantParams
} else {
result += AAFwk::WantParams::GetStringByType(it.second, typeId);
}
if (it != *wantParams_.GetParams().rbegin()) {
if (it != *wantParams.GetParams().rbegin()) {
result += ",";
}
}
@ -94,53 +86,76 @@ std::string ModalSystemUiExtension::ToString(const AAFwk::WantParams& wantParams
return result;
}
void ModalSystemUiExtension::DialogAbilityConnection::OnAbilityConnectDone(
const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
bool ModalSystemUiExtension::DialogAbilityConnection::SendWant(const sptr<IRemoteObject>& remoteObject)
{
TLOGI(WmsLogTag::WMS_UIEXT, "called");
std::lock_guard lock(mutex_);
if (remoteObject == nullptr) {
TLOGE(WmsLogTag::WMS_UIEXT, "remoteObject is nullptr");
return;
}
if (g_remoteObject == nullptr) {
g_remoteObject = remoteObject;
}
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE)) {
TLOGE(WmsLogTag::WMS_UIEXT, "write message parcel key size failed");
return;
return false;
}
if (!data.WriteString16(u"bundleName") || !data.WriteString16(Str8ToStr16(want_.GetElement().GetBundleName()))) {
TLOGE(WmsLogTag::WMS_UIEXT, "write bundleName failed");
return;
return false;
}
if (!data.WriteString16(u"abilityName") || !data.WriteString16(Str8ToStr16(want_.GetElement().GetAbilityName()))) {
TLOGE(WmsLogTag::WMS_UIEXT, "write abilityName failed");
return;
return false;
}
if (!data.WriteString16(u"parameters") ||
!data.WriteString16(Str8ToStr16(ModalSystemUiExtension::ToString(want_.GetParams())))) {
TLOGE(WmsLogTag::WMS_UIEXT, "write parameters failed");
return;
return false;
}
int32_t ret = remoteObject->SendRequest(AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
if (ret != ERR_OK) {
TLOGE(WmsLogTag::WMS_UIEXT, "show dialog failed");
return false;
}
return true;
}
void ModalSystemUiExtension::DialogAbilityConnection::OnAbilityConnectDone(
const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
{
TLOGI(WmsLogTag::WMS_UIEXT, "called");
if (remoteObject == nullptr) {
TLOGE(WmsLogTag::WMS_UIEXT, "remoteObject is nullptr");
return;
}
g_isDialogShow = true;
if (!SendWant(remoteObject)) {
return;
}
auto task = [weakThis = wptr(this)] {
auto connection = weakThis.promote();
if (!connection) {
TLOGI(WmsLogTag::WMS_UIEXT, "session is null or already disconnected");
return;
}
auto abilityManagerClient = AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
TLOGE(WmsLogTag::WMS_UIEXT, "AbilityManagerClient is nullptr");
return;
}
auto result = abilityManagerClient->DisconnectAbility(connection);
if (result != ERR_OK) {
TLOGE(WmsLogTag::WMS_UIEXT, "DisconnectAbility failed, result = %{public}d", result);
} else {
TLOGI(WmsLogTag::WMS_UIEXT, "DisconnectAbility success");
}
};
ffrt::task_handle handle = ffrt::submit_h(std::move(task),
ffrt::task_attr().delay(DISCONNECT_ABILITY_DELAY_TIME_MICROSECONDS));
if (handle == nullptr) {
TLOGE(WmsLogTag::WMS_UIEXT, "Failed to post task");
}
}
void ModalSystemUiExtension::DialogAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
int resultCode)
{
TLOGI(WmsLogTag::WMS_UIEXT, "called");
std::lock_guard lock(mutex_);
g_isDialogShow = false;
g_remoteObject = nullptr;
}
} // namespace Rosen
} // namespace OHOS

View File

@ -30,24 +30,21 @@ public:
~ModalSystemUiExtension();
bool CreateModalUIExtension(const AAFwk::Want& want);
static std::string ToString(const AAFwk::WantParams& wantParams_);
static std::string ToString(const AAFwk::WantParams& wantParams);
private:
class DialogAbilityConnection : public OHOS::AAFwk::AbilityConnectionStub {
public:
DialogAbilityConnection(const AAFwk::Want& want)
{
want_ = want;
}
virtual ~DialogAbilityConnection() = default;
public:
explicit DialogAbilityConnection(const AAFwk::Want& want) : want_(want) {};
virtual ~DialogAbilityConnection() = default;
void OnAbilityConnectDone(const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject,
int resultCode) override;
void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override;
void OnAbilityConnectDone(const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject,
int resultCode) override;
void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override;
private:
std::mutex mutex_;
AAFwk::Want want_;
private:
AAFwk::Want want_;
bool SendWant(const sptr<IRemoteObject>& remoteObject);
};
sptr<OHOS::AAFwk::IAbilityConnection> dialogConnectionCallback_{ nullptr };

View File

@ -8802,13 +8802,13 @@ void SceneSessionManager::DestroyExtensionSession(const sptr<IRemoteObject>& rem
auto task = [this, remoteExtSession]() {
auto iter = remoteExtSessionMap_.find(remoteExtSession);
if (iter == remoteExtSessionMap_.end()) {
TLOGE(WmsLogTag::WMS_UIEXT, "Invalid remoteExtSession");
TLOGI(WmsLogTag::WMS_UIEXT, "Invalid remoteExtSession or already destroyed");
return;
}
int32_t persistentId = INVALID_SESSION_ID;
int32_t parentId = INVALID_SESSION_ID;
if (!GetExtensionWindowIds(iter->second, persistentId, parentId)) {
TLOGD(WmsLogTag::WMS_UIEXT, "Get UIExtension window ids by token failed");
TLOGE(WmsLogTag::WMS_UIEXT, "Get UIExtension window ids by token failed");
return;
}