LocalNotification.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import java.util.ArrayList;
  20. import org.apache.cordova.CallbackContext;
  21. import org.apache.cordova.CordovaInterface;
  22. import org.apache.cordova.CordovaPlugin;
  23. import org.apache.cordova.CordovaWebView;
  24. import org.apache.cordova.LOG;
  25. import org.apache.cordova.PluginResult;
  26. import org.json.JSONArray;
  27. import org.json.JSONException;
  28. import org.json.JSONObject;
  29. import android.app.Activity;
  30. import android.app.AlarmManager;
  31. import android.app.NotificationManager;
  32. import android.content.Context;
  33. import android.content.SharedPreferences;
  34. import android.content.SharedPreferences.Editor;
  35. import android.os.Build;
  36. import android.annotation.TargetApi;
  37. import de.appplant.cordova.plugin.notification.*;
  38. /**
  39. * This plugin utilizes the Android AlarmManager in combination with StatusBar
  40. * notifications. When a local notification is scheduled the alarm manager takes
  41. * care of firing the event. When the event is processed, a notification is put
  42. * in the Android status bar.
  43. */
  44. public class LocalNotification extends CordovaPlugin {
  45. protected final static String PLUGIN_NAME = "LocalNotification";
  46. static protected final String STORAGE_FOLDER = "/localnotification";
  47. private static CordovaWebView webView = null;
  48. private static Boolean deviceready = false;
  49. protected static Context context = null;
  50. protected static Boolean isInBackground = true;
  51. private static ArrayList<String> eventQueue = new ArrayList<String>();
  52. static Activity activity;
  53. Asset asset;
  54. Manager manager;
  55. NotificationWrapper nWrapper;
  56. @Override
  57. public void initialize (CordovaInterface cordova, CordovaWebView webView) {
  58. super.initialize(cordova, webView);
  59. LocalNotification.webView = super.webView;
  60. LocalNotification.context = super.cordova.getActivity().getApplicationContext();
  61. LocalNotification.activity = super.cordova.getActivity();
  62. this.asset = new Asset(activity,STORAGE_FOLDER);
  63. this.manager = new Manager(context, PLUGIN_NAME);
  64. this.nWrapper = new NotificationWrapper(context,Receiver.class,PLUGIN_NAME,Receiver.OPTIONS);
  65. }
  66. @Override
  67. public boolean execute (String action, final JSONArray args, final CallbackContext command) throws JSONException {
  68. if (action.equalsIgnoreCase("add")) {
  69. cordova.getThreadPool().execute( new Runnable() {
  70. public void run() {
  71. add(args);
  72. command.success();
  73. }
  74. });
  75. }
  76. if (action.equalsIgnoreCase("update")) {
  77. cordova.getThreadPool().execute( new Runnable() {
  78. public void run() {
  79. update(args);
  80. command.success();
  81. }
  82. });
  83. }
  84. if (action.equalsIgnoreCase("cancel")) {
  85. cordova.getThreadPool().execute( new Runnable() {
  86. public void run() {
  87. cancel(args);
  88. command.success();
  89. }
  90. });
  91. }
  92. if (action.equalsIgnoreCase("cancelAll")) {
  93. cordova.getThreadPool().execute( new Runnable() {
  94. public void run() {
  95. cancelAll(args);
  96. command.success();
  97. }
  98. });
  99. }
  100. if (action.equalsIgnoreCase("clear")) {
  101. cordova.getThreadPool().execute( new Runnable() {
  102. public void run() {
  103. clear(args);
  104. command.success();
  105. }
  106. });
  107. }
  108. if (action.equalsIgnoreCase("clearAll")) {
  109. cordova.getThreadPool().execute( new Runnable() {
  110. public void run() {
  111. clearAll(args);
  112. command.success();
  113. }
  114. });
  115. }
  116. if (action.equalsIgnoreCase("isScheduled")) {
  117. String id = args.optString(0);
  118. boolean isScheduled = manager.isScheduled(id);
  119. PluginResult result = new PluginResult(PluginResult.Status.OK, (isScheduled));
  120. command.sendPluginResult(result);
  121. }
  122. if (action.equalsIgnoreCase("isTriggered")) {
  123. String id = args.optString(0);
  124. boolean isTriggered = manager.isTriggered(id);
  125. PluginResult result = new PluginResult(PluginResult.Status.OK, isTriggered);
  126. command.sendPluginResult(result);
  127. }
  128. if (action.equalsIgnoreCase("exist")) {
  129. String id = args.optString(0);
  130. boolean exist = manager.exist(id);
  131. PluginResult result = new PluginResult(PluginResult.Status.OK, exist);
  132. command.sendPluginResult(result);
  133. }
  134. if (action.equalsIgnoreCase("getScheduledIds")) {
  135. JSONArray scheduledIds = manager.getScheduledIds();
  136. command.success(scheduledIds);
  137. }
  138. if (action.equalsIgnoreCase("getTriggeredIds")) {
  139. JSONArray triggeredIds = manager.getTriggeredIds();
  140. command.success(triggeredIds);
  141. }
  142. if (action.equalsIgnoreCase("getAllIds")) {
  143. JSONArray allIds = manager.getAllIds();
  144. command.success(allIds);
  145. }
  146. if (action.equalsIgnoreCase("getAll")) {
  147. JSONArray ids;
  148. JSONArray all;
  149. try{
  150. ids = args.getJSONArray(0);
  151. all = manager.getAll(ids);
  152. } catch (JSONException jse){
  153. all = manager.getAll();
  154. }
  155. command.success(all);
  156. }
  157. if (action.equalsIgnoreCase("getTriggered")) {
  158. JSONArray ids;
  159. JSONArray triggered;
  160. try{
  161. ids = args.getJSONArray(0);
  162. triggered = manager.getTriggered(ids);
  163. } catch (JSONException jse){
  164. triggered = manager.getTriggered();
  165. }
  166. command.success(triggered);
  167. }
  168. if (action.equalsIgnoreCase("getScheduled")) {
  169. JSONArray ids;
  170. JSONArray scheduled;
  171. try{
  172. ids = args.getJSONArray(0);
  173. scheduled = manager.getScheduled(ids);
  174. } catch (JSONException jse){
  175. scheduled = manager.getScheduled();
  176. }
  177. command.success(scheduled);
  178. }
  179. if (action.equalsIgnoreCase("deviceready")) {
  180. cordova.getThreadPool().execute( new Runnable() {
  181. public void run() {
  182. deviceready();
  183. }
  184. });
  185. }
  186. if (action.equalsIgnoreCase("pause")) {
  187. isInBackground = true;
  188. }
  189. if (action.equalsIgnoreCase("resume")) {
  190. isInBackground = false;
  191. }
  192. return true;
  193. }
  194. //------------------------------------------------exec-Functions-----------------------------------------------
  195. /**
  196. * Schedule notifications contained in the args-Array
  197. * @param args
  198. */
  199. private void add(JSONArray args){
  200. JSONArray notifications = args.optJSONArray(0);
  201. JSONObject arguments;
  202. for(int i=0;i<notifications.length();i++){
  203. arguments = notifications.optJSONObject(i);
  204. LOG.d("LocalNotification", arguments.toString());
  205. arguments = asset.parseURIs(arguments);
  206. Options options = new Options(context).parse(arguments);
  207. options.setInitDate();
  208. nWrapper.schedule(options);
  209. JSONArray data = new JSONArray().put(options.getJSONObject());
  210. fireEvent("add", options.getId(), options.getJSON(), data);
  211. }
  212. }
  213. /**
  214. * Update existing notifications
  215. * @param args
  216. */
  217. private void update(JSONArray args){
  218. JSONArray updates = args.optJSONArray(0);
  219. JSONObject updateContent;
  220. for(int i=0;i<updates.length();i++){
  221. updateContent = args.optJSONObject(i);
  222. nWrapper.update(updateContent);
  223. }
  224. }
  225. /**
  226. * Cancel scheduled Notifications
  227. * @param args
  228. */
  229. private void cancel(JSONArray args){
  230. JSONArray ids = args.optJSONArray(0);
  231. String id;
  232. for(int i=0;i<ids.length();i++){
  233. id = args.optString(i);
  234. nWrapper.cancel(id);
  235. JSONArray managerId = new JSONArray().put(id);
  236. JSONArray data = new JSONArray().put(manager.getAll(managerId));
  237. fireEvent("cancel", id, "",data);
  238. }
  239. }
  240. /**
  241. * Cancel all scheduled notifications
  242. * @param args
  243. */
  244. public void cancelAll(JSONArray args){
  245. JSONArray options = manager.getAll();
  246. nWrapper.cancelAll();
  247. String id;
  248. JSONObject arguments;
  249. for(int i=0;i<options.length();i++){
  250. arguments= (JSONObject) options.opt(i);
  251. JSONArray data = new JSONArray().put(arguments);
  252. id = arguments.optString("id");
  253. fireEvent("cancel", id, "",data);
  254. }
  255. }
  256. /**
  257. * Clear triggered notifications without cancel repeating.
  258. * @param args
  259. */
  260. public void clear(JSONArray args){
  261. JSONArray ids = args.optJSONArray(0);
  262. String id;
  263. for(int i=0;i<ids.length();i++){
  264. id = args.optString(i);
  265. nWrapper.clear(id);
  266. JSONArray managerId = new JSONArray().put(id);
  267. JSONArray data = new JSONArray().put(manager.getAll(managerId));
  268. fireEvent("clear", id, "",data);
  269. }
  270. }
  271. /**
  272. * Clear all triggered notifications without cancel repeating.
  273. * @param args
  274. */
  275. public void clearAll(JSONArray args){
  276. JSONArray options = manager.getAll();
  277. nWrapper.clearAll();
  278. String id;
  279. JSONObject arguments;
  280. for(int i=0;i<options.length();i++){
  281. arguments= (JSONObject) options.opt(i);
  282. JSONArray data = new JSONArray().put(arguments);
  283. id = arguments.optString("id");
  284. fireEvent("clear", id, "",data);
  285. }
  286. }
  287. //-------------------------------------------------------------------------------------------------------------
  288. /**
  289. * Calls all pending callbacks after the deviceready event has been fired.
  290. */
  291. private static void deviceready () {
  292. deviceready = true;
  293. for (String js : eventQueue) {
  294. sendJavascript(js);
  295. }
  296. eventQueue.clear();
  297. }
  298. /**
  299. * Persist the information of this alarm to the Android Shared Preferences.
  300. * This will allow the application to restore the alarm upon device reboot.
  301. * Also this is used by the cancelAll method.
  302. *
  303. * @param alarmId
  304. * The Id of the notification that must be persisted.
  305. * @param args
  306. * The assumption is that parse has been called already.
  307. */
  308. public static void persist (String alarmId, JSONObject args) {
  309. Editor editor = getSharedPreferences().edit();
  310. if (alarmId != null) {
  311. editor.putString(alarmId, args.toString());
  312. if (Build.VERSION.SDK_INT<9) {
  313. editor.commit();
  314. } else {
  315. editor.apply();
  316. }
  317. }
  318. }
  319. /**
  320. * Remove a specific alarm from the Android shared Preferences.
  321. *
  322. * @param alarmId
  323. * The Id of the notification that must be removed.
  324. */
  325. public static void unpersist (String alarmId) {
  326. Editor editor = getSharedPreferences().edit();
  327. if (alarmId != null) {
  328. editor.remove(alarmId);
  329. if (Build.VERSION.SDK_INT<9) {
  330. editor.commit();
  331. } else {
  332. editor.apply();
  333. }
  334. }
  335. }
  336. /**
  337. * Clear all alarms from the Android shared Preferences.
  338. */
  339. public static void unpersistAll () {
  340. Editor editor = getSharedPreferences().edit();
  341. editor.clear();
  342. if (Build.VERSION.SDK_INT<9) {
  343. editor.commit();
  344. } else {
  345. editor.apply();
  346. }
  347. }
  348. //
  349. /**
  350. * Fires the given event.
  351. *
  352. * @param {String} event The Name of the event
  353. * @param {String} id The ID of the notification
  354. * @param {String} json A custom (JSON) string
  355. */
  356. public static void fireEvent (String event, String id, String json, JSONArray data) {
  357. String state = getApplicationState();
  358. //TODO dataArray handling
  359. String params = "\"" + id + "\",\"" + state + "\",\\'" + JSONObject.quote(json) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
  360. // params = params + "," + dataArray;
  361. String js = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)";
  362. // webview may available, but callbacks needs to be executed
  363. // after deviceready
  364. if (deviceready == false) {
  365. eventQueue.add(js);
  366. } else {
  367. sendJavascript(js);
  368. }
  369. }
  370. /**
  371. * Retrieves the application state
  372. *
  373. * @return {String}
  374. * Either "background" or "foreground"
  375. */
  376. protected static String getApplicationState () {
  377. return isInBackground ? "background" : "foreground";
  378. }
  379. /**
  380. * Set the application context if not already set.
  381. */
  382. protected static void setContext (Context context) {
  383. if (LocalNotification.context == null) {
  384. LocalNotification.context = context;
  385. }
  386. }
  387. /**
  388. * The Local storage for the application.
  389. */
  390. protected static SharedPreferences getSharedPreferences () {
  391. return context.getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
  392. }
  393. /**
  394. * The alarm manager for the application.
  395. */
  396. protected static AlarmManager getAlarmManager () {
  397. return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  398. }
  399. /**
  400. * The notification manager for the application.
  401. */
  402. protected static NotificationManager getNotificationManager () {
  403. return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  404. }
  405. /**
  406. * Use this instead of deprecated sendJavascript
  407. */
  408. @TargetApi(Build.VERSION_CODES.KITKAT)
  409. private static void sendJavascript(final String js){
  410. webView.post(new Runnable(){
  411. public void run(){
  412. if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){
  413. webView.evaluateJavascript(js, null);
  414. } else {
  415. webView.loadUrl("javascript:" + js);
  416. }
  417. }
  418. });
  419. }
  420. }