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

Commit 7234187c authored by Xiaowen Lei's avatar Xiaowen Lei
Browse files

Removes complication in onMediaDataLoaded if hasActiveMedia is false.

With this change, the complication is removed for the following cases:
  1. When the media app is removed from Recents screen.
     - Notes:
       - This doesn't trigger onMediaDataRemoved, but it causes the
       media to become inactive.
       - This also causes the mediaHost view to become invisible.
  2. When the user swipes to dismiss the UMO on Keyguard.
     - This also causes the media to become inactive.

Bug: 242613318
Test: manually on device
Test: atest MediaDreamSentinelTest
Change-Id: Iedd7ed264e62306b2e34f6f798259176591039ec
parent 486626b5
Loading
Loading
Loading
Loading
+25 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.media.dream;
import static com.android.systemui.flags.Flags.MEDIA_DREAM_COMPLICATION;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -38,6 +39,9 @@ import javax.inject.Inject;
 * the media complication as appropriate
 */
public class MediaDreamSentinel extends CoreStartable {
    private static final String TAG = "MediaDreamSentinel";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private final MediaDataManager.Listener mListener = new MediaDataManager.Listener() {
        private boolean mAdded;
        @Override
@@ -46,11 +50,17 @@ public class MediaDreamSentinel extends CoreStartable {

        @Override
        public void onMediaDataRemoved(@NonNull String key) {
            final boolean hasActiveMedia = mMediaDataManager.hasActiveMedia();
            if (DEBUG) {
                Log.d(TAG, "onMediaDataRemoved(" + key + "), mAdded=" + mAdded + ", hasActiveMedia="
                        + hasActiveMedia);
            }

            if (!mAdded) {
                return;
            }

            if (mMediaDataManager.hasActiveMedia()) {
            if (hasActiveMedia) {
                return;
            }

@@ -71,11 +81,24 @@ public class MediaDreamSentinel extends CoreStartable {
                return;
            }

            final boolean hasActiveMedia = mMediaDataManager.hasActiveMedia();
            if (DEBUG) {
                Log.d(TAG, "onMediaDataLoaded(" + key + "), mAdded=" + mAdded + ", hasActiveMedia="
                        + hasActiveMedia);
            }

            // Media data can become inactive without triggering onMediaDataRemoved.
            if (mAdded && !hasActiveMedia) {
                mAdded = false;
                mDreamOverlayStateController.removeComplication(mMediaEntryComplication);
                return;
            }

            if (mAdded) {
                return;
            }

            if (!mMediaDataManager.hasActiveMedia()) {
            if (!hasActiveMedia) {
                return;
            }

+39 −3
Original line number Diff line number Diff line
@@ -72,13 +72,13 @@ public class MediaDreamSentinelTest extends SysuiTestCase {
    }

    @Test
    public void testComplicationAddition() {
    public void testOnMediaDataLoaded_complicationAddition() {
        final MediaDreamSentinel sentinel = new MediaDreamSentinel(mContext, mMediaDataManager,
                mDreamOverlayStateController, mMediaEntryComplication, mFeatureFlags);

        sentinel.start();

        final MediaDataManager.Listener listener = captureMediaDataListener();

        when(mMediaDataManager.hasActiveMedia()).thenReturn(false);
        listener.onMediaDataLoaded(mKey, mOldKey, mData, /* immediately= */ true,
                /* receivedSmartspaceCardLatency= */ 0, /* isSsReactived= */ false);
@@ -90,6 +90,19 @@ public class MediaDreamSentinelTest extends SysuiTestCase {
        verify(mDreamOverlayStateController).addComplication(eq(mMediaEntryComplication));
        verify(mDreamOverlayStateController, never()).addComplication(
                not(eq(mMediaEntryComplication)));
    }

    @Test
    public void testOnMediaDataRemoved_complicationRemoval() {
        final MediaDreamSentinel sentinel = new MediaDreamSentinel(mContext, mMediaDataManager,
                mDreamOverlayStateController, mMediaEntryComplication, mFeatureFlags);
        sentinel.start();

        final MediaDataManager.Listener listener = captureMediaDataListener();

        when(mMediaDataManager.hasActiveMedia()).thenReturn(true);
        listener.onMediaDataLoaded(mKey, mOldKey, mData, /* immediately= */true,
                /* receivedSmartspaceCardLatency= */0, /* isSsReactived= */ false);

        listener.onMediaDataRemoved(mKey);
        verify(mDreamOverlayStateController, never()).removeComplication(any());
@@ -100,7 +113,30 @@ public class MediaDreamSentinelTest extends SysuiTestCase {
    }

    @Test
    public void testMediaDreamSentinel_mediaComplicationDisabled_doNotAddComplication() {
    public void testOnMediaDataLoaded_complicationRemoval() {
        final MediaDreamSentinel sentinel = new MediaDreamSentinel(mContext, mMediaDataManager,
                mDreamOverlayStateController, mMediaEntryComplication, mFeatureFlags);
        sentinel.start();

        final MediaDataManager.Listener listener = captureMediaDataListener();

        when(mMediaDataManager.hasActiveMedia()).thenReturn(true);
        listener.onMediaDataLoaded(mKey, mOldKey, mData, /* immediately= */true,
                /* receivedSmartspaceCardLatency= */0, /* isSsReactived= */ false);
        verify(mDreamOverlayStateController, never()).removeComplication(any());

        listener.onMediaDataLoaded(mKey, mOldKey, mData, /* immediately= */true,
                /* receivedSmartspaceCardLatency= */0, /* isSsReactived= */ false);
        verify(mDreamOverlayStateController, never()).removeComplication(any());

        when(mMediaDataManager.hasActiveMedia()).thenReturn(false);
        listener.onMediaDataLoaded(mKey, mOldKey, mData, /* immediately= */true,
                /* receivedSmartspaceCardLatency= */0, /* isSsReactived= */ false);
        verify(mDreamOverlayStateController).removeComplication(eq(mMediaEntryComplication));
    }

    @Test
    public void testOnMediaDataLoaded_mediaComplicationDisabled_doesNotAddComplication() {
        when(mFeatureFlags.isEnabled(MEDIA_DREAM_COMPLICATION)).thenReturn(false);

        final MediaDreamSentinel sentinel = new MediaDreamSentinel(mContext, mMediaDataManager,