Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/android_alarm_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.2

* Added support for setting alarms which work when the phone is in doze mode.

## 0.4.1+8

* Remove dependency on google-services in the Android example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public void notImplemented() {
private static void scheduleAlarm(
Context context,
int requestCode,
boolean alarmClock,
boolean allowWhileIdle,
boolean repeating,
boolean exact,
boolean wakeup,
Expand All @@ -228,6 +230,8 @@ private static void scheduleAlarm(
addPersistentAlarm(
context,
requestCode,
alarmClock,
allowWhileIdle,
repeating,
exact,
wakeup,
Expand All @@ -250,17 +254,31 @@ private static void scheduleAlarm(

// Schedule the alarm.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

if (alarmClock) {
AlarmManagerCompat.setAlarmClock(manager, startMillis, pendingIntent, pendingIntent);
return;
}

if (exact) {
if (repeating) {
manager.setRepeating(clock, startMillis, intervalMillis, pendingIntent);
} else {
AlarmManagerCompat.setExact(manager, clock, startMillis, pendingIntent);
if (allowWhileIdle) {
AlarmManagerCompat.setExactAndAllowWhileIdle(manager, clock, startMillis, pendingIntent);
} else {
AlarmManagerCompat.setExact(manager, clock, startMillis, pendingIntent);
}
}
} else {
if (repeating) {
manager.setInexactRepeating(clock, startMillis, intervalMillis, pendingIntent);
} else {
manager.set(clock, startMillis, pendingIntent);
if (allowWhileIdle) {
AlarmManagerCompat.setAndAllowWhileIdle(manager, clock, startMillis, pendingIntent);
} else {
manager.set(clock, startMillis, pendingIntent);
}
}
}
}
Expand All @@ -270,6 +288,8 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
scheduleAlarm(
context,
request.requestCode,
request.alarmClock,
request.allowWhileIdle,
repeating,
request.exact,
request.wakeup,
Expand All @@ -282,9 +302,13 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
public static void setPeriodic(
Context context, AndroidAlarmManagerPlugin.PeriodicRequest request) {
final boolean repeating = true;
final boolean allowWhileIdle = false;
final boolean alarmClock = false;
scheduleAlarm(
context,
request.requestCode,
alarmClock,
allowWhileIdle,
repeating,
request.exact,
request.wakeup,
Expand Down Expand Up @@ -317,13 +341,17 @@ private static String getPersistentAlarmKey(int requestCode) {
private static void addPersistentAlarm(
Context context,
int requestCode,
boolean alarmClock,
boolean allowWhileIdle,
boolean repeating,
boolean exact,
boolean wakeup,
long startMillis,
long intervalMillis,
long callbackHandle) {
HashMap<String, Object> alarmSettings = new HashMap<>();
alarmSettings.put("alarmClock", alarmClock);
alarmSettings.put("allowWhileIdle", allowWhileIdle);
alarmSettings.put("repeating", repeating);
alarmSettings.put("exact", exact);
alarmSettings.put("wakeup", wakeup);
Expand Down Expand Up @@ -389,6 +417,8 @@ public static void reschedulePersistentAlarms(Context context) {
}
try {
JSONObject alarm = new JSONObject(json);
boolean alarmClock = alarm.getBoolean("alarmClock");
boolean allowWhileIdle = alarm.getBoolean("allowWhileIdle");
boolean repeating = alarm.getBoolean("repeating");
boolean exact = alarm.getBoolean("exact");
boolean wakeup = alarm.getBoolean("wakeup");
Expand All @@ -398,6 +428,8 @@ public static void reschedulePersistentAlarms(Context context) {
scheduleAlarm(
context,
requestCode,
alarmClock,
allowWhileIdle,
repeating,
exact,
wakeup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,28 @@ public boolean onViewDestroy(FlutterNativeView nativeView) {
static final class OneShotRequest {
static OneShotRequest fromJson(JSONArray json) throws JSONException {
int requestCode = json.getInt(0);
boolean exact = json.getBoolean(1);
boolean wakeup = json.getBoolean(2);
long startMillis = json.getLong(3);
boolean rescheduleOnReboot = json.getBoolean(4);
long callbackHandle = json.getLong(5);
boolean alarmClock = json.getBoolean(1);
boolean allowWhileIdle = json.getBoolean(2);
boolean exact = json.getBoolean(3);
boolean wakeup = json.getBoolean(4);
long startMillis = json.getLong(5);
boolean rescheduleOnReboot = json.getBoolean(6);
long callbackHandle = json.getLong(7);

return new OneShotRequest(
requestCode, exact, wakeup, startMillis, rescheduleOnReboot, callbackHandle);
requestCode,
alarmClock,
allowWhileIdle,
exact,
wakeup,
startMillis,
rescheduleOnReboot,
callbackHandle);
}

final int requestCode;
final boolean alarmClock;
final boolean allowWhileIdle;
final boolean exact;
final boolean wakeup;
final long startMillis;
Expand All @@ -185,12 +196,16 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {

OneShotRequest(
int requestCode,
boolean alarmClock,
boolean allowWhileIdle,
boolean exact,
boolean wakeup,
long startMillis,
boolean rescheduleOnReboot,
long callbackHandle) {
this.requestCode = requestCode;
this.alarmClock = alarmClock;
this.allowWhileIdle = allowWhileIdle;
this.exact = exact;
this.wakeup = wakeup;
this.startMillis = startMillis;
Expand Down
15 changes: 13 additions & 2 deletions packages/android_alarm_manager/lib/android_alarm_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ class AndroidAlarmManager {
/// The timer is uniquely identified by `id`. Calling this function again
/// again with the same `id` will cancel and replace the existing timer.
///
/// If `alarmClock` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setAlarmClock`.
///
/// If `allowWhileIdle` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setExactAndAllowWhileIdle` or
/// `AlarmManagerCompat.setAndAllowWhileIdle`.
///
/// If `exact` is passed as `true`, the timer will be created with Android's
/// `AlarmManager.setRepeating`. When `exact` is `false` (the default), the
/// timer will be created with `AlarmManager.setInexactRepeating`.
/// `AlarmManagerCompat.setExact`. When `exact` is `false` (the default), the
/// timer will be created with `AlarmManager.set`.
///
/// If `wakeup` is passed as `true`, the device will be woken up when the
/// alarm fires. If `wakeup` is false (the default), the device will not be
Expand All @@ -101,6 +108,8 @@ class AndroidAlarmManager {
Duration delay,
int id,
dynamic Function() callback, {
bool alarmClock = false,
bool allowWhileIdle = false,
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
Expand All @@ -113,6 +122,8 @@ class AndroidAlarmManager {
}
final bool r = await _channel.invokeMethod<bool>('Alarm.oneShot', <dynamic>[
id,
alarmClock,
allowWhileIdle,
exact,
wakeup,
first,
Expand Down
2 changes: 1 addition & 1 deletion packages/android_alarm_manager/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: android_alarm_manager
description: Flutter plugin for accessing the Android AlarmManager service, and
running Dart code in the background when alarms fire.
version: 0.4.1+8
version: 0.4.2
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager

Expand Down