/* Copyright 2013-2014 appPlant UG Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package de.appplant.cordova.plugin.localnotification; import java.util.ArrayList; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; import org.apache.cordova.LOG; import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlarmManager; import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Build; import android.annotation.TargetApi; import de.appplant.cordova.plugin.notification.*; /** * This plugin utilizes the Android AlarmManager in combination with StatusBar * notifications. When a local notification is scheduled the alarm manager takes * care of firing the event. When the event is processed, a notification is put * in the Android status bar. */ public class LocalNotification extends CordovaPlugin { protected final static String PLUGIN_NAME = "LocalNotification"; static protected final String STORAGE_FOLDER = "/localnotification"; private static CordovaWebView webView = null; private static Boolean deviceready = false; protected static Context context = null; protected static Boolean isInBackground = true; private static ArrayList eventQueue = new ArrayList(); static Activity activity; Asset asset; Manager manager; NotificationWrapper nWrapper; @Override public void initialize (CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); LocalNotification.webView = super.webView; LocalNotification.context = super.cordova.getActivity().getApplicationContext(); LocalNotification.activity = super.cordova.getActivity(); this.asset = new Asset(activity,STORAGE_FOLDER); this.manager = new Manager(context, PLUGIN_NAME); this.nWrapper = new NotificationWrapper(context,Receiver.class,PLUGIN_NAME,Receiver.OPTIONS); } @Override public boolean execute (String action, final JSONArray args, final CallbackContext command) throws JSONException { if (action.equalsIgnoreCase("add")) { cordova.getThreadPool().execute( new Runnable() { public void run() { add(args); command.success(); } }); } if (action.equalsIgnoreCase("update")) { cordova.getThreadPool().execute( new Runnable() { public void run() { update(args); command.success(); } }); } if (action.equalsIgnoreCase("cancel")) { cordova.getThreadPool().execute( new Runnable() { public void run() { cancel(args); command.success(); } }); } if (action.equalsIgnoreCase("cancelAll")) { cordova.getThreadPool().execute( new Runnable() { public void run() { cancelAll(args); command.success(); } }); } if (action.equalsIgnoreCase("clear")) { cordova.getThreadPool().execute( new Runnable() { public void run() { clear(args); command.success(); } }); } if (action.equalsIgnoreCase("clearAll")) { cordova.getThreadPool().execute( new Runnable() { public void run() { clearAll(args); command.success(); } }); } if (action.equalsIgnoreCase("isScheduled")) { String id = args.optString(0); boolean isScheduled = manager.isScheduled(id); PluginResult result = new PluginResult(PluginResult.Status.OK, (isScheduled)); command.sendPluginResult(result); } if (action.equalsIgnoreCase("isTriggered")) { String id = args.optString(0); boolean isTriggered = manager.isTriggered(id); PluginResult result = new PluginResult(PluginResult.Status.OK, isTriggered); command.sendPluginResult(result); } if (action.equalsIgnoreCase("exist")) { String id = args.optString(0); boolean exist = manager.exist(id); PluginResult result = new PluginResult(PluginResult.Status.OK, exist); command.sendPluginResult(result); } if (action.equalsIgnoreCase("getScheduledIds")) { JSONArray scheduledIds = manager.getScheduledIds(); command.success(scheduledIds); } if (action.equalsIgnoreCase("getTriggeredIds")) { JSONArray triggeredIds = manager.getTriggeredIds(); command.success(triggeredIds); } if (action.equalsIgnoreCase("getAllIds")) { JSONArray allIds = manager.getAllIds(); command.success(allIds); } if (action.equalsIgnoreCase("getAll")) { JSONArray ids; JSONArray all; try{ ids = args.getJSONArray(0); all = manager.getAll(ids); } catch (JSONException jse){ all = manager.getAll(); } command.success(all); } if (action.equalsIgnoreCase("getTriggered")) { JSONArray ids; JSONArray triggered; try{ ids = args.getJSONArray(0); triggered = manager.getTriggered(ids); } catch (JSONException jse){ triggered = manager.getTriggered(); } command.success(triggered); } if (action.equalsIgnoreCase("getScheduled")) { JSONArray ids; JSONArray scheduled; try{ ids = args.getJSONArray(0); scheduled = manager.getScheduled(ids); } catch (JSONException jse){ scheduled = manager.getScheduled(); } command.success(scheduled); } if (action.equalsIgnoreCase("deviceready")) { cordova.getThreadPool().execute( new Runnable() { public void run() { deviceready(); } }); } if (action.equalsIgnoreCase("pause")) { isInBackground = true; } if (action.equalsIgnoreCase("resume")) { isInBackground = false; } return true; } //------------------------------------------------exec-Functions----------------------------------------------- /** * Schedule notifications contained in the args-Array * @param args */ private void add(JSONArray args){ JSONArray notifications = args.optJSONArray(0); JSONObject arguments; for(int i=0;i= Build.VERSION_CODES.KITKAT){ webView.evaluateJavascript(js, null); } else { webView.loadUrl("javascript:" + js); } } }); } }