From 2d70047b1939822608158e959d6e96509b54a894 Mon Sep 17 00:00:00 2001 From: Oli Lan Date: Mon, 22 Mar 2021 18:07:34 +0000 Subject: [PATCH] Change clipboard access notification wording. This changes the wording of the clipboard access toast notifications to: " pasted text you copied" or " pasted content you copied". A string is also included for the case " pasted an image you copied" - a future change may add support for that case if needed. Bug: 179469804 Test: manual, copy text and non-text and check notification is correct. Change-Id: I426c38c53316362bd8e79ec2b2ff40fa420ac071 --- core/res/res/values/strings.xml | 9 +++++ core/res/res/values/symbols.xml | 3 ++ .../server/clipboard/ClipboardService.java | 34 +++++++++++-------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 7ea762c2fbbb3..4d43943d022cd 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2802,6 +2802,15 @@ %1$s pasted from clipboard + + %1$s pasted text you copied + + + %1$s pasted an image you copied + + + %1$s pasted content you copied + More diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 1d74d85fb9db6..b7d132ec1c593 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -560,6 +560,9 @@ + + + diff --git a/services/core/java/com/android/server/clipboard/ClipboardService.java b/services/core/java/com/android/server/clipboard/ClipboardService.java index 71565301e3ed1..2fbae81ce4c96 100644 --- a/services/core/java/com/android/server/clipboard/ClipboardService.java +++ b/services/core/java/com/android/server/clipboard/ClipboardService.java @@ -1044,27 +1044,16 @@ public class ClipboardService extends SystemService { clipboard.mNotifiedUids.put(uid, true); Binder.withCleanCallingIdentity(() -> { - // Retrieve the app label of the source of the clip data - CharSequence sourceAppLabel = null; - if (clipboard.mPrimaryClipPackage != null) { - try { - sourceAppLabel = mPm.getApplicationLabel(mPm.getApplicationInfoAsUser( - clipboard.mPrimaryClipPackage, 0, userId)); - } catch (PackageManager.NameNotFoundException e) { - // leave label as null - } - } - try { CharSequence callingAppLabel = mPm.getApplicationLabel( mPm.getApplicationInfoAsUser(callingPackage, 0, userId)); String message; - if (sourceAppLabel != null) { + if (isText(clipboard.primaryClip)) { message = getContext().getString( - R.string.pasted_from_app, callingAppLabel, sourceAppLabel); + R.string.pasted_text, callingAppLabel); } else { message = getContext().getString( - R.string.pasted_from_clipboard, callingAppLabel); + R.string.pasted_content, callingAppLabel); } Slog.i(TAG, message); Toast.makeText( @@ -1075,4 +1064,21 @@ public class ClipboardService extends SystemService { } }); } + + /** + * Returns true if the provided {@link ClipData} represents a single piece of text. That is, if + * there is only on {@link ClipData.Item}, and that item contains a non-empty piece of text and + * no URI or Intent. Note that HTML may be provided along with text so the presence of + * HtmlText in the clip does not prevent this method returning true. + */ + private static boolean isText(@NonNull ClipData data) { + if (data.getItemCount() > 1) { + return false; + } + ClipData.Item item = data.getItemAt(0); + + return !TextUtils.isEmpty(item.getText()) && item.getUri() == null + && item.getIntent() == null; + } + }