| Talking Reminder this project is intenfed for people who are not tech savvy

ReminderEditActivity.java
1 package com.sudogeeks.talking_reminder;
2
3 import android.app.AlertDialog;
4 import android.app.DatePickerDialog;
5 import android.app.TimePickerDialog;
6 import android.content.DialogInterface;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.text.Editable;
10 import android.text.InputType;
11 import android.text.TextWatcher;
12 import android.view.Menu;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.widget.DatePicker;
16 import android.widget.EditText;
17 import android.widget.Switch;
18 import android.widget.TextView;
19 import android.widget.TimePicker;
20 import android.widget.Toast;
21
22 import androidx.appcompat.app.AppCompatActivity;
23 import androidx.appcompat.widget.Toolbar;
24
25 import com.google.android.material.floatingactionbutton.FloatingActionButton;
26
27 import java.util.Calendar;
28
32 public class ReminderEditActivity extends AppCompatActivity {
33
34 // Constant Intent String
35 public static final String EXTRA_REMINDER_ID = "Reminder_ID";
36 // Values for orientation change
37 private static final String KEY_TITLE = "title_key";
38 private static final String KEY_TIME = "time_key";
39 private static final String KEY_DATE = "date_key";
40 private static final String KEY_REPEAT = "repeat_key";
41 private static final String KEY_REPEAT_NO = "repeat_no_key";
42 private static final String KEY_REPEAT_TYPE = "repeat_type_key";
43 private static final String KEY_ACTIVE = "active_key";
44 // Constant values in milliseconds
45 private static final long milMinute = 60000L;
46 private static final long milHour = 3600000L;
47 private static final long milDay = 86400000L;
48 private static final long milWeek = 604800000L;
49 private static final long milMonth = 2592000000L;
50 private Toolbar mToolbar;
51 private EditText mTitleText;
52 private TextView mDateText, mTimeText, mRepeatText, mRepeatNoText, mRepeatTypeText;
53 private FloatingActionButton mFAB1;
54 private FloatingActionButton mFAB2;
55 private Switch mRepeatSwitch;
56 private String mTitle;
57 private String mTime;
58 private String mDate;
59 private String mRepeatNo;
60 private String mRepeatType;
61 private String mActive;
62 private String mRepeat;
63 private String[] mDateSplit;
64 private String[] mTimeSplit;
65 private int mReceivedID;
66 private int mYear, mMonth, mHour, mMinute, mDay;
67 private long mRepeatTime;
68 private Calendar mCalendar;
69 private ReminderDO mReceivedReminderDO;
70 private ReminderDatabase rb;
71 private AlarmReceiver mAlarmReceiver;
72
77 @Override
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 setContentView(R.layout.activity_add_reminder);
81
82 // Initialize Views
83 mToolbar = findViewById(R.id.toolbar);
84 mTitleText = findViewById(R.id.reminder_title);
85 mDateText = findViewById(R.id.set_date);
86 mTimeText = findViewById(R.id.set_time);
87 mRepeatText = findViewById(R.id.set_repeat);
88 mRepeatNoText = findViewById(R.id.set_repeat_no);
89 mRepeatTypeText = findViewById(R.id.set_repeat_type);
90
91 mRepeatSwitch = findViewById(R.id.repeat_switch);
92
93 // Setup Toolbar
94 setSupportActionBar(mToolbar);
95 getSupportActionBar().setTitle(R.string.activity_edit_reminder_label);
96 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
97 getSupportActionBar().setHomeButtonEnabled(true);
98
99 // Setup ReminderDO Title EditText
100 mTitleText.addTextChangedListener(new TextWatcher() {
101 @Override
102 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
103 }
104
105 @Override
106 public void onTextChanged(CharSequence s, int start, int before, int count) {
107 mTitle = s.toString().trim();
108 mTitleText.setError(null);
109 }
110
111 @Override
112 public void afterTextChanged(Editable s) {
113 }
114 });
115
116 // Get reminder id from intent
117 mReceivedID = Integer.parseInt(getIntent().getStringExtra(EXTRA_REMINDER_ID));
118
119 // Get reminder using reminder id
120 rb = new ReminderDatabase(this);
121
122 /*Intent intent = getIntent();
123 if(intent.getType().equals("text/"+Utility.FILE_EXTENSION)){
124 mReceivedReminderDO.setTitle(intent.getStringExtra("mTitle"));
125 System.out.println("**********************************");
126 System.out.println(mReceivedReminderDO.getTitle());
127 System.out.println("**********************************");
128 }
129 else {*/
130 // Get reminder id from intent
131 mReceivedReminderDO = rb.getReminder(mReceivedID);
132 //}
133
134 // Get values from reminder
135 mTitle = mReceivedReminderDO.getTitle();
136 mDate = mReceivedReminderDO.getDate();
137 mTime = mReceivedReminderDO.getTime();
138 mRepeat = mReceivedReminderDO.getRepeat();
139 mRepeatNo = mReceivedReminderDO.getRepeatNo();
140 mRepeatType = mReceivedReminderDO.getRepeatType();
141 mActive = mReceivedReminderDO.getActive();
142
143 // Setup TextViews using reminder values
144 mTitleText.setText(mTitle);
145 mDateText.setText(mDate);
146 mTimeText.setText(mTime);
147 mRepeatNoText.setText(mRepeatNo);
148 mRepeatTypeText.setText(mRepeatType);
149 mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
150
151 // To save state on device rotation
152 if (savedInstanceState != null) {
153 String savedTitle = savedInstanceState.getString(KEY_TITLE);
154 mTitleText.setText(savedTitle);
155 mTitle = savedTitle;
156
157 String savedTime = savedInstanceState.getString(KEY_TIME);
158 mTimeText.setText(savedTime);
159 mTime = savedTime;
160
161 String savedDate = savedInstanceState.getString(KEY_DATE);
162 mDateText.setText(savedDate);
163 mDate = savedDate;
164
165 String saveRepeat = savedInstanceState.getString(KEY_REPEAT);
166 mRepeatText.setText(saveRepeat);
167 mRepeat = saveRepeat;
168
169 String savedRepeatNo = savedInstanceState.getString(KEY_REPEAT_NO);
170 mRepeatNoText.setText(savedRepeatNo);
171 mRepeatNo = savedRepeatNo;
172
173 String savedRepeatType = savedInstanceState.getString(KEY_REPEAT_TYPE);
174 mRepeatTypeText.setText(savedRepeatType);
175 mRepeatType = savedRepeatType;
176
177 mActive = savedInstanceState.getString(KEY_ACTIVE);
178 }
179
180
181 if (mRepeat.equals("false")) {
182 mRepeatSwitch.setChecked(false);
183 mRepeatText.setText(R.string.repeat_off);
184
185 } else if (mRepeat.equals("true")) {
186 mRepeatSwitch.setChecked(true);
187 }
188
189 // Obtain Date and Time details
190 mCalendar = Calendar.getInstance();
191 mAlarmReceiver = new AlarmReceiver();
192
193 mDateSplit = mDate.split("/");
194 mTimeSplit = mTime.split(":");
195
196 mDay = Integer.parseInt(mDateSplit[0]);
197 mMonth = Integer.parseInt(mDateSplit[1]);
198 mYear = Integer.parseInt(mDateSplit[2]);
199 mHour = Integer.parseInt(mTimeSplit[0]);
200 mMinute = Integer.parseInt(mTimeSplit[1]);
201 }
202
203 // To save state on device rotation
204 @Override
205 protected void onSaveInstanceState(Bundle outState) {
206 super.onSaveInstanceState(outState);
207
208 outState.putCharSequence(KEY_TITLE, mTitleText.getText());
209 outState.putCharSequence(KEY_TIME, mTimeText.getText());
210 outState.putCharSequence(KEY_DATE, mDateText.getText());
211 outState.putCharSequence(KEY_REPEAT, mRepeatText.getText());
212 outState.putCharSequence(KEY_REPEAT_NO, mRepeatNoText.getText());
213 outState.putCharSequence(KEY_REPEAT_TYPE, mRepeatTypeText.getText());
214 outState.putCharSequence(KEY_ACTIVE, mActive);
215 }
216
217 @Override
218 public void onActivityResult(int requestCode, int resultCode, Intent data) {
219 super.onActivityResult(requestCode, resultCode, data);
220 }
226 public void setTime(View v) {
227 Calendar now = Calendar.getInstance();
228 TimePickerDialog timePickerDialog = new TimePickerDialog(this,
229 new TimePickerDialog.OnTimeSetListener() {
230
231 @Override
232 public void onTimeSet(TimePicker view, int hourOfDay,
233 int minute) {
234 mHour = hourOfDay;
235 mMinute = minute;
236 if (minute < 10) {
237 mTime = hourOfDay + ":" + "0" + minute;
238 } else {
239 mTime = hourOfDay + ":" + minute;
240 }
241 mTimeText.setText(mTime);
242 }
243 }, mHour, mMinute, false);
244 timePickerDialog.show();
245 }
246
252 public void setDate(View v) {
253 Calendar now = Calendar.getInstance();
254 DatePickerDialog datePickerDialog = new DatePickerDialog(this,
255 new DatePickerDialog.OnDateSetListener() {
256
257 @Override
258 public void onDateSet(DatePicker view, int year,
259 int monthOfYear, int dayOfMonth) {
260 mDay = dayOfMonth;
261 mMonth = monthOfYear + 1;
262 mYear = year;
263 mDate = mDay + "/" + mMonth + "/" + mYear;
264 mDateText.setText(mDate);
265 }
266 }, mYear, mMonth, mDay);
267 datePickerDialog.show();
268 }
269
276 public void onSwitchRepeat(View view) {
277 boolean on = ((Switch) view).isChecked();
278 if (on) {
279 mRepeat = "true";
280 mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
281
282 } else {
283 mRepeat = "false";
284 mRepeatText.setText(R.string.repeat_off);
285 }
286 }
292 public void selectRepeatType(View v) {
293 final String[] items = new String[5];
294
295 items[0] = "Minute";
296 items[1] = "Hour";
297 items[2] = "Day";
298 items[3] = "Week";
299 items[4] = "Month";
300
301 // Create List Dialog
302 AlertDialog.Builder builder = new AlertDialog.Builder(this);
303 builder.setTitle("Select Type");
304 builder.setItems(items, new DialogInterface.OnClickListener() {
305
306 public void onClick(DialogInterface dialog, int item) {
307
308 mRepeatType = items[item];
309 mRepeatTypeText.setText(mRepeatType);
310 mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
311 }
312 });
313 AlertDialog alert = builder.create();
314 alert.show();
315 }
316
322 public void setRepeatNo(View v) {
323 AlertDialog.Builder alert = new AlertDialog.Builder(this);
324 alert.setTitle("Enter Number");
325
326 // Create EditText box to input repeat number
327 final EditText input = new EditText(this);
328 input.setInputType(InputType.TYPE_CLASS_NUMBER);
329 alert.setView(input);
330 alert.setPositiveButton("Ok",
331 new DialogInterface.OnClickListener() {
332 public void onClick(DialogInterface dialog, int whichButton) {
333
334 if (input.getText().toString().length() == 0) {
335 mRepeatNo = Integer.toString(1);
336 mRepeatNoText.setText(mRepeatNo);
337 mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
338 } else {
339 mRepeatNo = input.getText().toString().trim();
340 mRepeatNoText.setText(mRepeatNo);
341 mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
342 }
343 }
344 });
345 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
346 public void onClick(DialogInterface dialog, int whichButton) {
347 // Do nothing
348 }
349 });
350 alert.show();
351 }
356 // On clicking the update button
357 public void updateReminder() {
358 // Set new values in the reminder
359 mReceivedReminderDO.setTitle(mTitle);
360 mReceivedReminderDO.setDate(mDate);
361 mReceivedReminderDO.setTime(mTime);
362 mReceivedReminderDO.setRepeat(mRepeat);
363 mReceivedReminderDO.setRepeatNo(mRepeatNo);
364 mReceivedReminderDO.setRepeatType(mRepeatType);
365 mReceivedReminderDO.setActive(mActive);
366
367 // Update reminder
368 rb.updateReminder(mReceivedReminderDO);
369
370 // Set up calender for creating the notification
371 mCalendar.set(Calendar.MONTH, --mMonth);
372 mCalendar.set(Calendar.YEAR, mYear);
373 mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
374 mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
375 mCalendar.set(Calendar.MINUTE, mMinute);
376 mCalendar.set(Calendar.SECOND, 0);
377
378 // Cancel existing notification of the reminder by using its ID
379 mAlarmReceiver.cancelAlarm(getApplicationContext(), mReceivedID);
380
381 // Check repeat type
382 if (mRepeatType.equals("Minute")) {
383 mRepeatTime = Integer.parseInt(mRepeatNo) * milMinute;
384 } else if (mRepeatType.equals("Hour")) {
385 mRepeatTime = Integer.parseInt(mRepeatNo) * milHour;
386 } else if (mRepeatType.equals("Day")) {
387 mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
388 } else if (mRepeatType.equals("Week")) {
389 mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
390 } else if (mRepeatType.equals("Month")) {
391 mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
392 }
393
394 // Create a new notification
395 if (mActive.equals("true")) {
396 if (mRepeat.equals("true")) {
397 mAlarmReceiver.setRepeatAlarm(getApplicationContext(), mCalendar, mReceivedID, mRepeatTime);
398 } else if (mRepeat.equals("false")) {
399 mAlarmReceiver.setAlarm(getApplicationContext(), mCalendar, mReceivedID);
400 }
401 }
402
403 // Create toast to confirm update
404 Toast.makeText(getApplicationContext(), "Edited",
405 Toast.LENGTH_SHORT).show();
406 onBackPressed();
407 }
411 @Override
412 public void onBackPressed() {
413 super.onBackPressed();
414 }
420 @Override
421 public boolean onCreateOptionsMenu(Menu menu) {
422 getMenuInflater().inflate(R.menu.menu_add_reminder, menu);
423 return true;
424 }
430 @Override
431 public boolean onOptionsItemSelected(MenuItem item) {
432 switch (item.getItemId()) {
433
434 // On clicking the back arrow
435 // Discard any changes
436 case android.R.id.home:
437 onBackPressed();
438 return true;
439
440 // On clicking save reminder button
441 // Update reminder
442 case R.id.save_reminder:
443 mTitleText.setText(mTitle);
444
445 if (mTitleText.getText().toString().length() == 0)
446 mTitleText.setError("ReminderDO Title cannot be blank!");
447
448 else {
449 updateReminder();
450 }
451 return true;
452
453 // On clicking discard reminder button
454 // Discard any changes
455 case R.id.discard_reminder:
456 Toast.makeText(getApplicationContext(), "Changes Discarded",
457 Toast.LENGTH_SHORT).show();
458
459 onBackPressed();
460 return true;
461
462 default:
463 return super.onOptionsItemSelected(item);
464 }
465 }
466 }
com.sudogeeks.talking_reminder.AlarmReceiver.setAlarm
void setAlarm(Context context, Calendar calendar, int ID)
Definition: AlarmReceiver.java:73
com.sudogeeks.talking_reminder.ReminderEditActivity.onSwitchRepeat
void onSwitchRepeat(View view)
Definition: ReminderEditActivity.java:276
com.sudogeeks.talking_reminder.ReminderDatabase.getReminder
ReminderDO getReminder(int id)
Definition: ReminderDatabase.java:106
com.sudogeeks.talking_reminder.ReminderDatabase.updateReminder
int updateReminder(ReminderDO reminderDO)
Definition: ReminderDatabase.java:187
com.sudogeeks.talking_reminder.ReminderEditActivity.setRepeatNo
void setRepeatNo(View v)
Definition: ReminderEditActivity.java:322
com.sudogeeks.talking_reminder.ReminderEditActivity.setTime
void setTime(View v)
Definition: ReminderEditActivity.java:226
com.sudogeeks.talking_reminder.ReminderEditActivity.onBackPressed
void onBackPressed()
Definition: ReminderEditActivity.java:412
com.sudogeeks.talking_reminder.ReminderEditActivity.onCreateOptionsMenu
boolean onCreateOptionsMenu(Menu menu)
Definition: ReminderEditActivity.java:421
com.sudogeeks.talking_reminder.ReminderDO
Data Object class for Reminder This class conatins all the attributes of reminder.
Definition: ReminderDO.java:15
com.sudogeeks.talking_reminder.ReminderEditActivity.setDate
void setDate(View v)
Definition: ReminderEditActivity.java:252
com.sudogeeks.talking_reminder.ReminderEditActivity.updateReminder
void updateReminder()
Definition: ReminderEditActivity.java:357
com.sudogeeks.talking_reminder.ReminderDatabase
Class to manage database This class is for managing the database.
Definition: ReminderDatabase.java:15
com.sudogeeks.talking_reminder.AlarmReceiver
A class implementing basic alarm functions This class includes very basic features for an alarm.
Definition: AlarmReceiver.java:21
com.sudogeeks.talking_reminder.ReminderEditActivity.selectRepeatType
void selectRepeatType(View v)
Definition: ReminderEditActivity.java:292
com.sudogeeks.talking_reminder.AlarmReceiver.cancelAlarm
void cancelAlarm(Context context, int ID)
Definition: AlarmReceiver.java:139
com.sudogeeks.talking_reminder.ReminderEditActivity
Java class for handling UI component of Edit reminder layout.
Definition: ReminderEditActivity.java:32
com.sudogeeks.talking_reminder.AlarmReceiver.setRepeatAlarm
void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime)
Definition: AlarmReceiver.java:107
com.sudogeeks.talking_reminder.ReminderEditActivity.onOptionsItemSelected
boolean onOptionsItemSelected(MenuItem item)
Definition: ReminderEditActivity.java:431
com.sudogeeks.talking_reminder.ReminderEditActivity.onCreate
void onCreate(Bundle savedInstanceState)
Definition: ReminderEditActivity.java:78