Bug 1446729 - 1. Get rid of DebugGeckoInputConnection; r?esawin draft
authorJim Chen <nchen@mozilla.com>
Sun, 18 Mar 2018 05:38:55 -0400
changeset 769091 3634fa6023a3316b6a2155e4278965feb5e2f42b
parent 769090 90963e9c24b1f30a9cbbd4aa8aa1edb11984bf64
child 769092 605375eea828da85adbeeeeb625b17d41fb75c70
push id103035
push userbmo:nchen@mozilla.com
push dateSun, 18 Mar 2018 09:44:48 +0000
reviewersesawin
bugs1446729
milestone61.0a1
Bug 1446729 - 1. Get rid of DebugGeckoInputConnection; r?esawin DebugGeckoInputConnection was a wrapper around GeckoInputConnection to log InputConnection calls. Move its functionality to GeckoInputConnection.wrapForDebug, so we don't have a secondary class in GeckoInputConnection.java. MozReview-Commit-ID: 6uh5kuNenpD
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoInputConnection.java
@@ -85,21 +85,80 @@ public class GeckoInputConnection
 
     // Prevent showSoftInput and hideSoftInput from causing reentrant calls on some devices.
     private volatile boolean mSoftInputReentrancyGuard;
 
     public static TextInputController.Delegate create(
             final GeckoSession session,
             final View targetView,
             final TextInputController.EditableClient editable) {
+        TextInputController.Delegate ic = new GeckoInputConnection(session, targetView, editable);
         if (DEBUG) {
-            return DebugGeckoInputConnection.create(session, targetView, editable);
-        } else {
-            return new GeckoInputConnection(session, targetView, editable);
+            ic = wrapForDebug(ic);
         }
+        return ic;
+    }
+
+    private static TextInputController.Delegate wrapForDebug(final TextInputController.Delegate ic) {
+        final InvocationHandler handler = new InvocationHandler() {
+            private final StringBuilder mCallLevel = new StringBuilder();
+
+            @Override
+            public Object invoke(final Object proxy, final Method method,
+                                 final Object[] args) throws Throwable {
+                final StringBuilder log = new StringBuilder(mCallLevel);
+                log.append("> ").append(method.getName()).append("(");
+                if (args != null) {
+                    for (int i = 0; i < args.length; i++) {
+                        final Object arg = args[i];
+                        // translate argument values to constant names
+                        if ("notifyIME".equals(method.getName()) && i == 0) {
+                            log.append(GeckoEditable.getConstantName(
+                                    TextInputController.EditableListener.class,
+                                    "NOTIFY_IME_", arg));
+                        } else if ("notifyIMEContext".equals(method.getName()) && i == 0) {
+                            log.append(GeckoEditable.getConstantName(
+                                    TextInputController.EditableListener.class,
+                                    "IME_STATE_", arg));
+                        } else {
+                            GeckoEditable.debugAppend(log, arg);
+                        }
+                        log.append(", ");
+                    }
+                    if (args.length > 0) {
+                        log.setLength(log.length() - 2);
+                    }
+                }
+                log.append(")");
+                Log.d(LOGTAG, log.toString());
+
+                mCallLevel.append(' ');
+                Object ret = method.invoke(ic, args);
+                if (ret == ic) {
+                    ret = proxy;
+                }
+                mCallLevel.setLength(Math.max(0, mCallLevel.length() - 1));
+
+                log.setLength(mCallLevel.length());
+                log.append("< ").append(method.getName());
+                if (!method.getReturnType().equals(Void.TYPE)) {
+                    GeckoEditable.debugAppend(log.append(": "), ret);
+                }
+                Log.d(LOGTAG, log.toString());
+                return ret;
+            }
+        };
+
+        return (TextInputController.Delegate) Proxy.newProxyInstance(
+                GeckoInputConnection.class.getClassLoader(),
+                new Class<?>[] {
+                        InputConnection.class,
+                        TextInputController.Delegate.class,
+                        TextInputController.EditableListener.class
+                }, handler);
     }
 
     protected GeckoInputConnection(final GeckoSession session,
                                    final View targetView,
                                    final TextInputController.EditableClient editable) {
         super(targetView, true);
         mSession = session;
         mView = targetView;
@@ -1031,81 +1090,8 @@ public class GeckoInputConnection
         // notifyIME, so we skip restartInput here. On blur, the notifyIMEContext call
         // comes *after* the notifyIME(NOTIFY_IME_OF_BLUR) call, and we need to call
         // restartInput here.
         if (mIMEState == IME_STATE_DISABLED || mFocused) {
             restartInput();
         }
     }
 }
-
-final class DebugGeckoInputConnection
-        extends GeckoInputConnection
-        implements InvocationHandler {
-
-    private InputConnection mProxy;
-    private final StringBuilder mCallLevel;
-
-    private DebugGeckoInputConnection(final GeckoSession session,
-                                      final View targetView,
-                                      final TextInputController.EditableClient editable) {
-        super(session, targetView, editable);
-        mCallLevel = new StringBuilder();
-    }
-
-    public static TextInputController.Delegate create(
-            final GeckoSession session,
-            final View targetView,
-            final TextInputController.EditableClient editable) {
-        final Class<?>[] PROXY_INTERFACES = { InputConnection.class,
-                TextInputController.Delegate.class,
-                TextInputController.EditableListener.class };
-        DebugGeckoInputConnection dgic =
-                new DebugGeckoInputConnection(session, targetView, editable);
-        dgic.mProxy = (InputConnection) Proxy.newProxyInstance(
-                GeckoInputConnection.class.getClassLoader(),
-                PROXY_INTERFACES, dgic);
-        return (TextInputController.Delegate) dgic.mProxy;
-    }
-
-    @Override
-    public Object invoke(Object proxy, Method method, Object[] args)
-            throws Throwable {
-
-        StringBuilder log = new StringBuilder(mCallLevel);
-        log.append("> ").append(method.getName()).append("(");
-        if (args != null) {
-            for (Object arg : args) {
-                // translate argument values to constant names
-                if ("notifyIME".equals(method.getName()) && arg == args[0]) {
-                    log.append(GeckoEditable.getConstantName(
-                        TextInputController.EditableListener.class, "NOTIFY_IME_", arg));
-                } else if ("notifyIMEContext".equals(method.getName()) && arg == args[0]) {
-                    log.append(GeckoEditable.getConstantName(
-                        TextInputController.EditableListener.class, "IME_STATE_", arg));
-                } else {
-                    GeckoEditable.debugAppend(log, arg);
-                }
-                log.append(", ");
-            }
-            if (args.length > 0) {
-                log.setLength(log.length() - 2);
-            }
-        }
-        log.append(")");
-        Log.d(LOGTAG, log.toString());
-
-        mCallLevel.append(' ');
-        Object ret = method.invoke(this, args);
-        if (ret == this) {
-            ret = mProxy;
-        }
-        mCallLevel.setLength(Math.max(0, mCallLevel.length() - 1));
-
-        log.setLength(mCallLevel.length());
-        log.append("< ").append(method.getName());
-        if (!method.getReturnType().equals(Void.TYPE)) {
-            GeckoEditable.debugAppend(log.append(": "), ret);
-        }
-        Log.d(LOGTAG, log.toString());
-        return ret;
-    }
-}