From 86b60d419577bcf7ccfea7e118ed5b4ed287c508 Mon Sep 17 00:00:00 2001 From: guojin31 Date: Wed, 2 Jul 2025 17:13:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96callingwindow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guojin31 --- .../include/input_method_panel.h | 3 +- .../src/input_method_ability.cpp | 15 +++-- .../src/input_method_panel.cpp | 21 +++++-- .../IInputMethodSystemAbility.idl | 1 + .../src/input_method_controller.cpp | 10 +++ .../include/input_method_controller.h | 11 ++++ .../include/input_method_system_ability.h | 1 + services/include/peruser_session.h | 1 + services/src/input_method_system_ability.cpp | 10 +++ services/src/peruser_session.cpp | 17 +++++ .../src/input_method_ability_test.cpp | 2 +- .../cpp_test/src/input_method_attach_test.cpp | 1 - .../src/input_method_controller_test.cpp | 63 +++++++++++++++++++ .../cpp_test/src/input_method_panel_test.cpp | 2 +- 14 files changed, 143 insertions(+), 15 deletions(-) diff --git a/frameworks/native/inputmethod_ability/include/input_method_panel.h b/frameworks/native/inputmethod_ability/include/input_method_panel.h index 093df7747..d5fac53cf 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_panel.h +++ b/frameworks/native/inputmethod_ability/include/input_method_panel.h @@ -71,7 +71,7 @@ public: int32_t ChangePanelFlag(PanelFlag panelFlag); PanelType GetPanelType(); PanelFlag GetPanelFlag(); - int32_t ShowPanel(); + int32_t ShowPanel(uint32_t windowId = 0); int32_t HidePanel(); int32_t SizeChange(const WindowSize &size); WindowSize GetKeyboardSize(); @@ -201,6 +201,7 @@ private: bool sizeChangeRegistered_ = false; bool sizeUpdateRegistered_ = false; uint32_t invalidGravityPercent = 0; + uint32_t callingWindowId = 0; std::shared_ptr panelStatusListener_ = nullptr; static std::atomic sequenceId_; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index bbacf2b23..5e2d78d59 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -608,9 +608,6 @@ int32_t InputMethodAbility::InvokeStartInputCallback(const TextTotalConfig &text textConfig.textSelection.newBegin, textConfig.textSelection.newEnd); } } - if (textConfig.windowId != INVALID_WINDOW_ID) { - imeListener_->OnSetCallingWindow(textConfig.windowId); - } return ErrorCode::NO_ERROR; } @@ -885,7 +882,6 @@ bool InputMethodAbility::NotifyInfoToWmsInStartInput(const TextTotalConfig &text if (type == SOFT_KEYBOARD && panel->GetPanelFlag() == FLG_FIXED && panel->IsShowing()) { panel->SetTextFieldAvoidInfo(textConfig.positionY, textConfig.height); } - panel->SetCallingWindow(textConfig.windowId); return false; }); }; @@ -1129,7 +1125,13 @@ int32_t InputMethodAbility::ShowPanel( IMSA_HILOGE("failed to set keyBoard, ret: %{public}d!", ret); } } - auto ret = inputMethodPanel->ShowPanel(); + TextTotalConfig textConfig; + auto ret = GetTextConfig(textConfig); + if (ret != ErrorCode::NO_ERROR) { + MSA_HILOGI("failed to get window id, ret: %{public}d!", ret); + return ErrorCode::ERROR_GET_TEXT_CONFIG; + } + ret = inputMethodPanel->ShowPanel(textConfig.windowId); if (ret == ErrorCode::NO_ERROR) { NotifyPanelStatus(false); PanelStatusInfo info; @@ -1138,6 +1140,9 @@ int32_t InputMethodAbility::ShowPanel( info.visible = true; info.trigger = trigger; NotifyPanelStatusInfo(info); + if (imeListener_ != nullptr) { + imeListener_->OnSetCallingWindow(textConfig.windowId); + } } return ret; } diff --git a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp index 2ff190142..7a2522d4b 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp @@ -1339,9 +1339,10 @@ PanelFlag InputMethodPanel::GetPanelFlag() return panelFlag_; } -int32_t InputMethodPanel::ShowPanel() +int32_t InputMethodPanel::ShowPanel(uint32_t windowId) { IMSA_HILOGD("InputMethodPanel start."); + callingWindowId = windowId; int32_t waitTime = 0; while (isWaitSetUiContent_ && waitTime < MAXWAITTIME) { std::this_thread::sleep_for(std::chrono::milliseconds(WAITTIME)); @@ -1360,7 +1361,7 @@ int32_t InputMethodPanel::ShowPanel() { KeyboardEffectOption option = ConvertToWmEffect(immersiveMode_, immersiveEffect_); InputMethodSyncTrace tracer("InputMethodPanel_ShowPanel"); - ret = window_->ShowKeyboard(option); + ret = window_->ShowKeyboard(option, windowId); } if (ret != WMError::WM_OK) { IMSA_HILOGE("ShowPanel error, err = %{public}d", ret); @@ -1421,12 +1422,20 @@ int32_t InputMethodPanel::HidePanel() int32_t InputMethodPanel::SetCallingWindow(uint32_t windowId) { IMSA_HILOGD("InputMethodPanel start, windowId: %{public}d.", windowId); + if (windowId == callingWindowId) { + return ErrorCode::NO_ERROR; + } + callingWindowId_ = windowId; if (window_ == nullptr) { IMSA_HILOGE("window_ is nullptr!"); return ErrorCode::ERROR_PANEL_NOT_FOUND; } - auto ret = window_->SetCallingWindow(windowId); - IMSA_HILOGI("ret: %{public}d, windowId: %{public}u", ret, windowId); + if (!IsShowing()) { + IMSA_HILOGW("the keyboard is not showing"); + return ErrorCode::NO_ERROR; + } + auto ret = window_->SetCallingWindow(callingWindowId_); + IMSA_HILOGI("ret: %{public}d, windowId: %{public}u", ret, callingWindowId_); return ret == WMError::WM_OK ? ErrorCode::NO_ERROR : ErrorCode::ERROR_WINDOW_MANAGER; } @@ -1437,12 +1446,12 @@ int32_t InputMethodPanel::GetCallingWindowInfo(CallingWindowInfo &windowInfo) IMSA_HILOGE("window_ is nullptr!"); return ErrorCode::ERROR_PANEL_NOT_FOUND; } - auto ret = window_->GetCallingWindowWindowStatus(windowInfo.status); + auto ret = window_->GetCallingWindowWindowStatus(callingWindowId_, windowInfo.status); if (ret != WMError::WM_OK) { IMSA_HILOGE("get status failed, ret: %{public}d!", ret); return ErrorCode::ERROR_WINDOW_MANAGER; } - ret = window_->GetCallingWindowRect(windowInfo.rect); + ret = window_->GetCallingWindowRect(callingWindowId_, windowInfo.rect); if (ret != WMError::WM_OK) { IMSA_HILOGE("get rect failed, ret: %{public}d!", ret); return ErrorCode::ERROR_WINDOW_MANAGER; diff --git a/frameworks/native/inputmethod_controller/IInputMethodSystemAbility.idl b/frameworks/native/inputmethod_controller/IInputMethodSystemAbility.idl index 2f69e2f4d..d11d074de 100644 --- a/frameworks/native/inputmethod_controller/IInputMethodSystemAbility.idl +++ b/frameworks/native/inputmethod_controller/IInputMethodSystemAbility.idl @@ -72,4 +72,5 @@ interface OHOS.MiscServices.IInputMethodSystemAbility { void SendPrivateData([in] Value value); void IsDefaultImeScreen([in] unsigned long displayId, [out] boolean resultValue); void IsCapacitySupport([in] int capacity, [out] boolean isSupport); + bool IsKeyboardCallingProcess(int32_t pid); } diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 3eaff2308..09b1c2178 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -352,6 +352,16 @@ int32_t InputMethodController::Attach(sptr listener, cons return ErrorCode::NO_ERROR; } +bool InputMethodController::IsKeyboardCallingProcess(int32_t pid) +{ + auto proxy = GetSystemAbilityProxy(); + if (proxy == nullptr) { + IMSA_HILOGE("proxy is nullptr!"); + return ErrorCode::ERROR_EX_NULL_POINTER; + } + return proxy->IsKeyboardCallingProcess(pid); +} + int32_t InputMethodController::ShowTextInputInner(const AttachOptions &attachOptions, ClientType type) { InputMethodSyncTrace tracer("IMC_ShowTextInput"); diff --git a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h index c09b21433..2c9642018 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -461,6 +461,17 @@ public: */ IMF_API int32_t GetInputMethodConfig(AppExecFwk::ElementName &inputMethodConfig); + /** + * @brief Is keyboard calling process. + * + * This function is used to get is keyboard calling process for pid. + * + * @param pid Indicates the process id. + * @return Returns true or false. + * @since 20 + */ + IMF_API bool IsKeyboardCallingProcess(int32_t pid) + /** * @brief Set calling window id. * diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index d358b5141..be3d78bdd 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -93,6 +93,7 @@ public: int32_t UnregisterProxyIme(uint64_t displayId) override; ErrCode IsDefaultImeScreen(uint64_t displayId, bool &resultValue) override; ErrCode IsCapacitySupport(int32_t capacity, bool &isSupport) override; + bool IsKeyboardCallingProcess(int32_t pid) override; int32_t GetCallingUserId(); protected: diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index 81b88c50f..04ba8e639 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -144,6 +144,7 @@ public: std::shared_ptr GetImeNativeCfg(int32_t userId, const std::string &bundleName, const std::string &subName); int32_t OnSetCallingWindow(uint32_t callingWindowId, uint64_t callingDisplayId, sptr client); + bool IsKeyboardCallingProcess(int32_t pid); int32_t GetInputStartInfo( uint64_t displayId, bool &isInputStart, uint32_t &callingWndId, int32_t &requestKeyboardReason); bool IsSaReady(int32_t saId); diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index f7dbac7ec..98bcabf51 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -1435,6 +1435,16 @@ ErrCode InputMethodSystemAbility::GetCurrentInputMethod(Property& resultValue) return ERR_OK; } +bool InputMethodSystemAbility::IsKeyboardCallingProcess(int32_t pid) +{ + int32_t userId = GetCallingUserId(); + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + return ErrorCode::ERROR_NULL_POINTER; + } + return session->IsKeyboardCallingProcess(pid); +} + ErrCode InputMethodSystemAbility::IsDefaultImeSet(bool& resultValue) { resultValue = ImeInfoInquirer::GetInstance().IsDefaultImeSet(GetCallingUserId()); diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index e0a497ce8..243774b3a 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -1116,6 +1116,23 @@ std::shared_ptr PerUserSession::GetRealCurrentIme(bool needSwitchT return ImeInfoInquirer::GetInstance().GetImeToStart(userId_); } +bool PerUserSession::IsKeyboardCallingProcess(int32_t pid) +{ + auto clientGroup = GetClientGroup(ImeType::IME); + auto clientInfo = clientGroup != nullptr ? clientGroup->GetCurrentClientInfo() : nullptr; + if (clientInfo == nullptr) { + IMSA_HILOGE("failed to get cur client info!"); + return false; + } + auto identityChecker = std::make_shared(); + if (clientInfo->uiExtensionTokenId != IMF_INVALID_TOKENID + && identityChecker->IsFocusedUIExtension(clientInfo->uiExtensionTokenId)) { + IMSA_HILOGI("UIExtension focused"); + return true; + } + return clientInfo->pid == pid; +} + int32_t PerUserSession::StartCurrentIme(bool isStopCurrentIme) { auto imeToStart = GetRealCurrentIme(true); diff --git a/test/unittest/cpp_test/src/input_method_ability_test.cpp b/test/unittest/cpp_test/src/input_method_ability_test.cpp index cd15f0fce..cdce7f507 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -1540,7 +1540,7 @@ HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_004, TestSize.Level0) InputMethodAbilityTest::imc_->clientInfo_.config.windowId = INVALID_WINDOW_ID; CallingWindowInfo windowInfo; int32_t ret = InputMethodAbilityTest::inputMethodAbility_.GetCallingWindowInfo(windowInfo); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_TRUE(ret == ErrorCode::NO_ERROR || ret == ErrorCode::ERROR_WINDOW_MANAGER); InputMethodAbilityTest::inputMethodAbility_.DestroyPanel(inputMethodPanel); InputMethodAbilityTest::GetIMCDetachIMA(); } diff --git a/test/unittest/cpp_test/src/input_method_attach_test.cpp b/test/unittest/cpp_test/src/input_method_attach_test.cpp index 412b4420f..33b806e24 100644 --- a/test/unittest/cpp_test/src/input_method_attach_test.cpp +++ b/test/unittest/cpp_test/src/input_method_attach_test.cpp @@ -736,7 +736,6 @@ HWTEST_F(InputMethodAttachTest, testImeCallbackInAttach, TestSize.Level0) EXPECT_TRUE(KeyboardListenerTestImpl::WaitSelectionChange(selectionRange.start)); EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate()); EXPECT_TRUE(KeyboardListenerTestImpl::WaitEditorAttributeChange(attribute)); - EXPECT_TRUE(InputMethodEngineListenerImpl::WaitSetCallingWindow(config.windowId)); } /** diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index be683ab8d..d46a83b7d 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -578,6 +578,69 @@ HWTEST_F(InputMethodControllerTest, testIMCAttach002, TestSize.Level0) InputMethodControllerTest::CheckTextConfig(textConfig); } +/** + * @tc.name: testIMCAttach003 + * @tc.desc: IMC Attach. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodControllerTest, testIMCAttach003, TestSize.Level0) +{ + PanelInfo info = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING }; + auto panel = std::make_shared(); + auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextListener::ResetParam(); + CursorInfo cursorInfo = { 1, 1, 1, 1 }; + Range selectionRange = { 1, 2 }; + InputAttribute attribute = { 1, 1 }; + uint32_t windowId = 10; + TextConfig textConfig = { + .inputAttribute = attribute, .cursorInfo = cursorInfo, .range = selectionRange, .windowId = windowId + }; + + inputMethodController_->Attach(textListener_, true, textConfig); + InputMethodControllerTest::CheckTextConfig(textConfig); + EXPECT_EQ(imeListener_->windowId_, textConfig.windowId); + + TextListener::ResetParam(); + cursorInfo = { 2, 2, 2, 2 }; + selectionRange = { 3, 4 }; + attribute = { 2, 2 }; + windowId = 11; + textConfig = { + .inputAttribute = attribute, .cursorInfo = cursorInfo, .range = selectionRange, .windowId = windowId + }; + inputMethodController_->Attach(textListener_, true, textConfig); + InputMethodControllerTest::CheckTextConfig(textConfig); + inputMethodAbility_->DestroyPanel(panel); + EXPECT_EQ(imeListener_->windowId_, textConfig.windowId); +} + +/** + * @tc.name: testIsKeyboardCallingProcess + * @tc.desc: IMC Attach. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodControllerTest, IsKeyboardCallingProcess, TestSize.Level0) +{ + IMSA_HILOGI("IMC IsKeyboardCallingProcess Test START"); + IMSA_HILOGI("IMC testIsKeyboardCallingProcess Test START"); + auto ret = inputMethodController_->IsKeyboardCallingProcess(0); + EXPECT_FALSE(ret); + + imeListener_->isInputStart_ = false; + TextListener::ResetParam(); + inputMethodController_->Attach(textListener_, true); + ret = inputMethodController_->IsKeyboardCallingProcess(getpid()); + EXPECT_TRUE(ret); + + TextListener::ResetParam(); + inputMethodController_->DeactivateClient(); +} + /** * @tc.name: testIMCSetCallingWindow * @tc.desc: IMC SetCallingWindow. diff --git a/test/unittest/cpp_test/src/input_method_panel_test.cpp b/test/unittest/cpp_test/src/input_method_panel_test.cpp index 7af01430c..c2f8610bd 100644 --- a/test/unittest/cpp_test/src/input_method_panel_test.cpp +++ b/test/unittest/cpp_test/src/input_method_panel_test.cpp @@ -1864,7 +1864,7 @@ HWTEST_F(InputMethodPanelTest, testGetCallingWindowInfo02, TestSize.Level0) InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); CallingWindowInfo windowInfo; auto ret = inputMethodPanel->GetCallingWindowInfo(windowInfo); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(ret, ErrorCode::ERROR_WINDOW_MANAGER); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); InputMethodPanelTest::imc_->Close(); TddUtil::DestroyWindow(); -- Gitee