Example usage for android.content BroadcastReceiver getResultCode

List of usage examples for android.content BroadcastReceiver getResultCode

Introduction

In this page you can find the example usage for android.content BroadcastReceiver getResultCode.

Prototype

public final int getResultCode() 

Source Link

Document

Retrieve the current result code, as set by the previous receiver.

Usage

From source file:me.myatminsoe.myansms.SmsReceiver.java

static void handleOnReceive(final BroadcastReceiver receiver, final Context context, final Intent intent) {
    final String action = intent.getAction();
    final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakelock.acquire();/*from  w  ww.j  a  v  a  2s  .c  o  m*/

    try {

        Thread.sleep(SLEEP);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    String t = null;
    if (SenderActivity.MESSAGE_SENT_ACTION.equals(action)) {
        handleSent(context, intent, receiver.getResultCode());
    } else {
        boolean silent = false;

        if (ACTION_SMS_OLD.equals(action) || ACTION_SMS_NEW.equals(action)) {
            Bundle b = intent.getExtras();
            assert b != null;
            Object[] messages = (Object[]) b.get("pdus");
            SmsMessage[] smsMessage = new SmsMessage[messages.length];
            int l = messages.length;
            for (int i = 0; i < l; i++) {
                smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
            }
            t = null;
            if (l > 0) {
                // concatenate multipart SMS body
                StringBuilder sbt = new StringBuilder();
                for (int i = 0; i < l; i++) {
                    sbt.append(smsMessage[i].getMessageBody());
                }
                t = sbt.toString();

                // ! Check in blacklist db - filter spam
                String s = smsMessage[0].getDisplayOriginatingAddress();

                // this code is used to strip a forwarding agent and display the orginated number as sender
                final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                if (prefs.getBoolean(PreferencesActivity.PREFS_FORWARD_SMS_CLEAN, false) && t.contains(":")) {
                    Pattern smsPattern = Pattern.compile("([0-9a-zA-Z+]+):");
                    Matcher m = smsPattern.matcher(t);
                    if (m.find()) {
                        s = m.group(1);
                        // now strip the sender from the message
                        Pattern textPattern = Pattern.compile("^[0-9a-zA-Z+]+: (.*)");
                        Matcher m2 = textPattern.matcher(t);
                        if (t.contains(":") && m2.find()) {
                            t = m2.group(1);
                            Log.d(TAG, "stripped the message");
                        }
                    }
                }

                final SpamDB db = new SpamDB(context);
                db.open();
                if (db.isInDB(smsMessage[0].getOriginatingAddress())) {

                    silent = true;
                }
                db.close();

                if (action.equals(ACTION_SMS_NEW)) {
                    // API19+: save message to the database
                    ContentValues values = new ContentValues();
                    values.put("address", s);
                    values.put("body", t);
                    context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
                }
            }
        } else if (ACTION_MMS_OLD.equals(action) || ACTION_MMS_MEW.equals(action)) {
            t = MMS_BODY;
            // TODO API19+ MMS code
        }

        if (!silent) {

            int count = MAX_SPINS;
            do {

                try {

                    Thread.sleep(SLEEP);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                --count;
            } while (updateNewMessageNotification(context, t) <= 0 && count > 0);
            if (count == 0) { // use messages as they are available
                updateNewMessageNotification(context, null);
            }
        }
    }
    wakelock.release();
    Log.i(TAG, "wakelock released");
}

From source file:de.ub0r.android.smsdroid.SmsReceiver.java

static void handleOnReceive(final BroadcastReceiver receiver, final Context context, final Intent intent) {
    final String action = intent.getAction();
    Log.d(TAG, "onReceive(context, ", action, ")");
    final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakelock.acquire();/*  w w  w. j av  a 2 s  . co m*/
    Log.i(TAG, "got wakelock");
    Log.d(TAG, "got intent: ", action);
    try {
        Log.d(TAG, "sleep(", SLEEP, ")");
        Thread.sleep(SLEEP);
    } catch (InterruptedException e) {
        Log.d(TAG, "interrupted in spinlock", e);
        e.printStackTrace();
    }
    String text;
    if (SenderActivity.MESSAGE_SENT_ACTION.equals(action)) {
        handleSent(context, intent, receiver.getResultCode());
    } else {
        boolean silent = false;

        if (shouldHandleSmsAction(context, action)) {
            Bundle b = intent.getExtras();
            assert b != null;
            Object[] messages = (Object[]) b.get("pdus");
            SmsMessage[] smsMessage = new SmsMessage[messages.length];
            int l = messages.length;
            for (int i = 0; i < l; i++) {
                smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
            }
            text = null;
            if (l > 0) {
                // concatenate multipart SMS body
                StringBuilder sbt = new StringBuilder();
                for (int i = 0; i < l; i++) {
                    sbt.append(smsMessage[i].getMessageBody());
                }
                text = sbt.toString();

                // ! Check in blacklist db - filter spam
                String s = smsMessage[0].getDisplayOriginatingAddress();

                // this code is used to strip a forwarding agent and display the orginated number as sender
                final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                if (prefs.getBoolean(PreferencesActivity.PREFS_FORWARD_SMS_CLEAN, false)
                        && text.contains(":")) {
                    Pattern smsPattern = Pattern.compile("([0-9a-zA-Z+]+):");
                    Matcher m = smsPattern.matcher(text);
                    if (m.find()) {
                        s = m.group(1);
                        Log.d(TAG, "found forwarding sms number: (", s, ")");
                        // now strip the sender from the message
                        Pattern textPattern = Pattern.compile("^[0-9a-zA-Z+]+: (.*)");
                        Matcher m2 = textPattern.matcher(text);
                        if (text.contains(":") && m2.find()) {
                            text = m2.group(1);
                            Log.d(TAG, "stripped the message");
                        }
                    }
                }

                final SpamDB db = new SpamDB(context);
                db.open();
                if (db.isInDB(smsMessage[0].getOriginatingAddress())) {
                    Log.d(TAG, "Message from ", s, " filtered.");
                    silent = true;
                } else {
                    Log.d(TAG, "Message from ", s, " NOT filtered.");
                }
                db.close();

                if (action.equals(ACTION_SMS_NEW)) {
                    // API19+: save message to the database
                    ContentValues values = new ContentValues();
                    values.put("address", s);
                    values.put("body", text);
                    context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
                    Log.d(TAG, "Insert SMS into database: ", s, ", ", text);
                }
            }
            updateNotificationsWithNewText(context, text, silent);
        } else if (ACTION_MMS_OLD.equals(action) || ACTION_MMS_MEW.equals(action)) {
            text = MMS_BODY;
            // TODO API19+ MMS code
            updateNotificationsWithNewText(context, text, silent);
        }
    }
    wakelock.release();
    Log.i(TAG, "wakelock released");
}