Fix departure alarms

This commit is contained in:
Doug Keen 2016-10-30 21:19:52 -07:00
parent d77d80e39e
commit 6dc026fa67
2 changed files with 41 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package com.dougkeen.bart.activities;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
@ -101,12 +102,16 @@ public class TrainAlarmDialogFragment extends DialogFragment {
Context.MODE_PRIVATE).edit();
editor.putInt(KEY_LAST_ALARM_LEAD_TIME,
alarmLeadTime);
editor.commit();
editor.apply();
((BartRunnerApplication) getActivity()
.getApplication())
.getBoardedDeparture().setUpAlarm(
alarmLeadTime);
BartRunnerApplication application =
(BartRunnerApplication) getActivity().getApplication();
application.getBoardedDeparture().setUpAlarm(
alarmLeadTime,
application,
(AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE)
);
}
})
.setNegativeButton(R.string.cancel,

View File

@ -17,6 +17,7 @@ import android.util.Log;
import com.dougkeen.bart.BartRunnerApplication;
import com.dougkeen.bart.R;
import com.dougkeen.bart.activities.ViewDeparturesActivity;
import com.dougkeen.bart.receivers.AlarmBroadcastReceiver;
import com.dougkeen.bart.services.BoardedDepartureService;
import com.dougkeen.util.Observable;
@ -58,7 +59,9 @@ public class Departure implements Parcelable, Comparable<Departure> {
private Station passengerDestination;
private Line line;
private String destinationColor;
private @ColorInt int destinationColorInt;
private
@ColorInt
int destinationColorInt;
private String platform;
private String direction;
private boolean bikeAllowed;
@ -566,11 +569,9 @@ public class Departure implements Parcelable, Comparable<Departure> {
}
private PendingIntent getAlarmIntent(Context context) {
Intent intent = new Intent(context, ViewDeparturesActivity.class);
intent.putExtra(Constants.STATION_PAIR_EXTRA, getStationPair());
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.setAction(Constants.ACTION_ALARM);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private long getAlarmClockTime() {
@ -581,39 +582,44 @@ public class Departure implements Parcelable, Comparable<Departure> {
return getMeanSecondsLeft() - getAlarmLeadTimeMinutes() * 60;
}
public void setUpAlarm(int leadTimeMinutes) {
public void setUpAlarm(int leadTimeMinutes, Context context, AlarmManager alarmManager) {
this.alarmLeadTimeMinutes.setValue(leadTimeMinutes);
this.alarmPending.setValue(true);
scheduleAlarm(alarmManager, getAlarmIntent(context));
}
public void updateAlarm(Context context, AlarmManager alarmManager) {
if (alarmManager == null)
if (alarmManager == null) {
Log.w(Constants.TAG, "No alarm manager available, so alarm will not be updated");
return;
}
if (isAlarmPending() && getAlarmLeadTimeMinutes() > 0) {
final PendingIntent alarmIntent = getAlarmIntent(context);
alarmManager.cancel(alarmIntent);
scheduleAlarm(alarmManager, getAlarmIntent(context));
}
}
private void scheduleAlarm(AlarmManager alarmManager, PendingIntent alarmIntent) {
long alarmTime = getAlarmClockTime();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (alarmTime < System.currentTimeMillis()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
Log.e(Constants.TAG, "Could not find a suitable method for scheduling an alarm");
}
if (Log.isLoggable(Constants.TAG, Log.VERBOSE))
Log.v(Constants.TAG,
"Scheduling alarm for "
+ DateFormatUtils.format(alarmTime, "h:mm:ss"));
}
Log.v(Constants.TAG, "Scheduling alarm for " + DateFormatUtils.format(alarmTime, "h:mm:ss"));
}
public void cancelAlarm(Context context, AlarmManager alarmManager) {
alarmManager.cancel(getAlarmIntent(context));
this.alarmPending.setValue(false);
Log.d(Constants.TAG, "Alarm cancelled");
}
private PendingIntent notificationIntent;