Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d55429bd authored by Steve Kondik's avatar Steve Kondik Committed by Gerrit Code Review
Browse files

Merge "More ergonomic TimePicker and DatePicker" into gingerbread

parents aa9fdd49 b71a7996
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.util.SparseArray;
import android.view.LayoutInflater;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnChangedListener;
import android.widget.NumberPicker.OnTextChangedListener;

import com.android.internal.R;

@@ -58,6 +59,15 @@ public class DatePicker extends FrameLayout {
    private final NumberPicker mMonthPicker;
    private final NumberPicker mYearPicker;

    // Types of pickers
    private final int DAY_PICKER = 1;
    private final int MONTH_PICKER = 2;
    private final int YEAR_PICKER = 3;

    // List of pickers in the UI
    private PickersList mPickersList = new PickersList();
    private boolean isMediumDateFormat = true;

    /**
     * How we notify users the date has changed.
     */
@@ -195,6 +205,7 @@ public class DatePicker extends FrameLayout {

        if (months[0].startsWith("1")) {
            format = DateFormat.getDateFormat(getContext());
            isMediumDateFormat = false;
        } else {
            format = DateFormat.getMediumDateFormat(getContext());
        }
@@ -226,12 +237,15 @@ public class DatePicker extends FrameLayout {
                if (c == DateFormat.DATE && !didDay) {
                    parent.addView(mDayPicker);
                    didDay = true;
                    mPickersList.add(mDayPicker, DAY_PICKER);
                } else if ((c == DateFormat.MONTH || c == 'L') && !didMonth) {
                    parent.addView(mMonthPicker);
                    didMonth = true;
                    mPickersList.add(mMonthPicker, MONTH_PICKER);
                } else if (c == DateFormat.YEAR && !didYear) {
                    parent.addView (mYearPicker);
                    didYear = true;
                    mPickersList.add(mYearPicker, YEAR_PICKER);
                }
            }
        }
@@ -239,12 +253,47 @@ public class DatePicker extends FrameLayout {
        // Shouldn't happen, but just in case.
        if (!didMonth) {
            parent.addView(mMonthPicker);
            mPickersList.add(mMonthPicker, MONTH_PICKER);
        }
        if (!didDay) {
            parent.addView(mDayPicker);
            mPickersList.add(mDayPicker, DAY_PICKER);
        }
        if (!didYear) {
            parent.addView(mYearPicker);
            mPickersList.add(mYearPicker, YEAR_PICKER);
        }
    }

    private class PickersList {
        // Arrays where to store info about the pickers
        private NumberPicker[] listOfNumberPickers = {null, null, null};
        private NumberPicker[] listOfNumberPickersNext = {null, null, null};
        private int[] listOfNumberPickersType = {0, 0, 0};

        // Index of adding
        private int listIndex = 0;

        public void add(NumberPicker picker, int type) {
            listOfNumberPickers[listIndex] = picker;
            listOfNumberPickersType[listIndex] = type;

            if (listIndex > 0) {
                final int index = new Integer(listIndex-1);

                listOfNumberPickersNext[index] = picker;

                // Set the OnTextChangedListener to first two NumberPickers
                listOfNumberPickers[index].setOnTextChangeListener(new OnTextChangedListener() {
                    public void onTextChanged(String text) {
                        if ((!isMediumDateFormat && text.length() == 2) || (isMediumDateFormat && (listOfNumberPickersType[index] == MONTH_PICKER && text.length() == 3) || (listOfNumberPickersType[index] != MONTH_PICKER && text.length() == 2))) {
                            listOfNumberPickers[index+1].requestTextFocus();
                        }
                    }
                });
            }

            listIndex++;
        }
    }

+65 −0
Original line number Diff line number Diff line
@@ -21,9 +21,11 @@ import com.android.internal.R;
import android.annotation.Widget;
import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@@ -50,6 +52,16 @@ public class NumberPicker extends LinearLayout {
        void onChanged(NumberPicker picker, int oldVal, int newVal);
    }

    /**
     * The callback interface used to indicate the number value has been changed.
     */
    public interface OnTextChangedListener {
        /**
         * @param text   The text of the TextEdit.
         */
        public abstract void onTextChanged(String text);
    }

    /**
     * Interface used to format the number into a string for presentation
     */
@@ -92,6 +104,9 @@ public class NumberPicker extends LinearLayout {

    private final EditText mText;
    private final InputFilter mNumberInputFilter;
    private boolean isFocused = false;
    private boolean buttonPressed = false;
    private String mPrevText = "";

    private String[] mDisplayedValues;

@@ -115,6 +130,7 @@ public class NumberPicker extends LinearLayout {
     */
    private int mPrevious;
    private OnChangedListener mListener;
    private OnTextChangedListener mTextListener = null;
    private Formatter mFormatter;
    private long mSpeed = 300;

@@ -144,6 +160,8 @@ public class NumberPicker extends LinearLayout {

        OnClickListener clickListener = new OnClickListener() {
            public void onClick(View v) {
                buttonPressed = true;

                validateInput(mText);
                if (!mText.hasFocus()) mText.requestFocus();

@@ -153,6 +171,8 @@ public class NumberPicker extends LinearLayout {
                } else if (R.id.decrement == v.getId()) {
                    changeCurrent(mCurrent - 1);
                }

                buttonPressed = false;
            }
        };

@@ -164,6 +184,9 @@ public class NumberPicker extends LinearLayout {
                 */
                if (!hasFocus) {
                    validateInput(v);
                    isFocused = false;
                } else {
                    isFocused = true;
                }
            }
        };
@@ -174,6 +197,8 @@ public class NumberPicker extends LinearLayout {
             * to inform us when the long click has ended.
             */
            public boolean onLongClick(View v) {
                buttonPressed = true;

                /* The text view may still have focus so clear it's focus which will
                 * trigger the on focus changed and any typed values to be pulled.
                 */
@@ -186,6 +211,9 @@ public class NumberPicker extends LinearLayout {
                    mDecrement = true;
                    mHandler.post(mRunnable);
                }

                buttonPressed = false;

                return true;
            }
        };
@@ -206,12 +234,38 @@ public class NumberPicker extends LinearLayout {
        mText.setOnFocusChangeListener(focusListener);
        mText.setFilters(new InputFilter[] {inputFilter});
        mText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
        mText.setSelectAllOnFocus(true);
        mText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            public void afterTextChanged(Editable s) {}

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String curText = mText.getText().toString();

                // Do nothing if there is no TextListener defined or if there is
                // no change in the text or if we don't have focus or if the
                // change is caused by a button
                if (mTextListener == null || mPrevText.compareTo(curText) == 0 || isFocused == false || buttonPressed == true) {
                    return;
                }

                // Save current text
                mPrevText = curText;

                // Notify the listener
                mTextListener.onTextChanged(curText);
            }
        });

        if (!isEnabled()) {
            setEnabled(false);
        }
    }

    public void requestTextFocus() {
        mText.requestFocus();
    }

    /**
     * Set the enabled state of this view. The interpretation of the enabled
     * state varies by subclass.
@@ -234,6 +288,14 @@ public class NumberPicker extends LinearLayout {
        mListener = listener;
    }

    /**
     * Set the callback that indicates the number has been changed by the user.
     * @param listener the callback, should not be null.
     */
    public void setOnTextChangeListener(OnTextChangedListener listener) {
        mTextListener = listener;
    }

    /**
     * Set the formatter that will be used to format the number for presentation
     * @param formatter the formatter object.  If formatter is null, String.valueOf()
@@ -291,6 +353,9 @@ public class NumberPicker extends LinearLayout {
        }
        mCurrent = current;
        updateView();

        // Get the current value
        mPrevText = mText.getText().toString();
    }

    /**
+8 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnTextChangedListener;

import com.android.internal.R;

@@ -126,6 +127,13 @@ public class TimePicker extends FrameLayout {
                onTimeChanged();
            }
        });
        mHourPicker.setOnTextChangeListener(new OnTextChangedListener() {
            public void onTextChanged(String text) {
                if (text.length() == 2) {
                    mMinutePicker.requestTextFocus();
                }
            }
        });

        // digits of minute
        mMinutePicker = (NumberPicker) findViewById(R.id.minute);