Skip to content

Commit

Permalink
Added reminder title to notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
blanyal committed May 2, 2015
1 parent 7df489f commit ec7f173
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
49 changes: 31 additions & 18 deletions app/src/main/java/com/blanyal/remindme/AlarmReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,41 @@ public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));

PendingIntent remind = PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderReceiver.class)
, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("INTENT ID: ", Integer.toString(mReceivedID));

// Get notification title from Reminder Database
ReminderDatabase rb = new ReminderDatabase(context);
Reminder reminder = rb.getReminder(mReceivedID);
String mTitle = reminder.getTitle();

// Create Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setSmallIcon( R.drawable.ic_launcher)
.setTicker("Reminder")
.setTicker(mTitle)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText("This is a reminder")
.setContentText(mTitle)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOnlyAlertOnce(true);

int mNotificationId = 1;

NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(mNotificationId, mBuilder.build());
nManager.notify(mReceivedID, mBuilder.build());

Log.d("NOTIFICATION:", "SUCCESS!!!");

}

public void setAlarm(Context context, Calendar calendar, int ID)
{
public void setAlarm(Context context, Calendar calendar, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);

// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

// Calculate notification time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
Expand All @@ -77,7 +85,7 @@ public void setAlarm(Context context, Calendar calendar, int ID)
Log.d("TIME3:", Long.toString(diffTime));
Log.d("ID:", Integer.toString(ID));

// Set notification time
// Start alarm using notification time
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + diffTime,
mPendingIntent);
Expand All @@ -90,11 +98,15 @@ public void setAlarm(Context context, Calendar calendar, int ID)
PackageManager.DONT_KILL_APP);
}

public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime)
{
public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);

// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

// Calculate notification time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
Expand All @@ -105,7 +117,7 @@ public void setRepeatAlarm(Context context, Calendar calendar, int ID, long Repe
Log.d("REPEAT_TIME: ", Long.toString(RepeatTime));
Log.d("ID: ", Integer.toString(ID));

// Set initial notification time and repeat interval time
// Start alarm using initial notification time and repeat interval time
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + diffTime,
RepeatTime , mPendingIntent);
Expand All @@ -118,16 +130,17 @@ public void setRepeatAlarm(Context context, Calendar calendar, int ID, long Repe
PackageManager.DONT_KILL_APP);
}

public void cancelAlarm(Context context, int ID)
{
public void cancelAlarm(Context context, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

// Cancel Alarm using Reminder ID
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
mAlarmManager.cancel(mPendingIntent);

Log.d("ID: ", Integer.toString(ID));
Log.d("CANCEL ALARM: ", "Alarm Cancelled");

// Disable BootReceiver so that alarm won't start again if device is rebooted
// Disable alarm if device is rebooted
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
Expand Down
46 changes: 45 additions & 1 deletion app/src/main/java/com/blanyal/remindme/ReminderEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public class ReminderEditActivity extends AppCompatActivity implements
private ReminderDatabase rb;
private AlarmReceiver mAlarmReceiver;

// 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";
Expand All @@ -79,6 +82,7 @@ public class ReminderEditActivity extends AppCompatActivity implements
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;
Expand All @@ -91,6 +95,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);

// Initialize Views
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mTitleText = (EditText) findViewById(R.id.reminder_title);
mDateText = (TextView) findViewById(R.id.set_date);
Expand All @@ -102,11 +107,13 @@ protected void onCreate(Bundle savedInstanceState) {
mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
mRepeatSwitch = (Switch) findViewById(R.id.repeat_switch);

// Setup Toolbar
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(R.string.title_activity_add_reminder);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

// Setup Reminder Title EditText
mTitleText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
Expand All @@ -121,11 +128,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
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);
mReceivedReminder = rb.getReminder(mReceivedID);

// Get values from reminder
mTitle = mReceivedReminder.getTitle();
mDate = mReceivedReminder.getDate();
mTime = mReceivedReminder.getTime();
Expand All @@ -134,13 +144,15 @@ public void afterTextChanged(Editable s) {}
mRepeatType = mReceivedReminder.getRepeatType();
mActive = mReceivedReminder.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);
Expand Down Expand Up @@ -169,6 +181,7 @@ public void afterTextChanged(Editable s) {}
mActive = savedInstanceState.getString(KEY_ACTIVE);
}

// Setup up active buttons
if (mActive.equals("false")) {
mFAB1.setVisibility(View.VISIBLE);
mFAB2.setVisibility(View.GONE);
Expand All @@ -178,6 +191,7 @@ public void afterTextChanged(Editable s) {}
mFAB2.setVisibility(View.VISIBLE);
}

// Setup repeat switch
if (mRepeat.equals("false")) {
mRepeatSwitch.setChecked(false);
mRepeatText.setText(R.string.repeat_off);
Expand All @@ -186,6 +200,7 @@ public void afterTextChanged(Editable s) {}
mRepeatSwitch.setChecked(true);
}

// Obtain Date and Time details
mCalendar = Calendar.getInstance();
mAlarmReceiver = new AlarmReceiver();

Expand All @@ -202,6 +217,7 @@ public void afterTextChanged(Editable s) {}
mMinute = Integer.parseInt(mTimeSplit[1]);
}

// To save state on device rotation
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
Expand All @@ -219,6 +235,7 @@ protected void onSaveInstanceState (Bundle outState) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}

// On clicking Time picker
public void setTime(View v){
Calendar now = Calendar.getInstance();
TimePickerDialog tpd = TimePickerDialog.newInstance(
Expand All @@ -231,6 +248,7 @@ public void setTime(View v){
tpd.show(getFragmentManager(), "Timepickerdialog");
}

// On clicking Date picker
public void setDate(View v){
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
Expand All @@ -242,6 +260,7 @@ public void setDate(View v){
dpd.show(getFragmentManager(), "Datepickerdialog");
}

// Obtain time from time picker
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
mHour = hourOfDay;
Expand All @@ -250,6 +269,7 @@ public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
mTimeText.setText(mTime);
}

// Obtain date from date picker
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
monthOfYear ++;
Expand All @@ -260,6 +280,7 @@ public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayO
mDateText.setText(mDate);
}

// On clicking the active button
public void selectFab1(View v) {
mFAB1 = (FloatingActionButton) findViewById(R.id.starred1);
mFAB1.setVisibility(View.GONE);
Expand All @@ -268,6 +289,7 @@ public void selectFab1(View v) {
mActive = "true";
}

// On clicking the inactive button
public void selectFab2(View v) {
mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
mFAB2.setVisibility(View.GONE);
Expand All @@ -276,6 +298,7 @@ public void selectFab2(View v) {
mActive = "false";
}

// On clicking the repeat switch
public void onSwitchRepeat(View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
Expand All @@ -288,6 +311,7 @@ public void onSwitchRepeat(View view) {
}
}

// On clicking repeat type button
public void selectRepeatType(View v){
final String[] items = new String[5];

Expand All @@ -297,6 +321,7 @@ public void selectRepeatType(View v){
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() {
Expand All @@ -312,10 +337,12 @@ public void onClick(DialogInterface dialog, int item) {
alert.show();
}

// On clicking repeat no button
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);
Expand All @@ -330,13 +357,15 @@ public void onClick(DialogInterface dialog, int whichButton) {
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
// Do nothing
}
});
alert.show();
}

// On clicking the update button
public void updateReminder(){
// Set new values in the reminder
mReceivedReminder.setTitle(mTitle);
mReceivedReminder.setDate(mDate);
mReceivedReminder.setTime(mTime);
Expand All @@ -345,17 +374,21 @@ public void updateReminder(){
mReceivedReminder.setRepeatType(mRepeatType);
mReceivedReminder.setActive(mActive);

// Update reminder
rb.updateReminder(mReceivedReminder);

// 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")) {
Expand All @@ -368,36 +401,45 @@ public void updateReminder(){
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}

// Create a new notification
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();
}

// On pressing the back button
@Override
public void onBackPressed() {
super.onBackPressed();
}

// Creating the menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_reminder, menu);
return true;
}

// On clicking menu buttons
@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);

Expand All @@ -409,6 +451,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return true;

// On clicking discard reminder button
// Discard any changes
case R.id.discard_reminder:
Toast.makeText(getApplicationContext(), "Changes Discarded",
Toast.LENGTH_SHORT).show();
Expand Down

0 comments on commit ec7f173

Please sign in to comment.