Commit a82fa397 by Sebastián Katzer

Implemented launchDetails on Android

parent 452e572b
......@@ -21,7 +21,10 @@
package de.appplant.cordova.plugin.localnotification;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.util.Pair;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
......@@ -42,9 +45,6 @@ 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.Manager;
// import de.appplant.cordova.plugin.notification.Notification;
/**
* This plugin utilizes the Android AlarmManager in combination with local
* notifications. When a local notification is scheduled the alarm manager takes
......@@ -65,6 +65,9 @@ public class LocalNotification extends CordovaPlugin {
// Queues all events before deviceready
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.
* Prefer to use pluginInitialize instead since there is no value in
......@@ -78,8 +81,7 @@ public class LocalNotification extends CordovaPlugin {
/**
* Called when the system is about to start resuming a previous activity.
*
* @param multitasking
* Flag indicating if multitasking is turned on for app
* @param multitasking Flag indicating if multitasking is turned on for app.
*/
@Override
public void onPause(boolean multitasking) {
......@@ -119,19 +121,22 @@ public class LocalNotification extends CordovaPlugin {
* To run on the UI thread, use:
* cordova.getActivity().runOnUiThread(runnable);
*
* @param action
* The action to execute.
* @param args
* The exec() arguments in JSON form.
* @param command
* The callback context used when calling back into JavaScript.
* @return
* Whether the action was valid.
* @param action The action to execute.
* @param args The exec() arguments in JSON form.
* @param command The callback context used when calling back into
* JavaScript.
*
* @return Whether the action was valid.
*/
@Override
public boolean execute (final String action, final JSONArray args,
final CallbackContext command) throws JSONException {
if (action.equals("launch")) {
launch(command);
return true;
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {
if (action.equals("ready")) {
......@@ -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.
*
* @param command The callback context used when calling back into
......@@ -600,12 +630,10 @@ public class LocalNotification extends CordovaPlugin {
* Fire given event on JS side. Does inform all event listeners.
*
* @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.
*/
static void fireEvent (String event, Notification notification,
JSONObject data) {
static void fireEvent (String event, Notification toast, JSONObject data) {
String params, js;
try {
......@@ -613,15 +641,15 @@ public class LocalNotification extends CordovaPlugin {
data.put("foreground", !isInBackground);
data.put("queued", !deviceready);
if (notification != null) {
data.put("notification", notification.getId());
if (toast != null) {
data.put("notification", toast.getId());
}
} catch (JSONException e) {
e.printStackTrace();
}
if (notification != null) {
params = notification.toString() + "," + data.toString();
if (toast != null) {
params = toast.toString() + "," + data.toString();
} else {
params = data.toString();
}
......@@ -629,13 +657,17 @@ public class LocalNotification extends CordovaPlugin {
js = "cordova.plugins.notification.local.core.fireEvent(" +
"\"" + event + "\"," + params + ")";
if (launchDetails == null && !deviceready && toast != null) {
launchDetails = new Pair<Integer, String>(toast.getId(), event);
}
sendJavascript(js);
}
/**
* 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) {
......@@ -643,18 +675,12 @@ public class LocalNotification extends CordovaPlugin {
eventQueue.add(js);
return;
}
Runnable jsLoader = new Runnable() {
((Activity)(webView.getContext())).runOnUiThread(new Runnable() {
public void run() {
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) {
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
channel.deviceready.subscribe(function () {
// Device is ready now, the listeners are registered
// and all queued events can be executed.
exports.exec('ready');
});
// Called before 'deviceready' event
channel.onCordovaReady.subscribe(function () {
// Set launchDetails object
exports.exec('launch');
// Device plugin is ready now
exports.setLaunchDetails();
channel.onCordovaInfoReady.subscribe(function () {
// Merge platform specifics into defaults
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