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

Commit 0aa0f4ac authored by Bryce Lee's avatar Bryce Lee
Browse files

Remove Dream Overlay when Dream Window detaches.

This changelist removes the dream overlay window when the associated
dream window is removed. This follows closely to the attach behavior,
where the dream overlay window is added after the dream window is
attached. Previously, the dream overlay window was removed during
onDestroy. Deferring removal to this later point in the lifecycle
allows for the window to be incorrectly acted upon after the associated
dream is gone.

This changelist also implements a default implementation of
DreamOverlayService#onWakeUp which immediately runs the callback. It
also addresses an issue where service connection consumers were not
cleared in the overlay service connection.

Fixes: 261913526
Test: atest DreamOverlayServiceTest
Test: atest DreamOverlayTest
Change-Id: I4c80b585a484c20a8282fca75bd1908b494ce928
parent 16a8a796
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -2412,6 +2412,7 @@ package android.service.dreams {
  public abstract class DreamOverlayService extends android.app.Service {
  public abstract class DreamOverlayService extends android.app.Service {
    ctor public DreamOverlayService();
    ctor public DreamOverlayService();
    method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent);
    method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent);
    method public void onEndDream();
    method public abstract void onStartDream(@NonNull android.view.WindowManager.LayoutParams);
    method public abstract void onStartDream(@NonNull android.view.WindowManager.LayoutParams);
    method public final void requestExit();
    method public final void requestExit();
    method public final boolean shouldShowComplications();
    method public final boolean shouldShowComplications();
+15 −1
Original line number Original line Diff line number Diff line
@@ -50,6 +50,11 @@ public abstract class DreamOverlayService extends Service {
            onStartDream(layoutParams);
            onStartDream(layoutParams);
        }
        }


        @Override
        public void endDream() {
            onEndDream();
        }

        @Override
        @Override
        public void wakeUp() {
        public void wakeUp() {
            onWakeUp(() -> {
            onWakeUp(() -> {
@@ -83,13 +88,22 @@ public abstract class DreamOverlayService extends Service {


    /**
    /**
     * This method is overridden by implementations to handle when the dream has been requested
     * This method is overridden by implementations to handle when the dream has been requested
     * to wakeup. This allows any overlay animations to run.
     * to wakeup. This allows any overlay animations to run. By default, the method will invoke
     * the callback immediately.
     *
     *
     * @param onCompleteCallback The callback to trigger to notify the dream service that the
     * @param onCompleteCallback The callback to trigger to notify the dream service that the
     *                           overlay has completed waking up.
     *                           overlay has completed waking up.
     * @hide
     * @hide
     */
     */
    public void onWakeUp(@NonNull Runnable onCompleteCallback) {
    public void onWakeUp(@NonNull Runnable onCompleteCallback) {
        onCompleteCallback.run();
    }

    /**
     * This method is overridden by implementations to handle when the dream has ended. There may
     * be earlier signals leading up to this step, such as @{@link #onWakeUp(Runnable)}.
     */
    public void onEndDream() {
    }
    }


    /**
    /**
+22 −8
Original line number Original line Diff line number Diff line
@@ -297,14 +297,20 @@ public class DreamService extends Service implements Window.Callback {
        }
        }


        public void addConsumer(Consumer<IDreamOverlay> consumer) {
        public void addConsumer(Consumer<IDreamOverlay> consumer) {
            execute(() -> {
                mConsumers.add(consumer);
                mConsumers.add(consumer);
                if (mOverlay != null) {
                if (mOverlay != null) {
                    consumer.accept(mOverlay);
                    consumer.accept(mOverlay);
                }
                }
            });
        }
        }


        public void removeConsumer(Consumer<IDreamOverlay> consumer) {
        public void removeConsumer(Consumer<IDreamOverlay> consumer) {
            mConsumers.remove(consumer);
            execute(() -> mConsumers.remove(consumer));
        }

        public void clearConsumers() {
            execute(() -> mConsumers.clear());
        }
        }
    }
    }


@@ -1383,6 +1389,17 @@ public class DreamService extends Service implements Window.Callback {


                    @Override
                    @Override
                    public void onViewDetachedFromWindow(View v) {
                    public void onViewDetachedFromWindow(View v) {
                        if (mOverlayConnection != null) {
                            mOverlayConnection.addConsumer(overlay -> {
                                try {
                                    overlay.endDream();
                                } catch (RemoteException e) {
                                    Log.e(mTag, "could not inform overlay of dream end:" + e);
                                }
                            });
                            mOverlayConnection.clearConsumers();
                        }

                        if (mActivity == null || !mActivity.isChangingConfigurations()) {
                        if (mActivity == null || !mActivity.isChangingConfigurations()) {
                            // Only stop the dream if the view is not detached by relaunching
                            // Only stop the dream if the view is not detached by relaunching
                            // activity for configuration changes. It is important to also clear
                            // activity for configuration changes. It is important to also clear
@@ -1391,9 +1408,6 @@ public class DreamService extends Service implements Window.Callback {
                            mActivity = null;
                            mActivity = null;
                            finish();
                            finish();
                        }
                        }
                        if (mOverlayConnection != null && mDreamStartOverlayConsumer != null) {
                            mOverlayConnection.removeConsumer(mDreamStartOverlayConsumer);
                        }
                    }
                    }
                });
                });
    }
    }
+3 −0
Original line number Original line Diff line number Diff line
@@ -41,4 +41,7 @@ interface IDreamOverlay {


    /** Called when the dream is waking, to do any exit animations */
    /** Called when the dream is waking, to do any exit animations */
    void wakeUp();
    void wakeUp();

    /** Called when the dream has ended. */
    void endDream();
}
}
+7 −0
Original line number Original line Diff line number Diff line
@@ -164,6 +164,13 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
        mFlags = flags;
        mFlags = flags;
    }
    }


    /**
     * Executes code on the executor specified at construction.
     */
    public void execute(Runnable runnable) {
        mExecutor.execute(runnable);
    }

    /**
    /**
     * Initiate binding to the service.
     * Initiate binding to the service.
     *
     *
Loading