| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.content.browser.input; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | |
| 9 | import java.text.DateFormatSymbols; |
| 10 | import java.util.Arrays; |
| 11 | import java.util.Calendar; |
| 12 | import java.util.Locale; |
| 13 | |
| 14 | import org.chromium.content.R; |
| 15 | |
| 16 | public class MonthPicker extends TwoFieldDatePicker { |
| 17 | private static final int MONTHS_NUMBER = 12; |
| 18 | |
| 19 | private String[] mShortMonths; |
| 20 | |
| 21 | public MonthPicker(Context context, long minValue, long maxValue) { |
| 22 | super(context, minValue, maxValue); |
| 23 | |
| 24 | getPositionInYearSpinner().setContentDescription( |
| 25 | getResources().getString(R.string.accessibility_date_picker_month)); |
| 26 | |
| 27 | // initialization based on locale |
| 28 | mShortMonths = |
| 29 | DateFormatSymbols.getInstance(Locale.getDefault()).getShortMonths(); |
| 30 | |
| 31 | // initialize to current date |
| 32 | Calendar cal = Calendar.getInstance(); |
| 33 | init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), null); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | protected Calendar createDateFromValue(long value) { |
| 38 | int year = (int)Math.min(value / 12 + 1970, Integer.MAX_VALUE); |
| 39 | int month = (int) (value % 12); |
| 40 | Calendar cal = Calendar.getInstance(); |
| 41 | cal.clear(); |
| 42 | cal.set(year, month, 1); |
| 43 | return cal; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected void setCurrentDate(int year, int month) { |
| 48 | Calendar date = Calendar.getInstance(); |
| 49 | date.set(year, month, 1); |
| 50 | if (date.before(getMinDate())) { |
| 51 | setCurrentDate(getMinDate()); |
| 52 | } else if (date.after(getMaxDate())) { |
| 53 | setCurrentDate(getMaxDate()); |
| 54 | } else { |
| 55 | setCurrentDate(date); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void updateSpinners() { |
| 61 | super.updateSpinners(); |
| 62 | |
| 63 | // make sure the month names are a zero based array |
| 64 | // with the months in the month spinner |
| 65 | String[] displayedValues = Arrays.copyOfRange(mShortMonths, |
| 66 | getPositionInYearSpinner().getMinValue(), |
| 67 | getPositionInYearSpinner().getMaxValue() + 1); |
| 68 | getPositionInYearSpinner().setDisplayedValues(displayedValues); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return The selected month. |
| 73 | */ |
| 74 | public int getMonth() { |
| 75 | return getCurrentDate().get(Calendar.MONTH); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int getPositionInYear() { |
| 80 | return getMonth(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected int getMaxYear() { |
| 85 | return getMaxDate().get(Calendar.YEAR); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected int getMinYear() { |
| 90 | return getMinDate().get(Calendar.YEAR); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | @Override |
| 95 | protected int getMaxPositionInYear() { |
| 96 | if (getYear() == getMaxDate().get(Calendar.YEAR)) { |
| 97 | return getMaxDate().get(Calendar.MONTH); |
| 98 | } |
| 99 | return MONTHS_NUMBER - 1; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected int getMinPositionInYear() { |
| 104 | if (getYear() == getMinDate().get(Calendar.YEAR)) { |
| 105 | return getMinDate().get(Calendar.MONTH); |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | } |