Java Code Examples for android.view.KeyEvent#FLAG_FROM_SYSTEM

The following examples show how to use android.view.KeyEvent#FLAG_FROM_SYSTEM . 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: PieController.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
public void handleMessage(Message m) {
    switch (m.what) {
        case MSG_INJECT_KEY:
            final long eventTime = SystemClock.uptimeMillis();
            final InputManager inputManager = (InputManager)
                    XposedHelpers.callStaticMethod(InputManager.class, "getInstance");

            int flags = KeyEvent.FLAG_FROM_SYSTEM;
            XposedHelpers.callMethod(inputManager, "injectInputEvent",
                    new KeyEvent(eventTime - 50, eventTime - 50, KeyEvent.ACTION_DOWN, m.arg1, 0,
                            0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_UNKNOWN), 0);
            XposedHelpers.callMethod(inputManager, "injectInputEvent",
                    new KeyEvent(eventTime - 50, eventTime - 25, KeyEvent.ACTION_UP, m.arg1, 0,
                            0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_UNKNOWN), 0);

            break;
    }
}
 
Example 2
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 3
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 4
Source File: Instrumentation.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 5
Source File: KeyButtonView.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
void sendEvent(int action, int flags, long when, boolean applyDefaultFlags) {
    try {
        final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;
        if (applyDefaultFlags) {
            flags |= KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY;
        }
        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount,
                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags,
                InputDevice.SOURCE_KEYBOARD);
        final Object inputManager = XposedHelpers.callStaticMethod(InputManager.class, "getInstance");
        XposedHelpers.callMethod(inputManager, "injectInputEvent", ev, 0);
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 6
Source File: ModVolumeKeySkipTrack.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
    if (!allowSkipTrack) return;

    final KeyEvent event = (KeyEvent) param.args[0];
    final int keyCode = event.getKeyCode();
    initManagers((Context) XposedHelpers.getObjectField(param.thisObject, "mContext"));
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
            keyCode == KeyEvent.KEYCODE_VOLUME_UP) &&
            (event.getFlags() & KeyEvent.FLAG_FROM_SYSTEM) != 0 &&
            !mPowerManager.isInteractive() &&
            mAudioManager != null && mAudioManager.isMusicActive()) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            mIsLongPress = false;
            handleVolumeLongPress(param.thisObject, keyCode);
        } else {
            handleVolumeLongPressAbort(param.thisObject);
            if (!mIsLongPress) {
                if (mShoudTriggerWakeUp) {
                    wakeUp();
                } else {
                    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        keyCode == KeyEvent.KEYCODE_VOLUME_UP ?
                        AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER, 0);
                }
            }
        }
        param.setResult(0);
        return;
    }
}
 
Example 7
Source File: Instrumentation.java    From droidel with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 8
Source File: VolumeAccessibilityService.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean onKeyEvent(KeyEvent event) {
    if (null == event) return true; // WTF??

    // Make sure we're not in the middle of a phone call.
    if (null != mVolumePanel && mVolumePanel.getCallState() != TelephonyManager.CALL_STATE_IDLE)
        return super.onKeyEvent(event);

    final int flags = event.getFlags();
    final int code = event.getKeyCode();
    final boolean system = ((flags & KeyEvent.FLAG_FROM_SYSTEM) == KeyEvent.FLAG_FROM_SYSTEM);

    // Specifically avoid software keys or "fake" hardware buttons.
    if (((flags & KeyEvent.FLAG_SOFT_KEYBOARD) == KeyEvent.FLAG_SOFT_KEYBOARD) ||
        ((flags & KeyEvent.FLAG_VIRTUAL_HARD_KEY) == KeyEvent.FLAG_VIRTUAL_HARD_KEY)) {
        return super.onKeyEvent(event);
    } else {
        // Specifically avoid handling certain keys. We never want to interfere
        // with them or harm performance in any way.
        switch (code) {
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_HOME:
            case KeyEvent.KEYCODE_MENU:
            case KeyEvent.KEYCODE_POWER:
            case KeyEvent.KEYCODE_SEARCH:
                return super.onKeyEvent(event);
        }
    }

    if (!system) return super.onKeyEvent(event);

    LOGI(TAG, "--onKeyEvent(code=" + event.getKeyCode() + ", action=" + event.getAction() +
            ", topPackage=" + mCurrentActivityPackage + ", disabledButtons=" + disabledButtons + ')');

    // Check if we're supposed to disable Noyze for a Blacklisted app.
    if (blacklist.contains(mCurrentActivityPackage)) {
        if (null != mVolumePanel) mVolumePanel.setEnabled(false);
        return super.onKeyEvent(event);
    } else {
        // NOTE: we need a "safe" way to enable the volume panel that
        // takes into consideration its previous state.
        if (null != mVolumePanel) mVolumePanel.enable();
    }

    // If we're told to disable one or more of the volume buttons, do so (returning true consumes the event).
    if (disabledButtons == code) return true;
    // NOTE: KeyEvent#KEYCODE_VOLUME_DOWN + KeyEvent#KEYCODE_VOLUME_UP == KeyEvent_KEYCODE_U
    final int upAndDown = (KeyEvent.KEYCODE_VOLUME_DOWN + KeyEvent.KEYCODE_VOLUME_UP); // 49
    final int upSquared = KeyEvent.KEYCODE_VOLUME_UP * KeyEvent.KEYCODE_VOLUME_UP; // 576
    if (disabledButtons == upAndDown && Utils.isVolumeKeyCode(upAndDown - code)) return true;
    if (disabledButtons == upSquared && mVolumePanel != null && mVolumePanel.isLocked()) return true;
    if (disabledButtons == upSquared && KEYGUARD.equals(mCurrentActivityPackage)) return true;

    // Check if the volume panel has been disabled, or shouldn't handle this event (e.g. "safe volume").
    if (null != mVolumePanel && mVolumePanel.isEnabled()) {
        if (Utils.isVolumeKeyCode(code)) {
            Message.obtain(mHandler, MESSAGE_KEY_EVENT, event).sendToTarget(); // Run asynchronously
            return true;
        }
    }

	return super.onKeyEvent(event);
}
 
Example 9
Source File: VolumeAccessibilityService.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean onKeyEvent(KeyEvent event) {
    if (null == event) return true; // WTF??

    // Make sure we're not in the middle of a phone call.
    if (null != mVolumePanel && mVolumePanel.getCallState() != TelephonyManager.CALL_STATE_IDLE)
        return super.onKeyEvent(event);

    final int flags = event.getFlags();
    final int code = event.getKeyCode();
    final boolean system = ((flags & KeyEvent.FLAG_FROM_SYSTEM) == KeyEvent.FLAG_FROM_SYSTEM);

    // Specifically avoid software keys or "fake" hardware buttons.
    if (((flags & KeyEvent.FLAG_SOFT_KEYBOARD) == KeyEvent.FLAG_SOFT_KEYBOARD) ||
        ((flags & KeyEvent.FLAG_VIRTUAL_HARD_KEY) == KeyEvent.FLAG_VIRTUAL_HARD_KEY)) {
        return super.onKeyEvent(event);
    } else {
        // Specifically avoid handling certain keys. We never want to interfere
        // with them or harm performance in any way.
        switch (code) {
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_HOME:
            case KeyEvent.KEYCODE_MENU:
            case KeyEvent.KEYCODE_POWER:
            case KeyEvent.KEYCODE_SEARCH:
                return super.onKeyEvent(event);
        }
    }

    if (!system) return super.onKeyEvent(event);

    LOGI(TAG, "--onKeyEvent(code=" + event.getKeyCode() + ", action=" + event.getAction() +
            ", topPackage=" + mCurrentActivityPackage + ", disabledButtons=" + disabledButtons + ')');

    // Check if we're supposed to disable Noyze for a Blacklisted app.
    if (blacklist.contains(mCurrentActivityPackage)) {
        if (null != mVolumePanel) mVolumePanel.setEnabled(false);
        return super.onKeyEvent(event);
    } else {
        // NOTE: we need a "safe" way to enable the volume panel that
        // takes into consideration its previous state.
        if (null != mVolumePanel) mVolumePanel.enable();
    }

    // If we're told to disable one or more of the volume buttons, do so (returning true consumes the event).
    if (disabledButtons == code) return true;
    // NOTE: KeyEvent#KEYCODE_VOLUME_DOWN + KeyEvent#KEYCODE_VOLUME_UP == KeyEvent_KEYCODE_U
    final int upAndDown = (KeyEvent.KEYCODE_VOLUME_DOWN + KeyEvent.KEYCODE_VOLUME_UP); // 49
    final int upSquared = KeyEvent.KEYCODE_VOLUME_UP * KeyEvent.KEYCODE_VOLUME_UP; // 576
    if (disabledButtons == upAndDown && Utils.isVolumeKeyCode(upAndDown - code)) return true;
    if (disabledButtons == upSquared && mVolumePanel != null && mVolumePanel.isLocked()) return true;
    if (disabledButtons == upSquared && KEYGUARD.equals(mCurrentActivityPackage)) return true;

    // Check if the volume panel has been disabled, or shouldn't handle this event (e.g. "safe volume").
    if (null != mVolumePanel && mVolumePanel.isEnabled()) {
        if (Utils.isVolumeKeyCode(code)) {
            Message.obtain(mHandler, MESSAGE_KEY_EVENT, event).sendToTarget(); // Run asynchronously
            return true;
        }
    }

	return super.onKeyEvent(event);
}