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

Commit c818b141 authored by Marco Nelissen's avatar Marco Nelissen
Browse files

Do not enable the rew/pause/ffwd buttons when the media being played does not

support these functions.
This extends the MediaPlayerControl interface with some new methods, and uses
reflection to detect whether the old or new interface is used by an application.
parent 460f5410
Loading
Loading
Loading
Loading
+66 −0
Original line number Diff line number Diff line
@@ -175196,6 +175196,39 @@
 deprecated="not deprecated"
 visibility="public"
>
<method name="canPause"
 return="boolean"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="canSeekBackward"
 return="boolean"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="canSeekForward"
 return="boolean"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getBufferPercentage"
 return="int"
 abstract="true"
@@ -183524,6 +183557,39 @@
<parameter name="defStyle" type="int">
</parameter>
</constructor>
<method name="canPause"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="canSeekBackward"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="canSeekForward"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getBufferPercentage"
 return="int"
 abstract="false"
+32 −9
Original line number Diff line number Diff line
@@ -249,6 +249,29 @@ public class MediaController extends FrameLayout {
        show(sDefaultTimeout);
    }

    /**
     * Disable pause or seek buttons if the stream cannot be paused or seeked.
     * This requires the control interface to be a MediaPlayerControlExt
     */
    private void disableUnsupportedButtons() {
        try {
            if (mPauseButton != null && !mPlayer.canPause()) {
                mPauseButton.setEnabled(false);
            }
            if (mRewButton != null && !mPlayer.canSeekBackward()) {
                mRewButton.setEnabled(false);
            }
            if (mFfwdButton != null && !mPlayer.canSeekForward()) {
                mFfwdButton.setEnabled(false);
            }
        } catch (IncompatibleClassChangeError ex) {
            // We were given an old version of the interface, that doesn't have
            // the canPause/canSeekXYZ methods. This is OK, it just means we
            // assume the media can be paused and seeked, and so we don't disable
            // the buttons.
        }
    }
    
    /**
     * Show the controller on screen. It will go away
     * automatically after 'timeout' milliseconds of inactivity.
@@ -259,6 +282,7 @@ public class MediaController extends FrameLayout {

        if (!mShowing && mAnchor != null) {
            setProgress();
            disableUnsupportedButtons();

            int [] anchorpos = new int[2];
            mAnchor.getLocationOnScreen(anchorpos);
@@ -421,17 +445,13 @@ public class MediaController extends FrameLayout {
    };

    private void updatePausePlay() {
        if (mRoot == null)
            return;

        ImageButton button = (ImageButton) mRoot.findViewById(com.android.internal.R.id.pause);
        if (button == null)
        if (mRoot == null || mPauseButton == null)
            return;

        if (mPlayer.isPlaying()) {
            button.setImageResource(com.android.internal.R.drawable.ic_media_pause);
            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_pause);
        } else {
            button.setImageResource(com.android.internal.R.drawable.ic_media_play);
            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_play);
        }
    }

@@ -516,7 +536,7 @@ public class MediaController extends FrameLayout {
        if (mProgress != null) {
            mProgress.setEnabled(enabled);
        }

        disableUnsupportedButtons();
        super.setEnabled(enabled);
    }

@@ -579,5 +599,8 @@ public class MediaController extends FrameLayout {
        void    seekTo(int pos);
        boolean isPlaying();
        int     getBufferPercentage();
    };
        boolean canPause();
        boolean canSeekBackward();
        boolean canSeekForward();
    }
}
+29 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Metadata;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
@@ -34,7 +35,7 @@ import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.MediaController.*;

import java.io.IOException;

@@ -81,6 +82,9 @@ public class VideoView extends SurfaceView implements MediaPlayerControl {
    private int         mCurrentBufferPercentage;
    private OnErrorListener mOnErrorListener;
    private int         mSeekWhenPrepared;  // recording the seek position while preparing
    private boolean     mCanPause;
    private boolean     mCanSeekBack;
    private boolean     mCanSeekForward;

    public VideoView(Context context) {
        super(context);
@@ -259,6 +263,17 @@ public class VideoView extends SurfaceView implements MediaPlayerControl {
    MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mCurrentState = STATE_PREPARED;

            // Get the capabilities of the player for this stream
            Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,
                                      MediaPlayer.BYPASS_METADATA_FILTER);
            mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)
                    || data.getBoolean(Metadata.PAUSE_AVAILABLE);
            mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)
                    || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);
            mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)
                    || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);

            if (mOnPreparedListener != null) {
                mOnPreparedListener.onPrepared(mMediaPlayer);
            }
@@ -267,6 +282,7 @@ public class VideoView extends SurfaceView implements MediaPlayerControl {
            }
            mVideoWidth = mp.getVideoWidth();
            mVideoHeight = mp.getVideoHeight();

            int seekToPosition = mSeekWhenPrepared;  // mSeekWhenPrepared may be changed after seekTo() call
            if (seekToPosition != 0) {
                seekTo(seekToPosition);
@@ -580,4 +596,16 @@ public class VideoView extends SurfaceView implements MediaPlayerControl {
                mCurrentState != STATE_IDLE &&
                mCurrentState != STATE_PREPARING);
    }

    public boolean canPause() {
        return mCanPause;
    }

    public boolean canSeekBackward() {
        return mCanSeekBack;
    }

    public boolean canSeekForward() {
        return mCanSeekForward;
    }
}