package org.traccar.client;

import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.app.AlarmManager;

import static org.traccar.client.R.xml.preferences;

/**
 * Created by Nathan on 26/02/2017.
 */

public class SMSReceiver extends BroadcastReceiver {
    /**
     * The Action fired by the Android-System when a SMS was received.
     * We are using the Default Package-Visibility
     */

    private SharedPreferences preferences;
    private String cellphone;
    private static final String SMS_EXTRA_NAME = "pdus";
    private String format = "3gpp";

    private AlarmManager alarmManager;
    private PendingIntent alarmIntent;

    public static final String KEY_DEVICE = "id";
    public static final String KEY_ADDRESS = "address";
    public static final String KEY_PORT = "port";
    public static final String KEY_SECURE = "secure";
    public static final String KEY_INTERVAL = "interval";
    public static final String KEY_PROVIDER = "provider";
    public static final String KEY_STATUS = "status";
    public static final String KEY_CELLPHONE = "cellphone";
    public static final String KEY_SENDTOSERVER = "SendToServer";
    public static final String KEY_SENDSMS ="SendSMS";

    @Override
    public void onReceive(Context context, Intent intent) {

        preferences = PreferenceManager.getDefaultSharedPreferences(context);

        cellphone = preferences.getString(MainActivity.KEY_CELLPHONE, Constants.CELLPHONE);

        SmsMessage[] messages = null;
        String strMessage = "";
        Bundle myBundle;

        Log.d("REMOTE", "Message received");

        String action="";
        action = intent.getAction();

        if (action.equals(Constants.ACTION)) {
            myBundle = intent.getExtras();

            if (myBundle != null) {
                Object[] pdus = (Object[]) myBundle.get("pdus");

                messages = new SmsMessage[pdus.length];

                for (int i = 0; i < messages.length; i++) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = myBundle.getString("format");
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                    } else {
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    }
                    strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                    strMessage += " : ";
                    strMessage += messages[i].getMessageBody();
                    strMessage += "\n";
                }
                Log.d("SMS content", strMessage);

                if (strMessage.toString().contains(Constants.START_MESSAGE)) {
                    Log.d("REMOTE", "Starting service");
                    context.startService(new Intent(context, TrackingService.class));
                }

                if (strMessage.toString().contains(Constants.STOP_MESSAGE)) {
                    Log.d("REMOTE", "Stopping service");
                    stopTrackingService(context);
                }

            }
        }
    }

    private void stopTrackingService(Context context) {
        context.stopService(new Intent(context, TrackingService.class));
    }

}



@@ -935,570 +935,8 @@
 t;%0A%0A
-    public static final String KEY_DEVICE = %22id%22;%0A    public static final String KEY_ADDRESS = %22address%22;%0A    public static final String KEY_PORT = %22port%22;%0A    public static final String KEY_SECURE = %22secure%22;%0A    public static final String KEY_INTERVAL = %22interval%22;%0A    public static final String KEY_PROVIDER = %22provider%22;%0A    public static final String KEY_STATUS = %22status%22;%0A    public static final String KEY_CELLPHONE = %22cellphone%22;%0A    public static final String KEY_SENDTOSERVER = %22SendToServer%22;%0A    public static final String KEY_SENDSMS =%22SendSMS%22;%0A%0A
     
@@ -1084,17 +1084,16 @@
 ntext);%0A
-%0A
         
@@ -1633,16 +1633,43 @@
 ngth%5D;%0A%0A
+                outerloop:%0A
         
@@ -1716,24 +1716,25 @@
 gth; i++) %7B%0A
+%0A
             
@@ -2080,32 +2080,33 @@
               %7D%0A
+%0A
                 
@@ -2113,36 +2113,122 @@
     
-strMessage += %22SMS From: %22 +
+String sender=%22%22;%0A                    //sender = messages%5Bi%5D.getOriginatingAddress();%0A                    sender =
  mes
@@ -2231,32 +2231,39 @@
  messages%5Bi%5D.get
+Display
 OriginatingAddre
@@ -2292,26 +2292,30 @@
     
-strM
+String m
 essage
- += %22 : 
+Body = %22
 %22;%0A 
@@ -2333,28 +2333,28 @@
         
-strM
+m
 essage
+Body
  
-+
 = messag
@@ -2373,24 +2373,25 @@
 sageBody();%0A
+%0A
             
@@ -2417,11 +2417,28 @@
 += %22
-%5Cn%22
+SMS From: %22 + sender
 ;%0A  
@@ -2455,46 +2455,99 @@
     
-%7D%0A                Log.d(%22SMS content%22,
+    strMessage += %22 : %22;%0A                    strMessage += messageBody;%0A                   
  str
@@ -2553,17 +2553,24 @@
 rMessage
-)
+ += %22%5Cn%22
 ;%0A%0A     
@@ -2580,39 +2580,153 @@
         
+    
 if (s
-trMessage.toString
+ender.toUpperCase().contains(cellphone.toUpperCase()))%0A                    %7B%0A                        if (messageBody.toUpperCase
 ().conta
@@ -2744,37 +2744,59 @@
 ts.START_MESSAGE
-)) %7B%0A
+.toUpperCase())) %7B%0A        
                 
@@ -2848,32 +2848,40 @@
                 
+        
 context.startSer
@@ -2926,24 +2926,77 @@
 ce.class));%0A
+                            break outerloop;%0A        
             
@@ -3022,31 +3022,43 @@
     
-if (strMessage.toString
+        if (messageBody.toUpperCase
 ().c
@@ -3087,21 +3087,43 @@
 _MESSAGE
-)) %7B%0A
+.toUpperCase())) %7B%0A        
         
@@ -3195,38 +3195,134 @@
     
-stopTrackingService(context);%0A
+        context.stopService(new Intent(context, TrackingService.class));%0A                            break outerloop;%0A        
     
@@ -3344,26 +3344,24 @@
             
-%7D%0A
         %7D%0A  
@@ -3362,143 +3362,100 @@
   %7D%0A
+%0A            
     %7D%0A
-%0A
     
-private void stopTrackingService(Context context) %7B%0A        context.stopService(new Intent(context, TrackingService.class));
+            Log.d(%22SMS content%22, strMessage);%0A            %7D%0A        %7D
 %0A   

Validate sender