All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pushlibrary.utils.NotificationUtils Maven / Gradle / Ivy

There is a newer version: 0.8
Show newest version
package pushlibrary.utils;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import java.util.Arrays;
import java.util.HashSet;

import pushlibrary.events.OnAfterLaunchNotification;
import pushlibrary.events.OnBeforeLaunchNotification;
import pushlibrary.notifications.EasypushNotification;

public class NotificationUtils {
	private static String DEFAULT_NOTIFICATION = "EP_default_notification";
	private static String LAUNCH_INTENT = "EP_launch_intent";
	private static String BEFORE_SEND = "EP_before_send";
	private static String AFTER_SEND = "EP_after_send";

	public static void registerDefaultNotification(Context c,
			Class notificationClass) {

		Editor editor = PushUtils.getGCMPreferences(c).edit();
		editor.putString(DEFAULT_NOTIFICATION, notificationClass.getName());
		editor.commit();
	}

	public static void unregisterDefaultNotification(Context c) {

		Editor editor = PushUtils.getGCMPreferences(c).edit();
		editor.remove(DEFAULT_NOTIFICATION);
		editor.commit();
	}

	public static EasypushNotification getDefaultNotification(Context c) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		String defaultNotificationClass = preferences.getString(
				DEFAULT_NOTIFICATION, "");
		if (!TextUtils.isEmpty(defaultNotificationClass)) {
			try {
				Object myClass = Class.forName(defaultNotificationClass)
						.newInstance();
				if (myClass instanceof EasypushNotification) {
					return (EasypushNotification) myClass;
				}
			} catch (Throwable t) {
			}
		}
		return null;
	}

	public static void setLaunchIntentOnNotification(Context c, Intent i) {
		Editor editor = PushUtils.getGCMPreferences(c).edit();
		editor.putString(LAUNCH_INTENT, i.getComponent().getClassName());
		editor.commit();
	}

	public static Intent getLaunchIntentOnNotification(Context c) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		String launchIntentClass = preferences.getString(LAUNCH_INTENT, "");
		if (!TextUtils.isEmpty(launchIntentClass)) {
			try {
				return new Intent(c, Class.forName(launchIntentClass));
			} catch (ClassNotFoundException e) {
			}
		}
		return null;
	}

	public static void addOnBeforeLaunchNotification(Context c,
			Class onBeforeSend) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(BEFORE_SEND, "").split(",")));
		if (!set.contains(onBeforeSend.getName())) {
			set.add(onBeforeSend.getName());
		}
		Editor editor = preferences.edit();
		editor.putString(BEFORE_SEND, TextUtils.join(",", set.toArray()));
		editor.commit();
	}

	public static void removeOnBeforeLaunchNotification(Context c,
			Class onBeforeSend) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(BEFORE_SEND, "").split(",")));
		if (set.contains(onBeforeSend.getName())) {
			set.remove(onBeforeSend.getName());
		}
		Editor editor = preferences.edit();
		editor.putString(BEFORE_SEND, TextUtils.join(",", set.toArray()));
		editor.commit();

	}

	public static void preventAllOnBeforeLaunchNotification(Context c,
			Notification notification) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(BEFORE_SEND, "").split(",")));
		for (String className : set) {
			try {
				Object myClass = Class.forName(className).newInstance();
				if (myClass instanceof OnBeforeLaunchNotification) {
					((OnBeforeLaunchNotification) myClass)
							.beforeLaunchNotification(notification);
				}
			} catch (Throwable t) {
			}

		}

	}

	public static void addOnAfterLaunchNotification(Context c,
			Class onAfterSend) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(AFTER_SEND, "").split(",")));
		if (!set.contains(onAfterSend.getName())) {
			set.add(onAfterSend.getName());
		}
		Editor editor = preferences.edit();
		editor.putString(AFTER_SEND, TextUtils.join(",", set.toArray()));
		editor.commit();

	}

	public static void removeOnAfterLaunchNotification(Context c,
			Class onAfterSend) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(AFTER_SEND, "").split(",")));
		if (set.contains(onAfterSend.getName())) {
			set.remove(onAfterSend.getName());
		}
		Editor editor = preferences.edit();
		editor.putString(AFTER_SEND, TextUtils.join(",", set.toArray()));
		editor.commit();

	}

	public static void preventAllOnAfterLaunchNotification(Context c,
			Notification notification) {
		SharedPreferences preferences = PushUtils.getGCMPreferences(c);
		HashSet set = new HashSet(Arrays.asList(preferences
				.getString(AFTER_SEND, "").split(",")));
		for (String className : set) {
			try {
				Object myClass = Class.forName(className).newInstance();
				if (myClass instanceof OnBeforeLaunchNotification) {
					((OnBeforeLaunchNotification) myClass)
							.beforeLaunchNotification(notification);
				}
			} catch (Throwable t) {
			}

		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy