Commit b539019b by Sebastián Katzer

Code rework

parent 787f4960
...@@ -14,7 +14,7 @@ import java.util.Calendar; ...@@ -14,7 +14,7 @@ import java.util.Calendar;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.annotation.TargetApi; import android.annotation.SuppressLint;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.Notification; import android.app.Notification;
import android.app.Notification.Builder; import android.app.Notification.Builder;
...@@ -32,17 +32,17 @@ import android.os.Bundle; ...@@ -32,17 +32,17 @@ import android.os.Bundle;
* Android notification bar. The notification uses the default notification * Android notification bar. The notification uses the default notification
* sound and it vibrates the phone. * sound and it vibrates the phone.
*/ */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class LocalNotificationReceiver extends BroadcastReceiver { public class LocalNotificationReceiver extends BroadcastReceiver {
public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS"; public final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
private Context context;
private LocalNotificationOptions options;
@Override @Override
public void onReceive (Context context, Intent intent) { public void onReceive (Context context, Intent intent) {
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
LocalNotificationOptions options = null; LocalNotificationOptions options = null;
Bundle bundle = intent.getExtras(); Bundle bundle = intent.getExtras();
int id = 0;
JSONObject args; JSONObject args;
try { try {
...@@ -50,17 +50,33 @@ public class LocalNotificationReceiver extends BroadcastReceiver { ...@@ -50,17 +50,33 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
options = new LocalNotificationOptions(args); options = new LocalNotificationOptions(args);
} catch (JSONException e) {} } catch (JSONException e) {}
try { this.context = context;
id = Integer.parseInt(options.getId()); this.options = options;
} catch (Exception e) {}
if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId());
} else if (isFirstAlarmInFuture()) {
return;
}
Builder notification = buildNotification();
if (!isInBackground(context)) {
// exec foreground callback
invokeForegroundCallback(options);
}
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 () {
if (options.getInterval() > 0) { if (options.getInterval() > 0) {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
Calendar alarm = options.getCalendar(); Calendar alarm = options.getCalendar();
...@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver { ...@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
int currentMin = now.get(Calendar.MINUTE); int currentMin = now.get(Calendar.MINUTE);
if (currentHour != alarmHour && currentMin != alarmMin) { if (currentHour != alarmHour && currentMin != alarmMin) {
return; return true;
} }
} else { }
LocalNotification.unpersist(options.getId());
};
String packageName = context.getPackageName(); return false;
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); }
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/**
* Erstellt die Notification.
*/
private Builder buildNotification () {
Builder notification = new Notification.Builder(context) Builder notification = new Notification.Builder(context)
.setContentTitle(options.getTitle()) .setContentTitle(options.getTitle())
.setContentText(options.getSubTitle()) .setContentText(options.getSubTitle())
.setNumber(options.getBadge()) .setNumber(options.getBadge())
.setContentIntent(contentIntent)
.setTicker(options.getTitle()) .setTicker(options.getTitle())
.setSmallIcon(options.getIcon()); .setSmallIcon(options.getIcon());
...@@ -93,22 +109,48 @@ public class LocalNotificationReceiver extends BroadcastReceiver { ...@@ -93,22 +109,48 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
if (isInBackground(context)) { if (isInBackground(context)) {
// app is in background // app is in background
notification.setDefaults(Notification.DEFAULT_SOUND); notification.setDefaults(Notification.DEFAULT_SOUND);
} else {
// exec foreground callback
invokeForegroundCallback(options);
} }
} catch (Exception e) { } catch (Exception e) {
// missing GET_TASKS permission // missing GET_TASKS permission
notification.setDefaults(Notification.DEFAULT_SOUND); notification.setDefaults(Notification.DEFAULT_SOUND);
} }
/* setClickEvent(notification);
* If you want all reminders to stay in the notification bar, you should
* generate a random ID. If you want do replace an existing return notification;
* notification, make sure the ID below matches the ID that you store in }
* the alarm intent.
*/ /**
notificationMgr.notify(id, notification.build()); * Fügt der Notification einen onclick Handler hinzu.
*/
private Builder setClickEvent (Builder notification) {
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return notification.setContentIntent(contentIntent);
}
/**
* Zeigt die Notification an.
*/
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void showNotification (Builder notification) {
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int id = 0;
try {
id = Integer.parseInt(options.getId());
} catch (Exception e) {}
if (Build.VERSION.SDK_INT<16) {
// build notification for HoneyComb to ICS
notificationMgr.notify(id, notification.getNotification());
} else if (Build.VERSION.SDK_INT>15) {
// Notification for Jellybean and above
notificationMgr.notify(id, notification.build());
}
} }
/** /**
......
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