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

This commit is contained in:
Nate Myren
2023-02-23 00:27:36 +00:00
committed by Android (Google) Code Review
5 changed files with 92 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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.

View File

@@ -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);
}

View File

@@ -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"

View File

@@ -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,