!794 [Bugfix]: 修改焦点监听解绑机制

Merge pull request !794 from 赵凌岚/master
This commit is contained in:
openharmony_ci 2023-07-11 09:36:57 +00:00 committed by Gitee
commit 0838d1ec4b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 59 additions and 11 deletions

View File

@ -20,7 +20,7 @@
namespace OHOS {
namespace MiscServices {
using FocusHandle = std::function<void(int32_t, int32_t)>;
using FocusHandle = std::function<void(bool, int32_t, int32_t)>;
class FocusMonitorManager {
public:
static FocusMonitorManager &GetInstance();

View File

@ -21,6 +21,11 @@ namespace OHOS {
namespace MiscServices {
void FocusChangedListener::OnFocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo)
{
if (focusChangeInfo == nullptr || focusHandle_ == nullptr) {
IMSA_HILOGE("error nullptr");
return;
}
focusHandle_(true, focusChangeInfo->pid_, focusChangeInfo->uid_);
}
void FocusChangedListener::OnUnfocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo)
@ -29,7 +34,7 @@ void FocusChangedListener::OnUnfocused(const sptr<Rosen::FocusChangeInfo> &focus
IMSA_HILOGE("error nullptr");
return;
}
focusHandle_(focusChangeInfo->pid_, focusChangeInfo->uid_);
focusHandle_(false, focusChangeInfo->pid_, focusChangeInfo->uid_);
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -84,6 +84,7 @@ public:
void StopInputService(std::string imeId);
int32_t OnSwitchIme(const Property &property, const SubProperty &subProperty, bool isSubtypeSwitch);
void UpdateCurrentUserId(int32_t userId);
void OnFocused(int32_t pid, int32_t uid);
void OnUnfocused(int32_t pid, int32_t uid);
bool CheckFocused(uint32_t tokenId);
int32_t OnPanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo);
@ -137,6 +138,8 @@ private:
void SetAgent(sptr<IInputMethodAgent> agent);
sptr<IInputMethodAgent> GetAgent();
sptr<AAFwk::IAbilityManager> GetAbilityManagerService();
bool IsCurrentClient(int32_t pid, int32_t uid);
void UnbindClient(const sptr<IInputClient> &client);
static inline bool IsValid(int32_t index)
{

View File

@ -747,9 +747,10 @@ int32_t InputMethodSystemAbility::InitKeyEventMonitor()
bool InputMethodSystemAbility::InitFocusChangeMonitor()
{
return ImCommonEventManager::GetInstance()->SubscribeWindowManagerService(
[this](int32_t pid, int32_t uid) { return userSession_->OnUnfocused(pid, uid); },
[this](int32_t userId) { StartInputService(ImeInfoInquirer::GetInstance().GetStartedIme(userId_)); }
);
[this](bool isOnFocused, int32_t pid, int32_t uid) {
return isOnFocused ? userSession_->OnFocused(pid, uid) : userSession_->OnUnfocused(pid, uid);
},
[this](int32_t userId) { StartInputService(ImeInfoInquirer::GetInstance().GetStartedIme(userId_)); });
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -536,27 +536,66 @@ void PerUserSession::SetAgent(sptr<IInputMethodAgent> agent)
agent_ = agent;
}
void PerUserSession::OnUnfocused(int32_t pid, int32_t uid)
void PerUserSession::OnFocused(int32_t pid, int32_t uid)
{
if (IsCurrentClient(pid, uid)) {
IMSA_HILOGD("pid[%{public}d] same as current client", pid);
return;
}
auto client = GetCurrentClient();
if (client == nullptr) {
IMSA_HILOGD("no client in bound state");
return;
}
auto clientInfo = GetClientInfo(client->AsObject());
if (clientInfo == nullptr) {
IMSA_HILOGI("focus shifts to pid: %{public}d, start unbinding", pid);
UnbindClient(client);
InputMethodSysEvent::OperateSoftkeyboardBehaviour(IME_HIDE_UNFOCUSED);
}
void PerUserSession::OnUnfocused(int32_t pid, int32_t uid)
{
if (IsCurrentClient(pid, uid)) {
IMSA_HILOGD("pid[%{public}d] same as current client", pid);
return;
}
if (clientInfo->pid != pid || clientInfo->uid != uid) {
std::lock_guard<std::recursive_mutex> lock(mtx);
for (const auto &mapClient : mapClients_) {
if (mapClient.second->pid == pid) {
IMSA_HILOGI("clear unfocused client info: %{public}d", pid);
UnbindClient(mapClient.second->client);
InputMethodSysEvent::OperateSoftkeyboardBehaviour(IME_HIDE_UNFOCUSED);
break;
}
}
}
void PerUserSession::UnbindClient(const sptr<IInputClient> &client)
{
if (client == nullptr) {
IMSA_HILOGE("client is nullptr");
return;
}
IMSA_HILOGI("current client is unfocused, start unbinding");
int32_t ret = client->OnInputStop();
IMSA_HILOGI("OnInputStop ret: %{public}d", ret);
ret = OnReleaseInput(client);
InputMethodSysEvent::OperateSoftkeyboardBehaviour(IME_HIDE_UNFOCUSED);
IMSA_HILOGI("release input ret: %{public}d", ret);
}
bool PerUserSession::IsCurrentClient(int32_t pid, int32_t uid)
{
auto client = GetCurrentClient();
if (client == nullptr) {
IMSA_HILOGD("no client in bound state");
return false;
}
auto clientInfo = GetClientInfo(client->AsObject());
if (clientInfo == nullptr) {
IMSA_HILOGE("failed to get client info");
return false;
}
return clientInfo->pid == pid && clientInfo->uid == uid;
}
sptr<AAFwk::IAbilityManager> PerUserSession::GetAbilityManagerService()
{
IMSA_HILOGD("InputMethodSystemAbility::GetAbilityManagerService start");