Explorar el Código

Support for input actions on Android

Sebastián Katzer hace 8 años
padre
commit
2ca56e6c2c

+ 1 - 1
src/android/LocalNotification.java

@@ -607,7 +607,7 @@ public class LocalNotification extends CordovaPlugin {
         String js = "cordova.plugins.notification.local.core.fireEvent(" +
                 "\"" + event + "\"," + params + ")";
 
-        sendJavascript(js);
+        // sendJavascript(js);
     }
 
     /**

+ 50 - 30
src/android/notification/Action.java

@@ -22,7 +22,9 @@
 package de.appplant.cordova.plugin.notification;
 
 import android.content.Context;
+import android.support.v4.app.RemoteInput;
 
+import org.json.JSONArray;
 import org.json.JSONObject;
 
 import de.appplant.cordova.plugin.notification.util.AssetUtil;
@@ -38,17 +40,11 @@ final class Action {
     // Key name for bundled extras
     static final String EXTRA = "NOTIFICATION_ACTION_ID";
 
-    // The ID of the action
-    private final String id;
+    // The application context
+    private final Context context;
 
-    // The title for the action
-    private final String title;
-
-    // The icon for the action
-    private final int icon;
-
-    // The launch flag for the action
-    private final boolean launch;
+    // The action spec
+    private final JSONObject options;
 
     /**
      * Structure to encapsulate a named action that can be shown as part of
@@ -58,57 +54,81 @@ final class Action {
      * @param options The action options.
      */
     Action (Context context, JSONObject options) {
-        this.icon  = parseIcon(context, options.optString("icon"));
-        this.title = options.optString("title");
-        this.id    = options.optString("id", title);
-        this.launch = options.optBoolean("launch", false);
+        this.context = context;
+        this.options = options;
     }
 
     /**
      * Gets the ID for the action.
      */
     public String getId() {
-        return id;
+        return options.optString("id", getTitle());
     }
 
     /**
      * Gets the Title for the action.
      */
     public String getTitle() {
-        return title;
+        return options.optString("title", "unknown");
     }
 
     /**
      * Gets the icon for the action.
      */
     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.
      */
     public boolean isLaunchingApp() {
-        return launch;
+        return options.optBoolean("launch", false);
     }
 
     /**
-     * Parse the uri into a resource id.
-     *
-     * @param context The application context.
-     * @param resPath The resource path.
-     *
-     * @return The resource id.
+     * Gets the type for the action.
      */
-    private int parseIcon(Context context, String resPath) {
-        AssetUtil assets = AssetUtil.getInstance(context);
-        int resId        = assets.getResId(resPath);
+    boolean isWithInput() {
+        String type = options.optString("type");
+        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();
+    }
+
+    /**
+     * 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 resId;
+        return choices;
     }
 
 }

+ 13 - 1
src/android/notification/ActionGroup.java

@@ -22,6 +22,7 @@
 package de.appplant.cordova.plugin.notification;
 
 import android.content.Context;
+import android.util.Log;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
@@ -31,6 +32,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static android.os.Build.VERSION.SDK_INT;
+import static android.os.Build.VERSION_CODES.N;
+
 public final class ActionGroup {
 
     // Default action group id
@@ -86,9 +90,17 @@ public final class ActionGroup {
 
         for (int i = 0; i < list.length(); i++) {
             JSONObject opts = list.optJSONObject(i);
+            String type     = opts.optString("type", "button");
+
+            if (type.equals("input") && SDK_INT < N) {
+                Log.w("Action", "Type input is not supported");
+                continue;
+            }
 
-            if (!opts.optString("type", "button").equals("button"))
+            if (!(type.equals("button") || type.equals("input"))) {
+                Log.w("Action", "Unknown type: " + type);
                 continue;
+            }
 
             actions.add(new Action(context, opts));
         }

+ 10 - 3
src/android/notification/Builder.java

@@ -229,14 +229,21 @@ public final class Builder {
      */
     private void applyActions (NotificationCompat.Builder builder) {
         Action[] actions = options.getActions();
+        NotificationCompat.Action.Builder btn;
 
         if (actions == null || actions.length == 0)
             return;
 
         for (Action action : actions) {
-            builder.addAction(
-                    action.getIcon(), action.getTitle(),
-                    getPendingIntentForAction(action));
+             btn = new NotificationCompat.Action.Builder(
+                     action.getIcon(), action.getTitle(),
+                     getPendingIntentForAction(action));
+
+            if (action.isWithInput()) {
+                btn.addRemoteInput(action.getInput());
+            }
+
+            builder.addAction(btn.build());
         }
     }