summaryrefslogtreecommitdiff
path: root/androvdr/src/de/bjusystems/androvdr/gui/TimerDetailsActivity.java
blob: 1bd25ad99f79bf6d22031a34361f5c02ee40154b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package de.bjusystems.androvdr.gui;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import de.bjusystems.androvdr.app.AndroVdrApp;
import de.bjusystems.androvdr.data.Preferences;
import de.bjusystems.androvdr.data.Timer;
import de.bjusystems.androvdr.utils.date.DateFormatter;
import de.bjusystems.androvdr.utils.svdrp.SetTimerClient;
import de.bjusystems.androvdr.utils.svdrp.SvdrpAsyncListener;
import de.bjusystems.androvdr.utils.svdrp.SvdrpAsyncTask;
import de.bjusystems.androvdr.utils.svdrp.SvdrpClient;
import de.bjusystems.androvdr.utils.svdrp.SvdrpEvent;
import de.bjusystems.androvdr.utils.svdrp.SvdrpException;
import de.bjusystems.androvdr.R;

/**
 * This class is used for showing what's
 * current running on all channels
 * @author bju
 */
public class TimerDetailsActivity extends Activity
				implements OnClickListener, OnDateSetListener, OnTimeSetListener, SvdrpAsyncListener<Timer> {

	Preferences prefs;
	TextView dateField;
	TextView startField;
	TextView endField;
	boolean editStart;
	Timer timer;
	SvdrpProgressDialog progress;
	SetTimerClient setTimerClient;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Attach view
		setContentView(R.layout.timer_detail);

    // timer
    timer = ((AndroVdrApp)getApplication()).getCurrentTimer();

    // update display
    updateDisplay();

    // register buttons
    final Button saveButton = (Button) findViewById(R.id.timer_details_save);
    saveButton.setOnClickListener(this);
    if (timer.getNumber() > 0) {
    	saveButton.setText(R.string.timer_details_save_title);
    } else {
    	saveButton.setText(R.string.timer_details_create_title);
    }

    // register text fields for editing
    dateField.setOnClickListener(this);
    startField.setOnClickListener(this);
    endField.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public void onClick(final View view) {
		switch (view.getId()) {
		case R.id.timer_detail_day:
		{
			final Calendar cal = new GregorianCalendar();
			cal.setTime(timer.getStart());
			final DatePickerDialog dialog = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
																									cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			dialog.show();
			break;
		}
		case R.id.timer_detail_start:
		{
			final Calendar cal = new GregorianCalendar();
			cal.setTime(timer.getStart());
			editStart = true;
			final TimePickerDialog dialog = new TimePickerDialog(this, this, cal.get(Calendar.HOUR_OF_DAY),
																									cal.get(Calendar.MINUTE), true);
			dialog.show();
			break;
		}
		case R.id.timer_detail_end:
		{
			final Calendar cal = new GregorianCalendar();
			cal.setTime(timer.getStop());
			editStart = false;
			final TimePickerDialog dialog = new TimePickerDialog(this, this, cal.get(Calendar.HOUR_OF_DAY),
																									cal.get(Calendar.MINUTE), true);
			dialog.show();
			break;
		}
		case R.id.timer_details_save:
		{
			// collect values
	    timer.setTitle(((TextView)findViewById(R.id.timer_detail_title)).getText().toString());

			// create client for saving the timer
			setTimerClient = new SetTimerClient(timer, false);

			// create backgound task
			final SvdrpAsyncTask<Timer, SvdrpClient<Timer>> task = new SvdrpAsyncTask<Timer, SvdrpClient<Timer>>(setTimerClient);

			// create progress
			progress = new SvdrpProgressDialog(this, setTimerClient);

			// attach listener
			task.addListener(this);

			// start task
			task.run();
		}
		}
	}

	public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute) {
		if (editStart) {
			timer.setStart(calculateTime(timer.getStart(), hourOfDay, minute, null));
		} else {
			timer.setStop(calculateTime(timer.getStop(), hourOfDay, minute, timer.getStart()));
		}
		updateDisplay();
	}

	public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
		timer.setStart(calculateDate(timer.getStart(), year, monthOfYear, dayOfMonth));
		updateDisplay();
	}

	private Date calculateDate(final Date oldDate, final int year, final int monthOfYear, final int dayOfMonth) {

		final Calendar cal = new GregorianCalendar();
		cal.setTime(oldDate);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, monthOfYear);
		cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);

		return cal.getTime();
	}

	private Date calculateTime(final Date oldTime, final int hourOfDay, final int minute, final Date startTime) {

		// set hour and minute
		final Calendar cal = new GregorianCalendar();
		cal.setTime(oldTime);
		cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
		cal.set(Calendar.MINUTE, minute);

		// go to the next day if end time before start time
		if (startTime != null) {
			if (cal.getTime().before(startTime)) {
				cal.add(Calendar.DAY_OF_MONTH, 1);
			}
		}

		return cal.getTime();

	}

	private void updateDisplay() {

    ((TextView)findViewById(R.id.timer_detail_title)).setText(timer.getTitle());
    ((TextView)findViewById(R.id.timer_detail_channel)).setText(timer.getChannelName());
    dateField = (TextView)findViewById(R.id.timer_detail_day);
    final DateFormatter dateFormatter = new DateFormatter(timer.getStart());
    dateField.setText(dateFormatter.getDateString());
    startField = (TextView)findViewById(R.id.timer_detail_start);
    startField.setText(dateFormatter.getTimeString());
    endField = (TextView)findViewById(R.id.timer_detail_end);
    endField.setText(new DateFormatter(timer.getStop()).getTimeString());

    final Button button = (Button) findViewById(R.id.timer_details_save);
    if (timer.getNumber() > 0) {
    	// existing timer
    	button.setText(R.string.timer_details_save_title);
    } else {
    	// new timer
    	button.setText(R.string.timer_details_create_title);
    }
	}

	public void svdrpEvent(final SvdrpEvent event, final Timer result) {

		progress.svdrpEvent(event);

		switch (event) {
		case FINISHED:
			// remove this activity from stack
			finish();

			// finish previous activities
			final AndroVdrApp app = (AndroVdrApp) getApplication();
			app.finishActivities();

			// refresh last view
			app.setReload(true);

			// free progress dialog
			progress = null;

			break;
		}
	}

	public void svdrpException(final SvdrpException exception) {
		progress.svdrpException(exception);
	}
}