package com.anjlab.android.smsgateway.gcm;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.util.Log;

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


public class SmsIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;

    public SmsIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "SMS Gateway";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            	String number = extras.getString("number");
            	String message = extras.getString("message");
            	if (!TextUtils.isEmpty(number) && !TextUtils.isEmpty(message)) {
            		try {
                        if (!number.startsWith("+"))
                        {
                            number = "+" + number;
                        }
            			SmsManager smsManager = SmsManager.getDefault();
            			smsManager.sendTextMessage(number, null, message, null, null);

            			String result = number + ": " + message;
            			Log.i(TAG, result);

            			sendNotification(result);

            			ContentValues values = new ContentValues();
            			values.put("address", number);
            			values.put("body", message); 
            			getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
            		}
            		catch (Exception ex) {
            			Log.e(TAG, ex.toString());
            		}
            	}
            }
        }
        SmsBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

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

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle(getText(R.string.app_name))
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg).setAutoCancel(true);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

@@ -497,16 +497,44 @@
 aging;%0A%0A
+import java.util.ArrayList;%0A
 %0Apublic 
@@ -872,16 +872,20 @@
  intent)
+%0A   
  %7B%0A     
@@ -1086,16 +1086,24 @@
 Empty())
+%0A       
  %7B%0A     
@@ -1175,16 +1175,28 @@
 geType))
+%0A           
  %7B%0A     
@@ -1198,25 +1198,28 @@
             
-%09
+    
 String numbe
@@ -1262,17 +1262,20 @@
         
+   
-%09
+ 
 String m
@@ -1324,17 +1324,20 @@
         
-%09
+    
 if (!Tex
@@ -1394,28 +1394,70 @@
 ge))
- %7B%0A            %09%09try
+%0A                %7B%0A                    try%0A                   
  %7B%0A 
@@ -1623,19 +1623,28 @@
         
-%09%09%09
+            
 SmsManag
@@ -1696,19 +1696,368 @@
         
-%09%09%09
+            ArrayList%3CString%3E parts = smsManager.divideMessage(message);%0A                        if (parts.size() %3E 1)%0A                        %7B%0A                            smsManager.sendMultipartTextMessage(number, null, parts, null, null);%0A                        %7D%0A                        else%0A                        %7B%0A                            
 smsManag
@@ -2111,17 +2111,49 @@
  null);%0A
-%0A
+                        %7D%0A%0A      
         
@@ -2156,19 +2156,22 @@
         
-%09%09%09
+      
 String r
@@ -2211,27 +2211,36 @@
             
-%09%09%09
+            
 Log.i(TAG, r
@@ -2260,19 +2260,28 @@
         
+         
-%09%09%09
+   
 sendNoti
@@ -2311,19 +2311,28 @@
         
-%09%09%09
+            
 ContentV
@@ -2371,35 +2371,44 @@
 ();%0A            
-%09%09%09
+            
 values.put(%22addr
@@ -2434,19 +2434,28 @@
         
-%09%09%09
+            
 values.p
@@ -2474,17 +2474,16 @@
 essage);
- 
 %0A       
@@ -2487,19 +2487,28 @@
         
-%09%09%09
+            
 getAppli
@@ -2543,16 +2543,64 @@
 solver()
+%0A                                               
 .insert(
@@ -2645,34 +2645,40 @@
 s);%0A            
-%09%09
+        
 %7D%0A            %09%09
@@ -2675,18 +2675,24 @@
         
-%09%09
+        
 catch (E
@@ -2703,19 +2703,48 @@
 tion ex)
- %7B%0A
+%0A                    %7B%0A         
         
@@ -2751,11 +2751,11 @@
     
-%09%09%09
+   
 Log.
@@ -2793,10 +2793,16 @@
     
-%09%09
+        
 %7D%0A  
@@ -2815,9 +2815,12 @@
     
-%09
+    
 %7D%0A  

send multipart sms messages