Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skipOnRunning property that skips app launch when its already running #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}]
}
);

Expand All @@ -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'),
Expand All @@ -62,4 +77,4 @@ This plugin is meant to work with Cordova 3.5.0+.
} else {
console.log('wakeup unhandled type (' + result.type + ')');
}
};
};
5 changes: 4 additions & 1 deletion src/android/WakeupPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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)));
Expand All @@ -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);
}
Expand Down
40 changes: 31 additions & 9 deletions src/android/WakeupReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ActivityManager.RunningTaskInfo> 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);
}
Expand All @@ -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) {
Expand Down