diff --git a/README.md b/README.md index 8bbfb09..a5b00c4 100644 --- a/README.md +++ b/README.md @@ -25,16 +25,31 @@ This plugin is meant to work with Cordova 3.5.0+. // all responses from the audio player are channeled through successCallback and errorCallback // set wakeup timer - window.wakeuptimer.wakeup( successCallback, - errorCallback, + window.wakeuptimer.wakeup( successCallback, + errorCallback, // a list of alarms to set { alarms : [{ type : 'onetime', time : { hour : 14, minute : 30 }, - extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, + extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, message : 'Alarm has expired!' - }] + }] + } + ); + + // set wakeup timer, but skip launch if app is already running + window.wakeuptimer.wakeup( successCallback, + errorCallback, + // a list of alarms to set + { + alarms : [{ + type : 'onetime', + skipOnRunning: true, + time : { hour : 11, minute : 20 }, + extra : { message : 'json containing app-specific information to be posted when alarm triggers' }, + message : 'Alarm has expired!' + }] } ); @@ -44,7 +59,7 @@ This plugin is meant to work with Cordova 3.5.0+. { alarms : [{ type : 'snooze', - time : { seconds : 60 }, // snooze for 60 seconds + time : { seconds : 60 }, // snooze for 60 seconds extra : { }, // json containing app-specific information to be posted when alarm triggers message : this.get('message'), sound : this.get('sound'), @@ -62,4 +77,4 @@ This plugin is meant to work with Cordova 3.5.0+. } else { console.log('wakeup unhandled type (' + result.type + ')'); } - }; + }; diff --git a/src/android/WakeupPlugin.java b/src/android/WakeupPlugin.java index 62e3b38..643c4ee 100644 --- a/src/android/WakeupPlugin.java +++ b/src/android/WakeupPlugin.java @@ -147,12 +147,13 @@ protected static void setAlarms(Context context, JSONArray alarms, boolean cance } JSONObject time=alarm.getJSONObject("time"); - + if ( type.equals("onetime")) { Calendar alarmDate=getOneTimeAlarmDate(time); Intent intent = new Intent(context, WakeupReceiver.class); if(alarm.has("extra")){ intent.putExtra("extra", alarm.getJSONObject("extra").toString()); + intent.putExtra("skipOnRunning", alarm.getBoolean("skipOnRunning")); intent.putExtra("type", type); } @@ -169,6 +170,7 @@ protected static void setAlarms(Context context, JSONArray alarms, boolean cance intent.putExtra("type", type); intent.putExtra("time", time.toString()); intent.putExtra("day", days.getString(j)); + intent.putExtra("skipOnRunning", alarm.getBoolean("skipOnRunning")); } setNotification(context, type, alarmDate, intent, ID_DAYLIST_OFFSET + daysOfWeek.get(days.getString(j))); @@ -180,6 +182,7 @@ protected static void setAlarms(Context context, JSONArray alarms, boolean cance if(alarm.has("extra")){ intent.putExtra("extra", alarm.getJSONObject("extra").toString()); intent.putExtra("type", type); + intent.putExtra("skipOnRunning", alarm.getBoolean("skipOnRunning")); } setNotification(context, type, alarmDate, intent, ID_SNOOZE_OFFSET); } diff --git a/src/android/WakeupReceiver.java b/src/android/WakeupReceiver.java index 36ae447..8252ebc 100644 --- a/src/android/WakeupReceiver.java +++ b/src/android/WakeupReceiver.java @@ -2,12 +2,14 @@ import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import org.apache.cordova.PluginResult; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; +import android.app.ActivityManager; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -21,29 +23,49 @@ public class WakeupReceiver extends BroadcastReceiver { private static final String LOG_TAG = "WakeupReceiver"; + public boolean isRunning(Context ctx) { + ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); + List tasks = activityManager.getRunningTasks(Integer.MAX_VALUE); + + for (ActivityManager.RunningTaskInfo task : tasks) { + if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) + return true; + } + + return false; + } + @SuppressLint({ "SimpleDateFormat", "NewApi" }) @Override public void onReceive(Context context, Intent intent) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Log.d(LOG_TAG, "wakeuptimer expired at " + sdf.format(new Date().getTime())); - + + Bundle extrasBundle = intent.getExtras(); + + if (extrasBundle.get("skipOnRunning").equals(true)) { + if (isRunning(context)) { + Log.d(LOG_TAG, "app is already running. No need to launch"); + return; + } + } + try { String packageName = context.getPackageName(); Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); - String className = launchIntent.getComponent().getClassName(); + String className = launchIntent.getComponent().getClassName(); Log.d(LOG_TAG, "launching activity for class " + className); @SuppressWarnings("rawtypes") - Class c = Class.forName(className); + Class c = Class.forName(className); Intent i = new Intent(context, c); i.putExtra("wakeup", true); - Bundle extrasBundle = intent.getExtras(); String extras=null; if (extrasBundle!=null && extrasBundle.get("extra")!=null) { extras = extrasBundle.get("extra").toString(); } - + if (extras!=null) { i.putExtra("extra", extras); } @@ -58,20 +80,20 @@ public void onReceive(Context context, Intent intent) { } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, o); pluginResult.setKeepCallback(true); - WakeupPlugin.connectionCallbackContext.sendPluginResult(pluginResult); + WakeupPlugin.connectionCallbackContext.sendPluginResult(pluginResult); } - + if (extrasBundle!=null && extrasBundle.getString("type")!=null && extrasBundle.getString("type").equals("daylist")) { // repeat in one week Date next = new Date(new Date().getTime() + (7 * 24 * 60 * 60 * 1000)); Log.d(LOG_TAG,"resetting alarm at " + sdf.format(next)); - + Intent reschedule = new Intent(context, WakeupReceiver.class); if (extras!=null) { reschedule.putExtra("extra", intent.getExtras().get("extra").toString()); } reschedule.putExtra("day", WakeupPlugin.daysOfWeek.get(intent.getExtras().get("day"))); - + PendingIntent sender = PendingIntent.getBroadcast(context, 19999 + WakeupPlugin.daysOfWeek.get(intent.getExtras().get("day")), intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if (Build.VERSION.SDK_INT>=19) {