Options.java 17 KB

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