Commit 2ca56e6c by Sebastián Katzer

Support for input actions on Android

parent c52bd8c8
...@@ -607,7 +607,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -607,7 +607,7 @@ public class LocalNotification extends CordovaPlugin {
String js = "cordova.plugins.notification.local.core.fireEvent(" + String js = "cordova.plugins.notification.local.core.fireEvent(" +
"\"" + event + "\"," + params + ")"; "\"" + event + "\"," + params + ")";
sendJavascript(js); // sendJavascript(js);
} }
/** /**
......
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
package de.appplant.cordova.plugin.notification; package de.appplant.cordova.plugin.notification;
import android.content.Context; import android.content.Context;
import android.support.v4.app.RemoteInput;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import de.appplant.cordova.plugin.notification.util.AssetUtil; import de.appplant.cordova.plugin.notification.util.AssetUtil;
...@@ -38,17 +40,11 @@ final class Action { ...@@ -38,17 +40,11 @@ final class Action {
// Key name for bundled extras // Key name for bundled extras
static final String EXTRA = "NOTIFICATION_ACTION_ID"; static final String EXTRA = "NOTIFICATION_ACTION_ID";
// The ID of the action // The application context
private final String id; private final Context context;
// The title for the action // The action spec
private final String title; private final JSONObject options;
// The icon for the action
private final int icon;
// The launch flag for the action
private final boolean launch;
/** /**
* Structure to encapsulate a named action that can be shown as part of * Structure to encapsulate a named action that can be shown as part of
...@@ -58,57 +54,81 @@ final class Action { ...@@ -58,57 +54,81 @@ final class Action {
* @param options The action options. * @param options The action options.
*/ */
Action (Context context, JSONObject options) { Action (Context context, JSONObject options) {
this.icon = parseIcon(context, options.optString("icon")); this.context = context;
this.title = options.optString("title"); this.options = options;
this.id = options.optString("id", title);
this.launch = options.optBoolean("launch", false);
} }
/** /**
* Gets the ID for the action. * Gets the ID for the action.
*/ */
public String getId() { public String getId() {
return id; return options.optString("id", getTitle());
} }
/** /**
* Gets the Title for the action. * Gets the Title for the action.
*/ */
public String getTitle() { public String getTitle() {
return title; return options.optString("title", "unknown");
} }
/** /**
* Gets the icon for the action. * Gets the icon for the action.
*/ */
public int getIcon() { public int getIcon() {
return icon; AssetUtil assets = AssetUtil.getInstance(context);
String resPath = options.optString("icon");
int resId = assets.getResId(resPath);
if (resId == 0) {
resId = android.R.drawable.screen_background_dark;
}
return resId;
} }
/** /**
* Gets the value of the launch flag. * Gets the value of the launch flag.
*/ */
public boolean isLaunchingApp() { public boolean isLaunchingApp() {
return launch; return options.optBoolean("launch", false);
} }
/** /**
* Parse the uri into a resource id. * Gets the type for the action.
*
* @param context The application context.
* @param resPath The resource path.
*
* @return The resource id.
*/ */
private int parseIcon(Context context, String resPath) { boolean isWithInput() {
AssetUtil assets = AssetUtil.getInstance(context); String type = options.optString("type");
int resId = assets.getResId(resPath); return type.equals("input");
}
if (resId == 0) { /**
resId = android.R.drawable.screen_background_dark; * Gets the input config in case of the action is of type input.
*/
RemoteInput getInput() {
return new RemoteInput.Builder(getId())
.setLabel(options.optString("emptyText"))
.setAllowFreeFormInput(options.optBoolean("editable", true))
.setChoices(getChoices())
.build();
} }
return resId; /**
* List of possible choices for input actions.
*/
private String[] getChoices() {
JSONArray opts = options.optJSONArray("choices");
if (opts == null)
return null;
String[] choices = new String[opts.length()];
for (int i = 0; i < choices.length; i++) {
choices[i] = opts.optString(i);
}
return choices;
} }
} }
\ No newline at end of file
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
package de.appplant.cordova.plugin.notification; package de.appplant.cordova.plugin.notification;
import android.content.Context; import android.content.Context;
import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -31,6 +32,9 @@ import java.util.HashMap; ...@@ -31,6 +32,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.N;
public final class ActionGroup { public final class ActionGroup {
// Default action group id // Default action group id
...@@ -86,9 +90,17 @@ public final class ActionGroup { ...@@ -86,9 +90,17 @@ public final class ActionGroup {
for (int i = 0; i < list.length(); i++) { for (int i = 0; i < list.length(); i++) {
JSONObject opts = list.optJSONObject(i); JSONObject opts = list.optJSONObject(i);
String type = opts.optString("type", "button");
if (!opts.optString("type", "button").equals("button")) if (type.equals("input") && SDK_INT < N) {
Log.w("Action", "Type input is not supported");
continue; continue;
}
if (!(type.equals("button") || type.equals("input"))) {
Log.w("Action", "Unknown type: " + type);
continue;
}
actions.add(new Action(context, opts)); actions.add(new Action(context, opts));
} }
......
...@@ -229,14 +229,21 @@ public final class Builder { ...@@ -229,14 +229,21 @@ public final class Builder {
*/ */
private void applyActions (NotificationCompat.Builder builder) { private void applyActions (NotificationCompat.Builder builder) {
Action[] actions = options.getActions(); Action[] actions = options.getActions();
NotificationCompat.Action.Builder btn;
if (actions == null || actions.length == 0) if (actions == null || actions.length == 0)
return; return;
for (Action action : actions) { for (Action action : actions) {
builder.addAction( btn = new NotificationCompat.Action.Builder(
action.getIcon(), action.getTitle(), action.getIcon(), action.getTitle(),
getPendingIntentForAction(action)); getPendingIntentForAction(action));
if (action.isWithInput()) {
btn.addRemoteInput(action.getInput());
}
builder.addAction(btn.build());
} }
} }
......
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