Android Open Source - RS-Text-Forwarder-Android-Client Sms Receiver






From Project

Back to project page RS-Text-Forwarder-Android-Client.

License

The source code is released under:

GNU General Public License

If you think the Android project RS-Text-Forwarder-Android-Client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.rolisoft.textforwarder;
/*w w w  .j  a  va 2 s  .co  m*/
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmsReceiver extends BroadcastReceiver {

    private final String TAG  = this.toString();
    private static final String STAG = SmsReceiver.class.toString();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i(TAG, "Received SMS broadcast...");
        BackgroundIntentService.start(context, intent, BackgroundIntentService.SMS_RECEIVED);
        setResultCode(Activity.RESULT_OK);
    }

    public static void handle(Context context, Intent intent)
    {
        SharedPreferences sp = context.getSharedPreferences("fwd", 0);
        if (!sp.getBoolean("forward", true) || !sp.getBoolean("forward_sms", true) || !MainActivity.isConnectedToInternet(context)) {
            Log.w(STAG, "Forwarding disabled or no internet connection.");
            return;
        }

        WakeLocker.pushd(context);

        try {
            List<TextMessage> messages = getMessagesFrom(context, intent);

            for (TextMessage msg : messages) {
                MainActivity.sendMessageAsync(context, sp, "send", msg.from, msg.body);
            }
        } catch (Exception ex) {
            Log.e(STAG, "Error while extracting messages from intent.", ex);
            MainActivity.displayNotification(context, "Request to send failed", "Local error: " + ex.getClass().getName() + ": " + ex.getMessage());
        } finally {
            WakeLocker.popd();
        }
    }

    public static List<TextMessage> getMessagesFrom(Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();
        List<TextMessage> messages = new ArrayList<TextMessage>();
        Map<String, Integer> msgidx = new HashMap<String, Integer>();

        if (bundle == null || !bundle.containsKey("pdus")) {
            return messages;
        }

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

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

        for (int i = 0; i < pdu.length; i++) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[])pdu[i]);

            String from = sms.getOriginatingAddress();
            String body = sms.getMessageBody();

            if (msgidx.containsKey(from)) {
                messages.get(msgidx.get(from)).append(body);
            } else {
                msgidx.put(from, messages.size());

                String sender = null;
                boolean isAlpha = false, isNetSpec = false, refDone = false;

                try {
                    Field wrappedSmsMessageField = sms.getClass().getDeclaredField("mWrappedSmsMessage");
                    wrappedSmsMessageField.setAccessible(true);
                    Object wrappedSmsMessageValue = wrappedSmsMessageField.get(sms);

                    Field originatingAddressField = wrappedSmsMessageValue.getClass().getSuperclass().getDeclaredField("originatingAddress");
                    originatingAddressField.setAccessible(true);
                    Object originatingAddressValue = originatingAddressField.get(wrappedSmsMessageValue);

                    Class originatingAddressClass = originatingAddressValue.getClass();
                    Method isAlphaMethod = originatingAddressClass.getDeclaredMethod("isAlphanumeric");
                    isAlphaMethod.setAccessible(true);
                    Method isNetSpecMethod = originatingAddressClass.getDeclaredMethod("isNetworkSpecific");
                    isNetSpecMethod.setAccessible(true);

                    isAlpha = (Boolean)isAlphaMethod.invoke(originatingAddressValue);
                    isNetSpec = (Boolean)isNetSpecMethod.invoke(originatingAddressValue);
                    refDone = true;
                } catch (Exception ex) { }

                if (refDone && (isAlpha || isNetSpec)) {
                    if (isAlpha) {
                        sender = ContactTools.createSlug(from);
                    } else if (isNetSpec) {
                        sender = ContactTools.cleanNumber(from);
                    }
                } else {
                    Contact contact = ContactTools.findContact(context, from, true);
                    if (contact != null) {
                        sender = ContactTools.createXmppAddrAutoSelCheck(context, contact);
                    } else {
                        if (ContactTools.isPhoneNumber(from)) {
                            sender = ContactTools.cleanNumber(from);
                        } else {
                            sender = ContactTools.createSlug(from);
                        }
                    }
                }

                messages.add(new TextMessage(sender, (double)sms.getTimestampMillis() / 1000.0, body));
            }
        }

        return messages;
    }

}




Java Source Code List

eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java
net.rolisoft.textforwarder.BackgroundIntentService.java
net.rolisoft.textforwarder.CallReceiver.java
net.rolisoft.textforwarder.ContactTools.java
net.rolisoft.textforwarder.Contact.java
net.rolisoft.textforwarder.MainActivity.java
net.rolisoft.textforwarder.MmsReceiver.java
net.rolisoft.textforwarder.PrngFix.java
net.rolisoft.textforwarder.PushReceiver.java
net.rolisoft.textforwarder.ServerError.java
net.rolisoft.textforwarder.SmsReceiver.java
net.rolisoft.textforwarder.TextMessage.java
net.rolisoft.textforwarder.Tuple.java
net.rolisoft.textforwarder.WakeLocker.java