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.
4
5package org.chromium.ui.picker;
6
7import android.content.Context;
8
9import org.chromium.ui.R;
10
11import java.text.DateFormatSymbols;
12import java.util.Arrays;
13import java.util.Calendar;
14import java.util.Locale;
15import java.util.TimeZone;
16
17public class MonthPicker extends TwoFieldDatePicker {
18    private static final int MONTHS_NUMBER = 12;
19
20    private final String[] mShortMonths;
21
22    public MonthPicker(Context context, double minValue, double maxValue) {
23        super(context, minValue, maxValue);
24
25        getPositionInYearSpinner().setContentDescription(
26                getResources().getString(R.string.accessibility_date_picker_month));
27
28        // initialization based on locale
29        mShortMonths =
30                DateFormatSymbols.getInstance(Locale.getDefault()).getShortMonths();
31
32        // initialize to current date
33        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
34        init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), null);
35    }
36
37    /**
38     * Creates a date object from the |value| which is months since epoch.
39     */
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"));
44        cal.clear();
45        cal.set(year, month, 1);
46        return cal;
47    }
48
49    @Override
50    protected Calendar getDateForValue(double value) {
51        return MonthPicker.createDateFromValue(value);
52    }
53
54    @Override
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());
62        } else {
63            setCurrentDate(date);
64        }
65    }
66
67    @Override
68    protected void updateSpinners() {
69        super.updateSpinners();
70
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);
77    }
78
79    /**
80     * @return The selected month.
81     */
82    public int getMonth() {
83        return getCurrentDate().get(Calendar.MONTH);
84    }
85
86    @Override
87    public int getPositionInYear() {
88        return getMonth();
89    }
90
91    @Override
92    protected int getMaxYear() {
93        return getMaxDate().get(Calendar.YEAR);
94    }
95
96    @Override
97    protected int getMinYear() {
98        return getMinDate().get(Calendar.YEAR);
99    }
100
101
102    @Override
103    protected int getMaxPositionInYear(int year) {
104        if (year == getMaxDate().get(Calendar.YEAR)) {
105            return getMaxDate().get(Calendar.MONTH);
106        }
107        return MONTHS_NUMBER - 1;
108    }
109
110    @Override
111    protected int getMinPositionInYear(int year) {
112        if (year == getMinDate().get(Calendar.YEAR)) {
113            return getMinDate().get(Calendar.MONTH);
114        }
115        return 0;
116    }
117}
118