1 // Copyright 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.
5 package org.chromium.ui.picker;
7 import android.content.Context;
9 import org.chromium.ui.R;
11 import java.text.DateFormatSymbols;
12 import java.util.Arrays;
13 import java.util.Calendar;
14 import java.util.Locale;
15 import java.util.TimeZone;
17 public class MonthPicker extends TwoFieldDatePicker {
18 private static final int MONTHS_NUMBER = 12;
20 private final String[] mShortMonths;
22 public MonthPicker(Context context, double minValue, double maxValue) {
23 super(context, minValue, maxValue);
25 getPositionInYearSpinner().setContentDescription(
26 getResources().getString(R.string.accessibility_date_picker_month));
28 // initialization based on locale
30 DateFormatSymbols.getInstance(Locale.getDefault()).getShortMonths();
32 // initialize to current date
33 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
34 init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), null);
38 * Creates a date object from the |value| which is months since epoch.
40 public static Calendar createDateFromValue(double value) {
41 int year = (int) Math.min(value / 12 + 1970, Integer.MAX_VALUE);
42 int month = (int) (value % 12);
43 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
45 cal.set(year, month, 1);
50 protected Calendar getDateForValue(double value) {
51 return MonthPicker.createDateFromValue(value);
55 protected void setCurrentDate(int year, int month) {
56 Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
57 date.set(year, month, 1);
58 if (date.before(getMinDate())) {
59 setCurrentDate(getMinDate());
60 } else if (date.after(getMaxDate())) {
61 setCurrentDate(getMaxDate());
68 protected void updateSpinners() {
69 super.updateSpinners();
71 // make sure the month names are a zero based array
72 // with the months in the month spinner
73 String[] displayedValues = Arrays.copyOfRange(mShortMonths,
74 getPositionInYearSpinner().getMinValue(),
75 getPositionInYearSpinner().getMaxValue() + 1);
76 getPositionInYearSpinner().setDisplayedValues(displayedValues);
80 * @return The selected month.
82 public int getMonth() {
83 return getCurrentDate().get(Calendar.MONTH);
87 public int getPositionInYear() {
92 protected int getMaxYear() {
93 return getMaxDate().get(Calendar.YEAR);
97 protected int getMinYear() {
98 return getMinDate().get(Calendar.YEAR);
103 protected int getMaxPositionInYear(int year) {
104 if (year == getMaxDate().get(Calendar.YEAR)) {
105 return getMaxDate().get(Calendar.MONTH);
107 return MONTHS_NUMBER - 1;
111 protected int getMinPositionInYear(int year) {
112 if (year == getMinDate().get(Calendar.YEAR)) {
113 return getMinDate().get(Calendar.MONTH);