Options.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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;
  22. import android.content.Context;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Color;
  25. import android.net.Uri;
  26. import android.support.v4.app.NotificationCompat;
  27. import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
  28. import android.support.v4.media.session.MediaSessionCompat;
  29. import org.json.JSONArray;
  30. import org.json.JSONObject;
  31. import org.json.JSONArray;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.Date;
  35. import java.util.List;
  36. import de.appplant.cordova.plugin.notification.action.Action;
  37. import de.appplant.cordova.plugin.notification.action.ActionGroup;
  38. import de.appplant.cordova.plugin.notification.util.AssetUtil;
  39. import static android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS;
  40. import static android.support.v4.app.NotificationCompat.DEFAULT_SOUND;
  41. import static android.support.v4.app.NotificationCompat.DEFAULT_VIBRATE;
  42. import static android.support.v4.app.NotificationCompat.PRIORITY_MAX;
  43. import static android.support.v4.app.NotificationCompat.PRIORITY_MIN;
  44. import static android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC;
  45. import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
  46. /**
  47. * Wrapper around the JSON object passed through JS which contains all
  48. * possible option values. Class provides simple readers and more advanced
  49. * methods to convert independent values into platform specific values.
  50. */
  51. public final class Options {
  52. // Key name for bundled sound extra
  53. static final String EXTRA_SOUND = "NOTIFICATION_SOUND";
  54. // Key name for bundled launch extra
  55. public static final String EXTRA_LAUNCH = "NOTIFICATION_LAUNCH";
  56. // Default icon path
  57. private static final String DEFAULT_ICON = "res://icon";
  58. // The original JSON object
  59. private final JSONObject options;
  60. // The application context
  61. private final Context context;
  62. // Asset util instance
  63. private final AssetUtil assets;
  64. /**
  65. * When creating without a context, various methods might not work well.
  66. *
  67. * @param options The options dict map.
  68. */
  69. public Options(JSONObject options) {
  70. this.options = options;
  71. this.context = null;
  72. this.assets = null;
  73. }
  74. /**
  75. * Constructor
  76. *
  77. * @param context The application context.
  78. * @param options The options dict map.
  79. */
  80. public Options(Context context, JSONObject options) {
  81. this.context = context;
  82. this.options = options;
  83. this.assets = AssetUtil.getInstance(context);
  84. }
  85. /**
  86. * Application context.
  87. */
  88. public Context getContext () {
  89. return context;
  90. }
  91. /**
  92. * Wrapped JSON object.
  93. */
  94. public JSONObject getDict() {
  95. return options;
  96. }
  97. /**
  98. * JSON object as string.
  99. */
  100. public String toString() {
  101. return options.toString();
  102. }
  103. /**
  104. * Gets the ID for the local notification.
  105. *
  106. * @return 0 if the user did not specify.
  107. */
  108. public Integer getId() {
  109. return options.optInt("id", 0);
  110. }
  111. /**
  112. * The identifier for the local notification.
  113. *
  114. * @return The notification ID as the string
  115. */
  116. public String getIdentifier() {
  117. return getId().toString();
  118. }
  119. /**
  120. * Badge number for the local notification.
  121. */
  122. public int getBadgeNumber() {
  123. return options.optInt("badge", 0);
  124. }
  125. /**
  126. * Number for the local notification.
  127. */
  128. public int getNumber() {
  129. return options.optInt("number", 0);
  130. }
  131. /**
  132. * ongoing flag for local notifications.
  133. */
  134. public Boolean isSticky() {
  135. return options.optBoolean("sticky", false);
  136. }
  137. /**
  138. * autoClear flag for local notifications.
  139. */
  140. Boolean isAutoClear() {
  141. return options.optBoolean("autoClear", false);
  142. }
  143. /**
  144. * Gets the raw trigger spec as provided by the user.
  145. */
  146. public JSONObject getTrigger() {
  147. return options.optJSONObject("trigger");
  148. }
  149. /**
  150. * Gets the value of the silent flag.
  151. */
  152. boolean isSilent() {
  153. return options.optBoolean("silent", false);
  154. }
  155. /**
  156. * The group for that notification.
  157. */
  158. String getGroup() {
  159. return options.optString("group", null);
  160. }
  161. /**
  162. * launch flag for the notification.
  163. */
  164. boolean isLaunchingApp() {
  165. return options.optBoolean("launch", true);
  166. }
  167. /**
  168. * The channel id of that notification.
  169. */
  170. String getChannel() {
  171. return options.optString("channel", Manager.CHANNEL_ID);
  172. }
  173. /**
  174. * If the group shall show a summary.
  175. */
  176. boolean getGroupSummary() {
  177. return options.optBoolean("groupSummary", false);
  178. }
  179. /**
  180. * Text for the local notification.
  181. */
  182. public String getText() {
  183. Object text = options.opt("text");
  184. return text instanceof String ? (String) text : "";
  185. }
  186. /**
  187. * Title for the local notification.
  188. */
  189. public String getTitle() {
  190. String title = options.optString("title", "");
  191. if (title.isEmpty()) {
  192. title = context.getApplicationInfo().loadLabel(
  193. context.getPackageManager()).toString();
  194. }
  195. return title;
  196. }
  197. /**
  198. * The notification color for LED.
  199. */
  200. int getLedColor() {
  201. Object cfg = options.opt("led");
  202. String hex = null;
  203. if (cfg instanceof String) {
  204. hex = options.optString("led");
  205. } else
  206. if (cfg instanceof JSONArray) {
  207. hex = options.optJSONArray("led").optString(0);
  208. } else
  209. if (cfg instanceof JSONObject) {
  210. hex = options.optJSONObject("led").optString("color");
  211. }
  212. if (hex == null)
  213. return 0;
  214. try {
  215. hex = stripHex(hex);
  216. int aRGB = Integer.parseInt(hex, 16);
  217. return aRGB + 0xFF000000;
  218. } catch (NumberFormatException e) {
  219. e.printStackTrace();
  220. }
  221. return 0;
  222. }
  223. /**
  224. * The notification color for LED.
  225. */
  226. int getLedOn() {
  227. Object cfg = options.opt("led");
  228. int defVal = 1000;
  229. if (cfg instanceof JSONArray)
  230. return options.optJSONArray("led").optInt(1, defVal);
  231. if (cfg instanceof JSONObject)
  232. return options.optJSONObject("led").optInt("on", defVal);
  233. return defVal;
  234. }
  235. /**
  236. * The notification color for LED.
  237. */
  238. int getLedOff() {
  239. Object cfg = options.opt("led");
  240. int defVal = 1000;
  241. if (cfg instanceof JSONArray)
  242. return options.optJSONArray("led").optInt(2, defVal);
  243. if (cfg instanceof JSONObject)
  244. return options.optJSONObject("led").optInt("off", defVal);
  245. return defVal;
  246. }
  247. /**
  248. * The notification background color for the small icon.
  249. *
  250. * @return null, if no color is given.
  251. */
  252. public int getColor() {
  253. String hex = options.optString("color", null);
  254. if (hex == null)
  255. return NotificationCompat.COLOR_DEFAULT;
  256. try {
  257. hex = stripHex(hex);
  258. if (hex.matches("[^0-9]*")) {
  259. return Color.class
  260. .getDeclaredField(hex.toUpperCase())
  261. .getInt(null);
  262. }
  263. int aRGB = Integer.parseInt(hex, 16);
  264. return aRGB + 0xFF000000;
  265. } catch (NumberFormatException e) {
  266. e.printStackTrace();
  267. } catch (NoSuchFieldException e) {
  268. e.printStackTrace();
  269. } catch (IllegalAccessException e) {
  270. e.printStackTrace();
  271. }
  272. return NotificationCompat.COLOR_DEFAULT;
  273. }
  274. /**
  275. * Sound file path for the local notification.
  276. */
  277. public Uri getSound() {
  278. return assets.parse(options.optString("sound", null));
  279. }
  280. public long[] getVibrate() {
  281. JSONArray array = options.optJSONArray("vibrate");
  282. if (array == null)
  283. return null;
  284. long[] rv = new long[array.length()];
  285. for (int i = 0; i < array.length(); i++) {
  286. rv[i] = array.optInt(i);
  287. }
  288. return rv;
  289. }
  290. /**
  291. * Icon bitmap for the local notification.
  292. */
  293. Bitmap getLargeIcon() {
  294. Uri uri = assets.parse(options.optString("icon", DEFAULT_ICON));
  295. Bitmap bmp = null;
  296. try {
  297. bmp = assets.getIconFromUri(uri);
  298. } catch (Exception e){
  299. e.printStackTrace();
  300. }
  301. return bmp;
  302. }
  303. /**
  304. * Icon resource ID for the local notification.
  305. */
  306. public int getIcon () {
  307. String icon = options.optString("icon", DEFAULT_ICON);
  308. int resId = assets.getResId(icon);
  309. if (resId == 0) {
  310. resId = getSmallIcon();
  311. }
  312. if (resId == 0) {
  313. resId = assets.getResId(DEFAULT_ICON);
  314. }
  315. if (resId == 0) {
  316. resId = context.getApplicationInfo().icon;
  317. }
  318. if (resId == 0) {
  319. resId = android.R.drawable.ic_popup_reminder;
  320. }
  321. return resId;
  322. }
  323. /**
  324. * Small icon resource ID for the local notification.
  325. */
  326. int getSmallIcon() {
  327. String icon = options.optString("smallIcon", "");
  328. return assets.getResId(icon);
  329. }
  330. /**
  331. * If the phone should vibrate.
  332. */
  333. private boolean isWithVibration() {
  334. return options.optBoolean("vibrate", true);
  335. }
  336. /**
  337. * If the phone should play no sound.
  338. */
  339. private boolean isWithoutSound() {
  340. Object value = options.opt("sound");
  341. return value == null || value.equals(false);
  342. }
  343. /**
  344. * If the phone should play the default sound.
  345. */
  346. private boolean isWithDefaultSound() {
  347. Object value = options.opt("sound");
  348. return value != null && value.equals(true);
  349. }
  350. /**
  351. * If the phone should show no LED light.
  352. */
  353. private boolean isWithoutLights() {
  354. Object value = options.opt("led");
  355. return value == null || value.equals(false);
  356. }
  357. /**
  358. * If the phone should show the default LED lights.
  359. */
  360. private boolean isWithDefaultLights() {
  361. Object value = options.opt("led");
  362. return value != null && value.equals(true);
  363. }
  364. /**
  365. * Set the default notification options that will be used.
  366. * The value should be one or more of the following fields combined with
  367. * bitwise-or: DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.
  368. */
  369. int getDefaults() {
  370. int defaults = options.optInt("defaults", 0);
  371. if (isWithVibration()) {
  372. defaults |= DEFAULT_VIBRATE;
  373. } else {
  374. defaults &= DEFAULT_VIBRATE;
  375. }
  376. if (isWithDefaultSound()) {
  377. defaults |= DEFAULT_SOUND;
  378. } else
  379. if (isWithoutSound()) {
  380. defaults &= DEFAULT_SOUND;
  381. }
  382. if (isWithDefaultLights()) {
  383. defaults |= DEFAULT_LIGHTS;
  384. } else
  385. if (isWithoutLights()) {
  386. defaults &= DEFAULT_LIGHTS;
  387. }
  388. return defaults;
  389. }
  390. /**
  391. * Gets the visibility for the notification.
  392. *
  393. * @return VISIBILITY_PUBLIC or VISIBILITY_SECRET
  394. */
  395. int getVisibility() {
  396. if (options.optBoolean("lockscreen", true)) {
  397. return VISIBILITY_PUBLIC;
  398. } else {
  399. return VISIBILITY_SECRET;
  400. }
  401. }
  402. /**
  403. * Gets the notifications priority.
  404. */
  405. int getPriority() {
  406. int prio = options.optInt("priority");
  407. return Math.min(Math.max(prio, PRIORITY_MIN), PRIORITY_MAX);
  408. }
  409. /**
  410. * If the notification shall show the when date.
  411. */
  412. boolean getShowWhen() {
  413. return options.optBoolean("showWhen", true);
  414. }
  415. /**
  416. * If the notification shall display a progress bar.
  417. */
  418. boolean isWithProgressBar() {
  419. return options
  420. .optJSONObject("progressBar")
  421. .optBoolean("enabled", false);
  422. }
  423. /**
  424. * Gets the progress value.
  425. *
  426. * @return 0 by default.
  427. */
  428. int getProgressValue() {
  429. return options
  430. .optJSONObject("progressBar")
  431. .optInt("value", 0);
  432. }
  433. /**
  434. * Gets the progress value.
  435. *
  436. * @return 100 by default.
  437. */
  438. int getProgressMaxValue() {
  439. return options
  440. .optJSONObject("progressBar")
  441. .optInt("maxValue", 100);
  442. }
  443. /**
  444. * Gets the progress indeterminate value.
  445. *
  446. * @return false by default.
  447. */
  448. boolean isIndeterminateProgress() {
  449. return options
  450. .optJSONObject("progressBar")
  451. .optBoolean("indeterminate", false);
  452. }
  453. /**
  454. * The summary for inbox style notifications.
  455. */
  456. String getSummary() {
  457. return options.optString("summary", null);
  458. }
  459. /**
  460. * Image attachments for image style notifications.
  461. *
  462. * @return For now it only returns the first item as Android does not
  463. * support multiple attachments like iOS.
  464. */
  465. List<Bitmap> getAttachments() {
  466. JSONArray paths = options.optJSONArray("attachments");
  467. List<Bitmap> pics = new ArrayList<Bitmap>();
  468. if (paths == null)
  469. return pics;
  470. for (int i = 0; i < paths.length(); i++) {
  471. Uri uri = assets.parse(paths.optString(i));
  472. if (uri == Uri.EMPTY)
  473. continue;
  474. try {
  475. Bitmap pic = assets.getIconFromUri(uri);
  476. pics.add(pic);
  477. break;
  478. } catch (IOException e) {
  479. e.printStackTrace();
  480. }
  481. }
  482. return pics;
  483. }
  484. /**
  485. * Gets the list of actions to display.
  486. */
  487. Action[] getActions() {
  488. String groupId = options.optString("actionGroupId", null);
  489. JSONArray actions = options.optJSONArray("actions");
  490. ActionGroup group = null;
  491. if (actions != null && actions.length() > 0) {
  492. group = ActionGroup.parse(context, options);
  493. }
  494. if (group == null && groupId != null) {
  495. group = ActionGroup.lookup(groupId);
  496. }
  497. if (group != null) {
  498. ActionGroup.register(group);
  499. return group.getActions();
  500. }
  501. return null;
  502. }
  503. /**
  504. * Gets the list of messages to display.
  505. *
  506. * @return null if there are no messages.
  507. */
  508. Message[] getMessages() {
  509. Object text = options.opt("text");
  510. if (text == null || text instanceof String)
  511. return null;
  512. JSONArray list = (JSONArray) text;
  513. if (list.length() == 0)
  514. return null;
  515. Message[] messages = new Message[list.length()];
  516. long now = new Date().getTime();
  517. for (int i = 0; i < messages.length; i++) {
  518. JSONObject msg = list.optJSONObject(i);
  519. String message = msg.optString("message");
  520. long timestamp = msg.optLong("date", now);
  521. String person = msg.optString("person", null);
  522. messages[i] = new Message(message, timestamp, person);
  523. }
  524. return messages;
  525. }
  526. /**
  527. * Gets the token for the specified media session.
  528. *
  529. * @return null if there no session.
  530. */
  531. MediaSessionCompat.Token getMediaSessionToken() {
  532. String tag = options.optString("mediaSession", null);
  533. if (tag == null)
  534. return null;
  535. MediaSessionCompat session = new MediaSessionCompat(context, tag);
  536. return session.getSessionToken();
  537. }
  538. /**
  539. * Strips the hex code #FF00FF => FF00FF
  540. *
  541. * @param hex The hex code to strip.
  542. *
  543. * @return The stripped hex code without a leading #
  544. */
  545. private String stripHex(String hex) {
  546. return (hex.charAt(0) == '#') ? hex.substring(1) : hex;
  547. }
  548. }