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

Commit 0f0e910e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix the clipboard access toast logic from VirtualDevices." into main

parents b66255e9 de7cca06
Loading
Loading
Loading
Loading
+40 −24
Original line number Diff line number Diff line
@@ -684,7 +684,8 @@ public class ClipboardService extends SystemService {
                if (clipboard == null) {
                    return null;
                }
                showAccessNotificationLocked(pkg, intendingUid, intendingUserId, clipboard);
                showAccessNotificationLocked(
                        pkg, intendingUid, intendingUserId, clipboard, deviceId);
                notifyTextClassifierLocked(clipboard, pkg, intendingUid);
                if (clipboard.primaryClip != null) {
                    scheduleAutoClear(userId, intendingUid, intendingDeviceId);
@@ -1438,7 +1439,7 @@ public class ClipboardService extends SystemService {
     */
    @GuardedBy("mLock")
    private void showAccessNotificationLocked(String callingPackage, int uid, @UserIdInt int userId,
            Clipboard clipboard) {
            Clipboard clipboard, int accessDeviceId) {
        if (clipboard.primaryClip == null) {
            return;
        }
@@ -1477,7 +1478,7 @@ public class ClipboardService extends SystemService {
            return;
        }

        final ArraySet<Context> toastContexts = getToastContexts(clipboard);
        final ArraySet<Context> toastContexts = getToastContexts(clipboard, accessDeviceId);
        Binder.withCleanCallingIdentity(() -> {
            try {
                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
     * 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.
     *
     * 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<>();
        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) {
            DisplayManager displayManager = getContext().getSystemService(DisplayManager.class);
        if ((accessDeviceId == DEVICE_ID_DEFAULT && clipboard.deviceId == DEVICE_ID_DEFAULT)
                || 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();
            ArraySet<Integer> displayIds = mVdmInternal.getDisplayIdsForDevice(clipboard.deviceId);

        if (displayIds.contains(topFocusedDisplayId)) {
            Display display = displayManager.getDisplay(topFocusedDisplayId);
@@ -1540,16 +1558,14 @@ public class ClipboardService extends SystemService {
                contexts.add(getContext().createDisplayContext(display));
            }
        }
            if (!contexts.isEmpty()) {
                return contexts;
            }
        if (contexts.isEmpty()) {
            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
            // the default display below.
            contexts.add(getContext());
        }

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