Commit c967ea57 by Sebastián Katzer

Add back all querry methods on Android

parent be881a56
...@@ -35,6 +35,7 @@ import org.json.JSONException; ...@@ -35,6 +35,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
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;
...@@ -42,6 +43,9 @@ import de.appplant.cordova.plugin.notification.Options; ...@@ -42,6 +43,9 @@ import de.appplant.cordova.plugin.notification.Options;
import de.appplant.cordova.plugin.notification.Request; import de.appplant.cordova.plugin.notification.Request;
import de.appplant.cordova.plugin.notification.action.ActionGroup; import de.appplant.cordova.plugin.notification.action.ActionGroup;
import static de.appplant.cordova.plugin.notification.Notification.Type.SCHEDULED;
import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERED;
/** /**
* This plugin utilizes the Android AlarmManager in combination with local * This plugin utilizes the Android AlarmManager in combination with local
* notifications. When a local notification is scheduled the alarm manager takes * notifications. When a local notification is scheduled the alarm manager takes
...@@ -157,58 +161,46 @@ public class LocalNotification extends CordovaPlugin { ...@@ -157,58 +161,46 @@ public class LocalNotification extends CordovaPlugin {
// update(args); // update(args);
// command.success(); // command.success();
// } else // } else
// if (action.equals("cancel")) { if (action.equals("cancel")) {
// cancel(args); cancel(args);
// command.success(); command.success();
// } else } else
// if (action.equals("cancelAll")) { if (action.equals("cancelAll")) {
// cancelAll(); cancelAll();
// command.success(); command.success();
// } else } else
// if (action.equals("clear")) { if (action.equals("clear")) {
// clear(args); clear(args);
// command.success(); command.success();
// } else } else
if (action.equals("clearAll")) { if (action.equals("clearAll")) {
clearAll(); clearAll();
command.success(); command.success();
} else
if (action.equals("type")) {
type(args.optInt(0), command);
} else
if (action.equals("ids")) {
ids(command);
} else
if (action.equals("scheduledIds")) {
scheduledIds(command);
} else
if (action.equals("triggeredIds")) {
triggeredIds(command);
} else
if (action.equals("notification")) {
notification(args.optInt(0), command);
} else
if (action.equals("notifications")) {
notifications(args, command);
} else
if (action.equals("scheduledNotifications")) {
scheduledNotifications(command);
} else
if (action.equals("triggeredNotifications")) {
triggeredNotifications(command);
} }
// if (action.equals("isPresent")) {
// isPresent(args.optInt(0), command);
// } else
// if (action.equals("isScheduled")) {
// isScheduled(args.optInt(0), command);
// } else
// if (action.equals("isTriggered")) {
// isTriggered(args.optInt(0), command);
// } else
// if (action.equals("getAllIds")) {
// getAllIds(command);
// } else
// if (action.equals("getScheduledIds")) {
// getScheduledIds(command);
// } else
// if (action.equals("getTriggeredIds")) {
// getTriggeredIds(command);
// } else
// if (action.equals("getSingle")) {
// getSingle(args, command);
// } else
// if (action.equals("getSingleScheduled")) {
// getSingleScheduled(args, command);
// } else
// if (action.equals("getSingleTriggered")) {
// getSingleTriggered(args, command);
// } else
// if (action.equals("getAll")) {
// getAll(args, command);
// } else
// if (action.equals("getScheduled")) {
// getScheduled(args, command);
// } else
// if (action.equals("getTriggered")) {
// getTriggered(args, command);
// }
} }
}); });
...@@ -292,9 +284,11 @@ public class LocalNotification extends CordovaPlugin { ...@@ -292,9 +284,11 @@ public class LocalNotification extends CordovaPlugin {
Notification notification = Notification notification =
mgr.schedule(request, TriggerReceiver.class); mgr.schedule(request, TriggerReceiver.class);
if (notification != null) {
fireEvent("add", notification); fireEvent("add", notification);
} }
} }
}
// /** // /**
// * Update multiple local notifications. // * Update multiple local notifications.
...@@ -317,53 +311,51 @@ public class LocalNotification extends CordovaPlugin { ...@@ -317,53 +311,51 @@ public class LocalNotification extends CordovaPlugin {
// } // }
// } // }
// /** /**
// * Cancel multiple local notifications. * Cancel multiple local notifications.
// * *
// * @param ids * @param ids Set of local notification IDs
// * Set of local notification IDs */
// */ private void cancel (JSONArray ids) {
// private void cancel (JSONArray ids) { for (int i = 0; i < ids.length(); i++) {
// for (int i = 0; i < ids.length(); i++) { int id = ids.optInt(i, 0);
// int id = ids.optInt(i, 0);
// Notification notification = Notification notification =
// getNotMgr().cancel(id); getNotMgr().cancel(id);
// if (notification == null) if (notification == null)
// continue; continue;
// fireEvent("cancel", notification); fireEvent("cancel", notification);
// } }
// } }
// /** /**
// * Cancel all scheduled notifications. * Cancel all scheduled notifications.
// */ */
// private void cancelAll() { private void cancelAll() {
// getNotMgr().cancelAll(); getNotMgr().cancelAll();
// fireEvent("cancelall"); fireEvent("cancelall");
// } }
// /** /**
// * Clear multiple local notifications without canceling them. * Clear multiple local notifications without canceling them.
// * *
// * @param ids * @param ids Set of local notification IDs
// * Set of local notification IDs */
// */ private void clear(JSONArray ids){
// private void clear(JSONArray ids){ for (int i = 0; i < ids.length(); i++) {
// for (int i = 0; i < ids.length(); i++) { int id = ids.optInt(i, 0);
// int id = ids.optInt(i, 0);
// Notification notification = Notification notification =
// getNotMgr().clear(id); getNotMgr().clear(id);
// if (notification == null) if (notification == null)
// continue; continue;
// fireEvent("clear", notification); fireEvent("clear", notification);
// } }
// } }
/** /**
* Clear all triggered notifications without canceling them. * Clear all triggered notifications without canceling them.
...@@ -373,222 +365,124 @@ public class LocalNotification extends CordovaPlugin { ...@@ -373,222 +365,124 @@ public class LocalNotification extends CordovaPlugin {
fireEvent("clearall"); fireEvent("clearall");
} }
// /** /**
// * If a notification with an ID is present. * Get the type of the notification (unknown, scheduled, triggered).
// * *
// * @param id * @param id The ID of the notification to check.
// * Notification ID * @param command The callback context used when calling back into
// * @param command * JavaScript.
// * The callback context used when calling back into JavaScript. */
// */ private void type (int id, CallbackContext command) {
// private void isPresent (int id, CallbackContext command) { Notification toast = getNotMgr().get(id);
// boolean exist = getNotMgr().exist(id);
// PluginResult result = new PluginResult(
// PluginResult.Status.OK, exist);
// command.sendPluginResult(result);
// }
// /**
// * If a notification with an ID is scheduled.
// *
// * @param id
// * Notification ID
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void isScheduled (int id, CallbackContext command) {
// boolean exist = getNotMgr().exist(
// id, Notification.Type.SCHEDULED);
// PluginResult result = new PluginResult(
// PluginResult.Status.OK, exist);
// command.sendPluginResult(result);
// }
// /**
// * If a notification with an ID is triggered.
// *
// * @param id
// * Notification ID
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void isTriggered (int id, CallbackContext command) {
// boolean exist = getNotMgr().exist(
// id, Notification.Type.TRIGGERED);
// PluginResult result = new PluginResult(
// PluginResult.Status.OK, exist);
// command.sendPluginResult(result);
// }
// /**
// * Set of IDs from all existent notifications.
// *
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getAllIds (CallbackContext command) {
// List<Integer> ids = getNotMgr().getIds();
// command.success(new JSONArray(ids));
// }
// /**
// * Set of IDs from all scheduled notifications.
// *
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getScheduledIds (CallbackContext command) {
// List<Integer> ids = getNotMgr().getIdsByType(
// Notification.Type.SCHEDULED);
// command.success(new JSONArray(ids));
// }
// /**
// * Set of IDs from all triggered notifications.
// *
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getTriggeredIds (CallbackContext command) {
// List<Integer> ids = getNotMgr().getIdsByType(
// Notification.Type.TRIGGERED);
// command.success(new JSONArray(ids));
// }
// /**
// * Options from local notification.
// *
// * @param ids
// * Set of local notification IDs
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getSingle (JSONArray ids, CallbackContext command) {
// getOptions(ids.optString(0), Notification.Type.ALL, command);
// }
// /**
// * Options from scheduled notification.
// *
// * @param ids
// * Set of local notification IDs
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getSingleScheduled (JSONArray ids, CallbackContext command) {
// getOptions(ids.optString(0), Notification.Type.SCHEDULED, command);
// }
// /**
// * Options from triggered notification.
// *
// * @param ids
// * Set of local notification IDs
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getSingleTriggered (JSONArray ids, CallbackContext command) {
// getOptions(ids.optString(0), Notification.Type.TRIGGERED, command);
// }
// /** if (toast == null) {
// * Set of options from local notification. command.success("unknown");
// * return;
// * @param ids }
// * Set of local notification IDs
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getAll (JSONArray ids, CallbackContext command) {
// getOptions(ids, Notification.Type.ALL, command);
// }
// /** switch (toast.getType()) {
// * Set of options from scheduled notifications. case SCHEDULED:
// * command.success("scheduled");
// * @param ids break;
// * Set of local notification IDs case TRIGGERED:
// * @param command command.success("triggered");
// * The callback context used when calling back into JavaScript. break;
// */ default:
// private void getScheduled (JSONArray ids, CallbackContext command) { command.success("unknown");
// getOptions(ids, Notification.Type.SCHEDULED, command); break;
// } }
}
// /** /**
// * Set of options from triggered notifications. * Set of IDs from all existent notifications.
// * *
// * @param ids * @param command The callback context used when calling back into
// * Set of local notification IDs * JavaScript.
// * @param command */
// * The callback context used when calling back into JavaScript. private void ids (CallbackContext command) {
// */ List<Integer> ids = getNotMgr().getIds();
// private void getTriggered (JSONArray ids, CallbackContext command) { command.success(new JSONArray(ids));
// getOptions(ids, Notification.Type.TRIGGERED, command); }
// }
// /** /**
// * Options from local notification. * Set of IDs from all scheduled notifications.
// * *
// * @param id * @param command The callback context used when calling back into
// * Set of local notification IDs * JavaScript.
// * @param type */
// * The local notification life cycle type private void scheduledIds (CallbackContext command) {
// * @param command List<Integer> ids = getNotMgr().getIdsByType(SCHEDULED);
// * The callback context used when calling back into JavaScript. command.success(new JSONArray(ids));
// */ }
// private void getOptions (String id, Notification.Type type,
// CallbackContext command) {
// JSONArray ids = new JSONArray().put(id); /**
// PluginResult result; * Set of IDs from all triggered notifications.
*
* @param command The callback context used when calling back into
* JavaScript.
*/
private void triggeredIds (CallbackContext command) {
List<Integer> ids = getNotMgr().getIdsByType(TRIGGERED);
command.success(new JSONArray(ids));
}
// List<JSONObject> options = /**
// getNotMgr().getOptionsBy(type, toList(ids)); * Options from local notification.
*
* @param id The ID of the notification.
* @param command The callback context used when calling back into
* JavaScript.
*/
private void notification (int id, CallbackContext command) {
Options options = getNotMgr().getOptions(id);
// if (options.isEmpty()) { if (options != null) {
// // Status.NO_RESULT led to no callback invocation :( command.success(options.getDict());
// // Status.OK led to no NPE and crash } else {
// result = new PluginResult(PluginResult.Status.NO_RESULT); command.success();
// } else { }
// result = new PluginResult(PluginResult.Status.OK, options.get(0)); }
// }
// command.sendPluginResult(result); /**
// } * Set of options from local notification.
*
* @param ids Set of local notification IDs.
* @param command The callback context used when calling back into
* JavaScript.
*/
private void notifications (JSONArray ids, CallbackContext command) {
List<JSONObject> options;
// /** if (ids.length() == 0) {
// * Set of options from local notifications. options = getNotMgr().getOptions();
// * } else {
// * @param ids options = getNotMgr().getOptionsById(toList(ids));
// * Set of local notification IDs }
// * @param type
// * The local notification life cycle type
// * @param command
// * The callback context used when calling back into JavaScript.
// */
// private void getOptions (JSONArray ids, Notification.Type type,
// CallbackContext command) {
// List<JSONObject> options; command.success(new JSONArray(options));
}
// if (ids.length() == 0) { /**
// options = getNotMgr().getOptionsByType(type); * Set of options from scheduled notifications.
// } else { *
// options = getNotMgr().getOptionsBy(type, toList(ids)); * @param command The callback context used when calling back into
// } * JavaScript.
*/
private void scheduledNotifications (CallbackContext command) {
List<JSONObject> options = getNotMgr().getOptionsByType(SCHEDULED);
command.success(new JSONArray(options));
}
// command.success(new JSONArray(options)); /**
// } * Set of options from triggered notifications.
*
* @param command The callback context used when calling back into
* JavaScript.
*/
private void triggeredNotifications (CallbackContext command) {
List<JSONObject> options = getNotMgr().getOptionsByType(TRIGGERED);
command.success(new JSONArray(options));
}
/** /**
* Call all pending callbacks after the deviceready event has been fired. * Call all pending callbacks after the deviceready event has been fired.
...@@ -680,21 +574,20 @@ public class LocalNotification extends CordovaPlugin { ...@@ -680,21 +574,20 @@ public class LocalNotification extends CordovaPlugin {
}); });
} }
// /** /**
// * Convert JSON array of integers to List. * Convert JSON array of integers to List.
// * *
// * @param ary * @param ary Array of integers.
// * Array of integers */
// */ private List<Integer> toList (JSONArray ary) {
// private List<Integer> toList (JSONArray ary) { List<Integer> list = new ArrayList<Integer>();
// ArrayList<Integer> list = new ArrayList<Integer>();
// for (int i = 0; i < ary.length(); i++) { for (int i = 0; i < ary.length(); i++) {
// list.add(ary.optInt(i)); list.add(ary.optInt(i));
// } }
// return list; return list;
// } }
/** /**
* Notification manager instance. * Notification manager instance.
......
...@@ -35,6 +35,9 @@ import java.util.Random; ...@@ -35,6 +35,9 @@ import java.util.Random;
import de.appplant.cordova.plugin.notification.action.Action; import de.appplant.cordova.plugin.notification.action.Action;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
/** /**
* Builder class for local notifications. Build fully configured local * Builder class for local notifications. Build fully configured local
* notification specified by JSON object passed from JS side. * notification specified by JSON object passed from JS side.
...@@ -47,6 +50,9 @@ public final class Builder { ...@@ -47,6 +50,9 @@ public final class Builder {
// Notification options passed by JS // Notification options passed by JS
private final Options options; private final Options options;
// To generate unique request codes
private final Random random = new Random();
// Receiver to handle the clear event // Receiver to handle the clear event
private Class<?> clearReceiver; private Class<?> clearReceiver;
...@@ -283,8 +289,10 @@ public final class Builder { ...@@ -283,8 +289,10 @@ public final class Builder {
.setAction(options.getIdentifier()) .setAction(options.getIdentifier())
.putExtra(Notification.EXTRA_ID, options.getId()); .putExtra(Notification.EXTRA_ID, options.getId());
int reqCode = random.nextInt();
PendingIntent deleteIntent = PendingIntent.getBroadcast( PendingIntent deleteIntent = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); context, reqCode, intent, FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(deleteIntent); builder.setDeleteIntent(deleteIntent);
} }
...@@ -307,10 +315,10 @@ public final class Builder { ...@@ -307,10 +315,10 @@ public final class Builder {
.putExtra(Options.EXTRA_LAUNCH, options.isLaunchingApp()) .putExtra(Options.EXTRA_LAUNCH, options.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
int reqCode = new Random().nextInt(); int reqCode = random.nextInt();
PendingIntent contentIntent = PendingIntent.getActivity( PendingIntent contentIntent = PendingIntent.getActivity(
context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); context, reqCode, intent, FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent); builder.setContentIntent(contentIntent);
} }
...@@ -354,10 +362,10 @@ public final class Builder { ...@@ -354,10 +362,10 @@ public final class Builder {
.putExtra(Options.EXTRA_LAUNCH, action.isLaunchingApp()) .putExtra(Options.EXTRA_LAUNCH, action.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
int reqCode = new Random().nextInt(); int reqCode = random.nextInt();
return PendingIntent.getActivity( return PendingIntent.getActivity(
context, reqCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); context, reqCode, intent, FLAG_CANCEL_CURRENT);
} }
} }
...@@ -25,17 +25,23 @@ import android.app.NotificationChannel; ...@@ -25,17 +25,23 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.service.notification.StatusBarNotification;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import de.appplant.cordova.plugin.badge.BadgeImpl; import de.appplant.cordova.plugin.badge.BadgeImpl;
import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O; import static android.os.Build.VERSION_CODES.O;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT; import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;
import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY; import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID;
import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERED;
/** /**
* Central way to access all or single local notifications set by specific * Central way to access all or single local notifications set by specific
...@@ -142,262 +148,202 @@ public final class Manager { ...@@ -142,262 +148,202 @@ public final class Manager {
// return schedule(options, receiver); // return schedule(options, receiver);
// } // }
// /** /**
// * Clear local notification specified by ID. * Clear local notification specified by ID.
// * *
// * @param id The notification ID. * @param id The notification ID.
// */ */
// public Notification clear (int id) { public Notification clear (int id) {
// Notification notification = get(id); Notification toast = get(id);
// if (notification != null) { if (toast != null) {
// notification.clear(); toast.clear();
// } }
// return notification; return toast;
// } }
/** /**
* Clear all local notifications. * Clear all local notifications.
*/ */
public void clearAll () { public void clearAll () {
List<Notification> toasts = getByType(TRIGGERED);
for (Notification toast : toasts) {
toast.clear();
}
getNotCompMgr().cancelAll(); getNotCompMgr().cancelAll();
setBadge(0); setBadge(0);
} }
// /** /**
// * Clear local notification specified by ID. * Clear local notification specified by ID.
// * *
// * @param id * @param id The notification ID
// * The notification ID */
// */ public Notification cancel (int id) {
// public Notification cancel (int id) { Notification toast = get(id);
// Notification notification = get(id);
// if (notification != null) {
// notification.cancel();
// }
// return notification;
// }
// /**
// * Cancel all local notifications.
// */
// public void cancelAll () {
// List<Notification> notifications = getAll();
// for (Notification notification : notifications) {
// notification.cancel();
// }
// getNotCompMgr().cancelAll();
// }
// /**
// * All local notifications IDs.
// */
// public List<Integer> getIds() {
// Set<String> keys = getPrefs().getAll().keySet();
// ArrayList<Integer> ids = new ArrayList<Integer>();
// for (String key : keys) {
// try {
// ids.add(Integer.parseInt(key));
// } catch (NumberFormatException e) {
// e.printStackTrace();
// }
// }
// return ids;
// }
// /** if (toast != null) {
// * All local notification IDs for given type. toast.cancel();
// * }
// * @param type
// * The notification life cycle type
// */
// public List<Integer> getIdsByType(Notification.Type type) {
// List<Notification> notifications = getAll();
// ArrayList<Integer> ids = new ArrayList<Integer>();
// for (Notification notification : notifications) { return toast;
// if (notification.getType() == type) { }
// ids.add(notification.getId());
// }
// }
// return ids; /**
// } * Cancel all local notifications.
*/
public void cancelAll () {
List<Notification> notifications = getAll();
// /** for (Notification notification : notifications) {
// * List of local notifications with matching ID. notification.cancel();
// * }
// * @param ids
// * Set of notification IDs
// */
// public List<Notification> getByIds(List<Integer> ids) {
// ArrayList<Notification> notifications = new ArrayList<Notification>();
// for (int id : ids) { getNotCompMgr().cancelAll();
// Notification notification = get(id); setBadge(0);
}
// if (notification != null) { /**
// notifications.add(notification); * All local notifications IDs.
// } */
// } public List<Integer> getIds() {
Set<String> keys = getPrefs().getAll().keySet();
List<Integer> ids = new ArrayList<Integer>();
// return notifications; for (String key : keys) {
// } try {
ids.add(Integer.parseInt(key));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
// /** return ids;
// * List of all local notification. }
// */
// public List<Notification> getAll() {
// return getByIds(getIds());
// }
// /** /**
// * List of local notifications from given type. * All local notification IDs for given type.
// * *
// * @param type * @param type The notification life cycle type
// * The notification life cycle type */
// */ public List<Integer> getIdsByType(Notification.Type type) {
// public List<Notification> getByType(Notification.Type type) {
// List<Notification> notifications = getAll();
// ArrayList<Notification> list = new ArrayList<Notification>();
// if (type == Notification.Type.ALL) if (type == Notification.Type.ALL)
// return notifications; return getIds();
// for (Notification notification : notifications) { StatusBarNotification[] activeToasts = getNotMgr().getActiveNotifications();
// if (notification.getType() == type) { List<Integer> activeIds = new ArrayList<Integer>();
// list.add(notification);
// }
// }
// return list; for (StatusBarNotification toast : activeToasts) {
// } activeIds.add(toast.getId());
}
// /** if (type == TRIGGERED)
// * List of local notifications with matching ID from given type. return activeIds;
// *
// * @param type
// * The notification life cycle type
// * @param ids
// * Set of notification IDs
// */
// @SuppressWarnings("UnusedDeclaration")
// public List<Notification> getBy(Notification.Type type, List<Integer> ids) {
// ArrayList<Notification> notifications = new ArrayList<Notification>();
// for (int id : ids) { List<Integer> ids = getIds();
// Notification notification = get(id); ids.removeAll(activeIds);
// if (notification != null && notification.isScheduled()) { return ids;
// notifications.add(notification); }
// }
// }
// return notifications; /**
// } * List of local notifications with matching ID.
*
* @param ids Set of notification IDs.
*/
private List<Notification> getByIds(List<Integer> ids) {
List<Notification> toasts = new ArrayList<Notification>();
// /** for (int id : ids) {
// * If a notification with an ID exists. Notification toast = get(id);
// *
// * @param id
// * Notification ID
// */
// public boolean exist (int id) {
// return get(id) != null;
// }
// /** if (toast != null) {
// * If a notification with an ID and type exists. toasts.add(toast);
// * }
// * @param id }
// * Notification ID
// * @param type
// * Notification type
// */
// public boolean exist (int id, Notification.Type type) {
// Notification notification = get(id);
// return notification != null && notification.getType() == type; return toasts;
// } }
// /** /**
// * List of properties from all local notifications. * List of all local notification.
// */ */
// public List<JSONObject> getOptions() { public List<Notification> getAll() {
// return getOptionsById(getIds()); return getByIds(getIds());
// } }
// /** /**
// * List of properties from local notifications with matching ID. * List of local notifications from given type.
// * *
// * @param ids * @param type The notification life cycle type
// * Set of notification IDs */
// */ private List<Notification> getByType(Notification.Type type) {
// public List<JSONObject> getOptionsById(List<Integer> ids) {
// ArrayList<JSONObject> options = new ArrayList<JSONObject>();
// for (int id : ids) { if (type == Notification.Type.ALL)
// Notification notification = get(id); return getAll();
List<Integer> ids = getIdsByType(type);
// if (notification != null) { return getByIds(ids);
// options.add(notification.getOptions().getDict()); }
// }
// }
// return options; /**
// } * If a notification with an ID exists.
*
* @param id Notification ID
*
* @return true if found.
*/
public boolean exist (int id) {
return get(id) != null;
}
// /** /**
// * List of properties from all local notifications from given type. * List of properties from all local notifications.
// * */
// * @param type public List<JSONObject> getOptions() {
// * The notification life cycle type return getOptionsById(getIds());
// */ }
// public List<JSONObject> getOptionsByType(Notification.Type type) {
// ArrayList<JSONObject> options = new ArrayList<JSONObject>();
// List<Notification> notifications = getByType(type);
// for (Notification notification : notifications) { /**
// options.add(notification.getOptions().getDict()); * List of properties from local notifications with matching ID.
// } *
* @param ids Set of notification IDs
*/
public List<JSONObject> getOptionsById(List<Integer> ids) {
List<JSONObject> toasts = new ArrayList<JSONObject>();
// return options; for (int id : ids) {
// } Options options = getOptions(id);
// /** if (options != null) {
// * List of properties from local notifications with matching ID from toasts.add(options.getDict());
// * given type. }
// * }
// * @param type
// * The notification life cycle type
// * @param ids
// * Set of notification IDs
// */
// public List<JSONObject> getOptionsBy(Notification.Type type,
// List<Integer> ids) {
// if (type == Notification.Type.ALL) return toasts;
// return getOptionsById(ids); }
// ArrayList<JSONObject> options = new ArrayList<JSONObject>(); /**
// List<Notification> notifications = getByIds(ids); * List of properties from all local notifications from given type.
*
* @param type
* The notification life cycle type
*/
public List<JSONObject> getOptionsByType(Notification.Type type) {
ArrayList<JSONObject> options = new ArrayList<JSONObject>();
List<Notification> notifications = getByType(type);
// for (Notification notification : notifications) { for (Notification notification : notifications) {
// if (notification.getType() == type) { options.add(notification.getOptions().getDict());
// options.add(notification.getOptions().getDict()); }
// }
// }
// return options; return options;
// } }
/** /**
* Get local notification options. * Get local notification options.
...@@ -481,7 +427,7 @@ public final class Manager { ...@@ -481,7 +427,7 @@ public final class Manager {
* Shared private preferences for the application. * Shared private preferences for the application.
*/ */
private SharedPreferences getPrefs () { private SharedPreferences getPrefs () {
return context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE); return context.getSharedPreferences(PREF_KEY_ID, Context.MODE_PRIVATE);
} }
/** /**
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
package de.appplant.cordova.plugin.notification; package de.appplant.cordova.plugin.notification;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
...@@ -30,6 +29,7 @@ import android.content.Context; ...@@ -30,6 +29,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.service.notification.StatusBarNotification;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.util.ArraySet; import android.support.v4.util.ArraySet;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
...@@ -63,7 +63,10 @@ public final class Notification { ...@@ -63,7 +63,10 @@ public final class Notification {
public static final String EXTRA_ID = "NOTIFICATION_ID"; public static final String EXTRA_ID = "NOTIFICATION_ID";
// Key for private preferences // Key for private preferences
static final String PREF_KEY = "LocalNotification"; static final String PREF_KEY_ID = "NOTIFICATION_ID";
// Key for private preferences
private static final String PREF_KEY_PID = "NOTIFICATION_PID";
// Application context passed by constructor // Application context passed by constructor
private final Context context; private final Context context;
...@@ -127,42 +130,21 @@ public final class Notification { ...@@ -127,42 +130,21 @@ public final class Notification {
return getOptions().getTrigger().has("every"); return getOptions().getTrigger().has("every");
} }
// /** /**
// * If the notification is scheduled. * Notification type can be one of triggered or scheduled.
// */ */
// public boolean isScheduled () { public Type getType () {
// return isRepeating() || !wasInThePast(); StatusBarNotification[] toasts = getNotMgr().getActiveNotifications();
// } int id = getId();
// /** for (StatusBarNotification toast : toasts) {
// * If the notification is triggered. if (toast.getId() == id) {
// */ return Type.TRIGGERED;
// public boolean isTriggered () { }
// return wasInThePast(); }
// }
return Type.SCHEDULED;
// /** }
// * If the notification is an update.
// *
// * @param keepFlag
// * Set to false to remove the flag from the option map
// */
// protected boolean isUpdate (boolean keepFlag) {
// boolean updated = options.getDict().optBoolean("updated", false);
// if (!keepFlag) {
// options.getDict().remove("updated");
// }
// return updated;
// }
// /**
// * Notification type can be one of pending or scheduled.
// */
// public Type getType () {
// return isScheduled() ? Type.SCHEDULED : Type.TRIGGERED;
// }
/** /**
* Schedule the local notification. * Schedule the local notification.
...@@ -182,10 +164,11 @@ public final class Notification { ...@@ -182,10 +164,11 @@ public final class Notification {
continue; continue;
Intent intent = new Intent(context, receiver) Intent intent = new Intent(context, receiver)
.setAction(PREF_KEY + "#" + request.getIdentifier()) .setAction(PREF_KEY_ID + request.getIdentifier())
.putExtra(Notification.EXTRA_ID, options.getId()) .putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Request.EXTRA_OCCURRENCE, request.getOccurrence()); .putExtra(Request.EXTRA_OCCURRENCE, request.getOccurrence());
ids.add(intent.getAction());
intents.add(new Pair<Date, Intent>(date, intent)); intents.add(new Pair<Date, Intent>(date, intent));
} }
while (request.moveNext()); while (request.moveNext());
...@@ -193,6 +176,8 @@ public final class Notification { ...@@ -193,6 +176,8 @@ public final class Notification {
if (intents.isEmpty()) if (intents.isEmpty())
return; return;
persist(ids);
Intent last = intents.get(intents.size() - 1).second; Intent last = intents.get(intents.size() - 1).second;
last.putExtra(Request.EXTRA_LAST, true); last.putExtra(Request.EXTRA_LAST, true);
...@@ -219,14 +204,11 @@ public final class Notification { ...@@ -219,14 +204,11 @@ public final class Notification {
mgr.setExact(RTC_WAKEUP, time, pi); mgr.setExact(RTC_WAKEUP, time, pi);
break; break;
} }
ids.add(intent.getAction());
} catch (Exception ignore) { } catch (Exception ignore) {
// Samsung devices have a known bug where a 500 alarms limit // Samsung devices have a known bug where a 500 alarms limit
// can crash the app // can crash the app
} }
} }
persist(ids);
} }
/** /**
...@@ -253,7 +235,7 @@ public final class Notification { ...@@ -253,7 +235,7 @@ public final class Notification {
/** /**
* Clear the local notification without canceling repeating alarms. * Clear the local notification without canceling repeating alarms.
*/ */
public void clear () { public void clear() {
getNotMgr().cancel(getId()); getNotMgr().cancel(getId());
if (isRepeating()) if (isRepeating())
...@@ -271,8 +253,9 @@ public final class Notification { ...@@ -271,8 +253,9 @@ public final class Notification {
* method and cancel it. * method and cancel it.
*/ */
public void cancel() { public void cancel() {
Set<String> actions = getPrefs().getStringSet( SharedPreferences prefs = getPrefs(PREF_KEY_PID);
"#" + options.getIdentifier(), null); String id = options.getIdentifier();
Set<String> actions = prefs.getStringSet(id, null);
unpersist(); unpersist();
getNotMgr().cancel(options.getId()); getNotMgr().cancel(options.getId());
...@@ -295,7 +278,7 @@ public final class Notification { ...@@ -295,7 +278,7 @@ public final class Notification {
/** /**
* Present the local notification to user. * Present the local notification to user.
*/ */
public void show () { public void show() {
if (builder == null) if (builder == null)
return; return;
...@@ -304,22 +287,6 @@ public final class Notification { ...@@ -304,22 +287,6 @@ public final class Notification {
getNotMgr().notify(getId(), builder.build()); getNotMgr().notify(getId(), builder.build());
} }
// /**
// * Count of triggers since schedule.
// */
// public int getTriggerCountSinceSchedule() {
// long now = System.currentTimeMillis();
// long triggerTime = options.getTriggerTime();
// if (!wasInThePast())
// return 0;
// if (!isRepeating())
// return 1;
// return (int) ((now - triggerTime) / options.getRepeatInterval());
// }
/** /**
* Encode options to JSON. * Encode options to JSON.
*/ */
...@@ -344,11 +311,15 @@ public final class Notification { ...@@ -344,11 +311,15 @@ public final class Notification {
* @param ids List of intent actions to persist. * @param ids List of intent actions to persist.
*/ */
private void persist (Set<String> ids) { private void persist (Set<String> ids) {
SharedPreferences.Editor editor = getPrefs().edit();
String id = options.getIdentifier(); String id = options.getIdentifier();
SharedPreferences.Editor editor;
editor = getPrefs(PREF_KEY_ID).edit();
editor.putString(id, options.toString()); editor.putString(id, options.toString());
editor.putStringSet("#" + id, ids); editor.apply();
editor = getPrefs(PREF_KEY_PID).edit();
editor.putStringSet(id, ids);
editor.apply(); editor.apply();
} }
...@@ -356,13 +327,16 @@ public final class Notification { ...@@ -356,13 +327,16 @@ public final class Notification {
* Remove the notification from the Android shared Preferences. * Remove the notification from the Android shared Preferences.
*/ */
private void unpersist () { private void unpersist () {
SharedPreferences.Editor editor = getPrefs().edit(); String[] keys = { PREF_KEY_ID, PREF_KEY_PID };
String id = options.getIdentifier(); String id = options.getIdentifier();
SharedPreferences.Editor editor;
for (String key : keys) {
editor = getPrefs(key).edit();
editor.remove(id); editor.remove(id);
editor.remove("#" + id);
editor.apply(); editor.apply();
} }
}
/** /**
* Since Android 7 the app will crash if an external process has no * Since Android 7 the app will crash if an external process has no
...@@ -383,8 +357,8 @@ public final class Notification { ...@@ -383,8 +357,8 @@ public final class Notification {
/** /**
* Shared private preferences for the application. * Shared private preferences for the application.
*/ */
private SharedPreferences getPrefs () { private SharedPreferences getPrefs (String key) {
return context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE); return context.getSharedPreferences(key, Context.MODE_PRIVATE);
} }
/** /**
......
...@@ -106,7 +106,7 @@ public final class Options { ...@@ -106,7 +106,7 @@ public final class Options {
/** /**
* Wrapped JSON object. * Wrapped JSON object.
*/ */
JSONObject getDict () { public JSONObject getDict() {
return options; return options;
} }
......
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