Java Code Examples for android.view.KeyEvent#KEYCODE_HEADSETHOOK

The following examples show how to use android.view.KeyEvent#KEYCODE_HEADSETHOOK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentViewCore.java    From 365browser with Apache License 2.0 7 votes vote down vote up
/**
 * Check whether a key should be propagated to the embedder or not.
 * We need to send almost every key to Blink. However:
 * 1. We don't want to block the device on the renderer for
 * some keys like menu, home, call.
 * 2. There are no WebKit equivalents for some of these keys
 * (see app/keyboard_codes_win.h)
 * Note that these are not the same set as KeyEvent.isSystemKey:
 * for instance, AKEYCODE_MEDIA_* will be dispatched to webkit*.
 */
private static boolean shouldPropagateKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_HOME
            || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_CALL
            || keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_POWER
            || keyCode == KeyEvent.KEYCODE_HEADSETHOOK || keyCode == KeyEvent.KEYCODE_CAMERA
            || keyCode == KeyEvent.KEYCODE_FOCUS || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
            || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE
            || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        return false;
    }
    return true;
}
 
Example 2
Source File: IjkVideoView.java    From MKVideoPlayer with MIT License 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&
            keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
            keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
            keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&
            keyCode != KeyEvent.KEYCODE_MENU &&
            keyCode != KeyEvent.KEYCODE_CALL &&
            keyCode != KeyEvent.KEYCODE_ENDCALL;
    if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
                keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            } else {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
            if (!mMediaPlayer.isPlaying()) {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
                || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            }
            return true;
        } else {
            toggleMediaControlsVisiblity();
        }
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 3
Source File: ContentViewClient.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean shouldOverrideKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    // We need to send almost every key to WebKit. However:
    // 1. We don't want to block the device on the renderer for
    // some keys like menu, home, call.
    // 2. There are no WebKit equivalents for some of these keys
    // (see app/keyboard_codes_win.h)
    // Note that these are not the same set as KeyEvent.isSystemKey:
    // for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
    if (keyCode == KeyEvent.KEYCODE_MENU ||
        keyCode == KeyEvent.KEYCODE_HOME ||
        keyCode == KeyEvent.KEYCODE_BACK ||
        keyCode == KeyEvent.KEYCODE_CALL ||
        keyCode == KeyEvent.KEYCODE_ENDCALL ||
        keyCode == KeyEvent.KEYCODE_POWER ||
        keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
        keyCode == KeyEvent.KEYCODE_CAMERA ||
        keyCode == KeyEvent.KEYCODE_FOCUS ||
        keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
        keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
        keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        return true;
    }

    // We also have to intercept some shortcuts before we send them to the ContentView.
    if (event.isCtrlPressed() && (
            keyCode == KeyEvent.KEYCODE_TAB ||
            keyCode == KeyEvent.KEYCODE_W ||
            keyCode == KeyEvent.KEYCODE_F4)) {
        return true;
    }

    return false;
}
 
Example 4
Source File: TabWebContentsDelegateAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
 * other buttons to function correctly.
 */
@TargetApi(19)
private void handleMediaKey(KeyEvent e) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
    switch (e.getKeyCode()) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_MEDIA_CLOSE:
        case KeyEvent.KEYCODE_MEDIA_EJECT:
        case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
            AudioManager am = (AudioManager) mTab.getApplicationContext().getSystemService(
                    Context.AUDIO_SERVICE);
            am.dispatchMediaKeyEvent(e);
            break;
        default:
            break;
    }
}
 
Example 5
Source File: MainActivity.java    From Rainville with MIT License 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
            mFrontPanelFragment.togglePlay();
            return true;
    }

    return super.onKeyUp(keyCode, event);
}
 
Example 6
Source File: HeadsetNotificationBroadcast.java    From Rey-MusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    mApp = (Common) context.getApplicationContext();
    if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
        KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
        if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
            return;
        switch (keyEvent.getKeyCode()) {
            case KeyEvent.KEYCODE_HEADSETHOOK:
                mApp.getPlayBackStarter().playSongs();
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                mApp.getPlayBackStarter().pauseSong();
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                mApp.getPlayBackStarter().playSongs();
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                mApp.getPlayBackStarter().pauseSong();
                break;
            case KeyEvent.KEYCODE_MEDIA_STOP:
                mApp.getPlayBackStarter().pauseSong();
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                mApp.getPlayBackStarter().nextSong();
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                mApp.getPlayBackStarter().previousSong();
                break;
            default:
                break;
        }
    }
}
 
Example 7
Source File: MjpegView.java    From CameraV with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (mIsPrepared &&
            keyCode != KeyEvent.KEYCODE_BACK &&
            keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
            keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
            keyCode != KeyEvent.KEYCODE_MENU &&
            keyCode != KeyEvent.KEYCODE_CALL &&
            keyCode != KeyEvent.KEYCODE_ENDCALL &&
            //mMediaPlayer != null &&
            mMediaController != null) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
                mMediaController.show();
            } else {
            	start();
                mMediaController.hide();
            }
            return true;
        } else {
            toggleMediaControlsVisiblity();
        }
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 8
Source File: IjkVideoView.java    From IjkPlayerDemo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&
            keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
            keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
            keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&
            keyCode != KeyEvent.KEYCODE_MENU &&
            keyCode != KeyEvent.KEYCODE_CALL &&
            keyCode != KeyEvent.KEYCODE_ENDCALL;
    if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
                keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            } else {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
            if (!mMediaPlayer.isPlaying()) {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
                || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            }
            return true;
        } else {
            toggleMediaControlsVisiblity();
        }
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 9
Source File: MediaControlView.java    From Cirrus_depricated with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    final boolean uniqueDown = event.getRepeatCount() == 0
            && event.getAction() == KeyEvent.ACTION_DOWN;
    if (keyCode ==  KeyEvent.KEYCODE_HEADSETHOOK
            || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
            || keyCode == KeyEvent.KEYCODE_SPACE) {
        if (uniqueDown) {
            doPauseResume();
            //show(sDefaultTimeout);
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            }
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
        if (uniqueDown && !mPlayer.isPlaying()) {
            mPlayer.start();
            updatePausePlay();
            //show(sDefaultTimeout);
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
            || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
        if (uniqueDown && mPlayer.isPlaying()) {
            mPlayer.pause();
            updatePausePlay();
            //show(sDefaultTimeout);
        }
        return true;
    }

    //show(sDefaultTimeout);
    return super.dispatchKeyEvent(event);
}
 
Example 10
Source File: MediaButtonIntentReceiver.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
            case KeyEvent.KEYCODE_MEDIA_PLAY:
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = ReadAloudService.ActionMediaButton;
                break;
            default:
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                readAloud(context, command);
                return true;
            }
        }
    }
    return false;
}
 
Example 11
Source File: ContentVideoView.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&
                                 keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
                                 keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
                                 keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&
                                 keyCode != KeyEvent.KEYCODE_CALL &&
                                 keyCode != KeyEvent.KEYCODE_MENU &&
                                 keyCode != KeyEvent.KEYCODE_SEARCH &&
                                 keyCode != KeyEvent.KEYCODE_ENDCALL;
    if (isInPlaybackState() && isKeyCodeSupported && mControls != null) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
                keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
            if (isPlaying()) {
                pause();
                mControls.show();
            } else {
                start();
                mControls.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
            if (!isPlaying()) {
                start();
                mControls.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
                || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
            if (isPlaying()) {
                pause();
                mControls.show();
            }
            return true;
        } else {
            toggleMediaControlsVisiblity();
        }
    } else if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        exitFullscreen(false);
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_SEARCH) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 12
Source File: MediaButtonIntentReceiver.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime() != 0 ?
                event.getEventTime() : System.currentTimeMillis();
        // Fallback to system time if event time was not available.

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = MusicService.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = MusicService.ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = MusicService.ACTION_SKIP;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = MusicService.ACTION_REWIND;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = MusicService.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = MusicService.ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 13
Source File: MediaNotificationManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void processAction(Intent intent, MediaNotificationManager manager) {
    String action = intent.getAction();

    // Before Android L, instead of using the MediaSession callback, the system will fire
    // ACTION_MEDIA_BUTTON intents which stores the information about the key event.
    if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
        KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) return;
        if (event.getAction() != KeyEvent.ACTION_DOWN) return;

        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                manager.onPlay(
                        MediaNotificationListener.ACTION_SOURCE_MEDIA_SESSION);
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                manager.onPause(
                        MediaNotificationListener.ACTION_SOURCE_MEDIA_SESSION);
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                if (manager.mMediaNotificationInfo.isPaused) {
                    manager.onPlay(MediaNotificationListener.ACTION_SOURCE_MEDIA_SESSION);
                } else {
                    manager.onPause(
                            MediaNotificationListener.ACTION_SOURCE_MEDIA_SESSION);
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                manager.onMediaSessionAction(MediaSessionAction.PREVIOUS_TRACK);
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                manager.onMediaSessionAction(MediaSessionAction.NEXT_TRACK);
                break;
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                manager.onMediaSessionAction(MediaSessionAction.SEEK_FORWARD);
                break;
            case KeyEvent.KEYCODE_MEDIA_REWIND:
                manager.onMediaSessionAction(MediaSessionAction.SEEK_BACKWARD);
                break;
            default:
                break;
        }
    } else if (ACTION_STOP.equals(action)
            || ACTION_SWIPE.equals(action)
            || ACTION_CANCEL.equals(action)) {
        manager.onStop(
                MediaNotificationListener.ACTION_SOURCE_MEDIA_NOTIFICATION);
        stopListenerService();
    } else if (ACTION_PLAY.equals(action)) {
        manager.onPlay(MediaNotificationListener.ACTION_SOURCE_MEDIA_NOTIFICATION);
    } else if (ACTION_PAUSE.equals(action)) {
        manager.onPause(MediaNotificationListener.ACTION_SOURCE_MEDIA_NOTIFICATION);
    } else if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(action)) {
        manager.onPause(MediaNotificationListener.ACTION_SOURCE_HEADSET_UNPLUG);
    } else if (ACTION_PREVIOUS_TRACK.equals(action)) {
        manager.onMediaSessionAction(MediaSessionAction.PREVIOUS_TRACK);
    } else if (ACTION_NEXT_TRACK.equals(action)) {
        manager.onMediaSessionAction(MediaSessionAction.NEXT_TRACK);
    } else if (ACTION_SEEK_FORWARD.equals(action)) {
        manager.onMediaSessionAction(MediaSessionAction.SEEK_FORWARD);
    } else if (ACTION_SEEK_BACKWARD.equals(action)) {
        manager.onMediaSessionAction(MediaSessionAction.SEEK_BACKWARD);
    }
}
 
Example 14
Source File: UniversalVideoView.java    From LLApp with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&
            keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
            keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
            keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&
            keyCode != KeyEvent.KEYCODE_MENU &&
            keyCode != KeyEvent.KEYCODE_CALL &&
            keyCode != KeyEvent.KEYCODE_ENDCALL;
    if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
                keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            } else {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
            if (!mMediaPlayer.isPlaying()) {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
                || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            }
            return true;
        } else {
            toggleMediaControlsVisibility();
        }
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 15
Source File: DownloadServiceLifecycleSupport.java    From Audinaut with GNU General Public License v3.0 4 votes vote down vote up
private void handleKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                downloadService.fastForward();
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                downloadService.rewind();
                break;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                if (lastPressTime < (System.currentTimeMillis() - 500)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.togglePlayPause();
                } else {
                    downloadService.next(true);
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.previous();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.next();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_REWIND:
                downloadService.rewind();
                break;
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                downloadService.fastForward();
                break;
            case KeyEvent.KEYCODE_MEDIA_STOP:
                downloadService.stop();
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                if (downloadService.getPlayerState() != PlayerState.STARTED) {
                    downloadService.start();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                downloadService.pause();
            default:
                break;
        }
    }
}
 
Example 16
Source File: RcvMediaControl.java    From freemp with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
        KeyEvent event = (KeyEvent) intent
                .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        if (event.getAction() != KeyEvent.ACTION_DOWN) {
            return;
        }

        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                sendMessage(context, "play");
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                sendMessage(context, "play");
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:

                sendMessage(context, "play");
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                sendMessage(context, "next");
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                sendMessage(context, "prev");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
                sendMessage(context, "voup");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                sendMessage(context, "vodn");
                break;
        }
    }

}
 
Example 17
Source File: MediaButtonReceiver.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();

    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        KeyEvent keyEvent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

        if (keyEvent == null) {
            return;
        }

        int action = keyEvent.getAction();
        int keyCode = keyEvent.getKeyCode();
        long eventTime = keyEvent.getEventTime();

        LogUtil.d(TAG, "carlife: onReceive is triggered!");

        if (action == KeyEvent.ACTION_UP) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_HEADSETHOOK:
                    break;

                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    LogUtil.d(TAG, "KEYCODE_MEDIA_PREVIOUS");
                    TouchListenerManager.getInstance().sendHardKeyCodeEvent(CommonParams.KEYCODE_SEEK_SUB);
                    break;

                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    LogUtil.d(TAG, "KEYCODE_MEDIA_NEXT");
                    TouchListenerManager.getInstance().sendHardKeyCodeEvent(CommonParams.KEYCODE_SEEK_ADD);
                    break;
                default:
                    break;
            }
        }

        if (isOrderedBroadcast()) {
            abortBroadcast();
        }
    }
}
 
Example 18
Source File: VideoView.java    From NewsMe with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	log("onKeyDown() into");
	boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK
			&& keyCode != KeyEvent.KEYCODE_VOLUME_UP
			&& keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
			&& keyCode != KeyEvent.KEYCODE_MENU
			&& keyCode != KeyEvent.KEYCODE_CALL
			&& keyCode != KeyEvent.KEYCODE_ENDCALL;
	if (isInPlaybackState() && isKeyCodeSupported
			&& mMediaController != null) {
		if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
				|| keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
				|| keyCode == KeyEvent.KEYCODE_SPACE) {
			if (mMediaPlayer.isPlaying()) {
				pause();
				mMediaController.show();
			} else {
				start();
				mMediaController.hide();
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
			if (!mMediaPlayer.isPlaying()) {
				start();
				mMediaController.hide();
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
				|| keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
			if (mMediaPlayer.isPlaying()) {
				pause();
				mMediaController.show();
			}
			return true;
		} else {
			toggleMediaControlsVisiblity();
		}
	}

	return super.onKeyDown(keyCode, event);
}
 
Example 19
Source File: MediaController.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    final boolean uniqueDown = event.getRepeatCount() == 0
            && event.getAction() == KeyEvent.ACTION_DOWN;
    if (keyCode ==  KeyEvent.KEYCODE_HEADSETHOOK
            || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
            || keyCode == KeyEvent.KEYCODE_SPACE) {
        if (uniqueDown) {
            doPauseResume();
            show(sDefaultTimeout);
            if (mPauseButton != null) {
                mPauseButton.requestFocus();
            }
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
        if (uniqueDown && !mPlayer.isPlaying()) {
            mPlayer.start();
            updatePausePlay();
            show(sDefaultTimeout);
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
            || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
        if (uniqueDown && mPlayer.isPlaying()) {
            mPlayer.pause();
            updatePausePlay();
            show(sDefaultTimeout);
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
            || keyCode == KeyEvent.KEYCODE_VOLUME_UP
            || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE
            || keyCode == KeyEvent.KEYCODE_CAMERA) {
        // don't show the controls for volume adjustment
        return super.dispatchKeyEvent(event);
    } else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
        if (uniqueDown) {
            hide();
        }
        return true;
    }

    show(sDefaultTimeout);
    return super.dispatchKeyEvent(event);
}
 
Example 20
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private boolean isVoiceKey(int keyCode) {
    return keyCode == KeyEvent.KEYCODE_HEADSETHOOK
            || (!mHasFeatureLeanback && keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
}