NumberPicker.java revision 698e1d5d867a7ac653fa1d6f29cef4cfb73fad38
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrookpackage android.widget;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.animation.Animator;
20206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.animation.AnimatorListenerAdapter;
21206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.animation.AnimatorSet;
22206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.animation.ObjectAnimator;
2368f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrookimport android.annotation.Widget;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
25206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.content.res.ColorStateList;
26206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.content.res.TypedArray;
27206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.graphics.Canvas;
28206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.graphics.Color;
29206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.graphics.Paint;
30e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganovimport android.graphics.Paint.Align;
31b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganovimport android.graphics.Rect;
329cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganovimport android.graphics.drawable.Drawable;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.InputFilter;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.InputType;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.Spanned;
36206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.text.TextUtils;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.method.NumberKeyListener;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.AttributeSet;
39206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.util.SparseArray;
409cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganovimport android.util.TypedValue;
41206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.view.KeyEvent;
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.LayoutInflater;
43b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganovimport android.view.LayoutInflater.Filter;
44206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.view.MotionEvent;
45206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.view.VelocityTracker;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.View;
47206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.view.ViewConfiguration;
483fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
493fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganovimport android.view.accessibility.AccessibilityManager;
509cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganovimport android.view.animation.DecelerateInterpolator;
51206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganovimport android.view.inputmethod.InputMethodManager;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
53b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganovimport com.android.internal.R;
54b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
5568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook/**
564bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * A widget that enables the user to select a number form a predefined range.
574bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * The widget presents an input filed and up and down buttons for selecting the
584bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * current value. Pressing/long pressing the up and down buttons increments and
594bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * decrements the current value respectively. Touching the input filed shows a
604bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * scroll wheel, tapping on which while shown and not moving allows direct edit
614bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * of the current value. Sliding motions up or down hide the buttons and the
624bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * input filed, show the scroll wheel, and rotate the latter. Flinging is
634bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * also supported. The widget enables mapping from positions to strings such
644bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * that instead the position index the corresponding string is displayed.
654bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * <p>
664bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * For an example of using this widget, see {@link android.widget.TimePicker}.
674bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov * </p>
6868f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook */
6968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook@Widget
7068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrookpublic class NumberPicker extends LinearLayout {
71fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
7268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
73e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * The default update interval during long press.
74e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     */
75e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private static final long DEFAULT_LONG_PRESS_UPDATE_INTERVAL = 300;
76e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
77e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    /**
78206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The index of the middle selector item.
7968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
80206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private static final int SELECTOR_MIDDLE_ITEM_INDEX = 2;
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
83206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The coefficient by which to adjust (divide) the max fling velocity.
8468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
85206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private static final int SELECTOR_MAX_FLING_VELOCITY_ADJUSTMENT = 8;
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
87206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
88206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The the duration for adjusting the selector wheel.
89206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
90206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private static final int SELECTOR_ADJUSTMENT_DURATION_MILLIS = 800;
91fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
92206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
93b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The duration of scrolling to the next/previous value while changing
94b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * the current value by one, i.e. increment or decrement.
95b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
96b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int CHANGE_CURRENT_BY_ONE_SCROLL_DURATION = 300;
97b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
98b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
99206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The the delay for showing the input controls after a single tap on the
100206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * input text.
101206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
102206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private static final int SHOW_INPUT_CONTROLS_DELAY_MILLIS = ViewConfiguration
103206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            .getDoubleTapTimeout();
104206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
105206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
106b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The strength of fading in the top and bottom while drawing the selector.
107206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
108b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final float TOP_AND_BOTTOM_FADING_EDGE_STRENGTH = 0.9f;
109206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
110206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
111b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The default unscaled height of the selection divider.
112206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
113b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int UNSCALED_DEFAULT_SELECTION_DIVIDER_HEIGHT = 2;
114206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
115206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
116b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * In this state the selector wheel is not shown.
117206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
118b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int SELECTOR_WHEEL_STATE_NONE = 0;
119206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
120206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
121b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * In this state the selector wheel is small.
122b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
123b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int SELECTOR_WHEEL_STATE_SMALL = 1;
124b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
125b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
126b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * In this state the selector wheel is large.
127b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
128b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int SELECTOR_WHEEL_STATE_LARGE = 2;
129b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
130b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
131b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The alpha of the selector wheel when it is bright.
1329cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     */
133b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int SELECTOR_WHEEL_BRIGHT_ALPHA = 255;
134b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
135b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
136b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The alpha of the selector wheel when it is dimmed.
137b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
138b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int SELECTOR_WHEEL_DIM_ALPHA = 60;
139b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
140b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
141b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The alpha for the increment/decrement button when it is transparent.
142b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
143b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int BUTTON_ALPHA_TRANSPARENT = 0;
144b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
145b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
146b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The alpha for the increment/decrement button when it is opaque.
147b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
148b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final int BUTTON_ALPHA_OPAQUE = 1;
149b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
150b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
151b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The property for setting the selector paint.
152b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
153b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final String PROPERTY_SELECTOR_PAINT_ALPHA = "selectorPaintAlpha";
154b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
155b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
156b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The property for setting the increment/decrement button alpha.
157b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
158b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private static final String PROPERTY_BUTTON_ALPHA = "alpha";
1599cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
1609cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    /**
161206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The numbers accepted by the input text's {@link Filter}
162206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
163206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private static final char[] DIGIT_CHARACTERS = new char[] {
164206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
165206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    };
166206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
167206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
168206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Use a custom NumberPicker formatting callback to use two-digit minutes
169206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * strings like "01". Keeping a static formatter etc. is the most efficient
170206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * way to do this; it avoids creating temporary objects on every call to
171206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * format().
172e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *
173e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @hide
174206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
175206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public static final NumberPicker.Formatter TWO_DIGIT_FORMATTER = new NumberPicker.Formatter() {
176206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        final StringBuilder mBuilder = new StringBuilder();
177206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
178f0ef665299d795df7905897e1c337e37891dafefJean-Baptiste Queru        final java.util.Formatter mFmt = new java.util.Formatter(mBuilder, java.util.Locale.US);
179206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
180206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        final Object[] mArgs = new Object[1];
181206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
182e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        public String format(int value) {
183206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mArgs[0] = value;
184206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mBuilder.delete(0, mBuilder.length());
185206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mFmt.format("%02d", mArgs);
186206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return mFmt.toString();
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
190206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
191206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The increment button.
192206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
193206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final ImageButton mIncrementButton;
194206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
195206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
196206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The decrement button.
197206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
198206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final ImageButton mDecrementButton;
199206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
200206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
201206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The text for showing the current value.
202206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
203206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final EditText mInputText;
204206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
205206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
206ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * The min height of this widget.
207ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     */
208ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    private final int mMinHeight;
209ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
210ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    /**
211e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     * The max height of this widget.
212e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     */
213e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    private final int mMaxHeight;
214e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov
215e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    /**
216e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     * The max width of this widget.
217e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     */
218ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    private final int mMinWidth;
219ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
220ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    /**
221ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * The max width of this widget.
222ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     */
223ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    private int mMaxWidth;
224ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
225ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    /**
226ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * Flag whether to compute the max width.
227ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     */
228ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    private final boolean mComputeMaxWidth;
229e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov
230e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    /**
231206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The height of the text.
232206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
233206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final int mTextSize;
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
235206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
236b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The height of the gap between text elements if the selector wheel.
237b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
238b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private int mSelectorTextGapHeight;
239b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
240b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
241206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The values to be displayed instead the indices.
242206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String[] mDisplayedValues;
24468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
24568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
24668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     * Lower value of the range of numbers allowed for the NumberPicker
24768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
248e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private int mMinValue;
24968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
25068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
25168f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     * Upper value of the range of numbers allowed for the NumberPicker
25268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
253e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private int mMaxValue;
25468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
25568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
25668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     * Current value of this NumberPicker
25768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
258e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private int mValue;
25968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
26068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
261206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Listener to be notified upon current value change.
26268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
263cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov    private OnValueChangeListener mOnValueChangeListener;
26450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
26550f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    /**
26650f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     * Listener to be notified upon scroll state change.
26750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     */
26850f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    private OnScrollListener mOnScrollListener;
269206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
270206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
271206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Formatter for for displaying the current value.
272206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private Formatter mFormatter;
274fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
275206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
276206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The speed for updating the value form long press.
277206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
278e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private long mLongPressUpdateInterval = DEFAULT_LONG_PRESS_UPDATE_INTERVAL;
279fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
28068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
281206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Cache for the string representation of selector indices.
282206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
283206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final SparseArray<String> mSelectorIndexToStringCache = new SparseArray<String>();
284206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
285206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
286206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The selector indices whose value are show by the selector.
287206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
288206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final int[] mSelectorIndices = new int[] {
289206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE,
290206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            Integer.MIN_VALUE
291206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    };
292206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
293206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
294206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The {@link Paint} for drawing the selector.
295206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
296b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private final Paint mSelectorWheelPaint;
297206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
298206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
299206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The height of a selector element (text + gap).
300206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
301206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mSelectorElementHeight;
302206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
303206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
304206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The initial offset of the scroll selector.
305206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
306206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mInitialScrollOffset = Integer.MIN_VALUE;
307206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
308206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
309206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The current offset of the scroll selector.
310206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
311206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mCurrentScrollOffset;
312206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
313206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
314206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The {@link Scroller} responsible for flinging the selector.
315206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
316206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final Scroller mFlingScroller;
317206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
318206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
319206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The {@link Scroller} responsible for adjusting the selector.
320206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
321206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final Scroller mAdjustScroller;
322206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
323206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
324206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The previous Y coordinate while scrolling the selector.
325206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
326206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mPreviousScrollerY;
327206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
328206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
329206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Handle to the reusable command for setting the input text selection.
330206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
331206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private SetSelectionCommand mSetSelectionCommand;
332206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
333206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
334206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Handle to the reusable command for adjusting the scroller.
335206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
336206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private AdjustScrollerCommand mAdjustScrollerCommand;
337206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
338206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
339b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Handle to the reusable command for changing the current value from long
340b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * press by one.
341206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
342b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private ChangeCurrentByOneFromLongPressCommand mChangeCurrentByOneFromLongPressCommand;
343206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
344206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
345206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * {@link Animator} for showing the up/down arrows.
346206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
347206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final AnimatorSet mShowInputControlsAnimator;
348206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
349206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
350b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * {@link Animator} for dimming the selector wheel.
351b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
352b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private final Animator mDimSelectorWheelAnimator;
353b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
354b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
355206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The Y position of the last down event.
356206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
357206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private float mLastDownEventY;
358206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
359206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
360206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The Y position of the last motion event.
361206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
362206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private float mLastMotionEventY;
363206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
364206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
365206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Flag if to begin edit on next up event.
366206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
367206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private boolean mBeginEditOnUpEvent;
368206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
369206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
370206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Flag if to adjust the selector wheel on next up event.
371206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
372206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private boolean mAdjustScrollerOnUpEvent;
373206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
374206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
375b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * The state of the selector wheel.
376206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
377b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private int mSelectorWheelState;
378206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
379206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
380206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Determines speed during touch scrolling.
381206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
382206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private VelocityTracker mVelocityTracker;
383206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
384206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
385206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @see ViewConfiguration#getScaledTouchSlop()
386206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
387206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mTouchSlop;
388206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
389206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
390206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @see ViewConfiguration#getScaledMinimumFlingVelocity()
391206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
392206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mMinimumFlingVelocity;
393206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
394206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
395206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @see ViewConfiguration#getScaledMaximumFlingVelocity()
396206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
397206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int mMaximumFlingVelocity;
398206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
399206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
400206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Flag whether the selector should wrap around.
401206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
402e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    private boolean mWrapSelectorWheel;
403206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
404206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
405206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * The back ground color used to optimize scroller fading.
40668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
407206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final int mSolidColor;
408206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
409206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
4104243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov     * Flag indicating if this widget supports flinging.
4114243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov     */
4124243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov    private final boolean mFlingable;
4134243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov
4144243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov    /**
4159cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * Divider for showing item to be selected while scrolling
4169cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     */
4179cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    private final Drawable mSelectionDivider;
4189cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
4199cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    /**
4209cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * The height of the selection divider.
4219cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     */
4229cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    private final int mSelectionDividerHeight;
4239cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
4249cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    /**
425206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Reusable {@link Rect} instance.
426206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
427206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private final Rect mTempRect = new Rect();
428206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
429206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
43050f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     * The current scroll state of the number picker.
43150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     */
43250f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    private int mScrollState = OnScrollListener.SCROLL_STATE_IDLE;
43350f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
43450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    /**
4359cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * The duration of the animation for showing the input controls.
4369cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     */
4379cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    private final long mShowInputControlsAnimimationDuration;
4389cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
4399cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    /**
440b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Flag whether the scoll wheel and the fading edges have been initialized.
441b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
442b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private boolean mScrollWheelAndFadingEdgesInitialized;
443b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
444b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
445e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Interface to listen for changes of the current value.
446206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
447cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov    public interface OnValueChangeListener {
448e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
449206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        /**
4504bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         * Called upon a change of the current value.
4514bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         *
452206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * @param picker The NumberPicker associated with this listener.
453206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * @param oldVal The previous value.
454206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * @param newVal The new value.
455206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         */
456e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        void onValueChange(NumberPicker picker, int oldVal, int newVal);
457206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
458206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
459206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
460e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Interface to listen for the picker scroll state.
46150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     */
46250f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    public interface OnScrollListener {
46350f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
46450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        /**
46550f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         * The view is not scrolling.
46650f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         */
46750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        public static int SCROLL_STATE_IDLE = 0;
46850f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
46950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        /**
47050f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         * The user is scrolling using touch, and their finger is still on the screen.
47150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         */
47250f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        public static int SCROLL_STATE_TOUCH_SCROLL = 1;
47350f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
47450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        /**
47550f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         * The user had previously been scrolling using touch and performed a fling.
47650f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         */
47750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        public static int SCROLL_STATE_FLING = 2;
47850f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
47950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        /**
480e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         * Callback invoked while the number picker scroll state has changed.
48150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         *
482e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         * @param view The view whose scroll state is being reported.
483e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         * @param scrollState The current scroll state. One of
484e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         *            {@link #SCROLL_STATE_IDLE},
485e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         *            {@link #SCROLL_STATE_TOUCH_SCROLL} or
486e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         *            {@link #SCROLL_STATE_IDLE}.
48750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov         */
4884bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov        public void onScrollStateChange(NumberPicker view, int scrollState);
48950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    }
49050f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
49150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    /**
492e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Interface used to format current value into a string for presentation.
493206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
494206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public interface Formatter {
4954bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov
4964bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov        /**
497e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov         * Formats a string representation of the current value.
4984bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         *
4994bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         * @param value The currently selected value.
5004bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         * @return A formatted string representation.
5014bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov         */
502e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        public String format(int value);
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
504fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
50568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
5064bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Create a new number picker.
5074bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     *
5084bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * @param context The application environment.
5094bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     */
5104bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    public NumberPicker(Context context) {
5114bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov        this(context, null);
5124bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    }
5134bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov
5144bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    /**
5154bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Create a new number picker.
516206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
517206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @param context The application environment.
518206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @param attrs A collection of attributes.
51968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public NumberPicker(Context context, AttributeSet attrs) {
521206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        this(context, attrs, R.attr.numberPickerStyle);
522206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
523206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
524206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
525206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Create a new number picker
526206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
527206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @param context the application environment.
528206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @param attrs a collection of attributes.
529206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @param defStyle The default style to apply to this view.
530206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
531206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
532206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        super(context, attrs, defStyle);
533206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
534206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // process style attributes
535206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        TypedArray attributesArray = context.obtainStyledAttributes(attrs,
536206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                R.styleable.NumberPicker, defStyle, 0);
537206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mSolidColor = attributesArray.getColor(R.styleable.NumberPicker_solidColor, 0);
5384243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov        mFlingable = attributesArray.getBoolean(R.styleable.NumberPicker_flingable, true);
5399cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mSelectionDivider = attributesArray.getDrawable(R.styleable.NumberPicker_selectionDivider);
5409cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        int defSelectionDividerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
5419cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                UNSCALED_DEFAULT_SELECTION_DIVIDER_HEIGHT,
5429cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                getResources().getDisplayMetrics());
5439cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mSelectionDividerHeight = attributesArray.getDimensionPixelSize(
5449cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                R.styleable.NumberPicker_selectionDividerHeight, defSelectionDividerHeight);
545ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mMinHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_minHeight, 0);
546ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mMaxHeight = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxHeight,
547ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                Integer.MAX_VALUE);
548ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        if (mMinHeight > mMaxHeight) {
549ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            throw new IllegalArgumentException("minHeight > maxHeight");
550ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        }
551ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mMinWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_minWidth, 0);
552ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mMaxWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_maxWidth,
553ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                Integer.MAX_VALUE);
554ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        if (mMinWidth > mMaxWidth) {
555ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            throw new IllegalArgumentException("minWidth > maxWidth");
556ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        }
557ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mComputeMaxWidth = (mMaxWidth == Integer.MAX_VALUE);
558206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        attributesArray.recycle();
559206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
5609cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mShowInputControlsAnimimationDuration = getResources().getInteger(
5619cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                R.integer.config_longAnimTime);
5629cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
563206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // By default Linearlayout that we extend is not drawn. This is
564206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // its draw() method is not called but dispatchDraw() is called
565206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // directly (see ViewGroup.drawChild()). However, this class uses
566206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // the fading edge effect implemented by View and we need our
567206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // draw() method to be called. Therefore, we declare we will draw.
568206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        setWillNotDraw(false);
569b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        setSelectorWheelState(SELECTOR_WHEEL_STATE_NONE);
570206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
571206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
572206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                Context.LAYOUT_INFLATER_SERVICE);
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        inflater.inflate(R.layout.number_picker, this, true);
57468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
575206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        OnClickListener onClickListener = new OnClickListener() {
57668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook            public void onClick(View v) {
5776304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                InputMethodManager inputMethodManager = InputMethodManager.peekInstance();
5786304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                if (inputMethodManager != null && inputMethodManager.isActive(mInputText)) {
5796304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                    inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
5806304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                }
581206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mInputText.clearFocus();
582206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (v.getId() == R.id.increment) {
583b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    changeCurrentByOne(true);
584206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                } else {
585b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    changeCurrentByOne(false);
58668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook                }
58768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook            }
58868f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook        };
58968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
590206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        OnLongClickListener onLongClickListener = new OnLongClickListener() {
591206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            public boolean onLongClick(View v) {
592206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mInputText.clearFocus();
593206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (v.getId() == R.id.increment) {
594b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    postChangeCurrentByOneFromLongPress(true);
595206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                } else {
596b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    postChangeCurrentByOneFromLongPress(false);
597206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
598206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                return true;
599206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
600206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        };
60168f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
602206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // increment button
603206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mIncrementButton = (ImageButton) findViewById(R.id.increment);
604206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mIncrementButton.setOnClickListener(onClickListener);
605206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mIncrementButton.setOnLongClickListener(onLongClickListener);
606206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
607206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // decrement button
608206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mDecrementButton = (ImageButton) findViewById(R.id.decrement);
609206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mDecrementButton.setOnClickListener(onClickListener);
610206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mDecrementButton.setOnLongClickListener(onLongClickListener);
611206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
612206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // input text
613012dd5a461f18a2e5dad38c60282fac1c21ff7feSvetoslav Ganov        mInputText = (EditText) findViewById(R.id.numberpicker_input);
614206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setOnFocusChangeListener(new OnFocusChangeListener() {
615206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            public void onFocusChange(View v, boolean hasFocus) {
616a53efe9923bedab4fe5d578f32eaff308e5b9e76Svetoslav Ganov                if (hasFocus) {
617a53efe9923bedab4fe5d578f32eaff308e5b9e76Svetoslav Ganov                    mInputText.selectAll();
6186304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                    InputMethodManager inputMethodManager = InputMethodManager.peekInstance();
619b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    if (inputMethodManager != null) {
620b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                        inputMethodManager.showSoftInput(mInputText, 0);
621b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    }
622a53efe9923bedab4fe5d578f32eaff308e5b9e76Svetoslav Ganov                } else {
623a53efe9923bedab4fe5d578f32eaff308e5b9e76Svetoslav Ganov                    mInputText.setSelection(0, 0);
624206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    validateInputTextView(v);
62568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook                }
62668f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook            }
627206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        });
628206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setFilters(new InputFilter[] {
629206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            new InputTextFilter()
630206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        });
63168f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
632206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
633206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
634206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // initialize constants
635206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mTouchSlop = ViewConfiguration.getTapTimeout();
636206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        ViewConfiguration configuration = ViewConfiguration.get(context);
637206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mTouchSlop = configuration.getScaledTouchSlop();
638206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
639206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity()
640206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                / SELECTOR_MAX_FLING_VELOCITY_ADJUSTMENT;
641206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mTextSize = (int) mInputText.getTextSize();
642206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
643206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // create the selector wheel paint
644206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        Paint paint = new Paint();
645206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        paint.setAntiAlias(true);
646206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        paint.setTextAlign(Align.CENTER);
647206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        paint.setTextSize(mTextSize);
648206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        paint.setTypeface(mInputText.getTypeface());
649206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        ColorStateList colors = mInputText.getTextColors();
650206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int color = colors.getColorForState(ENABLED_STATE_SET, Color.WHITE);
651206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        paint.setColor(color);
652b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mSelectorWheelPaint = paint;
653206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
654206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // create the animator for showing the input controls
655b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mDimSelectorWheelAnimator = ObjectAnimator.ofInt(this, PROPERTY_SELECTOR_PAINT_ALPHA,
656b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                SELECTOR_WHEEL_BRIGHT_ALPHA, SELECTOR_WHEEL_DIM_ALPHA);
657206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        final ObjectAnimator showIncrementButton = ObjectAnimator.ofFloat(mIncrementButton,
658b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                PROPERTY_BUTTON_ALPHA, BUTTON_ALPHA_TRANSPARENT, BUTTON_ALPHA_OPAQUE);
659206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        final ObjectAnimator showDecrementButton = ObjectAnimator.ofFloat(mDecrementButton,
660b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                PROPERTY_BUTTON_ALPHA, BUTTON_ALPHA_TRANSPARENT, BUTTON_ALPHA_OPAQUE);
661206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mShowInputControlsAnimator = new AnimatorSet();
662b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mShowInputControlsAnimator.playTogether(mDimSelectorWheelAnimator, showIncrementButton,
6635203194fdff0c0aaefc405d65871ef0e06e7c682Svetoslav Ganov                showDecrementButton);
664206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mShowInputControlsAnimator.addListener(new AnimatorListenerAdapter() {
665206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            private boolean mCanceled = false;
666206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
667206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            @Override
668206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            public void onAnimationEnd(Animator animation) {
669206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (!mCanceled) {
670206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    // if canceled => we still want the wheel drawn
671b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    setSelectorWheelState(SELECTOR_WHEEL_STATE_SMALL);
67268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook                }
673206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mCanceled = false;
67468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook            }
67568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
676206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            @Override
677206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            public void onAnimationCancel(Animator animation) {
678206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (mShowInputControlsAnimator.isRunning()) {
679206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    mCanceled = true;
680206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
681206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
682206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        });
68368f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
684206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // create the fling and adjust scrollers
685bf80562d22b2bbe7944d80d0524c69d0238010cbSvetoslav Ganov        mFlingScroller = new Scroller(getContext(), null, true);
6869cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mAdjustScroller = new Scroller(getContext(), new DecelerateInterpolator(2.5f));
687fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
688206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        updateInputTextView();
689206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        updateIncrementAndDecrementButtonsVisibilityState();
6909cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
691b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mFlingable) {
692b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov           if (isInEditMode()) {
693b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov               setSelectorWheelState(SELECTOR_WHEEL_STATE_SMALL);
694b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov           } else {
695b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                // Start with shown selector wheel and hidden controls. When made
696b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                // visible hide the selector and fade-in the controls to suggest
697b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                // fling interaction.
698b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                setSelectorWheelState(SELECTOR_WHEEL_STATE_LARGE);
699b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                hideInputControls();
700b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov           }
7019cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        }
702206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
703206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
704206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
7059cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
706ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int msrdWdth = getMeasuredWidth();
707ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int msrdHght = getMeasuredHeight();
708ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
709ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        // Increment button at the top.
710ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inctBtnMsrdWdth = mIncrementButton.getMeasuredWidth();
711ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int incrBtnLeft = (msrdWdth - inctBtnMsrdWdth) / 2;
712ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int incrBtnTop = 0;
713ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int incrBtnRight = incrBtnLeft + inctBtnMsrdWdth;
714ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int incrBtnBottom = incrBtnTop + mIncrementButton.getMeasuredHeight();
715ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mIncrementButton.layout(incrBtnLeft, incrBtnTop, incrBtnRight, incrBtnBottom);
716ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
717ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        // Input text centered horizontally.
718ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtMsrdWdth = mInputText.getMeasuredWidth();
719ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtMsrdHght = mInputText.getMeasuredHeight();
720ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtLeft = (msrdWdth - inptTxtMsrdWdth) / 2;
721ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtTop = (msrdHght - inptTxtMsrdHght) / 2;
722ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtRight = inptTxtLeft + inptTxtMsrdWdth;
723ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int inptTxtBottom = inptTxtTop + inptTxtMsrdHght;
724ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mInputText.layout(inptTxtLeft, inptTxtTop, inptTxtRight, inptTxtBottom);
725ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
726ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        // Decrement button at the top.
727ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int decrBtnMsrdWdth = mIncrementButton.getMeasuredWidth();
728ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int decrBtnLeft = (msrdWdth - decrBtnMsrdWdth) / 2;
729ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int decrBtnTop = msrdHght - mDecrementButton.getMeasuredHeight();
730ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int decrBtnRight = decrBtnLeft + decrBtnMsrdWdth;
731ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int decrBtnBottom = msrdHght;
732ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        mDecrementButton.layout(decrBtnLeft, decrBtnTop, decrBtnRight, decrBtnBottom);
733e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov
734b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (!mScrollWheelAndFadingEdgesInitialized) {
735b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mScrollWheelAndFadingEdgesInitialized = true;
736b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            // need to do all this when we know our size
737b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            initializeSelectorWheel();
738b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            initializeFadingEdges();
739b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
740206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
741206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
742206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
743e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
744698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        // Try greedily to fit the max width and height.
745698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int newWidthMeasureSpec = makeMeasureSpec(widthMeasureSpec, mMaxWidth);
746698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int newHeightMeasureSpec = makeMeasureSpec(heightMeasureSpec, mMaxHeight);
747ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        super.onMeasure(newWidthMeasureSpec, newHeightMeasureSpec);
748698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        // Flag if we are measured with width or height less than the respective min.
749698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int desiredWidth = Math.max(mMinWidth, getMeasuredWidth());
750698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int desiredHeight = Math.max(mMinHeight, getMeasuredHeight());
751698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int widthSize = resolveSizeAndState(desiredWidth, newWidthMeasureSpec, 0);
752698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        final int heightSize = resolveSizeAndState(desiredHeight, newHeightMeasureSpec, 0);
753698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov        setMeasuredDimension(widthSize, heightSize);
754e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    }
755e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov
756e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    @Override
757206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public boolean onInterceptTouchEvent(MotionEvent event) {
7584243dc394d89a93cb207efa36e9755c2424d688bSvetoslav Ganov        if (!isEnabled() || !mFlingable) {
75951c52edad7d40697d7fb2a091f850506fa897643Svetoslav Ganov            return false;
76051c52edad7d40697d7fb2a091f850506fa897643Svetoslav Ganov        }
761206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        switch (event.getActionMasked()) {
762206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            case MotionEvent.ACTION_DOWN:
763206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mLastMotionEventY = mLastDownEventY = event.getY();
764206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                removeAllCallbacks();
765092bda57c4b8caad378400b2bc97b9ac66fd2179Svetoslav Ganov                mShowInputControlsAnimator.cancel();
766b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                mDimSelectorWheelAnimator.cancel();
767206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mBeginEditOnUpEvent = false;
768206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mAdjustScrollerOnUpEvent = true;
769b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                if (mSelectorWheelState == SELECTOR_WHEEL_STATE_LARGE) {
77050f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                    boolean scrollersFinished = mFlingScroller.isFinished()
771206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                            && mAdjustScroller.isFinished();
77250f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                    if (!scrollersFinished) {
77350f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                        mFlingScroller.forceFinished(true);
77450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                        mAdjustScroller.forceFinished(true);
7759cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                        onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
77650f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                    }
77750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                    mBeginEditOnUpEvent = scrollersFinished;
778206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    mAdjustScrollerOnUpEvent = true;
779092bda57c4b8caad378400b2bc97b9ac66fd2179Svetoslav Ganov                    hideInputControls();
780206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return true;
781206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
782e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                if (isEventInVisibleViewHitRect(event, mIncrementButton)
783e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                        || isEventInVisibleViewHitRect(event, mDecrementButton)) {
784e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                    return false;
785206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
786e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                mAdjustScrollerOnUpEvent = false;
787e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                setSelectorWheelState(SELECTOR_WHEEL_STATE_LARGE);
788e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                hideInputControls();
789e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov                return true;
790206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            case MotionEvent.ACTION_MOVE:
791206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                float currentMoveY = event.getY();
792206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY);
793206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (deltaDownY > mTouchSlop) {
794206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    mBeginEditOnUpEvent = false;
7959cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                    onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
796b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    setSelectorWheelState(SELECTOR_WHEEL_STATE_LARGE);
797092bda57c4b8caad378400b2bc97b9ac66fd2179Svetoslav Ganov                    hideInputControls();
798206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return true;
799206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
800206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                break;
801206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
802206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return false;
803206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
804206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
805206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
806206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public boolean onTouchEvent(MotionEvent ev) {
80751c52edad7d40697d7fb2a091f850506fa897643Svetoslav Ganov        if (!isEnabled()) {
80851c52edad7d40697d7fb2a091f850506fa897643Svetoslav Ganov            return false;
80951c52edad7d40697d7fb2a091f850506fa897643Svetoslav Ganov        }
810206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mVelocityTracker == null) {
811206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mVelocityTracker = VelocityTracker.obtain();
812206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
813206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mVelocityTracker.addMovement(ev);
814206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int action = ev.getActionMasked();
815206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        switch (action) {
816206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            case MotionEvent.ACTION_MOVE:
817206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                float currentMoveY = ev.getY();
81850f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                if (mBeginEditOnUpEvent
81950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                        || mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
820206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY);
821206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    if (deltaDownY > mTouchSlop) {
822206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        mBeginEditOnUpEvent = false;
8239cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                        onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
824206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    }
825206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
826206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int deltaMoveY = (int) (currentMoveY - mLastMotionEventY);
827206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                scrollBy(0, deltaMoveY);
828206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                invalidate();
829206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mLastMotionEventY = currentMoveY;
830206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                break;
831206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            case MotionEvent.ACTION_UP:
832206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (mBeginEditOnUpEvent) {
833b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    setSelectorWheelState(SELECTOR_WHEEL_STATE_SMALL);
8349cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                    showInputControls(mShowInputControlsAnimimationDuration);
835206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    mInputText.requestFocus();
836206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return true;
837206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
838206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                VelocityTracker velocityTracker = mVelocityTracker;
839206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
840206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int initialVelocity = (int) velocityTracker.getYVelocity();
841206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (Math.abs(initialVelocity) > mMinimumFlingVelocity) {
842206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    fling(initialVelocity);
8439cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                    onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
844206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                } else {
845206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    if (mAdjustScrollerOnUpEvent) {
846206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        if (mFlingScroller.isFinished() && mAdjustScroller.isFinished()) {
847206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                            postAdjustScrollerCommand(0);
848206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        }
849206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    } else {
850206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        postAdjustScrollerCommand(SHOW_INPUT_CONTROLS_DELAY_MILLIS);
851206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    }
852206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
853206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mVelocityTracker.recycle();
854206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mVelocityTracker = null;
855206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                break;
856206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
857206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return true;
858206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
859206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
860206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
861206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public boolean dispatchTouchEvent(MotionEvent event) {
862b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        final int action = event.getActionMasked();
863b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        switch (action) {
864b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            case MotionEvent.ACTION_MOVE:
865b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                if (mSelectorWheelState == SELECTOR_WHEEL_STATE_LARGE) {
866b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    removeAllCallbacks();
867b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                    forceCompleteChangeCurrentByOneViaScroll();
868b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                }
869b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                break;
870b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            case MotionEvent.ACTION_CANCEL:
871b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            case MotionEvent.ACTION_UP:
872b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                removeAllCallbacks();
873b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                break;
874206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
875206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return super.dispatchTouchEvent(event);
876206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
877206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
878206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
879206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public boolean dispatchKeyEvent(KeyEvent event) {
880206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int keyCode = event.getKeyCode();
881206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
882206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeAllCallbacks();
883206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
884206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return super.dispatchKeyEvent(event);
885206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
886206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
887206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
888206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public boolean dispatchTrackballEvent(MotionEvent event) {
889206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int action = event.getActionMasked();
890206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
891206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeAllCallbacks();
892206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
893206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return super.dispatchTrackballEvent(event);
894206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
896206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
897206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public void computeScroll() {
898b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mSelectorWheelState == SELECTOR_WHEEL_STATE_NONE) {
899206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
900206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
901206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        Scroller scroller = mFlingScroller;
902206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (scroller.isFinished()) {
903206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            scroller = mAdjustScroller;
904206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            if (scroller.isFinished()) {
905206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                return;
906206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
907206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
908206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        scroller.computeScrollOffset();
909206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int currentScrollerY = scroller.getCurrY();
910206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mPreviousScrollerY == 0) {
911206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mPreviousScrollerY = scroller.getStartY();
912206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
913206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        scrollBy(0, currentScrollerY - mPreviousScrollerY);
914206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mPreviousScrollerY = currentScrollerY;
915206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (scroller.isFinished()) {
916206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            onScrollerFinished(scroller);
917206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
918206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            invalidate();
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
921fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setEnabled(boolean enabled) {
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        super.setEnabled(enabled);
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mIncrementButton.setEnabled(enabled);
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mDecrementButton.setEnabled(enabled);
927206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setEnabled(enabled);
928206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
929206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
930206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
931206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public void scrollBy(int x, int y) {
932b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mSelectorWheelState == SELECTOR_WHEEL_STATE_NONE) {
933b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            return;
934b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
935b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        int[] selectorIndices = mSelectorIndices;
93634c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov        if (!mWrapSelectorWheel && y > 0
93734c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov                && selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX] <= mMinValue) {
938206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mCurrentScrollOffset = mInitialScrollOffset;
939206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
940206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
94134c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov        if (!mWrapSelectorWheel && y < 0
94234c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov                && selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX] >= mMaxValue) {
943206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mCurrentScrollOffset = mInitialScrollOffset;
944206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
945206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
946206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mCurrentScrollOffset += y;
947b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        while (mCurrentScrollOffset - mInitialScrollOffset > mSelectorTextGapHeight) {
948206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mCurrentScrollOffset -= mSelectorElementHeight;
949206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            decrementSelectorIndices(selectorIndices);
950206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            changeCurrent(selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX]);
951b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (!mWrapSelectorWheel && selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX] <= mMinValue) {
952206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mCurrentScrollOffset = mInitialScrollOffset;
953206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
954206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
955b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        while (mCurrentScrollOffset - mInitialScrollOffset < -mSelectorTextGapHeight) {
956206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mCurrentScrollOffset += mSelectorElementHeight;
957b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            incrementSelectorIndices(selectorIndices);
958206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            changeCurrent(selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX]);
959b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (!mWrapSelectorWheel && selectorIndices[SELECTOR_MIDDLE_ITEM_INDEX] >= mMaxValue) {
960206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                mCurrentScrollOffset = mInitialScrollOffset;
961206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
962206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
9639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
964fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
9654bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    @Override
9664bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    public int getSolidColor() {
9674bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov        return mSolidColor;
9684bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    }
9694bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov
97068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
9714bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Sets the listener to be notified on change of the current value.
972206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
973e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param onValueChangedListener The listener.
97468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
975cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov    public void setOnValueChangedListener(OnValueChangeListener onValueChangedListener) {
976cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov        mOnValueChangeListener = onValueChangedListener;
97750f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    }
97850f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
97950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    /**
9804bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Set listener to be notified for scroll state changes.
98150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     *
982e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param onScrollListener The listener.
98350f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     */
98450f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    public void setOnScrollListener(OnScrollListener onScrollListener) {
98550f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        mOnScrollListener = onScrollListener;
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
987fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
98868f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
9894bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Set the formatter to be used for formatting the current value.
9904bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * <p>
991e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Note: If you have provided alternative values for the values this
992e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * formatter is never invoked.
9934bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * </p>
994206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
995e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param formatter The formatter object. If formatter is <code>null</code>,
996e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *            {@link String#valueOf(int)} will be used.
9974bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     *
998e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @see #setDisplayedValues(String[])
99968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setFormatter(Formatter formatter) {
1001e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (formatter == mFormatter) {
1002e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return;
1003e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mFormatter = formatter;
1005b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
10069cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        updateInputTextView();
1007206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1008206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1009206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1010e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Set the current value for the number picker.
10114bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * <p>
1012e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * If the argument is less than the {@link NumberPicker#getMinValue()} and
1013e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * {@link NumberPicker#getWrapSelectorWheel()} is <code>false</code> the
1014e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * current value is set to the {@link NumberPicker#getMinValue()} value.
1015e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * </p>
1016e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * <p>
1017e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * If the argument is less than the {@link NumberPicker#getMinValue()} and
1018e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * {@link NumberPicker#getWrapSelectorWheel()} is <code>true</code> the
1019e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * current value is set to the {@link NumberPicker#getMaxValue()} value.
1020e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * </p>
1021e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * <p>
1022e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * If the argument is less than the {@link NumberPicker#getMaxValue()} and
1023e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * {@link NumberPicker#getWrapSelectorWheel()} is <code>false</code> the
1024e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * current value is set to the {@link NumberPicker#getMaxValue()} value.
1025e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * </p>
1026e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * <p>
1027e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * If the argument is less than the {@link NumberPicker#getMaxValue()} and
1028e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * {@link NumberPicker#getWrapSelectorWheel()} is <code>true</code> the
1029e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * current value is set to the {@link NumberPicker#getMinValue()} value.
10304bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * </p>
1031206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
1032e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param value The current value.
10334bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * @see #setWrapSelectorWheel(boolean)
1034e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @see #setMinValue(int)
1035e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @see #setMaxValue(int)
1036206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1037e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public void setValue(int value) {
1038e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mValue == value) {
1039a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov            return;
1040a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov        }
1041e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (value < mMinValue) {
1042e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            value = mWrapSelectorWheel ? mMaxValue : mMinValue;
1043206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1044e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (value > mMaxValue) {
1045e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            value = mWrapSelectorWheel ? mMinValue : mMaxValue;
10468bcbebd4178b2e9aca9ee3bd9e1e12c42e74c8dbAdam Powell        }
1047e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        mValue = value;
1048b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
1049e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        updateInputTextView();
1050e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        updateIncrementAndDecrementButtonsVisibilityState();
1051b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        invalidate();
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1053fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
105468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1055ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * Computes the max width if no such specified as an attribute.
1056ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     */
1057ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    private void tryComputeMaxWidth() {
1058ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        if (!mComputeMaxWidth) {
1059ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            return;
1060ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        }
1061ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        int maxTextWidth = 0;
1062ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        if (mDisplayedValues == null) {
1063ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            float maxDigitWidth = 0;
1064ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            for (int i = 0; i <= 9; i++) {
1065ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                final float digitWidth = mSelectorWheelPaint.measureText(String.valueOf(i));
1066ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                if (digitWidth > maxDigitWidth) {
1067ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                    maxDigitWidth = digitWidth;
1068ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                }
1069ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            }
1070ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            int numberOfDigits = 0;
1071ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            int current = mMaxValue;
1072ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            while (current > 0) {
1073ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                numberOfDigits++;
1074ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                current = current / 10;
1075ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            }
1076ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            maxTextWidth = (int) (numberOfDigits * maxDigitWidth);
1077ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        } else {
1078ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            final int valueCount = mDisplayedValues.length;
1079ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            for (int i = 0; i < valueCount; i++) {
1080ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                final float textWidth = mSelectorWheelPaint.measureText(mDisplayedValues[i]);
1081ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                if (textWidth > maxTextWidth) {
1082ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                    maxTextWidth = (int) textWidth;
1083ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                }
1084ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            }
1085ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        }
1086ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        maxTextWidth += mInputText.getPaddingLeft() + mInputText.getPaddingRight();
1087ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        if (mMaxWidth != maxTextWidth) {
1088ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            if (maxTextWidth > mMinWidth) {
1089ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                mMaxWidth = maxTextWidth;
1090ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            } else {
1091ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                mMaxWidth = mMinWidth;
1092ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            }
1093ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov            invalidate();
1094ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        }
1095ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    }
1096ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov
1097ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov    /**
1098e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Gets whether the selector wheel wraps when reaching the min/max value.
109968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     *
1100e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @return True if the selector wheel wraps.
11014bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     *
1102e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @see #getMinValue()
1103e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @see #getMaxValue()
110468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1105e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public boolean getWrapSelectorWheel() {
1106e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mWrapSelectorWheel;
1107206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1108206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1109206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1110e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Sets whether the selector wheel shown during flinging/scrolling should
1111e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * wrap around the {@link NumberPicker#getMinValue()} and
1112e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * {@link NumberPicker#getMaxValue()} values.
1113e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * <p>
1114e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * By default if the range (max - min) is more than five (the number of
1115e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * items shown on the selector wheel) the selector wheel wrapping is
1116e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * enabled.
1117e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * </p>
1118206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
11196304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov     * @param wrapSelectorWheel Whether to wrap.
1120206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
11216304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov    public void setWrapSelectorWheel(boolean wrapSelectorWheel) {
11226304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov        if (wrapSelectorWheel && (mMaxValue - mMinValue) < mSelectorIndices.length) {
1123206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            throw new IllegalStateException("Range less than selector items count.");
1124206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
11256304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov        if (wrapSelectorWheel != mWrapSelectorWheel) {
11266304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            mWrapSelectorWheel = wrapSelectorWheel;
1127156f20919b3d5f298f8851215adbf65f8b4dc61bSvetoslav Ganov            updateIncrementAndDecrementButtonsVisibilityState();
1128206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
11299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11324bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * Sets the speed at which the numbers be incremented and decremented when
11334bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * the up and down buttons are long pressed respectively.
1134e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * <p>
1135e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * The default value is 300 ms.
1136e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * </p>
113768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     *
11384bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     * @param intervalMillis The speed (in milliseconds) at which the numbers
1139e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *            will be incremented and decremented.
11409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11414bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    public void setOnLongPressUpdateInterval(long intervalMillis) {
11424bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov        mLongPressUpdateInterval = intervalMillis;
11439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1144fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1145206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1146e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Returns the value of the picker.
1147e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *
1148e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @return The value.
1149e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     */
1150e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public int getValue() {
1151e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mValue;
1152e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    }
1153e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
1154e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    /**
1155e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Returns the min value of the picker.
1156e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *
1157e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @return The min value
1158e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     */
1159e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public int getMinValue() {
1160e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mMinValue;
1161e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    }
1162e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
1163e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    /**
1164e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Sets the min value of the picker.
1165206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     *
1166e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param minValue The min value.
1167206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1168e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public void setMinValue(int minValue) {
1169e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mMinValue == minValue) {
1170e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return;
1171e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1172e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (minValue < 0) {
1173e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            throw new IllegalArgumentException("minValue must be >= 0");
1174e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1175e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        mMinValue = minValue;
1176e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mMinValue > mValue) {
1177e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mValue = mMinValue;
1178e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1179e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        boolean wrapSelectorWheel = mMaxValue - mMinValue > mSelectorIndices.length;
1180e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        setWrapSelectorWheel(wrapSelectorWheel);
1181b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
1182e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        updateInputTextView();
1183ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        tryComputeMaxWidth();
1184206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1185206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
11864bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    /**
1187e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Returns the max value of the picker.
11884bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     *
1189e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @return The max value.
11904bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     */
1191e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public int getMaxValue() {
1192e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mMaxValue;
11934bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    }
11944bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov
11954bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov    /**
1196e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Sets the max value of the picker.
11974bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     *
1198e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param maxValue The max value.
11994bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov     */
1200e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public void setMaxValue(int maxValue) {
1201e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mMaxValue == maxValue) {
1202e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return;
1203e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1204e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (maxValue < 0) {
1205e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            throw new IllegalArgumentException("maxValue must be >= 0");
1206e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1207e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        mMaxValue = maxValue;
1208e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mMaxValue < mValue) {
1209e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mValue = mMaxValue;
1210e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1211e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        boolean wrapSelectorWheel = mMaxValue - mMinValue > mSelectorIndices.length;
1212e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        setWrapSelectorWheel(wrapSelectorWheel);
1213b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
1214e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        updateInputTextView();
1215ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        tryComputeMaxWidth();
1216e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    }
1217e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
1218e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    /**
1219e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Gets the values to be displayed instead of string values.
1220e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *
1221e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @return The displayed values.
1222e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     */
1223e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public String[] getDisplayedValues() {
1224e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mDisplayedValues;
1225e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    }
1226e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov
1227e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    /**
1228e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * Sets the values to be displayed.
1229e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     *
1230e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     * @param displayedValues The displayed values.
1231e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov     */
1232e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov    public void setDisplayedValues(String[] displayedValues) {
1233e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mDisplayedValues == displayedValues) {
1234e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return;
1235e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1236e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        mDisplayedValues = displayedValues;
1237e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mDisplayedValues != null) {
1238e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            // Allow text entry rather than strictly numeric entry.
1239e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mInputText.setRawInputType(InputType.TYPE_CLASS_TEXT
1240e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                    | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
1241e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        } else {
1242e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mInputText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
1243e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        }
1244e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        updateInputTextView();
1245b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
1246206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1247206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1248206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
1249206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    protected float getTopFadingEdgeStrength() {
1250206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return TOP_AND_BOTTOM_FADING_EDGE_STRENGTH;
1251206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1252206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1253206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
1254206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    protected float getBottomFadingEdgeStrength() {
1255206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return TOP_AND_BOTTOM_FADING_EDGE_STRENGTH;
1256206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1257206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1258206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
12599cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    protected void onAttachedToWindow() {
12609cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        super.onAttachedToWindow();
12619cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        // make sure we show the controls only the very
12629cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        // first time the user sees this widget
12634fd9dae6a23a22142f73749c291c9fd18459e381Svetoslav Ganov        if (mFlingable && !isInEditMode()) {
12649cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            // animate a bit slower the very first time
12659cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            showInputControls(mShowInputControlsAnimimationDuration * 2);
12669cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        }
12679cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    }
12689cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
12699cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    @Override
1270206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    protected void onDetachedFromWindow() {
1271206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        removeAllCallbacks();
1272206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1273206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1274206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
1275206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    protected void dispatchDraw(Canvas canvas) {
1276206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // There is a good reason for doing this. See comments in draw().
1277206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1278206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1279206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
1280206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    public void draw(Canvas canvas) {
1281206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // Dispatch draw to our children only if we are not currently running
1282b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        // the animation for simultaneously dimming the scroll wheel and
1283206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // showing in the buttons. This class takes advantage of the View
1284206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // implementation of fading edges effect to draw the selector wheel.
1285206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // However, in View.draw(), the fading is applied after all the children
1286206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // have been drawn and we do not want this fading to be applied to the
1287b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        // buttons. Therefore, we draw our children after we have completed
1288b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        // drawing ourselves.
1289c75ec3324e9e3e6500148cc2978caab90ea962e4Chet Haase        super.draw(canvas);
1290206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1291206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // Draw our children if we are not showing the selector wheel of fading
1292206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // it out
1293b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mShowInputControlsAnimator.isRunning()
1294b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                || mSelectorWheelState != SELECTOR_WHEEL_STATE_LARGE) {
1295206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            long drawTime = getDrawingTime();
1296206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            for (int i = 0, count = getChildCount(); i < count; i++) {
1297206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                View child = getChildAt(i);
1298206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (!child.isShown()) {
1299206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    continue;
1300206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1301206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                drawChild(canvas, getChildAt(i), drawTime);
1302206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
1303206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1304206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1305206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1306206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @Override
1307206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    protected void onDraw(Canvas canvas) {
1308b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mSelectorWheelState == SELECTOR_WHEEL_STATE_NONE) {
1309206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
1310206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1311b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1312206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        float x = (mRight - mLeft) / 2;
1313206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        float y = mCurrentScrollOffset;
1314206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1315b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        final int restoreCount = canvas.save();
1316b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1317b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mSelectorWheelState == SELECTOR_WHEEL_STATE_SMALL) {
1318b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            Rect clipBounds = canvas.getClipBounds();
1319b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            clipBounds.inset(0, mSelectorElementHeight);
1320b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            canvas.clipRect(clipBounds);
1321b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
1322b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
13239cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        // draw the selector wheel
1324b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        int[] selectorIndices = mSelectorIndices;
1325206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        for (int i = 0; i < selectorIndices.length; i++) {
1326206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            int selectorIndex = selectorIndices[i];
1327206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            String scrollSelectorValue = mSelectorIndexToStringCache.get(selectorIndex);
13286304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            // Do not draw the middle item if input is visible since the input is shown only
13296304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            // if the wheel is static and it covers the middle item. Otherwise, if the user
13306304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            // starts editing the text via the IME he may see a dimmed version of the old
13316304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            // value intermixed with the new one.
13326304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            if (i != SELECTOR_MIDDLE_ITEM_INDEX || mInputText.getVisibility() != VISIBLE) {
13336304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov                canvas.drawText(scrollSelectorValue, x, y, mSelectorWheelPaint);
13346304b0d58e74509a9f21b67b5227b2fee2f1b60fSvetoslav Ganov            }
1335206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            y += mSelectorElementHeight;
1336206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
13379cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
13389cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        // draw the selection dividers (only if scrolling and drawable specified)
13399cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        if (mSelectionDivider != null) {
13409cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            // draw the top divider
13419cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            int topOfTopDivider =
13429cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                (getHeight() - mSelectorElementHeight - mSelectionDividerHeight) / 2;
13439cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            int bottomOfTopDivider = topOfTopDivider + mSelectionDividerHeight;
13449cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            mSelectionDivider.setBounds(0, topOfTopDivider, mRight, bottomOfTopDivider);
13459cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            mSelectionDivider.draw(canvas);
13469cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
13479cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            // draw the bottom divider
13489cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            int topOfBottomDivider =  topOfTopDivider + mSelectorElementHeight;
13499cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            int bottomOfBottomDivider = bottomOfTopDivider + mSelectorElementHeight;
13509cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            mSelectionDivider.setBounds(0, topOfBottomDivider, mRight, bottomOfBottomDivider);
13519cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            mSelectionDivider.draw(canvas);
13529cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        }
1353b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1354b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        canvas.restoreToCount(restoreCount);
13559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1356fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
13573fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov    @Override
13583fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov    public void sendAccessibilityEvent(int eventType) {
13593fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov        // Do not send accessibility events - we want the user to
13603fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov        // perceive this widget as several controls rather as a whole.
13613fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov    }
13623fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov
136368f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1364ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * Makes a measure spec that tries greedily to use the max value.
1365e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     *
1366e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     * @param measureSpec The measure spec.
1367698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov     * @param maxSize The max value for the size.
1368ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov     * @return A measure spec greedily imposing the max size.
1369e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     */
1370698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov    private int makeMeasureSpec(int measureSpec, int maxSize) {
1371ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov        final int size = MeasureSpec.getSize(measureSpec);
1372e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        final int mode = MeasureSpec.getMode(measureSpec);
1373e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        switch (mode) {
1374e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            case MeasureSpec.EXACTLY:
1375ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                return measureSpec;
1376e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            case MeasureSpec.AT_MOST:
1377698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov                return MeasureSpec.makeMeasureSpec(Math.min(size, maxSize), MeasureSpec.EXACTLY);
1378e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            case MeasureSpec.UNSPECIFIED:
1379698e1d5d867a7ac653fa1d6f29cef4cfb73fad38Svetoslav Ganov                return MeasureSpec.makeMeasureSpec(maxSize, MeasureSpec.EXACTLY);
1380e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            default:
1381ec1e06a00d26a783fb948e739ad8157c03e55302Svetoslav Ganov                throw new IllegalArgumentException("Unknown measure mode: " + mode);
1382e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        }
1383e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    }
1384e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov
1385e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    /**
1386a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov     * Resets the selector indices and clear the cached
1387a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov     * string representation of these indices.
1388a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov     */
1389b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void initializeSelectorWheelIndices() {
1390a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov        mSelectorIndexToStringCache.clear();
1391b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        int[] selectorIdices = mSelectorIndices;
1392b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        int current = getValue();
1393b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        for (int i = 0; i < mSelectorIndices.length; i++) {
1394b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            int selectorIndex = current + (i - SELECTOR_MIDDLE_ITEM_INDEX);
1395b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (mWrapSelectorWheel) {
1396b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                selectorIndex = getWrappedSelectorIndex(selectorIndex);
1397b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            }
1398b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mSelectorIndices[i] = selectorIndex;
1399b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            ensureCachedScrollSelectorValue(mSelectorIndices[i]);
1400a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov        }
1401a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov    }
1402a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov
1403a911d4a2b1be7e9ea909a579167826e4a5bac1a3Svetoslav Ganov    /**
1404206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Sets the current value of this NumberPicker, and sets mPrevious to the
1405206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * previous value. If current is greater than mEnd less than mStart, the
1406206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * value of mCurrent is wrapped around. Subclasses can override this to
1407206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * change the wrapping behavior
140868f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     *
140968f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     * @param current the new value of the NumberPicker
141068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1411206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void changeCurrent(int current) {
1412e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mValue == current) {
1413206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
1414206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // Wrap around the values if we go past the start or end
1416e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel) {
1417206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            current = getWrappedSelectorIndex(current);
14189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1419e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        int previous = mValue;
1420e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        setValue(current);
1421206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        notifyChange(previous, current);
14229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1423fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
142468f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1425b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Changes the current value by one which is increment or
1426b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * decrement based on the passes argument.
1427b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     *
1428b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * @param increment True to increment, false to decrement.
1429b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
1430b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void changeCurrentByOne(boolean increment) {
1431b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mFlingable) {
1432b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mDimSelectorWheelAnimator.cancel();
1433b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mInputText.setVisibility(View.INVISIBLE);
1434b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mSelectorWheelPaint.setAlpha(SELECTOR_WHEEL_BRIGHT_ALPHA);
1435b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mPreviousScrollerY = 0;
1436b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            forceCompleteChangeCurrentByOneViaScroll();
1437b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (increment) {
1438b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight,
1439b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                        CHANGE_CURRENT_BY_ONE_SCROLL_DURATION);
1440b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            } else {
1441b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight,
1442b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                        CHANGE_CURRENT_BY_ONE_SCROLL_DURATION);
1443b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            }
1444b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            invalidate();
1445b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        } else {
1446b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (increment) {
1447b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                changeCurrent(mValue + 1);
1448b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            } else {
1449b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                changeCurrent(mValue - 1);
1450b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            }
1451b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
1452b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    }
1453b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1454b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
1455b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Ensures that if we are in the process of changing the current value
1456b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * by one via scrolling the scroller gets to its final state and the
1457b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * value is updated.
1458b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
1459b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void forceCompleteChangeCurrentByOneViaScroll() {
1460b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        Scroller scroller = mFlingScroller;
1461b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (!scroller.isFinished()) {
1462b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            final int yBeforeAbort = scroller.getCurrY();
1463b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            scroller.abortAnimation();
1464b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            final int yDelta = scroller.getCurrY() - yBeforeAbort;
1465b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            scrollBy(0, yDelta);
1466b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
1467b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    }
1468b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1469b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
1470206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Sets the <code>alpha</code> of the {@link Paint} for drawing the selector
1471206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * wheel.
147268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1473206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    @SuppressWarnings("unused")
1474b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    // Called via reflection
1475206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void setSelectorPaintAlpha(int alpha) {
1476b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mSelectorWheelPaint.setAlpha(alpha);
1477b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        invalidate();
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
148068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1481e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov     * @return If the <code>event</code> is in the visible <code>view</code>.
148268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1483e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov    private boolean isEventInVisibleViewHitRect(MotionEvent event, View view) {
1484e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        if (view.getVisibility() == VISIBLE) {
1485e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            view.getHitRect(mTempRect);
1486e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov            return mTempRect.contains((int) event.getX(), (int) event.getY());
1487e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        }
1488e0c8ab5aaa02d7b9b01fe68fb893af4d78482f6cSvetoslav Ganov        return false;
1489206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1490206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1491206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1492b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Sets the <code>selectorWheelState</code>.
1493206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1494b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void setSelectorWheelState(int selectorWheelState) {
1495b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mSelectorWheelState = selectorWheelState;
1496b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (selectorWheelState == SELECTOR_WHEEL_STATE_LARGE) {
1497b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mSelectorWheelPaint.setAlpha(SELECTOR_WHEEL_BRIGHT_ALPHA);
1498b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        }
14993fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov
1500b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mFlingable && selectorWheelState == SELECTOR_WHEEL_STATE_LARGE
15013fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov                && AccessibilityManager.getInstance(mContext).isEnabled()) {
15023fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            AccessibilityManager.getInstance(mContext).interrupt();
15033fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            String text = mContext.getString(R.string.number_picker_increment_scroll_action);
15043fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            mInputText.setContentDescription(text);
15053fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            mInputText.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
15063fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            mInputText.setContentDescription(null);
15073fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov        }
1508206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1509206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1510b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void initializeSelectorWheel() {
1511b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        initializeSelectorWheelIndices();
1512b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        int[] selectorIndices = mSelectorIndices;
15139cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        int totalTextHeight = selectorIndices.length * mTextSize;
151401fa0d7aae1a551e1e7cfb90d2aeaf2fcb3978afSvetoslav Ganov        float totalTextGapHeight = (mBottom - mTop) - totalTextHeight;
151501fa0d7aae1a551e1e7cfb90d2aeaf2fcb3978afSvetoslav Ganov        float textGapCount = selectorIndices.length - 1;
1516b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount + 0.5f);
1517b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mSelectorElementHeight = mTextSize + mSelectorTextGapHeight;
1518eeafd4296b7cf4c2ad9c985562f6d70e2a3ffc03Chet Haase        // Ensure that the middle item is positioned the same as the text in mInputText
1519eeafd4296b7cf4c2ad9c985562f6d70e2a3ffc03Chet Haase        int editTextTextPosition = mInputText.getBaseline() + mInputText.getTop();
1520eeafd4296b7cf4c2ad9c985562f6d70e2a3ffc03Chet Haase        mInitialScrollOffset = editTextTextPosition -
1521eeafd4296b7cf4c2ad9c985562f6d70e2a3ffc03Chet Haase                (mSelectorElementHeight * SELECTOR_MIDDLE_ITEM_INDEX);
15226a19fcde07527706c2a9175e0b9f2a71549b7ffeSvetoslav Ganov        mCurrentScrollOffset = mInitialScrollOffset;
15239cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        updateInputTextView();
15249cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    }
15259cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov
1526b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void initializeFadingEdges() {
1527b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        setVerticalFadingEdgeEnabled(true);
1528b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        setFadingEdgeLength((mBottom - mTop - mTextSize) / 2);
1529b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    }
1530b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1531206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1532206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Callback invoked upon completion of a given <code>scroller</code>.
1533206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1534206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void onScrollerFinished(Scroller scroller) {
1535206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (scroller == mFlingScroller) {
1536b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            if (mSelectorWheelState == SELECTOR_WHEEL_STATE_LARGE) {
1537b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                postAdjustScrollerCommand(0);
1538b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
1539b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            } else {
1540b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                updateInputTextView();
1541b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov                fadeSelectorWheel(mShowInputControlsAnimimationDuration);
1542b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            }
15439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
1544206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            updateInputTextView();
15459cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            showInputControls(mShowInputControlsAnimimationDuration);
15469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1548fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1549206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
15509cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * Handles transition to a given <code>scrollState</code>
155150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov     */
15529cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    private void onScrollStateChange(int scrollState) {
15539cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        if (mScrollState == scrollState) {
15549cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            return;
15559cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        }
15569cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mScrollState = scrollState;
15579cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        if (mOnScrollListener != null) {
15584bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov            mOnScrollListener.onScrollStateChange(this, scrollState);
155950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov        }
156050f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    }
156150f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov
156250f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov    /**
1563206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Flings the selector with the given <code>velocityY</code>.
1564206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1565206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void fling(int velocityY) {
1566206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mPreviousScrollerY = 0;
1567206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        Scroller flingScroller = mFlingScroller;
1568206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1569e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel) {
1570206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            if (velocityY > 0) {
1571206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                flingScroller.fling(0, 0, 0, velocityY, 0, 0, 0, Integer.MAX_VALUE);
1572206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            } else {
1573206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                flingScroller.fling(0, Integer.MAX_VALUE, 0, velocityY, 0, 0, 0, Integer.MAX_VALUE);
1574206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
1575206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1576206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            if (velocityY > 0) {
1577e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                int maxY = mTextSize * (mValue - mMinValue);
1578206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                flingScroller.fling(0, 0, 0, velocityY, 0, 0, 0, maxY);
1579206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            } else {
1580e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                int startY = mTextSize * (mMaxValue - mValue);
1581206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int maxY = startY;
1582206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                flingScroller.fling(0, startY, 0, velocityY, 0, 0, 0, maxY);
15832bf761c20253222a763bce9a63d14c48ab08a556Suchi Amalapurapu            }
15849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1585206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1586206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        invalidate();
15879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1589206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1590206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Hides the input controls which is the up/down arrows and the text field.
1591206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1592206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void hideInputControls() {
1593206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mShowInputControlsAnimator.cancel();
1594206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mIncrementButton.setVisibility(INVISIBLE);
1595206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mDecrementButton.setVisibility(INVISIBLE);
1596206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setVisibility(INVISIBLE);
1597206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1598c39a6e0c51e182338deb8b63d07933b585134929The Android Open Source Project
1599206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1600206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Show the input controls by making them visible and animating the alpha
1601206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * property up/down arrows.
16029cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     *
16039cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * @param animationDuration The duration of the animation.
1604206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
16059cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov    private void showInputControls(long animationDuration) {
1606206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        updateIncrementAndDecrementButtonsVisibilityState();
1607206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setVisibility(VISIBLE);
16089cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov        mShowInputControlsAnimator.setDuration(animationDuration);
1609206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mShowInputControlsAnimator.start();
1610206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1611c39a6e0c51e182338deb8b63d07933b585134929The Android Open Source Project
1612206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1613b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Fade the selector wheel via an animation.
1614b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     *
1615b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * @param animationDuration The duration of the animation.
1616b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     */
1617b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void fadeSelectorWheel(long animationDuration) {
1618b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mInputText.setVisibility(VISIBLE);
1619b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mDimSelectorWheelAnimator.setDuration(animationDuration);
1620b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mDimSelectorWheelAnimator.start();
1621b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    }
1622b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov
1623b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    /**
1624206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Updates the visibility state of the increment and decrement buttons.
1625206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1626206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void updateIncrementAndDecrementButtonsVisibilityState() {
1627e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel || mValue < mMaxValue) {
1628206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mIncrementButton.setVisibility(VISIBLE);
1629206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1630206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mIncrementButton.setVisibility(INVISIBLE);
1631206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1632e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel || mValue > mMinValue) {
1633206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mDecrementButton.setVisibility(VISIBLE);
1634206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1635206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mDecrementButton.setVisibility(INVISIBLE);
16369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1640206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @return The wrapped index <code>selectorIndex</code> value.
164168f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1642206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int getWrappedSelectorIndex(int selectorIndex) {
1643e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (selectorIndex > mMaxValue) {
1644e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return mMinValue + (selectorIndex - mMaxValue) % (mMaxValue - mMinValue) - 1;
1645e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        } else if (selectorIndex < mMinValue) {
1646e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            return mMaxValue - (mMinValue - selectorIndex) % (mMaxValue - mMinValue) + 1;
1647206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1648206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        return selectorIndex;
16499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1650fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1651206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1652206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Increments the <code>selectorIndices</code> whose string representations
1653206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * will be displayed in the selector.
1654206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1655b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void incrementSelectorIndices(int[] selectorIndices) {
1656206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        for (int i = 0; i < selectorIndices.length - 1; i++) {
1657206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            selectorIndices[i] = selectorIndices[i + 1];
1658206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1659206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int nextScrollSelectorIndex = selectorIndices[selectorIndices.length - 2] + 1;
1660e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel && nextScrollSelectorIndex > mMaxValue) {
1661e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            nextScrollSelectorIndex = mMinValue;
1662206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1663206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        selectorIndices[selectorIndices.length - 1] = nextScrollSelectorIndex;
1664206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        ensureCachedScrollSelectorValue(nextScrollSelectorIndex);
1665206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1666fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1667206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1668206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Decrements the <code>selectorIndices</code> whose string representations
1669206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * will be displayed in the selector.
1670206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1671206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void decrementSelectorIndices(int[] selectorIndices) {
1672206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        for (int i = selectorIndices.length - 1; i > 0; i--) {
1673206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            selectorIndices[i] = selectorIndices[i - 1];
1674206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1675206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        int nextScrollSelectorIndex = selectorIndices[1] - 1;
1676e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (mWrapSelectorWheel && nextScrollSelectorIndex < mMinValue) {
1677e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            nextScrollSelectorIndex = mMaxValue;
1678206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1679206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        selectorIndices[0] = nextScrollSelectorIndex;
1680206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        ensureCachedScrollSelectorValue(nextScrollSelectorIndex);
1681206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1682fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1683206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1684206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Ensures we have a cached string representation of the given <code>
1685206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * selectorIndex</code>
1686206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * to avoid multiple instantiations of the same string.
1687206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1688206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void ensureCachedScrollSelectorValue(int selectorIndex) {
1689206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        SparseArray<String> cache = mSelectorIndexToStringCache;
1690206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        String scrollSelectorValue = cache.get(selectorIndex);
1691206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (scrollSelectorValue != null) {
1692206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return;
1693206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1694e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        if (selectorIndex < mMinValue || selectorIndex > mMaxValue) {
1695206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            scrollSelectorValue = "";
1696206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1697206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            if (mDisplayedValues != null) {
1698e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                int displayedValueIndex = selectorIndex - mMinValue;
169950f34d14f6dd3411fdbdb6a7b8b285c2b8fdbf5cSvetoslav Ganov                scrollSelectorValue = mDisplayedValues[displayedValueIndex];
1700206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            } else {
1701206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                scrollSelectorValue = formatNumber(selectorIndex);
17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
17039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1704206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        cache.put(selectorIndex, scrollSelectorValue);
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1706fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1707206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private String formatNumber(int value) {
1708e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return (mFormatter != null) ? mFormatter.format(value) : String.valueOf(value);
1709206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1711206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void validateInputTextView(View v) {
1712206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        String str = String.valueOf(((TextView) v).getText());
1713206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (TextUtils.isEmpty(str)) {
1714206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            // Restore to the old value as we don't allow empty values
1715206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            updateInputTextView();
1716206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1717206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            // Check the new value and ensure it's in range
1718206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            int current = getSelectedPos(str.toString());
1719206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            changeCurrent(current);
17209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1721206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1722fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1723206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1724206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Updates the view of this NumberPicker. If displayValues were specified in
17259cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * the string corresponding to the index specified by the current value will
17269cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * be returned. Otherwise, the formatter specified in {@link #setFormatter}
17279cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov     * will be used to format the number.
1728206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1729206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void updateInputTextView() {
1730206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        /*
1731206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * If we don't have displayed values then use the current number else
1732206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * find the correct value in the displayed values for the current
1733206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         * number.
1734206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov         */
1735206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mDisplayedValues == null) {
1736e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mInputText.setText(formatNumber(mValue));
1737206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1738e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov            mInputText.setText(mDisplayedValues[mValue - mMinValue]);
17399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1740206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.setSelection(mInputText.getText().length());
17413fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov
17423fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov        if (mFlingable && AccessibilityManager.getInstance(mContext).isEnabled()) {
17433fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            String text = mContext.getString(R.string.number_picker_increment_scroll_mode,
17443fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov                    mInputText.getText());
17453fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov            mInputText.setContentDescription(text);
17463fec3fe0e3a83c5e0d1264f34bcc55b158537bc6Svetoslav Ganov        }
1747206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1748fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1749206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1750206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Notifies the listener, if registered, of a change of the value of this
1751206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * NumberPicker.
1752206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1753206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void notifyChange(int previous, int current) {
1754cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov        if (mOnValueChangeListener != null) {
1755cedc446684e94c9971c38c3206f1f224314bda2aSvetoslav Ganov            mOnValueChangeListener.onValueChange(this, previous, mValue);
1756206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1757206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
17589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1759206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1760b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Posts a command for changing the current value by one.
1761b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     *
1762b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * @param increment Whether to increment or decrement the value.
1763206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1764b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    private void postChangeCurrentByOneFromLongPress(boolean increment) {
1765206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mInputText.clearFocus();
1766206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        removeAllCallbacks();
1767b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mChangeCurrentByOneFromLongPressCommand == null) {
1768b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mChangeCurrentByOneFromLongPressCommand = new ChangeCurrentByOneFromLongPressCommand();
1769206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1770b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        mChangeCurrentByOneFromLongPressCommand.setIncrement(increment);
1771b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        post(mChangeCurrentByOneFromLongPressCommand);
1772206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
17739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1774206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1775206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Removes all pending callback from the message queue.
1776206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1777206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void removeAllCallbacks() {
1778b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        if (mChangeCurrentByOneFromLongPressCommand != null) {
1779b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            removeCallbacks(mChangeCurrentByOneFromLongPressCommand);
1780206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1781206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mAdjustScrollerCommand != null) {
1782206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeCallbacks(mAdjustScrollerCommand);
1783206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1784206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mSetSelectionCommand != null) {
1785206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeCallbacks(mSetSelectionCommand);
17869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
17879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1790206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * @return The selected index given its displayed <code>value</code>.
1791206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1792206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private int getSelectedPos(String value) {
17939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mDisplayedValues == null) {
1794aa516beb79693ceffbd81a7c9d14c940e412cccbRomain Guy            try {
1795206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                return Integer.parseInt(value);
1796aa516beb79693ceffbd81a7c9d14c940e412cccbRomain Guy            } catch (NumberFormatException e) {
1797206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                // Ignore as if it's not a number we don't care
1798aa516beb79693ceffbd81a7c9d14c940e412cccbRomain Guy            }
17999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
18009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i < mDisplayedValues.length; i++) {
1801206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                // Don't force the user to type in jan when ja will do
1802206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                value = value.toLowerCase();
1803206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (mDisplayedValues[i].toLowerCase().startsWith(value)) {
1804e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                    return mMinValue + i;
18059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
18069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1807fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1808206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            /*
1809206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov             * The user might have typed in a number into the month field i.e.
18109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             * 10 instead of OCT so support that too.
18119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project             */
18129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1813206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                return Integer.parseInt(value);
18149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (NumberFormatException e) {
1815fdf6db6748b32f620a7f68b13a67c38b4e59e5edTom Taylor
1816206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                // Ignore as if it's not a number we don't care
18179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
18189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1819e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov        return mMinValue;
18209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
18229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1823206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Posts an {@link SetSelectionCommand} from the given <code>selectionStart
1824206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * </code> to
1825206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * <code>selectionEnd</code>.
18269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1827206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void postSetSelectionCommand(int selectionStart, int selectionEnd) {
1828206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mSetSelectionCommand == null) {
1829206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mSetSelectionCommand = new SetSelectionCommand();
1830206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1831206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeCallbacks(mSetSelectionCommand);
1832206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1833206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mSetSelectionCommand.mSelectionStart = selectionStart;
1834206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        mSetSelectionCommand.mSelectionEnd = selectionEnd;
1835206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        post(mSetSelectionCommand);
18369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
183768f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
183868f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1839206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Posts an {@link AdjustScrollerCommand} within the given <code>
1840206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * delayMillis</code>
1841206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * .
184268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1843206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    private void postAdjustScrollerCommand(int delayMillis) {
1844206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        if (mAdjustScrollerCommand == null) {
1845206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mAdjustScrollerCommand = new AdjustScrollerCommand();
1846206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        } else {
1847206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            removeCallbacks(mAdjustScrollerCommand);
1848206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1849206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        postDelayed(mAdjustScrollerCommand, delayMillis);
185068f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    }
185168f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook
185268f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    /**
1853206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Filter for accepting only valid indices or prefixes of the string
1854206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * representation of valid indices.
185568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook     */
1856206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    class InputTextFilter extends NumberKeyListener {
1857206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1858206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // XXX This doesn't allow for range limits when controlled by a
1859206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        // soft input method!
1860206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        public int getInputType() {
1861206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return InputType.TYPE_CLASS_TEXT;
1862206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1863206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1864206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        @Override
1865206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        protected char[] getAcceptedChars() {
1866206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            return DIGIT_CHARACTERS;
1867206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1868206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1869206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        @Override
1870206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
1871206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int dstart, int dend) {
1872206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            if (mDisplayedValues == null) {
1873206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                CharSequence filtered = super.filter(source, start, end, dest, dstart, dend);
1874206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (filtered == null) {
1875206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    filtered = source.subSequence(start, end);
1876206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1877206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1878206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                String result = String.valueOf(dest.subSequence(0, dstart)) + filtered
1879206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        + dest.subSequence(dend, dest.length());
1880206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1881206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if ("".equals(result)) {
1882206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return result;
1883206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1884206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                int val = getSelectedPos(result);
1885206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1886206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                /*
1887206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                 * Ensure the user can't type in a value greater than the max
1888206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                 * allowed. We have to allow less than min as the user might
1889206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                 * want to delete some numbers and then type a new number.
1890206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                 */
1891e9730bf3d2dcbea1879f24c18aaf9810ac57084cSvetoslav Ganov                if (val > mMaxValue) {
1892206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return "";
1893206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                } else {
1894206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return filtered;
1895206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1896206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            } else {
1897206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                CharSequence filtered = String.valueOf(source.subSequence(start, end));
1898206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                if (TextUtils.isEmpty(filtered)) {
1899206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    return "";
1900206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1901206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                String result = String.valueOf(dest.subSequence(0, dstart)) + filtered
1902206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        + dest.subSequence(dend, dest.length());
1903206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                String str = String.valueOf(result).toLowerCase();
1904206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                for (String val : mDisplayedValues) {
1905206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    String valLowerCase = val.toLowerCase();
1906206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    if (valLowerCase.startsWith(str)) {
1907206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        postSetSelectionCommand(result.length(), val.length());
1908206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                        return val.subSequence(dstart, val.length());
1909206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                    }
1910206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                }
1911206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov                return "";
1912206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            }
1913206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1914206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1915206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1916206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1917206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Command for setting the input text selection.
1918206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1919206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    class SetSelectionCommand implements Runnable {
1920206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        private int mSelectionStart;
1921206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1922206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        private int mSelectionEnd;
1923206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1924206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        public void run() {
1925206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mInputText.setSelection(mSelectionStart, mSelectionEnd);
1926206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1927206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1928206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1929206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1930206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * Command for adjusting the scroller to show in its center the closest of
1931206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     * the displayed items.
1932206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1933206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    class AdjustScrollerCommand implements Runnable {
1934206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        public void run() {
1935206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            mPreviousScrollerY = 0;
193634c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov            if (mInitialScrollOffset == mCurrentScrollOffset) {
193734c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov                updateInputTextView();
19389cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov                showInputControls(mShowInputControlsAnimimationDuration);
193934c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov                return;
194034c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov            }
194134c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov            // adjust to the closest value
1942206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            int deltaY = mInitialScrollOffset - mCurrentScrollOffset;
194334c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov            if (Math.abs(deltaY) > mSelectorElementHeight / 2) {
194434c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov                deltaY += (deltaY > 0) ? -mSelectorElementHeight : mSelectorElementHeight;
194534c0688ceafbeef2648bd2287b3b3c3801679448Svetoslav Ganov            }
19469cd5fb2f46e219ec8250383fb201b94cb2ee8c0aSvetoslav Ganov            mAdjustScroller.startScroll(0, 0, 0, deltaY, SELECTOR_ADJUSTMENT_DURATION_MILLIS);
1947206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov            invalidate();
1948206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1949206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    }
1950206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1951206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov    /**
1952b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov     * Command for changing the current value from a long press by one.
1953206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov     */
1954b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov    class ChangeCurrentByOneFromLongPressCommand implements Runnable {
1955b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        private boolean mIncrement;
1956206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1957b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov        private void setIncrement(boolean increment) {
1958b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            mIncrement = increment;
1959206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
1960206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov
1961206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        public void run() {
1962b80a3fcad1776c1b9abe3662899660b4f88ac2abSvetoslav Ganov            changeCurrentByOne(mIncrement);
19634bfd794475e6fb34c9dfa83d4302e9db365a3709Svetoslav Ganov            postDelayed(this, mLongPressUpdateInterval);
1964206316a61f904ea0a6b106137dd7715a2c246d4cSvetoslav Ganov        }
196568f2f547f56f239b60d13b2b62a08a65874f6662Paul Westbrook    }
19667762d93621f508f4410fd37b5bbdfec0071b0420Paul Westbrook}
1967