package de.adorsys.android.securemobilepush.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsMessage;
import android.util.Log;

import java.util.Arrays;
import java.util.List;

import de.adorsys.android.securemobilepush.BuildConfig;

public class SmsReceiver extends BroadcastReceiver {
    public static final String INTENT_ACTION_SMS = "intent_action_sms";
    public static final String KEY_SMS_SENDER = "key_sms_sender";
    public static final String KEY_SMS_MESSAGE = "key_sms_message";

    private static final String INTENT_ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(INTENT_ACTION_SMS_RECEIVED)) {
            List<String> smsSenderNumbers = Arrays.asList(SmsConfig.SMS_SENDER_NUMBERS);
            Bundle bundle = intent.getExtras();
            SmsMessage[] smsMessages;
            String messageFrom;
            if (bundle != null) {
                try {
                    //PDU = protocol data unit
                    //A PDU is a “protocol data unit”, which is the industry format for an SMS message.
                    //Because SMSMessage reads/writes them you shouldn’t need to disect them.
                    //A large message might be broken into many, which is why it is an array of objects.
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    if (pdus != null) {
                        smsMessages = new SmsMessage[pdus.length];
                        for (int i = 0; i < smsMessages.length; i++) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdus[i],
                                        bundle.getString("format"));
                            } else {
                                smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                            }
                            messageFrom = smsMessages[i].getOriginatingAddress();
                            if (smsSenderNumbers.contains(messageFrom)) {
                                sendBroadcast(context, messageFrom, smsMessages[i]);
                            }
                        }
                    }
                } catch (Exception e) {
                    if (BuildConfig.DEBUG) {
                        Log.d(SmsReceiver.class.getName(), e.getMessage());
                    }
                    sendBroadcast(context, null, null);
                }
            }
        }
    }

    private void sendBroadcast(@NonNull Context context,
                               @Nullable String messageFrom,
                               @Nullable SmsMessage smsMessage) {
        Intent broadcastIntent = new Intent(INTENT_ACTION_SMS);

        broadcastIntent.putExtra(KEY_SMS_SENDER, messageFrom);
        broadcastIntent.putExtra(KEY_SMS_MESSAGE, smsMessage != null
                ? getSmsCode(smsMessage.getMessageBody()) : null);
        LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);
    }

    @NonNull
    private String getSmsCode(@NonNull String message) {
        int startIndex = message.indexOf(SmsConfig.BEGIN_INDEX);
        int endIndex = message.indexOf(SmsConfig.END_INDEX);

        return message.substring(startIndex, endIndex).replace(SmsConfig.BEGIN_INDEX, "").trim();
    }
}
@@ -380,16 +380,47 @@
 essage;%0A
+import android.text.TextUtils;%0A
 import a
@@ -436,16 +436,16 @@
 il.Log;%0A
-
 %0Aimport 
@@ -1256,16 +1256,23 @@
 sageFrom
+ = null
 ;%0A      
@@ -1545,10 +1545,10 @@
 ould
+'
 n
-%E2%80%99
 t ne
@@ -1556,16 +1556,17 @@
 d to dis
+s
 ect them
@@ -1846,16 +1846,540 @@
 ength%5D;%0A
+                        // If the sent message is longer than 160 characters  it will be broken down%0A                        // in to chunks of 153 characters before being received on the device.%0A                        // To rectify that receivedMessage is the result of appending every single%0A                        // short message into one large one for our usage. see:%0A                        //http://www.textanywhere.net/faq/is-there-a-maximum-sms-message-length%0A                        String receivedMessage = %22%22;%0A
         
@@ -2680,24 +2680,125 @@
 %22format%22));%0A
+                                receivedMessage = receivedMessage + smsMessages%5Bi%5D.getMessageBody();%0A
             
@@ -2911,24 +2911,125 @@
 ) pdus%5Bi%5D);%0A
+                                receivedMessage = receivedMessage + smsMessages%5Bi%5D.getMessageBody();%0A
             
@@ -3152,24 +3152,113 @@
         
-    if (
+%7D%0A                        if (!TextUtils.isEmpty(messageFrom)%0A                                && 
 smsSende
@@ -3311,36 +3311,32 @@
                 
-    
 sendBroadcast(co
@@ -3359,54 +3359,25 @@
 om, 
-smsMessages%5Bi%5D);%0A                            %7D
+receivedMessage);
 %0A   
@@ -3869,25 +3869,21 @@
 llable S
-msMessage
+tring
  smsMess
@@ -4082,24 +4082,24 @@
 age != null%0A
+
             
@@ -4125,33 +4125,16 @@
 sMessage
-.getMessageBody()
 ) : null

Accept sms messages that are longer than the standard of 160 characters