Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 5e53149

Browse files
brouxcomithun-mondal
authored andcommitted
[android_alarm_manager] Added support for setting alarms which work when the phone is in doze mode (flutter#1895)
1 parent 6a41c7a commit 5e53149

5 files changed

Lines changed: 73 additions & 11 deletions

File tree

packages/android_alarm_manager/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.2
2+
3+
* Added support for setting alarms which work when the phone is in doze mode.
4+
15
## 0.4.1+8
26

37
* Remove dependency on google-services in the Android example.

packages/android_alarm_manager/android/src/main/java/io/flutter/plugins/androidalarmmanager/AlarmService.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ public void notImplemented() {
217217
private static void scheduleAlarm(
218218
Context context,
219219
int requestCode,
220+
boolean alarmClock,
221+
boolean allowWhileIdle,
220222
boolean repeating,
221223
boolean exact,
222224
boolean wakeup,
@@ -228,6 +230,8 @@ private static void scheduleAlarm(
228230
addPersistentAlarm(
229231
context,
230232
requestCode,
233+
alarmClock,
234+
allowWhileIdle,
231235
repeating,
232236
exact,
233237
wakeup,
@@ -250,17 +254,31 @@ private static void scheduleAlarm(
250254

251255
// Schedule the alarm.
252256
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
257+
258+
if (alarmClock) {
259+
AlarmManagerCompat.setAlarmClock(manager, startMillis, pendingIntent, pendingIntent);
260+
return;
261+
}
262+
253263
if (exact) {
254264
if (repeating) {
255265
manager.setRepeating(clock, startMillis, intervalMillis, pendingIntent);
256266
} else {
257-
AlarmManagerCompat.setExact(manager, clock, startMillis, pendingIntent);
267+
if (allowWhileIdle) {
268+
AlarmManagerCompat.setExactAndAllowWhileIdle(manager, clock, startMillis, pendingIntent);
269+
} else {
270+
AlarmManagerCompat.setExact(manager, clock, startMillis, pendingIntent);
271+
}
258272
}
259273
} else {
260274
if (repeating) {
261275
manager.setInexactRepeating(clock, startMillis, intervalMillis, pendingIntent);
262276
} else {
263-
manager.set(clock, startMillis, pendingIntent);
277+
if (allowWhileIdle) {
278+
AlarmManagerCompat.setAndAllowWhileIdle(manager, clock, startMillis, pendingIntent);
279+
} else {
280+
manager.set(clock, startMillis, pendingIntent);
281+
}
264282
}
265283
}
266284
}
@@ -270,6 +288,8 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
270288
scheduleAlarm(
271289
context,
272290
request.requestCode,
291+
request.alarmClock,
292+
request.allowWhileIdle,
273293
repeating,
274294
request.exact,
275295
request.wakeup,
@@ -282,9 +302,13 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
282302
public static void setPeriodic(
283303
Context context, AndroidAlarmManagerPlugin.PeriodicRequest request) {
284304
final boolean repeating = true;
305+
final boolean allowWhileIdle = false;
306+
final boolean alarmClock = false;
285307
scheduleAlarm(
286308
context,
287309
request.requestCode,
310+
alarmClock,
311+
allowWhileIdle,
288312
repeating,
289313
request.exact,
290314
request.wakeup,
@@ -317,13 +341,17 @@ private static String getPersistentAlarmKey(int requestCode) {
317341
private static void addPersistentAlarm(
318342
Context context,
319343
int requestCode,
344+
boolean alarmClock,
345+
boolean allowWhileIdle,
320346
boolean repeating,
321347
boolean exact,
322348
boolean wakeup,
323349
long startMillis,
324350
long intervalMillis,
325351
long callbackHandle) {
326352
HashMap<String, Object> alarmSettings = new HashMap<>();
353+
alarmSettings.put("alarmClock", alarmClock);
354+
alarmSettings.put("allowWhileIdle", allowWhileIdle);
327355
alarmSettings.put("repeating", repeating);
328356
alarmSettings.put("exact", exact);
329357
alarmSettings.put("wakeup", wakeup);
@@ -389,6 +417,8 @@ public static void reschedulePersistentAlarms(Context context) {
389417
}
390418
try {
391419
JSONObject alarm = new JSONObject(json);
420+
boolean alarmClock = alarm.getBoolean("alarmClock");
421+
boolean allowWhileIdle = alarm.getBoolean("allowWhileIdle");
392422
boolean repeating = alarm.getBoolean("repeating");
393423
boolean exact = alarm.getBoolean("exact");
394424
boolean wakeup = alarm.getBoolean("wakeup");
@@ -398,6 +428,8 @@ public static void reschedulePersistentAlarms(Context context) {
398428
scheduleAlarm(
399429
context,
400430
requestCode,
431+
alarmClock,
432+
allowWhileIdle,
401433
repeating,
402434
exact,
403435
wakeup,

packages/android_alarm_manager/android/src/main/java/io/flutter/plugins/androidalarmmanager/AndroidAlarmManagerPlugin.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,28 @@ public boolean onViewDestroy(FlutterNativeView nativeView) {
166166
static final class OneShotRequest {
167167
static OneShotRequest fromJson(JSONArray json) throws JSONException {
168168
int requestCode = json.getInt(0);
169-
boolean exact = json.getBoolean(1);
170-
boolean wakeup = json.getBoolean(2);
171-
long startMillis = json.getLong(3);
172-
boolean rescheduleOnReboot = json.getBoolean(4);
173-
long callbackHandle = json.getLong(5);
169+
boolean alarmClock = json.getBoolean(1);
170+
boolean allowWhileIdle = json.getBoolean(2);
171+
boolean exact = json.getBoolean(3);
172+
boolean wakeup = json.getBoolean(4);
173+
long startMillis = json.getLong(5);
174+
boolean rescheduleOnReboot = json.getBoolean(6);
175+
long callbackHandle = json.getLong(7);
174176

175177
return new OneShotRequest(
176-
requestCode, exact, wakeup, startMillis, rescheduleOnReboot, callbackHandle);
178+
requestCode,
179+
alarmClock,
180+
allowWhileIdle,
181+
exact,
182+
wakeup,
183+
startMillis,
184+
rescheduleOnReboot,
185+
callbackHandle);
177186
}
178187

179188
final int requestCode;
189+
final boolean alarmClock;
190+
final boolean allowWhileIdle;
180191
final boolean exact;
181192
final boolean wakeup;
182193
final long startMillis;
@@ -185,12 +196,16 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
185196

186197
OneShotRequest(
187198
int requestCode,
199+
boolean alarmClock,
200+
boolean allowWhileIdle,
188201
boolean exact,
189202
boolean wakeup,
190203
long startMillis,
191204
boolean rescheduleOnReboot,
192205
long callbackHandle) {
193206
this.requestCode = requestCode;
207+
this.alarmClock = alarmClock;
208+
this.allowWhileIdle = allowWhileIdle;
194209
this.exact = exact;
195210
this.wakeup = wakeup;
196211
this.startMillis = startMillis;

packages/android_alarm_manager/lib/android_alarm_manager.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,16 @@ class AndroidAlarmManager {
8383
/// The timer is uniquely identified by `id`. Calling this function again
8484
/// again with the same `id` will cancel and replace the existing timer.
8585
///
86+
/// If `alarmClock` is passed as `true`, the timer will be created with
87+
/// Android's `AlarmManagerCompat.setAlarmClock`.
88+
///
89+
/// If `allowWhileIdle` is passed as `true`, the timer will be created with
90+
/// Android's `AlarmManagerCompat.setExactAndAllowWhileIdle` or
91+
/// `AlarmManagerCompat.setAndAllowWhileIdle`.
92+
///
8693
/// If `exact` is passed as `true`, the timer will be created with Android's
87-
/// `AlarmManager.setRepeating`. When `exact` is `false` (the default), the
88-
/// timer will be created with `AlarmManager.setInexactRepeating`.
94+
/// `AlarmManagerCompat.setExact`. When `exact` is `false` (the default), the
95+
/// timer will be created with `AlarmManager.set`.
8996
///
9097
/// If `wakeup` is passed as `true`, the device will be woken up when the
9198
/// alarm fires. If `wakeup` is false (the default), the device will not be
@@ -101,6 +108,8 @@ class AndroidAlarmManager {
101108
Duration delay,
102109
int id,
103110
dynamic Function() callback, {
111+
bool alarmClock = false,
112+
bool allowWhileIdle = false,
104113
bool exact = false,
105114
bool wakeup = false,
106115
bool rescheduleOnReboot = false,
@@ -113,6 +122,8 @@ class AndroidAlarmManager {
113122
}
114123
final bool r = await _channel.invokeMethod<bool>('Alarm.oneShot', <dynamic>[
115124
id,
125+
alarmClock,
126+
allowWhileIdle,
116127
exact,
117128
wakeup,
118129
first,

packages/android_alarm_manager/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: android_alarm_manager
22
description: Flutter plugin for accessing the Android AlarmManager service, and
33
running Dart code in the background when alarms fire.
4-
version: 0.4.1+8
4+
version: 0.4.2
55
author: Flutter Team <flutter-dev@googlegroups.com>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager
77

0 commit comments

Comments
 (0)