Commit 39b26a6d by Sebastián Katzer

Convert indentations to spaces.

parent 52df6198
...@@ -34,142 +34,142 @@ import android.os.Bundle; ...@@ -34,142 +34,142 @@ import android.os.Bundle;
*/ */
public class LocalNotificationReceiver extends BroadcastReceiver { public class LocalNotificationReceiver extends BroadcastReceiver {
public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS"; public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
private Context context; private Context context;
private LocalNotificationOptions options; private LocalNotificationOptions options;
@Override @Override
public void onReceive (Context context, Intent intent) { public void onReceive (Context context, Intent intent) {
LocalNotificationOptions options = null; LocalNotificationOptions options = null;
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
JSONObject args; JSONObject args;
try { try {
args = new JSONObject(bundle.getString(OPTIONS)); args = new JSONObject(bundle.getString(OPTIONS));
options = new LocalNotificationOptions(args, context); options = new LocalNotificationOptions(args, context);
} catch (JSONException e) {} } catch (JSONException e) {}
this.context = context; this.context = context;
this.options = options; this.options = options;
if (options.getInterval() == 0) { if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId()); LocalNotification.unpersist(options.getId());
} else if (isFirstAlarmInFuture()) { } else if (isFirstAlarmInFuture()) {
return; return;
} }
Builder notification = buildNotification(); Builder notification = buildNotification();
if (!isInBackground(context)) { if (!isInBackground(context)) {
// exec foreground callback // exec foreground callback
invokeForegroundCallback(options); invokeForegroundCallback(options);
} }
showNotification(notification); showNotification(notification);
} }
/* /*
* If you set a repeating alarm at 11:00 in the morning and it * If you set a repeating alarm at 11:00 in the morning and it
* should trigger every morning at 08:00 o'clock, it will * should trigger every morning at 08:00 o'clock, it will
* immediately fire. E.g. Android tries to make up for the * immediately fire. E.g. Android tries to make up for the
* 'forgotten' reminder for that day. Therefore we ignore the event * 'forgotten' reminder for that day. Therefore we ignore the event
* if Android tries to 'catch up'. * if Android tries to 'catch up'.
*/ */
private Boolean isFirstAlarmInFuture () { private Boolean isFirstAlarmInFuture () {
if (options.getInterval() > 0) { if (options.getInterval() > 0) {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
Calendar alarm = options.getCalendar(); Calendar alarm = options.getCalendar();
int alarmHour = alarm.get(Calendar.HOUR_OF_DAY); int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
int alarmMin = alarm.get(Calendar.MINUTE); int alarmMin = alarm.get(Calendar.MINUTE);
int currentHour = now.get(Calendar.HOUR_OF_DAY); int currentHour = now.get(Calendar.HOUR_OF_DAY);
int currentMin = now.get(Calendar.MINUTE); int currentMin = now.get(Calendar.MINUTE);
if (currentHour != alarmHour && currentMin != alarmMin) { if (currentHour != alarmHour && currentMin != alarmMin) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Erstellt die Notification. * Erstellt die Notification.
*/ */
private Builder buildNotification () { private Builder buildNotification () {
Builder notification = new Notification.Builder(context) Builder notification = new Notification.Builder(context)
.setContentTitle(options.getTitle()) .setContentTitle(options.getTitle())
.setContentText(options.getMessage()) .setContentText(options.getMessage())
.setNumber(options.getBadge()) .setNumber(options.getBadge())
.setTicker(options.getTitle()) .setTicker(options.getTitle())
.setSmallIcon(options.getIcon()) .setSmallIcon(options.getIcon())
.setSound(options.getSound()); .setSound(options.getSound());
setClickEvent(notification); setClickEvent(notification);
return notification; return notification;
} }
/** /**
* Fügt der Notification einen onclick Handler hinzu. * Fügt der Notification einen onclick Handler hinzu.
*/ */
private Builder setClickEvent (Builder notification) { private Builder setClickEvent (Builder notification) {
String packageName = context.getPackageName(); String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return notification.setContentIntent(contentIntent); return notification.setContentIntent(contentIntent);
} }
/** /**
* Zeigt die Notification an. * Zeigt die Notification an.
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint("NewApi") @SuppressLint("NewApi")
private void showNotification (Builder notification) { private void showNotification (Builder notification) {
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int id = 0; int id = 0;
try { try {
id = Integer.parseInt(options.getId()); id = Integer.parseInt(options.getId());
} catch (Exception e) {} } catch (Exception e) {}
if (Build.VERSION.SDK_INT<16) { if (Build.VERSION.SDK_INT<16) {
// build notification for HoneyComb to ICS // build notification for HoneyComb to ICS
mgr.notify(id, notification.getNotification()); mgr.notify(id, notification.getNotification());
} else if (Build.VERSION.SDK_INT>15) { } else if (Build.VERSION.SDK_INT>15) {
// Notification for Jellybean and above // Notification for Jellybean and above
mgr.notify(id, notification.build()); mgr.notify(id, notification.build());
}
}
/**
* Gibt an, ob die App im Hintergrund läuft.
*/
private boolean isInBackground (Context context) {
return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
}
/**
* Ruft die `foreground` Callback Funktion auf.
*/
private void invokeForegroundCallback (LocalNotificationOptions options) {
String function = options.getForeground();
if (function != null) {
LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
}
}
/**
* Ruft die `background` Callback Funktion auf.
*/
private void invokeBackgroundCallback (LocalNotificationOptions options) {
String function = options.getBackground();
if (function != null) {
LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
} }
} }
/**
* Gibt an, ob die App im Hintergrund läuft.
*/
private boolean isInBackground (Context context) {
return !context.getPackageName().equalsIgnoreCase(((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName());
}
/**
* Ruft die `foreground` Callback Funktion auf.
*/
private void invokeForegroundCallback (LocalNotificationOptions options) {
String function = options.getForeground();
if (function != null) {
LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
}
}
/**
* Ruft die `background` Callback Funktion auf.
*/
private void invokeBackgroundCallback (LocalNotificationOptions options) {
String function = options.getBackground();
if (function != null) {
LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")");
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment