Commit a82fa397 by Sebastián Katzer

Implemented launchDetails on Android

parent 452e572b
...@@ -21,7 +21,10 @@ ...@@ -21,7 +21,10 @@
package de.appplant.cordova.plugin.localnotification; package de.appplant.cordova.plugin.localnotification;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.util.Log;
import android.util.Pair;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
...@@ -42,9 +45,6 @@ import de.appplant.cordova.plugin.notification.Notification; ...@@ -42,9 +45,6 @@ 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.Request;
// import de.appplant.cordova.plugin.notification.Manager;
// import de.appplant.cordova.plugin.notification.Notification;
/** /**
* 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
...@@ -65,6 +65,9 @@ public class LocalNotification extends CordovaPlugin { ...@@ -65,6 +65,9 @@ public class LocalNotification extends CordovaPlugin {
// Queues all events before deviceready // Queues all events before deviceready
private static ArrayList<String> eventQueue = new ArrayList<String>(); private static ArrayList<String> eventQueue = new ArrayList<String>();
// Launch details
private static Pair<Integer, String> launchDetails;
/** /**
* Called after plugin construction and fields have been initialized. * Called after plugin construction and fields have been initialized.
* Prefer to use pluginInitialize instead since there is no value in * Prefer to use pluginInitialize instead since there is no value in
...@@ -78,8 +81,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -78,8 +81,7 @@ public class LocalNotification extends CordovaPlugin {
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
* *
* @param multitasking * @param multitasking Flag indicating if multitasking is turned on for app.
* Flag indicating if multitasking is turned on for app
*/ */
@Override @Override
public void onPause(boolean multitasking) { public void onPause(boolean multitasking) {
...@@ -105,7 +107,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -105,7 +107,7 @@ public class LocalNotification extends CordovaPlugin {
*/ */
@Override @Override
public void onDestroy() { public void onDestroy() {
deviceready = false; deviceready = false;
isInBackground = true; isInBackground = true;
} }
...@@ -119,19 +121,22 @@ public class LocalNotification extends CordovaPlugin { ...@@ -119,19 +121,22 @@ public class LocalNotification extends CordovaPlugin {
* To run on the UI thread, use: * To run on the UI thread, use:
* cordova.getActivity().runOnUiThread(runnable); * cordova.getActivity().runOnUiThread(runnable);
* *
* @param action * @param action The action to execute.
* The action to execute. * @param args The exec() arguments in JSON form.
* @param args * @param command The callback context used when calling back into
* The exec() arguments in JSON form. * JavaScript.
* @param command *
* The callback context used when calling back into JavaScript. * @return Whether the action was valid.
* @return
* Whether the action was valid.
*/ */
@Override @Override
public boolean execute (final String action, final JSONArray args, public boolean execute (final String action, final JSONArray args,
final CallbackContext command) throws JSONException { final CallbackContext command) throws JSONException {
if (action.equals("launch")) {
launch(command);
return true;
}
cordova.getThreadPool().execute(new Runnable() { cordova.getThreadPool().execute(new Runnable() {
public void run() { public void run() {
if (action.equals("ready")) { if (action.equals("ready")) {
...@@ -214,6 +219,31 @@ public class LocalNotification extends CordovaPlugin { ...@@ -214,6 +219,31 @@ public class LocalNotification extends CordovaPlugin {
} }
/** /**
* Set launchDetails object.
*
* @param command The callback context used when calling back into
* JavaScript.
*/
@SuppressLint("DefaultLocale")
private void launch(CallbackContext command) {
if (launchDetails == null)
return;
JSONObject details = new JSONObject();
try {
details.put("id", launchDetails.first);
details.put("action", launchDetails.second);
} catch (JSONException e) {
e.printStackTrace();
}
command.success(details);
launchDetails = null;
}
/**
* Ask if user has enabled permission for local notifications. * Ask if user has enabled permission for local notifications.
* *
* @param command The callback context used when calling back into * @param command The callback context used when calling back into
...@@ -599,13 +629,11 @@ public class LocalNotification extends CordovaPlugin { ...@@ -599,13 +629,11 @@ public class LocalNotification extends CordovaPlugin {
/** /**
* Fire given event on JS side. Does inform all event listeners. * Fire given event on JS side. Does inform all event listeners.
* *
* @param event The event name. * @param event The event name.
* @param notification Optional notification to pass with. * @param toast Optional notification to pass with.
* @param data Event object with additional data. * @param data Event object with additional data.
*/ */
static void fireEvent (String event, Notification notification, static void fireEvent (String event, Notification toast, JSONObject data) {
JSONObject data) {
String params, js; String params, js;
try { try {
...@@ -613,15 +641,15 @@ public class LocalNotification extends CordovaPlugin { ...@@ -613,15 +641,15 @@ public class LocalNotification extends CordovaPlugin {
data.put("foreground", !isInBackground); data.put("foreground", !isInBackground);
data.put("queued", !deviceready); data.put("queued", !deviceready);
if (notification != null) { if (toast != null) {
data.put("notification", notification.getId()); data.put("notification", toast.getId());
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (notification != null) { if (toast != null) {
params = notification.toString() + "," + data.toString(); params = toast.toString() + "," + data.toString();
} else { } else {
params = data.toString(); params = data.toString();
} }
...@@ -629,13 +657,17 @@ public class LocalNotification extends CordovaPlugin { ...@@ -629,13 +657,17 @@ public class LocalNotification extends CordovaPlugin {
js = "cordova.plugins.notification.local.core.fireEvent(" + js = "cordova.plugins.notification.local.core.fireEvent(" +
"\"" + event + "\"," + params + ")"; "\"" + event + "\"," + params + ")";
if (launchDetails == null && !deviceready && toast != null) {
launchDetails = new Pair<Integer, String>(toast.getId(), event);
}
sendJavascript(js); sendJavascript(js);
} }
/** /**
* Use this instead of deprecated sendJavascript * Use this instead of deprecated sendJavascript
* *
* @param js JS code snippet as string * @param js JS code snippet as string.
*/ */
private static synchronized void sendJavascript(final String js) { private static synchronized void sendJavascript(final String js) {
...@@ -643,18 +675,12 @@ public class LocalNotification extends CordovaPlugin { ...@@ -643,18 +675,12 @@ public class LocalNotification extends CordovaPlugin {
eventQueue.add(js); eventQueue.add(js);
return; return;
} }
Runnable jsLoader = new Runnable() {
((Activity)(webView.getContext())).runOnUiThread(new Runnable() {
public void run() { public void run() {
webView.loadUrl("javascript:" + js); webView.loadUrl("javascript:" + js);
} }
}; });
try {
Method post = webView.getClass().getMethod("post",Runnable.class);
post.invoke(webView,jsLoader);
} catch(Exception e) {
((Activity)(webView.getContext())).runOnUiThread(jsLoader);
}
} }
// /** // /**
......
...@@ -395,20 +395,24 @@ exports.exec = function (action, args, callback, scope) { ...@@ -395,20 +395,24 @@ exports.exec = function (action, args, callback, scope) {
exec(fn, null, 'LocalNotification', action, params); exec(fn, null, 'LocalNotification', action, params);
}; };
exports.setLaunchDetails = function () {
exports.exec('launch', null, function (details) {
if (details) {
cordova.plugins.notification.local.launchDetails = details;
}
});
};
// Called after 'deviceready' event // Called after 'deviceready' event
channel.deviceready.subscribe(function () { channel.deviceready.subscribe(function () {
// Device is ready now, the listeners are registered
// and all queued events can be executed.
exports.exec('ready'); exports.exec('ready');
}); });
// Called before 'deviceready' event // Called before 'deviceready' event
channel.onCordovaReady.subscribe(function () { channel.onCordovaReady.subscribe(function () {
// Set launchDetails object exports.setLaunchDetails();
exports.exec('launch');
// Device plugin is ready now
channel.onCordovaInfoReady.subscribe(function () { channel.onCordovaInfoReady.subscribe(function () {
// Merge platform specifics into defaults
exports.applyPlatformSpecificOptions(); exports.applyPlatformSpecificOptions();
}); });
}); });
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