package edu.berkeley.eecs.emission.cordova.unifiedlogger;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import org.apache.cordova.CordovaActivity;

import edu.berkeley.eecs.emission.MainActivity;
import edu.berkeley.eecs.emission.R;

public class NotificationHelper {
	private static String TAG = "NotificationHelper";

	public static void createNotification(Context context, int id, String message) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
		builder.setSmallIcon(R.drawable.icon);
		builder.setContentTitle(context.getString(R.string.app_name));
		builder.setContentText(message);
		
		/*
		 * This is a bit of magic voodoo. The tutorial on launching the activity actually uses a stackbuilder
		 * to create a fake stack for the new activity. However, it looks like the stackbuilder
		 * is only available in more recent versions of the API. So I use the version for a special activity PendingIntent
		 * (since our app currently has only one activity) which resolves that issue.
		 * This also appears to work, at least in the emulator.
		 * 
		 * TODO: Decide what level API we want to support, and whether we want a more comprehensive activity.
		 */
		Intent activityIntent = new Intent(context, MainActivity.class);
		activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0,
				activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(activityPendingIntent);		
		
		NotificationManager nMgr =
				(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		
		Log.d(context, TAG, "Generating notify with id " + id + " and message " + message);
		nMgr.notify(id, builder.build());
	}
}

@@ -1887,11 +1887,1259 @@
 d());%0A%09%7D
+%0A%0A%09/*%0A%09 * Used to show a pending intent - e.g. to turn on location services%0A%09 */%0A%09public static void createNotification(Context context, int id, String message, PendingIntent intent) %7B%0A%09%09NotificationCompat.Builder builder = new NotificationCompat.Builder(context);%0A%09%09builder.setSmallIcon(R.drawable.icon);%0A%09%09builder.setContentTitle(context.getString(R.string.app_name));%0A%09%09builder.setContentText(message);%0A%0A%09%09/*%0A%09%09 * This is a bit of magic voodoo. The tutorial on launching the activity actually uses a stackbuilder%0A%09%09 * to create a fake stack for the new activity. However, it looks like the stackbuilder%0A%09%09 * is only available in more recent versions of the API. So I use the version for a special activity PendingIntent%0A%09%09 * (since our app currently has only one activity) which resolves that issue.%0A%09%09 * This also appears to work, at least in the emulator.%0A%09%09 *%0A%09%09 * TODO: Decide what level API we want to support, and whether we want a more comprehensive activity.%0A%09%09 */%0A%09%09builder.setContentIntent(intent);%0A%0A%09%09NotificationManager nMgr =%0A%09%09%09%09(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);%0A%0A%09%09Log.d(context, TAG, %22Generating notify with id %22 + id + %22 and message %22 + message);%0A%09%09nMgr.notify(id, builder.build());%0A%09%7D
 %0A%7D%0A

Add a new notification helper that takes the pending intent as an argument