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

Commit 3e966e29 authored by Vadim Caen's avatar Vadim Caen
Browse files

Fix Activity leaking form OnBackInvokedCallbackWrapper

 - The callback was not removed on the server in the clear() method since mWindow and
   mWindowSession fields were cleared before
 - The wrapper class was not static, risking to leak it's outer class
   and its field.

Test: com.android.launcher3.ui.widget.RequestPinItemTest#testPinWidgetWithConfig
Fixes: 220385755
Change-Id: I3e5a81bf48c2272b1cb2b5a9f2ba940d0f794a6f
parent e4590dbf
Loading
Loading
Loading
Loading
+21 −25
Original line number Original line Diff line number Diff line
@@ -76,9 +76,9 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {


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


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


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


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


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


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


        @Override
        @Override
        public void onBackCancelled() throws RemoteException {
        public void onBackCancelled() {
            Handler.getMain().post(() -> mCallback.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.
     * Returns if the legacy back behavior should be used.
     *
     *