OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 | 8 |
9 import org.chromium.content.R; | 9 import org.chromium.content.R; |
10 | 10 |
11 import java.util.Calendar; | 11 import java.util.Calendar; |
| 12 import java.util.TimeZone; |
12 | 13 |
13 // This class is heavily based on android.widget.DatePicker. | 14 // This class is heavily based on android.widget.DatePicker. |
14 public class WeekPicker extends TwoFieldDatePicker { | 15 public class WeekPicker extends TwoFieldDatePicker { |
15 | 16 |
16 public WeekPicker(Context context, long minValue, long maxValue) { | 17 public WeekPicker(Context context, double minValue, double maxValue) { |
17 super(context, minValue, maxValue); | 18 super(context, minValue, maxValue); |
18 | 19 |
19 getPositionInYearSpinner().setContentDescription( | 20 getPositionInYearSpinner().setContentDescription( |
20 getResources().getString(R.string.accessibility_date_picker_week
)); | 21 getResources().getString(R.string.accessibility_date_picker_week
)); |
21 | 22 |
22 // initialize to current date | 23 // initialize to current date |
23 Calendar cal = Calendar.getInstance(); | 24 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
24 cal.setFirstDayOfWeek(Calendar.MONDAY); | 25 cal.setFirstDayOfWeek(Calendar.MONDAY); |
25 cal.setMinimalDaysInFirstWeek(4); | 26 cal.setMinimalDaysInFirstWeek(4); |
26 cal.setTimeInMillis(System.currentTimeMillis()); | 27 cal.setTimeInMillis(System.currentTimeMillis()); |
27 init(getISOWeekYearForDate(cal), getWeekForDate(cal), null); | 28 init(getISOWeekYearForDate(cal), getWeekForDate(cal), null); |
28 } | 29 } |
29 | 30 |
30 private Calendar createDateFromWeek(int year, int week) { | 31 /** |
31 Calendar date = Calendar.getInstance(); | 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")); |
32 date.clear(); | 36 date.clear(); |
33 date.setFirstDayOfWeek(Calendar.MONDAY); | 37 date.setFirstDayOfWeek(Calendar.MONDAY); |
34 date.setMinimalDaysInFirstWeek(4); | 38 date.setMinimalDaysInFirstWeek(4); |
35 date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); | 39 date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); |
36 date.set(Calendar.YEAR, year); | 40 date.set(Calendar.YEAR, year); |
37 date.set(Calendar.WEEK_OF_YEAR, week); | 41 date.set(Calendar.WEEK_OF_YEAR, week); |
38 return date; | 42 return date; |
39 } | 43 } |
40 | 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 |
41 @Override | 57 @Override |
42 protected Calendar createDateFromValue(long value) { | 58 protected Calendar getDateForValue(double value) { |
43 Calendar date = Calendar.getInstance(); | 59 return WeekPicker.createDateFromValue(value); |
44 date.clear(); | |
45 date.setFirstDayOfWeek(Calendar.MONDAY); | |
46 date.setMinimalDaysInFirstWeek(4); | |
47 date.setTimeInMillis(value); | |
48 return date; | |
49 } | 60 } |
50 | 61 |
51 public static int getISOWeekYearForDate(Calendar date) { | 62 public static int getISOWeekYearForDate(Calendar date) { |
52 int year = date.get(Calendar.YEAR); | 63 int year = date.get(Calendar.YEAR); |
53 int month = date.get(Calendar.MONTH); | 64 int month = date.get(Calendar.MONTH); |
54 int week = date.get(Calendar.WEEK_OF_YEAR); | 65 int week = date.get(Calendar.WEEK_OF_YEAR); |
55 if (month == 0 && week > 51) { | 66 if (month == 0 && week > 51) { |
56 year--; | 67 year--; |
57 } else if (month == 11 && week == 1) { | 68 } else if (month == 11 && week == 1) { |
58 year++; | 69 year++; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 132 } |
122 | 133 |
123 @Override | 134 @Override |
124 protected int getMinPositionInYear(int year) { | 135 protected int getMinPositionInYear(int year) { |
125 if (year == getISOWeekYearForDate(getMinDate())) { | 136 if (year == getISOWeekYearForDate(getMinDate())) { |
126 return getWeekForDate(getMinDate()); | 137 return getWeekForDate(getMinDate()); |
127 } | 138 } |
128 return 1; | 139 return 1; |
129 } | 140 } |
130 } | 141 } |
OLD | NEW |