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] Android Code Inspection and Clean up (#3020)
Browse files Browse the repository at this point in the history
-Werror and code cleanup for Java code.
  • Loading branch information
hamdikahloun committed Sep 14, 2020
1 parent 7e0fc94 commit 4eb849a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
4 changes: 4 additions & 0 deletions packages/android_alarm_manager/CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.4.5+13

* Android Code Inspection and Clean up.

## 0.4.5+12

* Update package:e2e reference to use the local version in the flutter/plugins
Expand Down
5 changes: 5 additions & 0 deletions packages/android_alarm_manager/android/build.gradle
@@ -1,5 +1,6 @@
group 'io.flutter.plugins.androidalarmmanager'
version '1.0-SNAPSHOT'
def args = ["-Xlint:deprecation","-Xlint:unchecked","-Werror"]

buildscript {
repositories {
Expand All @@ -19,6 +20,10 @@ rootProject.allprojects {
}
}

project.getTasks().withType(JavaCompile){
options.compilerArgs.addAll(args)
}

apply plugin: 'com.android.library'

android {
Expand Down
Expand Up @@ -17,7 +17,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -76,9 +75,8 @@ public static void startBackgroundIsolate(Context context, long callbackHandle)
synchronized (alarmQueue) {
// Handle all the alarm events received before the Dart isolate was
// initialized, then clear the queue.
Iterator<Intent> i = alarmQueue.iterator();
while (i.hasNext()) {
flutterBackgroundExecutor.executeDartCallbackInBackgroundIsolate(i.next(), null);
for (Intent intent : alarmQueue) {
flutterBackgroundExecutor.executeDartCallbackInBackgroundIsolate(intent, null);
}
alarmQueue.clear();
}
Expand Down Expand Up @@ -231,7 +229,7 @@ public static void cancel(Context context, int requestCode) {
}

private static String getPersistentAlarmKey(int requestCode) {
return "android_alarm_manager/persistent_alarm_" + Integer.toString(requestCode);
return "android_alarm_manager/persistent_alarm_" + requestCode;
}

private static void addPersistentAlarm(
Expand Down Expand Up @@ -276,13 +274,14 @@ private static void addPersistentAlarm(
}

private static void clearPersistentAlarm(Context context, int requestCode) {
String request = String.valueOf(requestCode);
SharedPreferences p = context.getSharedPreferences(SHARED_PREFERENCES_KEY, 0);
synchronized (persistentAlarmsLock) {
Set<String> persistentAlarms = p.getStringSet(PERSISTENT_ALARMS_SET_KEY, null);
if ((persistentAlarms == null) || !persistentAlarms.contains(requestCode)) {
if ((persistentAlarms == null) || !persistentAlarms.contains(request)) {
return;
}
persistentAlarms.remove(requestCode);
persistentAlarms.remove(request);
String key = getPersistentAlarmKey(requestCode);
p.edit().remove(key).putStringSet(PERSISTENT_ALARMS_SET_KEY, persistentAlarms).apply();

Expand All @@ -301,14 +300,12 @@ public static void reschedulePersistentAlarms(Context context) {
return;
}

Iterator<String> it = persistentAlarms.iterator();
while (it.hasNext()) {
int requestCode = Integer.parseInt(it.next());
for (String persistentAlarm : persistentAlarms) {
int requestCode = Integer.parseInt(persistentAlarm);
String key = getPersistentAlarmKey(requestCode);
String json = p.getString(key, null);
if (json == null) {
Log.e(
TAG, "Data for alarm request code " + Integer.toString(requestCode) + " is invalid.");
Log.e(TAG, "Data for alarm request code " + requestCode + " is invalid.");
continue;
}
try {
Expand Down
Expand Up @@ -107,37 +107,43 @@ public void onMethodCall(MethodCall call, Result result) {
String method = call.method;
Object arguments = call.arguments;
try {
if (method.equals("AlarmService.start")) {
// This message is sent when the Dart side of this plugin is told to initialize.
long callbackHandle = ((JSONArray) arguments).getLong(0);
// In response, this (native) side of the plugin needs to spin up a background
// Dart isolate by using the given callbackHandle, and then setup a background
// method channel to communicate with the new background isolate. Once completed,
// this onMethodCall() method will receive messages from both the primary and background
// method channels.
AlarmService.setCallbackDispatcher(context, callbackHandle);
AlarmService.startBackgroundIsolate(context, callbackHandle);
result.success(true);
} else if (method.equals("Alarm.periodic")) {
// This message indicates that the Flutter app would like to schedule a periodic
// task.
PeriodicRequest periodicRequest = PeriodicRequest.fromJson((JSONArray) arguments);
AlarmService.setPeriodic(context, periodicRequest);
result.success(true);
} else if (method.equals("Alarm.oneShotAt")) {
// This message indicates that the Flutter app would like to schedule a one-time
// task.
OneShotRequest oneShotRequest = OneShotRequest.fromJson((JSONArray) arguments);
AlarmService.setOneShot(context, oneShotRequest);
result.success(true);
} else if (method.equals("Alarm.cancel")) {
// This message indicates that the Flutter app would like to cancel a previously
// scheduled task.
int requestCode = ((JSONArray) arguments).getInt(0);
AlarmService.cancel(context, requestCode);
result.success(true);
} else {
result.notImplemented();
switch (method) {
case "AlarmService.start":
// This message is sent when the Dart side of this plugin is told to initialize.
long callbackHandle = ((JSONArray) arguments).getLong(0);
// In response, this (native) side of the plugin needs to spin up a background
// Dart isolate by using the given callbackHandle, and then setup a background
// method channel to communicate with the new background isolate. Once completed,
// this onMethodCall() method will receive messages from both the primary and background
// method channels.
AlarmService.setCallbackDispatcher(context, callbackHandle);
AlarmService.startBackgroundIsolate(context, callbackHandle);
result.success(true);
break;
case "Alarm.periodic":
// This message indicates that the Flutter app would like to schedule a periodic
// task.
PeriodicRequest periodicRequest = PeriodicRequest.fromJson((JSONArray) arguments);
AlarmService.setPeriodic(context, periodicRequest);
result.success(true);
break;
case "Alarm.oneShotAt":
// This message indicates that the Flutter app would like to schedule a one-time
// task.
OneShotRequest oneShotRequest = OneShotRequest.fromJson((JSONArray) arguments);
AlarmService.setOneShot(context, oneShotRequest);
result.success(true);
break;
case "Alarm.cancel":
// This message indicates that the Flutter app would like to cancel a previously
// scheduled task.
int requestCode = ((JSONArray) arguments).getInt(0);
AlarmService.cancel(context, requestCode);
result.success(true);
break;
default:
result.notImplemented();
break;
}
} catch (JSONException e) {
result.error("error", "JSON error: " + e.getMessage(), null);
Expand Down
Expand Up @@ -78,7 +78,6 @@ private void onInitialized() {
@Override
public void onMethodCall(MethodCall call, Result result) {
String method = call.method;
Object arguments = call.arguments;
try {
if (method.equals("AlarmService.initialized")) {
// This message is sent by the background method channel as soon as the background isolate
Expand Down Expand Up @@ -152,20 +151,16 @@ public void startBackgroundIsolate(Context context, long callbackHandle) {
}

Log.i(TAG, "Starting AlarmService...");
String appBundlePath = FlutterMain.findAppBundlePath(context);
String appBundlePath = FlutterMain.findAppBundlePath();
AssetManager assets = context.getAssets();
if (appBundlePath != null && !isRunning()) {
if (!isRunning()) {
backgroundFlutterEngine = new FlutterEngine(context);

// We need to create an instance of `FlutterEngine` before looking up the
// callback. If we don't, the callback cache won't be initialized and the
// lookup will fail.
FlutterCallbackInformation flutterCallback =
FlutterCallbackInformation.lookupCallbackInformation(callbackHandle);
if (flutterCallback == null) {
Log.e(TAG, "Fatal: failed to find callback");
return;
}

DartExecutor executor = backgroundFlutterEngine.getDartExecutor();
initializeMethodChannel(executor);
Expand Down
2 changes: 1 addition & 1 deletion packages/android_alarm_manager/pubspec.yaml
Expand Up @@ -4,7 +4,7 @@ description: Flutter plugin for accessing the Android AlarmManager service, and
# 0.4.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.4.5+11
version: 0.4.5+12
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager

dependencies:
Expand Down

0 comments on commit 4eb849a

Please sign in to comment.