Commit 9ad2292b by Sebastián Katzer

Each LocalNotificationOptions object needs to be created either with an intent…

Each LocalNotificationOptions object needs to be created either with an intent or context to get informed about the package name. The `parse` method must be called to read the options passed through javascript.
parent 39b26a6d
...@@ -18,6 +18,7 @@ import org.json.JSONObject; ...@@ -18,6 +18,7 @@ import org.json.JSONObject;
import android.content.Intent; import android.content.Intent;
import android.content.Context; import android.content.Context;
import android.app.Activity;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
...@@ -36,10 +37,10 @@ import org.apache.cordova.CordovaWebView; ...@@ -36,10 +37,10 @@ import org.apache.cordova.CordovaWebView;
*/ */
public class LocalNotification extends CordovaPlugin { public class LocalNotification extends CordovaPlugin {
public static final String PLUGIN_NAME = "LocalNotification"; public static final String PLUGIN_NAME = "LocalNotification";
public static CordovaInterface cordova = null; public static CordovaInterface cordova = null;
public static CordovaWebView webView = null; public static CordovaWebView webView = null;
@Override @Override
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
...@@ -53,7 +54,8 @@ public class LocalNotification extends CordovaPlugin { ...@@ -53,7 +54,8 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancel")) { if (action.equalsIgnoreCase("cancel")) {
JSONObject arguments = args.getJSONObject(0); JSONObject arguments = args.getJSONObject(0);
LocalNotificationOptions options = new LocalNotificationOptions(arguments); Activity activity = cordova.getActivity();
LocalNotificationOptions options = new LocalNotificationOptions(activity).parse(arguments);
String alarmId = options.getId(); String alarmId = options.getId();
cancel(alarmId); cancel(alarmId);
...@@ -77,7 +79,8 @@ public class LocalNotification extends CordovaPlugin { ...@@ -77,7 +79,8 @@ public class LocalNotification extends CordovaPlugin {
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(new Runnable() {
public void run() { public void run() {
JSONObject arguments = args.optJSONObject(0); JSONObject arguments = args.optJSONObject(0);
LocalNotificationOptions options = new LocalNotificationOptions(arguments); Activity activity = cordova.getActivity();
LocalNotificationOptions options = new LocalNotificationOptions(activity).parse(arguments);
persist(options.getId(), args); persist(options.getId(), args);
add(options); add(options);
......
...@@ -14,6 +14,7 @@ import java.util.Date; ...@@ -14,6 +14,7 @@ import java.util.Date;
import org.json.JSONObject; import org.json.JSONObject;
import android.app.Activity;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.content.Context; import android.content.Context;
import android.media.RingtoneManager; import android.media.RingtoneManager;
...@@ -33,28 +34,18 @@ public class LocalNotificationOptions { ...@@ -33,28 +34,18 @@ public class LocalNotificationOptions {
private long interval = 0; private long interval = 0;
private long date = 0; private long date = 0;
/** LocalNotificationOptions (Activity activity) {
* Parse options passed from javascript part of this plugin. packageName = activity.getPackageName();
*/
LocalNotificationOptions (JSONObject options) {
packageName = LocalNotification.cordova.getActivity().getPackageName();
parse(options);
} }
/** LocalNotificationOptions (Context context) {
* Parse options passed from javascript part of this plugin.
*/
LocalNotificationOptions (JSONObject options, Context context) {
packageName = context.getPackageName(); packageName = context.getPackageName();
parse(options);
} }
/** /**
* Parst die übergebenen Eigenschaften. * Parst die übergebenen Eigenschaften.
*/ */
private void parse (JSONObject options) { public LocalNotificationOptions parse (JSONObject options) {
String repeat = options.optString("repeat"); String repeat = options.optString("repeat");
this.options = options; this.options = options;
...@@ -70,6 +61,8 @@ public class LocalNotificationOptions { ...@@ -70,6 +61,8 @@ public class LocalNotificationOptions {
} else if (repeat.equalsIgnoreCase("yearly")) { } else if (repeat.equalsIgnoreCase("yearly")) {
interval = AlarmManager.INTERVAL_DAY*365; interval = AlarmManager.INTERVAL_DAY*365;
} }
return this;
} }
/** /**
......
...@@ -47,7 +47,7 @@ public class LocalNotificationReceiver extends BroadcastReceiver { ...@@ -47,7 +47,7 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
try { try {
args = new JSONObject(bundle.getString(OPTIONS)); args = new JSONObject(bundle.getString(OPTIONS));
options = new LocalNotificationOptions(args, context); options = new LocalNotificationOptions(context).parse(args);
} catch (JSONException e) {} } catch (JSONException e) {}
this.context = context; this.context = context;
......
...@@ -48,7 +48,7 @@ public class LocalNotificationRestore extends BroadcastReceiver { ...@@ -48,7 +48,7 @@ public class LocalNotificationRestore extends BroadcastReceiver {
try { try {
args = new JSONObject(alarms.getString(alarmId, "")); args = new JSONObject(alarms.getString(alarmId, ""));
options = new LocalNotificationOptions(args, context); options = new LocalNotificationOptions(context).parse(args);
LocalNotification.add(options); LocalNotification.add(options);
} catch (JSONException e) {} } catch (JSONException e) {}
......
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