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.content.browser.input; |
6 | |
7 | import android.app.AlertDialog; |
8 | import android.content.Context; |
9 | import android.content.DialogInterface; |
10 | import android.content.DialogInterface.OnClickListener; |
11 | import android.os.Build; |
12 | import android.os.Bundle; |
13 | import android.view.View; |
14 | |
15 | import org.chromium.content.browser.input.TwoFieldDatePicker.OnMonthOrWeekChangedListener; |
16 | import org.chromium.content.R; |
17 | |
18 | public abstract class TwoFieldDatePickerDialog extends AlertDialog implements OnClickListener, |
19 | OnMonthOrWeekChangedListener { |
20 | |
21 | private static final String YEAR = "year"; |
22 | private static final String POSITION_IN_YEAR = "position_in_year"; |
23 | |
24 | protected final TwoFieldDatePicker mPicker; |
25 | protected final OnValueSetListener mCallBack; |
26 | |
27 | /** |
28 | * The callback used to indicate the user is done filling in the date. |
29 | */ |
30 | public interface OnValueSetListener { |
31 | |
32 | /** |
33 | * @param year The year that was set. |
34 | * @param positionInYear The week in year. |
35 | * with {@link java.util.Calendar}. |
36 | */ |
37 | void onValueSet(int year, int positionInYear); |
38 | } |
39 | |
40 | /** |
41 | * @param context The context the dialog is to run in. |
42 | * @param callBack How the parent is notified that the date is set. |
43 | * @param year The initial year of the dialog. |
44 | * @param weekOfYear The initial week of the dialog. |
45 | */ |
46 | public TwoFieldDatePickerDialog(Context context, |
47 | OnValueSetListener callBack, |
48 | int year, |
49 | int positionInYear, |
50 | long minValue, |
51 | long maxValue) { |
52 | this(context, 0, callBack, year, positionInYear, minValue, maxValue); |
53 | } |
54 | |
55 | /** |
56 | * @param context The context the dialog is to run in. |
57 | * @param theme the theme to apply to this dialog |
58 | * @param callBack How the parent is notified that the date is set. |
59 | * @param year The initial year of the dialog. |
60 | * @param weekOfYear The initial week of the dialog. |
61 | */ |
62 | public TwoFieldDatePickerDialog(Context context, |
63 | int theme, |
64 | OnValueSetListener callBack, |
65 | int year, |
66 | int positionInYear, |
67 | long minValue, |
68 | long maxValue) { |
69 | super(context, theme); |
70 | |
71 | mCallBack = callBack; |
72 | |
73 | setButton(BUTTON_POSITIVE, context.getText( |
74 | R.string.date_picker_dialog_set), this); |
75 | setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), |
76 | (OnClickListener) null); |
77 | setIcon(0); |
78 | |
79 | mPicker = createPicker(context, minValue, maxValue); |
80 | setView(mPicker); |
81 | mPicker.init(year, positionInYear, this); |
82 | } |
83 | |
84 | protected TwoFieldDatePicker createPicker(Context context, long minValue, long maxValue) { |
85 | return null; |
86 | } |
87 | |
88 | @Override |
89 | public void onClick(DialogInterface dialog, int which) { |
90 | tryNotifyDateSet(); |
91 | } |
92 | |
93 | /** |
94 | * Notifies the listener, if such, that a date has been set. |
95 | */ |
96 | protected abstract void tryNotifyDateSet(); |
97 | |
98 | @Override |
99 | protected void onStop() { |
100 | if (Build.VERSION.SDK_INT >= 16) { |
101 | // The default behavior of dialogs changed in JellyBean and onwards. |
102 | // Dismissing a dialog (by pressing back for example) |
103 | // applies the chosen date. This code is added here so that the custom |
104 | // pickers behave the same as the internal DatePickerDialog. |
105 | tryNotifyDateSet(); |
106 | } |
107 | super.onStop(); |
108 | } |
109 | |
110 | @Override |
111 | public void onMonthOrWeekChanged(TwoFieldDatePicker view, int year, int positionInYear) { |
112 | mPicker.init(year, positionInYear, null); |
113 | } |
114 | |
115 | /** |
116 | * Sets the current date. |
117 | * |
118 | * @param year The date week year. |
119 | * @param weekOfYear The date week. |
120 | */ |
121 | public void updateDate(int year, int weekOfYear) { |
122 | mPicker.updateDate(year, weekOfYear); |
123 | } |
124 | } |