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 5package org.chromium.content.browser.input; 6 7import android.app.AlertDialog; 8import android.content.Context; 9import android.content.DialogInterface; 10import android.content.DialogInterface.OnClickListener; 11 12import org.chromium.content.R; 13import org.chromium.content.browser.input.TwoFieldDatePicker.OnMonthOrWeekChangedListener; 14 15public abstract class TwoFieldDatePickerDialog extends AlertDialog implements OnClickListener, 16 OnMonthOrWeekChangedListener { 17 18 private static final String YEAR = "year"; 19 private static final String POSITION_IN_YEAR = "position_in_year"; 20 21 protected final TwoFieldDatePicker mPicker; 22 protected final OnValueSetListener mCallBack; 23 24 /** 25 * The callback used to indicate the user is done filling in the date. 26 */ 27 public interface OnValueSetListener { 28 29 /** 30 * @param year The year that was set. 31 * @param positionInYear The position in the year that was set. 32 */ 33 void onValueSet(int year, int positionInYear); 34 } 35 36 /** 37 * @param context The context the dialog is to run in. 38 * @param callBack How the parent is notified that the date is set. 39 * @param year The initial year of the dialog. 40 * @param weekOfYear The initial week of the dialog. 41 */ 42 public TwoFieldDatePickerDialog(Context context, 43 OnValueSetListener callBack, 44 int year, 45 int positionInYear, 46 double minValue, 47 double maxValue) { 48 this(context, 0, callBack, year, positionInYear, minValue, maxValue); 49 } 50 51 /** 52 * @param context The context the dialog is to run in. 53 * @param theme the theme to apply to this dialog 54 * @param callBack How the parent is notified that the date is set. 55 * @param year The initial year of the dialog. 56 * @param weekOfYear The initial week of the dialog. 57 */ 58 public TwoFieldDatePickerDialog(Context context, 59 int theme, 60 OnValueSetListener callBack, 61 int year, 62 int positionInYear, 63 double minValue, 64 double maxValue) { 65 super(context, theme); 66 67 mCallBack = callBack; 68 69 setButton(BUTTON_POSITIVE, context.getText( 70 R.string.date_picker_dialog_set), this); 71 setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), 72 (OnClickListener) null); 73 setIcon(0); 74 75 mPicker = createPicker(context, minValue, maxValue); 76 setView(mPicker); 77 mPicker.init(year, positionInYear, this); 78 } 79 80 protected TwoFieldDatePicker createPicker(Context context, double minValue, double maxValue) { 81 return null; 82 } 83 84 @Override 85 public void onClick(DialogInterface dialog, int which) { 86 tryNotifyDateSet(); 87 } 88 89 /** 90 * Notifies the listener, if such, that a date has been set. 91 */ 92 protected void tryNotifyDateSet() { 93 if (mCallBack != null) { 94 mPicker.clearFocus(); 95 mCallBack.onValueSet(mPicker.getYear(), mPicker.getPositionInYear()); 96 } 97 } 98 99 @Override 100 public void onMonthOrWeekChanged(TwoFieldDatePicker view, int year, int positionInYear) { 101 mPicker.init(year, positionInYear, null); 102 } 103 104 /** 105 * Sets the current date. 106 * 107 * @param year The date week year. 108 * @param weekOfYear The date week. 109 */ 110 public void updateDate(int year, int weekOfYear) { 111 mPicker.updateDate(year, weekOfYear); 112 } 113} 114