Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[android_alarm_manager] Added ability to get id in the callback (#1985)
Browse files Browse the repository at this point in the history
Co-Authored-By: Charles Crete <charles@cretezy.com>
  • Loading branch information
2 people authored and bkonyi committed Aug 19, 2019
1 parent 61c39d1 commit 4a7ddd2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
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.4

* Add `id` to `callback` if it is of type `Function(int)`

## 0.4.3

* Added `oneShotAt` method to run `callback` at a given DateTime `time`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ public void notImplemented() {
// provided.
// TODO(mattcarroll): consider giving a method name anyway for the purpose of developer discoverability
// when reading the source code. Especially on the Dart side.
sBackgroundChannel.invokeMethod("", new Object[] {callbackHandle}, result);
sBackgroundChannel.invokeMethod(
"", new Object[] {callbackHandle, intent.getIntExtra("id", -1)}, result);
}

private static void scheduleAlarm(
Expand Down Expand Up @@ -242,6 +243,7 @@ private static void scheduleAlarm(

// Create an Intent for the alarm and set the desired Dart callback handle.
Intent alarm = new Intent(context, AlarmBroadcastReceiver.class);
alarm.putExtra("id", requestCode);
alarm.putExtra("callbackHandle", callbackHandle);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, requestCode, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
34 changes: 28 additions & 6 deletions packages/android_alarm_manager/lib/android_alarm_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ void _alarmManagerCallbackDispatcher() {
print('Fatal: could not find callback');
exit(-1);
}
closure();

if (closure is Function()) {
closure();
} else if (closure is Function(int)) {
final int id = args[1];
closure(id);
}
});

// Once we've finished initializing, let the native portion of the plugin
Expand Down Expand Up @@ -80,8 +86,12 @@ class AndroidAlarmManager {
/// `callback` must be either a top-level function or a static method from a
/// class.
///
/// `callback` can be `Function()` or `Function(int)`
///
/// The timer is uniquely identified by `id`. Calling this function again
/// again with the same `id` will cancel and replace the existing timer.
/// with the same `id` will cancel and replace the existing timer.
///
/// `id` will passed to `callback` if it is of type `Function(int)`
///
/// If `alarmClock` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setAlarmClock`.
Expand All @@ -107,7 +117,7 @@ class AndroidAlarmManager {
static Future<bool> oneShot(
Duration delay,
int id,
dynamic Function() callback, {
Function callback, {
bool alarmClock = false,
bool allowWhileIdle = false,
bool exact = false,
Expand All @@ -134,8 +144,12 @@ class AndroidAlarmManager {
/// `callback` must be either a top-level function or a static method from a
/// class.
///
/// `callback` can be `Function()` or `Function(int)`
///
/// The timer is uniquely identified by `id`. Calling this function again
/// again with the same `id` will cancel and replace the existing timer.
/// with the same `id` will cancel and replace the existing timer.
///
/// `id` will passed to `callback` if it is of type `Function(int)`
///
/// If `alarmClock` is passed as `true`, the timer will be created with
/// Android's `AlarmManagerCompat.setAlarmClock`.
Expand All @@ -161,13 +175,15 @@ class AndroidAlarmManager {
static Future<bool> oneShotAt(
DateTime time,
int id,
dynamic Function() callback, {
Function callback, {
bool alarmClock = false,
bool allowWhileIdle = false,
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) async {
assert(callback is Function() || callback is Function(int));
assert(id.bitLength < 32);
final int startMillis = time.millisecondsSinceEpoch;
final CallbackHandle handle = PluginUtilities.getCallbackHandle(callback);
if (handle == null) {
Expand Down Expand Up @@ -196,9 +212,13 @@ class AndroidAlarmManager {
/// `callback` must be either a top-level function or a static method from a
/// class.
///
/// `callback` can be `Function()` or `Function(int)`
///
/// The repeating timer is uniquely identified by `id`. Calling this function
/// again with the same `id` will cancel and replace the existing timer.
///
/// `id` will passed to `callback` if it is of type `Function(int)`
///
/// If `startAt` is passed, the timer will first go off at that time and
/// subsequently run with period `duration`.
///
Expand All @@ -219,12 +239,14 @@ class AndroidAlarmManager {
static Future<bool> periodic(
Duration duration,
int id,
dynamic Function() callback, {
Function callback, {
DateTime startAt,
bool exact = false,
bool wakeup = false,
bool rescheduleOnReboot = false,
}) async {
assert(callback is Function() || callback is Function(int));
assert(id.bitLength < 32);
final int now = DateTime.now().millisecondsSinceEpoch;
final int period = duration.inMilliseconds;
final int 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.3
version: 0.4.4
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager

Expand Down

0 comments on commit 4a7ddd2

Please sign in to comment.