Example usage for android.view KeyEvent KEYCODE_6

List of usage examples for android.view KeyEvent KEYCODE_6

Introduction

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

Prototype

int KEYCODE_6

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

Click Source Link

Document

Key code constant: '6' key.

Usage

From source file:com.codetroopers.betterpickers.radialtimepicker.RadialTimePickerDialogFragment.java

/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key./*from  ww w .  j  a  va2s . com*/
 * @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);
        }
        doneClickValidateAndCallback();
        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;
}

From source file:cn.fulldroid.lib.datetimepicker.time.TimePickerDialog.java

/**
 * Create a tree for deciding what keys can legally be typed.
 *///from  w  ww .j  a  v a2 s  .co  m
private void generateLegalTimesTree() {
    // Create a quick cache of numbers to their keycodes.
    int k0 = KeyEvent.KEYCODE_0;
    int k1 = KeyEvent.KEYCODE_1;
    int k2 = KeyEvent.KEYCODE_2;
    int k3 = KeyEvent.KEYCODE_3;
    int k4 = KeyEvent.KEYCODE_4;
    int k5 = KeyEvent.KEYCODE_5;
    int k6 = KeyEvent.KEYCODE_6;
    int k7 = KeyEvent.KEYCODE_7;
    int k8 = KeyEvent.KEYCODE_8;
    int k9 = KeyEvent.KEYCODE_9;

    // The root of the tree doesn't contain any numbers.
    mLegalTimesTree = new Node();
    if (mIs24HourMode) {
        // We'll be re-using these nodes, so we'll save them.
        Node minuteFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
        Node minuteSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        // The first digit must be followed by the second digit.
        minuteFirstDigit.addChild(minuteSecondDigit);

        // The first digit may be 0-1.
        Node firstDigit = new Node(k0, k1);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 0-1, the second digit may be 0-5.
        Node secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);
        // We may now be followed by the first minute digit. E.g. 00:09, 15:58.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 0-1, and the second digit is 0-5, the third digit may be 6-9.
        Node thirdDigit = new Node(k6, k7, k8, k9);
        // The time must now be finished. E.g. 0:55, 1:08.
        secondDigit.addChild(thirdDigit);

        // When the first digit is 0-1, the second digit may be 6-9.
        secondDigit = new Node(k6, k7, k8, k9);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 06:50, 18:20.
        secondDigit.addChild(minuteFirstDigit);

        // The first digit may be 2.
        firstDigit = new Node(k2);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 2, the second digit may be 0-3.
        secondDigit = new Node(k0, k1, k2, k3);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 20:50, 23:09.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 2, the second digit may be 4-5.
        secondDigit = new Node(k4, k5);
        firstDigit.addChild(secondDigit);
        // We must now be followd by the last minute digit. E.g. 2:40, 2:53.
        secondDigit.addChild(minuteSecondDigit);

        // The first digit may be 3-9.
        firstDigit = new Node(k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We must now be followed by the first minute digit. E.g. 3:57, 8:12.
        firstDigit.addChild(minuteFirstDigit);
    } else {
        // We'll need to use the AM/PM node a lot.
        // Set up AM and PM to respond to "a" and "p".
        Node ampm = new Node(getAmOrPmKeyCode(AM), getAmOrPmKeyCode(PM));

        // The first hour digit may be 1.
        Node firstDigit = new Node(k1);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour times. E.g. 1pm.
        firstDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 0-2.
        Node secondDigit = new Node(k0, k1, k2);
        firstDigit.addChild(secondDigit);
        // Also for quick input of on-the-hour times. E.g. 10pm, 12am.
        secondDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 0-5.
        Node thirdDigit = new Node(k0, k1, k2, k3, k4, k5);
        secondDigit.addChild(thirdDigit);
        // The time may be finished now. E.g. 1:02pm, 1:25am.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5,
        // the fourth digit may be 0-9.
        Node fourthDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        thirdDigit.addChild(fourthDigit);
        // The time must be finished now. E.g. 10:49am, 12:40pm.
        fourthDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 6-9.
        thirdDigit = new Node(k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:08am, 1:26pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 3-5.
        secondDigit = new Node(k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 1, and the second digit is 3-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:39am, 1:50pm.
        thirdDigit.addChild(ampm);

        // The hour digit may be 2-9.
        firstDigit = new Node(k2, k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour-times. E.g. 2am, 5pm.
        firstDigit.addChild(ampm);

        // When the first digit is 2-9, the second digit may be 0-5.
        secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 2-9, and the second digit is 0-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 2:57am, 9:30pm.
        thirdDigit.addChild(ampm);
    }
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.SublimeTimePicker.java

/**
 * For keyboard mode, processes key events.
 *
 * @param keyCode the pressed key.//www .ja  va 2  s .c o m
 * @return true if the key was successfully processed, false otherwise.
 */
private boolean processKeyUp(int keyCode) {
    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));
                }

                AccessibilityUtils.makeAnnouncement(this, 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
            || (!mIs24HourView && (keyCode == getAmOrPmKeyCode(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        if (!mInKbMode) {
            if (mRadialTimePickerView == null) {
                // Something is 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;
}

From source file:com.borax12.materialdaterangepicker.time.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key./*from w  w  w .  j  ava2s.c o  m*/
 * @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;
}

From source file:com.philliphsu.bottomsheetpickers.time.grid.GridTimePickerDialog.java

/**
 * Create a tree for deciding what keys can legally be typed.
 *///from w w  w .j av  a 2 s .c o  m
private void generateLegalTimesTree() {
    // Create a quick cache of numbers to their keycodes.
    int k0 = KeyEvent.KEYCODE_0;
    int k1 = KeyEvent.KEYCODE_1;
    int k2 = KeyEvent.KEYCODE_2;
    int k3 = KeyEvent.KEYCODE_3;
    int k4 = KeyEvent.KEYCODE_4;
    int k5 = KeyEvent.KEYCODE_5;
    int k6 = KeyEvent.KEYCODE_6;
    int k7 = KeyEvent.KEYCODE_7;
    int k8 = KeyEvent.KEYCODE_8;
    int k9 = KeyEvent.KEYCODE_9;

    // The root of the tree doesn't contain any numbers.
    mLegalTimesTree = new Node();
    if (mIs24HourMode) {
        // We'll be re-using these nodes, so we'll save them.
        Node minuteFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
        Node minuteSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        // The first digit must be followed by the second digit.
        minuteFirstDigit.addChild(minuteSecondDigit);

        // The first digit may be 0-1.
        Node firstDigit = new Node(k0, k1);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 0-1, the second digit may be 0-5.
        Node secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);
        // We may now be followed by the first minute digit. E.g. 00:09, 15:58.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 0-1, and the second digit is 0-5, the third digit may be 6-9.
        Node thirdDigit = new Node(k6, k7, k8, k9);
        // The time must now be finished. E.g. 0:55, 1:08.
        secondDigit.addChild(thirdDigit);

        // When the first digit is 0-1, the second digit may be 6-9.
        secondDigit = new Node(k6, k7, k8, k9);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 06:50, 18:20.
        secondDigit.addChild(minuteFirstDigit);

        // The first digit may be 2.
        firstDigit = new Node(k2);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 2, the second digit may be 0-3.
        secondDigit = new Node(k0, k1, k2, k3);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 20:50, 23:09.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 2, the second digit may be 4-5.
        secondDigit = new Node(k4, k5);
        firstDigit.addChild(secondDigit);
        // We must now be followd by the last minute digit. E.g. 2:40, 2:53.
        secondDigit.addChild(minuteSecondDigit);

        // The first digit may be 3-9.
        firstDigit = new Node(k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We must now be followed by the first minute digit. E.g. 3:57, 8:12.
        firstDigit.addChild(minuteFirstDigit);
    } else {
        // We'll need to use the AM/PM node a lot.
        // Set up AM and PM to respond to "a" and "p".
        Node ampm = new Node(getAmOrPmKeyCode(HALF_DAY_1), getAmOrPmKeyCode(HALF_DAY_2));

        // The first hour digit may be 1.
        Node firstDigit = new Node(k1);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour times. E.g. 1pm.
        firstDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 0-2.
        Node secondDigit = new Node(k0, k1, k2);
        firstDigit.addChild(secondDigit);
        // Also for quick input of on-the-hour times. E.g. 10pm, 12am.
        secondDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 0-5.
        Node thirdDigit = new Node(k0, k1, k2, k3, k4, k5);
        secondDigit.addChild(thirdDigit);
        // The time may be finished now. E.g. 1:02pm, 1:25am.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5,
        // the fourth digit may be 0-9.
        Node fourthDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        thirdDigit.addChild(fourthDigit);
        // The time must be finished now. E.g. 10:49am, 12:40pm.
        fourthDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 6-9.
        thirdDigit = new Node(k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:08am, 1:26pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 3-5.
        secondDigit = new Node(k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 1, and the second digit is 3-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:39am, 1:50pm.
        thirdDigit.addChild(ampm);

        // The hour digit may be 2-9.
        firstDigit = new Node(k2, k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour-times. E.g. 2am, 5pm.
        firstDigit.addChild(ampm);

        // When the first digit is 2-9, the second digit may be 0-5.
        secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 2-9, and the second digit is 0-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 2:57am, 9:30pm.
        thirdDigit.addChild(ampm);
    }
}

From source file:com.borax12.materialdaterangepicker.single.time.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key.//w w w  .  j  a  v  a 2  s . c o m
 * @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(),
                    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;
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.SublimeTimePicker.java

/**
 * Create a tree for deciding what keys can legally be typed.
 *///w w w .  ja  va2  s  .c  om
private void generateLegalTimesTree() {
    // Create a quick cache of numbers to their keycodes.
    final int k0 = KeyEvent.KEYCODE_0;
    final int k1 = KeyEvent.KEYCODE_1;
    final int k2 = KeyEvent.KEYCODE_2;
    final int k3 = KeyEvent.KEYCODE_3;
    final int k4 = KeyEvent.KEYCODE_4;
    final int k5 = KeyEvent.KEYCODE_5;
    final int k6 = KeyEvent.KEYCODE_6;
    final int k7 = KeyEvent.KEYCODE_7;
    final int k8 = KeyEvent.KEYCODE_8;
    final int k9 = KeyEvent.KEYCODE_9;

    // The root of the tree doesn't contain any numbers.
    mLegalTimesTree = new Node();
    if (mIs24HourView) {
        // We'll be re-using these nodes, so we'll save them.
        Node minuteFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
        Node minuteSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        // The first digit must be followed by the second digit.
        minuteFirstDigit.addChild(minuteSecondDigit);

        // The first digit may be 0-1.
        Node firstDigit = new Node(k0, k1);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 0-1, the second digit may be 0-5.
        Node secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);
        // We may now be followed by the first minute digit. E.g. 00:09, 15:58.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 0-1, and the second digit is 0-5, the third digit may be 6-9.
        Node thirdDigit = new Node(k6, k7, k8, k9);
        // The time must now be finished. E.g. 0:55, 1:08.
        secondDigit.addChild(thirdDigit);

        // When the first digit is 0-1, the second digit may be 6-9.
        secondDigit = new Node(k6, k7, k8, k9);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 06:50, 18:20.
        secondDigit.addChild(minuteFirstDigit);

        // The first digit may be 2.
        firstDigit = new Node(k2);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 2, the second digit may be 0-3.
        secondDigit = new Node(k0, k1, k2, k3);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 20:50, 23:09.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 2, the second digit may be 4-5.
        secondDigit = new Node(k4, k5);
        firstDigit.addChild(secondDigit);
        // We must now be followd by the last minute digit. E.g. 2:40, 2:53.
        secondDigit.addChild(minuteSecondDigit);

        // The first digit may be 3-9.
        firstDigit = new Node(k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We must now be followed by the first minute digit. E.g. 3:57, 8:12.
        firstDigit.addChild(minuteFirstDigit);
    } else {
        // We'll need to use the AM/PM node a lot.
        // Set up AM and PM to respond to "a" and "p".
        Node ampm = new Node(getAmOrPmKeyCode(AM), getAmOrPmKeyCode(PM));

        // The first hour digit may be 1.
        Node firstDigit = new Node(k1);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour times. E.g. 1pm.
        firstDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 0-2.
        Node secondDigit = new Node(k0, k1, k2);
        firstDigit.addChild(secondDigit);
        // Also for quick input of on-the-hour times. E.g. 10pm, 12am.
        secondDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 0-5.
        Node thirdDigit = new Node(k0, k1, k2, k3, k4, k5);
        secondDigit.addChild(thirdDigit);
        // The time may be finished now. E.g. 1:02pm, 1:25am.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5,
        // the fourth digit may be 0-9.
        Node fourthDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        thirdDigit.addChild(fourthDigit);
        // The time must be finished now. E.g. 10:49am, 12:40pm.
        fourthDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 6-9.
        thirdDigit = new Node(k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:08am, 1:26pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 3-5.
        secondDigit = new Node(k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 1, and the second digit is 3-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:39am, 1:50pm.
        thirdDigit.addChild(ampm);

        // The hour digit may be 2-9.
        firstDigit = new Node(k2, k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour-times. E.g. 2am, 5pm.
        firstDigit.addChild(ampm);

        // When the first digit is 2-9, the second digit may be 0-5.
        secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 2-9, and the second digit is 0-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 2:57am, 9:30pm.
        thirdDigit.addChild(ampm);
    }
}

From source file:com.wdullaer.materialdatetimepicker.time.TimePickerView.java

/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key./*from  www.  j av a 2 s  .  c om*/
 * @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(),
                    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;
}

From source file:com.borax12.materialdaterangepicker.single.time.TimePickerDialog.java

/**
 * Create a tree for deciding what keys can legally be typed.
 *//*from  w  w w.  j  a v a  2 s  . c  o  m*/
private void generateLegalTimesTree() {
    // Create a quick cache of numbers to their keycodes.
    int k0 = KeyEvent.KEYCODE_0;
    int k1 = KeyEvent.KEYCODE_1;
    int k2 = KeyEvent.KEYCODE_2;
    int k3 = KeyEvent.KEYCODE_3;
    int k4 = KeyEvent.KEYCODE_4;
    int k5 = KeyEvent.KEYCODE_5;
    int k6 = KeyEvent.KEYCODE_6;
    int k7 = KeyEvent.KEYCODE_7;
    int k8 = KeyEvent.KEYCODE_8;
    int k9 = KeyEvent.KEYCODE_9;

    // The root of the tree doesn't contain any numbers.
    mLegalTimesTree = new Node();
    if (mIs24HourMode) {
        // We'll be re-using these nodes, so we'll save them.
        Node minuteFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
        Node minuteSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        // The first digit must be followed by the second digit.
        minuteFirstDigit.addChild(minuteSecondDigit);

        if (mEnableSeconds) {
            Node secondsFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
            Node secondsSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
            secondsFirstDigit.addChild(secondsSecondDigit);

            // Minutes can be followed by seconds.
            minuteSecondDigit.addChild(secondsFirstDigit);
        }

        // The first digit may be 0-1.
        Node firstDigit = new Node(k0, k1);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 0-1, the second digit may be 0-5.
        Node secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);
        // We may now be followed by the first minute digit. E.g. 00:09, 15:58.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 0-1, and the second digit is 0-5, the third digit may be 6-9.
        Node thirdDigit = new Node(k6, k7, k8, k9);
        // The time must now be finished. E.g. 0:55, 1:08.
        secondDigit.addChild(thirdDigit);

        // When the first digit is 0-1, the second digit may be 6-9.
        secondDigit = new Node(k6, k7, k8, k9);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 06:50, 18:20.
        secondDigit.addChild(minuteFirstDigit);

        // The first digit may be 2.
        firstDigit = new Node(k2);
        mLegalTimesTree.addChild(firstDigit);

        // When the first digit is 2, the second digit may be 0-3.
        secondDigit = new Node(k0, k1, k2, k3);
        firstDigit.addChild(secondDigit);
        // We must now be followed by the first minute digit. E.g. 20:50, 23:09.
        secondDigit.addChild(minuteFirstDigit);

        // When the first digit is 2, the second digit may be 4-5.
        secondDigit = new Node(k4, k5);
        firstDigit.addChild(secondDigit);
        // We must now be followd by the last minute digit. E.g. 2:40, 2:53.
        secondDigit.addChild(minuteSecondDigit);

        // The first digit may be 3-9.
        firstDigit = new Node(k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We must now be followed by the first minute digit. E.g. 3:57, 8:12.
        firstDigit.addChild(minuteFirstDigit);
    } else {
        // We'll need to use the AM/PM node a lot.
        // Set up AM and PM to respond to "a" and "p".
        Node ampm = new Node(getAmOrPmKeyCode(AM), getAmOrPmKeyCode(PM));

        // Seconds will be used a few times as well, if enabled.
        Node secondsFirstDigit = new Node(k0, k1, k2, k3, k4, k5);
        Node secondsSecondDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondsSecondDigit.addChild(ampm);
        secondsFirstDigit.addChild(secondsSecondDigit);

        // The first hour digit may be 1.
        Node firstDigit = new Node(k1);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour times. E.g. 1pm.
        firstDigit.addChild(ampm);

        // When the first digit is 1, the second digit may be 0-2.
        Node secondDigit = new Node(k0, k1, k2);
        firstDigit.addChild(secondDigit);
        // Also for quick input of on-the-hour times. E.g. 10pm, 12am.
        secondDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 0-5.
        Node thirdDigit = new Node(k0, k1, k2, k3, k4, k5);
        secondDigit.addChild(thirdDigit);
        // The time may be finished now. E.g. 1:02pm, 1:25am.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5,
        // the fourth digit may be 0-9.
        Node fourthDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        thirdDigit.addChild(fourthDigit);
        // The time must be finished now, when seconds are disabled. E.g. 10:49am, 12:40pm.
        fourthDigit.addChild(ampm);

        // When the first digit is 1, the second digit is 0-2, and the third digit is 0-5,
        // and fourth digit is 0-9, we may add seconds if enabled.
        if (mEnableSeconds) {
            // The time must be finished now. E.g. 10:49:01am, 12:40:59pm.
            fourthDigit.addChild(secondsFirstDigit);
        }

        // When the first digit is 1, and the second digit is 0-2, the third digit may be 6-9.
        thirdDigit = new Node(k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 1:08am, 1:26pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 0-2, and the third digit is 6-9,
        // we may add seconds is enabled.
        if (mEnableSeconds) {
            // The time must be finished now. E.g. 1:08:01am, 1:26:59pm.
            thirdDigit.addChild(secondsFirstDigit);
        }

        // When the first digit is 1, the second digit may be 3-5.
        secondDigit = new Node(k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 1, and the second digit is 3-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now if seconds are disabled. E.g. 1:39am, 1:50pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 1, and the second digit is 3-5, and the third digit is 0-9,
        // we may add seconds if enabled.
        if (mEnableSeconds) {
            // The time must be finished now. E.g. 1:39:01am, 1:50:59pm.
            thirdDigit.addChild(secondsFirstDigit);
        }

        // The hour digit may be 2-9.
        firstDigit = new Node(k2, k3, k4, k5, k6, k7, k8, k9);
        mLegalTimesTree.addChild(firstDigit);
        // We'll allow quick input of on-the-hour-times. E.g. 2am, 5pm.
        firstDigit.addChild(ampm);

        // When the first digit is 2-9, the second digit may be 0-5.
        secondDigit = new Node(k0, k1, k2, k3, k4, k5);
        firstDigit.addChild(secondDigit);

        // When the first digit is 2-9, and the second digit is 0-5, the third digit may be 0-9.
        thirdDigit = new Node(k0, k1, k2, k3, k4, k5, k6, k7, k8, k9);
        secondDigit.addChild(thirdDigit);
        // The time must be finished now. E.g. 2:57am, 9:30pm.
        thirdDigit.addChild(ampm);

        // When the first digit is 2-9, and the second digit is 0-5, and third digit is 0-9, we
        // may add seconds if enabled.
        if (mEnableSeconds) {
            // The time must be finished now. E.g. 2:57:01am, 9:30:59pm.
            thirdDigit.addChild(secondsFirstDigit);
        }
    }
}

From source file:com.customdatepicker.time.TimePickerDialog.java

/**
 * For keyboard mode, processes key events.
 * @param keyCode the pressed key./*from  w  w  w  .j  a v a2 s  . c  om*/
 * @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;
}