FileDocCategorySizeDatePackage
TransportMediatorJellybeanMR2.javaAPI DocAndroid 5.1 API7580Thu Mar 12 22:22:56 GMT 2015android.support.v4.media

TransportMediatorJellybeanMR2

public class TransportMediatorJellybeanMR2 extends Object implements RemoteControlClient.OnGetPlaybackPositionListener, RemoteControlClient.OnPlaybackPositionUpdateListener

Fields Summary
final android.content.Context
mContext
final android.media.AudioManager
mAudioManager
final android.view.View
mTargetView
final TransportMediatorCallback
mTransportCallback
final String
mReceiverAction
final android.content.IntentFilter
mReceiverFilter
final android.content.Intent
mIntent
final ViewTreeObserver.OnWindowAttachListener
mWindowAttachListener
final ViewTreeObserver.OnWindowFocusChangeListener
mWindowFocusListener
final android.content.BroadcastReceiver
mMediaButtonReceiver
AudioManager.OnAudioFocusChangeListener
mAudioFocusChangeListener
android.app.PendingIntent
mPendingIntent
android.media.RemoteControlClient
mRemoteControl
boolean
mFocused
int
mPlayState
boolean
mAudioFocused
Constructors Summary
public TransportMediatorJellybeanMR2(android.content.Context context, android.media.AudioManager audioManager, android.view.View view, TransportMediatorCallback transportCallback)


        
                
        mContext = context;
        mAudioManager = audioManager;
        mTargetView = view;
        mTransportCallback = transportCallback;
        mReceiverAction = context.getPackageName() + ":transport:" + System.identityHashCode(this);
        mIntent = new Intent(mReceiverAction);
        mIntent.setPackage(context.getPackageName());
        mReceiverFilter = new IntentFilter();
        mReceiverFilter.addAction(mReceiverAction);
        mTargetView.getViewTreeObserver().addOnWindowAttachListener(mWindowAttachListener);
        mTargetView.getViewTreeObserver().addOnWindowFocusChangeListener(mWindowFocusListener);
    
Methods Summary
public voiddestroy()

        windowDetached();
        mTargetView.getViewTreeObserver().removeOnWindowAttachListener(mWindowAttachListener);
        mTargetView.getViewTreeObserver().removeOnWindowFocusChangeListener(mWindowFocusListener);
    
voiddropAudioFocus()

        if (mAudioFocused) {
            mAudioFocused = false;
            mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
        }
    
voidgainFocus()

        if (!mFocused) {
            mFocused = true;
            mAudioManager.registerMediaButtonEventReceiver(mPendingIntent);
            mAudioManager.registerRemoteControlClient(mRemoteControl);
            if (mPlayState == RemoteControlClient.PLAYSTATE_PLAYING) {
                takeAudioFocus();
            }
        }
    
public java.lang.ObjectgetRemoteControlClient()

        return mRemoteControl;
    
voidloseFocus()

        dropAudioFocus();
        if (mFocused) {
            mFocused = false;
            mAudioManager.unregisterRemoteControlClient(mRemoteControl);
            mAudioManager.unregisterMediaButtonEventReceiver(mPendingIntent);
        }
    
public longonGetPlaybackPosition()

        return mTransportCallback.getPlaybackPosition();
    
public voidonPlaybackPositionUpdate(long newPositionMs)

        mTransportCallback.playbackPositionUpdate(newPositionMs);
    
public voidpausePlaying()

        if (mPlayState == RemoteControlClient.PLAYSTATE_PLAYING) {
            mPlayState = RemoteControlClient.PLAYSTATE_PAUSED;
            mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        }
        dropAudioFocus();
    
public voidrefreshState(boolean playing, long position, int transportControls)

        if (mRemoteControl != null) {
            mRemoteControl.setPlaybackState(playing ? RemoteControlClient.PLAYSTATE_PLAYING
                    : RemoteControlClient.PLAYSTATE_STOPPED, position, playing ? 1 : 0);
            mRemoteControl.setTransportControlFlags(transportControls);
        }
    
public voidstartPlaying()

        if (mPlayState != RemoteControlClient.PLAYSTATE_PLAYING) {
            mPlayState = RemoteControlClient.PLAYSTATE_PLAYING;
            mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        }
        if (mFocused) {
            takeAudioFocus();
        }
    
public voidstopPlaying()

        if (mPlayState != RemoteControlClient.PLAYSTATE_STOPPED) {
            mPlayState = RemoteControlClient.PLAYSTATE_STOPPED;
            mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
        }
        dropAudioFocus();
    
voidtakeAudioFocus()

        if (!mAudioFocused) {
            mAudioFocused = true;
            mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
                    AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    
voidwindowAttached()

        mContext.registerReceiver(mMediaButtonReceiver, mReceiverFilter);
        mPendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        mRemoteControl = new RemoteControlClient(mPendingIntent);
        mRemoteControl.setOnGetPlaybackPositionListener(this);
        mRemoteControl.setPlaybackPositionUpdateListener(this);
    
voidwindowDetached()

        loseFocus();
        if (mPendingIntent != null) {
            mContext.unregisterReceiver(mMediaButtonReceiver);
            mPendingIntent.cancel();
            mPendingIntent = null;
            mRemoteControl = null;
        }