Java Code Examples for android.view.KeyEvent#KEYCODE_ESCAPE

The following examples show how to use android.view.KeyEvent#KEYCODE_ESCAPE . 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: Player.java    From M3UPlayer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_MENU) {
		if (mMenu.getVisibility() != View.VISIBLE) {
			mMenu.setVisibility(View.VISIBLE);
			mMenu.requestLayout();
			mMenu.requestFocus();
		} else {
			choose(mPosition);
		}
		return true;
	} else if (keyCode == KeyEvent.KEYCODE_BACK
			|| keyCode == KeyEvent.KEYCODE_ESCAPE) {
		if (mMenu.getVisibility() == View.VISIBLE) {
			mMenu.setVisibility(View.INVISIBLE);
			return true;
		}
	}
	return super.onKeyDown(keyCode, event);
}
 
Example 2
Source File: Dialog.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * A key was released.
 * <p>
 * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK}
 * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE
 * KEYCODE_ESCAPE} to close the dialog.
 *
 * @see #onKeyDown
 * @see android.view.KeyEvent
 */
@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
            && event.isTracking()
            && !event.isCanceled()) {
        onBackPressed();
        return true;
    }
    return false;
}
 
Example 3
Source File: TabWebContentsDelegateAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void handleKeyboardEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && mTab.getActivity() != null) {
        if (mTab.getActivity().onKeyDown(event.getKeyCode(), event)) return;

        // Handle the Escape key here (instead of in KeyboardShortcuts.java), so it doesn't
        // interfere with other parts of the activity (e.g. the URL bar).
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
            WebContents wc = mTab.getWebContents();
            if (wc != null) wc.stop();
            return;
        }
    }
    handleMediaKey(event);
}
 
Example 4
Source File: KeyboardShortcutDialogPreference.java    From talkback with Apache License 2.0 5 votes vote down vote up
private boolean processKeyEvent(KeyEvent event) {
  if (event == null) {
    return false;
  }

  if (event.getRepeatCount() > 1) {
    return false;
  }

  //noinspection SimplifiableIfStatement
  if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
      || event.getKeyCode() == KeyEvent.KEYCODE_HOME
      || event.getKeyCode() == KeyEvent.KEYCODE_CALL
      || event.getKeyCode() == KeyEvent.KEYCODE_ENDCALL) {
    return false;
  }

  // Enter and Esc are used to accept/dismiss dialogs. However, the default shortcuts
  // involve Enter and Esc (with modifiers), so we should only trap Enter and Esc without
  // modifiers.
  boolean isDialogNavigation =
      event.getKeyCode() == KeyEvent.KEYCODE_ENTER
          || event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE;
  if (isDialogNavigation && event.hasNoModifiers()) {
    return false;
  }

  return event.getAction() == KeyEvent.ACTION_DOWN;
}
 
Example 5
Source File: IMEService.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
private boolean handleKeyboardFocusEvent(int keyCode){
	if(mInputView != null) {
		if (Environment.needDebug) {
			Environment.debug(TAG, "handleKeyboardFocusEvent:" + keyCode);
		}
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				if(mInputView.isShown()) {
					requestNextButtonFocus(keyCode);
					return true;
				}
				break;
			case KeyEvent.KEYCODE_ENTER:
			case KeyEvent.KEYCODE_DPAD_CENTER:
				if (mInputView.isShown() && focusedView != null) {
					clickButtonByKey(focusedView);
					return true;
				}
				break;
			case KeyEvent.KEYCODE_CAPS_LOCK:
				toggleCapsState(true);
				return true;
			case KeyEvent.KEYCODE_ESCAPE:
			case KeyEvent.KEYCODE_BACK:
				if (mInputView.isShown()){
					if(helpDialog != null && helpDialog.isShown()){
						helpDialog.setVisibility(View.GONE);
					}else {
						this.finishInput();
					}
					return true;
				}
				break;
		}
	}
	return false;
}
 
Example 6
Source File: LauncherAppWidgetHostView.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mChildrenFocused && event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE
            && event.getAction() == KeyEvent.ACTION_UP) {
        mChildrenFocused = false;
        requestFocus();
        return true;
    }
    return super.dispatchKeyEvent(event);
}
 
Example 7
Source File: TabWebContentsDelegateAndroid.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void handleKeyboardEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && mTab.getActivity() != null) {
        if (mTab.getActivity().onKeyDown(event.getKeyCode(), event)) return;

        // Handle the Escape key here (instead of in KeyboardShortcuts.java), so it doesn't
        // interfere with other parts of the activity (e.g. the URL bar).
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
            WebContents wc = mTab.getWebContents();
            if (wc != null) wc.stop();
            return;
        }
    }
    handleMediaKey(event);
}
 
Example 8
Source File: LocationBarLayout.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (KeyNavigationUtil.isGoDown(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        int suggestionCount = mSuggestionListAdapter.getCount();
        if (mSuggestionList.getSelectedItemPosition() < suggestionCount - 1) {
            if (suggestionCount > 0) mIgnoreOmniboxItemSelection = false;
        } else {
            // Do not pass down events when the last item is already selected as it will
            // dismiss the suggestion list.
            return true;
        }

        if (mSuggestionList.getSelectedItemPosition()
                == ListView.INVALID_POSITION) {
            // When clearing the selection after a text change, state is not reset
            // correctly so hitting down again will cause it to start from the previous
            // selection point. We still have to send the key down event to let the list
            // view items take focus, but then we select the first item explicitly.
            boolean result = mSuggestionList.onKeyDown(keyCode, event);
            mSuggestionList.setSelection(0);
            return result;
        } else {
            return mSuggestionList.onKeyDown(keyCode, event);
        }
    } else if (KeyNavigationUtil.isGoUp(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        if (mSuggestionList.getSelectedItemPosition() != 0
                && mSuggestionListAdapter.getCount() > 0) {
            mIgnoreOmniboxItemSelection = false;
        }
        return mSuggestionList.onKeyDown(keyCode, event);
    } else if (KeyNavigationUtil.isGoRight(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()
            && mSuggestionList.getSelectedItemPosition()
                    != ListView.INVALID_POSITION) {
        OmniboxResultItem selectedItem =
                (OmniboxResultItem) mSuggestionListAdapter.getItem(
                        mSuggestionList.getSelectedItemPosition());
        // Set the UrlBar text to empty, so that it will trigger a text change when we
        // set the text to the suggestion again.
        setUrlBarText("", null);
        mUrlBar.setText(selectedItem.getSuggestion().getFillIntoEdit());
        mSuggestionList.setSelection(0);
        mUrlBar.setSelection(mUrlBar.getText().length());
        return true;
    } else if (KeyNavigationUtil.isEnter(event)
            && LocationBarLayout.this.getVisibility() == VISIBLE) {
        UiUtils.hideKeyboard(mUrlBar);
        mSuggestionSelectionInProgress = true;
        final String urlText = mUrlBar.getTextWithAutocomplete();
        if (mNativeInitialized) {
            findMatchAndLoadUrl(urlText);
        } else {
            mDeferredNativeRunnables.add(new Runnable() {
                @Override
                public void run() {
                    findMatchAndLoadUrl(urlText);
                }
            });
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            // Tell the framework to start tracking this event.
            getKeyDispatcherState().startTracking(event, this);
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                backKeyPressed();
                return true;
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            revertChanges();
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: TimePickerDialog.java    From Conquer with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if (mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
            (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 10
Source File: TimePickerDialog.java    From MaterialDateRangePicker with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        if(isCancelable()) dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if(mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes(),mTimePickerEnd.getHours(), mTimePickerEnd.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                if(tabHost.getCurrentTab()==0){
                    Utils.tryAccessibilityAnnounce(mTimePicker,
                            String.format(mDeletedKeyFormat, deletedKeyStr));
                }else{
                    Utils.tryAccessibilityAnnounce(mTimePickerEnd,
                            String.format(mDeletedKeyFormat, deletedKeyStr));
                }

                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
                    (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 11
Source File: SwitchTimePicker.java    From Android-SwitchDateTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        //dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if (mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (onTimeSelectedListener != null) {
            onTimeSelectedListener.onTimeSelected(mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        //dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format(Locale.getDefault(), "%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
            (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 12
Source File: TimePickerDialog.java    From Blackbulb with GNU General Public License v3.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        if(isCancelable()) dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if(mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(this,
                    mTimePicker.getHours(), mTimePicker.getMinutes(), mTimePicker.getSeconds());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
                    (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 13
Source File: GridTimePickerDialog.java    From BottomSheetPickers with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if(mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(HALF_DAY_1)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(HALF_DAY_2)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
                    (keyCode == getAmOrPmKeyCode(HALF_DAY_1) || keyCode == getAmOrPmKeyCode(HALF_DAY_2)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 14
Source File: KeyboardShortcuts.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * This should be called from the Activity's dispatchKeyEvent() to handle keyboard shortcuts.
 *
 * Note: dispatchKeyEvent() is called before the active view or web page gets a chance to handle
 * the key event. So the keys handled here cannot be overridden by any view or web page.
 *
 * @param event The KeyEvent to handle.
 * @param activity The ChromeActivity in which the key was pressed.
 * @param uiInitialized Whether the UI has been initialized. If this is false, most keys will
 *                      not be handled.
 * @return True if the event was handled. False if the event was ignored. Null if the event
 *         should be handled by the activity's parent class.
 */
@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public static Boolean dispatchKeyEvent(KeyEvent event, ChromeActivity activity,
        boolean uiInitialized) {
    int keyCode = event.getKeyCode();
    if (!uiInitialized) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU) return true;
        return null;
    }

    switch (keyCode) {
        case KeyEvent.KEYCODE_SEARCH:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false);
            }
            // Always consume the SEARCH key events to prevent android from showing
            // the default app search UI, which locks up Chrome.
            return true;
        case KeyEvent.KEYCODE_MENU:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.show_menu, false);
            }
            return true;
        case KeyEvent.KEYCODE_ESCAPE:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if (activity.exitFullscreenIfShowing()) return true;
            }
            break;
        case KeyEvent.KEYCODE_TV:
        case KeyEvent.KEYCODE_GUIDE:
        case KeyEvent.KEYCODE_DVR:
        case KeyEvent.KEYCODE_AVR_INPUT:
        case KeyEvent.KEYCODE_AVR_POWER:
        case KeyEvent.KEYCODE_STB_INPUT:
        case KeyEvent.KEYCODE_STB_POWER:
        case KeyEvent.KEYCODE_TV_INPUT:
        case KeyEvent.KEYCODE_TV_POWER:
        case KeyEvent.KEYCODE_WINDOW:
            // Do not consume the AV device-related keys so that the system will take
            // an appropriate action, such as switching to TV mode.
            return false;
    }

    return null;
}
 
Example 15
Source File: LocationBarLayout.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (KeyNavigationUtil.isGoDown(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        int suggestionCount = mSuggestionListAdapter.getCount();
        if (mSuggestionList.getSelectedItemPosition() < suggestionCount - 1) {
            if (suggestionCount > 0) mIgnoreOmniboxItemSelection = false;
        } else {
            // Do not pass down events when the last item is already selected as it will
            // dismiss the suggestion list.
            return true;
        }

        if (mSuggestionList.getSelectedItemPosition()
                == ListView.INVALID_POSITION) {
            // When clearing the selection after a text change, state is not reset
            // correctly so hitting down again will cause it to start from the previous
            // selection point. We still have to send the key down event to let the list
            // view items take focus, but then we select the first item explicitly.
            boolean result = mSuggestionList.onKeyDown(keyCode, event);
            mSuggestionList.setSelection(0);
            return result;
        } else {
            return mSuggestionList.onKeyDown(keyCode, event);
        }
    } else if (KeyNavigationUtil.isGoUp(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        if (mSuggestionList.getSelectedItemPosition() != 0
                && mSuggestionListAdapter.getCount() > 0) {
            mIgnoreOmniboxItemSelection = false;
        }
        return mSuggestionList.onKeyDown(keyCode, event);
    } else if (KeyNavigationUtil.isGoRight(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()
            && mSuggestionList.getSelectedItemPosition()
                    != ListView.INVALID_POSITION) {
        OmniboxResultItem selectedItem =
                (OmniboxResultItem) mSuggestionListAdapter.getItem(
                        mSuggestionList.getSelectedItemPosition());
        // Set the UrlBar text to empty, so that it will trigger a text change when we
        // set the text to the suggestion again.
        setUrlBarText("", null);
        mUrlBar.setText(selectedItem.getSuggestion().getFillIntoEdit());
        mSuggestionList.setSelection(0);
        mUrlBar.setSelection(mUrlBar.getText().length());
        return true;
    } else if (KeyNavigationUtil.isEnter(event)
            && LocationBarLayout.this.getVisibility() == VISIBLE) {
        UiUtils.hideKeyboard(mUrlBar);
        mSuggestionSelectionInProgress = true;
        final String urlText = mUrlBar.getQueryText();
        if (mNativeInitialized) {
            findMatchAndLoadUrl(urlText);
        } else {
            mDeferredNativeRunnables.add(new Runnable() {
                @Override
                public void run() {
                    findMatchAndLoadUrl(urlText);
                }
            });
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            // Tell the framework to start tracking this event.
            getKeyDispatcherState().startTracking(event, this);
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                backKeyPressed();
                return true;
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            revertChanges();
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: TimePickerDialog.java    From narrate-android with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if(mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
                    (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 17
Source File: TimePickerDialog.java    From DateTimepicker with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_TAB) {
        if(mInKbMode) {
            if (isTypedTimeFullyLegal()) {
                finishKbMode(true);
            }
            return true;
        }
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
        if (mInKbMode) {
            if (!isTypedTimeFullyLegal()) {
                return true;
            }
            finishKbMode(false);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                Utils.tryAccessibilityAnnounce(mTimePicker,
                        String.format(mDeletedKeyFormat, deletedKeyStr));
                updateDisplay(true);
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
            || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
            || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
            || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
            || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
            || (!mIs24HourMode &&
                    (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mTimePicker == null) {
                // Something's wrong, because time picker should definitely not be null.
                Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
                return true;
            }
            mTypedTimes.clear();
            tryStartingKbMode(keyCode);
            return true;
        }
        // We're already in keyboard mode.
        if (addKeyIfLegal(keyCode)) {
            updateDisplay(false);
        }
        return true;
    }
    return false;
}
 
Example 18
Source File: KeyboardShortcuts.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * This should be called from the Activity's dispatchKeyEvent() to handle keyboard shortcuts.
 *
 * Note: dispatchKeyEvent() is called before the active view or web page gets a chance to handle
 * the key event. So the keys handled here cannot be overridden by any view or web page.
 *
 * @param event The KeyEvent to handle.
 * @param activity The ChromeActivity in which the key was pressed.
 * @param uiInitialized Whether the UI has been initialized. If this is false, most keys will
 *                      not be handled.
 * @return True if the event was handled. False if the event was ignored. Null if the event
 *         should be handled by the activity's parent class.
 */
@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public static Boolean dispatchKeyEvent(KeyEvent event, ChromeActivity activity,
        boolean uiInitialized) {
    int keyCode = event.getKeyCode();
    if (!uiInitialized) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU) return true;
        return null;
    }

    switch (keyCode) {
        case KeyEvent.KEYCODE_SEARCH:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false);
            }
            // Always consume the SEARCH key events to prevent android from showing
            // the default app search UI, which locks up Chrome.
            return true;
        case KeyEvent.KEYCODE_MENU:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.show_menu, false);
            }
            return true;
        case KeyEvent.KEYCODE_ESCAPE:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if (activity.exitFullscreenIfShowing()) return true;
            }
            break;
        case KeyEvent.KEYCODE_TV:
        case KeyEvent.KEYCODE_GUIDE:
        case KeyEvent.KEYCODE_DVR:
        case KeyEvent.KEYCODE_AVR_INPUT:
        case KeyEvent.KEYCODE_AVR_POWER:
        case KeyEvent.KEYCODE_STB_INPUT:
        case KeyEvent.KEYCODE_STB_POWER:
        case KeyEvent.KEYCODE_TV_INPUT:
        case KeyEvent.KEYCODE_TV_POWER:
        case KeyEvent.KEYCODE_WINDOW:
            // Do not consume the AV device-related keys so that the system will take
            // an appropriate action, such as switching to TV mode.
            return false;
    }

    return null;
}
 
Example 19
Source File: PersianTimePickerDialog.java    From PersianDateRangePicker with Apache License 2.0 4 votes vote down vote up
/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key.
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
  if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
    if (isCancelable()) {
      dismiss();
    }
    return true;
  } else if (keyCode == KeyEvent.KEYCODE_TAB) {
    if (mInKbMode) {
      if (isTypedTimeFullyLegal()) {
        finishKbMode(true);
      }
      return true;
    }
  } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
    if (mInKbMode) {
      if (!isTypedTimeFullyLegal()) {
        return true;
      }
      finishKbMode(false);
    }
    if (mCallback != null) {
      mCallback.onTimeSet(mTimePicker,
        mTimePicker.getHours(), mTimePicker.getMinutes(), mTimePickerEnd.getHours(), mTimePickerEnd.getMinutes());
    }
    dismiss();
    return true;
  } else if (keyCode == KeyEvent.KEYCODE_DEL) {
    if (mInKbMode) {
      if (!mTypedTimes.isEmpty()) {
        int deleted = deleteLastTypedKey();
        String deletedKeyStr;
        if (deleted == getAmOrPmKeyCode(AM)) {
          deletedKeyStr = mAmText;
        } else if (deleted == getAmOrPmKeyCode(PM)) {
          deletedKeyStr = mPmText;
        } else {
          deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
        }
        if (tabHost.getCurrentTab() == 1) {
          Utils.tryAccessibilityAnnounce(mTimePicker,
            String.format(mDeletedKeyFormat, deletedKeyStr));
        } else {
          Utils.tryAccessibilityAnnounce(mTimePickerEnd,
            String.format(mDeletedKeyFormat, deletedKeyStr));
        }

        updateDisplay(true);
      }
    }
  } else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
    || keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
    || keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
    || keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
    || keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
    || (!mIs24HourMode &&
    (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
    if (!mInKbMode) {
      if (mTimePicker == null) {
        // Something's wrong, because time picker should definitely not be null.
        Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
        return true;
      }
      mTypedTimes.clear();
      tryStartingKbMode(keyCode);
      return true;
    }
    // We're already in keyboard mode.
    if (addKeyIfLegal(keyCode)) {
      updateDisplay(false);
    }
    return true;
  }
  return false;
}
 
Example 20
Source File: Dialog.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * A key was pressed down.
 * <p>
 * If the focused view didn't want this event, this method is called.
 * <p>
 * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK}
 * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE
 * KEYCODE_ESCAPE} to later handle them in {@link #onKeyUp}.
 * 
 * @see #onKeyUp
 * @see android.view.KeyEvent
 */
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
        event.startTracking();
        return true;
    }

    return false;
}