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

Commit 3a05f900 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix Activity leaking form OnBackInvokedCallbackWrapper" into tm-dev

parents 0616082f 3e966e29
Loading
Loading
Loading
Loading
+21 −25
Original line number Diff line number Diff line
@@ -76,9 +76,9 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {

    /** Detaches the dispatcher instance from its window. */
    public void detachFromWindow() {
        clear();
        mWindow = null;
        mWindowSession = null;
        clear();
    }

    // TODO: Take an Executor for the callback to run on.
@@ -165,21 +165,32 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
            } else {
                int priority = mAllCallbacks.get(callback);
                mWindowSession.setOnBackInvokedCallback(
                        mWindow, new OnBackInvokedCallbackWrapper(callback, priority), priority);
                        mWindow, new OnBackInvokedCallbackWrapper(callback), priority);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to set OnBackInvokedCallback to WM. Error: " + e);
        }
    }

    private class OnBackInvokedCallbackWrapper extends IOnBackInvokedCallback.Stub {
    @Override
    public OnBackInvokedCallback getTopCallback() {
        if (mAllCallbacks.isEmpty()) {
            return null;
        }
        for (Integer priority : mOnBackInvokedCallbacks.descendingKeySet()) {
            ArrayList<OnBackInvokedCallback> callbacks = mOnBackInvokedCallbacks.get(priority);
            if (!callbacks.isEmpty()) {
                return callbacks.get(callbacks.size() - 1);
            }
        }
        return null;
    }

    private static class OnBackInvokedCallbackWrapper extends IOnBackInvokedCallback.Stub {
        private final OnBackInvokedCallback mCallback;
        private final @Priority int mPriority;

        OnBackInvokedCallbackWrapper(
                @NonNull OnBackInvokedCallback callback, @Priority int priority) {
        OnBackInvokedCallbackWrapper(@NonNull OnBackInvokedCallback callback) {
            mCallback = callback;
            mPriority = priority;
        }

        @NonNull
@@ -188,18 +199,17 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
        }

        @Override
        public void onBackStarted() throws RemoteException {
        public void onBackStarted() {
            Handler.getMain().post(() -> mCallback.onBackStarted());
        }

        @Override
        public void onBackProgressed(BackEvent backEvent)
                throws RemoteException {
        public void onBackProgressed(BackEvent backEvent) {
            Handler.getMain().post(() -> mCallback.onBackProgressed(backEvent));
        }

        @Override
        public void onBackCancelled() throws RemoteException {
        public void onBackCancelled() {
            Handler.getMain().post(() -> mCallback.onBackCancelled());
        }

@@ -209,20 +219,6 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
        }
    }

    @Override
    public OnBackInvokedCallback getTopCallback() {
        if (mAllCallbacks.isEmpty()) {
            return null;
        }
        for (Integer priority : mOnBackInvokedCallbacks.descendingKeySet()) {
            ArrayList<OnBackInvokedCallback> callbacks = mOnBackInvokedCallbacks.get(priority);
            if (!callbacks.isEmpty()) {
                return callbacks.get(callbacks.size() - 1);
            }
        }
        return null;
    }

    /**
     * Returns if the legacy back behavior should be used.
     *