From 104380d7c45e6a50abbe2fa1dc1940f0fca9fa7d Mon Sep 17 00:00:00 2001 From: Katsiaryna Naliuka Date: Thu, 14 May 2020 14:01:29 +0200 Subject: [PATCH] Pass the user handle for the screenshot app to the content suggestion service (SysUI part). Bug: 156405841 Test: atest ScreenshotNotificationSmartActionsTest Change-Id: Ie57744f11b00a8e8fa76184174e107330f7e3cf9 --- .../screenshot/SaveImageInBackgroundTask.java | 8 +++---- ...nshotNotificationSmartActionsProvider.java | 5 +++-- .../screenshot/ScreenshotSmartActions.java | 7 ++++--- ...creenshotNotificationSmartActionsTest.java | 21 ++++++++----------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java b/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java index bc3c33d68b673..67c71f2a5c9df 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java @@ -26,7 +26,6 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; -import android.content.pm.UserInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.Icon; @@ -146,7 +145,7 @@ class SaveImageInBackgroundTask extends AsyncTask { CompletableFuture> smartActionsFuture = ScreenshotSmartActions.getSmartActionsFuture( mScreenshotId, uri, image, mSmartActionsProvider, - mSmartActionsEnabled, isManagedProfile(mContext)); + mSmartActionsEnabled, getUserHandle(mContext)); try { // First, write the actual data for our screenshot @@ -382,10 +381,9 @@ class SaveImageInBackgroundTask extends AsyncTask { } } - private boolean isManagedProfile(Context context) { + private UserHandle getUserHandle(Context context) { UserManager manager = UserManager.get(context); - UserInfo info = manager.getUserInfo(getUserHandleOfForegroundApplication(context)); - return info.isManagedProfile(); + return manager.getUserInfo(getUserHandleOfForegroundApplication(context)).getUserHandle(); } private List buildSmartActions( diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsProvider.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsProvider.java index 3edb33da9cd0f..63f323ed2768c 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsProvider.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsProvider.java @@ -20,6 +20,7 @@ import android.app.Notification; import android.content.ComponentName; import android.graphics.Bitmap; import android.net.Uri; +import android.os.UserHandle; import android.util.Log; import java.util.Collections; @@ -64,14 +65,14 @@ public class ScreenshotNotificationSmartActionsProvider { * @param componentName Contains package and activity class names where the screenshot was * taken. This is used as an additional signal to generate and rank * more relevant actions. - * @param isManagedProfile The screenshot was taken for a work profile app. + * @param userHandle The user handle of the app where the screenshot was taken. */ public CompletableFuture> getActions( String screenshotId, Uri screenshotUri, Bitmap bitmap, ComponentName componentName, - boolean isManagedProfile) { + UserHandle userHandle) { Log.d(TAG, "Returning empty smart action list."); return CompletableFuture.completedFuture(Collections.emptyList()); } diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotSmartActions.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotSmartActions.java index c228fe2c4334e..442b373b31bee 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotSmartActions.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotSmartActions.java @@ -26,6 +26,7 @@ import android.graphics.Bitmap; import android.net.Uri; import android.os.Handler; import android.os.SystemClock; +import android.os.UserHandle; import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; @@ -48,7 +49,7 @@ public class ScreenshotSmartActions { static CompletableFuture> getSmartActionsFuture( String screenshotId, Uri screenshotUri, Bitmap image, ScreenshotNotificationSmartActionsProvider smartActionsProvider, - boolean smartActionsEnabled, boolean isManagedProfile) { + boolean smartActionsEnabled, UserHandle userHandle) { if (!smartActionsEnabled) { Slog.i(TAG, "Screenshot Intelligence not enabled, returning empty list."); return CompletableFuture.completedFuture(Collections.emptyList()); @@ -60,7 +61,7 @@ public class ScreenshotSmartActions { return CompletableFuture.completedFuture(Collections.emptyList()); } - Slog.d(TAG, "Screenshot from a managed profile: " + isManagedProfile); + Slog.d(TAG, "Screenshot from user profile: " + userHandle.getIdentifier()); CompletableFuture> smartActionsFuture; long startTimeMs = SystemClock.uptimeMillis(); try { @@ -71,7 +72,7 @@ public class ScreenshotSmartActions { ? runningTask.topActivity : new ComponentName("", ""); smartActionsFuture = smartActionsProvider.getActions( - screenshotId, screenshotUri, image, componentName, isManagedProfile); + screenshotId, screenshotUri, image, componentName, userHandle); } catch (Throwable e) { long waitTimeMs = SystemClock.uptimeMillis() - startTimeMs; smartActionsFuture = CompletableFuture.completedFuture(Collections.emptyList()); diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsTest.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsTest.java index 2c4304d51720a..d3b33992d0176 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotNotificationSmartActionsTest.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -37,6 +36,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.os.UserHandle; import android.testing.AndroidTestingRunner; import androidx.test.filters.SmallTest; @@ -79,13 +79,12 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase { when(bitmap.getConfig()).thenReturn(Bitmap.Config.HARDWARE); ScreenshotNotificationSmartActionsProvider smartActionsProvider = mock( ScreenshotNotificationSmartActionsProvider.class); - when(smartActionsProvider.getActions(any(), any(), any(), any(), - eq(false))).thenThrow( - RuntimeException.class); + when(smartActionsProvider.getActions(any(), any(), any(), any(), any())) + .thenThrow(RuntimeException.class); CompletableFuture> smartActionsFuture = ScreenshotSmartActions.getSmartActionsFuture( "", Uri.parse("content://authority/data"), bitmap, smartActionsProvider, - true, false); + true, UserHandle.getUserHandleForUid(UserHandle.myUserId())); assertNotNull(smartActionsFuture); List smartActions = smartActionsFuture.get(5, TimeUnit.MILLISECONDS); assertEquals(Collections.emptyList(), smartActions); @@ -125,9 +124,8 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase { CompletableFuture> smartActionsFuture = ScreenshotSmartActions.getSmartActionsFuture( "", Uri.parse("content://autority/data"), bitmap, mSmartActionsProvider, - true, true); - verify(mSmartActionsProvider, never()).getActions(any(), any(), any(), any(), - eq(false)); + true, UserHandle.getUserHandleForUid(UserHandle.myUserId())); + verify(mSmartActionsProvider, never()).getActions(any(), any(), any(), any(), any()); assertNotNull(smartActionsFuture); List smartActions = smartActionsFuture.get(5, TimeUnit.MILLISECONDS); assertEquals(Collections.emptyList(), smartActions); @@ -140,9 +138,8 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase { when(bitmap.getConfig()).thenReturn(Bitmap.Config.HARDWARE); ScreenshotSmartActions.getSmartActionsFuture( "", Uri.parse("content://autority/data"), bitmap, mSmartActionsProvider, true, - true); - verify(mSmartActionsProvider, times(1)) - .getActions(any(), any(), any(), any(), eq(true)); + UserHandle.getUserHandleForUid(UserHandle.myUserId())); + verify(mSmartActionsProvider, times(1)).getActions(any(), any(), any(), any(), any()); } // Tests for a hardware bitmap, a completed future is returned. @@ -157,7 +154,7 @@ public class ScreenshotNotificationSmartActionsTest extends SysuiTestCase { CompletableFuture> smartActionsFuture = ScreenshotSmartActions.getSmartActionsFuture("", null, bitmap, actionsProvider, - true, true); + true, UserHandle.getUserHandleForUid(UserHandle.myUserId())); assertNotNull(smartActionsFuture); List smartActions = smartActionsFuture.get(5, TimeUnit.MILLISECONDS); assertEquals(smartActions.size(), 0);