From 6ba2c558256f8da66cb99d41f904dbc8185cf645 Mon Sep 17 00:00:00 2001 From: Tony Mak Date: Wed, 31 Mar 2021 20:51:07 +0100 Subject: [PATCH] Use the text classifier in the right user Bug: 180577441 Test: Copy something from the work profile, verify that the text classifier service running in the work profile is invoked. Test: Repeat that in the personal profile Change-Id: Id9df958b576ee48bc346a65905d960daa09b307f --- .../server/clipboard/ClipboardService.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/services/core/java/com/android/server/clipboard/ClipboardService.java b/services/core/java/com/android/server/clipboard/ClipboardService.java index e14877500c47b..bc1a0d171bff8 100644 --- a/services/core/java/com/android/server/clipboard/ClipboardService.java +++ b/services/core/java/com/android/server/clipboard/ClipboardService.java @@ -209,7 +209,6 @@ public class ClipboardService extends SystemService { private final AppOpsManager mAppOps; private final ContentCaptureManagerInternal mContentCaptureInternal; private final AutofillManagerInternal mAutofillInternal; - private final TextClassificationManager mTextClassificationManager; private final IBinder mPermissionOwner; private final HostClipboardMonitor mHostClipboardMonitor; private final Handler mWorkerHandler; @@ -240,8 +239,6 @@ public class ClipboardService extends SystemService { mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); mContentCaptureInternal = LocalServices.getService(ContentCaptureManagerInternal.class); mAutofillInternal = LocalServices.getService(AutofillManagerInternal.class); - mTextClassificationManager = (TextClassificationManager) - getContext().getSystemService(Context.TEXT_CLASSIFICATION_SERVICE); final IBinder permOwner = mUgmInternal.newUriPermissionOwner("clipboard"); mPermissionOwner = permOwner; if (IS_EMULATOR) { @@ -634,12 +631,12 @@ public class ClipboardService extends SystemService { } } + final int userId = UserHandle.getUserId(uid); if (clip != null) { - startClassificationLocked(clip); + startClassificationLocked(clip, userId); } // Update this user - final int userId = UserHandle.getUserId(uid); setPrimaryClipInternalLocked(getClipboardLocked(userId), clip, uid, sourcePackage); // Update related users @@ -738,14 +735,15 @@ public class ClipboardService extends SystemService { } @GuardedBy("mLock") - private void startClassificationLocked(@NonNull ClipData clip) { + private void startClassificationLocked(@NonNull ClipData clip, @UserIdInt int userId) { TextClassifier classifier; final long ident = Binder.clearCallingIdentity(); try { - classifier = mTextClassificationManager.createTextClassificationSession( - new TextClassificationContext.Builder( - getContext().getPackageName(), - TextClassifier.WIDGET_TYPE_CLIPBOARD + classifier = createTextClassificationManagerAsUser(userId) + .createTextClassificationSession( + new TextClassificationContext.Builder( + getContext().getPackageName(), + TextClassifier.WIDGET_TYPE_CLIPBOARD ).build() ); } finally { @@ -1100,4 +1098,8 @@ public class ClipboardService extends SystemService { && item.getIntent() == null; } + private TextClassificationManager createTextClassificationManagerAsUser(@UserIdInt int userId) { + Context context = getContext().createContextAsUser(UserHandle.of(userId), /* flags= */ 0); + return context.getSystemService(TextClassificationManager.class); + } }