Example usage for android.view KeyEvent KEYCODE_HEADSETHOOK

List of usage examples for android.view KeyEvent KEYCODE_HEADSETHOOK

Introduction

In this page you can find the example usage for android.view KeyEvent KEYCODE_HEADSETHOOK.

Prototype

int KEYCODE_HEADSETHOOK

To view the source code for android.view KeyEvent KEYCODE_HEADSETHOOK.

Click Source Link

Document

Key code constant: Headset Hook key.

Usage

From source file:com.bluros.music.helpers.MediaButtonIntentReceiver.java

@Override
public void onReceive(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        if (PreferencesUtility.getInstance(context).pauseEnabledOnDetach())
            startService(context, MusicService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }//from ww  w  .j a  v  a  2 s.c  o m

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

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicService.CMDSTOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicService.CMDTOGGLEPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicService.CMDNEXT;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicService.CMDPREVIOUS;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicService.CMDPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicService.CMDPLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (mDown) {
                    if (MusicService.CMDTOGGLEPAUSE.equals(command) || MusicService.CMDPLAY.equals(command)) {
                        if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                            acquireWakeLockAndSendMessage(context,
                                    mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
                        }
                    }
                } else if (event.getRepeatCount() == 0) {

                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        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);
                    }
                    mLaunched = false;
                    mDown = true;
                }
            } else {
                mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
                mDown = false;
            }
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
            releaseWakeLockIfHandlerIdle();
        }
    }
}

From source file:com.geryon.ocraa.Fragment_Player.java

public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
    case KeyEvent.KEYCODE_HEADSETHOOK:
        getActivity().startService(new Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
        return true;
    }/*from www .j  a va 2s  .  c  o  m*/
    return onKeyDown(keyCode, event);
}

From source file:freed.cam.ActivityFreeDcamMain.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (activityIsResumed) {
        Log.d(TAG, "KeyCode Pressed:" + keyCode);
        int appSettingsKeyShutter = 0;

        if (getAppSettings().getApiString(AppSettingsManager.SETTING_EXTERNALSHUTTER).equals(StringUtils.VoLP))
            appSettingsKeyShutter = KeyEvent.KEYCODE_VOLUME_UP;
        else if (getAppSettings().getApiString(AppSettingsManager.SETTING_EXTERNALSHUTTER)
                .equals(StringUtils.VoLM))
            appSettingsKeyShutter = KeyEvent.KEYCODE_VOLUME_DOWN;
        else if (getAppSettings().getApiString(AppSettingsManager.SETTING_EXTERNALSHUTTER)
                .equals(StringUtils.Hook)
                || getAppSettings().getApiString(AppSettingsManager.SETTING_EXTERNALSHUTTER).equals(""))
            appSettingsKeyShutter = KeyEvent.KEYCODE_HEADSETHOOK;

        if (keyCode == KeyEvent.KEYCODE_3D_MODE || keyCode == KeyEvent.KEYCODE_POWER
                || keyCode == appSettingsKeyShutter || keyCode == KeyEvent.KEYCODE_UNKNOWN
                || keyCode == KeyEvent.KEYCODE_CAMERA) {
            cameraFragment.GetModuleHandler().DoWork();
            return true;
        }//from  w  ww.ja v a 2  s  .c o  m
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            closeActivity();
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.stasbar.knowyourself.alarms.AlarmActivity.java

@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent keyEvent) {
    // Do this in dispatch to intercept a few of the system keys.
    LOGGER.v("dispatchKeyEvent: %s", keyEvent);

    switch (keyEvent.getKeyCode()) {
    // Volume keys and camera keys dismiss the alarm.
    case KeyEvent.KEYCODE_POWER:
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_MUTE:
    case KeyEvent.KEYCODE_HEADSETHOOK:
    case KeyEvent.KEYCODE_CAMERA:
    case KeyEvent.KEYCODE_FOCUS:
        if (!mAlarmHandled && keyEvent.getAction() == KeyEvent.ACTION_UP) {
            switch (mVolumeBehavior) {
            case SettingsActivity.VOLUME_BEHAVIOR_SNOOZE:
                snooze();/* w w w  .java2 s  . c o m*/
                break;
            case SettingsActivity.VOLUME_BEHAVIOR_DISMISS:
                dismiss();
                break;
            default:
                break;
            }
        }
        return true;
    default:
        return super.dispatchKeyEvent(keyEvent);
    }
}

From source file:com.androidinspain.deskclock.alarms.AlarmActivity.java

@Override
public boolean dispatchKeyEvent(@NonNull KeyEvent keyEvent) {
    // Do this in dispatch to intercept a few of the system keys.
    LOGGER.v("dispatchKeyEvent: %s", keyEvent);

    final int keyCode = keyEvent.getKeyCode();
    switch (keyCode) {
    // Volume keys and camera keys dismiss the alarm.
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
    case KeyEvent.KEYCODE_VOLUME_MUTE:
    case KeyEvent.KEYCODE_HEADSETHOOK:
    case KeyEvent.KEYCODE_CAMERA:
    case KeyEvent.KEYCODE_FOCUS:
        if (!mAlarmHandled) {
            switch (mVolumeBehavior) {
            case SNOOZE:
                if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                    snooze();//from   w  w w  . j  a  v a 2s .c  o m
                }
                return true;
            case DISMISS:
                if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                    dismiss();
                }
                return true;
            }
        }
    }
    return super.dispatchKeyEvent(keyEvent);
}

From source file:org.easyaccess.phonedialer.CallStateService.java

@Override
public void onShake(float force) {
    if (Utils.ringing == 1) {
        // answer call
        Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        cxt.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
    }//w  ww.ja  v  a  2  s  . co  m
}

From source file:edu.stanford.mobisocial.dungbeetle.ui.fragments.AppsViewFragment.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        event.startTracking();/*  w  ww  .  ja va  2 s .c o m*/
        return true;
    }
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        return true;
    }
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
        Intent record = new Intent(getActivity(), VoiceQuickRecordActivity.class);
        record.putExtra("feed_uri", mFeedUri);
        startActivity(record);
        return true;
    }
    return false;
}

From source file:com.koma.music.service.MusicService.java

private void setUpMediaSession() {
    mSession = new MediaSession(this, "KomaMusic");
    mSession.setCallback(new MediaSession.Callback() {
        @Override/* ww w . ja  va 2 s  . c  om*/
        public void onPause() {
            pause();
            mPausedByTransientLossOfFocus = false;
        }

        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onSeekTo(long pos) {
            seek(pos);
        }

        @Override
        public void onSkipToNext() {
            gotoNext(true);
        }

        @Override
        public void onSkipToPrevious() {
            prev(false);
        }

        @Override
        public void onStop() {
            pause();
            mPausedByTransientLossOfFocus = false;
            seek(0);
            releaseServiceUiAndStop();
        }

        @Override
        public void onSkipToQueueItem(long id) {
            setQueuePosition((int) id);
        }

        @Override
        public boolean onMediaButtonEvent(@NonNull Intent mediaButtonIntent) {
            if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonIntent.getAction())) {
                KeyEvent ke = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                if (ke != null && ke.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
                    if (ke.getAction() == KeyEvent.ACTION_UP) {
                        handleHeadsetHookClick(ke.getEventTime());
                    }
                    return true;
                }
            }
            return super.onMediaButtonEvent(mediaButtonIntent);
        }
    });

    PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, MediaButtonIntentReceiver.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setMediaButtonReceiver(pi);

    mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
}

From source file:mobisocial.musubi.ui.fragments.FeedViewFragment.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
        Intent record = new Intent(mActivity, VoiceRecordActivity.class);
        record.putExtra("feed_uri", mFeedUri);
        startActivity(record);//from  w w  w  . j  a  va2 s .  co m
        return true;
    }
    return false;
}

From source file:android.widget.TiVideoView4.java

@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 || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
            if (mMediaPlayer.isPlaying()) {
                pause();/* w  w  w .  j  a v a 2 s  .c  om*/
                mMediaController.show();
            } else {
                start();
                mMediaController.hide();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP && mMediaPlayer.isPlaying()) {
            pause();
            mMediaController.show();
        } else {
            toggleMediaControlsVisiblity();
        }
    }

    return super.onKeyDown(keyCode, event);
}