package com.plugin.gcm;

import com.google.android.gcm.GCMBaseIntentService;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

	public static final int NOTIFICATION_ID = 237;

    private static String TAG = "PushPlugin-GCMIntentService";

    public static final String MESSAGE = "message";

    public static final String COLDSTART = "coldstart";

    public GCMIntentService() {
		super("GCMIntentService");
	}

	@Override
	public void onRegistered(Context context, String regId) {
		Log.v(TAG, "onRegistered: "+ regId);
        NotificationService.getInstance(context).onRegistered(regId);
	}

	@Override
	public void onUnregistered(Context context, String regId) {
		Log.d(TAG, "onUnregistered - regId: " + regId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		boolean isPushPluginActive = NotificationService.getInstance(context).isActive();

        Log.d(TAG, "onMessage - isPushPluginActive: " + isPushPluginActive);

		Bundle extras = intent.getExtras();
		if (extras != null) {

            if (!isPushPluginActive) {
                extras.putBoolean(COLDSTART, true);
            }
            NotificationService.getInstance(context).onMessage(extras);

			if (extras.getString(MESSAGE) != null && extras.getString(MESSAGE).length() != 0) {
				createNotification(context, extras);
			}
		}
	}

	public void createNotification(Context context, Bundle extras)
	{
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		String appName = getAppName(this);

		Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		notificationIntent.putExtra("pushBundle", extras);

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		int defaults = Notification.DEFAULT_ALL;

		if (extras.getString("defaults") != null) {
			try {
				defaults = Integer.parseInt(extras.getString("defaults"));
			}
			catch (NumberFormatException e) {}
		}
		
		Bitmap myBitmap = null;
		String customIconUrl = null;
		customIconUrl = extras.getString("icon");
		if (customIconUrl != null) {
			try {
				URL url = new URL(customIconUrl);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setDoInput(true);
				connection.connect();
				InputStream input = connection.getInputStream();
				myBitmap = BitmapFactory.decodeStream(input);
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		
		NotificationCompat.Builder mBuilder =
			new NotificationCompat.Builder(context)
				.setDefaults(defaults)
				.setSmallIcon(context.getApplicationInfo().icon)
				.setLargeIcon(myBitmap)
				.setWhen(System.currentTimeMillis())
				.setContentTitle(extras.getString("title"))
				.setTicker(extras.getString("title"))
				.setContentIntent(contentIntent)
				.setAutoCancel(true);

		String message = extras.getString("message");
		if (message != null) {
			mBuilder.setContentText(message);
		} else {
			mBuilder.setContentText("<missing message content>");
		}

		String msgcnt = extras.getString("msgcnt");
		if (msgcnt != null) {
			mBuilder.setNumber(Integer.parseInt(msgcnt));
		}

		int notId = NOTIFICATION_ID;

		try {
			notId = Integer.parseInt(extras.getString("notId"));
		}
		catch(NumberFormatException e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
		}
		catch(Exception e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
		}

		mNotificationManager.notify((String) appName, notId, mBuilder.build());
		
	}

	public static void cancelNotification(Context context)
	{
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);
	}

	private static String getAppName(Context context)
	{
		CharSequence appName =
				context
					.getPackageManager()
					.getApplicationLabel(context.getApplicationInfo());

		return (String)appName;
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e(TAG, "onError - errorId: " + errorId);
	}

}

@@ -875,16 +875,20 @@
 start%22;%0A
+    
 %0A    pub
@@ -1409,25 +1409,111 @@
 tive();%0A
-%0A        
+%09%09boolean isPushPluginForeground = NotificationService.getInstance(context).isForeground();%0A%0A%09%09
 Log.d(TA
@@ -1636,29 +1636,19 @@
 null) %7B%0A
-%0A            
+%09%09%09
 if (!isP
@@ -1670,24 +1670,12 @@
 ) %7B%0A
-                
+%09%09%09%09
 extr
@@ -1710,22 +1710,14 @@
 e);%0A
-            
+%09%09%09
 %7D%0A
+%09
     
@@ -1716,32 +1716,36 @@
 %09%7D%0A%09            
+%0A%09%09%09
 NotificationServ
@@ -1788,19 +1788,67 @@
 xtras);%0A
-%0A
 %09%09
+%09if (!isPushPluginForeground) %7B%0A%09                
 %09if (ext
@@ -1928,16 +1928,17 @@
 ) %7B%0A%09%09%09%09
+%09
 createNo
@@ -1966,16 +1966,22 @@
 xtras);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Update GCMIntentService.java