Commit b539019b authored by Sebastián Katzer's avatar Sebastián Katzer
Browse files

Code rework

parent 787f4960
Loading
Loading
Loading
Loading
+66 −24
Changes for src/android/LocalNotificationReceiver.java: 66 added lines, 24 removed lines.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ import java.util.Calendar;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Notification.Builder;
@@ -32,17 +32,17 @@ import android.os.Bundle;
 * Android notification bar. The notification uses the default notification
 * sound and it vibrates the phone.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
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
	public void onReceive (Context context, Intent intent) {
		NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		LocalNotificationOptions options    = null;
		Bundle bundle                       = intent.getExtras();
		int id                              = 0;
		JSONObject args;

		try {
@@ -50,9 +50,24 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
			options = new LocalNotificationOptions(args);
		} catch (JSONException e) {}

		try {
			id = Integer.parseInt(options.getId());
		} catch (Exception e) {}
		this.context = context;
		this.options = options;

		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
@@ -61,6 +76,7 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
	 * 'forgotten' reminder for that day. Therefore we ignore the event
	 * if Android tries to 'catch up'.
	 */
	private Boolean isFirstAlarmInFuture () {
		if (options.getInterval() > 0) {
			Calendar now    = Calendar.getInstance();
			Calendar alarm  = options.getCalendar();
@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
			int currentMin  = now.get(Calendar.MINUTE);

			if (currentHour != alarmHour && currentMin != alarmMin) {
				return;
				return true;
			}
		}
		} else {
			LocalNotification.unpersist(options.getId());
		};

		String packageName          = context.getPackageName();
		Intent launchIntent         = context.getPackageManager().getLaunchIntentForPackage(packageName);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		return false;
	}

	/**
	 * Erstellt die Notification.
	 */
	private Builder buildNotification () {
		Builder notification = new Notification.Builder(context)
		.setContentTitle(options.getTitle())
		.setContentText(options.getSubTitle())
		.setNumber(options.getBadge())
		.setContentIntent(contentIntent)
		.setTicker(options.getTitle())
		.setSmallIcon(options.getIcon());

@@ -93,23 +109,49 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
			if (isInBackground(context)) {
				// app is in background
				notification.setDefaults(Notification.DEFAULT_SOUND);
			} else {
				// exec foreground callback
				invokeForegroundCallback(options);
			}
		} catch (Exception e) {
			// missing GET_TASKS permission
			notification.setDefaults(Notification.DEFAULT_SOUND);
		}

		/*
		 * If you want all reminders to stay in the notification bar, you should
		 * generate a random ID. If you want do replace an existing
		 * notification, make sure the ID below matches the ID that you store in
		 * the alarm intent.
		setClickEvent(notification);

		return notification;
	}

	/**
	 * 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());
        }
	}

	/**
	 * Gibt an, ob die App im Hintergrund läuft.