From 3f513f107ef0754da2b1c704167325151ce9b7ea Mon Sep 17 00:00:00 2001
From: Yohei Yukawa <yukawa@google.com>
Date: Sat, 19 Jul 2014 13:33:42 +0900
Subject: [PATCH] Enable InputMethodService#onUpdateCursor behind the flag

With this CL, InputMethodService#onUpdateCursor and
InputMethodService#onUpdateCursorAnchorInfo can be enabled
on L when ENABLE_CURSOR_ANCHOR_INFO_CALLBACK and
ENABLE_CURSOR_RECT_CALLBACK are specified, respectively.

BUG: 16382260
Change-Id: I29924128f6bd2f08cbd91cc4e82c9c4a6ecce3ff
---
 .../compat/InputMethodServiceCompatUtils.java | 27 -----------------
 .../android/inputmethod/latin/LatinIME.java   | 14 +++++----
 .../latin/define/ProductionFlags.java         | 29 ++++++++++++++-----
 3 files changed, 31 insertions(+), 39 deletions(-)

diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatUtils.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatUtils.java
index cbae6952c1..48047ddbf6 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatUtils.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatUtils.java
@@ -17,7 +17,6 @@
 package com.android.inputmethod.compat;
 
 import android.inputmethodservice.InputMethodService;
-import com.android.inputmethod.latin.define.ProductionFlags;
 
 import java.lang.reflect.Method;
 
@@ -35,30 +34,4 @@ public final class InputMethodServiceCompatUtils {
         return (Boolean)CompatUtils.invoke(ims, false /* defaultValue */,
                 METHOD_enableHardwareAcceleration);
     }
-
-    public static void setCursorAnchorMonitorMode(final InputMethodService ims, final int mode) {
-        if (ProductionFlags.USES_CURSOR_ANCHOR_MONITOR) {
-            ExperimentalAPIUtils.setCursorAnchorMonitorMode(ims, mode);
-        }
-    }
-
-    /*
-     * For unreleased APIs. ProGuard will strip this class entirely, unless used explicitly.
-     */
-    private static final class ExperimentalAPIUtils {
-        // Note that {@link InputMethodManager#setCursorAnchorMonitorMode} is not yet available as
-        // an official API as of API level 19 (Build.VERSION_CODES.KITKAT).
-        private static final Method METHOD_setCursorAnchorMonitorMode = CompatUtils.getMethod(
-                InputMethodService.class, "setCursorAnchorMonitorMode", int.class);
-
-        private ExperimentalAPIUtils() {
-            // This utility class is not publicly instantiable.
-        }
-
-        public static void setCursorAnchorMonitorMode(final InputMethodService ims,
-                final int mode) {
-            CompatUtils.invoke(ims, null /* defaultValue */,
-                    METHOD_setCursorAnchorMonitorMode, mode);
-        }
-    }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index dd0060f4a4..b641f3a8b1 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -55,6 +55,7 @@ import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
 import com.android.inputmethod.annotations.UsedForTesting;
+import com.android.inputmethod.compat.InputConnectionCompatUtils;
 import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
 import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
 import com.android.inputmethod.event.Event;
@@ -413,11 +414,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
                 if (latinIme != null) {
                     executePendingImsCallback(latinIme, editorInfo, restarting);
                     latinIme.onStartInputInternal(editorInfo, restarting);
-                    if (ProductionFlags.USES_CURSOR_ANCHOR_MONITOR) {
-                        // Currently we need to call this every time when the IME is attached to
-                        // new application.
-                        // TODO: Consider if we can do this automatically in the framework.
-                        InputMethodServiceCompatUtils.setCursorAnchorMonitorMode(latinIme, 1);
+                    if (ProductionFlags.ENABLE_CURSOR_RECT_CALLBACK) {
+                        InputConnectionCompatUtils.requestCursorRect(
+                                latinIme.getCurrentInputConnection(), true /* enableMonitor */);
+                    }
+                    if (ProductionFlags.ENABLE_CURSOR_ANCHOR_INFO_CALLBACK) {
+                        InputConnectionCompatUtils.requestCursorAnchorInfo(
+                                latinIme.getCurrentInputConnection(), true /* enableMonitor */,
+                                true /* requestImmediateCallback */);
                     }
                 }
             }
diff --git a/java/src/com/android/inputmethod/latin/define/ProductionFlags.java b/java/src/com/android/inputmethod/latin/define/ProductionFlags.java
index 6dccd789d0..d385cf840c 100644
--- a/java/src/com/android/inputmethod/latin/define/ProductionFlags.java
+++ b/java/src/com/android/inputmethod/latin/define/ProductionFlags.java
@@ -23,15 +23,30 @@ public final class ProductionFlags {
 
     public static final boolean IS_HARDWARE_KEYBOARD_SUPPORTED = false;
 
-    // When true, enable {@link InputMethodService#onUpdateCursor} callback with
-    // {@link InputMethodService#setCursorAnchorMonitorMode}, which is not yet available in
-    // API level 19. Do not turn this on in production until the new API becomes publicly
-    // available.
-    public static final boolean USES_CURSOR_ANCHOR_MONITOR = false;
+    /**
+     *  When true, enable {@link InputMethodService#onUpdateCursorAnchorInfo} callback via
+     *  {@link InputConnection#requestCursorAnchorInfo}. This flag has no effect in API Level 20
+     *  and prior. In general, this callback provides more detailed positional information,
+     *  even though an explicit support is required by the editor.
+     */
+    public static final boolean ENABLE_CURSOR_ANCHOR_INFO_CALLBACK = false;
 
-    // Include all suggestions from all dictionaries in {@link SuggestedWords#mRawSuggestions}.
+    /**
+     * When true, enable {@link InputMethodService#onUpdateCursor} callback via
+     * {@link InputConnection#requestCursorAnchorInfo}. Although this callback has been available
+     * since API Level 3, the callback has never been used until API Level 20. Thus it may or may
+     * not work well as expected. Should rely on {@link InputMethodService#onUpdateCursorAnchorInfo}
+     * whenever possible since it is supposed to be more reliable and accurate.
+     */
+    public static final boolean ENABLE_CURSOR_RECT_CALLBACK = false;
+
+    /**
+     * Include all suggestions from all dictionaries in {@link SuggestedWords#mRawSuggestions}.
+     */
     public static final boolean INCLUDE_RAW_SUGGESTIONS = false;
 
-    // When false, the metrics logging is not yet ready to be enabled.
+    /**
+     * When false, the metrics logging is not yet ready to be enabled.
+     */
     public static final boolean IS_METRICS_LOGGING_SUPPORTED = false;
 }
-- 
GitLab