Skip to content

Commit 5272c94

Browse files
feat(android_alarm_manager_plus): we can now send extra data to alarm manager and receive it in our callback (#1014)
Co-authored-by: Mostafa <123456> Co-authored-by: Joachim Nohl <[email protected]>
1 parent 1795b72 commit 5272c94

File tree

7 files changed

+253
-43
lines changed

7 files changed

+253
-43
lines changed

packages/android_alarm_manager_plus/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:7.0.2'
11+
classpath 'com.android.tools.build:gradle:7.2.1'
1212
}
1313
}
1414

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Tue Oct 05 10:00:26 EEST 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

packages/android_alarm_manager_plus/android/src/main/java/dev/fluttercommunity/plus/androidalarmmanager/AlarmService.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public static void startBackgroundIsolate(Context context, long callbackHandle)
7272
* AlarmService.initialized} message. Processes all alarm events that came in while the isolate
7373
* was starting.
7474
*/
75-
/* package */ static void onInitialized() {
75+
/* package */
76+
static void onInitialized() {
7677
Log.i(TAG, "AlarmService started!");
7778
synchronized (alarmQueue) {
7879
// Handle all the alarm events received before the Dart isolate was
@@ -103,7 +104,8 @@ private static void scheduleAlarm(
103104
long startMillis,
104105
long intervalMillis,
105106
boolean rescheduleOnReboot,
106-
long callbackHandle) {
107+
long callbackHandle,
108+
JSONObject params) {
107109
if (rescheduleOnReboot) {
108110
addPersistentAlarm(
109111
context,
@@ -115,13 +117,15 @@ private static void scheduleAlarm(
115117
wakeup,
116118
startMillis,
117119
intervalMillis,
118-
callbackHandle);
120+
callbackHandle,
121+
params);
119122
}
120123

121124
// Create an Intent for the alarm and set the desired Dart callback handle.
122125
Intent alarm = new Intent(context, AlarmBroadcastReceiver.class);
123126
alarm.putExtra("id", requestCode);
124127
alarm.putExtra("callbackHandle", callbackHandle);
128+
alarm.putExtra("params", params == null ? null : params.toString());
125129
PendingIntent pendingIntent =
126130
PendingIntent.getBroadcast(
127131
context,
@@ -190,7 +194,8 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
190194
request.startMillis,
191195
0,
192196
request.rescheduleOnReboot,
193-
request.callbackHandle);
197+
request.callbackHandle,
198+
request.params);
194199
}
195200

196201
/** Schedules a periodic alarm to be executed repeatedly in the future. */
@@ -209,7 +214,8 @@ public static void setPeriodic(
209214
request.startMillis,
210215
request.intervalMillis,
211216
request.rescheduleOnReboot,
212-
request.callbackHandle);
217+
request.callbackHandle,
218+
request.params);
213219
}
214220

215221
/** Cancels an alarm with ID {@code requestCode}. */
@@ -248,7 +254,8 @@ private static void addPersistentAlarm(
248254
boolean wakeup,
249255
long startMillis,
250256
long intervalMillis,
251-
long callbackHandle) {
257+
long callbackHandle,
258+
JSONObject params) {
252259
HashMap<String, Object> alarmSettings = new HashMap<>();
253260
alarmSettings.put("alarmClock", alarmClock);
254261
alarmSettings.put("allowWhileIdle", allowWhileIdle);
@@ -258,6 +265,7 @@ private static void addPersistentAlarm(
258265
alarmSettings.put("startMillis", startMillis);
259266
alarmSettings.put("intervalMillis", intervalMillis);
260267
alarmSettings.put("callbackHandle", callbackHandle);
268+
alarmSettings.put("params", params);
261269
JSONObject obj = new JSONObject(alarmSettings);
262270
String key = getPersistentAlarmKey(requestCode);
263271
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_KEY, 0);
@@ -322,6 +330,7 @@ public static void reschedulePersistentAlarms(Context context) {
322330
long startMillis = alarm.getLong("startMillis");
323331
long intervalMillis = alarm.getLong("intervalMillis");
324332
long callbackHandle = alarm.getLong("callbackHandle");
333+
JSONObject params = alarm.getJSONObject("params");
325334
scheduleAlarm(
326335
context,
327336
requestCode,
@@ -333,7 +342,8 @@ public static void reschedulePersistentAlarms(Context context) {
333342
startMillis,
334343
intervalMillis,
335344
false,
336-
callbackHandle);
345+
callbackHandle,
346+
params);
337347
} catch (JSONException e) {
338348
Log.e(TAG, "Data for alarm request code " + requestCode + " is invalid: " + json);
339349
}

packages/android_alarm_manager_plus/android/src/main/java/dev/fluttercommunity/plus/androidalarmmanager/AndroidAlarmManagerPlugin.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.flutter.view.FlutterNativeView;
1717
import org.json.JSONArray;
1818
import org.json.JSONException;
19+
import org.json.JSONObject;
1920

2021
/**
2122
* Flutter plugin for running one-shot and periodic tasks sometime in the future on Android.
@@ -149,6 +150,7 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
149150
long startMillis = json.getLong(5);
150151
boolean rescheduleOnReboot = json.getBoolean(6);
151152
long callbackHandle = json.getLong(7);
153+
JSONObject params = json.getJSONObject(8);
152154

153155
return new OneShotRequest(
154156
requestCode,
@@ -158,7 +160,8 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
158160
wakeup,
159161
startMillis,
160162
rescheduleOnReboot,
161-
callbackHandle);
163+
callbackHandle,
164+
params);
162165
}
163166

164167
final int requestCode;
@@ -169,6 +172,7 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
169172
final long startMillis;
170173
final boolean rescheduleOnReboot;
171174
final long callbackHandle;
175+
final JSONObject params;
172176

173177
OneShotRequest(
174178
int requestCode,
@@ -178,7 +182,8 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
178182
boolean wakeup,
179183
long startMillis,
180184
boolean rescheduleOnReboot,
181-
long callbackHandle) {
185+
long callbackHandle,
186+
JSONObject params) {
182187
this.requestCode = requestCode;
183188
this.alarmClock = alarmClock;
184189
this.allowWhileIdle = allowWhileIdle;
@@ -187,6 +192,7 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
187192
this.startMillis = startMillis;
188193
this.rescheduleOnReboot = rescheduleOnReboot;
189194
this.callbackHandle = callbackHandle;
195+
this.params = params;
190196
}
191197
}
192198

@@ -201,7 +207,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
201207
long intervalMillis = json.getLong(5);
202208
boolean rescheduleOnReboot = json.getBoolean(6);
203209
long callbackHandle = json.getLong(7);
204-
210+
JSONObject params = json.getJSONObject(8);
205211
return new PeriodicRequest(
206212
requestCode,
207213
allowWhileIdle,
@@ -210,7 +216,8 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
210216
startMillis,
211217
intervalMillis,
212218
rescheduleOnReboot,
213-
callbackHandle);
219+
callbackHandle,
220+
params);
214221
}
215222

216223
final int requestCode;
@@ -221,6 +228,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
221228
final long intervalMillis;
222229
final boolean rescheduleOnReboot;
223230
final long callbackHandle;
231+
final JSONObject params;
224232

225233
PeriodicRequest(
226234
int requestCode,
@@ -230,7 +238,8 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
230238
long startMillis,
231239
long intervalMillis,
232240
boolean rescheduleOnReboot,
233-
long callbackHandle) {
241+
long callbackHandle,
242+
JSONObject params) {
234243
this.requestCode = requestCode;
235244
this.allowWhileIdle = allowWhileIdle;
236245
this.exact = exact;
@@ -239,6 +248,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
239248
this.intervalMillis = intervalMillis;
240249
this.rescheduleOnReboot = rescheduleOnReboot;
241250
this.callbackHandle = callbackHandle;
251+
this.params = params;
242252
}
243253
}
244254
}

packages/android_alarm_manager_plus/android/src/main/java/dev/fluttercommunity/plus/androidalarmmanager/FlutterBackgroundExecutor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.Intent;
99
import android.content.SharedPreferences;
1010
import android.content.res.AssetManager;
11+
import android.text.TextUtils;
1112
import android.util.Log;
1213
import io.flutter.FlutterInjector;
1314
import io.flutter.embedding.engine.FlutterEngine;
@@ -24,7 +25,8 @@
2425
import io.flutter.view.FlutterCallbackInformation;
2526
import java.util.concurrent.CountDownLatch;
2627
import java.util.concurrent.atomic.AtomicBoolean;
27-
28+
import org.json.JSONException;
29+
import org.json.JSONObject;
2830
/**
2931
* An background execution abstraction which handles initializing a background isolate running a
3032
* callback dispatcher, used to invoke Dart callbacks while backgrounded.
@@ -193,7 +195,15 @@ public void executeDartCallbackInBackgroundIsolate(Intent intent, final CountDow
193195
// attention to the type of the callback handle as storing this value in a
194196
// variable of the wrong size will cause the callback lookup to fail.
195197
long callbackHandle = intent.getLongExtra("callbackHandle", 0);
196-
198+
String paramsJsonString = intent.getStringExtra("params");
199+
JSONObject params = null;
200+
if (!TextUtils.isEmpty(paramsJsonString)) {
201+
try {
202+
params = new JSONObject(paramsJsonString);
203+
} catch (JSONException e) {
204+
throw new IllegalArgumentException("Can not convert 'params' to JsonObject", e);
205+
}
206+
}
197207
// If another thread is waiting, then wake that thread when the callback returns a result.
198208
MethodChannel.Result result = null;
199209
if (latch != null) {
@@ -221,7 +231,7 @@ public void notImplemented() {
221231
// provided.
222232
backgroundChannel.invokeMethod(
223233
"invokeAlarmManagerCallback",
224-
new Object[] {callbackHandle, intent.getIntExtra("id", -1)},
234+
new Object[] {callbackHandle, intent.getIntExtra("id", -1), params},
225235
result);
226236
}
227237

0 commit comments

Comments
 (0)