From 0b59e691521612d3848c9aeffae3949cc22a5469 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Tue, 13 Nov 2012 17:27:19 -0500 Subject: [PATCH] Bug 808287 - Fix out-of-order IME events during focus change; r=cpeterson --- mobile/android/base/GeckoEditable.java | 6 ++++++ mobile/android/base/GeckoEvent.java | 1 + widget/android/AndroidJavaWrappers.h | 3 ++- widget/android/nsWindow.cpp | 21 ++++++++++++++++++++- widget/android/nsWindow.h | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/mobile/android/base/GeckoEditable.java b/mobile/android/base/GeckoEditable.java index 746d0441ed82..e9abea9f3952 100644 --- a/mobile/android/base/GeckoEditable.java +++ b/mobile/android/base/GeckoEditable.java @@ -53,6 +53,7 @@ final class GeckoEditable private static final boolean DEBUG = false; private static final String LOGTAG = "GeckoEditable"; private static final int NOTIFY_IME_REPLY_EVENT = 1; + private static final int NOTIFY_IME_FOCUSCHANGE = 3; // Filters to implement Editable's filtering functionality private InputFilter[] mFilters; @@ -464,6 +465,11 @@ final class GeckoEditable public void run() { // Make sure there are no other things going on mActionQueue.syncWithGecko(); + if (type == NOTIFY_IME_FOCUSCHANGE && state != 0) { + // Unmask events on the Gecko side + GeckoAppShell.sendEventToGecko(GeckoEvent.createIMEEvent( + GeckoEvent.IME_ACKNOWLEDGE_FOCUS)); + } if (mListener != null) { mListener.notifyIME(type, state); } diff --git a/mobile/android/base/GeckoEvent.java b/mobile/android/base/GeckoEvent.java index 578c7c39442f..2200f6966243 100644 --- a/mobile/android/base/GeckoEvent.java +++ b/mobile/android/base/GeckoEvent.java @@ -86,6 +86,7 @@ public class GeckoEvent { public static final int IME_ADD_COMPOSITION_RANGE = 3; public static final int IME_UPDATE_COMPOSITION = 4; public static final int IME_REMOVE_COMPOSITION = 5; + public static final int IME_ACKNOWLEDGE_FOCUS = 6; public static final int IME_RANGE_CARETPOSITION = 1; public static final int IME_RANGE_RAWINPUT = 2; diff --git a/widget/android/AndroidJavaWrappers.h b/widget/android/AndroidJavaWrappers.h index 8742278c72a7..20a8a5c6008f 100644 --- a/widget/android/AndroidJavaWrappers.h +++ b/widget/android/AndroidJavaWrappers.h @@ -815,7 +815,8 @@ public: IME_SET_SELECTION = 2, IME_ADD_COMPOSITION_RANGE = 3, IME_UPDATE_COMPOSITION = 4, - IME_REMOVE_COMPOSITION = 5 + IME_REMOVE_COMPOSITION = 5, + IME_ACKNOWLEDGE_FOCUS = 6 }; }; diff --git a/widget/android/nsWindow.cpp b/widget/android/nsWindow.cpp index 9c64b17fdd28..31421048dfdd 100644 --- a/widget/android/nsWindow.cpp +++ b/widget/android/nsWindow.cpp @@ -163,7 +163,9 @@ nsWindow::nsWindow() : mFocus(nullptr), mIMEComposing(false), mIMEMaskSelectionUpdate(false), - mIMEMaskTextUpdate(false) + mIMEMaskTextUpdate(false), + // Mask IME events initially because there is no focused editors yet + mIMEMaskEvents(true) { } @@ -1880,6 +1882,18 @@ nsWindow::OnIMEEvent(AndroidGeckoEvent *ae) composition through update composition events */ nsRefPtr kungFuDeathGrip(this); + + if (ae->Action() == AndroidGeckoEvent::IME_ACKNOWLEDGE_FOCUS) { + mIMEMaskEvents = false; + return; + } else if (mIMEMaskEvents) { + // Still reply to events, but don't do anything else + if (ae->Action() == AndroidGeckoEvent::IME_SYNCHRONIZE || + ae->Action() == AndroidGeckoEvent::IME_REPLACE_TEXT) { + AndroidBridge::NotifyIME(AndroidBridge::NOTIFY_IME_REPLY_EVENT, 0); + } + return; + } switch (ae->Action()) { case AndroidGeckoEvent::IME_SYNCHRONIZE: { @@ -2166,6 +2180,11 @@ nsWindow::OnIMEFocusChange(bool aFocus) if (aFocus) { OnIMETextChange(0, INT32_MAX, INT32_MAX); OnIMESelectionChange(); + } else { + // Mask events because we lost focus. On the next focus event, Gecko will notify + // Java, and Java will send an acknowledge focus event back to Gecko. That is + // where we unmask event handling + mIMEMaskEvents = true; } AndroidBridge::NotifyIME(AndroidBridge::NOTIFY_IME_FOCUSCHANGE, diff --git a/widget/android/nsWindow.h b/widget/android/nsWindow.h index 5e4ae6d8527c..760d8a277814 100644 --- a/widget/android/nsWindow.h +++ b/widget/android/nsWindow.h @@ -184,6 +184,7 @@ protected: bool mIMEComposing; bool mIMEMaskSelectionUpdate, mIMEMaskTextUpdate; + bool mIMEMaskEvents; nsString mIMEComposingText; nsAutoTArray mIMERanges;