Manager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.app.NotificationManager;
  23. import android.content.Context;
  24. import android.content.SharedPreferences;
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27. import java.util.ArrayList;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY;
  33. /**
  34. * Central way to access all or single local notifications set by specific
  35. * state like triggered or scheduled. Offers shortcut ways to schedule,
  36. * cancel or clear local notifications.
  37. */
  38. public class Manager {
  39. // Context passed through constructor and used for notification builder.
  40. private Context context;
  41. /**
  42. * Constructor
  43. *
  44. * @param context
  45. * Application context
  46. */
  47. private Manager(Context context){
  48. this.context = context;
  49. }
  50. /**
  51. * Static method to retrieve class instance.
  52. *
  53. * @param context
  54. * Application context
  55. */
  56. public static Manager getInstance(Context context) {
  57. return new Manager(context);
  58. }
  59. /**
  60. * Schedule local notification specified by JSON object.
  61. *
  62. * @param options
  63. * JSON object with set of options
  64. * @param receiver
  65. * Receiver to handle the trigger event
  66. */
  67. public Notification schedule (JSONObject options, Class<?> receiver) {
  68. return schedule(new Options(context).parse(options), receiver);
  69. }
  70. /**
  71. * Schedule local notification specified by options object.
  72. *
  73. * @param options
  74. * Set of notification options
  75. * @param receiver
  76. * Receiver to handle the trigger event
  77. */
  78. public Notification schedule (Options options, Class<?> receiver) {
  79. Notification notification = new Builder(options)
  80. .setTriggerReceiver(receiver)
  81. .build();
  82. notification.schedule();
  83. return notification;
  84. }
  85. /**
  86. * Clear local notification specified by ID.
  87. *
  88. * @param id
  89. * The notification ID
  90. * @param updates
  91. * JSON object with notification options
  92. * @param receiver
  93. * Receiver to handle the trigger event
  94. */
  95. public Notification update (int id, JSONObject updates, Class<?> receiver) {
  96. Notification notification = get(id);
  97. if (notification == null)
  98. return null;
  99. notification.cancel();
  100. JSONObject options = mergeJSONObjects(
  101. notification.getOptions().getDict(), updates);
  102. try {
  103. options.put("updated", true);
  104. } catch (JSONException ignore) {}
  105. return schedule(options, receiver);
  106. }
  107. /**
  108. * Clear local notification specified by ID.
  109. *
  110. * @param id
  111. * The notification ID
  112. */
  113. public Notification clear (int id) {
  114. Notification notification = get(id);
  115. if (notification != null) {
  116. notification.clear();
  117. }
  118. return notification;
  119. }
  120. /**
  121. * Clear local notification specified by ID.
  122. *
  123. * @param id
  124. * The notification ID
  125. */
  126. public Notification cancel (int id) {
  127. Notification notification = get(id);
  128. if (notification != null) {
  129. notification.cancel();
  130. }
  131. return notification;
  132. }
  133. /**
  134. * Clear all local notifications.
  135. */
  136. public void clearAll () {
  137. List<Notification> notifications = getAll();
  138. for (Notification notification : notifications) {
  139. notification.clear();
  140. }
  141. getNotMgr().cancelAll();
  142. }
  143. /**
  144. * Cancel all local notifications.
  145. */
  146. public void cancelAll () {
  147. List<Notification> notifications = getAll();
  148. for (Notification notification : notifications) {
  149. notification.cancel();
  150. }
  151. getNotMgr().cancelAll();
  152. }
  153. /**
  154. * All local notifications IDs.
  155. */
  156. public List<Integer> getIds() {
  157. Set<String> keys = getPrefs().getAll().keySet();
  158. ArrayList<Integer> ids = new ArrayList<Integer>();
  159. for (String key : keys) {
  160. try {
  161. ids.add(Integer.parseInt(key));
  162. } catch (NumberFormatException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. return ids;
  167. }
  168. /**
  169. * All local notification IDs for given type.
  170. *
  171. * @param type
  172. * The notification life cycle type
  173. */
  174. public List<Integer> getIdsByType(Notification.Type type) {
  175. List<Notification> notifications = getAll();
  176. ArrayList<Integer> ids = new ArrayList<Integer>();
  177. for (Notification notification : notifications) {
  178. if (notification.getType() == type) {
  179. ids.add(notification.getId());
  180. }
  181. }
  182. return ids;
  183. }
  184. /**
  185. * List of local notifications with matching ID.
  186. *
  187. * @param ids
  188. * Set of notification IDs
  189. */
  190. public List<Notification> getByIds(List<Integer> ids) {
  191. ArrayList<Notification> notifications = new ArrayList<Notification>();
  192. for (int id : ids) {
  193. Notification notification = get(id);
  194. if (notification != null) {
  195. notifications.add(notification);
  196. }
  197. }
  198. return notifications;
  199. }
  200. /**
  201. * List of all local notification.
  202. */
  203. public List<Notification> getAll() {
  204. return getByIds(getIds());
  205. }
  206. /**
  207. * List of local notifications from given type.
  208. *
  209. * @param type
  210. * The notification life cycle type
  211. */
  212. public List<Notification> getByType(Notification.Type type) {
  213. List<Notification> notifications = getAll();
  214. ArrayList<Notification> list = new ArrayList<Notification>();
  215. if (type == Notification.Type.ALL)
  216. return notifications;
  217. for (Notification notification : notifications) {
  218. if (notification.getType() == type) {
  219. list.add(notification);
  220. }
  221. }
  222. return list;
  223. }
  224. /**
  225. * List of local notifications with matching ID from given type.
  226. *
  227. * @param type
  228. * The notification life cycle type
  229. * @param ids
  230. * Set of notification IDs
  231. */
  232. @SuppressWarnings("UnusedDeclaration")
  233. public List<Notification> getBy(Notification.Type type, List<Integer> ids) {
  234. ArrayList<Notification> notifications = new ArrayList<Notification>();
  235. for (int id : ids) {
  236. Notification notification = get(id);
  237. if (notification != null && notification.isScheduled()) {
  238. notifications.add(notification);
  239. }
  240. }
  241. return notifications;
  242. }
  243. /**
  244. * If a notification with an ID exists.
  245. *
  246. * @param id
  247. * Notification ID
  248. */
  249. public boolean exist (int id) {
  250. return get(id) != null;
  251. }
  252. /**
  253. * If a notification with an ID and type exists.
  254. *
  255. * @param id
  256. * Notification ID
  257. * @param type
  258. * Notification type
  259. */
  260. public boolean exist (int id, Notification.Type type) {
  261. Notification notification = get(id);
  262. return notification != null && notification.getType() == type;
  263. }
  264. /**
  265. * List of properties from all local notifications.
  266. */
  267. public List<JSONObject> getOptions() {
  268. return getOptionsById(getIds());
  269. }
  270. /**
  271. * List of properties from local notifications with matching ID.
  272. *
  273. * @param ids
  274. * Set of notification IDs
  275. */
  276. public List<JSONObject> getOptionsById(List<Integer> ids) {
  277. ArrayList<JSONObject> options = new ArrayList<JSONObject>();
  278. for (int id : ids) {
  279. Notification notification = get(id);
  280. if (notification != null) {
  281. options.add(notification.getOptions().getDict());
  282. }
  283. }
  284. return options;
  285. }
  286. /**
  287. * List of properties from all local notifications from given type.
  288. *
  289. * @param type
  290. * The notification life cycle type
  291. */
  292. public List<JSONObject> getOptionsByType(Notification.Type type) {
  293. ArrayList<JSONObject> options = new ArrayList<JSONObject>();
  294. List<Notification> notifications = getByType(type);
  295. for (Notification notification : notifications) {
  296. options.add(notification.getOptions().getDict());
  297. }
  298. return options;
  299. }
  300. /**
  301. * List of properties from local notifications with matching ID from
  302. * given type.
  303. *
  304. * @param type
  305. * The notification life cycle type
  306. * @param ids
  307. * Set of notification IDs
  308. */
  309. public List<JSONObject> getOptionsBy(Notification.Type type,
  310. List<Integer> ids) {
  311. if (type == Notification.Type.ALL)
  312. return getOptionsById(ids);
  313. ArrayList<JSONObject> options = new ArrayList<JSONObject>();
  314. List<Notification> notifications = getByIds(ids);
  315. for (Notification notification : notifications) {
  316. if (notification.getType() == type) {
  317. options.add(notification.getOptions().getDict());
  318. }
  319. }
  320. return options;
  321. }
  322. /**
  323. * Get existent local notification.
  324. *
  325. * @param id
  326. * Notification ID
  327. */
  328. public Notification get(int id) {
  329. Map<String, ?> alarms = getPrefs().getAll();
  330. String notId = Integer.toString(id);
  331. JSONObject options;
  332. if (!alarms.containsKey(notId))
  333. return null;
  334. try {
  335. String json = alarms.get(notId).toString();
  336. options = new JSONObject(json);
  337. } catch (JSONException e) {
  338. e.printStackTrace();
  339. return null;
  340. }
  341. Builder builder = new Builder(context, options);
  342. return builder.build();
  343. }
  344. /**
  345. * Merge two JSON objects.
  346. *
  347. * @param obj1
  348. * JSON object
  349. * @param obj2
  350. * JSON object with new options
  351. */
  352. private JSONObject mergeJSONObjects (JSONObject obj1, JSONObject obj2) {
  353. Iterator it = obj2.keys();
  354. while (it.hasNext()) {
  355. try {
  356. String key = (String)it.next();
  357. obj1.put(key, obj2.opt(key));
  358. } catch (JSONException e) {
  359. e.printStackTrace();
  360. }
  361. }
  362. return obj1;
  363. }
  364. /**
  365. * Shared private preferences for the application.
  366. */
  367. private SharedPreferences getPrefs () {
  368. return context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
  369. }
  370. /**
  371. * Notification manager for the application.
  372. */
  373. private NotificationManager getNotMgr () {
  374. return (NotificationManager) context
  375. .getSystemService(Context.NOTIFICATION_SERVICE);
  376. }
  377. }