Java Code Examples for android.view.KeyEvent#changeAction()

The following examples show how to use android.view.KeyEvent#changeAction() . 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: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void handleVoiceKeyEventLocked(String packageName, int pid, int uid,
        boolean asSystemService, KeyEvent keyEvent, boolean needWakeLock) {
    int action = keyEvent.getAction();
    boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0;
    if (action == KeyEvent.ACTION_DOWN) {
        if (keyEvent.getRepeatCount() == 0) {
            mVoiceButtonDown = true;
            mVoiceButtonHandled = false;
        } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) {
            mVoiceButtonHandled = true;
            startVoiceInput(needWakeLock);
        }
    } else if (action == KeyEvent.ACTION_UP) {
        if (mVoiceButtonDown) {
            mVoiceButtonDown = false;
            if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
                // Resend the down then send this event through
                KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN);
                dispatchMediaKeyEventLocked(packageName, pid, uid, asSystemService,
                        downEvent, needWakeLock);
                dispatchMediaKeyEventLocked(packageName, pid, uid, asSystemService,
                        keyEvent, needWakeLock);
            }
        }
    }
}
 
Example 2
Source File: AudioHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * android.media.IAudioService#dispatchMediaKeyEvent(KeyEvent), except if this
 * method fails for any reason, fall back on broadcasting an event.
 */
public boolean dispatchMediaKeyEvent(Context mContext, int keyCode) {
    // We'll try the public API for API 19+, then the reflected API, then
    // finally we'll resort to broadcasting the action ourselves!
    if (isHTC(mContext) || !_dispatchMediaKeyEvent(mManager, keyCode)) {
        long eventtime = SystemClock.uptimeMillis();
        KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
        KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);
        Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown);
        mContext.sendOrderedBroadcast(keyIntent, null);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp);
        mContext.sendOrderedBroadcast(keyIntent, null);
        return false;
    }
    return true;
}
 
Example 3
Source File: AudioHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * android.media.IAudioService#dispatchMediaKeyEvent(KeyEvent), except if this
 * method fails for any reason, fall back on broadcasting an event.
 */
public boolean dispatchMediaKeyEvent(Context mContext, int keyCode) {
    // We'll try the public API for API 19+, then the reflected API, then
    // finally we'll resort to broadcasting the action ourselves!
    if (isHTC(mContext) || !_dispatchMediaKeyEvent(mManager, keyCode)) {
        long eventtime = SystemClock.uptimeMillis();
        KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
        KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);
        Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown);
        mContext.sendOrderedBroadcast(keyIntent, null);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp);
        mContext.sendOrderedBroadcast(keyIntent, null);
        return false;
    }
    return true;
}
 
Example 4
Source File: ModVolumeKeySkipTrack.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
private static void sendMediaButtonEvent(Object phoneWindowManager, int code) {
    long eventtime = SystemClock.uptimeMillis();
    Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent keyEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, code, 0);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
    dispatchMediaButtonEvent(keyEvent);

    keyEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
    dispatchMediaButtonEvent(keyEvent);
}
 
Example 5
Source File: AudioHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static boolean _dispatchMediaKeyEvent(AudioManager mManager, int keyCode) {
    long eventtime = SystemClock.uptimeMillis();
    KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
    KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);

    // KitKat this API is public, so use it!
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        dispatchMediaKeyEvent(mManager, keyUp);
        dispatchMediaKeyEvent(mManager, keyDown);
        return true;
    }

    // Otherwise try dispatching using Reflection.
    return (_dispatchMediaKeyEvent(keyDown) && _dispatchMediaKeyEvent(keyUp));
}
 
Example 6
Source File: AudioHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/** Use to send {@link android.content.Intent#ACTION_MEDIA_BUTTON} to this application. */
public static void dispatchMediaKeyEventSelf(Context mContext, int keyCode) {
    long eventtime = SystemClock.uptimeMillis();
    KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
    KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);
    Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    keyIntent.setPackage(mContext.getPackageName());
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown);
    mContext.sendOrderedBroadcast(keyIntent, null);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp);
    mContext.sendOrderedBroadcast(keyIntent, null);
}
 
Example 7
Source File: AudioHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static boolean _dispatchMediaKeyEvent(AudioManager mManager, int keyCode) {
    long eventtime = SystemClock.uptimeMillis();
    KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
    KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);

    // KitKat this API is public, so use it!
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        dispatchMediaKeyEvent(mManager, keyUp);
        dispatchMediaKeyEvent(mManager, keyDown);
        return true;
    }

    // Otherwise try dispatching using Reflection.
    return (_dispatchMediaKeyEvent(keyDown) && _dispatchMediaKeyEvent(keyUp));
}
 
Example 8
Source File: AudioHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/** Use to send {@link android.content.Intent#ACTION_MEDIA_BUTTON} to this application. */
public static void dispatchMediaKeyEventSelf(Context mContext, int keyCode) {
    long eventtime = SystemClock.uptimeMillis();
    KeyEvent keyDown = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
    KeyEvent keyUp = KeyEvent.changeAction(keyDown, KeyEvent.ACTION_UP);
    Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    keyIntent.setPackage(mContext.getPackageName());
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown);
    mContext.sendOrderedBroadcast(keyIntent, null);
    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp);
    mContext.sendOrderedBroadcast(keyIntent, null);
}
 
Example 9
Source File: InputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
boolean doMovementKey(int keyCode, KeyEvent event, int count) {
    final ExtractEditText eet = getExtractEditTextIfVisible();
    if (eet != null) {
        // If we are in fullscreen mode, the cursor will move around
        // the extract edit text, but should NOT cause focus to move
        // to other fields.
        MovementMethod movement = eet.getMovementMethod();
        Layout layout = eet.getLayout();
        if (movement != null && layout != null) {
            // We want our own movement method to handle the key, so the
            // cursor will properly move in our own word wrapping.
            if (count == MOVEMENT_DOWN) {
                if (movement.onKeyDown(eet, eet.getText(), keyCode, event)) {
                    reportExtractedMovement(keyCode, 1);
                    return true;
                }
            } else if (count == MOVEMENT_UP) {
                if (movement.onKeyUp(eet, eet.getText(), keyCode, event)) {
                    return true;
                }
            } else {
                if (movement.onKeyOther(eet, eet.getText(), event)) {
                    reportExtractedMovement(keyCode, count);
                } else {
                    KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN);
                    if (movement.onKeyDown(eet, eet.getText(), keyCode, down)) {
                        KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
                        movement.onKeyUp(eet, eet.getText(), keyCode, up);
                        while (--count > 0) {
                            movement.onKeyDown(eet, eet.getText(), keyCode, down);
                            movement.onKeyUp(eet, eet.getText(), keyCode, up);
                        }
                        reportExtractedMovement(keyCode, count);
                    }
                }
            }
        }
        // Regardless of whether the movement method handled the key,
        // we never allow DPAD navigation to the application.
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_DOWN:
                return true;
        }
    }

    return false;
}