1111import androidx .annotation .Nullable ;
1212import io .flutter .Log ;
1313import io .flutter .embedding .engine .systemchannels .KeyEventChannel ;
14- import io .flutter .embedding .engine .systemchannels .KeyEventChannel .FlutterKeyEvent ;
1514import io .flutter .plugin .editing .TextInputPlugin ;
16- import java .util .AbstractMap .SimpleImmutableEntry ;
1715import java .util .ArrayDeque ;
1816import java .util .Deque ;
19- import java .util .Map .Entry ;
2017
2118/**
2219 * A class to process key events from Android, passing them to the framework as messages using
@@ -96,20 +93,19 @@ public boolean onKeyEvent(@NonNull KeyEvent event) {
9693 // case the theory is wrong.
9794 return false ;
9895 }
99- long eventId = FlutterKeyEvent .computeEventId (event );
100- if (eventResponder .isHeadEvent (eventId )) {
96+ if (eventResponder .isHeadEvent (event )) {
10197 // If the event is at the head of the queue of pending events we've seen,
10298 // and has the same id, then we know that this is a re-dispatched event, and
10399 // we shouldn't respond to it, but we should remove it from tracking now.
104- eventResponder .removePendingEvent ( eventId );
100+ eventResponder .removeHeadEvent ( );
105101 return false ;
106102 }
107103
108104 Character complexCharacter = applyCombiningCharacterToBaseCharacter (event .getUnicodeChar ());
109105 KeyEventChannel .FlutterKeyEvent flutterEvent =
110106 new KeyEventChannel .FlutterKeyEvent (event , complexCharacter );
111107
112- eventResponder .addEvent (flutterEvent . eventId , event );
108+ eventResponder .addEvent (event );
113109 if (action == KeyEvent .ACTION_DOWN ) {
114110 keyEventChannel .keyDown (flutterEvent );
115111 } else {
@@ -127,8 +123,7 @@ public boolean onKeyEvent(@NonNull KeyEvent event) {
127123 * @return
128124 */
129125 public boolean isCurrentEvent (@ NonNull KeyEvent event ) {
130- long id = FlutterKeyEvent .computeEventId (event );
131- return eventResponder .isHeadEvent (id );
126+ return eventResponder .isHeadEvent (event );
132127 }
133128
134129 /**
@@ -194,7 +189,7 @@ private static class EventResponder implements KeyEventChannel.EventResponseHand
194189 // The maximum number of pending events that are held before starting to
195190 // complain.
196191 private static final long MAX_PENDING_EVENTS = 1000 ;
197- final Deque <Entry < Long , KeyEvent >> pendingEvents = new ArrayDeque <Entry < Long , KeyEvent > >();
192+ final Deque <KeyEvent > pendingEvents = new ArrayDeque <KeyEvent >();
198193 @ NonNull private final View view ;
199194 @ NonNull private final TextInputPlugin textInputPlugin ;
200195
@@ -203,67 +198,54 @@ public EventResponder(@NonNull View view, @NonNull TextInputPlugin textInputPlug
203198 this .textInputPlugin = textInputPlugin ;
204199 }
205200
206- /**
207- * Removes the pending event with the given id from the cache of pending events.
208- *
209- * @param id the id of the event to be removed.
210- */
211- private KeyEvent removePendingEvent (long id ) {
212- if (pendingEvents .getFirst ().getKey () != id ) {
213- throw new AssertionError (
214- "Event response received out of order. Should have seen event "
215- + pendingEvents .getFirst ().getKey ()
216- + " first. Instead, received "
217- + id );
218- }
219- return pendingEvents .removeFirst ().getValue ();
201+ /** Removes the first pending event from the cache of pending events. */
202+ private KeyEvent removeHeadEvent () {
203+ return pendingEvents .removeFirst ();
220204 }
221205
222- private KeyEvent findPendingEvent ( long id ) {
206+ private KeyEvent checkIsHeadEvent ( KeyEvent event ) {
223207 if (pendingEvents .size () == 0 ) {
224208 throw new AssertionError (
225- "Event response received when no events are in the queue. Received id " + id );
209+ "Event response received when no events are in the queue. Received event " + event );
226210 }
227- if (pendingEvents .getFirst (). getKey () != id ) {
211+ if (pendingEvents .getFirst () != event ) {
228212 throw new AssertionError (
229213 "Event response received out of order. Should have seen event "
230- + pendingEvents .getFirst (). getKey ()
214+ + pendingEvents .getFirst ()
231215 + " first. Instead, received "
232- + id );
216+ + event );
233217 }
234- return pendingEvents .getFirst (). getValue () ;
218+ return pendingEvents .getFirst ();
235219 }
236220
237- private boolean isHeadEvent (long id ) {
238- return pendingEvents .size () > 0 && pendingEvents .getFirst (). getKey () == id ;
221+ private boolean isHeadEvent (KeyEvent event ) {
222+ return pendingEvents .size () > 0 && pendingEvents .getFirst () == event ;
239223 }
240224
241225 /**
242226 * Called whenever the framework responds that a given key event was handled by the framework.
243227 *
244- * @param id the event id of the event to be marked as being handled by the framework. Must not
245- * be null.
228+ * @param event the event to be marked as being handled by the framework. Must not be null.
246229 */
247230 @ Override
248- public void onKeyEventHandled (long id ) {
249- removePendingEvent ( id );
231+ public void onKeyEventHandled (KeyEvent event ) {
232+ removeHeadEvent ( );
250233 }
251234
252235 /**
253236 * Called whenever the framework responds that a given key event wasn't handled by the
254237 * framework.
255238 *
256- * @param id the event id of the event to be marked as not being handled by the framework. Must
257- * not be null.
239+ * @param event the event to be marked as not being handled by the framework. Must not be null.
258240 */
259241 @ Override
260- public void onKeyEventNotHandled (long id ) {
261- dispatchKeyEvent ( findPendingEvent ( id ), id );
242+ public void onKeyEventNotHandled (KeyEvent event ) {
243+ redispatchKeyEvent ( checkIsHeadEvent ( event ) );
262244 }
263245
264- /** Adds an Android key event with an id to the event responder to wait for a response. */
265- public void addEvent (long id , @ NonNull KeyEvent event ) {
266- pendingEvents .addLast (new SimpleImmutableEntry < Long , KeyEvent >( id , event ) );
246+ /** Adds an Android key event to the event responder to wait for a response. */
247+ public void addEvent (@ NonNull KeyEvent event ) {
248+ pendingEvents .addLast (event );
267249 if (pendingEvents .size () > MAX_PENDING_EVENTS ) {
268250 Log .e (
269251 TAG ,
@@ -279,15 +261,15 @@ public void addEvent(long id, @NonNull KeyEvent event) {
279261 *
280262 * @param event the event to be dispatched to the activity.
281263 */
282- public void dispatchKeyEvent (KeyEvent event , long id ) {
264+ private void redispatchKeyEvent (KeyEvent event ) {
283265 // If the textInputPlugin is still valid and accepting text, then we'll try
284266 // and send the key event to it, assuming that if the event can be sent,
285267 // that it has been handled.
286268 if (textInputPlugin .getInputMethodManager ().isAcceptingText ()
287269 && textInputPlugin .getLastInputConnection () != null
288270 && textInputPlugin .getLastInputConnection ().sendKeyEvent (event )) {
289271 // The event was handled, so we can remove it from the queue.
290- removePendingEvent ( id );
272+ removeHeadEvent ( );
291273 return ;
292274 }
293275
0 commit comments