diff options
| author | Alan Viverette <alanv@google.com> | 2016-09-09 14:29:35 -0400 |
|---|---|---|
| committer | Alan Viverette <alanv@google.com> | 2016-09-09 14:29:35 -0400 |
| commit | 57fe701ddf37a5fcdc21321d48d4818e60577f15 (patch) | |
| tree | eea0f54f248a79dad6bd223910acefa47822346f /core/java/android/widget/NumberPicker.java | |
| parent | 8c83edc43b3c5c5dc5b756acd0bacb0e8348db9f (diff) | |
Cancel pending selection before filtering input
There may be a pending selection from a previous filter() call, but the
current filter() call may return an empty string without posting a new
pending selection.
Bug: 31049172
Change-Id: I2cb88b3283a3705b6c85d61f26951c35d2b0acd2
Diffstat (limited to 'core/java/android/widget/NumberPicker.java')
| -rw-r--r-- | core/java/android/widget/NumberPicker.java | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/core/java/android/widget/NumberPicker.java b/core/java/android/widget/NumberPicker.java index c3ddec787bfa..25580fd2496e 100644 --- a/core/java/android/widget/NumberPicker.java +++ b/core/java/android/widget/NumberPicker.java @@ -16,6 +16,8 @@ package android.widget; +import com.android.internal.R; + import android.annotation.CallSuper; import android.annotation.IntDef; import android.annotation.Widget; @@ -29,10 +31,12 @@ import android.graphics.Paint.Align; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.text.Editable; import android.text.InputFilter; import android.text.InputType; import android.text.Spanned; import android.text.TextUtils; +import android.text.TextWatcher; import android.text.method.NumberKeyListener; import android.util.AttributeSet; import android.util.SparseArray; @@ -52,9 +56,6 @@ import android.view.animation.DecelerateInterpolator; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; -import com.android.internal.R; -import libcore.icu.LocaleData; - import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; @@ -62,6 +63,8 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import libcore.icu.LocaleData; + /** * A widget that enables the user to select a number from a predefined range. * There are two flavors of this widget and which one is presented to the user @@ -1991,7 +1994,7 @@ public class NumberPicker extends LinearLayout { removeCallbacks(mChangeCurrentByOneFromLongPressCommand); } if (mSetSelectionCommand != null) { - removeCallbacks(mSetSelectionCommand); + mSetSelectionCommand.cancel(); } if (mBeginSoftInputOnLongPressCommand != null) { removeCallbacks(mBeginSoftInputOnLongPressCommand); @@ -2033,18 +2036,14 @@ public class NumberPicker extends LinearLayout { } /** - * Posts an {@link SetSelectionCommand} from the given <code>selectionStart - * </code> to <code>selectionEnd</code>. + * Posts a {@link SetSelectionCommand} from the given + * {@code selectionStart} to {@code selectionEnd}. */ private void postSetSelectionCommand(int selectionStart, int selectionEnd) { if (mSetSelectionCommand == null) { - mSetSelectionCommand = new SetSelectionCommand(); - } else { - removeCallbacks(mSetSelectionCommand); + mSetSelectionCommand = new SetSelectionCommand(mInputText); } - mSetSelectionCommand.mSelectionStart = selectionStart; - mSetSelectionCommand.mSelectionEnd = selectionEnd; - post(mSetSelectionCommand); + mSetSelectionCommand.post(selectionStart, selectionEnd); } /** @@ -2090,6 +2089,12 @@ public class NumberPicker extends LinearLayout { @Override public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { + // We don't know what the output will be, so always cancel any + // pending set selection command. + if (mSetSelectionCommand != null) { + mSetSelectionCommand.cancel(); + } + if (mDisplayedValues == null) { CharSequence filtered = super.filter(source, start, end, dest, dstart, dend); if (filtered == null) { @@ -2237,12 +2242,39 @@ public class NumberPicker extends LinearLayout { /** * Command for setting the input text selection. */ - class SetSelectionCommand implements Runnable { - private int mSelectionStart; + private static class SetSelectionCommand implements Runnable { + private final EditText mInputText; + private int mSelectionStart; private int mSelectionEnd; + /** Whether this runnable is currently posted. */ + private boolean mPosted; + + public SetSelectionCommand(EditText inputText) { + mInputText = inputText; + } + + public void post(int selectionStart, int selectionEnd) { + mSelectionStart = selectionStart; + mSelectionEnd = selectionEnd; + + if (!mPosted) { + mInputText.post(this); + mPosted = true; + } + } + + public void cancel() { + if (mPosted) { + mInputText.removeCallbacks(this); + mPosted = false; + } + } + + @Override public void run() { + mPosted = false; mInputText.setSelection(mSelectionStart, mSelectionEnd); } } |
