Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Android] Check for text to paste before trying to retrieve data from URI #48166

Merged
merged 19 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Build;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
Expand Down Expand Up @@ -520,21 +521,46 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for

if (!clipboard.hasPrimaryClip()) return null;

CharSequence charSequence = null;
CharSequence itemText = null;
try {
ClipData clip = clipboard.getPrimaryClip();
if (clip == null) return null;
if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) {
ClipData.Item item = clip.getItemAt(0);
AssetFileDescriptor assetFileDescriptor = null;
if (item.getUri() != null)
assetFileDescriptor =
activity
.getContentResolver()
.openTypedAssetFileDescriptor(item.getUri(), "text/*", null);
charSequence = item.coerceToText(activity);
if (assetFileDescriptor != null) assetFileDescriptor.close();
return charSequence;
// First, try getting clipboard data as text; no further processing
// required if so.
itemText = item.getText();
if (itemText == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonblocking nit: consider extracting uri logic into a method or methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved logic trying to extract text from URI to a new method!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undid this due to some recent changes here that actually made the new method more confusing.

// Clipboard data does not contain text, so check whether or not it
// contains a URI to extract text from.
Uri itemUri = item.getUri();

if (itemUri == null) {
Log.w(
TAG, "Clipboard item contained no textual content nor a URI to retrieve it from.");
return null;
}

// Will only try to extract text from URI if it has the content scheme.
String uriScheme = itemUri.getScheme();

if (!uriScheme.equals("content")) {
Log.w(
TAG,
"Clipboard item contains a Uri with scheme '" + uriScheme + "'that is unhandled.");
return null;
}

AssetFileDescriptor assetFileDescriptor =
activity.getContentResolver().openTypedAssetFileDescriptor(itemUri, "text/*", null);

// Safely return clipboard data coerced into text; will return either
// itemText or text retrieved from its URI.
itemText = item.coerceToText(activity);
if (assetFileDescriptor != null) assetFileDescriptor.close();
}

return itemText;
}
} catch (SecurityException e) {
Log.w(
Expand All @@ -545,10 +571,11 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for
e);
return null;
} catch (FileNotFoundException e) {
Log.w(TAG, "Clipboard text was unable to be received from content URI.");
return null;
} catch (IOException e) {
Log.w(TAG, "Failed to close AssetFileDescriptor while accessing clipboard data.", e);
return charSequence;
Log.w(TAG, "Failed to close AssetFileDescriptor while trying to read text from URI.", e);
return itemText;
}

return null;
Expand Down
Loading