Options.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  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. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. package de.appplant.cordova.plugin.notification;
  24. import java.util.Date;
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27. import android.app.AlarmManager;
  28. import android.content.Context;
  29. import android.graphics.Bitmap;
  30. import android.net.Uri;
  31. /**
  32. * Wrapper around the JSON object passed through JS which contains all
  33. * possible option values. Class provides simple readers and more advanced
  34. * methods to convert independent values into platform specific values.
  35. */
  36. public class Options {
  37. // Key name for bundled extras
  38. static final String EXTRA = "NOTIFICATION_OPTIONS";
  39. // The original JSON object
  40. private JSONObject options = new JSONObject();
  41. // Repeat interval
  42. private long interval = 0;
  43. // Application context
  44. private final Context context;
  45. // Asset util instance
  46. private final AssetUtil assets;
  47. /**
  48. * Constructor
  49. *
  50. * @param context
  51. * Application context
  52. */
  53. public Options(Context context){
  54. this.context = context;
  55. this.assets = AssetUtil.getInstance(context);
  56. }
  57. /**
  58. * Parse given JSON properties.
  59. *
  60. * @param options
  61. * JSON properties
  62. */
  63. public Options parse (JSONObject options) {
  64. this.options = options;
  65. parseInterval();
  66. parseAssets();
  67. return this;
  68. }
  69. /**
  70. * Parse repeat interval.
  71. */
  72. private void parseInterval() {
  73. String every = options.optString("every").toLowerCase();
  74. if (every.isEmpty()) {
  75. interval = 0;
  76. } else
  77. if (every.equals("second")) {
  78. interval = 1000;
  79. } else
  80. if (every.equals("minute")) {
  81. interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES / 15;
  82. } else
  83. if (every.equals("hour")) {
  84. interval = AlarmManager.INTERVAL_HOUR;
  85. } else
  86. if (every.equals("day")) {
  87. interval = AlarmManager.INTERVAL_DAY;
  88. } else
  89. if (every.equals("week")) {
  90. interval = AlarmManager.INTERVAL_DAY * 7;
  91. } else
  92. if (every.equals("month")) {
  93. interval = AlarmManager.INTERVAL_DAY * 31;
  94. } else
  95. if (every.equals("year")) {
  96. interval = AlarmManager.INTERVAL_DAY * 365;
  97. } else {
  98. try {
  99. interval = Integer.parseInt(every) * 60000;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. /**
  106. * Parse asset URIs.
  107. */
  108. private void parseAssets() {
  109. if (options.has("iconUri"))
  110. return;
  111. Uri iconUri = assets.parse(options.optString("icon", "icon"));
  112. Uri soundUri = assets.parseSound(options.optString("sound", null));
  113. try {
  114. options.put("iconUri", iconUri.toString());
  115. options.put("soundUri", soundUri.toString());
  116. } catch (JSONException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. /**
  121. * Application context.
  122. */
  123. public Context getContext () {
  124. return context;
  125. }
  126. /**
  127. * Wrapped JSON object.
  128. */
  129. JSONObject getDict () {
  130. return options;
  131. }
  132. /**
  133. * Text for the local notification.
  134. */
  135. public String getText() {
  136. return options.optString("text", "");
  137. }
  138. /**
  139. * Repeat interval (day, week, month, year, aso.)
  140. */
  141. public long getRepeatInterval() {
  142. return interval;
  143. }
  144. /**
  145. * Badge number for the local notification.
  146. */
  147. public int getBadgeNumber() {
  148. return options.optInt("badge", 0);
  149. }
  150. /**
  151. * ongoing flag for local notifications.
  152. */
  153. public Boolean isOngoing() {
  154. return options.optBoolean("ongoing", false);
  155. }
  156. /**
  157. * autoClear flag for local notifications.
  158. */
  159. public Boolean isAutoClear() {
  160. return options.optBoolean("autoClear", false);
  161. }
  162. /**
  163. * Trigger date in milliseconds.
  164. */
  165. public long getTriggerTime() {
  166. return options.optLong("at", 0) * 1000;
  167. }
  168. /**
  169. * Trigger date.
  170. */
  171. public Date getTriggerDate() {
  172. return new Date(getTriggerTime());
  173. }
  174. /**
  175. * ID for the local notification.
  176. */
  177. public String getId() {
  178. return options.optString("id", "0");
  179. }
  180. /**
  181. * ID for the local notification.
  182. */
  183. public int getIdAsInt() {
  184. try {
  185. return Integer.parseInt(getId());
  186. } catch (Exception ignore) {
  187. return 0;
  188. }
  189. }
  190. /**
  191. * Title for the local notification.
  192. */
  193. public String getTitle() {
  194. String title = options.optString("title", "");
  195. if (title.isEmpty()) {
  196. title = context.getApplicationInfo().loadLabel(
  197. context.getPackageManager()).toString();
  198. }
  199. return title;
  200. }
  201. /**
  202. * @return
  203. * The notification color for LED
  204. */
  205. public int getLedColor() {
  206. String hex = options.optString("led", "000000");
  207. int aRGB = Integer.parseInt(hex,16);
  208. aRGB += 0xFF000000;
  209. return aRGB;
  210. }
  211. /**
  212. * Sound file path for the local notification.
  213. */
  214. public Uri getSoundUri() {
  215. Uri uri = null;
  216. try{
  217. uri = Uri.parse(options.optString("soundUri"));
  218. } catch (Exception e){
  219. e.printStackTrace();
  220. }
  221. return uri;
  222. }
  223. /**
  224. * Icon bitmap for the local notification.
  225. */
  226. public Bitmap getIconBitmap() {
  227. String icon = options.optString("icon", "icon");
  228. Bitmap bmp;
  229. try{
  230. Uri uri = Uri.parse(options.optString("iconUri"));
  231. bmp = assets.getIconFromUri(uri);
  232. } catch (Exception e){
  233. bmp = assets.getIconFromDrawable(icon);
  234. }
  235. return bmp;
  236. }
  237. /**
  238. * Small icon resource ID for the local notification.
  239. */
  240. public int getSmallIcon () {
  241. String icon = options.optString("smallIcon", "");
  242. int resId = assets.getResIdForDrawable(icon);
  243. if (resId == 0) {
  244. resId = android.R.drawable.screen_background_dark;
  245. }
  246. return resId;
  247. }
  248. /**
  249. * JSON object as string.
  250. */
  251. public String toString() {
  252. return options.toString();
  253. }
  254. }