summaryrefslogtreecommitdiff
path: root/chromium/content/public/android/java/src/org/chromium/content/browser/input/WeekPicker.java
blob: efca4763a2935d43d68b6ba201421448e54123d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content.browser.input;

import android.content.Context;

import java.util.Calendar;

import org.chromium.content.R;

// This class is heavily based on android.widget.DatePicker.
public class WeekPicker extends TwoFieldDatePicker {

    public WeekPicker(Context context, long minValue, long maxValue) {
        super(context, minValue, maxValue);

        getPositionInYearSpinner().setContentDescription(
                getResources().getString(R.string.accessibility_date_picker_week));

        // initialize to current date
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setMinimalDaysInFirstWeek(4);
        cal.setTimeInMillis(System.currentTimeMillis());
        init(getISOWeekYearForDate(cal), getWeekForDate(cal), null);
    }

    private Calendar createDateFromWeek(int year, int week) {
        Calendar date = Calendar.getInstance();
        date.clear();
        date.setFirstDayOfWeek(Calendar.MONDAY);
        date.setMinimalDaysInFirstWeek(4);
        date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        date.set(Calendar.YEAR, year);
        date.set(Calendar.WEEK_OF_YEAR, week);
        return date;
    }

    @Override
    protected Calendar createDateFromValue(long value) {
        Calendar date = Calendar.getInstance();
        date.clear();
        date.setFirstDayOfWeek(Calendar.MONDAY);
        date.setMinimalDaysInFirstWeek(4);
        date.setTimeInMillis(value);
        return date;
    }

    public static int getISOWeekYearForDate(Calendar date) {
        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        int week = date.get(Calendar.WEEK_OF_YEAR);
        if (month == 0 && week > 51) {
            year--;
        } else if (month == 11 && week == 1) {
            year++;
        }
        return year;
    }

    public static int getWeekForDate(Calendar date) {
        return date.get(Calendar.WEEK_OF_YEAR);
    }

    @Override
    protected void setCurrentDate(int year, int week) {
        Calendar date = createDateFromWeek(year, week);
        if (date.before(getMinDate())) {
            setCurrentDate(getMinDate());
        } else if (date.after(getMaxDate())) {
            setCurrentDate(getMaxDate());
        } else {
            setCurrentDate(date);
        }
    }

    private int getNumberOfWeeks(int year) {
        // Create a date in the middle of the year, where the week year matches the year.
        Calendar date = createDateFromWeek(year, 20);
        return date.getActualMaximum(Calendar.WEEK_OF_YEAR);
    }

    /**
     * @return The selected year.
     */
    @Override
    public int getYear() {
        return getISOWeekYearForDate(getCurrentDate());
    }

    /**
     * @return The selected week.
     */
    public int getWeek() {
        return getWeekForDate(getCurrentDate());
    }

    @Override
    public int getPositionInYear() {
        return getWeek();
    }

    @Override
    protected int getMaxYear() {
        return getISOWeekYearForDate(getMaxDate());
    }

    @Override
    protected int getMinYear() {
        return getISOWeekYearForDate(getMinDate());
    }

    @Override
    protected int getMaxPositionInYear(int year) {
        if (year == getISOWeekYearForDate(getMaxDate())) {
            return getWeekForDate(getMaxDate());
        }
        return getNumberOfWeeks(year);
    }

    @Override
    protected int getMinPositionInYear(int year) {
        if (year == getISOWeekYearForDate(getMinDate())) {
            return getWeekForDate(getMinDate());
        }
        return 1;
    }
}