Java Code Examples for android.view.KeyEvent#DispatcherState

The following examples show how to use android.view.KeyEvent#DispatcherState . 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: MaterialTapTargetPrompt.java    From styT with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (mPromptOptions.getBackButtonDismissEnabled()
            && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                state.startTracking(event, this);
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP
                    && !event.isCanceled() && state.isTracking(event)) {
                if (mPromptTouchedListener != null) {
                    mPromptTouchedListener.onNonFocalPressed();
                }
                return mPromptOptions.getAutoDismiss()
                        || super.dispatchKeyEventPreIme(event);
            }
        }
    }

    return super.dispatchKeyEventPreIme(event);
}
 
Example 2
Source File: BookmarkSearchView.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // To intercept hardware key, a view must have focus.
    if (mDelegate == null) return super.dispatchKeyEvent(event);

    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                state.startTracking(event, this);
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()
                    && state.isTracking(event)) {
                onBackPressed();
                return true;
            }
        }
    }

    return super.dispatchKeyEvent(event);
}
 
Example 3
Source File: BookmarkSearchView.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // To intercept hardware key, a view must have focus.
    if (mDelegate == null) return super.dispatchKeyEvent(event);

    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                state.startTracking(event, this);
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()
                    && state.isTracking(event)) {
                onBackPressed();
                return true;
            }
        }
    }

    return super.dispatchKeyEvent(event);
}
 
Example 4
Source File: BottomSheetLayout.java    From ThreePhasesBottomSheet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && isSheetShowing()) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            KeyEvent.DispatcherState state = getKeyDispatcherState();
            if (state != null) {
                state.startTracking(event, this);
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            KeyEvent.DispatcherState dispatcherState = getKeyDispatcherState();
            if (dispatcherState != null) {
                dispatcherState.handleUpEvent(event);
            }
            if (isSheetShowing() && event.isTracking() && !event.isCanceled()) {
                if (state == State.EXPANDED && peekOnDismiss) {
                    peekSheet();
                } else {
                    dismissSheet();
                }
                return true;
            }
        }
    }
    return super.onKeyPreIme(keyCode, event);
}
 
Example 5
Source File: HoverView.java    From hover with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    // Intercept the hardware back button press if needed. When it's pressed, we'll collapse.
    if (mState.respondsToBackButton() && KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null) {
            if (KeyEvent.ACTION_DOWN == event.getAction()) {
                state.startTracking(event, this);
                return true;
            } else if (KeyEvent.ACTION_UP == event.getAction()) {
                onBackPressed();
                return true;
            }
        }
    }
    return super.dispatchKeyEventPreIme(event);
}
 
Example 6
Source File: BottomSheetLayout.java    From bottomsheet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && isSheetShowing()) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            KeyEvent.DispatcherState state = getKeyDispatcherState();
            if (state != null) {
                state.startTracking(event, this);
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            KeyEvent.DispatcherState dispatcherState = getKeyDispatcherState();
            if (dispatcherState != null) {
                dispatcherState.handleUpEvent(event);
            }
            if (isSheetShowing() && event.isTracking() && !event.isCanceled()) {
                if (state == State.EXPANDED && peekOnDismiss) {
                    peekSheet();
                } else {
                    dismissSheet();
                }
                return true;
            }
        }
    }
    return super.onKeyPreIme(keyCode, event);
}
 
Example 7
Source File: SoftInputWindow.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a SoftInputWindow that uses a custom style.
 * 
 * @param context The Context in which the DockWindow should run. In
 *        particular, it uses the window manager and theme from this context
 *        to present its UI.
 * @param theme A style resource describing the theme to use for the window.
 *        See <a href="{@docRoot}reference/available-resources.html#stylesandthemes">Style
 *        and Theme Resources</a> for more information about defining and
 *        using styles. This theme is applied on top of the current theme in
 *        <var>context</var>. If 0, the default dialog theme will be used.
 */
public SoftInputWindow(Context context, String name, int theme, Callback callback,
        KeyEvent.Callback keyEventCallback, KeyEvent.DispatcherState dispatcherState,
        int windowType, int gravity, boolean takesFocus) {
    super(context, theme);
    mName = name;
    mCallback = callback;
    mKeyEventCallback = keyEventCallback;
    mDispatcherState = dispatcherState;
    mWindowType = windowType;
    mGravity = gravity;
    mTakesFocus = takesFocus;
    initDockWindow();
}
 
Example 8
Source File: ZoomButtonsController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean onContainerKey(KeyEvent event) {
    int keyCode = event.getKeyCode();
    if (isInterestingKey(keyCode)) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                if (mOwnerView != null) {
                    KeyEvent.DispatcherState ds = mOwnerView.getKeyDispatcherState();
                    if (ds != null) {
                        ds.startTracking(event, this);
                    }
                }
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP
                    && event.isTracking() && !event.isCanceled()) {
                setVisible(false);
                return true;
            }

        } else {
            dismissControlsDelayed(ZOOM_CONTROLS_TIMEOUT);
        }

        // Let the container handle the key
        return false;

    } else {

        ViewRootImpl viewRoot = mOwnerView.getViewRootImpl();
        if (viewRoot != null) {
            viewRoot.dispatchInputEvent(event);
        }

        // We gave the key to the owner, don't let the container handle this key
        return true;
    }
}
 
Example 9
Source File: MaterialTapTargetPrompt.java    From MaterialTapTargetPrompt with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event)
{
    if (mPromptOptions.getBackButtonDismissEnabled()
            && event.getKeyCode() == KeyEvent.KEYCODE_BACK)
    {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0)
            {
                state.startTracking(event, this);
                return true;
            }
            else if (event.getAction() == KeyEvent.ACTION_UP
                    && !event.isCanceled() && state.isTracking(event))
            {
                if (mPromptTouchedListener != null)
                {
                    mPromptTouchedListener.onBackButtonPressed();
                }
                return mPromptOptions.getAutoDismiss()
                        || super.dispatchKeyEventPreIme(event);
            }
        }
    }

    return super.dispatchKeyEventPreIme(event);
}
 
Example 10
Source File: PromptViewUnitTest.java    From MaterialTapTargetPrompt with Apache License 2.0 5 votes vote down vote up
@Test
public void testPromptView_BackButton_NotBackKey()
{
    final MaterialTapTargetPrompt.PromptView promptView = createPromptView();
    promptView.mPromptTouchedListener = new MaterialTapTargetPrompt.PromptView.PromptTouchedListener() {
        @Override
        public void onFocalPressed()
        {
            fail();
        }

        @Override
        public void onNonFocalPressed()
        {
            fail();
        }

        @Override
        public void onBackButtonPressed()
        {
            fail();
        }
    };
    KeyEvent.DispatcherState state = new KeyEvent.DispatcherState();
    when(promptView.getKeyDispatcherState()).thenReturn(state);
    final KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_1);
    assertFalse(promptView.dispatchKeyEventPreIme(event));
}
 
Example 11
Source File: PromptViewUnitTest.java    From MaterialTapTargetPrompt with Apache License 2.0 5 votes vote down vote up
@Test
public void testPromptView_BackButton_Disabled()
{
    final MaterialTapTargetPrompt.PromptView promptView = createPromptView();
    promptView.mPromptOptions.setBackButtonDismissEnabled(false);
    promptView.mPromptTouchedListener = new MaterialTapTargetPrompt.PromptView.PromptTouchedListener() {
        @Override
        public void onFocalPressed()
        {
            fail();
        }

        @Override
        public void onNonFocalPressed()
        {
            fail();
        }

        @Override
        public void onBackButtonPressed()
        {
            fail();
        }
    };
    KeyEvent.DispatcherState state = new KeyEvent.DispatcherState();
    when(promptView.getKeyDispatcherState()).thenReturn(state);
    final KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
    assertFalse(promptView.dispatchKeyEventPreIme(event));
}
 
Example 12
Source File: AbstractInputMethodService.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
 * for used for processing events from the target application.
 * Normally you will not need to use this directly, but
 * just use the standard high-level event callbacks like {@link #onKeyDown}.
 */
public KeyEvent.DispatcherState getKeyDispatcherState() {
    return mDispatcherState;
}