Merge "Makes the finger-to-cursor distance configurable via DeviceConfig." into rvc-dev
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 816612f..16d9ed3 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -4589,7 +4589,7 @@
         private float mTouchOffsetY;
         // Where the touch position should be on the handle to ensure a maximum cursor visibility.
         // This is the distance in pixels from the top of the handle view.
-        private float mIdealVerticalOffset;
+        private final float mIdealVerticalOffset;
         // Parent's (TextView) previous position in window
         private int mLastParentX, mLastParentY;
         // Parent's (TextView) previous position on screen
@@ -4638,8 +4638,18 @@
 
             final int handleHeight = getPreferredHeight();
             mTouchOffsetY = -0.3f * handleHeight;
-            mIdealVerticalOffset = 0.7f * handleHeight;
-            mIdealFingerToCursorOffset = (int)(mIdealVerticalOffset - mTouchOffsetY);
+            final int distance = AppGlobals.getIntCoreSetting(
+                    WidgetFlags.KEY_FINGER_TO_CURSOR_DISTANCE,
+                    WidgetFlags.FINGER_TO_CURSOR_DISTANCE_DEFAULT);
+            if (distance < 0 || distance > 100) {
+                mIdealVerticalOffset = 0.7f * handleHeight;
+                mIdealFingerToCursorOffset = (int)(mIdealVerticalOffset - mTouchOffsetY);
+            } else {
+                mIdealFingerToCursorOffset = (int) TypedValue.applyDimension(
+                        TypedValue.COMPLEX_UNIT_DIP, distance,
+                        mTextView.getContext().getResources().getDisplayMetrics());
+                mIdealVerticalOffset = mIdealFingerToCursorOffset + mTouchOffsetY;
+            }
         }
 
         public float getIdealVerticalOffset() {
diff --git a/core/java/android/widget/WidgetFlags.java b/core/java/android/widget/WidgetFlags.java
index bce5497..09ab5aa 100644
--- a/core/java/android/widget/WidgetFlags.java
+++ b/core/java/android/widget/WidgetFlags.java
@@ -41,6 +41,25 @@
     public static final boolean ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT = true;
 
     /**
+     * The flag of finger-to-cursor distance in DP for cursor dragging.
+     * The value unit is DP and the range is {0..100}. If the value is out of range, the legacy
+     * value, which is based on handle size, will be used.
+     */
+    public static final String FINGER_TO_CURSOR_DISTANCE =
+            "CursorControlFeature__finger_to_cursor_distance";
+
+    /**
+     * The key used in app core settings for the flag {@link #FINGER_TO_CURSOR_DISTANCE}.
+     */
+    public static final String KEY_FINGER_TO_CURSOR_DISTANCE =
+            "widget__finger_to_cursor_distance";
+
+    /**
+     * Default value for the flag {@link #FINGER_TO_CURSOR_DISTANCE}.
+     */
+    public static final int FINGER_TO_CURSOR_DISTANCE_DEFAULT = -1;
+
+    /**
      * Whether additional gestures should be enabled for the insertion cursor handle (e.g.
      * long-press or double-tap on the handle to trigger selection).
      */
diff --git a/services/core/java/com/android/server/am/CoreSettingsObserver.java b/services/core/java/com/android/server/am/CoreSettingsObserver.java
index 48ceba9..8527ae9 100644
--- a/services/core/java/com/android/server/am/CoreSettingsObserver.java
+++ b/services/core/java/com/android/server/am/CoreSettingsObserver.java
@@ -115,6 +115,10 @@
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE,
                 WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class,
                 WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
+                DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.FINGER_TO_CURSOR_DISTANCE,
+                WidgetFlags.KEY_FINGER_TO_CURSOR_DISTANCE, int.class,
+                WidgetFlags.FINGER_TO_CURSOR_DISTANCE_DEFAULT));
         sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES,
                 WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class,