Android Open Source - TATupload Sms Receiver






From Project

Back to project page TATupload.

License

The source code is released under:

GNU General Public License

If you think the Android project TATupload 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 uk.org.sucu.tatupload.message;
/*  w  w  w  .j  ava2s  .c  o m*/
import java.util.Collection;
import java.util.HashMap;

import uk.org.sucu.tatupload.BrowserAccessor;
import uk.org.sucu.tatupload.NetCaller;
import uk.org.sucu.tatupload.Notifications;
import uk.org.sucu.tatupload.Settings;
import uk.org.sucu.tatupload.parse.Parser;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver{

  //TODO option to upload names from contacts with messages
  @Override
  public void onReceive(Context context, Intent intent) {
    //only proceed if we're processing
    Settings settings = new Settings(context);
    if(settings.getProcessingTexts()){

      //get the SMS message passed in
      Bundle bundle = intent.getExtras();
      SmsMessage[] msgs = null;
      if (bundle != null){
        //retrieve the SMS message received
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        HashMap<String,Text> numberBodyMap = new HashMap<String,Text>();
        
        for (int i = 0; i < msgs.length; i++){
          //createFromPdu to be deprecated soon
          msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
          //if 2 texts in the same receive are from the same number, merge them
          String number = msgs[i].getOriginatingAddress();
          if(numberBodyMap.containsKey(number)){
            Text currentMsg = numberBodyMap.get(number);
            currentMsg.appendBody(msgs[i].getMessageBody());
          } else {
            numberBodyMap.put(number, new Text(msgs[i]));
          }
        }

        Collection<Text> texts = numberBodyMap.values();

        //process the message!
        if(settings.getAutoQueueTexts()){
          //queue it if set to confirm before upload, the browser is not set/is uninstalled, or there's no network connection
          queueMessages(texts, context);
        } else if(!BrowserAccessor.usable(context)){
          queueMessages(texts, context);
        } else if(!NetCaller.isOnline(context)){
          //TODO PLAY ALARM WHEN TRYING TO UPLOAD AND NO NETWORK!!!
          queueMessages(texts, context);
        }else {
          for(Text m : texts){
            if(m != null){
              autoProcess(m, context);
            }
          }
        }

      }
    }

  }
  
  private void queueMessages(Collection<Text> messages, Context context){
    SmsList.getPendingList().addTexts(messages);
    new Settings(context).savePendingTextsList();
    Notifications.updateNotification(context);
  }

  public void autoProcess(Text text, Context context){

    String body = text.getBody();
    String number = text.getNumber();
    String time = Parser.timeStampToString(text.getTimestamp());

    String question = Parser.concatenateArrayList(Parser.getQuestion(body), "");
    String location = Parser.concatenateArrayList(Parser.getLocation(body), "");
    String toastie = Parser.concatenateArrayList(Parser.getFlavours(body), ", ");
    
    Uri uri = Parser.createUploadUri(number, question, location, toastie, body, time, context);
    NetCaller.callScript(uri, context);

    SmsList.getUploadedList().addText(text);
    new Settings(context).saveUploadedTextsList();
  }

}




Java Source Code List

org.apache.pig.impl.util.ObjectSerializer.java
uk.org.sucu.tatupload.BrowserAccessor.java
uk.org.sucu.tatupload.MessageArrayAdapter.java
uk.org.sucu.tatupload.NetCaller.java
uk.org.sucu.tatupload.Notifications.java
uk.org.sucu.tatupload.ParameterArrayAdapter.java
uk.org.sucu.tatupload.ResolveInfoArrayAdapter.java
uk.org.sucu.tatupload.Settings.java
uk.org.sucu.tatupload.TabContent.java
uk.org.sucu.tatupload.TabManager.java
uk.org.sucu.tatupload.TatUploadApplication.java
uk.org.sucu.tatupload.activity.MainActivity.java
uk.org.sucu.tatupload.activity.OptionActivity.java
uk.org.sucu.tatupload.activity.ParameterViewActivity.java
uk.org.sucu.tatupload.activity.SmsReviewActivity.java
uk.org.sucu.tatupload.activity.TutorialActivity.java
uk.org.sucu.tatupload.message.SmsList.java
uk.org.sucu.tatupload.message.SmsReceiver.java
uk.org.sucu.tatupload.message.Text.java
uk.org.sucu.tatupload.parse.Parameters.java
uk.org.sucu.tatupload.parse.Parser.java
uk.org.sucu.tatupload.parse.Property.java