Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 15 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 @@ -11,6 +11,7 @@
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
Expand Down Expand Up @@ -517,8 +518,27 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for
if (clip == null) return null;
if (format == null || format == PlatformChannel.ClipboardContentFormat.PLAIN_TEXT) {
ClipData.Item item = clip.getItemAt(0);
if (item.getUri() != null)
activity.getContentResolver().openTypedAssetFileDescriptor(item.getUri(), "text/*", null);
// First, try getting clipboard data as text; no further processing
// required if so.
CharSequence itemText = item.getText();

if (itemText == null) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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;
} else if (!canExtractTextFromUri(itemUri)) {
Log.w(TAG, "Clipboard text was unable to be received from content URI.");
return null;
}
}

// Safely return clipboard data coerced into text; will return either
// itemText or text retrieved from its URI.
return item.coerceToText(activity);
}
} catch (SecurityException e) {
Expand All @@ -529,13 +549,30 @@ private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat for
+ "https://developer.android.com/guide/topics/permissions/overview",
e);
return null;
} catch (FileNotFoundException e) {
return null;
}

return null;
}

private boolean canExtractTextFromUri(Uri uri) {
// Will only try to extract text from URI if it has the content scheme.
String uriScheme = uri.getScheme();

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

try {
activity.getContentResolver().openTypedAssetFileDescriptor(uri, "text/*", null);
} catch (FileNotFoundException e) {
return false;
}

// Text can successfully be extracted from content URI.
return true;
}

private void setClipboardData(String text) {
ClipboardManager clipboard =
(ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
Expand Down
Loading