Manager.java 12 KB

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