Commit 4068dc38 by Sebastián Katzer

Support for infinite trigger on Android

parent c65e9473
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
package de.appplant.cordova.plugin.localnotification; package de.appplant.cordova.plugin.localnotification;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
...@@ -30,12 +29,12 @@ import de.appplant.cordova.plugin.notification.Builder; ...@@ -30,12 +29,12 @@ import de.appplant.cordova.plugin.notification.Builder;
import de.appplant.cordova.plugin.notification.Manager; import de.appplant.cordova.plugin.notification.Manager;
import de.appplant.cordova.plugin.notification.Notification; import de.appplant.cordova.plugin.notification.Notification;
import de.appplant.cordova.plugin.notification.Options; import de.appplant.cordova.plugin.notification.Options;
import de.appplant.cordova.plugin.notification.Request;
import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver; import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver;
import static android.content.Context.POWER_SERVICE; import static android.content.Context.POWER_SERVICE;
import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.LOLLIPOP; import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY;
/** /**
* The alarm receiver is triggered when a scheduled alarm is fired. This class * The alarm receiver is triggered when a scheduled alarm is fired. This class
...@@ -57,10 +56,11 @@ public class TriggerReceiver extends AbstractTriggerReceiver { ...@@ -57,10 +56,11 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false); boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
Context context = notification.getContext(); Context context = notification.getContext();
Options options = notification.getOptions(); Options options = notification.getOptions();
Manager manager = Manager.getInstance(context);
int badge = options.getBadgeNumber(); int badge = options.getBadgeNumber();
if (badge > 0) { if (badge > 0) {
Manager.getInstance(context).setBadge(badge); manager.setBadge(badge);
} }
if (options.shallWakeUp()) { if (options.shallWakeUp()) {
...@@ -69,6 +69,10 @@ public class TriggerReceiver extends AbstractTriggerReceiver { ...@@ -69,6 +69,10 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
notification.show(); notification.show();
if (options.isInfiniteTrigger()) {
manager.schedule(new Request(options), this.getClass());
}
if (!isUpdate) { if (!isUpdate) {
LocalNotification.fireEvent("trigger", notification); LocalNotification.fireEvent("trigger", notification);
} }
...@@ -95,7 +99,7 @@ public class TriggerReceiver extends AbstractTriggerReceiver { ...@@ -95,7 +99,7 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
wakeLock.acquire(1000); wakeLock.acquire(1000);
if (SDK_INT >= LOLLIPOP) { if (SDK_INT >= LOLLIPOP) {
wakeLock.release(RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY); wakeLock.release(PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
} else { } else {
wakeLock.release(); wakeLock.release();
} }
......
...@@ -139,7 +139,7 @@ public final class Notification { ...@@ -139,7 +139,7 @@ public final class Notification {
/** /**
* Notification type can be one of triggered or scheduled. * Notification type can be one of triggered or scheduled.
*/ */
public Type getType () { public Type getType() {
Manager mgr = Manager.getInstance(context); Manager mgr = Manager.getInstance(context);
StatusBarNotification[] toasts = mgr.getActiveNotifications(); StatusBarNotification[] toasts = mgr.getActiveNotifications();
int id = getId(); int id = getId();
...@@ -164,6 +164,8 @@ public final class Notification { ...@@ -164,6 +164,8 @@ public final class Notification {
Set<String> ids = new ArraySet<String>(); Set<String> ids = new ArraySet<String>();
AlarmManager mgr = getAlarmMgr(); AlarmManager mgr = getAlarmMgr();
cancelScheduledAlarms();
do { do {
Date date = request.getTriggerDate(); Date date = request.getTriggerDate();
...@@ -180,13 +182,17 @@ public final class Notification { ...@@ -180,13 +182,17 @@ public final class Notification {
} }
while (request.moveNext()); while (request.moveNext());
if (intents.isEmpty()) if (intents.isEmpty()) {
unpersist();
return; return;
}
persist(ids); persist(ids);
Intent last = intents.get(intents.size() - 1).second; if (!options.isInfiniteTrigger()) {
last.putExtra(Request.EXTRA_LAST, true); Intent last = intents.get(intents.size() - 1).second;
last.putExtra(Request.EXTRA_LAST, true);
}
for (Pair<Date, Intent> pair : intents) { for (Pair<Date, Intent> pair : intents) {
Date date = pair.first; Date date = pair.first;
...@@ -257,20 +263,26 @@ public final class Notification { ...@@ -257,20 +263,26 @@ public final class Notification {
/** /**
* Cancel the local notification. * Cancel the local notification.
*/
public void cancel() {
cancelScheduledAlarms();
unpersist();
getNotMgr().cancel(options.getId());
}
/**
* Cancel the scheduled future local notification.
* *
* Create an intent that looks similar, to the one that was registered * Create an intent that looks similar, to the one that was registered
* using schedule. Making sure the notification id in the action is the * using schedule. Making sure the notification id in the action is the
* same. Now we can search for such an intent using the 'getService' * same. Now we can search for such an intent using the 'getService'
* method and cancel it. * method and cancel it.
*/ */
public void cancel() { private void cancelScheduledAlarms() {
SharedPreferences prefs = getPrefs(PREF_KEY_PID); SharedPreferences prefs = getPrefs(PREF_KEY_PID);
String id = options.getIdentifier(); String id = options.getIdentifier();
Set<String> actions = prefs.getStringSet(id, null); Set<String> actions = prefs.getStringSet(id, null);
unpersist();
getNotMgr().cancel(options.getId());
if (actions == null) if (actions == null)
return; return;
......
...@@ -528,6 +528,15 @@ public final class Options { ...@@ -528,6 +528,15 @@ public final class Options {
} }
/** /**
* If the trigger shall be infinite.
*/
public boolean isInfiniteTrigger() {
JSONObject trigger = options.optJSONObject("trigger");
return trigger.has("every") && trigger.optInt("count", -1) < 0;
}
/**
* The summary for inbox style notifications. * The summary for inbox style notifications.
*/ */
String getSummary() { String getSummary() {
......
...@@ -71,7 +71,7 @@ public final class Request { ...@@ -71,7 +71,7 @@ public final class Request {
public Request(Options options) { public Request(Options options) {
this.options = options; this.options = options;
this.spec = options.getTrigger(); this.spec = options.getTrigger();
this.count = spec.optInt("count", 1); this.count = Math.max(spec.optInt("count"), 1);
this.trigger = buildTrigger(); this.trigger = buildTrigger();
this.triggerDate = trigger.getNextTriggerDate(getBaseDate()); this.triggerDate = trigger.getNextTriggerDate(getBaseDate());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment