Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf555a91 authored by Nate Myren's avatar Nate Myren Committed by Android (Google) Code Review
Browse files

Merge "Add clipboard access APIs to ClipboardManager" into udc-dev

parents 5d3be7c0 380687e4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -173,6 +173,7 @@ package android {
    field public static final String MANAGE_BLUETOOTH_WHEN_WIRELESS_CONSENT_REQUIRED = "android.permission.MANAGE_BLUETOOTH_WHEN_WIRELESS_CONSENT_REQUIRED";
    field public static final String MANAGE_CARRIER_OEM_UNLOCK_STATE = "android.permission.MANAGE_CARRIER_OEM_UNLOCK_STATE";
    field public static final String MANAGE_CA_CERTIFICATES = "android.permission.MANAGE_CA_CERTIFICATES";
    field public static final String MANAGE_CLIPBOARD_ACCESS_NOTIFICATION = "android.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION";
    field public static final String MANAGE_CLOUDSEARCH = "android.permission.MANAGE_CLOUDSEARCH";
    field public static final String MANAGE_CONTENT_CAPTURE = "android.permission.MANAGE_CONTENT_CAPTURE";
    field public static final String MANAGE_CONTENT_SUGGESTIONS = "android.permission.MANAGE_CONTENT_SUGGESTIONS";
@@ -3399,6 +3400,8 @@ package android.content {
  }
  public class ClipboardManager extends android.text.ClipboardManager {
    method @RequiresPermission(android.Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION) public boolean areClipboardAccessNotificationsEnabled();
    method @RequiresPermission(android.Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION) public void setClipboardAccessNotificationsEnabled(boolean);
    method @RequiresPermission(android.Manifest.permission.SET_CLIP_SOURCE) public void setPrimaryClipAsPackage(@NonNull android.content.ClipData, @NonNull String);
  }
+33 −0
Original line number Diff line number Diff line
@@ -134,6 +134,39 @@ public class ClipboardManager extends android.text.ClipboardManager {
                ServiceManager.getServiceOrThrow(Context.CLIPBOARD_SERVICE));
    }

    /**
     * Determine if the Clipboard Access Notifications are enabled
     *
     * @return true if notifications are enabled, false otherwise.
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION)
    public boolean areClipboardAccessNotificationsEnabled() {
        try {
            return mService.areClipboardAccessNotificationsEnabledForUser(mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     *
     * Set the enable state of the Clipboard Access Notifications
     * @param enable Whether to enable notifications
     * @hide
     */
    @SystemApi
    @RequiresPermission(Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION)
    public void setClipboardAccessNotificationsEnabled(boolean enable) {
        try {
            mService.setClipboardAccessNotificationsEnabledForUser(enable, mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the current primary clip on the clipboard.  This is the clip that
     * is involved in normal cut and paste operations.
+4 −0
Original line number Diff line number Diff line
@@ -48,4 +48,8 @@ interface IClipboard {

    String getPrimaryClipSource(String callingPackage, String attributionTag, int userId,
            int deviceId);

    boolean areClipboardAccessNotificationsEnabledForUser(int userId);

    void setClipboardAccessNotificationsEnabledForUser(boolean enable, int userId);
}
+7 −0
Original line number Diff line number Diff line
@@ -7107,6 +7107,13 @@
         @hide Not for use by third-party applications. -->
    <permission android:name="android.permission.READ_CLIPBOARD_IN_BACKGROUND"
        android:protectionLevel="signature|role" />

    <!-- @SystemApi Permission that allows apps to disable the clipboard access notifications.
         @hide
         <p>Not for use by third-party applications. -->
    <permission android:name="android.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION"
        android:protectionLevel="signature|installer" />

    <!-- @hide Permission that suppresses the notification when the clipboard is accessed.
         <p>Not for use by third-party applications. -->
    <permission android:name="android.permission.SUPPRESS_CLIPBOARD_ACCESS_NOTIFICATION"
+45 −0
Original line number Diff line number Diff line
@@ -484,6 +484,51 @@ public class ClipboardService extends SystemService {
                    sourcePackage);
        }

        @Override
        public boolean areClipboardAccessNotificationsEnabledForUser(int userId) {
            int result = getContext().checkCallingOrSelfPermission(
                    Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION);
            if (result != PackageManager.PERMISSION_GRANTED) {
                throw new SecurityException("areClipboardAccessNotificationsEnable requires "
                        + "permission MANAGE_CLIPBOARD_ACCESS_NOTIFICATION");
            }

            long callingId = Binder.clearCallingIdentity();
            try {
                return Settings.Secure.getIntForUser(getContext().getContentResolver(),
                        Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS,
                        getDefaultClipboardAccessNotificationsSetting(), userId) != 0;
            } finally {
                Binder.restoreCallingIdentity(callingId);
            }
        }

        @Override
        public void setClipboardAccessNotificationsEnabledForUser(boolean enable, int userId) {
            int result = getContext().checkCallingOrSelfPermission(
                    Manifest.permission.MANAGE_CLIPBOARD_ACCESS_NOTIFICATION);
            if (result != PackageManager.PERMISSION_GRANTED) {
                throw new SecurityException("areClipboardAccessNotificationsEnable requires "
                        + "permission MANAGE_CLIPBOARD_ACCESS_NOTIFICATION");
            }

            long callingId = Binder.clearCallingIdentity();
            try {
                ContentResolver resolver = getContext()
                        .createContextAsUser(UserHandle.of(userId), 0).getContentResolver();
                Settings.Secure.putInt(resolver,
                        Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, (enable ? 1 : 0));
            } finally {
                Binder.restoreCallingIdentity(callingId);
            }
        }

        private int getDefaultClipboardAccessNotificationsSetting() {
            return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_CLIPBOARD,
                    ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS,
                    ClipboardManager.DEVICE_CONFIG_DEFAULT_SHOW_ACCESS_NOTIFICATIONS) ? 1 : 0;
        }

        private void checkAndSetPrimaryClip(
                ClipData clip,
                String callingPackage,