Merge "Disable screenshot actions for work profile." into tm-qpr-dev

This commit is contained in:
Matt Casey
2022-11-16 22:27:35 +00:00
committed by Android (Google) Code Review
2 changed files with 35 additions and 22 deletions

View File

@@ -38,6 +38,7 @@ import android.graphics.drawable.Icon;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
@@ -81,7 +82,6 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
private final ScreenshotNotificationSmartActionsProvider mSmartActionsProvider; private final ScreenshotNotificationSmartActionsProvider mSmartActionsProvider;
private String mScreenshotId; private String mScreenshotId;
private final boolean mSmartActionsEnabled;
private final Random mRandom = new Random(); private final Random mRandom = new Random();
private final Supplier<ActionTransition> mSharedElementTransition; private final Supplier<ActionTransition> mSharedElementTransition;
private final ImageExporter mImageExporter; private final ImageExporter mImageExporter;
@@ -109,8 +109,6 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
mParams = data; mParams = data;
// Initialize screenshot notification smart actions provider. // Initialize screenshot notification smart actions provider.
mSmartActionsEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.ENABLE_SCREENSHOT_NOTIFICATION_SMART_ACTIONS, true);
mSmartActionsProvider = screenshotNotificationSmartActionsProvider; mSmartActionsProvider = screenshotNotificationSmartActionsProvider;
} }
@@ -131,8 +129,16 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
Bitmap image = mParams.image; Bitmap image = mParams.image;
mScreenshotId = String.format(SCREENSHOT_ID_TEMPLATE, requestId); mScreenshotId = String.format(SCREENSHOT_ID_TEMPLATE, requestId);
boolean savingToOtherUser = mFlags.isEnabled(Flags.SCREENSHOT_WORK_PROFILE_POLICY)
&& (user != Process.myUserHandle());
// Smart actions don't yet work for cross-user saves.
boolean smartActionsEnabled = !savingToOtherUser
&& DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.ENABLE_SCREENSHOT_NOTIFICATION_SMART_ACTIONS,
true);
try { try {
if (mSmartActionsEnabled && mParams.mQuickShareActionsReadyListener != null) { if (smartActionsEnabled && mParams.mQuickShareActionsReadyListener != null) {
// Since Quick Share target recommendation does not rely on image URL, it is // Since Quick Share target recommendation does not rely on image URL, it is
// queried and surfaced before image compress/export. Action intent would not be // queried and surfaced before image compress/export. Action intent would not be
// used, because it does not contain image URL. // used, because it does not contain image URL.
@@ -150,10 +156,9 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
CompletableFuture<List<Notification.Action>> smartActionsFuture = CompletableFuture<List<Notification.Action>> smartActionsFuture =
mScreenshotSmartActions.getSmartActionsFuture( mScreenshotSmartActions.getSmartActionsFuture(
mScreenshotId, uri, image, mSmartActionsProvider, REGULAR_SMART_ACTIONS, mScreenshotId, uri, image, mSmartActionsProvider, REGULAR_SMART_ACTIONS,
mSmartActionsEnabled, user); smartActionsEnabled, user);
List<Notification.Action> smartActions = new ArrayList<>(); List<Notification.Action> smartActions = new ArrayList<>();
if (mSmartActionsEnabled) { if (smartActionsEnabled) {
int timeoutMs = DeviceConfig.getInt( int timeoutMs = DeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI, DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SCREENSHOT_NOTIFICATION_SMART_ACTIONS_TIMEOUT_MS, SystemUiDeviceConfigFlags.SCREENSHOT_NOTIFICATION_SMART_ACTIONS_TIMEOUT_MS,
@@ -168,9 +173,12 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
mImageData.uri = uri; mImageData.uri = uri;
mImageData.owner = user; mImageData.owner = user;
mImageData.smartActions = smartActions; mImageData.smartActions = smartActions;
mImageData.shareTransition = createShareAction(mContext, mContext.getResources(), uri); mImageData.shareTransition = createShareAction(mContext, mContext.getResources(), uri,
mImageData.editTransition = createEditAction(mContext, mContext.getResources(), uri); smartActionsEnabled);
mImageData.deleteAction = createDeleteAction(mContext, mContext.getResources(), uri); mImageData.editTransition = createEditAction(mContext, mContext.getResources(), uri,
smartActionsEnabled);
mImageData.deleteAction = createDeleteAction(mContext, mContext.getResources(), uri,
smartActionsEnabled);
mImageData.quickShareAction = createQuickShareAction(mContext, mImageData.quickShareAction = createQuickShareAction(mContext,
mQuickShareData.quickShareAction, uri); mQuickShareData.quickShareAction, uri);
mImageData.subject = getSubjectString(); mImageData.subject = getSubjectString();
@@ -228,7 +236,8 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
* Assumes that the action intent is sent immediately after being supplied. * Assumes that the action intent is sent immediately after being supplied.
*/ */
@VisibleForTesting @VisibleForTesting
Supplier<ActionTransition> createShareAction(Context context, Resources r, Uri uri) { Supplier<ActionTransition> createShareAction(Context context, Resources r, Uri uri,
boolean smartActionsEnabled) {
return () -> { return () -> {
ActionTransition transition = mSharedElementTransition.get(); ActionTransition transition = mSharedElementTransition.get();
@@ -274,7 +283,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
.putExtra(ScreenshotController.EXTRA_DISALLOW_ENTER_PIP, true) .putExtra(ScreenshotController.EXTRA_DISALLOW_ENTER_PIP, true)
.putExtra(ScreenshotController.EXTRA_ID, mScreenshotId) .putExtra(ScreenshotController.EXTRA_ID, mScreenshotId)
.putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED, .putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED,
mSmartActionsEnabled) smartActionsEnabled)
.setAction(Intent.ACTION_SEND) .setAction(Intent.ACTION_SEND)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND), .addFlags(Intent.FLAG_RECEIVER_FOREGROUND),
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE,
@@ -290,7 +299,8 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
} }
@VisibleForTesting @VisibleForTesting
Supplier<ActionTransition> createEditAction(Context context, Resources r, Uri uri) { Supplier<ActionTransition> createEditAction(Context context, Resources r, Uri uri,
boolean smartActionsEnabled) {
return () -> { return () -> {
ActionTransition transition = mSharedElementTransition.get(); ActionTransition transition = mSharedElementTransition.get();
// Note: Both the share and edit actions are proxied through ActionProxyReceiver in // Note: Both the share and edit actions are proxied through ActionProxyReceiver in
@@ -323,7 +333,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
.putExtra(ScreenshotController.EXTRA_ACTION_INTENT, pendingIntent) .putExtra(ScreenshotController.EXTRA_ACTION_INTENT, pendingIntent)
.putExtra(ScreenshotController.EXTRA_ID, mScreenshotId) .putExtra(ScreenshotController.EXTRA_ID, mScreenshotId)
.putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED, .putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED,
mSmartActionsEnabled) smartActionsEnabled)
.putExtra(ScreenshotController.EXTRA_OVERRIDE_TRANSITION, true) .putExtra(ScreenshotController.EXTRA_OVERRIDE_TRANSITION, true)
.setAction(Intent.ACTION_EDIT) .setAction(Intent.ACTION_EDIT)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND), .addFlags(Intent.FLAG_RECEIVER_FOREGROUND),
@@ -339,7 +349,8 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
} }
@VisibleForTesting @VisibleForTesting
Notification.Action createDeleteAction(Context context, Resources r, Uri uri) { Notification.Action createDeleteAction(Context context, Resources r, Uri uri,
boolean smartActionsEnabled) {
// Make sure pending intents for the system user are still unique across users // Make sure pending intents for the system user are still unique across users
// by setting the (otherwise unused) request code to the current user id. // by setting the (otherwise unused) request code to the current user id.
int requestCode = mContext.getUserId(); int requestCode = mContext.getUserId();
@@ -350,7 +361,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
.putExtra(ScreenshotController.SCREENSHOT_URI_ID, uri.toString()) .putExtra(ScreenshotController.SCREENSHOT_URI_ID, uri.toString())
.putExtra(ScreenshotController.EXTRA_ID, mScreenshotId) .putExtra(ScreenshotController.EXTRA_ID, mScreenshotId)
.putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED, .putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED,
mSmartActionsEnabled) smartActionsEnabled)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND), .addFlags(Intent.FLAG_RECEIVER_FOREGROUND),
PendingIntent.FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT
| PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_ONE_SHOT
@@ -391,7 +402,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
Intent intent = new Intent(context, SmartActionsReceiver.class) Intent intent = new Intent(context, SmartActionsReceiver.class)
.putExtra(ScreenshotController.EXTRA_ACTION_INTENT, action.actionIntent) .putExtra(ScreenshotController.EXTRA_ACTION_INTENT, action.actionIntent)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
addIntentExtras(mScreenshotId, intent, actionType, mSmartActionsEnabled); addIntentExtras(mScreenshotId, intent, actionType, true /* smartActionsEnabled */);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(context, PendingIntent broadcastIntent = PendingIntent.getBroadcast(context,
mRandom.nextInt(), mRandom.nextInt(),
intent, intent,
@@ -445,7 +456,9 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
Intent intent = new Intent(context, SmartActionsReceiver.class) Intent intent = new Intent(context, SmartActionsReceiver.class)
.putExtra(ScreenshotController.EXTRA_ACTION_INTENT, updatedPendingIntent) .putExtra(ScreenshotController.EXTRA_ACTION_INTENT, updatedPendingIntent)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
addIntentExtras(mScreenshotId, intent, actionType, mSmartActionsEnabled); // We only query for quick share actions when smart actions are enabled, so we can assert
// that it's true here.
addIntentExtras(mScreenshotId, intent, actionType, true /* smartActionsEnabled */);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(context, PendingIntent broadcastIntent = PendingIntent.getBroadcast(context,
mRandom.nextInt(), mRandom.nextInt(),
intent, intent,
@@ -464,7 +477,7 @@ class SaveImageInBackgroundTask extends AsyncTask<Void, Void, Void> {
mScreenshotSmartActions.getSmartActionsFuture( mScreenshotSmartActions.getSmartActionsFuture(
mScreenshotId, null, image, mSmartActionsProvider, mScreenshotId, null, image, mSmartActionsProvider,
QUICK_SHARE_ACTION, QUICK_SHARE_ACTION,
mSmartActionsEnabled, user); true /* smartActionsEnabled */, user);
int timeoutMs = DeviceConfig.getInt( int timeoutMs = DeviceConfig.getInt(
DeviceConfig.NAMESPACE_SYSTEMUI, DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.SCREENSHOT_NOTIFICATION_QUICK_SHARE_ACTIONS_TIMEOUT_MS, SystemUiDeviceConfigFlags.SCREENSHOT_NOTIFICATION_QUICK_SHARE_ACTIONS_TIMEOUT_MS,

View File

@@ -184,7 +184,7 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase {
ActionTransition::new, mSmartActionsProvider); ActionTransition::new, mSmartActionsProvider);
Notification.Action shareAction = task.createShareAction(mContext, mContext.getResources(), Notification.Action shareAction = task.createShareAction(mContext, mContext.getResources(),
Uri.parse("Screenshot_123.png")).get().action; Uri.parse("Screenshot_123.png"), true).get().action;
Intent intent = shareAction.actionIntent.getIntent(); Intent intent = shareAction.actionIntent.getIntent();
assertNotNull(intent); assertNotNull(intent);
@@ -212,7 +212,7 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase {
ActionTransition::new, mSmartActionsProvider); ActionTransition::new, mSmartActionsProvider);
Notification.Action editAction = task.createEditAction(mContext, mContext.getResources(), Notification.Action editAction = task.createEditAction(mContext, mContext.getResources(),
Uri.parse("Screenshot_123.png")).get().action; Uri.parse("Screenshot_123.png"), true).get().action;
Intent intent = editAction.actionIntent.getIntent(); Intent intent = editAction.actionIntent.getIntent();
assertNotNull(intent); assertNotNull(intent);
@@ -241,7 +241,7 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase {
Notification.Action deleteAction = task.createDeleteAction(mContext, Notification.Action deleteAction = task.createDeleteAction(mContext,
mContext.getResources(), mContext.getResources(),
Uri.parse("Screenshot_123.png")); Uri.parse("Screenshot_123.png"), true);
Intent intent = deleteAction.actionIntent.getIntent(); Intent intent = deleteAction.actionIntent.getIntent();
assertNotNull(intent); assertNotNull(intent);