Skip to content

Commit

Permalink
Block clipboard UI when device is locked
Browse files Browse the repository at this point in the history
In some situations (see bug for details) it's possible to enter the
clipboard even while the device is locked, and from there access the
provided intents. Users should not be able to access intents from this
state; this change adds an additional check before showing the interactive UI.

The behavior is identical to what we do when user setup is not complete
(b/251778420): we show a toast to note that content has been copied, but no interactive UI.

Interactive UI is only blocked when device is locked (i.e. requiring pin
entry/password/biometric/etc), not if the keyguard is up but trivially
dismissable.

Bug: 317048495
Test: atest ClipboardListenerTest; verification using steps in linked
bug as well as forcing text content to appear client-side, to verify
that even if text content is received in the ClipboardListener, no
interactive UI appears.

(cherry picked from commit 2976ca8)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:b9ffec803b31f1b57756105c9fbfd0cb672fcfde)
Merged-In: I1a48cbe64852dce3fba69915ca11dad8878f66eb
Change-Id: I1a48cbe64852dce3fba69915ca11dad8878f66eb
  • Loading branch information
Miranda Kephart authored and aoleary committed Dec 30, 2024
1 parent 236c9d7 commit 8e54079
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static com.google.android.setupcompat.util.WizardManagerHelper.SETTINGS_SECURE_USER_SETUP_COMPLETE;

import android.app.KeyguardManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class ClipboardListener implements
private final ClipboardToast mClipboardToast;
private final ClipboardManager mClipboardManager;
private final FeatureFlags mFeatureFlags;
private final KeyguardManager mKeyguardManager;
private final UiEventLogger mUiEventLogger;
private ClipboardOverlay mClipboardOverlay;

Expand All @@ -69,12 +71,14 @@ public ClipboardListener(Context context,
Provider<ClipboardOverlayController> clipboardOverlayControllerProvider,
ClipboardToast clipboardToast,
ClipboardManager clipboardManager,
KeyguardManager keyguardManager,
FeatureFlags featureFlags,
UiEventLogger uiEventLogger) {
mContext = context;
mOverlayProvider = clipboardOverlayControllerProvider;
mClipboardToast = clipboardToast;
mClipboardManager = clipboardManager;
mKeyguardManager = keyguardManager;
mFeatureFlags = featureFlags;
mUiEventLogger = uiEventLogger;
}
Expand Down Expand Up @@ -102,7 +106,9 @@ public void onPrimaryClipChanged() {
return;
}

if (!isUserSetupComplete() // user should not access intents from this state
// user should not access intents before setup or while device is locked
if (mKeyguardManager.isDeviceLocked()
|| !isUserSetupComplete()
|| clipData == null // shouldn't happen, but just in case
|| clipData.getItemCount() == 0) {
if (shouldShowToast(clipData)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import android.app.KeyguardManager;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
Expand Down Expand Up @@ -62,6 +63,8 @@ public class ClipboardListenerTest extends SysuiTestCase {
@Mock
private ClipboardManager mClipboardManager;
@Mock
private KeyguardManager mKeyguardManager;
@Mock
private ClipboardOverlayController mOverlayController;
@Mock
private ClipboardToast mClipboardToast;
Expand Down Expand Up @@ -102,7 +105,7 @@ public void setup() {
mFeatureFlags.set(CLIPBOARD_MINIMIZED_LAYOUT, true);

mClipboardListener = new ClipboardListener(getContext(), mOverlayControllerProvider,
mClipboardToast, mClipboardManager, mFeatureFlags, mUiEventLogger);
mClipboardToast, mClipboardManager, mKeyguardManager, mFeatureFlags, mUiEventLogger);
}


Expand Down Expand Up @@ -252,4 +255,44 @@ public void test_minimizedLayoutFlagOn_usesNew() {
assertEquals(mSampleClipData, mClipDataCaptor.getValue());
assertEquals(mSampleSource, mStringCaptor.getValue());
}

@Test
public void test_deviceLocked_showsToast() {
when(mKeyguardManager.isDeviceLocked()).thenReturn(true);

mClipboardListener.start();
mClipboardListener.onPrimaryClipChanged();

verify(mUiEventLogger, times(1)).log(
ClipboardOverlayEvent.CLIPBOARD_TOAST_SHOWN, 0, mSampleSource);
verify(mClipboardToast, times(1)).showCopiedToast();
verifyZeroInteractions(mClipboardOverlayControllerFactory);
}

@Test
public void test_nullClipData_showsNothing() {
when(mClipboardManager.getPrimaryClip()).thenReturn(null);

mClipboardListener.start();
mClipboardListener.onPrimaryClipChanged();

verifyZeroInteractions(mUiEventLogger);
verifyZeroInteractions(mClipboardToast);
verifyZeroInteractions(mClipboardOverlayControllerFactory);
}

@Test
public void test_emptyClipData_showsToast() {
ClipDescription description = new ClipDescription("Test", new String[0]);
ClipData noItems = new ClipData(description, new ArrayList<>());
when(mClipboardManager.getPrimaryClip()).thenReturn(noItems);

mClipboardListener.start();
mClipboardListener.onPrimaryClipChanged();

verify(mUiEventLogger, times(1)).log(
ClipboardOverlayEvent.CLIPBOARD_TOAST_SHOWN, 0, mSampleSource);
verify(mClipboardToast, times(1)).showCopiedToast();
verifyZeroInteractions(mClipboardOverlayControllerFactory);
}
}

0 comments on commit 8e54079

Please sign in to comment.