Testing multiple alarms

This commit is contained in:
Phillip Hsu 2016-05-29 13:56:05 -07:00
parent 666b3a2a6e
commit a8e2a88140
4 changed files with 47 additions and 21 deletions

View File

@ -23,7 +23,7 @@ public abstract class Alarm {
public static final int NUM_DAYS = 7; public static final int NUM_DAYS = 7;
// =========== MUTABLE =========== // =========== MUTABLE ===========
private long snoozingUntilMillis; private long snoozingUntilMillis; // TODO: Not necessary? Can just schedule another alarm w/ AlarmManager.
private boolean enabled; private boolean enabled;
// =============================== // ===============================
public abstract long id(); // TODO: Counter in the repository. Set this field as the repo creates instances. public abstract long id(); // TODO: Counter in the repository. Set this field as the repo creates instances.

View File

@ -68,9 +68,12 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pi = alarmIntent(); PendingIntent pi = alarmIntent(true);
am.cancel(pi); am.cancel(pi);
pi.cancel(); pi.cancel();
Intent intent = new Intent(MainActivity.this, UpcomingAlarmReceiver.class)
.setAction(UpcomingAlarmReceiver.ACTION_CANCEL_NOTIFICATION);
sendBroadcast(intent);
} }
}).show(); }).show();
} }
@ -188,10 +191,12 @@ public class MainActivity extends AppCompatActivity {
// todo: use alarm's ring time - (number of hours to be notified in advance, converted to millis) // todo: use alarm's ring time - (number of hours to be notified in advance, converted to millis)
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), notifyUpcomingAlarmIntent()); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), notifyUpcomingAlarmIntent());
// todo: get alarm's ring time // todo: get alarm's ring time
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, alarmIntent()); am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, alarmIntent(false));
} }
private PendingIntent alarmIntent() { private static int alarmCount;
private PendingIntent alarmIntent(boolean retrievePrevious) {
// TODO: Use appropriate subclass instead // TODO: Use appropriate subclass instead
Intent intent = new Intent(this, RingtoneActivity.class) Intent intent = new Intent(this, RingtoneActivity.class)
.setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)); .setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
@ -200,12 +205,16 @@ public class MainActivity extends AppCompatActivity {
// TODO: Use unique request codes per alarm. // TODO: Use unique request codes per alarm.
// If a PendingIntent with this request code already exists, then we are likely modifying // If a PendingIntent with this request code already exists, then we are likely modifying
// an alarm, so we should cancel the existing intent. // an alarm, so we should cancel the existing intent.
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); int requestCode = retrievePrevious ? alarmCount - 1 : alarmCount++;
int flag = retrievePrevious
? PendingIntent.FLAG_NO_CREATE
: PendingIntent.FLAG_CANCEL_CURRENT;
return PendingIntent.getActivity(this, requestCode, intent, flag);
} }
private PendingIntent notifyUpcomingAlarmIntent() { private PendingIntent notifyUpcomingAlarmIntent() {
Intent intent = new Intent(this, UpcomingAlarmReceiver.class); Intent intent = new Intent(this, UpcomingAlarmReceiver.class);
// TODO: Use unique request codes per alarm. // TODO: Use unique request codes per alarm.
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); return PendingIntent.getBroadcast(this, alarmCount, intent, PendingIntent.FLAG_CANCEL_CURRENT);
} }
} }

View File

@ -13,27 +13,31 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
public static final String ACTION_SHOW_SNOOZING public static final String ACTION_SHOW_SNOOZING
= "com.philliphsu.clock2.action.CANCEL_NOTIFICATION"; = "com.philliphsu.clock2.action.CANCEL_NOTIFICATION";
private static int count = -1;
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) { if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) {
nm.cancel("tag", 0); nm.cancel(count);
} else if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) { } else if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
Notification note = new NotificationCompat.Builder(context) Notification note = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Snoozing") .setContentTitle("Snoozing")
.setContentText("New ring time here") .setContentText("New ring time here")
.setOngoing(true)
.build(); .build();
// todo actions // todo actions
nm.notify("tag", 0, note); nm.notify(count, note);
} else { } else {
Notification note = new NotificationCompat.Builder(context) Notification note = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Upcoming alarm") .setContentTitle("Upcoming alarm")
.setContentText("Ring time here") .setContentText("Ring time here")
.setOngoing(true)
.build(); .build();
// todo actions // todo actions
nm.notify("tag", 0, note); nm.notify(++count, note);
} }
} }
} }

View File

@ -1,28 +1,41 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<merge <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.philliphsu.clock2.MainActivity"> tools:context="com.philliphsu.clock2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"> app:popupTheme="@style/AppTheme.PopupOverlay"
app:contentInsetStart="0dp"
app:layout_scrollFlags=""> <!-- None intentionally -->
<android.support.design.widget.TabLayout <android.support.design.widget.TabLayout
android:id="@+id/tabs" android:id="@+id/tabs"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="match_parent"/>
</android.support.v7.widget.Toolbar> </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager <android.support.v4.view.ViewPager
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"
@ -32,4 +45,4 @@
android:layout_margin="@dimen/fab_margin" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"/> android:src="@android:drawable/ic_dialog_email"/>
</merge> </android.support.design.widget.CoordinatorLayout>