Testing multiple alarms
This commit is contained in:
parent
666b3a2a6e
commit
a8e2a88140
@ -23,7 +23,7 @@ public abstract class Alarm {
|
||||
public static final int NUM_DAYS = 7;
|
||||
|
||||
// =========== MUTABLE ===========
|
||||
private long snoozingUntilMillis;
|
||||
private long snoozingUntilMillis; // TODO: Not necessary? Can just schedule another alarm w/ AlarmManager.
|
||||
private boolean enabled;
|
||||
// ===============================
|
||||
public abstract long id(); // TODO: Counter in the repository. Set this field as the repo creates instances.
|
||||
|
||||
@ -68,9 +68,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
|
||||
PendingIntent pi = alarmIntent();
|
||||
PendingIntent pi = alarmIntent(true);
|
||||
am.cancel(pi);
|
||||
pi.cancel();
|
||||
Intent intent = new Intent(MainActivity.this, UpcomingAlarmReceiver.class)
|
||||
.setAction(UpcomingAlarmReceiver.ACTION_CANCEL_NOTIFICATION);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
}).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)
|
||||
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), notifyUpcomingAlarmIntent());
|
||||
// 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
|
||||
Intent intent = new Intent(this, RingtoneActivity.class)
|
||||
.setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
|
||||
@ -200,12 +205,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
// TODO: Use unique request codes per alarm.
|
||||
// If a PendingIntent with this request code already exists, then we are likely modifying
|
||||
// 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() {
|
||||
Intent intent = new Intent(this, UpcomingAlarmReceiver.class);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,27 +13,31 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
||||
public static final String ACTION_SHOW_SNOOZING
|
||||
= "com.philliphsu.clock2.action.CANCEL_NOTIFICATION";
|
||||
|
||||
private static int count = -1;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) {
|
||||
nm.cancel("tag", 0);
|
||||
nm.cancel(count);
|
||||
} else if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentTitle("Snoozing")
|
||||
.setContentText("New ring time here")
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify("tag", 0, note);
|
||||
nm.notify(count, note);
|
||||
} else {
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentTitle("Upcoming alarm")
|
||||
.setContentText("Ring time here")
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify("tag", 0, note);
|
||||
nm.notify(++count, note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,28 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
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">
|
||||
|
||||
<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:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
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:id="@+id/tabs"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
|
||||
<android.support.v4.view.ViewPager
|
||||
android:id="@+id/container"
|
||||
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:id="@+id/fab"
|
||||
@ -32,4 +45,4 @@
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
android:src="@android:drawable/ic_dialog_email"/>
|
||||
|
||||
</merge>
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user