summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Alan Viverette <alanv@google.com> 2016-09-14 06:16:57 +0000
committer android-build-merger <android-build-merger@google.com> 2016-09-14 06:16:57 +0000
commitf612a576ca67e66064cc682aa415c2b7c6d549d8 (patch)
tree6e091a8ecd1a8df0cadf0296b7907a2e9120387b
parentaeed23577d9cea64edf7f419b97db1a64f9d750b (diff)
parent24e936228956d9e5dddf67428756835a0995f654 (diff)
Cancel pending selection before filtering input am: 57fe701ddf am: 1fd7fadfe4
am: 24e9362289 Change-Id: I4836b0ac8b4b2addee6d757f45d2774508869cca
-rw-r--r--core/java/android/widget/NumberPicker.java60
1 files changed, 46 insertions, 14 deletions
diff --git a/core/java/android/widget/NumberPicker.java b/core/java/android/widget/NumberPicker.java
index 82f5f0155598..72b2e31a1d3a 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.TestApi;
@@ -30,10 +32,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;
@@ -53,9 +57,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;
@@ -63,6 +64,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
@@ -2008,7 +2011,7 @@ public class NumberPicker extends LinearLayout {
removeCallbacks(mChangeCurrentByOneFromLongPressCommand);
}
if (mSetSelectionCommand != null) {
- removeCallbacks(mSetSelectionCommand);
+ mSetSelectionCommand.cancel();
}
if (mBeginSoftInputOnLongPressCommand != null) {
removeCallbacks(mBeginSoftInputOnLongPressCommand);
@@ -2050,18 +2053,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);
}
/**
@@ -2107,6 +2106,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) {
@@ -2254,12 +2259,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);
}
}