From c3672cd3f7e2bd87d6de9dada499de82b62fae84 Mon Sep 17 00:00:00 2001 From: Wale Ogunwale Date: Wed, 5 Nov 2014 15:17:35 -0800 Subject: [PATCH] Cancel or drop key events if activity is stopped. After an activity instance state is saved due to onStop()/onPause(), there is a small window where it can still get key events like the back button since we still allow the ActivityThread to handle pending messages (like memory trim request) before informing the activity manager that we are done. If the activity is stopped, we will now drop non-terminal input events and set the cancel flag on terminal events. Bug: 18151331 Change-Id: I370d7c871530eea4b16fa42428d0248f1a87abb6 --- core/java/android/view/InputEvent.java | 7 +++++++ core/java/android/view/KeyEvent.java | 10 ++++++++++ core/java/android/view/MotionEvent.java | 6 ++++++ core/java/android/view/ViewRootImpl.java | 19 +++++++++++++------ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/core/java/android/view/InputEvent.java b/core/java/android/view/InputEvent.java index 1ecdf3071281..e2ad3ad45c64 100644 --- a/core/java/android/view/InputEvent.java +++ b/core/java/android/view/InputEvent.java @@ -196,6 +196,13 @@ public abstract class InputEvent implements Parcelable { public abstract long getEventTimeNano(); /** + * Marks the input event as being canceled. + * + * @hide + */ + public abstract void cancel(); + + /** * Gets the unique sequence number of this event. * Every input event that is created or received by a process has a * unique sequence number. Moreover, a new sequence number is obtained diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java index 0701b53e4147..243a0fc17e52 100644 --- a/core/java/android/view/KeyEvent.java +++ b/core/java/android/view/KeyEvent.java @@ -2304,6 +2304,16 @@ public class KeyEvent extends InputEvent implements Parcelable { } /** + * Set {@link #FLAG_CANCELED} flag for the key event. + * + * @hide + */ + @Override + public final void cancel() { + mFlags |= FLAG_CANCELED; + } + + /** * Call this during {@link Callback#onKeyDown} to have the system track * the key through its final up (possibly including a long press). Note * that only one key can be tracked at a time -- if another key down diff --git a/core/java/android/view/MotionEvent.java b/core/java/android/view/MotionEvent.java index ae39b7aeee6d..1c5c41c74905 100644 --- a/core/java/android/view/MotionEvent.java +++ b/core/java/android/view/MotionEvent.java @@ -3168,6 +3168,12 @@ public final class MotionEvent extends InputEvent implements Parcelable { return ev; } + /** @hide */ + @Override + public final void cancel() { + setAction(ACTION_CANCEL); + } + public void writeToParcel(Parcel out, int flags) { out.writeInt(PARCEL_TOKEN_MOTION_EVENT); nativeWriteToParcel(mNativePtr, out); diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 5d2a24be51db..fe174175567b 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -3596,12 +3596,19 @@ public final class ViewRootImpl implements ViewParent, if (mView == null || !mAdded) { Slog.w(TAG, "Dropping event due to root view being removed: " + q.mEvent); return true; - } else if (!mAttachInfo.mHasWindowFocus && - !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER) && - !isTerminalInputEvent(q.mEvent)) { - // If this is a focused event and the window doesn't currently have input focus, - // then drop this event. This could be an event that came back from the previous - // stage but the window has lost focus in the meantime. + } else if ((!mAttachInfo.mHasWindowFocus || mStopped) + && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) { + // This is a focus event and the window doesn't currently have input focus or + // has stopped. This could be an event that came back from the previous stage + // but the window has lost focus or stopped in the meantime. + if (isTerminalInputEvent(q.mEvent)) { + // Don't drop terminal input events, however mark them as canceled. + q.mEvent.cancel(); + Slog.w(TAG, "Cancelling event due to no window focus: " + q.mEvent); + return false; + } + + // Drop non-terminal input events. Slog.w(TAG, "Dropping event due to no window focus: " + q.mEvent); return true; } -- 2.11.0