Android Open Source - MorseCode Message Receiver






From Project

Back to project page MorseCode.

License

The source code is released under:

Custom License You are free to download and edit the source code for personal use but not for commercial app purposes. We are going to use this code to create an app in the future.

If you think the Android project MorseCode 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 com.csc.morsecode.handlers;
//  w w  w .java 2s .  co m
import java.text.StringCharacterIterator;
import java.util.Iterator;

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

import com.csc.morsecode.Settings;
import com.csc.morsecode.data.Code;
import com.csc.morsecode.data.CodeMapping;
import com.csc.morsecode.data.Encoding;


public class MessageReceiver extends BroadcastReceiver implements Input {
  
  private static final String TAG = "MessageActivity";
  
  public MessageReceiver() {
    Settings.addInput(this);  //TODO is a new object created each time we receive a message or is it only registered once?
    Log.i(TAG, "message receiver constructor");
  }
  
  @Override
  public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
      //---retrieve the SMS message received---
      Object[] pdus = (Object[]) bundle.get("pdus");
      msgs = new SmsMessage[pdus.length];            
      for (int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
        //str += "sms from " + msgs[i].getOriginatingAddress();
        //str = str.trim();
        //str += " ";
        str += msgs[i].getMessageBody().toString();        
      }
      input(str);
    }
  }
  
  //--------------------------------------------------------------------------
  // Methods from Input
  //--------------------------------------------------------------------------
  
  public void input(String message) {
    if(message == null) {
      return;
    }
    
    Log.i(TAG, "Messag received:" + message);
    
    CodeMapping codeMapping = Settings.getCodeMapping();
    Iterator<Encoding> iter = codeMapping.sToEIterator(new StringCharacterIterator(message));
    
    //output each sequence
    Encoding encoding;
    while(iter.hasNext()) {
      encoding = iter.next();
      
      //each sequence goes through all enabled output methods
      for(Output output: Settings.getOutputs()) {
        output.output(encoding);
      }
    }
  }
  
  public void input(Code[] encoding) throws UnsupportedOperationException {
    throw new UnsupportedOperationException("Can't receive morse code over SMS");
  }
  
}




Java Source Code List

com.csc.morsecode.MainActivity.java
com.csc.morsecode.PopMessage.java
com.csc.morsecode.Settings.java
com.csc.morsecode.data.CodeMapping.java
com.csc.morsecode.data.Code.java
com.csc.morsecode.data.Encoding.java
com.csc.morsecode.handlers.ConsoleOutput.java
com.csc.morsecode.handlers.Input.java
com.csc.morsecode.handlers.MessageReceiver.java
com.csc.morsecode.handlers.Output.java
com.csc.morsecode.handlers.TapInput.java
com.csc.morsecode.handlers.VibratorOutput.java