Make MediaSessionStub static to enable garbage collection
Also remove some redundant throws clauses and make some
non-functional simplifications.
Bug: 37137738
Test: ./gradlew :media:media:connectedDebugAndroidTest
Change-Id: Iae0c182ff27115cfa5184304623ea157556722f5
diff --git a/media/media/src/main/java/android/support/v4/media/session/MediaSessionCompat.java b/media/media/src/main/java/android/support/v4/media/session/MediaSessionCompat.java
index c5d9336..e15d3d6 100644
--- a/media/media/src/main/java/android/support/v4/media/session/MediaSessionCompat.java
+++ b/media/media/src/main/java/android/support/v4/media/session/MediaSessionCompat.java
@@ -89,6 +89,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
/**
* Allows interaction with media controllers, volume keys, media buttons, and
@@ -2398,9 +2399,7 @@
private final PendingIntent mMediaButtonReceiverIntent;
private final MediaSessionStub mStub;
private final Token mToken;
- final String mPackageName;
final Bundle mSessionInfo;
- final String mTag;
final AudioManager mAudioManager;
final RemoteControlClient mRcc;
@@ -2455,13 +2454,13 @@
"MediaButtonReceiver component may not be null");
}
mContext = context;
- mPackageName = context.getPackageName();
mSessionInfo = sessionInfo;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
- mTag = tag;
mMediaButtonReceiverComponentName = mbrComponent;
mMediaButtonReceiverIntent = mbrIntent;
- mStub = new MediaSessionStub();
+ mStub =
+ new MediaSessionStub(
+ /* mediaSessionImpl= */ this, context.getPackageName(), tag);
mToken = new Token(mStub, /* extraBinder= */ null, session2Token);
mRatingType = RatingCompat.RATING_NONE;
@@ -3103,7 +3102,19 @@
}
}
- class MediaSessionStub extends IMediaSession.Stub {
+ static class MediaSessionStub extends IMediaSession.Stub {
+
+ private final AtomicReference<MediaSessionImplBase> mMediaSessionImplRef;
+ private final String mPackageName;
+ private final String mTag;
+
+ MediaSessionStub(
+ MediaSessionImplBase mediaSessionImpl, String packageName, String tag) {
+ mMediaSessionImplRef = new AtomicReference<>(mediaSessionImpl);
+ mPackageName = packageName;
+ mTag = tag;
+ }
+
@Override
public void sendCommand(String command, Bundle args, ResultReceiverWrapper cb) {
postToHandler(MessageHandler.MSG_COMMAND,
@@ -3120,7 +3131,8 @@
public void registerCallbackListener(IMediaControllerCallback cb) {
// If this session is already destroyed tell the caller and
// don't add them.
- if (mDestroyed) {
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
try {
cb.onSessionDestroyed();
} catch (Exception e) {
@@ -3130,13 +3142,16 @@
}
int callingPid = Binder.getCallingPid();
int callingUid = Binder.getCallingUid();
- RemoteUserInfo info = new RemoteUserInfo(
- getPackageNameForUid(callingUid), callingPid, callingUid);
- mControllerCallbacks.register(cb, info);
+ RemoteUserInfo info =
+ new RemoteUserInfo(
+ mediaSessionImpl.getPackageNameForUid(callingUid),
+ callingPid,
+ callingUid);
+ mediaSessionImpl.mControllerCallbacks.register(cb, info);
- synchronized (mLock) {
- if (mRegistrationCallbackHandler != null) {
- mRegistrationCallbackHandler.postCallbackRegistered(
+ synchronized (mediaSessionImpl.mLock) {
+ if (mediaSessionImpl.mRegistrationCallbackHandler != null) {
+ mediaSessionImpl.mRegistrationCallbackHandler.postCallbackRegistered(
callingPid, callingUid);
}
}
@@ -3144,13 +3159,17 @@
@Override
public void unregisterCallbackListener(IMediaControllerCallback cb) {
- mControllerCallbacks.unregister(cb);
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return;
+ }
+ mediaSessionImpl.mControllerCallbacks.unregister(cb);
int callingPid = Binder.getCallingPid();
int callingUid = Binder.getCallingUid();
- synchronized (mLock) {
- if (mRegistrationCallbackHandler != null) {
- mRegistrationCallbackHandler.postCallbackUnregistered(
+ synchronized (mediaSessionImpl.mLock) {
+ if (mediaSessionImpl.mRegistrationCallbackHandler != null) {
+ mediaSessionImpl.mRegistrationCallbackHandler.postCallbackUnregistered(
callingPid, callingUid);
}
}
@@ -3158,14 +3177,16 @@
@Override
public String getPackageName() {
- // mPackageName is final so doesn't need synchronize block
return mPackageName;
}
@Override
public Bundle getSessionInfo() {
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
// mSessionInfo is final so doesn't need synchronize block
- return mSessionInfo == null ? null : new Bundle(mSessionInfo);
+ return mediaSessionImpl != null && mediaSessionImpl.mSessionInfo != null
+ ? new Bundle(mediaSessionImpl.mSessionInfo)
+ : null;
}
@Override
@@ -3176,51 +3197,67 @@
@Override
public PendingIntent getLaunchPendingIntent() {
- synchronized (mLock) {
- return mSessionActivity;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return null;
+ }
+ synchronized (mediaSessionImpl.mLock) {
+ return mediaSessionImpl.mSessionActivity;
}
}
@Override
@SessionFlags
public long getFlags() {
- synchronized (mLock) {
- return mFlags;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return 0;
+ }
+ synchronized (mediaSessionImpl.mLock) {
+ return mediaSessionImpl.mFlags;
}
}
@Override
public ParcelableVolumeInfo getVolumeAttributes() {
- int controlType;
- int max;
- int current;
- int stream;
- int volumeType;
- synchronized (mLock) {
- volumeType = mVolumeType;
- stream = mLocalStream;
- VolumeProviderCompat vp = mVolumeProvider;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return null;
+ }
+ synchronized (mediaSessionImpl.mLock) {
+ int volumeType = mediaSessionImpl.mVolumeType;
+ int stream = mediaSessionImpl.mLocalStream;
+ VolumeProviderCompat vp = mediaSessionImpl.mVolumeProvider;
+ int controlType;
+ int max;
+ int current;
if (volumeType == MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_REMOTE) {
controlType = vp.getVolumeControl();
max = vp.getMaxVolume();
current = vp.getCurrentVolume();
} else {
controlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE;
- max = mAudioManager.getStreamMaxVolume(stream);
- current = mAudioManager.getStreamVolume(stream);
+ max = mediaSessionImpl.mAudioManager.getStreamMaxVolume(stream);
+ current = mediaSessionImpl.mAudioManager.getStreamVolume(stream);
}
+ return new ParcelableVolumeInfo(volumeType, stream, controlType, max, current);
}
- return new ParcelableVolumeInfo(volumeType, stream, controlType, max, current);
}
@Override
public void adjustVolume(int direction, int flags, String packageName) {
- MediaSessionImplBase.this.adjustVolume(direction, flags);
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl != null) {
+ mediaSessionImpl.adjustVolume(direction, flags);
+ }
}
@Override
public void setVolumeTo(int value, int flags, String packageName) {
- MediaSessionImplBase.this.setVolumeTo(value, flags);
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl != null) {
+ mediaSessionImpl.setVolumeTo(value, flags);
+ }
}
@Override
@@ -3229,17 +3266,17 @@
}
@Override
- public void prepareFromMediaId(String mediaId, Bundle extras) throws RemoteException {
+ public void prepareFromMediaId(String mediaId, Bundle extras) {
postToHandler(MessageHandler.MSG_PREPARE_MEDIA_ID, mediaId, extras);
}
@Override
- public void prepareFromSearch(String query, Bundle extras) throws RemoteException {
+ public void prepareFromSearch(String query, Bundle extras) {
postToHandler(MessageHandler.MSG_PREPARE_SEARCH, query, extras);
}
@Override
- public void prepareFromUri(Uri uri, Bundle extras) throws RemoteException {
+ public void prepareFromUri(Uri uri, Bundle extras) {
postToHandler(MessageHandler.MSG_PREPARE_URI, uri, extras);
}
@@ -3249,17 +3286,17 @@
}
@Override
- public void playFromMediaId(String mediaId, Bundle extras) throws RemoteException {
+ public void playFromMediaId(String mediaId, Bundle extras) {
postToHandler(MessageHandler.MSG_PLAY_MEDIA_ID, mediaId, extras);
}
@Override
- public void playFromSearch(String query, Bundle extras) throws RemoteException {
+ public void playFromSearch(String query, Bundle extras) {
postToHandler(MessageHandler.MSG_PLAY_SEARCH, query, extras);
}
@Override
- public void playFromUri(Uri uri, Bundle extras) throws RemoteException {
+ public void playFromUri(Uri uri, Bundle extras) {
postToHandler(MessageHandler.MSG_PLAY_URI, uri, extras);
}
@@ -3269,72 +3306,72 @@
}
@Override
- public void pause() throws RemoteException {
+ public void pause() {
postToHandler(MessageHandler.MSG_PAUSE);
}
@Override
- public void stop() throws RemoteException {
+ public void stop() {
postToHandler(MessageHandler.MSG_STOP);
}
@Override
- public void next() throws RemoteException {
+ public void next() {
postToHandler(MessageHandler.MSG_NEXT);
}
@Override
- public void previous() throws RemoteException {
+ public void previous() {
postToHandler(MessageHandler.MSG_PREVIOUS);
}
@Override
- public void fastForward() throws RemoteException {
+ public void fastForward() {
postToHandler(MessageHandler.MSG_FAST_FORWARD);
}
@Override
- public void rewind() throws RemoteException {
+ public void rewind() {
postToHandler(MessageHandler.MSG_REWIND);
}
@Override
- public void seekTo(long pos) throws RemoteException {
+ public void seekTo(long pos) {
postToHandler(MessageHandler.MSG_SEEK_TO, pos);
}
@Override
- public void rate(RatingCompat rating) throws RemoteException {
+ public void rate(RatingCompat rating) {
postToHandler(MessageHandler.MSG_RATE, rating);
}
@Override
- public void rateWithExtras(RatingCompat rating, Bundle extras) throws RemoteException {
+ public void rateWithExtras(RatingCompat rating, Bundle extras) {
postToHandler(MessageHandler.MSG_RATE_EXTRA, rating, extras);
}
@Override
- public void setPlaybackSpeed(float speed) throws RemoteException {
+ public void setPlaybackSpeed(float speed) {
postToHandler(MessageHandler.MSG_SET_PLAYBACK_SPEED, speed);
}
@Override
- public void setCaptioningEnabled(boolean enabled) throws RemoteException {
+ public void setCaptioningEnabled(boolean enabled) {
postToHandler(MessageHandler.MSG_SET_CAPTIONING_ENABLED, enabled);
}
@Override
- public void setRepeatMode(int repeatMode) throws RemoteException {
+ public void setRepeatMode(int repeatMode) {
postToHandler(MessageHandler.MSG_SET_REPEAT_MODE, repeatMode);
}
@Override
- public void setShuffleModeEnabledRemoved(boolean enabled) throws RemoteException {
+ public void setShuffleModeEnabledRemoved(boolean enabled) {
// Do nothing.
}
@Override
- public void setShuffleMode(int shuffleMode) throws RemoteException {
+ public void setShuffleMode(int shuffleMode) {
postToHandler(MessageHandler.MSG_SET_SHUFFLE_MODE, shuffleMode);
}
@@ -3346,24 +3383,33 @@
@Override
public MediaMetadataCompat getMetadata() {
- return mMetadata;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null ? mediaSessionImpl.mMetadata : null;
}
@Override
public PlaybackStateCompat getPlaybackState() {
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return null;
+ }
PlaybackStateCompat state;
MediaMetadataCompat metadata;
- synchronized (mLock) {
- state = mState;
- metadata = mMetadata;
+ synchronized (mediaSessionImpl.mLock) {
+ state = mediaSessionImpl.mState;
+ metadata = mediaSessionImpl.mMetadata;
}
return getStateWithUpdatedPosition(state, metadata);
}
@Override
public List<QueueItem> getQueue() {
- synchronized (mLock) {
- return mQueue;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return null;
+ }
+ synchronized (mediaSessionImpl.mLock) {
+ return mediaSessionImpl.mQueue;
}
}
@@ -3374,7 +3420,11 @@
@Override
public void addQueueItemAt(MediaDescriptionCompat description, int index) {
- postToHandler(MessageHandler.MSG_ADD_QUEUE_ITEM_AT, description, index);
+ postToHandler(
+ MessageHandler.MSG_ADD_QUEUE_ITEM_AT,
+ description,
+ index,
+ /* extras= */ null);
}
@Override
@@ -3389,31 +3439,43 @@
@Override
public CharSequence getQueueTitle() {
- return mQueueTitle;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null ? mediaSessionImpl.mQueueTitle : null;
}
@Override
public Bundle getExtras() {
- synchronized (mLock) {
- return mExtras;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl == null) {
+ return null;
+ }
+ synchronized (mediaSessionImpl.mLock) {
+ return mediaSessionImpl.mExtras;
}
}
@Override
@RatingCompat.Style
public int getRatingType() {
- return mRatingType;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null
+ ? mediaSessionImpl.mRatingType
+ : RatingCompat.RATING_NONE;
}
@Override
public boolean isCaptioningEnabled() {
- return mCaptioningEnabled;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null && mediaSessionImpl.mCaptioningEnabled;
}
@Override
@PlaybackStateCompat.RepeatMode
public int getRepeatMode() {
- return mRepeatMode;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null
+ ? mediaSessionImpl.mRepeatMode
+ : PlaybackStateCompat.REPEAT_MODE_INVALID;
}
@Override
@@ -3424,7 +3486,10 @@
@Override
@PlaybackStateCompat.ShuffleMode
public int getShuffleMode() {
- return mShuffleMode;
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ return mediaSessionImpl != null
+ ? mediaSessionImpl.mShuffleMode
+ : PlaybackStateCompat.SHUFFLE_MODE_INVALID;
}
@Override
@@ -3434,27 +3499,26 @@
}
void postToHandler(int what) {
- MediaSessionImplBase.this.postToHandler(what, 0, 0, null, null);
+ postToHandler(what, /* obj= */ null, /* arg1= */ 0, /* extras= */ null);
}
void postToHandler(int what, int arg1) {
- MediaSessionImplBase.this.postToHandler(what, arg1, 0, null, null);
- }
-
- void postToHandler(int what, int arg1, int arg2) {
- MediaSessionImplBase.this.postToHandler(what, arg1, arg2, null, null);
+ postToHandler(what, /* obj= */ null, arg1, /* extras= */ null);
}
void postToHandler(int what, Object obj) {
- MediaSessionImplBase.this.postToHandler(what, 0, 0, obj, null);
- }
-
- void postToHandler(int what, Object obj, int arg1) {
- MediaSessionImplBase.this.postToHandler(what, arg1, 0, obj, null);
+ postToHandler(what, obj, /* arg1= */ 0, /* extras= */ null);
}
void postToHandler(int what, Object obj, Bundle extras) {
- MediaSessionImplBase.this.postToHandler(what, 0, 0, obj, extras);
+ postToHandler(what, obj, /* arg1= */ 0, extras);
+ }
+
+ void postToHandler(int what, Object obj, int arg1, Bundle extras) {
+ MediaSessionImplBase mediaSessionImpl = mMediaSessionImplRef.get();
+ if (mediaSessionImpl != null) {
+ mediaSessionImpl.postToHandler(what, arg1, /* arg2= */ 0, obj, extras);
+ }
}
}