Bug 1464096 - 1. Add session parameter to all SessionTextInput.Delegate methods; r?esawin draft
authorJim Chen <nchen@mozilla.com>
Tue, 05 Jun 2018 17:49:00 -0400
changeset 804392 687fcdddf0c634b1a16dcb174a9808e8fb6ce334
parent 804003 a358755643e92f8fc55a23e3ab1fbf88695b8bf3
child 804393 867965bab9fa4ddc423dee7e01cc034105456c80
push id112368
push userbmo:nchen@mozilla.com
push dateTue, 05 Jun 2018 21:49:45 +0000
reviewersesawin
bugs1464096
milestone62.0a1
Bug 1464096 - 1. Add session parameter to all SessionTextInput.Delegate methods; r?esawin Follow the GV convention and add a session parameter to all methods in SessionTextInput.Delegate. MozReview-Commit-ID: 6uGI5THe46k
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionTextInput.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoInputConnection.java
@@ -284,41 +284,41 @@ import java.lang.reflect.Proxy;
             @Override
             public void run() {
                 if (showToolbar) {
                     mSession.getDynamicToolbarAnimator().showToolbar(/* immediately */ true);
                 }
                 mSession.getEventDispatcher().dispatch("GeckoView:ZoomToInput", null);
 
                 mSoftInputReentrancyGuard = true;
-                getInputDelegate().showSoftInput();
+                getInputDelegate().showSoftInput(mSession);
                 mSoftInputReentrancyGuard = false;
             }
         });
     }
 
     private void hideSoftInput() {
         if (mSoftInputReentrancyGuard) {
             return;
         }
         getView().post(new Runnable() {
             @Override
             public void run() {
                 mSoftInputReentrancyGuard = true;
-                getInputDelegate().hideSoftInput();
+                getInputDelegate().hideSoftInput(mSession);
                 mSoftInputReentrancyGuard = false;
             }
         });
     }
 
     private void restartInput(final @SessionTextInput.Delegate.RestartReason int reason) {
         getView().post(new Runnable() {
             @Override
             public void run() {
-                getInputDelegate().restartInput(reason);
+                getInputDelegate().restartInput(mSession, reason);
             }
         });
     }
 
     @Override // SessionTextInput.EditableListener
     public void onTextChange() {
         final Editable editable = getEditable();
         if (mUpdateRequest == null || editable == null) {
@@ -338,17 +338,17 @@ import java.lang.reflect.Proxy;
             extractedText.text = new SpannableString(editable);
         } else {
             extractedText.text = editable.toString();
         }
 
         getView().post(new Runnable() {
             @Override
             public void run() {
-                getInputDelegate().updateExtractedText(request, extractedText);
+                getInputDelegate().updateExtractedText(mSession, request, extractedText);
             }
         });
     }
 
     @Override // SessionTextInput.EditableListener
     public void onSelectionChange() {
 
         final Editable editable = getEditable();
@@ -366,17 +366,18 @@ import java.lang.reflect.Proxy;
         }
 
         final int compositionStart = getComposingSpanStart(editable);
         final int compositionEnd = getComposingSpanEnd(editable);
 
         getView().post(new Runnable() {
             @Override
             public void run() {
-                getInputDelegate().updateSelection(start, end, compositionStart, compositionEnd);
+                getInputDelegate().updateSelection(mSession, start, end,
+                                                   compositionStart, compositionEnd);
             }
         });
     }
 
     @TargetApi(21)
     @Override // SessionTextInput.EditableListener
     public void updateCompositionRects(final RectF[] rects) {
         if (!(Build.VERSION.SDK_INT >= 21)) {
@@ -432,17 +433,17 @@ import java.lang.reflect.Proxy;
         }
 
         mCursorAnchorInfoBuilder.setComposingText(0, composition);
 
         final CursorAnchorInfo info = mCursorAnchorInfoBuilder.build();
         getView().post(new Runnable() {
             @Override
             public void run() {
-                getInputDelegate().updateCursorAnchorInfo(info);
+                getInputDelegate().updateCursorAnchorInfo(mSession, info);
             }
         });
     }
 
     @Override
     public boolean requestCursorUpdates(int cursorUpdateMode) {
 
         if ((cursorUpdateMode & InputConnection.CURSOR_UPDATE_IMMEDIATE) != 0) {
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionTextInput.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/SessionTextInput.java
@@ -66,61 +66,69 @@ public final class SessionTextInput {
          * Reset the input method, and discard any existing states such as the current composition
          * or current autocompletion. Because the current focused editor may have changed, as
          * part of the reset, a custom input method would normally call {@link
          * #onCreateInputConnection} to update its knowledge of the focused editor. Note that
          * {@code restartInput} should be used to detect changes in focus, rather than {@link
          * #showSoftInput} or {@link #hideSoftInput}, because focus changes are not always
          * accompanied by requests to show or hide the soft input.
          *
+         * @param session Session instance.
          * @param reason Reason for the reset.
          */
-        void restartInput(@RestartReason int reason);
+        void restartInput(@NonNull GeckoSession session, @RestartReason int reason);
 
         /**
          * Display the soft input.
          *
+         * @param session Session instance.
          * @see #hideSoftInput
          * */
-        void showSoftInput();
+        void showSoftInput(@NonNull GeckoSession session);
 
         /**
          * Hide the soft input.
          *
+         * @param session Session instance.
          * @see #showSoftInput
          * */
-        void hideSoftInput();
+        void hideSoftInput(@NonNull GeckoSession session);
 
         /**
          * Update the soft input on the current selection.
          *
+         * @param session Session instance.
          * @param selStart Start offset of the selection.
          * @param selEnd End offset of the selection.
          * @param compositionStart Composition start offset, or -1 if there is no composition.
          * @param compositionEnd Composition end offset, or -1 if there is no composition.
          */
-        void updateSelection(int selStart, int selEnd, int compositionStart, int compositionEnd);
+        void updateSelection(@NonNull GeckoSession session, int selStart, int selEnd,
+                             int compositionStart, int compositionEnd);
 
         /**
          * Update the soft input on the current extracted text as requested through
          * InputConnection.getExtractText.
          *
+         * @param session Session instance.
          * @param request The extract text request.
          * @param text The extracted text.
          */
-        void updateExtractedText(@NonNull ExtractedTextRequest request,
+        void updateExtractedText(@NonNull GeckoSession session,
+                                 @NonNull ExtractedTextRequest request,
                                  @NonNull ExtractedText text);
 
         /**
          * Update the cursor-anchor information as requested through
          * InputConnection.requestCursorUpdates.
          *
+         * @param session Session instance.
          * @param info Cursor-anchor information.
          */
-        void updateCursorAnchorInfo(@NonNull CursorAnchorInfo info);
+        void updateCursorAnchorInfo(@NonNull GeckoSession session, @NonNull CursorAnchorInfo info);
     }
 
     // Interface to access GeckoInputConnection from SessionTextInput.
     /* package */ interface InputConnectionClient {
         View getView();
         Handler getHandler(Handler defHandler);
         InputConnection onCreateInputConnection(EditorInfo attrs);
         boolean isInputActive();
@@ -171,29 +179,31 @@ public final class SessionTextInput {
         void notifyIMEContext(int state, String typeHint, String modeHint,
                               String actionHint, int flag);
         void onSelectionChange();
         void onTextChange();
         void onDefaultKeyEvent(KeyEvent event);
         void updateCompositionRects(final RectF[] aRects);
     }
 
-    private final class DefaultDelegate implements Delegate {
+    private static final class DefaultDelegate implements Delegate {
+        public static final DefaultDelegate INSTANCE = new DefaultDelegate();
+
         private InputMethodManager getInputMethodManager(@Nullable final View view) {
             if (view == null) {
                 return null;
             }
             return (InputMethodManager) view.getContext()
                                             .getSystemService(Context.INPUT_METHOD_SERVICE);
         }
 
         @Override
-        public void restartInput(int reason) {
+        public void restartInput(@NonNull final GeckoSession session, final int reason) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm == null) {
                 return;
             }
 
             // InputMethodManager has internal logic to detect if we are restarting input
             // in an already focused View, which is the case here because all content text
             // fields are inside one LayerView. When this happens, InputMethodManager will
@@ -213,68 +223,71 @@ public final class SessionTextInput {
             try {
                 imm.restartInput(view);
             } catch (RuntimeException e) {
                 Log.e(LOGTAG, "Error restarting input", e);
             }
         }
 
         @Override
-        public void showSoftInput() {
+        public void showSoftInput(@NonNull final GeckoSession session) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm != null) {
                 if (view.hasFocus() && !imm.isActive(view)) {
                     // Marshmallow workaround: The view has focus but it is not the active
                     // view for the input method. (Bug 1211848)
                     view.clearFocus();
                     view.requestFocus();
                 }
                 imm.showSoftInput(view, 0);
             }
         }
 
         @Override
-        public void hideSoftInput() {
+        public void hideSoftInput(@NonNull final GeckoSession session) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm != null) {
                 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
             }
         }
 
         @Override
-        public void updateSelection(final int selStart, final int selEnd,
+        public void updateSelection(@NonNull final GeckoSession session,
+                                    final int selStart, final int selEnd,
                                     final int compositionStart, final int compositionEnd) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm != null) {
                 imm.updateSelection(view, selStart, selEnd, compositionStart, compositionEnd);
             }
         }
 
         @Override
-        public void updateExtractedText(@NonNull final ExtractedTextRequest request,
+        public void updateExtractedText(@NonNull final GeckoSession session,
+                                        @NonNull final ExtractedTextRequest request,
                                         @NonNull final ExtractedText text) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm != null) {
                 imm.updateExtractedText(view, request.token, text);
             }
         }
 
         @TargetApi(21)
         @Override
-        public void updateCursorAnchorInfo(@NonNull final CursorAnchorInfo info) {
+        public void updateCursorAnchorInfo(@NonNull final GeckoSession session,
+                                           @NonNull final CursorAnchorInfo info) {
             ThreadUtils.assertOnUiThread();
-            final View view = getView();
+            final View view = session.getTextInput().getView();
             final InputMethodManager imm = getInputMethodManager(view);
             if (imm != null) {
                 imm.updateCursorAnchorInfo(view, info);
             }
         }
     }
 
     private final GeckoSession mSession;
@@ -457,13 +470,13 @@ public final class SessionTextInput {
     /**
      * Get the current text input delegate.
      *
      * @return Delegate instance or a default instance if no delegate has been set.
      */
     public Delegate getDelegate() {
         ThreadUtils.assertOnUiThread();
         if (mDelegate == null) {
-            mDelegate = new DefaultDelegate();
+            mDelegate = DefaultDelegate.INSTANCE;
         }
         return mDelegate;
     }
 }