Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit de7cca06 authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

Fix the clipboard access toast logic from VirtualDevices.

When the clipboard is shared between the default and a virtual device,
the toast only shows on the default display but it needs to be shown
ALSO on the virtual display from where it's being accessed.

Bug: 322774984
Test: manual
Flag: EXEMPT bugfix
Change-Id: I48325e7c3680ad0dd3c443d11fce997608399f50
parent 58dbc145
Loading
Loading
Loading
Loading
+40 −24
Original line number Original line Diff line number Diff line
@@ -684,7 +684,8 @@ public class ClipboardService extends SystemService {
                if (clipboard == null) {
                if (clipboard == null) {
                    return null;
                    return null;
                }
                }
                showAccessNotificationLocked(pkg, intendingUid, intendingUserId, clipboard);
                showAccessNotificationLocked(
                        pkg, intendingUid, intendingUserId, clipboard, deviceId);
                notifyTextClassifierLocked(clipboard, pkg, intendingUid);
                notifyTextClassifierLocked(clipboard, pkg, intendingUid);
                if (clipboard.primaryClip != null) {
                if (clipboard.primaryClip != null) {
                    scheduleAutoClear(userId, intendingUid, intendingDeviceId);
                    scheduleAutoClear(userId, intendingUid, intendingDeviceId);
@@ -1438,7 +1439,7 @@ public class ClipboardService extends SystemService {
     */
     */
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private void showAccessNotificationLocked(String callingPackage, int uid, @UserIdInt int userId,
    private void showAccessNotificationLocked(String callingPackage, int uid, @UserIdInt int userId,
            Clipboard clipboard) {
            Clipboard clipboard, int accessDeviceId) {
        if (clipboard.primaryClip == null) {
        if (clipboard.primaryClip == null) {
            return;
            return;
        }
        }
@@ -1477,7 +1478,7 @@ public class ClipboardService extends SystemService {
            return;
            return;
        }
        }


        final ArraySet<Context> toastContexts = getToastContexts(clipboard);
        final ArraySet<Context> toastContexts = getToastContexts(clipboard, accessDeviceId);
        Binder.withCleanCallingIdentity(() -> {
        Binder.withCleanCallingIdentity(() -> {
            try {
            try {
                CharSequence callingAppLabel = mPm.getApplicationLabel(
                CharSequence callingAppLabel = mPm.getApplicationLabel(
@@ -1516,15 +1517,32 @@ public class ClipboardService extends SystemService {
     * If the clipboard is for a VirtualDevice, we attempt to return the single DisplayContext for
     * If the clipboard is for a VirtualDevice, we attempt to return the single DisplayContext for
     * the focused VirtualDisplay for that device, but might need to return the contexts for
     * the focused VirtualDisplay for that device, but might need to return the contexts for
     * multiple displays if the VirtualDevice has several but none of them were focused.
     * multiple displays if the VirtualDevice has several but none of them were focused.
     *
     * If the clipboard is NOT for a VirtualDevice, but it's being accessed from a VirtualDevice,
     * this means that the clipboard is shared between the default and that device. In this case we
     * need to show a toast in both places.
     */
     */
    private ArraySet<Context> getToastContexts(Clipboard clipboard) throws IllegalStateException {
    private ArraySet<Context> getToastContexts(Clipboard clipboard, int accessDeviceId)
            throws IllegalStateException {
        ArraySet<Context> contexts = new ArraySet<>();
        ArraySet<Context> contexts = new ArraySet<>();
        if (clipboard.deviceId == DEVICE_ID_DEFAULT || accessDeviceId == DEVICE_ID_DEFAULT) {
            // Always show the toast on the default display when the default clipboard is accessed -
            // also when the clipboard is shared with a virtual device and accessed from there.
            // Same when any clipboard is accessed from the default device.
            contexts.add(getContext());
        }


        if (mVdmInternal != null && clipboard.deviceId != DEVICE_ID_DEFAULT) {
        if ((accessDeviceId == DEVICE_ID_DEFAULT && clipboard.deviceId == DEVICE_ID_DEFAULT)
            DisplayManager displayManager = getContext().getSystemService(DisplayManager.class);
                || mVdmInternal == null) {
            // No virtual devices involved.
            return contexts;
        }


        // At this point the clipboard is either accessed from a virtual device, or it is a virtual
        // device clipboard, so show a toast on the relevant virtual display(s).
        DisplayManager displayManager = getContext().getSystemService(DisplayManager.class);
        ArraySet<Integer> displayIds = mVdmInternal.getDisplayIdsForDevice(accessDeviceId);
        int topFocusedDisplayId = mWm.getTopFocusedDisplayId();
        int topFocusedDisplayId = mWm.getTopFocusedDisplayId();
            ArraySet<Integer> displayIds = mVdmInternal.getDisplayIdsForDevice(clipboard.deviceId);


        if (displayIds.contains(topFocusedDisplayId)) {
        if (displayIds.contains(topFocusedDisplayId)) {
            Display display = displayManager.getDisplay(topFocusedDisplayId);
            Display display = displayManager.getDisplay(topFocusedDisplayId);
@@ -1540,16 +1558,14 @@ public class ClipboardService extends SystemService {
                contexts.add(getContext().createDisplayContext(display));
                contexts.add(getContext().createDisplayContext(display));
            }
            }
        }
        }
            if (!contexts.isEmpty()) {
        if (contexts.isEmpty()) {
                return contexts;
            }
            Slog.e(TAG, "getToastContexts Couldn't find any VirtualDisplays for VirtualDevice "
            Slog.e(TAG, "getToastContexts Couldn't find any VirtualDisplays for VirtualDevice "
                    + clipboard.deviceId);
                    + accessDeviceId);
            // Since we couldn't find any VirtualDisplays to use at all, just fall through to using
            // Since we couldn't find any VirtualDisplays to use at all, just fall through to using
            // the default display below.
            // the default display below.
            contexts.add(getContext());
        }
        }


        contexts.add(getContext());
        return contexts;
        return contexts;
    }
    }