Add recently used timezones into the list presented to the user.
[acal.git] / src / com / morphoss / acal / widget / DateTimeDialog.java
blobd2a25b42094a4a1147e3838717c3ce2e3d2857a7
1 /*
2 * Copyright (C) 2011 Morphoss Ltd
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com.morphoss.acal.widget;
21 import java.util.ArrayList;
23 import android.app.Dialog;
24 import android.content.Context;
25 import android.content.SharedPreferences;
26 import android.content.SharedPreferences.Editor;
27 import android.preference.PreferenceManager;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.AdapterView;
31 import android.widget.AdapterView.OnItemSelectedListener;
32 import android.widget.Button;
33 import android.widget.CheckBox;
34 import android.widget.CompoundButton;
35 import android.widget.CompoundButton.OnCheckedChangeListener;
36 import android.widget.DatePicker;
37 import android.widget.DatePicker.OnDateChangedListener;
38 import android.widget.Spinner;
39 import android.widget.TimePicker;
40 import android.widget.TimePicker.OnTimeChangedListener;
42 import com.morphoss.acal.R;
43 import com.morphoss.acal.acaltime.AcalDateTime;
44 import com.morphoss.acal.acaltime.AcalDateTimeFormatter;
46 /**
47 * @author Morphoss Ltd
50 public class DateTimeDialog extends Dialog
51 implements OnClickListener, OnTimeChangedListener, OnDateChangedListener,
52 OnCheckedChangeListener, OnItemSelectedListener
55 private static final String TZPREFNAME = "recentTimeZone-";
56 private Context context;
57 private DateTimeSetListener dialogListener;
59 private Button setButton;
60 private Button cancelButton;
61 private DatePicker datePicker;
62 private TimePicker timePicker;
63 private CheckBox dateOnlyCheckBox;
64 private Spinner timeZoneSpinner;
65 private TimeZoneListAdapter tzListAdapter;
66 private boolean allowDateVsDateTimeSwitching = true;
68 private int oldTimeZoneWas = 0;
70 private ArrayList<String> recentZones;
72 private AcalDateTime currentDateTime;
73 private final boolean use24HourTime;
74 private final boolean allowTimeZoneSetting;
75 private SharedPreferences prefs;
77 public DateTimeDialog(Context context, AcalDateTime dateTimeValue, boolean twentyFourHourTime,
78 boolean allowDateTimeSwitching, boolean allowTimeZones, DateTimeSetListener listener ) {
79 super(context);
80 this.context = context;
81 this.dialogListener = listener;
82 use24HourTime = twentyFourHourTime;
83 setContentView(R.layout.datetime_dialog);
84 allowDateVsDateTimeSwitching = allowDateTimeSwitching;
85 allowTimeZoneSetting = allowTimeZones;
87 recentZones = new ArrayList<String>(10);
88 prefs = PreferenceManager.getDefaultSharedPreferences(context);
89 String recentZonePref;
90 for( int i=0; i<10; i++ ) {
91 recentZonePref = prefs.getString(TZPREFNAME+i, null);
92 if ( recentZonePref != null ) {
93 recentZones.add(recentZonePref);
97 currentDateTime = (dateTimeValue == null ? new AcalDateTime() : dateTimeValue.clone());
98 populateLayout();
102 private void populateLayout() {
104 setButton = (Button)this.findViewById(R.id.DateTimeSetButton);
105 cancelButton = (Button)this.findViewById(R.id.DateTimeCancelButton);
106 setButton.setOnClickListener(this);
107 cancelButton.setOnClickListener(this);
109 datePicker = (DatePicker) this.findViewById(R.id.datePicker);
110 datePicker.init(currentDateTime.getYear(), currentDateTime.getMonth() - 1, currentDateTime.getMonthDay(),this);
112 timePicker = (TimePicker) this.findViewById(R.id.timePicker);
113 timePicker.setIs24HourView(use24HourTime);
114 timePicker.setCurrentHour((int) currentDateTime.getHour());
115 timePicker.setCurrentMinute((int) currentDateTime.getMinute());
116 timePicker.setOnTimeChangedListener(this);
118 dateOnlyCheckBox = (CheckBox) this.findViewById(R.id.DateTimeIsDate);
120 timeZoneSpinner = (Spinner) this.findViewById(R.id.DateTimeZoneSelect);
121 tzListAdapter = new TimeZoneListAdapter(this.context, currentDateTime.getTimeZone(), recentZones);
122 timeZoneSpinner.setAdapter(tzListAdapter);
123 timeZoneSpinner.setOnItemSelectedListener(this);
125 if ( allowDateVsDateTimeSwitching ) {
126 dateOnlyCheckBox.setChecked(currentDateTime.isDate());
127 dateOnlyCheckBox.setOnCheckedChangeListener(this);
129 else {
130 dateOnlyCheckBox.setVisibility(View.GONE);
131 if ( currentDateTime.isDate() ) {
132 timePicker.setVisibility(View.GONE);
133 timeZoneSpinner.setVisibility(View.GONE);
136 if ( !allowTimeZoneSetting )
137 timeZoneSpinner.setVisibility(View.GONE);
139 updateLayout();
143 private void updateLayout() {
144 timePicker.setEnabled(!dateOnlyCheckBox.isChecked());
145 timeZoneSpinner.setSelection(dateOnlyCheckBox.isChecked() ? 0 : tzListAdapter.getPositionOf(currentDateTime.getTimeZoneId()));
146 timeZoneSpinner.setEnabled(!dateOnlyCheckBox.isChecked());
147 this.setTitle(AcalDateTimeFormatter.fmtFull(currentDateTime,use24HourTime));
151 private void toggleIsDate( boolean isDate ) {
152 if ( isDate ) oldTimeZoneWas = timeZoneSpinner.getSelectedItemPosition();
153 currentDateTime.setAsDate(isDate);
154 if ( !isDate ) {
155 currentDateTime.setTimeZone(tzListAdapter.getTzId(oldTimeZoneWas));
156 timeZoneSpinner.setSelection(oldTimeZoneWas);
158 updateLayout();
162 @Override
163 public void onClick(View v) {
164 if (v == setButton) {
165 dialogListener.onDateTimeSet(currentDateTime);
167 if ( !currentDateTime.isFloating() ) {
168 String currentZone = currentDateTime.getTimeZoneId();
169 Editor e = prefs.edit();
170 e.putString(TZPREFNAME+0, currentZone);
171 for( int i=0; i<recentZones.size() && i<9; i++) {
172 if ( recentZones.get(i).equals(currentZone) ) recentZones.remove(i);
173 if ( i<recentZones.size() ) {
174 e.putString(TZPREFNAME+i, recentZones.get(i));
177 e.commit();
181 this.dismiss();
185 @Override
186 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
187 if ( buttonView == dateOnlyCheckBox ) toggleIsDate(isChecked);
191 @Override
192 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
193 currentDateTime.setTimeZone(tzListAdapter.getTzId(timeZoneSpinner.getSelectedItemPosition()));
194 updateLayout();
198 @Override
199 public void onNothingSelected(AdapterView<?> parent) {
200 // Do nothing.
206 @Override
207 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
208 currentDateTime.setHour(hourOfDay);
209 currentDateTime.setMinute(minute);
210 updateLayout();
214 @Override
215 public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
216 currentDateTime.setYearMonthDay(year, monthOfYear+1, dayOfMonth);
217 updateLayout();