Hide media on AOD when not playing
Media seems stuck on AOD if paused or stopped and still visible.
Test: play pause with bluetooth headphones
Test: play pause on lock screen
Test: skip songs on lock screen, go to aod
Fixes: 131314881
Change-Id: I3574bbf628b708d2f9d4c4363a912de5ded951ac
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardSliceProvider.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardSliceProvider.java
index 4590470..35b8d203 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardSliceProvider.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardSliceProvider.java
@@ -30,6 +30,7 @@
import android.icu.text.DateFormat;
import android.icu.text.DisplayContext;
import android.media.MediaMetadata;
+import android.media.session.PlaybackState;
import android.net.Uri;
import android.os.Handler;
import android.os.Trace;
@@ -57,6 +58,7 @@
import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
import java.util.Date;
+import java.util.HashSet;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
@@ -98,6 +100,7 @@
private final Date mCurrentTime = new Date();
private final Handler mHandler;
private final AlarmManager.OnAlarmListener mUpdateNextAlarm = this::updateNextAlarm;
+ private final HashSet<Integer> mMediaInvisibleStates;
private ZenModeController mZenModeController;
private String mDatePattern;
private DateFormat mDateFormat;
@@ -113,6 +116,7 @@
private StatusBarStateController mStatusBarStateController;
protected MediaMetadata mMediaMetaData;
protected boolean mDozing;
+ private boolean mMediaIsVisible;
/**
* Receiver responsible for time ticking and updating the date format.
@@ -169,6 +173,11 @@
mAlarmUri = Uri.parse(KEYGUARD_NEXT_ALARM_URI);
mDndUri = Uri.parse(KEYGUARD_DND_URI);
mMediaUri = Uri.parse(KEYGUARD_MEDIA_URI);
+
+ mMediaInvisibleStates = new HashSet<>();
+ mMediaInvisibleStates.add(PlaybackState.STATE_NONE);
+ mMediaInvisibleStates.add(PlaybackState.STATE_STOPPED);
+ mMediaInvisibleStates.add(PlaybackState.STATE_PAUSED);
}
/**
@@ -209,31 +218,33 @@
}
protected boolean needsMediaLocked() {
- return mMediaMetaData != null && mDozing;
+ return mMediaMetaData != null && mMediaIsVisible && mDozing;
}
protected void addMediaLocked(ListBuilder listBuilder) {
- if (mMediaMetaData != null) {
- CharSequence title = mMediaMetaData.getText(MediaMetadata.METADATA_KEY_TITLE);
- if (TextUtils.isEmpty(title)) {
- title = getContext().getResources().getString(R.string.music_controls_no_title);
+ if (mMediaMetaData == null) {
+ return;
+ }
+
+ CharSequence title = mMediaMetaData.getText(MediaMetadata.METADATA_KEY_TITLE);
+ if (TextUtils.isEmpty(title)) {
+ title = getContext().getResources().getString(R.string.music_controls_no_title);
+ }
+ listBuilder.setHeader(new ListBuilder.HeaderBuilder(mHeaderUri).setTitle(title));
+
+ CharSequence album = mMediaMetaData.getText(MediaMetadata.METADATA_KEY_ARTIST);
+ if (!TextUtils.isEmpty(album)) {
+ RowBuilder albumBuilder = new RowBuilder(mMediaUri);
+ albumBuilder.setTitle(album);
+
+ Icon mediaIcon = mMediaManager == null ? null : mMediaManager.getMediaIcon();
+ IconCompat mediaIconCompat = mediaIcon == null ? null
+ : IconCompat.createFromIcon(getContext(), mediaIcon);
+ if (mediaIconCompat != null) {
+ albumBuilder.addEndItem(mediaIconCompat, ListBuilder.ICON_IMAGE);
}
- listBuilder.setHeader(new ListBuilder.HeaderBuilder(mHeaderUri).setTitle(title));
- CharSequence album = mMediaMetaData.getText(MediaMetadata.METADATA_KEY_ARTIST);
- if (!TextUtils.isEmpty(album)) {
- RowBuilder albumBuilder = new RowBuilder(mMediaUri);
- albumBuilder.setTitle(album);
-
- Icon mediaIcon = mMediaManager == null ? null : mMediaManager.getMediaIcon();
- IconCompat mediaIconCompat = mediaIcon == null ? null
- : IconCompat.createFromIcon(getContext(), mediaIcon);
- if (mediaIconCompat != null) {
- albumBuilder.addEndItem(mediaIconCompat, ListBuilder.ICON_IMAGE);
- }
-
- listBuilder.addRow(albumBuilder);
- }
+ listBuilder.addRow(albumBuilder);
}
}
@@ -411,9 +422,14 @@
* @param metadata New metadata.
*/
@Override
- public void onMetadataChanged(MediaMetadata metadata) {
+ public void onMetadataOrStateChanged(MediaMetadata metadata, @PlaybackState.State int state) {
synchronized (this) {
+ boolean nextVisible = !mMediaInvisibleStates.contains(state);
+ if (nextVisible == mMediaIsVisible && metadata == mMediaMetaData) {
+ return;
+ }
mMediaMetaData = metadata;
+ mMediaIsVisible = nextVisible;
}
notifyChange();
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
index b9e0c60..75ef185 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationMediaManager.java
@@ -150,8 +150,8 @@
if (state != null) {
if (!isPlaybackActive(state.getState())) {
clearCurrentMediaNotification();
- dispatchUpdateMediaMetaData(true /* changed */, true /* allowAnimation */);
}
+ dispatchUpdateMediaMetaData(true /* changed */, true /* allowAnimation */);
}
}
@@ -242,7 +242,8 @@
public void addCallback(MediaListener callback) {
mMediaListeners.add(callback);
- callback.onMetadataChanged(mMediaMetadata);
+ callback.onMetadataOrStateChanged(mMediaMetadata,
+ getMediaControllerPlaybackState(mMediaController));
}
public void removeCallback(MediaListener callback) {
@@ -357,9 +358,10 @@
if (mPresenter != null) {
mPresenter.updateMediaMetaData(changed, allowEnterAnimation);
}
+ @PlaybackState.State int state = getMediaControllerPlaybackState(mMediaController);
ArrayList<MediaListener> callbacks = new ArrayList<>(mMediaListeners);
for (int i = 0; i < callbacks.size(); i++) {
- callbacks.get(i).onMetadataChanged(mMediaMetadata);
+ callbacks.get(i).onMetadataOrStateChanged(mMediaMetadata, state);
}
}
@@ -698,6 +700,12 @@
}
public interface MediaListener {
- void onMetadataChanged(MediaMetadata metadata);
+ /**
+ * Called whenever there's new metadata or playback state.
+ * @param metadata Current metadata.
+ * @param state Current playback state
+ * @see PlaybackState.State
+ */
+ void onMetadataOrStateChanged(MediaMetadata metadata, @PlaybackState.State int state);
}
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardSliceProviderTest.java b/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardSliceProviderTest.java
index c534de7..355e260 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardSliceProviderTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/KeyguardSliceProviderTest.java
@@ -29,6 +29,7 @@
import android.app.AlarmManager;
import android.content.ContentResolver;
import android.media.MediaMetadata;
+import android.media.session.PlaybackState;
import android.net.Uri;
import android.provider.Settings;
import android.testing.AndroidTestingRunner;
@@ -105,7 +106,7 @@
MediaMetadata metadata = mock(MediaMetadata.class);
when(metadata.getText(any())).thenReturn("metadata");
mProvider.onDozingChanged(true);
- mProvider.onMetadataChanged(metadata);
+ mProvider.onMetadataOrStateChanged(metadata, PlaybackState.STATE_PLAYING);
mProvider.onBindSlice(mProvider.getUri());
verify(metadata).getText(eq(MediaMetadata.METADATA_KEY_TITLE));
verify(metadata).getText(eq(MediaMetadata.METADATA_KEY_ARTIST));
@@ -170,7 +171,7 @@
public void onMetadataChanged_updatesSlice() {
mProvider.onDozingChanged(true);
reset(mContentResolver);
- mProvider.onMetadataChanged(mock(MediaMetadata.class));
+ mProvider.onMetadataOrStateChanged(mock(MediaMetadata.class), PlaybackState.STATE_PLAYING);
verify(mContentResolver).notifyChange(eq(mProvider.getUri()), eq(null));
// Hides after waking up
@@ -181,7 +182,7 @@
@Test
public void onDozingChanged_updatesSliceIfMedia() {
- mProvider.onMetadataChanged(mock(MediaMetadata.class));
+ mProvider.onMetadataOrStateChanged(mock(MediaMetadata.class), PlaybackState.STATE_PLAYING);
reset(mContentResolver);
// Show media when dozing
mProvider.onDozingChanged(true);