Allow setting defaults for DeviceConfig flags plumbed via settings

Bug: 149790259
Test: Manually
Change-Id: Ie456cf27edab204dfc028023c6364a11b7e5de48
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index e1af3ec7..9d3e15f 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -399,11 +399,14 @@
                 com.android.internal.R.bool.config_enableHapticTextHandle);
 
         mFlagCursorDragFromAnywhereEnabled = AppGlobals.getIntCoreSetting(
-                WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE , 1) != 0;
+                WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE,
+                WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT ? 1 : 0) != 0;
         mFlagInsertionHandleGesturesEnabled = AppGlobals.getIntCoreSetting(
-                WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES , 0) != 0;
+                WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES,
+                WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT ? 1 : 0) != 0;
         mNewMagnifierEnabled = AppGlobals.getIntCoreSetting(
-                WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, 0) != 0;
+                WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER,
+                WidgetFlags.ENABLE_NEW_MAGNIFIER_DEFAULT ? 1 : 0) != 0;
         if (TextView.DEBUG_CURSOR) {
             logCursor("Editor", "Cursor drag from anywhere is %s.",
                     mFlagCursorDragFromAnywhereEnabled ? "enabled" : "disabled");
@@ -453,9 +456,11 @@
         // TODO: supports changing the height/width dynamically because the text height can be
         // dynamically changed.
         float zoom = AppGlobals.getFloatCoreSetting(
-                WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, 1.5f);
+                WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR,
+                WidgetFlags.MAGNIFIER_ZOOM_FACTOR_DEFAULT);
         float aspectRatio = AppGlobals.getFloatCoreSetting(
-                WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, 5.5f);
+                WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO,
+                WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT);
         // Avoid invalid/unsupported values.
         if (zoom < 1.2f || zoom > 1.8f) {
             zoom = 1.5f;
@@ -5271,9 +5276,11 @@
             int opacity = 255;
             if (mFlagInsertionHandleGesturesEnabled) {
                 deltaHeight = AppGlobals.getIntCoreSetting(
-                        WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, 25);
+                        WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT,
+                        WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT_DEFAULT);
                 opacity = AppGlobals.getIntCoreSetting(
-                        WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, 50);
+                        WidgetFlags.KEY_INSERTION_HANDLE_OPACITY,
+                        WidgetFlags.INSERTION_HANDLE_OPACITY_DEFAULT);
                 // Avoid invalid/unsupported values.
                 if (deltaHeight < -25 || deltaHeight > 50) {
                     deltaHeight = 25;
diff --git a/core/java/android/widget/WidgetFlags.java b/core/java/android/widget/WidgetFlags.java
index 4305ddf..bce5497 100644
--- a/core/java/android/widget/WidgetFlags.java
+++ b/core/java/android/widget/WidgetFlags.java
@@ -36,6 +36,11 @@
             "widget__enable_cursor_drag_from_anywhere";
 
     /**
+     * Default value for the flag {@link #ENABLE_CURSOR_DRAG_FROM_ANYWHERE}.
+     */
+    public static final boolean ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT = true;
+
+    /**
      * Whether additional gestures should be enabled for the insertion cursor handle (e.g.
      * long-press or double-tap on the handle to trigger selection).
      */
@@ -49,8 +54,12 @@
             "widget__enable_insertion_handle_gestures";
 
     /**
+     * Default value for the flag {@link #ENABLE_INSERTION_HANDLE_GESTURES}.
+     */
+    public static final boolean ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT = false;
+
+    /**
      * The flag of delta height applies to the insertion handle when cursor control flag is enabled.
-     * The default value is 25.
      */
     public static final String INSERTION_HANDLE_DELTA_HEIGHT =
             "CursorControlFeature__insertion_handle_delta_height";
@@ -62,8 +71,13 @@
             "widget__insertion_handle_delta_height";
 
     /**
+     * Default value for the flag {@link #INSERTION_HANDLE_DELTA_HEIGHT}.
+     */
+    public static final int INSERTION_HANDLE_DELTA_HEIGHT_DEFAULT = 25;
+
+    /**
      * The flag of opacity applies to the insertion handle when cursor control flag is enabled.
-     * The opacity value is in the range of {0..100}. The default value is 50.
+     * The opacity value is in the range of {0..100}.
      */
     public static final String INSERTION_HANDLE_OPACITY =
             "CursorControlFeature__insertion_handle_opacity";
@@ -75,6 +89,11 @@
             "widget__insertion_handle_opacity";
 
     /**
+     * Default value for the flag {@link #INSERTION_HANDLE_OPACITY}.
+     */
+    public static final int INSERTION_HANDLE_OPACITY_DEFAULT = 50;
+
+    /**
      * The flag of enabling the new magnifier.
      */
     public static final String ENABLE_NEW_MAGNIFIER = "CursorControlFeature__enable_new_magnifier";
@@ -85,8 +104,12 @@
     public static final String KEY_ENABLE_NEW_MAGNIFIER = "widget__enable_new_magnifier";
 
     /**
+     * Default value for the flag {@link #ENABLE_NEW_MAGNIFIER}.
+     */
+    public static final boolean ENABLE_NEW_MAGNIFIER_DEFAULT = false;
+
+    /**
      * The flag of zoom factor applies to the new magnifier.
-     * The default value is 1.5f.
      */
     public static final String MAGNIFIER_ZOOM_FACTOR =
             "CursorControlFeature__magnifier_zoom_factor";
@@ -97,8 +120,12 @@
     public static final String KEY_MAGNIFIER_ZOOM_FACTOR = "widget__magnifier_zoom_factor";
 
     /**
+     * Default value for the flag {@link #MAGNIFIER_ZOOM_FACTOR}.
+     */
+    public static final float MAGNIFIER_ZOOM_FACTOR_DEFAULT = 1.5f;
+
+    /**
      * The flag of aspect ratio (width/height) applies to the new magnifier.
-     * The default value is 5.5f.
      */
     public static final String MAGNIFIER_ASPECT_RATIO =
             "CursorControlFeature__magnifier_aspect_ratio";
@@ -108,6 +135,11 @@
      */
     public static final String KEY_MAGNIFIER_ASPECT_RATIO = "widget__magnifier_aspect_ratio";
 
+    /**
+     * Default value for the flag {@link #MAGNIFIER_ASPECT_RATIO}.
+     */
+    public static final float MAGNIFIER_ASPECT_RATIO_DEFAULT = 5.5f;
+
     private WidgetFlags() {
     }
 }
diff --git a/services/core/java/com/android/server/am/CoreSettingsObserver.java b/services/core/java/com/android/server/am/CoreSettingsObserver.java
index 4a2353c..48ceba9 100644
--- a/services/core/java/com/android/server/am/CoreSettingsObserver.java
+++ b/services/core/java/com/android/server/am/CoreSettingsObserver.java
@@ -16,6 +16,7 @@
 
 package com.android.server.am;
 
+import android.annotation.NonNull;
 import android.app.ActivityThread;
 import android.content.Context;
 import android.database.ContentObserver;
@@ -32,6 +33,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * Helper class for watching a set of core settings which the framework
@@ -42,16 +44,20 @@
 final class CoreSettingsObserver extends ContentObserver {
     private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();
 
-    private static class DeviceConfigEntry {
+    private static class DeviceConfigEntry<T> {
         String namespace;
         String flag;
         String coreSettingKey;
-        Class<?> type;
-        DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class<?> type) {
+        Class<T> type;
+        T defaultValue;
+
+        DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class<T> type,
+                @NonNull T defaultValue) {
             this.namespace = namespace;
             this.flag = flag;
             this.coreSettingKey = coreSettingKey;
             this.type = type;
+            this.defaultValue = Objects.requireNonNull(defaultValue);
         }
     }
 
@@ -105,27 +111,34 @@
         sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, String.class);
         // add other global settings here...
 
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE,
-                WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class,
+                WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES,
-                WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class,
+                WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT,
-                WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class,
+                WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_OPACITY,
-                WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, int.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, int.class,
+                WidgetFlags.INSERTION_HANDLE_OPACITY_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_NEW_MAGNIFIER,
-                WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, boolean.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, boolean.class,
+                WidgetFlags.ENABLE_NEW_MAGNIFIER_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Float>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ZOOM_FACTOR,
-                WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, float.class));
-        sDeviceConfigEntries.add(new DeviceConfigEntry(
+                WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, float.class,
+                WidgetFlags.MAGNIFIER_ZOOM_FACTOR_DEFAULT));
+        sDeviceConfigEntries.add(new DeviceConfigEntry<Float>(
                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ASPECT_RATIO,
-                WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, float.class));
+                WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, float.class,
+                WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT));
         // add other device configs here...
     }
 
@@ -219,23 +232,29 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
     private void populateSettingsFromDeviceConfig() {
-        for (DeviceConfigEntry entry : sDeviceConfigEntries) {
+        for (DeviceConfigEntry<?> entry : sDeviceConfigEntries) {
             if (entry.type == String.class) {
+                String defaultValue = ((DeviceConfigEntry<String>) entry).defaultValue;
                 mCoreSettings.putString(entry.coreSettingKey,
-                        DeviceConfig.getString(entry.namespace, entry.flag, ""));
+                        DeviceConfig.getString(entry.namespace, entry.flag, defaultValue));
             } else if (entry.type == int.class) {
+                int defaultValue = ((DeviceConfigEntry<Integer>) entry).defaultValue;
                 mCoreSettings.putInt(entry.coreSettingKey,
-                        DeviceConfig.getInt(entry.namespace, entry.flag, 0));
+                        DeviceConfig.getInt(entry.namespace, entry.flag, defaultValue));
             } else if (entry.type == float.class) {
+                float defaultValue = ((DeviceConfigEntry<Float>) entry).defaultValue;
                 mCoreSettings.putFloat(entry.coreSettingKey,
-                        DeviceConfig.getFloat(entry.namespace, entry.flag, 0));
+                        DeviceConfig.getFloat(entry.namespace, entry.flag, defaultValue));
             } else if (entry.type == long.class) {
+                long defaultValue = ((DeviceConfigEntry<Long>) entry).defaultValue;
                 mCoreSettings.putLong(entry.coreSettingKey,
-                        DeviceConfig.getLong(entry.namespace, entry.flag, 0));
+                        DeviceConfig.getLong(entry.namespace, entry.flag, defaultValue));
             } else if (entry.type == boolean.class) {
+                boolean defaultValue = ((DeviceConfigEntry<Boolean>) entry).defaultValue;
                 mCoreSettings.putInt(entry.coreSettingKey,
-                        DeviceConfig.getBoolean(entry.namespace, entry.flag, false) ? 1 : 0);
+                        DeviceConfig.getBoolean(entry.namespace, entry.flag, defaultValue) ? 1 : 0);
             }
         }
     }