ReminderEditActivity.java
package com.sudogeeks.talking_reminder;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.Calendar;
public class ReminderEditActivity extends AppCompatActivity {
// Constant Intent String
public static final String EXTRA_REMINDER_ID = "Reminder_ID";
// Values for orientation change
private static final String KEY_TITLE = "title_key";
private static final String KEY_TIME = "time_key";
private static final String KEY_DATE = "date_key";
private static final String KEY_REPEAT = "repeat_key";
private static final String KEY_REPEAT_NO = "repeat_no_key";
private static final String KEY_REPEAT_TYPE = "repeat_type_key";
private static final String KEY_ACTIVE = "active_key";
// Constant values in milliseconds
private static final long milMinute = 60000L;
private static final long milHour = 3600000L;
private static final long milDay = 86400000L;
private static final long milWeek = 604800000L;
private static final long milMonth = 2592000000L;
private Toolbar mToolbar;
private EditText mTitleText;
private TextView mDateText, mTimeText, mRepeatText, mRepeatNoText, mRepeatTypeText;
private FloatingActionButton mFAB1;
private FloatingActionButton mFAB2;
private Switch mRepeatSwitch;
private String mTitle;
private String mTime;
private String mDate;
private String mRepeatNo;
private String mRepeatType;
private String mActive;
private String mRepeat;
private String[] mDateSplit;
private String[] mTimeSplit;
private int mReceivedID;
private int mYear, mMonth, mHour, mMinute, mDay;
private long mRepeatTime;
private Calendar mCalendar;
private ReminderDO mReceivedReminderDO;
private ReminderDatabase rb;
private AlarmReceiver mAlarmReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
// Initialize Views
mToolbar = findViewById(R.id.toolbar);
mTitleText = findViewById(R.id.reminder_title);
mDateText = findViewById(R.id.set_date);
mTimeText = findViewById(R.id.set_time);
mRepeatText = findViewById(R.id.set_repeat);
mRepeatNoText = findViewById(R.id.set_repeat_no);
mRepeatTypeText = findViewById(R.id.set_repeat_type);
mRepeatSwitch = findViewById(R.id.repeat_switch);
// Setup Toolbar
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(R.string.activity_edit_reminder_label);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// Setup ReminderDO Title EditText
mTitleText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mTitle = s.toString().trim();
mTitleText.setError(null);
}
@Override
public void afterTextChanged(Editable s) {
}
});
// Get reminder id from intent
mReceivedID = Integer.parseInt(getIntent().getStringExtra(EXTRA_REMINDER_ID));
// Get reminder using reminder id
rb = new ReminderDatabase(this);
/*Intent intent = getIntent();
if(intent.getType().equals("text/"+Utility.FILE_EXTENSION)){
mReceivedReminderDO.setTitle(intent.getStringExtra("mTitle"));
System.out.println("**********************************");
System.out.println(mReceivedReminderDO.getTitle());
System.out.println("**********************************");
}
else {*/
// Get reminder id from intent
mReceivedReminderDO = rb.getReminder(mReceivedID);
//}
// Get values from reminder
mTitle = mReceivedReminderDO.getTitle();
mDate = mReceivedReminderDO.getDate();
mTime = mReceivedReminderDO.getTime();
mRepeat = mReceivedReminderDO.getRepeat();
mRepeatNo = mReceivedReminderDO.getRepeatNo();
mRepeatType = mReceivedReminderDO.getRepeatType();
mActive = mReceivedReminderDO.getActive();
// Setup TextViews using reminder values
mTitleText.setText(mTitle);
mDateText.setText(mDate);
mTimeText.setText(mTime);
mRepeatNoText.setText(mRepeatNo);
mRepeatTypeText.setText(mRepeatType);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
// To save state on device rotation
if (savedInstanceState != null) {
String savedTitle = savedInstanceState.getString(KEY_TITLE);
mTitleText.setText(savedTitle);
mTitle = savedTitle;
String savedTime = savedInstanceState.getString(KEY_TIME);
mTimeText.setText(savedTime);
mTime = savedTime;
String savedDate = savedInstanceState.getString(KEY_DATE);
mDateText.setText(savedDate);
mDate = savedDate;
String saveRepeat = savedInstanceState.getString(KEY_REPEAT);
mRepeatText.setText(saveRepeat);
mRepeat = saveRepeat;
String savedRepeatNo = savedInstanceState.getString(KEY_REPEAT_NO);
mRepeatNoText.setText(savedRepeatNo);
mRepeatNo = savedRepeatNo;
String savedRepeatType = savedInstanceState.getString(KEY_REPEAT_TYPE);
mRepeatTypeText.setText(savedRepeatType);
mRepeatType = savedRepeatType;
mActive = savedInstanceState.getString(KEY_ACTIVE);
}
if (mRepeat.equals("false")) {
mRepeatSwitch.setChecked(false);
mRepeatText.setText(R.string.repeat_off);
} else if (mRepeat.equals("true")) {
mRepeatSwitch.setChecked(true);
}
// Obtain Date and Time details
mCalendar = Calendar.getInstance();
mAlarmReceiver = new AlarmReceiver();
mDateSplit = mDate.split("/");
mTimeSplit = mTime.split(":");
mDay = Integer.parseInt(mDateSplit[0]);
mMonth = Integer.parseInt(mDateSplit[1]);
mYear = Integer.parseInt(mDateSplit[2]);
mHour = Integer.parseInt(mTimeSplit[0]);
mMinute = Integer.parseInt(mTimeSplit[1]);
}
// To save state on device rotation
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence(KEY_TITLE, mTitleText.getText());
outState.putCharSequence(KEY_TIME, mTimeText.getText());
outState.putCharSequence(KEY_DATE, mDateText.getText());
outState.putCharSequence(KEY_REPEAT, mRepeatText.getText());
outState.putCharSequence(KEY_REPEAT_NO, mRepeatNoText.getText());
outState.putCharSequence(KEY_REPEAT_TYPE, mRepeatTypeText.getText());
outState.putCharSequence(KEY_ACTIVE, mActive);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
public void setTime(View v) {
Calendar now = Calendar.getInstance();
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
mHour = hourOfDay;
mMinute = minute;
if (minute < 10) {
mTime = hourOfDay + ":" + "0" + minute;
} else {
mTime = hourOfDay + ":" + minute;
}
mTimeText.setText(mTime);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
public void setDate(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mDay = dayOfMonth;
mMonth = monthOfYear + 1;
mYear = year;
mDate = mDay + "/" + mMonth + "/" + mYear;
mDateText.setText(mDate);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
public void onSwitchRepeat(View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
mRepeat = "true";
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
} else {
mRepeat = "false";
mRepeatText.setText(R.string.repeat_off);
}
}
public void selectRepeatType(View v) {
final String[] items = new String[5];
items[0] = "Minute";
items[1] = "Hour";
items[2] = "Day";
items[3] = "Week";
items[4] = "Month";
// Create List Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Type");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
mRepeatType = items[item];
mRepeatTypeText.setText(mRepeatType);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void setRepeatNo(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Number");
// Create EditText box to input repeat number
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (input.getText().toString().length() == 0) {
mRepeatNo = Integer.toString(1);
mRepeatNoText.setText(mRepeatNo);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
} else {
mRepeatNo = input.getText().toString().trim();
mRepeatNoText.setText(mRepeatNo);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing
}
});
alert.show();
}
// On clicking the update button
public void updateReminder() {
// Set new values in the reminder
mReceivedReminderDO.setTitle(mTitle);
mReceivedReminderDO.setDate(mDate);
mReceivedReminderDO.setTime(mTime);
mReceivedReminderDO.setRepeat(mRepeat);
mReceivedReminderDO.setRepeatNo(mRepeatNo);
mReceivedReminderDO.setRepeatType(mRepeatType);
mReceivedReminderDO.setActive(mActive);
// Update reminder
rb.updateReminder(mReceivedReminderDO);
// Set up calender for creating the notification
mCalendar.set(Calendar.MONTH, --mMonth);
mCalendar.set(Calendar.YEAR, mYear);
mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mCalendar.set(Calendar.MINUTE, mMinute);
mCalendar.set(Calendar.SECOND, 0);
// Cancel existing notification of the reminder by using its ID
mAlarmReceiver.cancelAlarm(getApplicationContext(), mReceivedID);
// Check repeat type
if (mRepeatType.equals("Minute")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMinute;
} else if (mRepeatType.equals("Hour")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milHour;
} else if (mRepeatType.equals("Day")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Week")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
} else if (mRepeatType.equals("Month")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}
// Create a new notification
if (mActive.equals("true")) {
if (mRepeat.equals("true")) {
mAlarmReceiver.setRepeatAlarm(getApplicationContext(), mCalendar, mReceivedID, mRepeatTime);
} else if (mRepeat.equals("false")) {
mAlarmReceiver.setAlarm(getApplicationContext(), mCalendar, mReceivedID);
}
}
// Create toast to confirm update
Toast.makeText(getApplicationContext(), "Edited",
Toast.LENGTH_SHORT).show();
onBackPressed();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_reminder, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// On clicking the back arrow
// Discard any changes
case android.R.id.home:
onBackPressed();
return true;
// On clicking save reminder button
// Update reminder
case R.id.save_reminder:
mTitleText.setText(mTitle);
if (mTitleText.getText().toString().length() == 0)
mTitleText.setError("ReminderDO Title cannot be blank!");
else {
updateReminder();
}
return true;
// On clicking discard reminder button
// Discard any changes
case R.id.discard_reminder:
Toast.makeText(getApplicationContext(), "Changes Discarded",
Toast.LENGTH_SHORT).show();
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
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