This source file includes following definitions.
- onMonthOrWeekChanged
- init
- isNewDate
- getDateForValue
- updateDate
- setCurrentDate
- setCurrentDate
- dispatchPopulateAccessibilityEvent
- onPopulateAccessibilityEvent
- getYear
- getPositionInYear
- getMaxYear
- getMinYear
- getMaxPositionInYear
- getMinPositionInYear
- getMaxDate
- getMinDate
- getCurrentDate
- getPositionInYearSpinner
- getYearSpinner
- updateSpinners
- notifyDateChanged
package org.chromium.content.browser.input;
import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.accessibility.AccessibilityEvent;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import org.chromium.content.R;
import java.util.Calendar;
import java.util.TimeZone;
public abstract class TwoFieldDatePicker extends FrameLayout {
private final NumberPicker mPositionInYearSpinner;
private final NumberPicker mYearSpinner;
private OnMonthOrWeekChangedListener mMonthOrWeekChangedListener;
private Calendar mMinDate;
private Calendar mMaxDate;
private Calendar mCurrentDate;
public interface OnMonthOrWeekChangedListener {
void onMonthOrWeekChanged(TwoFieldDatePicker view, int year, int positionInYear);
}
public TwoFieldDatePicker(Context context, double minValue, double maxValue) {
super(context, null, android.R.attr.datePickerStyle);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_field_date_picker, this, true);
OnValueChangeListener onChangeListener = new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
int year = getYear();
int positionInYear = getPositionInYear();
if (picker == mPositionInYearSpinner) {
positionInYear = newVal;
if (oldVal == picker.getMaxValue() && newVal == picker.getMinValue()) {
year += 1;
positionInYear = getMinPositionInYear(year);
} else if (oldVal == picker.getMinValue() && newVal == picker.getMaxValue()) {
year -= 1;
positionInYear = getMaxPositionInYear(year);
}
} else if (picker == mYearSpinner) {
year = newVal;
} else {
throw new IllegalArgumentException();
}
setCurrentDate(year, positionInYear);
updateSpinners();
notifyDateChanged();
}
};
mCurrentDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
if (minValue >= maxValue) {
mMinDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
mMinDate.set(0, 0, 1);
mMaxDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
mMaxDate.set(9999, 0, 1);
} else {
mMinDate = getDateForValue(minValue);
mMaxDate = getDateForValue(maxValue);
}
mPositionInYearSpinner = (NumberPicker) findViewById(R.id.position_in_year);
mPositionInYearSpinner.setOnLongPressUpdateInterval(200);
mPositionInYearSpinner.setOnValueChangedListener(onChangeListener);
mYearSpinner = (NumberPicker) findViewById(R.id.year);
mYearSpinner.setOnLongPressUpdateInterval(100);
mYearSpinner.setOnValueChangedListener(onChangeListener);
}
public void init(int year, int positionInYear,
OnMonthOrWeekChangedListener onMonthOrWeekChangedListener) {
setCurrentDate(year, positionInYear);
updateSpinners();
mMonthOrWeekChangedListener = onMonthOrWeekChangedListener;
}
public boolean isNewDate(int year, int positionInYear) {
return (getYear() != year || getPositionInYear() != positionInYear);
}
protected abstract Calendar getDateForValue(double value);
public void updateDate(int year, int positionInYear) {
if (!isNewDate(year, positionInYear)) {
return;
}
setCurrentDate(year, positionInYear);
updateSpinners();
notifyDateChanged();
}
protected abstract void setCurrentDate(int year, int positionInYear);
protected void setCurrentDate(Calendar date) {
mCurrentDate = date;
}
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
onPopulateAccessibilityEvent(event);
return true;
}
@Override
public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
super.onPopulateAccessibilityEvent(event);
final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
String selectedDateUtterance = DateUtils.formatDateTime(getContext(),
mCurrentDate.getTimeInMillis(), flags);
event.getText().add(selectedDateUtterance);
}
public int getYear() {
return mCurrentDate.get(Calendar.YEAR);
}
public abstract int getPositionInYear();
protected abstract int getMaxYear();
protected abstract int getMinYear();
protected abstract int getMaxPositionInYear(int year);
protected abstract int getMinPositionInYear(int year);
protected Calendar getMaxDate() {
return mMaxDate;
}
protected Calendar getMinDate() {
return mMinDate;
}
protected Calendar getCurrentDate() {
return mCurrentDate;
}
protected NumberPicker getPositionInYearSpinner() {
return mPositionInYearSpinner;
}
protected NumberPicker getYearSpinner() {
return mYearSpinner;
}
protected void updateSpinners() {
mPositionInYearSpinner.setDisplayedValues(null);
mPositionInYearSpinner.setMinValue(getMinPositionInYear(getYear()));
mPositionInYearSpinner.setMaxValue(getMaxPositionInYear(getYear()));
mPositionInYearSpinner.setWrapSelectorWheel(
!mCurrentDate.equals(mMinDate) && !mCurrentDate.equals(mMaxDate));
mYearSpinner.setMinValue(getMinYear());
mYearSpinner.setMaxValue(getMaxYear());
mYearSpinner.setWrapSelectorWheel(false);
mYearSpinner.setValue(getYear());
mPositionInYearSpinner.setValue(getPositionInYear());
}
protected void notifyDateChanged() {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
if (mMonthOrWeekChangedListener != null) {
mMonthOrWeekChangedListener.onMonthOrWeekChanged(this, getYear(), getPositionInYear());
}
}
}