Skip to content

Commit

Permalink
feat(android_alarm_manager_plus): we can now send extra data to alarm…
Browse files Browse the repository at this point in the history
… manager and receive it in our callback (#1014)

Co-authored-by: Mostafa <123456>
Co-authored-by: Joachim Nohl <43643339+nohli@users.noreply.github.com>
  • Loading branch information
smartdevelopers-ir and nohli committed Oct 20, 2022
1 parent 1795b72 commit 5272c94
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/android_alarm_manager_plus/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:7.2.1'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Oct 05 10:00:26 EEST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public static void startBackgroundIsolate(Context context, long callbackHandle)
* AlarmService.initialized} message. Processes all alarm events that came in while the isolate
* was starting.
*/
/* package */ static void onInitialized() {
/* package */
static void onInitialized() {
Log.i(TAG, "AlarmService started!");
synchronized (alarmQueue) {
// Handle all the alarm events received before the Dart isolate was
Expand Down Expand Up @@ -103,7 +104,8 @@ private static void scheduleAlarm(
long startMillis,
long intervalMillis,
boolean rescheduleOnReboot,
long callbackHandle) {
long callbackHandle,
JSONObject params) {
if (rescheduleOnReboot) {
addPersistentAlarm(
context,
Expand All @@ -115,13 +117,15 @@ private static void scheduleAlarm(
wakeup,
startMillis,
intervalMillis,
callbackHandle);
callbackHandle,
params);
}

// 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);
alarm.putExtra("params", params == null ? null : params.toString());
PendingIntent pendingIntent =
PendingIntent.getBroadcast(
context,
Expand Down Expand Up @@ -190,7 +194,8 @@ public static void setOneShot(Context context, AndroidAlarmManagerPlugin.OneShot
request.startMillis,
0,
request.rescheduleOnReboot,
request.callbackHandle);
request.callbackHandle,
request.params);
}

/** Schedules a periodic alarm to be executed repeatedly in the future. */
Expand All @@ -209,7 +214,8 @@ public static void setPeriodic(
request.startMillis,
request.intervalMillis,
request.rescheduleOnReboot,
request.callbackHandle);
request.callbackHandle,
request.params);
}

/** Cancels an alarm with ID {@code requestCode}. */
Expand Down Expand Up @@ -248,7 +254,8 @@ private static void addPersistentAlarm(
boolean wakeup,
long startMillis,
long intervalMillis,
long callbackHandle) {
long callbackHandle,
JSONObject params) {
HashMap<String, Object> alarmSettings = new HashMap<>();
alarmSettings.put("alarmClock", alarmClock);
alarmSettings.put("allowWhileIdle", allowWhileIdle);
Expand All @@ -258,6 +265,7 @@ private static void addPersistentAlarm(
alarmSettings.put("startMillis", startMillis);
alarmSettings.put("intervalMillis", intervalMillis);
alarmSettings.put("callbackHandle", callbackHandle);
alarmSettings.put("params", params);
JSONObject obj = new JSONObject(alarmSettings);
String key = getPersistentAlarmKey(requestCode);
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_KEY, 0);
Expand Down Expand Up @@ -322,6 +330,7 @@ public static void reschedulePersistentAlarms(Context context) {
long startMillis = alarm.getLong("startMillis");
long intervalMillis = alarm.getLong("intervalMillis");
long callbackHandle = alarm.getLong("callbackHandle");
JSONObject params = alarm.getJSONObject("params");
scheduleAlarm(
context,
requestCode,
Expand All @@ -333,7 +342,8 @@ public static void reschedulePersistentAlarms(Context context) {
startMillis,
intervalMillis,
false,
callbackHandle);
callbackHandle,
params);
} catch (JSONException e) {
Log.e(TAG, "Data for alarm request code " + requestCode + " is invalid: " + json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.flutter.view.FlutterNativeView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

return new OneShotRequest(
requestCode,
Expand All @@ -158,7 +160,8 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
wakeup,
startMillis,
rescheduleOnReboot,
callbackHandle);
callbackHandle,
params);
}

final int requestCode;
Expand All @@ -169,6 +172,7 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
final long startMillis;
final boolean rescheduleOnReboot;
final long callbackHandle;
final JSONObject params;

OneShotRequest(
int requestCode,
Expand All @@ -178,7 +182,8 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
boolean wakeup,
long startMillis,
boolean rescheduleOnReboot,
long callbackHandle) {
long callbackHandle,
JSONObject params) {
this.requestCode = requestCode;
this.alarmClock = alarmClock;
this.allowWhileIdle = allowWhileIdle;
Expand All @@ -187,6 +192,7 @@ static OneShotRequest fromJson(JSONArray json) throws JSONException {
this.startMillis = startMillis;
this.rescheduleOnReboot = rescheduleOnReboot;
this.callbackHandle = callbackHandle;
this.params = params;
}
}

Expand All @@ -201,7 +207,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
long intervalMillis = json.getLong(5);
boolean rescheduleOnReboot = json.getBoolean(6);
long callbackHandle = json.getLong(7);

JSONObject params = json.getJSONObject(8);
return new PeriodicRequest(
requestCode,
allowWhileIdle,
Expand All @@ -210,7 +216,8 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
startMillis,
intervalMillis,
rescheduleOnReboot,
callbackHandle);
callbackHandle,
params);
}

final int requestCode;
Expand All @@ -221,6 +228,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
final long intervalMillis;
final boolean rescheduleOnReboot;
final long callbackHandle;
final JSONObject params;

PeriodicRequest(
int requestCode,
Expand All @@ -230,7 +238,8 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
long startMillis,
long intervalMillis,
boolean rescheduleOnReboot,
long callbackHandle) {
long callbackHandle,
JSONObject params) {
this.requestCode = requestCode;
this.allowWhileIdle = allowWhileIdle;
this.exact = exact;
Expand All @@ -239,6 +248,7 @@ static PeriodicRequest fromJson(JSONArray json) throws JSONException {
this.intervalMillis = intervalMillis;
this.rescheduleOnReboot = rescheduleOnReboot;
this.callbackHandle = callbackHandle;
this.params = params;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
Expand All @@ -24,7 +25,8 @@
import io.flutter.view.FlutterCallbackInformation;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import org.json.JSONException;
import org.json.JSONObject;
/**
* An background execution abstraction which handles initializing a background isolate running a
* callback dispatcher, used to invoke Dart callbacks while backgrounded.
Expand Down Expand Up @@ -193,7 +195,15 @@ public void executeDartCallbackInBackgroundIsolate(Intent intent, final CountDow
// attention to the type of the callback handle as storing this value in a
// variable of the wrong size will cause the callback lookup to fail.
long callbackHandle = intent.getLongExtra("callbackHandle", 0);

String paramsJsonString = intent.getStringExtra("params");
JSONObject params = null;
if (!TextUtils.isEmpty(paramsJsonString)) {
try {
params = new JSONObject(paramsJsonString);
} catch (JSONException e) {
throw new IllegalArgumentException("Can not convert 'params' to JsonObject", e);
}
}
// If another thread is waiting, then wake that thread when the callback returns a result.
MethodChannel.Result result = null;
if (latch != null) {
Expand Down Expand Up @@ -221,7 +231,7 @@ public void notImplemented() {
// provided.
backgroundChannel.invokeMethod(
"invokeAlarmManagerCallback",
new Object[] {callbackHandle, intent.getIntExtra("id", -1)},
new Object[] {callbackHandle, intent.getIntExtra("id", -1), params},
result);
}

Expand Down

0 comments on commit 5272c94

Please sign in to comment.