Fix departure alarms
This commit is contained in:
parent
d77d80e39e
commit
6dc026fa67
@ -1,6 +1,7 @@
|
|||||||
package com.dougkeen.bart.activities;
|
package com.dougkeen.bart.activities;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
|
import android.app.AlarmManager;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
@ -101,12 +102,16 @@ public class TrainAlarmDialogFragment extends DialogFragment {
|
|||||||
Context.MODE_PRIVATE).edit();
|
Context.MODE_PRIVATE).edit();
|
||||||
editor.putInt(KEY_LAST_ALARM_LEAD_TIME,
|
editor.putInt(KEY_LAST_ALARM_LEAD_TIME,
|
||||||
alarmLeadTime);
|
alarmLeadTime);
|
||||||
editor.commit();
|
editor.apply();
|
||||||
|
|
||||||
((BartRunnerApplication) getActivity()
|
BartRunnerApplication application =
|
||||||
.getApplication())
|
(BartRunnerApplication) getActivity().getApplication();
|
||||||
.getBoardedDeparture().setUpAlarm(
|
application.getBoardedDeparture().setUpAlarm(
|
||||||
alarmLeadTime);
|
alarmLeadTime,
|
||||||
|
application,
|
||||||
|
(AlarmManager) getActivity()
|
||||||
|
.getSystemService(Context.ALARM_SERVICE)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.setNegativeButton(R.string.cancel,
|
.setNegativeButton(R.string.cancel,
|
||||||
|
@ -17,6 +17,7 @@ import android.util.Log;
|
|||||||
import com.dougkeen.bart.BartRunnerApplication;
|
import com.dougkeen.bart.BartRunnerApplication;
|
||||||
import com.dougkeen.bart.R;
|
import com.dougkeen.bart.R;
|
||||||
import com.dougkeen.bart.activities.ViewDeparturesActivity;
|
import com.dougkeen.bart.activities.ViewDeparturesActivity;
|
||||||
|
import com.dougkeen.bart.receivers.AlarmBroadcastReceiver;
|
||||||
import com.dougkeen.bart.services.BoardedDepartureService;
|
import com.dougkeen.bart.services.BoardedDepartureService;
|
||||||
import com.dougkeen.util.Observable;
|
import com.dougkeen.util.Observable;
|
||||||
|
|
||||||
@ -58,7 +59,9 @@ public class Departure implements Parcelable, Comparable<Departure> {
|
|||||||
private Station passengerDestination;
|
private Station passengerDestination;
|
||||||
private Line line;
|
private Line line;
|
||||||
private String destinationColor;
|
private String destinationColor;
|
||||||
private @ColorInt int destinationColorInt;
|
private
|
||||||
|
@ColorInt
|
||||||
|
int destinationColorInt;
|
||||||
private String platform;
|
private String platform;
|
||||||
private String direction;
|
private String direction;
|
||||||
private boolean bikeAllowed;
|
private boolean bikeAllowed;
|
||||||
@ -566,11 +569,9 @@ public class Departure implements Parcelable, Comparable<Departure> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PendingIntent getAlarmIntent(Context context) {
|
private PendingIntent getAlarmIntent(Context context) {
|
||||||
Intent intent = new Intent(context, ViewDeparturesActivity.class);
|
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
|
||||||
intent.putExtra(Constants.STATION_PAIR_EXTRA, getStationPair());
|
|
||||||
intent.setAction(Constants.ACTION_ALARM);
|
intent.setAction(Constants.ACTION_ALARM);
|
||||||
return PendingIntent.getBroadcast(context, 0, intent,
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getAlarmClockTime() {
|
private long getAlarmClockTime() {
|
||||||
@ -581,39 +582,44 @@ public class Departure implements Parcelable, Comparable<Departure> {
|
|||||||
return getMeanSecondsLeft() - getAlarmLeadTimeMinutes() * 60;
|
return getMeanSecondsLeft() - getAlarmLeadTimeMinutes() * 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUpAlarm(int leadTimeMinutes) {
|
public void setUpAlarm(int leadTimeMinutes, Context context, AlarmManager alarmManager) {
|
||||||
this.alarmLeadTimeMinutes.setValue(leadTimeMinutes);
|
this.alarmLeadTimeMinutes.setValue(leadTimeMinutes);
|
||||||
this.alarmPending.setValue(true);
|
this.alarmPending.setValue(true);
|
||||||
|
scheduleAlarm(alarmManager, getAlarmIntent(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateAlarm(Context context, AlarmManager alarmManager) {
|
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;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isAlarmPending() && getAlarmLeadTimeMinutes() > 0) {
|
if (isAlarmPending() && getAlarmLeadTimeMinutes() > 0) {
|
||||||
final PendingIntent alarmIntent = getAlarmIntent(context);
|
scheduleAlarm(alarmManager, getAlarmIntent(context));
|
||||||
alarmManager.cancel(alarmIntent);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleAlarm(AlarmManager alarmManager, PendingIntent alarmIntent) {
|
||||||
long alarmTime = getAlarmClockTime();
|
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);
|
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
|
||||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
|
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
|
||||||
} else {
|
} 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) {
|
public void cancelAlarm(Context context, AlarmManager alarmManager) {
|
||||||
alarmManager.cancel(getAlarmIntent(context));
|
alarmManager.cancel(getAlarmIntent(context));
|
||||||
this.alarmPending.setValue(false);
|
this.alarmPending.setValue(false);
|
||||||
|
Log.d(Constants.TAG, "Alarm cancelled");
|
||||||
}
|
}
|
||||||
|
|
||||||
private PendingIntent notificationIntent;
|
private PendingIntent notificationIntent;
|
||||||
|
Loading…
Reference in New Issue
Block a user