Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android_alarm_manager_plus): we can now send extra data to alarm manager and receive it in our callback #1014

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8da2603
Now we can send extra data as Map to alarm manager and receive it in …
ktdynamic Aug 10, 2022
705228a
code style fixed
ktdynamic Aug 10, 2022
1e0d717
Merge branch 'main' into alarm_manager_with_sending_data
nohli Sep 28, 2022
fa3a9c7
throw UnsupportedException when passing no Json parsable to params
ktdynamic Oct 7, 2022
89a541a
throw UnsupportedException when passing not Json parsable to params
ktdynamic Oct 7, 2022
ad9dd13
throw UnsupportedException when passing not Json parsable to params
ktdynamic Oct 7, 2022
2afde5b
throw UnsupportedException when passing not Json parsable to params
ktdynamic Oct 8, 2022
4d29cc4
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
86fdf64
Update packages/android_alarm_manager_plus/test/android_alarm_manager…
smartdevelopers-ir Oct 15, 2022
b4c02a0
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
a71a79c
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
b6898e3
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
e53a46f
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
22a63e1
Update packages/android_alarm_manager_plus/lib/android_alarm_manager_…
smartdevelopers-ir Oct 15, 2022
e1a0b9a
apply suggestions
ktdynamic Oct 15, 2022
75760b1
apply suggestions
ktdynamic Oct 15, 2022
4a9a387
apply suggestions
ktdynamic Oct 15, 2022
4ffcbb0
Rename method
nohli Oct 20, 2022
9373ac1
Add new line
nohli Oct 20, 2022
40e5264
Use single quotes, add trailing comma
nohli Oct 20, 2022
4bed0a3
Make test easier to read
nohli Oct 20, 2022
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
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());
smartdevelopers-ir marked this conversation as resolved.
Show resolved Hide resolved
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
Loading