Code example for ImageButton

Methods: setImageResource, requestFocus, setEnabled

0
     * 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. 
     * @param timeout The timeout in milliseconds. Use 0 to show 
     * the controller until hide() is called. 
     */ 
    public void show(int timeout) {
 
        if (!mShowing && mAnchor != null) {
            setProgress(); 
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            } 
            disableUnsupportedButtons(); 
 
            int [] anchorpos = new int[2];
            mAnchor.getLocationOnScreen(anchorpos);
 
            WindowManager.LayoutParams p = new WindowManager.LayoutParams();
            p.gravity = Gravity.TOP;
            p.width = mAnchor.getWidth();
            p.height = LayoutParams.WRAP_CONTENT;
            p.x = 0;
            p.y = anchorpos[1] + mAnchor.getHeight() - p.height;
            p.format = PixelFormat.TRANSLUCENT;
            p.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
            p.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
            p.token = null;
            p.windowAnimations = 0; // android.R.style.DropDownAnimationDown;
            mWindowManager.addView(mDecor, p);
            mShowing = true;
        } 
        updatePausePlay(); 
 
        // cause the progress bar to be updated even if mShowing 
        // was already true.  This happens, for example, if we're 
        // paused with the progress bar showing the user hits play. 
        mHandler.sendEmptyMessage(SHOW_PROGRESS);
 
        Message msg = mHandler.obtainMessage(FADE_OUT);
        if (timeout != 0) {
            mHandler.removeMessages(FADE_OUT);
            mHandler.sendMessageDelayed(msg, timeout);
        } 
    } 
 
    public boolean isShowing() { 
        return mShowing;
    } 
 
    /** 
     * Remove the controller from the screen. 
     */ 
    public void hide() { 
        if (mAnchor == null)
            return; 
 
        if (mShowing) {
            try { 
                mHandler.removeMessages(SHOW_PROGRESS);
                mWindowManager.removeView(mDecor);
            } catch (IllegalArgumentException ex) {
                Log.w("MediaController", "already removed");
            } 
            mShowing = false;
        } 
    } 
 
    private Handler mHandler = new Handler() {
        @Override 
        public void handleMessage(Message msg) { 
            int pos; 
            switch (msg.what) { 
                case FADE_OUT: 
                    hide(); 
                    break; 
                case SHOW_PROGRESS: 
                    pos = setProgress(); 
                    if (!mDragging && mShowing && mPlayer.isPlaying()) { 
                        msg = obtainMessage(SHOW_PROGRESS); 
                        sendMessageDelayed(msg, 1000 - (pos % 1000)); 
                    } 
                    break; 
            } 
        } 
    }; 
 
    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;
 
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours   = totalSeconds / 3600;
 
        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else { 
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        } 
    } 
 
    private int setProgress() { 
        if (mPlayer == null || mDragging) {
            return 0; 
        } 
        int position = mPlayer.getCurrentPosition();
        int duration = mPlayer.getDuration();
        if (mProgress != null) {
            if (duration > 0) {
                // use long to avoid overflow 
                long pos = 1000L * position / duration;
                mProgress.setProgress( (int) pos);
            } 
            int percent = mPlayer.getBufferPercentage();
            mProgress.setSecondaryProgress(percent * 10);
        } 
 
        if (mEndTime != null)
            mEndTime.setText(stringForTime(duration));
        if (mCurrentTime != null)
            mCurrentTime.setText(stringForTime(position));
 
        return position;
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        show(sDefaultTimeout);
        return true; 
    } 
 
    @Override 
    public boolean onTrackballEvent(MotionEvent ev) {
        show(sDefaultTimeout);
        return false; 
    } 
 
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_DOWN && (
                keyCode ==  KeyEvent.KEYCODE_HEADSETHOOK ||
                keyCode ==  KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE ||
                keyCode ==  KeyEvent.KEYCODE_SPACE)) {
            doPauseResume(); 
            show(sDefaultTimeout);
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            } 
            return true; 
        } else if (keyCode ==  KeyEvent.KEYCODE_MEDIA_STOP) {
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
                updatePausePlay(); 
            } 
            return true; 
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
                keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            // don't show the controls for volume adjustment 
            return super.dispatchKeyEvent(event);
        } else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
            hide(); 
 
            return true; 
        } else { 
            show(sDefaultTimeout);
        } 
        return super.dispatchKeyEvent(event);
    } 
 
    private View.OnClickListener mPauseListener = new View.OnClickListener() {
        public void onClick(View v) { 
            doPauseResume(); 
            show(sDefaultTimeout); 
        } 
    }; 
 
    private void updatePausePlay() { 
        if (mRoot == null || mPauseButton == null)
            return; 
 
        if (mPlayer.isPlaying()) {
            mPauseButton.setImageResource(android.R.drawable.ic_media_pause);
        } else { 
            mPauseButton.setImageResource(android.R.drawable.ic_media_play);
        } 
    } 
 
    private void doPauseResume() { 
        if (mPlayer.isPlaying()) {