Options.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. // codebeat:disable[TOO_MANY_FUNCTIONS]
  22. package de.appplant.cordova.plugin.notification;
  23. import android.content.Context;
  24. import android.graphics.Bitmap;
  25. import android.graphics.Color;
  26. import android.net.Uri;
  27. import android.support.v4.app.NotificationCompat;
  28. import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
  29. import android.support.v4.media.session.MediaSessionCompat;
  30. import org.json.JSONArray;
  31. import org.json.JSONObject;
  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. 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. * wakeup flag for the notification.
  169. */
  170. public boolean shallWakeUp() {
  171. return options.optBoolean("wakeup", true);
  172. }
  173. /**
  174. * Gets the value for the timeout flag.
  175. */
  176. long getTimeout() {
  177. return options.optLong("timeoutAfter");
  178. }
  179. /**
  180. * The channel id of that notification.
  181. */
  182. String getChannel() {
  183. return options.optString("channel", Manager.CHANNEL_ID);
  184. }
  185. /**
  186. * If the group shall show a summary.
  187. */
  188. boolean getGroupSummary() {
  189. return options.optBoolean("groupSummary", false);
  190. }
  191. /**
  192. * Text for the local notification.
  193. */
  194. public String getText() {
  195. Object text = options.opt("text");
  196. return text instanceof String ? (String) text : "";
  197. }
  198. /**
  199. * Title for the local notification.
  200. */
  201. public String getTitle() {
  202. String title = options.optString("title", "");
  203. if (title.isEmpty()) {
  204. title = context.getApplicationInfo().loadLabel(
  205. context.getPackageManager()).toString();
  206. }
  207. return title;
  208. }
  209. /**
  210. * The notification color for LED.
  211. */
  212. int getLedColor() {
  213. Object cfg = options.opt("led");
  214. String hex = null;
  215. if (cfg instanceof String) {
  216. hex = options.optString("led");
  217. } else
  218. if (cfg instanceof JSONArray) {
  219. hex = options.optJSONArray("led").optString(0);
  220. } else
  221. if (cfg instanceof JSONObject) {
  222. hex = options.optJSONObject("led").optString("color");
  223. }
  224. if (hex == null)
  225. return 0;
  226. try {
  227. hex = stripHex(hex);
  228. int aRGB = Integer.parseInt(hex, 16);
  229. return aRGB + 0xFF000000;
  230. } catch (NumberFormatException e) {
  231. e.printStackTrace();
  232. }
  233. return 0;
  234. }
  235. /**
  236. * The notification color for LED.
  237. */
  238. int getLedOn() {
  239. Object cfg = options.opt("led");
  240. int defVal = 1000;
  241. if (cfg instanceof JSONArray)
  242. return options.optJSONArray("led").optInt(1, defVal);
  243. if (cfg instanceof JSONObject)
  244. return options.optJSONObject("led").optInt("on", defVal);
  245. return defVal;
  246. }
  247. /**
  248. * The notification color for LED.
  249. */
  250. int getLedOff() {
  251. Object cfg = options.opt("led");
  252. int defVal = 1000;
  253. if (cfg instanceof JSONArray)
  254. return options.optJSONArray("led").optInt(2, defVal);
  255. if (cfg instanceof JSONObject)
  256. return options.optJSONObject("led").optInt("off", defVal);
  257. return defVal;
  258. }
  259. /**
  260. * The notification background color for the small icon.
  261. *
  262. * @return null, if no color is given.
  263. */
  264. public int getColor() {
  265. String hex = options.optString("color", null);
  266. if (hex == null)
  267. return NotificationCompat.COLOR_DEFAULT;
  268. try {
  269. hex = stripHex(hex);
  270. if (hex.matches("[^0-9]*")) {
  271. return Color.class
  272. .getDeclaredField(hex.toUpperCase())
  273. .getInt(null);
  274. }
  275. int aRGB = Integer.parseInt(hex, 16);
  276. return aRGB + 0xFF000000;
  277. } catch (NumberFormatException e) {
  278. e.printStackTrace();
  279. } catch (NoSuchFieldException e) {
  280. e.printStackTrace();
  281. } catch (IllegalAccessException e) {
  282. e.printStackTrace();
  283. }
  284. return NotificationCompat.COLOR_DEFAULT;
  285. }
  286. /**
  287. * Sound file path for the local notification.
  288. */
  289. Uri getSound() {
  290. return assets.parse(options.optString("sound", null));
  291. }
  292. /**
  293. * Icon resource ID for the local notification.
  294. */
  295. boolean hasLargeIcon() {
  296. String icon = options.optString("icon", null);
  297. return icon != null;
  298. }
  299. /**
  300. * Icon bitmap for the local notification.
  301. */
  302. Bitmap getLargeIcon() {
  303. String icon = options.optString("icon", null);
  304. Uri uri = assets.parse(icon);
  305. Bitmap bmp = null;
  306. try {
  307. bmp = assets.getIconFromUri(uri);
  308. } catch (Exception e){
  309. e.printStackTrace();
  310. }
  311. return bmp;
  312. }
  313. /**
  314. * Small icon resource ID for the local notification.
  315. */
  316. int getSmallIcon() {
  317. String icon = options.optString("smallIcon", DEFAULT_ICON);
  318. int resId = assets.getResId(icon);
  319. if (resId == 0) {
  320. resId = assets.getResId(DEFAULT_ICON);
  321. }
  322. if (resId == 0) {
  323. resId = context.getApplicationInfo().icon;
  324. }
  325. if (resId == 0) {
  326. resId = android.R.drawable.ic_popup_reminder;
  327. }
  328. return resId;
  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 getPrio() {
  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 showClock() {
  413. Object clock = options.opt("clock");
  414. return (clock instanceof Boolean) ? (Boolean) clock : true;
  415. }
  416. /**
  417. * If the notification shall show the when date.
  418. */
  419. boolean showChronometer() {
  420. Object clock = options.opt("clock");
  421. return (clock instanceof String) && clock.equals("chronometer");
  422. }
  423. /**
  424. * If the notification shall display a progress bar.
  425. */
  426. boolean isWithProgressBar() {
  427. return options
  428. .optJSONObject("progressBar")
  429. .optBoolean("enabled", false);
  430. }
  431. /**
  432. * Gets the progress value.
  433. *
  434. * @return 0 by default.
  435. */
  436. int getProgressValue() {
  437. return options
  438. .optJSONObject("progressBar")
  439. .optInt("value", 0);
  440. }
  441. /**
  442. * Gets the progress value.
  443. *
  444. * @return 100 by default.
  445. */
  446. int getProgressMaxValue() {
  447. return options
  448. .optJSONObject("progressBar")
  449. .optInt("maxValue", 100);
  450. }
  451. /**
  452. * Gets the progress indeterminate value.
  453. *
  454. * @return false by default.
  455. */
  456. boolean isIndeterminateProgress() {
  457. return options
  458. .optJSONObject("progressBar")
  459. .optBoolean("indeterminate", false);
  460. }
  461. /**
  462. * If the trigger shall be infinite.
  463. */
  464. public boolean isInfiniteTrigger() {
  465. JSONObject trigger = options.optJSONObject("trigger");
  466. return trigger.has("every") && trigger.optInt("count", -1) < 0;
  467. }
  468. /**
  469. * The summary for inbox style notifications.
  470. */
  471. String getSummary() {
  472. return options.optString("summary", null);
  473. }
  474. /**
  475. * Image attachments for image style notifications.
  476. *
  477. * @return For now it only returns the first item as Android does not
  478. * support multiple attachments like iOS.
  479. */
  480. List<Bitmap> getAttachments() {
  481. JSONArray paths = options.optJSONArray("attachments");
  482. List<Bitmap> pics = new ArrayList<Bitmap>();
  483. if (paths == null)
  484. return pics;
  485. for (int i = 0; i < paths.length(); i++) {
  486. Uri uri = assets.parse(paths.optString(i));
  487. if (uri == Uri.EMPTY)
  488. continue;
  489. try {
  490. Bitmap pic = assets.getIconFromUri(uri);
  491. pics.add(pic);
  492. break;
  493. } catch (IOException e) {
  494. e.printStackTrace();
  495. }
  496. }
  497. return pics;
  498. }
  499. /**
  500. * Gets the list of actions to display.
  501. */
  502. Action[] getActions() {
  503. Object value = options.opt("actions");
  504. String groupId = null;
  505. JSONArray actions = null;
  506. ActionGroup group = null;
  507. if (value instanceof String) {
  508. groupId = (String) value;
  509. } else
  510. if (value instanceof JSONArray) {
  511. actions = (JSONArray) value;
  512. }
  513. if (groupId != null) {
  514. group = ActionGroup.lookup(groupId);
  515. } else
  516. if (actions != null && actions.length() > 0) {
  517. group = ActionGroup.parse(context, actions);
  518. }
  519. return (group != null) ? group.getActions() : null;
  520. }
  521. /**
  522. * Gets the list of messages to display.
  523. *
  524. * @return null if there are no messages.
  525. */
  526. Message[] getMessages() {
  527. Object text = options.opt("text");
  528. if (text == null || text instanceof String)
  529. return null;
  530. JSONArray list = (JSONArray) text;
  531. if (list.length() == 0)
  532. return null;
  533. Message[] messages = new Message[list.length()];
  534. long now = new Date().getTime();
  535. for (int i = 0; i < messages.length; i++) {
  536. JSONObject msg = list.optJSONObject(i);
  537. String message = msg.optString("message");
  538. long timestamp = msg.optLong("date", now);
  539. String person = msg.optString("person", null);
  540. messages[i] = new Message(message, timestamp, person);
  541. }
  542. return messages;
  543. }
  544. /**
  545. * Gets the token for the specified media session.
  546. *
  547. * @return null if there no session.
  548. */
  549. MediaSessionCompat.Token getMediaSessionToken() {
  550. String tag = options.optString("mediaSession", null);
  551. if (tag == null)
  552. return null;
  553. MediaSessionCompat session = new MediaSessionCompat(context, tag);
  554. return session.getSessionToken();
  555. }
  556. /**
  557. * Strips the hex code #FF00FF => FF00FF
  558. *
  559. * @param hex The hex code to strip.
  560. *
  561. * @return The stripped hex code without a leading #
  562. */
  563. private String stripHex(String hex) {
  564. return (hex.charAt(0) == '#') ? hex.substring(1) : hex;
  565. }
  566. }
  567. // codebeat:enable[TOO_MANY_FUNCTIONS]