Merge "Unregister MediaListener on view detached" into rvc-dev am: 61b5936007 am: 79e63a675e am: 79248a1749 am: bdd4df80b6
Change-Id: I76a6190cbd9c0f26e2d8fd69ea237946025a1ce0
This commit is contained in:
@@ -37,6 +37,7 @@ import android.util.Log;
|
|||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.View.OnAttachStateChangeListener;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
@@ -53,6 +54,8 @@ import com.android.systemui.Dependency;
|
|||||||
import com.android.systemui.R;
|
import com.android.systemui.R;
|
||||||
import com.android.systemui.plugins.ActivityStarter;
|
import com.android.systemui.plugins.ActivityStarter;
|
||||||
import com.android.systemui.statusbar.NotificationMediaManager;
|
import com.android.systemui.statusbar.NotificationMediaManager;
|
||||||
|
import com.android.systemui.statusbar.NotificationMediaManager.MediaListener;
|
||||||
|
import com.android.systemui.util.Assert;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -60,7 +63,7 @@ import java.util.concurrent.Executor;
|
|||||||
/**
|
/**
|
||||||
* Base media control panel for System UI
|
* Base media control panel for System UI
|
||||||
*/
|
*/
|
||||||
public class MediaControlPanel implements NotificationMediaManager.MediaListener {
|
public class MediaControlPanel {
|
||||||
private static final String TAG = "MediaControlPanel";
|
private static final String TAG = "MediaControlPanel";
|
||||||
private final NotificationMediaManager mMediaManager;
|
private final NotificationMediaManager mMediaManager;
|
||||||
private final Executor mForegroundExecutor;
|
private final Executor mForegroundExecutor;
|
||||||
@@ -74,6 +77,7 @@ public class MediaControlPanel implements NotificationMediaManager.MediaListener
|
|||||||
private int mForegroundColor;
|
private int mForegroundColor;
|
||||||
private int mBackgroundColor;
|
private int mBackgroundColor;
|
||||||
protected ComponentName mRecvComponent;
|
protected ComponentName mRecvComponent;
|
||||||
|
private boolean mIsRegistered = false;
|
||||||
|
|
||||||
private final int[] mActionIds;
|
private final int[] mActionIds;
|
||||||
|
|
||||||
@@ -86,12 +90,34 @@ public class MediaControlPanel implements NotificationMediaManager.MediaListener
|
|||||||
com.android.internal.R.id.action4
|
com.android.internal.R.id.action4
|
||||||
};
|
};
|
||||||
|
|
||||||
private MediaController.Callback mSessionCallback = new MediaController.Callback() {
|
private final MediaController.Callback mSessionCallback = new MediaController.Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void onSessionDestroyed() {
|
public void onSessionDestroyed() {
|
||||||
Log.d(TAG, "session destroyed");
|
Log.d(TAG, "session destroyed");
|
||||||
mController.unregisterCallback(mSessionCallback);
|
mController.unregisterCallback(mSessionCallback);
|
||||||
clearControls();
|
clearControls();
|
||||||
|
makeInactive();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final MediaListener mMediaListener = new MediaListener() {
|
||||||
|
@Override
|
||||||
|
public void onMetadataOrStateChanged(MediaMetadata metadata, int state) {
|
||||||
|
if (state == PlaybackState.STATE_NONE) {
|
||||||
|
clearControls();
|
||||||
|
makeInactive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onViewAttachedToWindow(View unused) {
|
||||||
|
makeActive();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onViewDetachedFromWindow(View unused) {
|
||||||
|
makeInactive();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -111,6 +137,12 @@ public class MediaControlPanel implements NotificationMediaManager.MediaListener
|
|||||||
mContext = context;
|
mContext = context;
|
||||||
LayoutInflater inflater = LayoutInflater.from(mContext);
|
LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||||
mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false);
|
mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false);
|
||||||
|
// TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views
|
||||||
|
// mStateListener shouldn't need to be unregistered since this object shares the same
|
||||||
|
// lifecycle with the inflated view. It would be better, however, if this controller used an
|
||||||
|
// attach/detach of views instead of inflating them in the constructor, which would allow
|
||||||
|
// mStateListener to be unregistered in detach.
|
||||||
|
mMediaNotifView.addOnAttachStateChangeListener(mStateListener);
|
||||||
mMediaManager = manager;
|
mMediaManager = manager;
|
||||||
mActionIds = actionIds;
|
mActionIds = actionIds;
|
||||||
mForegroundExecutor = foregroundExecutor;
|
mForegroundExecutor = foregroundExecutor;
|
||||||
@@ -236,9 +268,7 @@ public class MediaControlPanel implements NotificationMediaManager.MediaListener
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure is only added once
|
makeActive();
|
||||||
mMediaManager.removeCallback(this);
|
|
||||||
mMediaManager.addCallback(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -422,11 +452,20 @@ public class MediaControlPanel implements NotificationMediaManager.MediaListener
|
|||||||
btn.setVisibility(View.VISIBLE);
|
btn.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void makeActive() {
|
||||||
public void onMetadataOrStateChanged(MediaMetadata metadata, int state) {
|
Assert.isMainThread();
|
||||||
if (state == PlaybackState.STATE_NONE) {
|
if (!mIsRegistered) {
|
||||||
clearControls();
|
mMediaManager.addCallback(mMediaListener);
|
||||||
mMediaManager.removeCallback(this);
|
mIsRegistered = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void makeInactive() {
|
||||||
|
Assert.isMainThread();
|
||||||
|
if (mIsRegistered) {
|
||||||
|
mMediaManager.removeCallback(mMediaListener);
|
||||||
|
mIsRegistered = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user