Android Open Source - info-mailer S M Sand Phone Receiver






From Project

Back to project page info-mailer.

License

The source code is released under:

MIT License

If you think the Android project info-mailer 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 hu.thewolf.infomailer;
//from   www.j  av a2s . c  o  m
import static hu.thewolf.infomailer.CONSTANTS.SEP;
import static hu.thewolf.infomailer.CONSTANTS.DATEFORMAT;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;

public class SMSandPhoneReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();    
    if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
      boolean success = true;
      Bundle bundle = intent.getExtras(); // ---get the SMS message passed in---
      SmsMessage[] msgs = null;
      ArrayList<Pair<String, String>> smses = new ArrayList<Pair<String,String>>();
      HashMap<String, String> msgMap = new HashMap<String, String>();
      if (bundle != null) {
        // ---retrieve the SMS message received---
        try {
          Object[] pdus = (Object[]) bundle.get("pdus");
          msgs = new SmsMessage[pdus.length];
          msgMap = new HashMap<String, String>(pdus.length);
          for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String msg_from = msgs[i].getOriginatingAddress();
            String msg_body = msgs[i].getMessageBody();    
            
                      if (!msgMap.containsKey(msg_from)) {
                        msgMap.put(msg_from, msg_body);                         
                      } else {    
                          msgMap.put(msg_from, msgMap.get(msg_from) + msg_body);
                      }

          }
        } catch (Exception e) {
          Log.e("INCOMING SMS(es)","ERROR on receiving", e);
          e.printStackTrace();
          smses.add(new Pair<String,String>("Exception caught", e.getMessage()));
          success= false;
        }        
        for (String msg_from : msgMap.keySet()) {
          String msg_body = msgMap.get(msg_from);
          smses.add(new Pair<String, String>(StringEscapeUtils.escapeHtml4(msg_from), StringEscapeUtils.escapeHtml4(msg_body)));
        }
      }      
      Intent eMailIntent = new Intent(context, NotifierService.class);
      String now = android.text.format.DateFormat.format(DATEFORMAT, new Date()).toString();
      if (smses.isEmpty()) {
        eMailIntent.putExtra(NotifierService.SUBJECT, "Something went wrong, 0 sms loaded");
        eMailIntent.putExtra(NotifierService.BODY, "It's not even an exception!");
      } else {
        if (smses.size() > 1) {        
          eMailIntent.putExtra(NotifierService.SUBJECT, "New messages" + (success?"":": some failed to receive"));
        } else {
          eMailIntent.putExtra(NotifierService.SUBJECT, "New message" + (success?" from: " + smses.get(0).first : ": failed to receive"));
        }
          StringBuilder sb = new StringBuilder();
          sb.append("At ").append(now).append(SEP).append(SEP);
          for (Pair<String, String> p : smses) {
            boolean number = StringUtils.isNumeric(p.first) || (p.first.startsWith("+") && StringUtils.isNumeric(p.first.substring(1)));
            sb.append("From: ");
            if (number) {
              sb.append("<a href=\"tel:").append(p.first).append("\">").append(p.first).append("</a>");
              //sb.append(SEP).append("<a href=\"sms:").append(p.first).append("\">").append("[Reply]").append("</a>")
            } else {
              sb.append(p.first);
            }
            sb.append(SEP).append("Text:").append(SEP).append(p.second).append(SEP).append("---------").append(SEP);
          }
          eMailIntent.putExtra(NotifierService.BODY, sb.toString());
      }
      context.startService(eMailIntent);
    } else if (action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
      IncomingPhoneStateListener iPSL = new IncomingPhoneStateListener(context);
      TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      telephony.listen(iPSL, PhoneStateListener.LISTEN_CALL_STATE);
      // action for phone state changed
    }
  }

}




Java Source Code List

com.android.internal.telephony.ITelephony.java
hu.thewolf.infomailer.CONSTANTS.java
hu.thewolf.infomailer.IncomingPhoneStateListener.java
hu.thewolf.infomailer.Mail.java
hu.thewolf.infomailer.MainActivity.java
hu.thewolf.infomailer.NotifierService.java
hu.thewolf.infomailer.SMSandPhoneReceiver.java