ActionGroup.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. package de.appplant.cordova.plugin.notification.action;
  22. import android.content.Context;
  23. import android.util.Log;
  24. import org.json.JSONArray;
  25. import org.json.JSONObject;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import static android.os.Build.VERSION.SDK_INT;
  31. import static android.os.Build.VERSION_CODES.N;
  32. public final class ActionGroup {
  33. // Default action group id
  34. private static final String GENERAL_ACTION_GROUP = "DEFAULT_GROUP";
  35. // Saves all groups for later lookup.
  36. private static final Map<String, ActionGroup> groups =
  37. new HashMap<String, ActionGroup>();
  38. // The ID of the action group.
  39. private final String id;
  40. // List of actions
  41. private final Action[] actions;
  42. /**
  43. * Lookup the action groups with the specified group id.
  44. *
  45. * @param id The ID of the action group to find.
  46. *
  47. * @return Null if no group was found.
  48. */
  49. public static ActionGroup lookup(String id) {
  50. return groups.get(id);
  51. }
  52. /**
  53. * Register the action group for later lookup.
  54. *
  55. * @param group The action group to register.
  56. */
  57. public static void register (ActionGroup group) {
  58. if (!group.getId().equalsIgnoreCase(GENERAL_ACTION_GROUP)) {
  59. groups.put(group.getId(), group);
  60. }
  61. }
  62. /**
  63. * Creates an action group by parsing the specified action specs.
  64. *
  65. * @param spec The action group spec containing the id and list of actions.
  66. *
  67. * @return A new action group.
  68. */
  69. public static ActionGroup parse (Context context, JSONObject spec) {
  70. String id = spec.optString("actionGroupId", GENERAL_ACTION_GROUP);
  71. JSONArray list = spec.optJSONArray("actions");
  72. if (list == null || list.length() == 0)
  73. return null;
  74. List<Action> actions = new ArrayList<Action>(list.length());
  75. for (int i = 0; i < list.length(); i++) {
  76. JSONObject opts = list.optJSONObject(i);
  77. String type = opts.optString("type", "button");
  78. if (type.equals("input") && SDK_INT < N) {
  79. Log.w("Action", "Type input is not supported");
  80. continue;
  81. }
  82. if (!(type.equals("button") || type.equals("input"))) {
  83. Log.w("Action", "Unknown type: " + type);
  84. continue;
  85. }
  86. actions.add(new Action(context, opts));
  87. }
  88. if (actions.isEmpty())
  89. return null;
  90. return new ActionGroup(id, actions.toArray(new Action[actions.size()]));
  91. }
  92. /**
  93. * Creates an action group.
  94. *
  95. * @param id The ID of the group.
  96. * @param actions The list of actions.
  97. */
  98. private ActionGroup(String id, Action[] actions) {
  99. this.id = id;
  100. this.actions = actions;
  101. }
  102. /**
  103. * Gets the action group id.
  104. */
  105. public String getId() {
  106. return id;
  107. }
  108. /**
  109. * Gets the action list.
  110. */
  111. public Action[] getActions() {
  112. return actions;
  113. }
  114. }