From ac919b0e8358ce2edafb47f5644302f8617b7f3e Mon Sep 17 00:00:00 2001 From: Nikita Dubrovsky Date: Tue, 18 Feb 2020 09:39:20 -0800 Subject: [PATCH 1/2] Use DeviceConfig flags for cursor drag and insertion handle gestures Bug: 149751656 Test: Manually and ran automated tests atest FrameworksCoreTests:TextViewActivityTest atest FrameworksCoreTests:EditorCursorDragTest Change-Id: I431dd7c13b573d7acd9ce20e16a6476370202b5f --- core/java/android/widget/Editor.java | 46 +++++++++------ core/java/android/widget/WidgetFlags.java | 27 ++++++--- .../android/widget/EditorCursorDragTest.java | 9 --- .../android/widget/TextViewActivityTest.java | 58 +++++-------------- .../server/am/CoreSettingsObserver.java | 7 ++- 5 files changed, 68 insertions(+), 79 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 12f245e87da20..e1af3ec7e6589 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -151,9 +151,6 @@ public class Editor { private static final String TAG = "Editor"; private static final boolean DEBUG_UNDO = false; - // Specifies whether to allow starting a cursor drag by dragging anywhere over the text. - @VisibleForTesting - public static boolean FLAG_ENABLE_CURSOR_DRAG = true; // Specifies whether to use the magnifier when pressing the insertion or selection handles. private static final boolean FLAG_USE_MAGNIFIER = true; @@ -387,9 +384,8 @@ public class Editor { private final SuggestionHelper mSuggestionHelper = new SuggestionHelper(); - // Specifies whether the cursor control feature set is enabled. - // This can only be true if the text view is editable. - private boolean mCursorControlEnabled; + private boolean mFlagCursorDragFromAnywhereEnabled; + private boolean mFlagInsertionHandleGesturesEnabled; // Specifies whether the new magnifier (with fish-eye effect) is enabled. private final boolean mNewMagnifierEnabled; @@ -402,26 +398,40 @@ public class Editor { mHapticTextHandleEnabled = mTextView.getContext().getResources().getBoolean( com.android.internal.R.bool.config_enableHapticTextHandle); - mCursorControlEnabled = AppGlobals.getIntCoreSetting( - WidgetFlags.KEY_ENABLE_CURSOR_CONTROL , 0) != 0; + mFlagCursorDragFromAnywhereEnabled = AppGlobals.getIntCoreSetting( + WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE , 1) != 0; + mFlagInsertionHandleGesturesEnabled = AppGlobals.getIntCoreSetting( + WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES , 0) != 0; mNewMagnifierEnabled = AppGlobals.getIntCoreSetting( WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, 0) != 0; if (TextView.DEBUG_CURSOR) { - logCursor("Editor", "Cursor control is %s.", - mCursorControlEnabled ? "enabled" : "disabled"); + logCursor("Editor", "Cursor drag from anywhere is %s.", + mFlagCursorDragFromAnywhereEnabled ? "enabled" : "disabled"); + logCursor("Editor", "Insertion handle gestures is %s.", + mFlagInsertionHandleGesturesEnabled ? "enabled" : "disabled"); logCursor("Editor", "New magnifier is %s.", mNewMagnifierEnabled ? "enabled" : "disabled"); } } @VisibleForTesting - public void setCursorControlEnabled(boolean enabled) { - mCursorControlEnabled = enabled; + public boolean getFlagCursorDragFromAnywhereEnabled() { + return mFlagCursorDragFromAnywhereEnabled; } @VisibleForTesting - public boolean getCursorControlEnabled() { - return mCursorControlEnabled; + public void setFlagCursorDragFromAnywhereEnabled(boolean enabled) { + mFlagCursorDragFromAnywhereEnabled = enabled; + } + + @VisibleForTesting + public boolean getFlagInsertionHandleGesturesEnabled() { + return mFlagInsertionHandleGesturesEnabled; + } + + @VisibleForTesting + public void setFlagInsertionHandleGesturesEnabled(boolean enabled) { + mFlagInsertionHandleGesturesEnabled = enabled; } // Lazy creates the magnifier animator. @@ -5259,7 +5269,7 @@ public class Editor { int deltaHeight = 0; int opacity = 255; - if (mCursorControlEnabled) { + if (mFlagInsertionHandleGesturesEnabled) { deltaHeight = AppGlobals.getIntCoreSetting( WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, 25); opacity = AppGlobals.getIntCoreSetting( @@ -5329,7 +5339,7 @@ public class Editor { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - if (mCursorControlEnabled) { + if (mFlagInsertionHandleGesturesEnabled) { final int height = Math.max( getPreferredHeight() + mDeltaHeight, mDrawable.getIntrinsicHeight()); setMeasuredDimension(getPreferredWidth(), height); @@ -5340,7 +5350,7 @@ public class Editor { @Override public boolean onTouchEvent(MotionEvent ev) { - if (mCursorControlEnabled && FLAG_ENABLE_CURSOR_DRAG) { + if (mFlagInsertionHandleGesturesEnabled && mFlagCursorDragFromAnywhereEnabled) { // Should only enable touch through when cursor drag is enabled. // Otherwise the insertion handle view cannot be moved. return touchThrough(ev); @@ -6025,7 +6035,7 @@ public class Editor { } if (mIsDraggingCursor) { performCursorDrag(event); - } else if (FLAG_ENABLE_CURSOR_DRAG + } else if (mFlagCursorDragFromAnywhereEnabled && mTextView.getLayout() != null && mTextView.isFocused() && mTouchState.isMovedEnoughForDrag() diff --git a/core/java/android/widget/WidgetFlags.java b/core/java/android/widget/WidgetFlags.java index 1a8e7a713e7a9..4305ddf873b55 100644 --- a/core/java/android/widget/WidgetFlags.java +++ b/core/java/android/widget/WidgetFlags.java @@ -17,23 +17,36 @@ package android.widget; /** - * Keeps the flags related to the Widget namespace in {@link DeviceConfig}. + * Flags in the {@link android.provider.DeviceConfig#NAMESPACE_WIDGET "widget" namespace}. * * @hide */ public final class WidgetFlags { /** - * Whether the cursor control feature set is enabled. - * TODO: Makes this flag key visible to webview/chrome. + * Whether starting a cursor drag from anywhere in the text should be enabled. */ - public static final String ENABLE_CURSOR_CONTROL = - "CursorControlFeature__enable_cursor_control"; + public static final String ENABLE_CURSOR_DRAG_FROM_ANYWHERE = + "CursorControlFeature__enable_cursor_drag_from_anywhere"; /** - * The key name used in app core settings for enable cursor control. + * The key used in app core settings for the flag {@link #ENABLE_CURSOR_DRAG_FROM_ANYWHERE}. */ - public static final String KEY_ENABLE_CURSOR_CONTROL = "widget__enable_cursor_control"; + public static final String KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE = + "widget__enable_cursor_drag_from_anywhere"; + + /** + * Whether additional gestures should be enabled for the insertion cursor handle (e.g. + * long-press or double-tap on the handle to trigger selection). + */ + public static final String ENABLE_INSERTION_HANDLE_GESTURES = + "CursorControlFeature__enable_insertion_handle_gestures"; + + /** + * The key used in app core settings for the flag {@link #ENABLE_INSERTION_HANDLE_GESTURES}. + */ + public static final String KEY_ENABLE_INSERTION_HANDLE_GESTURES = + "widget__enable_insertion_handle_gestures"; /** * The flag of delta height applies to the insertion handle when cursor control flag is enabled. diff --git a/core/tests/coretests/src/android/widget/EditorCursorDragTest.java b/core/tests/coretests/src/android/widget/EditorCursorDragTest.java index a602fa31281f6..82f589c012892 100644 --- a/core/tests/coretests/src/android/widget/EditorCursorDragTest.java +++ b/core/tests/coretests/src/android/widget/EditorCursorDragTest.java @@ -44,7 +44,6 @@ import com.android.frameworks.coretests.R; import com.google.common.base.Strings; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -57,7 +56,6 @@ public class EditorCursorDragTest { public ActivityTestRule mActivityRule = new ActivityTestRule<>( TextViewActivity.class); - private boolean mOriginalFlagValue; private Instrumentation mInstrumentation; private Activity mActivity; @@ -65,13 +63,6 @@ public class EditorCursorDragTest { public void before() throws Throwable { mInstrumentation = InstrumentationRegistry.getInstrumentation(); mActivity = mActivityRule.getActivity(); - mOriginalFlagValue = Editor.FLAG_ENABLE_CURSOR_DRAG; - Editor.FLAG_ENABLE_CURSOR_DRAG = true; - } - - @After - public void after() throws Throwable { - Editor.FLAG_ENABLE_CURSOR_DRAG = mOriginalFlagValue; } @Test diff --git a/core/tests/coretests/src/android/widget/TextViewActivityTest.java b/core/tests/coretests/src/android/widget/TextViewActivityTest.java index 0c38e7136655c..88a6f9e4af4b0 100644 --- a/core/tests/coretests/src/android/widget/TextViewActivityTest.java +++ b/core/tests/coretests/src/android/widget/TextViewActivityTest.java @@ -31,8 +31,8 @@ import static android.widget.espresso.TextViewActions.doubleTapAndDragHandle; import static android.widget.espresso.TextViewActions.doubleTapAndDragOnText; import static android.widget.espresso.TextViewActions.doubleTapHandle; import static android.widget.espresso.TextViewActions.dragHandle; -import static android.widget.espresso.TextViewActions.longPressAndDragOnText; import static android.widget.espresso.TextViewActions.longPressAndDragHandle; +import static android.widget.espresso.TextViewActions.longPressAndDragOnText; import static android.widget.espresso.TextViewActions.longPressHandle; import static android.widget.espresso.TextViewActions.longPressOnTextAtIndex; import static android.widget.espresso.TextViewAssertions.doesNotHaveStyledText; @@ -514,29 +514,26 @@ public class TextViewActivityTest { onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(text.indexOf("f"))); } + private void enableFlagsForInsertionHandleGestures() { + final TextView textView = mActivity.findViewById(R.id.textview); + final Editor editor = textView.getEditorForTesting(); + editor.setFlagCursorDragFromAnywhereEnabled(true); + editor.setFlagInsertionHandleGesturesEnabled(true); + // Note: We don't need to reset these flags explicitly at the end of each test, because a + // fresh TextView and Editor will be created for each test. + } + @Test public void testInsertionHandle_touchThrough() { - final TextView textView = mActivity.findViewById(R.id.textview); - boolean cursorControlEnabled = textView.getEditorForTesting().getCursorControlEnabled(); - boolean cursorDragEnabled = Editor.FLAG_ENABLE_CURSOR_DRAG; - textView.getEditorForTesting().setCursorControlEnabled(true); - Editor.FLAG_ENABLE_CURSOR_DRAG = true; - + enableFlagsForInsertionHandleGestures(); testInsertionHandle(); testInsertionHandle_multiLine(); - - textView.getEditorForTesting().setCursorControlEnabled(cursorControlEnabled); - Editor.FLAG_ENABLE_CURSOR_DRAG = cursorDragEnabled; } @Test public void testInsertionHandle_longPressToSelect() { - // This test only makes sense when Cursor Control flag is enabled. + enableFlagsForInsertionHandleGestures(); final TextView textView = mActivity.findViewById(R.id.textview); - boolean cursorControlEnabled = textView.getEditorForTesting().getCursorControlEnabled(); - boolean cursorDragEnabled = Editor.FLAG_ENABLE_CURSOR_DRAG; - textView.getEditorForTesting().setCursorControlEnabled(true); - Editor.FLAG_ENABLE_CURSOR_DRAG = true; final String text = "hello the world"; onView(withId(R.id.textview)).perform(replaceText(text)); @@ -546,20 +543,12 @@ public class TextViewActivityTest { onHandleView(com.android.internal.R.id.insertion_handle).perform(longPressHandle(textView)); onView(withId(R.id.textview)).check(hasSelection("world")); - - textView.getEditorForTesting().setCursorControlEnabled(cursorControlEnabled); - Editor.FLAG_ENABLE_CURSOR_DRAG = cursorDragEnabled; } @Test public void testInsertionHandle_longPressAndDragToSelect() { - // This test only makes sense when Cursor Control flag is enabled. + enableFlagsForInsertionHandleGestures(); final TextView textView = mActivity.findViewById(R.id.textview); - boolean cursorControlEnabled = textView.getEditorForTesting().getCursorControlEnabled(); - boolean cursorDragEnabled = Editor.FLAG_ENABLE_CURSOR_DRAG; - textView.getEditorForTesting().setCursorControlEnabled(true); - Editor.FLAG_ENABLE_CURSOR_DRAG = true; - final String text = "hello the world"; onView(withId(R.id.textview)).perform(replaceText(text)); @@ -569,19 +558,12 @@ public class TextViewActivityTest { onHandleView(com.android.internal.R.id.insertion_handle) .perform(longPressAndDragHandle(textView, Handle.INSERTION, text.indexOf('t'))); onView(withId(R.id.textview)).check(hasSelection("the world")); - - textView.getEditorForTesting().setCursorControlEnabled(cursorControlEnabled); - Editor.FLAG_ENABLE_CURSOR_DRAG = cursorDragEnabled; } @Test public void testInsertionHandle_doubleTapToSelect() { - // This test only makes sense when Cursor Control flag is enabled. + enableFlagsForInsertionHandleGestures(); final TextView textView = mActivity.findViewById(R.id.textview); - boolean cursorControlEnabled = textView.getEditorForTesting().getCursorControlEnabled(); - boolean cursorDragEnabled = Editor.FLAG_ENABLE_CURSOR_DRAG; - textView.getEditorForTesting().setCursorControlEnabled(true); - Editor.FLAG_ENABLE_CURSOR_DRAG = true; final String text = "hello the world"; onView(withId(R.id.textview)).perform(replaceText(text)); @@ -591,19 +573,12 @@ public class TextViewActivityTest { onHandleView(com.android.internal.R.id.insertion_handle).perform(doubleTapHandle(textView)); onView(withId(R.id.textview)).check(hasSelection("world")); - - textView.getEditorForTesting().setCursorControlEnabled(cursorControlEnabled); - Editor.FLAG_ENABLE_CURSOR_DRAG = cursorDragEnabled; } @Test public void testInsertionHandle_doubleTapAndDragToSelect() { - // This test only makes sense when Cursor Control flag is enabled. + enableFlagsForInsertionHandleGestures(); final TextView textView = mActivity.findViewById(R.id.textview); - boolean cursorControlEnabled = textView.getEditorForTesting().getCursorControlEnabled(); - boolean cursorDragEnabled = Editor.FLAG_ENABLE_CURSOR_DRAG; - textView.getEditorForTesting().setCursorControlEnabled(true); - Editor.FLAG_ENABLE_CURSOR_DRAG = true; final String text = "hello the world"; onView(withId(R.id.textview)).perform(replaceText(text)); @@ -614,9 +589,6 @@ public class TextViewActivityTest { onHandleView(com.android.internal.R.id.insertion_handle) .perform(doubleTapAndDragHandle(textView, Handle.INSERTION, text.indexOf('t'))); onView(withId(R.id.textview)).check(hasSelection("the world")); - - textView.getEditorForTesting().setCursorControlEnabled(cursorControlEnabled); - Editor.FLAG_ENABLE_CURSOR_DRAG = cursorDragEnabled; } @Test diff --git a/services/core/java/com/android/server/am/CoreSettingsObserver.java b/services/core/java/com/android/server/am/CoreSettingsObserver.java index a03f0bb4e3999..4a2353cd19135 100644 --- a/services/core/java/com/android/server/am/CoreSettingsObserver.java +++ b/services/core/java/com/android/server/am/CoreSettingsObserver.java @@ -106,8 +106,11 @@ final class CoreSettingsObserver extends ContentObserver { // add other global settings here... sDeviceConfigEntries.add(new DeviceConfigEntry( - DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_CONTROL, - WidgetFlags.KEY_ENABLE_CURSOR_CONTROL, boolean.class)); + DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE, + WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class)); + sDeviceConfigEntries.add(new DeviceConfigEntry( + DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES, + WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class)); sDeviceConfigEntries.add(new DeviceConfigEntry( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT, WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class)); From 9e139179d74d549b98a5cd96587dffa1e1c7ba15 Mon Sep 17 00:00:00 2001 From: Nikita Dubrovsky Date: Tue, 18 Feb 2020 16:23:17 -0800 Subject: [PATCH 2/2] Allow setting defaults for DeviceConfig flags plumbed via settings Bug: 149790259 Test: Manually Change-Id: Ie456cf27edab204dfc028023c6364a11b7e5de48 --- core/java/android/widget/Editor.java | 21 ++++-- core/java/android/widget/WidgetFlags.java | 40 ++++++++++-- .../server/am/CoreSettingsObserver.java | 65 ++++++++++++------- 3 files changed, 92 insertions(+), 34 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index e1af3ec7e6589..9d3e15f4c0a7e 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -399,11 +399,14 @@ public class Editor { 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 @@ public class Editor { // 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 @@ public class Editor { 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 4305ddf873b55..bce5497a7c2de 100644 --- a/core/java/android/widget/WidgetFlags.java +++ b/core/java/android/widget/WidgetFlags.java @@ -35,6 +35,11 @@ public final class WidgetFlags { public static final String KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE = "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). @@ -48,9 +53,13 @@ public final class WidgetFlags { public static final String KEY_ENABLE_INSERTION_HANDLE_GESTURES = "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"; @@ -61,9 +70,14 @@ public final class WidgetFlags { public static final String KEY_INSERTION_HANDLE_DELTA_HEIGHT = "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"; @@ -74,6 +88,11 @@ public final class WidgetFlags { public static final String KEY_INSERTION_HANDLE_OPACITY = "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. */ @@ -84,9 +103,13 @@ public final class WidgetFlags { */ 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"; @@ -96,9 +119,13 @@ public final class WidgetFlags { */ 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 final class WidgetFlags { */ 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 4a2353cd19135..48ceba976502a 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.HashMap; 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 @@ import java.util.Map; final class CoreSettingsObserver extends ContentObserver { private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName(); - private static class DeviceConfigEntry { + private static class DeviceConfigEntry { String namespace; String flag; String coreSettingKey; - Class type; - DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class type) { + Class type; + T defaultValue; + + DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class 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 @@ final class CoreSettingsObserver extends ContentObserver { sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, String.class); // add other global settings here... - sDeviceConfigEntries.add(new DeviceConfigEntry( + sDeviceConfigEntries.add(new DeviceConfigEntry( 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( 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( 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( 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( 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( 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( 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 @@ final class CoreSettingsObserver extends ContentObserver { } } + @SuppressWarnings("unchecked") private void populateSettingsFromDeviceConfig() { - for (DeviceConfigEntry entry : sDeviceConfigEntries) { + for (DeviceConfigEntry entry : sDeviceConfigEntries) { if (entry.type == String.class) { + String defaultValue = ((DeviceConfigEntry) 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) 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) 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) 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) entry).defaultValue; mCoreSettings.putInt(entry.coreSettingKey, - DeviceConfig.getBoolean(entry.namespace, entry.flag, false) ? 1 : 0); + DeviceConfig.getBoolean(entry.namespace, entry.flag, defaultValue) ? 1 : 0); } } }