Commit c967ea57 by Sebastián Katzer

Add back all querry methods on Android

parent be881a56
...@@ -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);
} }
} }
...@@ -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