package com.centaurwarchief.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class SmsReceiver extends BroadcastReceiver {
    private ReactApplicationContext mContext;

    private static final String EVENT = "com.centaurwarchief.smslistener:smsReceived";

    public SmsReceiver() {
        super();
    }

    public SmsReceiver(ReactApplicationContext context) {
        mContext = context;
    }

    private void receiveMessage(SmsMessage message) {
        if (mContext == null) {
            return;
        }

        if (! mContext.hasActiveCatalystInstance()) {
            return;
        }

        WritableNativeMap receivedMessage = new WritableNativeMap();

        receivedMessage.putString("originatingAddress", message.getOriginatingAddress());
        receivedMessage.putString("body", message.getMessageBody());

        mContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EVENT, receivedMessage);
    }

    private void logReceivedMessage(SmsMessage message) {
        Log.d(
            SmsListener.TAG,
            String.format("%s: %s", message.getOriginatingAddress(), message.getMessageBody())
        );
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);

            if (messages.length == 0) {
                return;
            }

            logReceivedMessage(messages[0]);
            receiveMessage(messages[0]);

            return;
        }

        try {
            final Bundle bundle = intent.getExtras();

            if (bundle == null) {
                return;
            }

            if (! bundle.containsKey("pdus")) {
                return;
            }

            final Object[] pdus = (Object[]) bundle.get("pdus");

            for (Object pdu : pdus) {
                SmsMessage message;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    message = SmsMessage.createFromPdu((byte[]) pdu, bundle.getString("format"));
                } else {
                    message = SmsMessage.createFromPdu((byte[]) pdu);
                }

                logReceivedMessage(message);
                receiveMessage(message);
            }
        } catch (Exception e) {
            Log.e(SmsListener.TAG, e.getMessage());
        }
    }
}

@@ -991,32 +991,183 @@
 urn;%0A        %7D%0A%0A
+        Log.d(%0A            SmsListener.TAG,%0A            String.format(%22%25s: %25s%22, message.getOriginatingAddress(), message.getMessageBody())%0A        );%0A%0A
         Writable
@@ -1530,223 +1530,8 @@
  %7D%0A%0A
-    private void logReceivedMessage(SmsMessage message) %7B%0A        Log.d(%0A            SmsListener.TAG,%0A            String.format(%22%25s: %25s%22, message.getOriginatingAddress(), message.getMessageBody())%0A        );%0A    %7D%0A%0A
     
@@ -1671,32 +1671,37 @@
 ) %7B%0A            
+for (
 SmsMessage%5B%5D mes
@@ -1698,18 +1698,16 @@
 sage
-%5B%5D
  message
 s = 
@@ -1702,19 +1702,18 @@
  message
-s =
+ :
  Telepho
@@ -1760,113 +1760,35 @@
 ent)
-;%0A%0A            if (messages.length == 0) %7B%0A                return;%0A            %7D%0A%0A            logR
+) %7B%0A                r
 eceive
-d
 Mess
@@ -1798,20 +1798,16 @@
 (message
-s%5B0%5D
 );%0A     
@@ -1817,36 +1817,9 @@
     
-receiveMessage(messages%5B0%5D);
+%7D
 %0A%0A  
@@ -1950,67 +1950,12 @@
 null
-) %7B%0A                return;%0A            %7D%0A%0A            if (
+ %7C%7C 
 ! bu
@@ -2145,394 +2145,61 @@
     
-SmsMessage message;%0A%0A                if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A                    message = SmsMessage.createFromPdu((byte%5B%5D) pdu, bundle.getString(%22format%22));%0A                %7D else %7B%0A                    message = SmsMessage.createFromPdu((byte%5B%5D) pdu);%0A                %7D%0A%0A                logReceivedMessage(message);%0A                receiveMessage(message
+receiveMessage(SmsMessage.createFromPdu((byte%5B%5D) pdu)
 );%0A 

Simplify the code