Commit 4068dc38 by Sebastián Katzer

Support for infinite trigger on Android

parent c65e9473
......@@ -22,7 +22,6 @@
package de.appplant.cordova.plugin.localnotification;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
......@@ -30,12 +29,12 @@ import de.appplant.cordova.plugin.notification.Builder;
import de.appplant.cordova.plugin.notification.Manager;
import de.appplant.cordova.plugin.notification.Notification;
import de.appplant.cordova.plugin.notification.Options;
import de.appplant.cordova.plugin.notification.Request;
import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver;
import static android.content.Context.POWER_SERVICE;
import static android.os.Build.VERSION.SDK_INT;
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
......@@ -57,10 +56,11 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
Context context = notification.getContext();
Options options = notification.getOptions();
Manager manager = Manager.getInstance(context);
int badge = options.getBadgeNumber();
if (badge > 0) {
Manager.getInstance(context).setBadge(badge);
manager.setBadge(badge);
}
if (options.shallWakeUp()) {
......@@ -69,6 +69,10 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
notification.show();
if (options.isInfiniteTrigger()) {
manager.schedule(new Request(options), this.getClass());
}
if (!isUpdate) {
LocalNotification.fireEvent("trigger", notification);
}
......@@ -95,7 +99,7 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
wakeLock.acquire(1000);
if (SDK_INT >= LOLLIPOP) {
wakeLock.release(RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
wakeLock.release(PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
} else {
wakeLock.release();
}
......
......@@ -139,7 +139,7 @@ public final class Notification {
/**
* Notification type can be one of triggered or scheduled.
*/
public Type getType () {
public Type getType() {
Manager mgr = Manager.getInstance(context);
StatusBarNotification[] toasts = mgr.getActiveNotifications();
int id = getId();
......@@ -164,6 +164,8 @@ public final class Notification {
Set<String> ids = new ArraySet<String>();
AlarmManager mgr = getAlarmMgr();
cancelScheduledAlarms();
do {
Date date = request.getTriggerDate();
......@@ -180,13 +182,17 @@ public final class Notification {
}
while (request.moveNext());
if (intents.isEmpty())
if (intents.isEmpty()) {
unpersist();
return;
}
persist(ids);
Intent last = intents.get(intents.size() - 1).second;
last.putExtra(Request.EXTRA_LAST, true);
if (!options.isInfiniteTrigger()) {
Intent last = intents.get(intents.size() - 1).second;
last.putExtra(Request.EXTRA_LAST, true);
}
for (Pair<Date, Intent> pair : intents) {
Date date = pair.first;
......@@ -257,20 +263,26 @@ public final class 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
* using schedule. Making sure the notification id in the action is the
* same. Now we can search for such an intent using the 'getService'
* method and cancel it.
*/
public void cancel() {
private void cancelScheduledAlarms() {
SharedPreferences prefs = getPrefs(PREF_KEY_PID);
String id = options.getIdentifier();
Set<String> actions = prefs.getStringSet(id, null);
unpersist();
getNotMgr().cancel(options.getId());
if (actions == null)
return;
......
......@@ -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.
*/
String getSummary() {
......
......@@ -71,7 +71,7 @@ public final class Request {
public Request(Options options) {
this.options = options;
this.spec = options.getTrigger();
this.count = spec.optInt("count", 1);
this.count = Math.max(spec.optInt("count"), 1);
this.trigger = buildTrigger();
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