summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
author TreeHugger Robot <treehugger-gerrit@google.com> 2023-06-26 22:18:17 +0000
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2023-06-26 22:18:17 +0000
commit7aeb9c3080643898a37e310774fbb787a90c0c19 (patch)
treec7f3fd3b63e5100aa8337fde044254a27eadd46b /java/src
parentab04cd8f31c3d40a278d1539a975f40009341dad (diff)
parent2d70b7ebc7c0017511127d241b331a7be93f874a (diff)
Merge "Provide copy action only when a text is sent" into udc-dev am: 2d70b7ebc7
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/IntentResolver/+/23676965 Change-Id: I35b49aed3a0e561efc7a7229b49f93cf1f553e69 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'java/src')
-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 4e595c96..06c7e8d7 100644
--- a/java/src/com/android/intentresolver/ChooserActionFactory.java
+++ b/java/src/com/android/intentresolver/ChooserActionFactory.java
@@ -89,6 +89,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;
@@ -145,7 +147,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,
@@ -224,49 +226,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);
@@ -276,6 +253,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,