summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Joanne Chung <joannechung@google.com> 2020-10-28 22:10:01 +0800
committer Joanne Chung <joannechung@google.com> 2021-03-02 14:13:48 +0800
commita6d3ac4721bef235f5de7f7cc16842362849583d (patch)
tree637f4c89f124897b89f019237ec8cc4bd1fd9cbd
parentb8737dac2c10558a05a8df5e6882b110136d72f9 (diff)
Make SelectionActionModeHelper hardcoded constants configurable.
Read the selection timeout value from ViewConfiguration, ViewConfiguration is statically configurable by the OEM. OEMs can set the value which works on their device before shipping it. After privacy sync, we can make the selection context size configurable by DeviceConfig but we need to provide a reasonable upper bound. The value is 240 after experiment. Bug: 169042855 Test: atest CtsTextClassifierTestCases Test: atest CtsWidgetTestCases:TextViewTest Test: atest android.widget.TextViewActivityTest Test: manual. Changes values by command and checks the values. Change-Id: I59346fdc3b482bca586c13ba6c653d1e9b593922
-rw-r--r--core/java/android/view/ViewConfiguration.java39
-rw-r--r--core/java/android/view/textclassifier/TextClassificationConstants.java14
-rw-r--r--core/java/android/widget/SelectionActionModeHelper.java26
-rw-r--r--core/res/res/values/config.xml8
-rw-r--r--core/res/res/values/symbols.xml2
5 files changed, 79 insertions, 10 deletions
diff --git a/core/java/android/view/ViewConfiguration.java b/core/java/android/view/ViewConfiguration.java
index c2f17c310363..ec23a29b2070 100644
--- a/core/java/android/view/ViewConfiguration.java
+++ b/core/java/android/view/ViewConfiguration.java
@@ -310,6 +310,18 @@ public class ViewConfiguration {
*/
private static final float AMBIGUOUS_GESTURE_MULTIPLIER = 2f;
+ /**
+ * The timeout value in milliseconds to adjust the selection span and actions for the selected
+ * text when TextClassifier has been initialized.
+ */
+ private static final int SMART_SELECTION_INITIALIZED_TIMEOUT_IN_MILLISECOND = 200;
+
+ /**
+ * The timeout value in milliseconds to adjust the selection span and actions for the selected
+ * text when TextClassifier has not been initialized.
+ */
+ private static final int SMART_SELECTION_INITIALIZING_TIMEOUT_IN_MILLISECOND = 500;
+
private final boolean mConstructedWithContext;
private final int mEdgeSlop;
private final int mFadingEdgeLength;
@@ -335,6 +347,8 @@ public class ViewConfiguration {
private final float mHorizontalScrollFactor;
private final boolean mShowMenuShortcutsWhenKeyboardPresent;
private final long mScreenshotChordKeyTimeout;
+ private final int mSmartSelectionInitializedTimeout;
+ private final int mSmartSelectionInitializingTimeout;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123768915)
private boolean sHasPermanentMenuKey;
@@ -378,6 +392,8 @@ public class ViewConfiguration {
// Getter throws if mConstructedWithContext is false so doesn't matter what
// this value is.
mMinScalingSpan = 0;
+ mSmartSelectionInitializedTimeout = SMART_SELECTION_INITIALIZED_TIMEOUT_IN_MILLISECOND;
+ mSmartSelectionInitializingTimeout = SMART_SELECTION_INITIALIZING_TIMEOUT_IN_MILLISECOND;
}
/**
@@ -488,6 +504,11 @@ public class ViewConfiguration {
mScreenshotChordKeyTimeout = res.getInteger(
com.android.internal.R.integer.config_screenshotChordKeyTimeout);
+
+ mSmartSelectionInitializedTimeout = res.getInteger(
+ com.android.internal.R.integer.config_smartSelectionInitializedTimeoutMillis);
+ mSmartSelectionInitializingTimeout = res.getInteger(
+ com.android.internal.R.integer.config_smartSelectionInitializingTimeoutMillis);
}
/**
@@ -1069,6 +1090,24 @@ public class ViewConfiguration {
}
/**
+ * @return the timeout value in milliseconds to adjust the selection span and actions for the
+ * selected text when TextClassifier has been initialized.
+ * @hide
+ */
+ public int getSmartSelectionInitializedTimeout() {
+ return mSmartSelectionInitializedTimeout;
+ }
+
+ /**
+ * @return the timeout value in milliseconds to adjust the selection span and actions for the
+ * selected text when TextClassifier has not been initialized.
+ * @hide
+ */
+ public int getSmartSelectionInitializingTimeout() {
+ return mSmartSelectionInitializingTimeout;
+ }
+
+ /**
* @return the duration in milliseconds before an end of a long press causes a tooltip to be
* hidden
* @hide
diff --git a/core/java/android/view/textclassifier/TextClassificationConstants.java b/core/java/android/view/textclassifier/TextClassificationConstants.java
index 2975afc084e6..d0959f97e438 100644
--- a/core/java/android/view/textclassifier/TextClassificationConstants.java
+++ b/core/java/android/view/textclassifier/TextClassificationConstants.java
@@ -90,6 +90,12 @@ public final class TextClassificationConstants {
static final String SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND =
"system_textclassifier_api_timeout_in_second";
+ /**
+ * The max amount of characters before and after the selected text that are passed to the
+ * TextClassifier for the smart selection.
+ */
+ private static final String SMART_SELECTION_TRIM_DELTA = "smart_selection_trim_delta";
+
private static final String DEFAULT_TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE = null;
private static final boolean LOCAL_TEXT_CLASSIFIER_ENABLED_DEFAULT = true;
private static final boolean SYSTEM_TEXT_CLASSIFIER_ENABLED_DEFAULT = true;
@@ -100,6 +106,7 @@ public final class TextClassificationConstants {
private static final boolean SMART_SELECT_ANIMATION_ENABLED_DEFAULT = true;
private static final int GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT = 100 * 1000;
private static final long SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND_DEFAULT = 60;
+ private static final int SMART_SELECTION_TRIM_DELTA_DEFAULT = 120;
@Nullable
public String getTextClassifierServicePackageOverride() {
@@ -155,6 +162,12 @@ public final class TextClassificationConstants {
SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND_DEFAULT);
}
+ public int getSmartSelectionTrimDelta() {
+ return DeviceConfig.getInt(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
+ SMART_SELECTION_TRIM_DELTA,
+ SMART_SELECTION_TRIM_DELTA_DEFAULT);
+ }
+
void dump(IndentingPrintWriter pw) {
pw.println("TextClassificationConstants:");
pw.increaseIndent();
@@ -170,6 +183,7 @@ public final class TextClassificationConstants {
getTextClassifierServicePackageOverride()).println();
pw.print(SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND,
getSystemTextClassifierApiTimeoutInSecond()).println();
+ pw.print(SMART_SELECTION_TRIM_DELTA, getSmartSelectionTrimDelta()).println();
pw.decreaseIndent();
}
} \ No newline at end of file
diff --git a/core/java/android/widget/SelectionActionModeHelper.java b/core/java/android/widget/SelectionActionModeHelper.java
index 6f189204434a..eb6bce4a2f59 100644
--- a/core/java/android/widget/SelectionActionModeHelper.java
+++ b/core/java/android/widget/SelectionActionModeHelper.java
@@ -36,6 +36,7 @@ import android.text.TextUtils;
import android.text.util.Linkify;
import android.util.Log;
import android.view.ActionMode;
+import android.view.ViewConfiguration;
import android.view.textclassifier.ExtrasUtils;
import android.view.textclassifier.SelectionEvent;
import android.view.textclassifier.SelectionEvent.InvocationMethod;
@@ -1056,10 +1057,12 @@ public final class SelectionActionModeHelper {
*/
private static final class TextClassificationHelper {
- private static final int TRIM_DELTA = 120; // characters
+ // The fixed upper bound of context size.
+ private static final int TRIM_DELTA_UPPER_BOUND = 240;
private final Context mContext;
private Supplier<TextClassifier> mTextClassifier;
+ private final ViewConfiguration mViewConfiguration;
/** The original TextView text. **/
private String mText;
@@ -1088,12 +1091,13 @@ public final class SelectionActionModeHelper {
private SelectionResult mLastClassificationResult;
/** Whether the TextClassifier has been initialized. */
- private boolean mHot;
+ private boolean mInitialized;
TextClassificationHelper(Context context, Supplier<TextClassifier> textClassifier,
CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) {
init(textClassifier, text, selectionStart, selectionEnd, locales);
mContext = Objects.requireNonNull(context);
+ mViewConfiguration = ViewConfiguration.get(mContext);
}
@UiThread
@@ -1110,13 +1114,13 @@ public final class SelectionActionModeHelper {
@WorkerThread
public SelectionResult classifyText() {
- mHot = true;
+ mInitialized = true;
return performClassification(null /* selection */);
}
@WorkerThread
public SelectionResult suggestSelection() {
- mHot = true;
+ mInitialized = true;
trimText();
final TextSelection selection;
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
@@ -1148,16 +1152,15 @@ public final class SelectionActionModeHelper {
/**
* Maximum time (in milliseconds) to wait for a textclassifier result before timing out.
*/
- // TODO: Consider making this a ViewConfiguration.
public int getTimeoutDuration() {
- if (mHot) {
- return 200;
+ if (mInitialized) {
+ return mViewConfiguration.getSmartSelectionInitializedTimeout();
} else {
// Return a slightly larger number than usual when the TextClassifier is first
// initialized. Initialization would usually take longer than subsequent calls to
// the TextClassifier. The impact of this on the UI is that we do not show the
// selection handles or toolbar until after this timeout.
- return 500;
+ return mViewConfiguration.getSmartSelectionInitializingTimeout();
}
}
@@ -1205,8 +1208,11 @@ public final class SelectionActionModeHelper {
}
private void trimText() {
- mTrimStart = Math.max(0, mSelectionStart - TRIM_DELTA);
- final int referenceEnd = Math.min(mText.length(), mSelectionEnd + TRIM_DELTA);
+ final int trimDelta = Math.min(
+ TextClassificationManager.getSettings(mContext).getSmartSelectionTrimDelta(),
+ TRIM_DELTA_UPPER_BOUND);
+ mTrimStart = Math.max(0, mSelectionStart - trimDelta);
+ final int referenceEnd = Math.min(mText.length(), mSelectionEnd + trimDelta);
mTrimmedText = mText.subSequence(mTrimStart, referenceEnd);
mRelativeStart = mSelectionStart - mTrimStart;
mRelativeEnd = mSelectionEnd - mTrimStart;
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index faa2157be302..84476ef3c6f7 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -4682,6 +4682,14 @@
<!-- If true, hide the display cutout with display area -->
<bool name="config_hideDisplayCutoutWithDisplayArea">false</bool>
+ <!-- The timeout value in milliseconds used by SelectionActionModeHelper for each selections
+ when TextClassifier has been initialized. -->
+ <integer name="config_smartSelectionInitializedTimeoutMillis">200</integer>
+
+ <!-- The timeout value in milliseconds used by SelectionActionModeHelper for each selections
+ when TextClassifier has not been initialized. -->
+ <integer name="config_smartSelectionInitializingTimeoutMillis">500</integer>
+
<!-- Indicates that default fitness tracker app needs to request sensor and location permissions. -->
<bool name="config_trackerAppNeedsPermissions">false</bool>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index f66d33b02b69..0e0994ecf4b9 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -481,6 +481,8 @@
<java-symbol type="array" name="config_integrityRuleProviderPackages" />
<java-symbol type="bool" name="config_useAssistantVolume" />
<java-symbol type="string" name="config_bandwidthEstimateSource" />
+ <java-symbol type="integer" name="config_smartSelectionInitializedTimeoutMillis" />
+ <java-symbol type="integer" name="config_smartSelectionInitializingTimeoutMillis" />
<java-symbol type="color" name="tab_indicator_text_v4" />