Home | History | Annotate | Download | only in picker
      1 // Copyright 2013 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.ui.picker;
      6 
      7 import android.content.Context;
      8 
      9 import org.chromium.ui.R;
     10 
     11 import java.util.Calendar;
     12 import java.util.TimeZone;
     13 
     14 // This class is heavily based on android.widget.DatePicker.
     15 public class WeekPicker extends TwoFieldDatePicker {
     16 
     17     public WeekPicker(Context context, double minValue, double maxValue) {
     18         super(context, minValue, maxValue);
     19 
     20         getPositionInYearSpinner().setContentDescription(
     21                 getResources().getString(R.string.accessibility_date_picker_week));
     22 
     23         // initialize to current date
     24         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
     25         cal.setFirstDayOfWeek(Calendar.MONDAY);
     26         cal.setMinimalDaysInFirstWeek(4);
     27         cal.setTimeInMillis(System.currentTimeMillis());
     28         init(getISOWeekYearForDate(cal), getWeekForDate(cal), null);
     29     }
     30 
     31     /**
     32      * Creates a date object from the |year| and |week|.
     33      */
     34     public static Calendar createDateFromWeek(int year, int week) {
     35         Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
     36         date.clear();
     37         date.setFirstDayOfWeek(Calendar.MONDAY);
     38         date.setMinimalDaysInFirstWeek(4);
     39         date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
     40         date.set(Calendar.YEAR, year);
     41         date.set(Calendar.WEEK_OF_YEAR, week);
     42         return date;
     43     }
     44 
     45     /**
     46      * Creates a date object from the |value| which is milliseconds since epoch.
     47      */
     48     public static Calendar createDateFromValue(double value) {
     49         Calendar date = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
     50         date.clear();
     51         date.setFirstDayOfWeek(Calendar.MONDAY);
     52         date.setMinimalDaysInFirstWeek(4);
     53         date.setTimeInMillis((long) value);
     54         return date;
     55     }
     56 
     57     @Override
     58     protected Calendar getDateForValue(double value) {
     59         return WeekPicker.createDateFromValue(value);
     60     }
     61 
     62     public static int getISOWeekYearForDate(Calendar date) {
     63         int year = date.get(Calendar.YEAR);
     64         int month = date.get(Calendar.MONTH);
     65         int week = date.get(Calendar.WEEK_OF_YEAR);
     66         if (month == 0 && week > 51) {
     67             year--;
     68         } else if (month == 11 && week == 1) {
     69             year++;
     70         }
     71         return year;
     72     }
     73 
     74     public static int getWeekForDate(Calendar date) {
     75         return date.get(Calendar.WEEK_OF_YEAR);
     76     }
     77 
     78     @Override
     79     protected void setCurrentDate(int year, int week) {
     80         Calendar date = createDateFromWeek(year, week);
     81         if (date.before(getMinDate())) {
     82             setCurrentDate(getMinDate());
     83         } else if (date.after(getMaxDate())) {
     84             setCurrentDate(getMaxDate());
     85         } else {
     86             setCurrentDate(date);
     87         }
     88     }
     89 
     90     private int getNumberOfWeeks(int year) {
     91         // Create a date in the middle of the year, where the week year matches the year.
     92         Calendar date = createDateFromWeek(year, 20);
     93         return date.getActualMaximum(Calendar.WEEK_OF_YEAR);
     94     }
     95 
     96     /**
     97      * @return The selected year.
     98      */
     99     @Override
    100     public int getYear() {
    101         return getISOWeekYearForDate(getCurrentDate());
    102     }
    103 
    104     /**
    105      * @return The selected week.
    106      */
    107     public int getWeek() {
    108         return getWeekForDate(getCurrentDate());
    109     }
    110 
    111     @Override
    112     public int getPositionInYear() {
    113         return getWeek();
    114     }
    115 
    116     @Override
    117     protected int getMaxYear() {
    118         return getISOWeekYearForDate(getMaxDate());
    119     }
    120 
    121     @Override
    122     protected int getMinYear() {
    123         return getISOWeekYearForDate(getMinDate());
    124     }
    125 
    126     @Override
    127     protected int getMaxPositionInYear(int year) {
    128         if (year == getISOWeekYearForDate(getMaxDate())) {
    129             return getWeekForDate(getMaxDate());
    130         }
    131         return getNumberOfWeeks(year);
    132     }
    133 
    134     @Override
    135     protected int getMinPositionInYear(int year) {
    136         if (year == getISOWeekYearForDate(getMinDate())) {
    137             return getWeekForDate(getMinDate());
    138         }
    139         return 1;
    140     }
    141 }
    142