diff options
| author | Nikita Dubrovsky <dubrovsky@google.com> | 2020-10-28 16:46:07 -0700 |
|---|---|---|
| committer | Nikita Dubrovsky <dubrovsky@google.com> | 2020-11-12 14:19:57 -0800 |
| commit | a25346bf0cec67f9a1e8644bd0dff718adb73d36 (patch) | |
| tree | 0e70e24e79ac62bf238bf5e5d5e77f41b956da55 /core/java/android/widget/TextView.java | |
| parent | 8902c223aa675fe12b91084af9c0a8966377d30f (diff) | |
Update onReceiveContent() logic for app vs platform processing
Previously onReceiveContent() would only invoke the app-configured
callback if the MIME type of the content matched one of the declared
MIME types for the callback. This change updates onReceiveContent()
to always invoke the listener if one is set (regardless of the MIME
type of the content). To delegate processing to the platform, the
app's listener can return some or all of the passed-in content. To
make this easy for apps to implement, the Payload class and its
Builder now provide some convenience methods to conditionally
partition the content.
Reasons for this change:
* Checking the MIME types could be an expensive operation. On SDKs prior
to S, ClipData does not keep track of the MIME types of individual
items, so for a ClipData that contains multiple items, checking the MIME
types requires making at least one RPC call per item.
* Allowing the listener to delegate processing to the platform via its
return value enables us to limit the API surface (we don't need to
expose TextViewOnReceiveContentListener as a public API, nor equivalent
classes for other types of views such as WebView).
* An app that wants to customize the platform behavior for coercing
content to text would previously need to declare "*/*" as the MIME type
for the callback (in order to be invoked for all content). But this
would make it impossible for features to know whether the app would
actually accept a particular type of content or just coerce it to text
(e.g. should the soft keyboard show GIF suggestions when the declared
MIME type is "*/*"). With the new logic the app's listener is always
invoked and can decide which content to process vs delegate to the
platform vs reject completely.
Bug: 170191676
Bug: 152068298
Test: atest CtsViewTestCases:ViewOnReceiveContentTest
Test: atest CtsWidgetTestCases:TextViewOnReceiveContentTest
Test: atest FrameworksCoreTests:TextViewOnReceiveContentTest
Change-Id: Ie48b6fe0b2ae4b014c371b5dc40248221947c6bf
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 91 |
1 files changed, 27 insertions, 64 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 3ac78bafdedc..9485753ce906 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -17,10 +17,10 @@ package android.widget; import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; -import static android.view.OnReceiveContentCallback.Payload.FLAG_CONVERT_TO_PLAIN_TEXT; -import static android.view.OnReceiveContentCallback.Payload.SOURCE_AUTOFILL; -import static android.view.OnReceiveContentCallback.Payload.SOURCE_CLIPBOARD; -import static android.view.OnReceiveContentCallback.Payload.SOURCE_PROCESS_TEXT; +import static android.view.OnReceiveContentListener.Payload.FLAG_CONVERT_TO_PLAIN_TEXT; +import static android.view.OnReceiveContentListener.Payload.SOURCE_AUTOFILL; +import static android.view.OnReceiveContentListener.Payload.SOURCE_CLIPBOARD; +import static android.view.OnReceiveContentListener.Payload.SOURCE_PROCESS_TEXT; import static android.view.accessibility.AccessibilityNodeInfo.EXTRA_DATA_RENDERING_INFO_KEY; import static android.view.accessibility.AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_ARG_LENGTH; import static android.view.accessibility.AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_ARG_START_INDEX; @@ -154,7 +154,7 @@ import android.view.InputDevice; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.MotionEvent; -import android.view.OnReceiveContentCallback; +import android.view.OnReceiveContentListener.Payload; import android.view.PointerIcon; import android.view.View; import android.view.ViewConfiguration; @@ -2151,10 +2151,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (result != null) { if (isTextEditable()) { ClipData clip = ClipData.newPlainText("", result); - OnReceiveContentCallback.Payload payload = - new OnReceiveContentCallback.Payload.Builder( - clip, SOURCE_PROCESS_TEXT) - .build(); + Payload payload = new Payload.Builder(clip, SOURCE_PROCESS_TEXT).build(); onReceiveContent(payload); if (mEditor != null) { mEditor.refreshTextActionMode(); @@ -11858,8 +11855,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener + " cannot be autofilled into " + this); return; } - final OnReceiveContentCallback.Payload payload = - new OnReceiveContentCallback.Payload.Builder(clip, SOURCE_AUTOFILL).build(); + final Payload payload = new Payload.Builder(clip, SOURCE_AUTOFILL).build(); onReceiveContent(payload); } @@ -12926,8 +12922,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (clip == null) { return; } - final OnReceiveContentCallback.Payload payload = - new OnReceiveContentCallback.Payload.Builder(clip, SOURCE_CLIPBOARD) + final Payload payload = new Payload.Builder(clip, SOURCE_CLIPBOARD) .setFlags(withFormatting ? 0 : FLAG_CONVERT_TO_PLAIN_TEXT) .build(); onReceiveContent(payload); @@ -13717,7 +13712,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener public void onInputConnectionOpenedInternal(@NonNull InputConnection ic, @NonNull EditorInfo editorInfo, @Nullable Handler handler) { if (mEditor != null) { - mEditor.getDefaultOnReceiveContentCallback().setInputConnectionInfo(ic, editorInfo); + mEditor.getDefaultOnReceiveContentListener().setInputConnectionInfo(this, ic, + editorInfo); } } @@ -13725,68 +13721,35 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener @Override public void onInputConnectionClosedInternal() { if (mEditor != null) { - mEditor.getDefaultOnReceiveContentCallback().clearInputConnectionInfo(); + mEditor.getDefaultOnReceiveContentListener().clearInputConnectionInfo(); } } /** - * Sets the callback to handle insertion of content into this view. + * Receives the given content. Clients wishing to provide custom behavior should configure a + * listener via {@link #setOnReceiveContentListener}. * - * <p>This callback will be invoked for the following scenarios: - * <ol> - * <li>Paste from the clipboard (e.g. "Paste" or "Paste as plain text" action in the - * insertion/selection menu) - * <li>Content insertion from the keyboard (from {@link InputConnection#commitContent}) - * <li>Drag and drop (drop events from {@link #onDragEvent(DragEvent)}) - * <li>Autofill (from {@link #autofill(AutofillValue)}) - * <li>{@link Intent#ACTION_PROCESS_TEXT} replacement - * </ol> + * <p>If a listener is set, invokes the listener. If the listener returns a non-null result, + * executes the default platform handling for the portion of the content returned by the + * listener. * - * <p>This callback is only invoked for content whose MIME type matches a type specified via - * the {code mimeTypes} parameter. If the MIME type is not supported by the callback, the - * default platform handling will be executed instead (no-op for the default {@link View}). - * - * <p><em>Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC - * MIME types. As a result, you should always write your MIME types with lower case letters, or - * use {@link android.content.Intent#normalizeMimeType} to ensure that it is converted to lower - * case.</em> - * - * @param mimeTypes The type of content for which the callback should be invoked. This may use - * wildcards such as "text/*", "image/*", etc. This must not be null or empty if a non-null - * callback is passed in. - * @param callback The callback to use. This can be null to reset to the default behavior. - */ - @SuppressWarnings("rawtypes") - @Override - public void setOnReceiveContentCallback( - @Nullable String[] mimeTypes, - @Nullable OnReceiveContentCallback callback) { - super.setOnReceiveContentCallback(mimeTypes, callback); - } - - /** - * Receives the given content. The default implementation invokes the callback set via - * {@link #setOnReceiveContentCallback}. If no callback is set or if the callback does not - * support the given content (based on the MIME type), executes the default platform handling - * (e.g. coerces content to text if the source is - * {@link OnReceiveContentCallback.Payload#SOURCE_CLIPBOARD} and this is an editable - * {@link TextView}). + * <p>If no listener is set, executes the default platform behavior. For non-editable TextViews + * the default behavior is a no-op (returns the passed-in content without acting on it). For + * editable TextViews the default behavior coerces all content to text and inserts into the + * view. * * @param payload The content to insert and related metadata. * - * @return Returns true if the content was handled in some way, false otherwise. Actual - * insertion may be processed asynchronously in the background and may or may not succeed even - * if this method returns true. For example, an app may not end up inserting an item if it - * exceeds the app's size limit for that type of content. + * @return The portion of the passed-in content that was not handled (may be all, some, or none + * of the passed-in content). */ @Override - public boolean onReceiveContent(@NonNull OnReceiveContentCallback.Payload payload) { - if (super.onReceiveContent(payload)) { - return true; - } else if (mEditor != null) { - return mEditor.getDefaultOnReceiveContentCallback().onReceiveContent(this, payload); + public @Nullable Payload onReceiveContent(@NonNull Payload payload) { + Payload remaining = super.onReceiveContent(payload); + if (remaining != null && mEditor != null) { + return mEditor.getDefaultOnReceiveContentListener().onReceiveContent(this, remaining); } - return false; + return remaining; } private static void logCursor(String location, @Nullable String msgFormat, Object ... msgArgs) { |
