PaoloConte / time-edit-text

Custom Android EditText-like time picker
Apache License 2.0
16 stars 6 forks source link

Samsung Galaxy crash #1

Open WillJonesMagic opened 5 years ago

WillJonesMagic commented 5 years ago

This does not work as expected on Samsung Galaxy devices. Calling setText() calls onKeyDown which eventually calls setText(), ad infinitum/until crash.

A simple fix for this is as follows (all in TimeEditText.java):

Top of file: boolean ignoreKeyPress = false;

Replace updateText() with the following:

private void updateText() {
    int bold = currentPosition > 1 ? currentPosition+1 : currentPosition;
    int color = getTextColors().getDefaultColor();
    Spannable text = new SpannableString(String.format("%02d:%02d", getHour(), getMinutes()));
    if (bold >= 0) {
        text.setSpan(new ForegroundColorSpan(color & 0xFFFFFF | 0xA0000000), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new StyleSpan(Typeface.BOLD), bold, bold+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLACK), bold, bold+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new BackgroundColorSpan(0x40808080), bold, bold+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    ignoreKeyPress = true;
    setText(text);
    ignoreKeyPress = false;
}

Replace sendKeyEvent inside return new BaseInputConnection at the bottom of the file with the following

@Override
public boolean sendKeyEvent(KeyEvent event) {
    if(!ignoreKeyPress)
        onKeyEvent(event.getKeyCode(), event);
    return true;
}

Tested on a Galaxy S9, Android 9.

PaoloConte commented 5 years ago

Hi, feel free to send a pull request to include the fix in the repo. Thanks