From e1f5e13c0fe8a436f53b402361e43728d31d8888 Mon Sep 17 00:00:00 2001 From: Vladislav Kaznacheev Date: Fri, 27 Jan 2017 16:56:40 -0800 Subject: [PATCH] Do not return cryptic strings from ClipData.Item.coerceToText Currently, when ClipData contains an URI of "content", "file" or "android.resource" scheme, and the URI cannot be opened as a stream for some reason, then ClipData.Item.coerceToText (or coerceToHtmlText) return the URI's textual representation. This representation is not useful for a user at all. Returning an empty string instead. Bug: 28929820 Test: Drag a file from Files app to any text input, no URI should be inserted Change-Id: I21a23b673ef145a008427e9177403b5b1a41b898 --- core/java/android/content/ClipData.java | 27 +++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/core/java/android/content/ClipData.java b/core/java/android/content/ClipData.java index 70967712f0687..ed8c196ba58b3 100644 --- a/core/java/android/content/ClipData.java +++ b/core/java/android/content/ClipData.java @@ -17,6 +17,9 @@ package android.content; import static android.content.ContentProvider.maybeAddUserId; +import static android.content.ContentResolver.SCHEME_ANDROID_RESOURCE; +import static android.content.ContentResolver.SCHEME_CONTENT; +import static android.content.ContentResolver.SCHEME_FILE; import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; @@ -371,8 +374,14 @@ public class ClipData implements Parcelable { } } - // If we couldn't open the URI as a stream, then the URI itself - // probably serves fairly well as a textual representation. + // If we couldn't open the URI as a stream, use the URI itself as a textual + // representation (but not for "content", "android.resource" or "file" schemes). + final String scheme = uri.getScheme(); + if (SCHEME_CONTENT.equals(scheme) + || SCHEME_ANDROID_RESOURCE.equals(scheme) + || SCHEME_FILE.equals(scheme)) { + return ""; + } return uri.toString(); } @@ -555,9 +564,15 @@ public class ClipData implements Parcelable { } } - // If we couldn't open the URI as a stream, then we can build - // some HTML text with the URI itself. - // probably serves fairly well as a textual representation. + // If we couldn't open the URI as a stream, use the URI itself as a textual + // representation (but not for "content", "android.resource" or "file" schemes). + final String scheme = mUri.getScheme(); + if (SCHEME_CONTENT.equals(scheme) + || SCHEME_ANDROID_RESOURCE.equals(scheme) + || SCHEME_FILE.equals(scheme)) { + return ""; + } + if (styled) { return uriToStyledText(mUri.toString()); } else { @@ -777,7 +792,7 @@ public class ClipData implements Parcelable { */ private static String[] getMimeTypes(ContentResolver resolver, Uri uri) { String[] mimeTypes = null; - if ("content".equals(uri.getScheme())) { + if (SCHEME_CONTENT.equals(uri.getScheme())) { String realType = resolver.getType(uri); mimeTypes = resolver.getStreamTypes(uri, "*/*"); if (realType != null) {