package org.tndata.android.grow;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import org.tndata.android.grow.activity.DemoActivity;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    private String TAG = "GcmIntentService";

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

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.common_signin_btn_icon_dark)
                        .setContentTitle("GCM Notification")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
@@ -1429,16 +1429,131 @@
  Bundle%0A
+            Log.i(TAG, %22extra! extra! message %22 + extras.get(%22message%22) + %22 activity: %22 + extras.get(%22activity%22));%0A
         
@@ -2401,32 +2401,33 @@
 messageType)) %7B%0A
+%0A
                 
@@ -2430,374 +2430,129 @@
     
-// This loop represents the service doing some work.%0A                for (int i=0; i%3C5; i++) %7B%0A                    Log.i(TAG, %22Working... %22 + (i+1)%0A                            + %22/5 @ %22 + SystemClock.elapsedRealtime());%0A                    try %7B%0A                        Thread.sleep(5000);%0A                    %7D catch (InterruptedException e) %7B%0A                    %7D
+sendNotification(%0A                        extras.getString(%22message%22),%0A                        extras.getString(%22title%22),
 %0A   
@@ -2564,26 +2564,24 @@
         
-%7D%0A
         
         
@@ -2576,207 +2576,57 @@
     
-        Log.i(TAG, %22Completed work @ %22 + SystemClock.elapsedRealtime());%0A                // Post notification of received message.%0A                sendNotification(%22Received: %22 + extras.toString()
+extras.getString(%22activity%22)%0A                
 );%0A
+%0A
     
@@ -2845,24 +2845,281 @@
 nt);%0A    %7D%0A%0A
+    private void sendNotification(String msg) %7B%0A        sendNotification(msg, DemoActivity.class.getCanonicalName());%0A    %7D%0A%0A    private void sendNotification(String msg, String activity) %7B%0A        sendNotification(msg, %22TN Data Commons%22, activity);%0A    %7D%0A%0A
     // Put t
@@ -3295,32 +3295,63 @@
 ation(String msg
+, String title, String activity
 ) %7B%0A        mNot
@@ -3457,24 +3457,271 @@
 _SERVICE);%0A%0A
+        Class%3C?%3E cls;%0A        try %7B%0A            cls = Class.forName(activity);%0A        %7D%0A        catch (Exception e) %7B%0A            Log.d(TAG, %22Activity %22 + activity + %22 not found! Using default!%22);%0A            cls = DemoActivity.class;%0A        %7D%0A%0A
         Pend
@@ -3814,33 +3814,18 @@
 t(this, 
-DemoActivity.clas
+cl
 s), 0);%0A
@@ -3977,35 +3977,19 @@
 ble.
-common_signin_btn_icon_dark
+ic_launcher
 )%0A  
@@ -4031,26 +4031,13 @@
 tle(
-%22GCM Notification%22
+title
 )%0A  

Improve notifications when GCM message arrives