From 9a35ddf5f83d16b53b08fd6293bd94ef2ab013ba Mon Sep 17 00:00:00 2001 From: Miranda Kephart Date: Tue, 20 Jun 2023 13:17:15 -0400 Subject: [PATCH 1/2] Remove setData from clipboard intents Some apps interpret the "data" field as "to". We're already putting the URI for share intents in EXTRA_STREAM, so remove it from setData. Bug: 285871401 Test: manual (share a copied image from personal profile to a work profile app) Change-Id: Ic50605799b6ce561674e79696090f0e7b8df03dc --- .../com/android/systemui/clipboardoverlay/IntentCreator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java index e342ac2f320dc..fc928a65848ec 100644 --- a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java +++ b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java @@ -42,8 +42,8 @@ class IntentCreator { // "If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the // MIME type of the data in EXTRA_STREAM" if (clipData.getItemAt(0).getUri() != null) { - shareIntent.setDataAndType( - clipData.getItemAt(0).getUri(), clipData.getDescription().getMimeType(0)); + // We don't use setData here because some apps interpret this as "to:". + shareIntent.setType(clipData.getDescription().getMimeType(0)); shareIntent.putExtra(Intent.EXTRA_STREAM, clipData.getItemAt(0).getUri()); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { From 8c73c06e6ee81e444eb3a665eb0daacfe51124a6 Mon Sep 17 00:00:00 2001 From: Miranda Kephart Date: Wed, 21 Jun 2023 12:47:26 -0400 Subject: [PATCH 2/2] Put clipboard image uri in clipData for share intent If the URI is only set in EXTRA_STREAM, grantPermissions is not able to pick up and add the GRANT_READ_URI permission. Add ClipData to the share intent sent from the clipboard UI to correctly propagate the permission through. Bug: 288272806 Fix: 288272806 Test: manual (copy image -> share -> verify preview appears); atest IntentCreatorTest Change-Id: Ic52ff10cab62f1cab9fd81d93011b0454885c21e --- .../systemui/clipboardoverlay/IntentCreator.java | 11 +++++++++-- .../systemui/clipboardoverlay/IntentCreatorTest.java | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java index fc928a65848ec..566a74ae3e077 100644 --- a/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java +++ b/packages/SystemUI/src/com/android/systemui/clipboardoverlay/IntentCreator.java @@ -17,6 +17,7 @@ package com.android.systemui.clipboardoverlay; import android.content.ClipData; +import android.content.ClipDescription; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -41,10 +42,16 @@ class IntentCreator { // From the ACTION_SEND docs: // "If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the // MIME type of the data in EXTRA_STREAM" - if (clipData.getItemAt(0).getUri() != null) { + Uri uri = clipData.getItemAt(0).getUri(); + if (uri != null) { // We don't use setData here because some apps interpret this as "to:". shareIntent.setType(clipData.getDescription().getMimeType(0)); - shareIntent.putExtra(Intent.EXTRA_STREAM, clipData.getItemAt(0).getUri()); + // Include URI in ClipData also, so that grantPermission picks it up. + shareIntent.setClipData(new ClipData( + new ClipDescription( + "content", new String[]{clipData.getDescription().getMimeType(0)}), + new ClipData.Item(uri))); + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { shareIntent.putExtra( diff --git a/packages/SystemUI/tests/src/com/android/systemui/clipboardoverlay/IntentCreatorTest.java b/packages/SystemUI/tests/src/com/android/systemui/clipboardoverlay/IntentCreatorTest.java index 2a4c0eb18d024..7628be44755df 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/clipboardoverlay/IntentCreatorTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/clipboardoverlay/IntentCreatorTest.java @@ -126,7 +126,8 @@ public class IntentCreatorTest extends SysuiTestCase { assertEquals(Intent.ACTION_CHOOSER, intent.getAction()); assertFlags(intent, EXTERNAL_INTENT_FLAGS); Intent target = intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent.class); - assertEquals(uri, target.getData()); + assertEquals(uri, target.getParcelableExtra(Intent.EXTRA_STREAM, Uri.class)); + assertEquals(uri, target.getClipData().getItemAt(0).getUri()); assertEquals("image/png", target.getType()); }