• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 package org.chromium.content.browser.input;
6 
7 import android.content.Context;
8 
9 import org.chromium.content.R;
10 
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;
16 
17 public class MonthPicker extends TwoFieldDatePicker {
18     private static final int MONTHS_NUMBER = 12;
19 
20     private final String[] mShortMonths;
21 
MonthPicker(Context context, double minValue, double maxValue)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      */
createDateFromValue(double value)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
getDateForValue(double value)50     protected Calendar getDateForValue(double value) {
51         return MonthPicker.createDateFromValue(value);
52     }
53 
54     @Override
setCurrentDate(int year, int month)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
updateSpinners()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      */
getMonth()82     public int getMonth() {
83         return getCurrentDate().get(Calendar.MONTH);
84     }
85 
86     @Override
getPositionInYear()87     public int getPositionInYear() {
88         return getMonth();
89     }
90 
91     @Override
getMaxYear()92     protected int getMaxYear() {
93         return getMaxDate().get(Calendar.YEAR);
94     }
95 
96     @Override
getMinYear()97     protected int getMinYear() {
98         return getMinDate().get(Calendar.YEAR);
99     }
100 
101 
102     @Override
getMaxPositionInYear(int year)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
getMinPositionInYear(int year)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