summaryrefslogtreecommitdiff
path: root/java/src/com
diff options
context:
space:
mode:
author Andrey Epin <ayepin@google.com> 2023-06-13 10:45:20 -0700
committer Matt Casey <mrcasey@google.com> 2023-06-22 16:41:33 +0000
commita1c9dc6af9fb0ff4b50ed1b8e7f59bdfccbfa682 (patch)
treecbd87038bf885a46c84dfb6a10021d95aefda0e1 /java/src/com
parentfd12c470628a561a71afcc4e6f132f07c055e5a0 (diff)
Provide copy action only when a text is sent
The copy button will be present to the user only when a text without images is sent i.e. intent action is ACTION_SEND, EXTRA_STREAM does not have any URI and EXTRA_TEXT is not null. Fix: 287061873 Fix: 275382122 Fix: 288425116 Test: manual testing Test: atest ChooserActionFactoryTest Change-Id: I26c6eef24e7f909842eb066a73286a7525124d21
Diffstat (limited to 'java/src/com')
-rw-r--r--java/src/com/android/intentresolver/ChooserActionFactory.java75
1 files changed, 38 insertions, 37 deletions
diff --git a/java/src/com/android/intentresolver/ChooserActionFactory.java b/java/src/com/android/intentresolver/ChooserActionFactory.java
index 6ec62753..2308dda5 100644
--- a/java/src/com/android/intentresolver/ChooserActionFactory.java
+++ b/java/src/com/android/intentresolver/ChooserActionFactory.java
@@ -84,6 +84,8 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
private static final String IMAGE_EDITOR_SHARED_ELEMENT = "screenshot_preview_image";
private final Context mContext;
+
+ @Nullable
private final Runnable mCopyButtonRunnable;
private final Runnable mEditButtonRunnable;
private final ImmutableList<ChooserAction> mCustomActions;
@@ -140,7 +142,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
@VisibleForTesting
ChooserActionFactory(
Context context,
- Runnable copyButtonRunnable,
+ @Nullable Runnable copyButtonRunnable,
Runnable editButtonRunnable,
List<ChooserAction> customActions,
@Nullable ChooserAction modifyShareAction,
@@ -219,49 +221,24 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
return mExcludeSharedTextAction;
}
+ @Nullable
private static Runnable makeCopyButtonRunnable(
Context context,
Intent targetIntent,
String referrerPackageName,
Consumer<Integer> finishCallback,
ChooserActivityLogger logger) {
+ final ClipData clipData;
+ try {
+ clipData = extractTextToCopy(targetIntent);
+ } catch (Throwable t) {
+ Log.e(TAG, "Failed to extract data to copy", t);
+ return null;
+ }
+ if (clipData == null) {
+ return null;
+ }
return () -> {
- if (targetIntent == null) {
- finishCallback.accept(null);
- return;
- }
-
- final String action = targetIntent.getAction();
-
- ClipData clipData = null;
- if (Intent.ACTION_SEND.equals(action)) {
- String extraText = targetIntent.getStringExtra(Intent.EXTRA_TEXT);
- Uri extraStream = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM);
-
- if (extraText != null) {
- clipData = ClipData.newPlainText(null, extraText);
- } else if (extraStream != null) {
- clipData = ClipData.newUri(context.getContentResolver(), null, extraStream);
- } else {
- Log.w(TAG, "No data available to copy to clipboard");
- return;
- }
- } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
- final ArrayList<Uri> streams = targetIntent.getParcelableArrayListExtra(
- Intent.EXTRA_STREAM);
- clipData = ClipData.newUri(context.getContentResolver(), null, streams.get(0));
- for (int i = 1; i < streams.size(); i++) {
- clipData.addItem(
- context.getContentResolver(),
- new ClipData.Item(streams.get(i)));
- }
- } else {
- // expected to only be visible with ACTION_SEND or ACTION_SEND_MULTIPLE
- // so warn about unexpected action
- Log.w(TAG, "Action (" + action + ") not supported for copying to clipboard");
- return;
- }
-
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(
Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClipAsPackage(clipData, referrerPackageName);
@@ -271,6 +248,30 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
};
}
+ @Nullable
+ private static ClipData extractTextToCopy(Intent targetIntent) {
+ if (targetIntent == null) {
+ return null;
+ }
+
+ final String action = targetIntent.getAction();
+
+ ClipData clipData = null;
+ if (Intent.ACTION_SEND.equals(action)) {
+ String extraText = targetIntent.getStringExtra(Intent.EXTRA_TEXT);
+
+ if (extraText != null) {
+ clipData = ClipData.newPlainText(null, extraText);
+ } else {
+ Log.w(TAG, "No data available to copy to clipboard");
+ }
+ } else {
+ // expected to only be visible with ACTION_SEND (when a text is shared)
+ Log.d(TAG, "Action (" + action + ") not supported for copying to clipboard");
+ }
+ return clipData;
+ }
+
private static TargetInfo getEditSharingTarget(
Context context,
Intent originalIntent,