-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditAlarm.java
263 lines (219 loc) · 7.35 KB
/
EditAlarm.java
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**************************************************************************
*
* Copyright (C) 2012-2015 Alex Taradov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************/
package com.application.brainfit;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class EditAlarm extends Activity
{
private EditText mTitle;
private CheckBox mAlarmEnabled;
private Spinner mOccurence;
private Button mDateButton;
private Button mTimeButton;
private Alarm mAlarm;
private DateTime mDateTime;
private GregorianCalendar mCalendar;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
static final int DAYS_DIALOG_ID = 2;
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.edit);
mTitle = (EditText)findViewById(R.id.title);
mAlarmEnabled = (CheckBox)findViewById(R.id.alarm_checkbox);
mOccurence = (Spinner)findViewById(R.id.occurence_spinner);
mDateButton = (Button)findViewById(R.id.date_button);
mTimeButton = (Button)findViewById(R.id.time_button);
mAlarm = new Alarm(this);
mAlarm.fromIntent(getIntent());
mDateTime = new DateTime(this);
mTitle.setText(mAlarm.getTitle());
mTitle.addTextChangedListener(mTitleChangedListener);
mOccurence.setSelection(mAlarm.getOccurence());
mOccurence.setOnItemSelectedListener(mOccurenceSelectedListener);
mAlarmEnabled.setChecked(mAlarm.getEnabled());
mAlarmEnabled.setOnCheckedChangeListener(mAlarmEnabledChangeListener);
mCalendar = new GregorianCalendar();
mCalendar.setTimeInMillis(mAlarm.getDate());
mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH);
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
mMinute = mCalendar.get(Calendar.MINUTE);
updateButtons();
}
@Override
protected Dialog onCreateDialog(int id)
{
if (DATE_DIALOG_ID == id)
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
else if (TIME_DIALOG_ID == id)
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, mDateTime.is24hClock());
else if (DAYS_DIALOG_ID == id)
return DaysPickerDialog();
else
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
if (DATE_DIALOG_ID == id)
((DatePickerDialog)dialog).updateDate(mYear, mMonth, mDay);
else if (TIME_DIALOG_ID == id)
((TimePickerDialog)dialog).updateTime(mHour, mMinute);
}
public void onDateClick(View view)
{
if (Alarm.ONCE == mAlarm.getOccurence())
showDialog(DATE_DIALOG_ID);
else if (Alarm.WEEKLY == mAlarm.getOccurence())
showDialog(DAYS_DIALOG_ID);
}
public void onTimeClick(View view)
{
showDialog(TIME_DIALOG_ID);
}
public void onDoneClick(View view)
{
Intent intent = new Intent();
mAlarm.toIntent(intent);
setResult(RESULT_OK, intent);
finish();
}
public void onCancelClick(View view)
{
setResult(RESULT_CANCELED, null);
finish();
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
mCalendar = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute);
mAlarm.setDate(mCalendar.getTimeInMillis());
updateButtons();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
mHour = hourOfDay;
mMinute = minute;
mCalendar = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute);
mAlarm.setDate(mCalendar.getTimeInMillis());
updateButtons();
}
};
private TextWatcher mTitleChangedListener = new TextWatcher()
{
public void afterTextChanged(Editable s)
{
mAlarm.setTitle(mTitle.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
};
private AdapterView.OnItemSelectedListener mOccurenceSelectedListener = new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
mAlarm.setOccurence(position);
updateButtons();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
};
private CompoundButton.OnCheckedChangeListener mAlarmEnabledChangeListener = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mAlarm.setEnabled(isChecked);
}
};
private void updateButtons()
{
if (Alarm.ONCE == mAlarm.getOccurence())
mDateButton.setText(mDateTime.formatDate(mAlarm));
else if (Alarm.WEEKLY == mAlarm.getOccurence())
mDateButton.setText(mDateTime.formatDays(mAlarm));
mTimeButton.setText(mDateTime.formatTime(mAlarm));
}
private Dialog DaysPickerDialog()
{
AlertDialog.Builder builder;
final boolean[] days = mDateTime.getDays(mAlarm);
final String[] names = mDateTime.getFullDayNames();
builder = new AlertDialog.Builder(this);
builder.setTitle("Week days");
builder.setMultiChoiceItems(names, days, new DialogInterface.OnMultiChoiceClickListener()
{
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked)
{
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
mDateTime.setDays(mAlarm, days);
updateButtons();
}
});
builder.setNegativeButton("Cancel", null);
return builder.create();
}
}