Tag Archives: Java

Get entire message body from an Intent

I recently started programming the Android and noticed that most examples for processing an incoming SMS are not entirely correct.

An SMS message is limited to 160 characters. Current mobile phones break up a larger message in multiple messages transparently for the user. When Android notifies you about an incoming SMS it has all parts (of that large message) available. So here is how you reconstruct the entire message body from an Intent

Bundle bundle = intent.getExtras();        
if (bundle == null) return;

StringBuilder message = new StringBuilder();
Object[] pdus = (Object[]) bundle.get("pdus");

// Rebuild this entire message from the multi part smses/pdus
for (Object pdu : pdus){
 // Notice that i use the deprecated android.telephony.gsm.SmsMessage
 // android.telephony.SmsMessage throws when i call createFromPdu 
 SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);                
 message.append(msg.getMessageBody().toString());
}

Presenting TimeOTPClient

Because most people have their java enabled mobile phone with them, and I did not want to buy an expensive token generator I decided to write a MIDlet that can generate passwords as specified in the TOTP algorithm. This project is build with Sun Java Wireless Toolkit for CLDC. Feel free to download the sources, TimeOTPClient.zip.

screenshot of TimeOTPClient input area
screenshot of TimeOTPClient generated password area

BBCode for Graphics2D

For my graduation project we needed the ability to print a couple of bills etc. Printing lines was pretty simple with the LinesPrinter i blogged about a while ago. We had two choices: either implement a specific print method for each module or implement a reusable markup system. Obviously we went for the second option and came up with something alike BBCode. The codes we implemented are:

  • [b]..[/b] for bold
  • [i]..[/i] for italic
  • [color=x]..[/color] for color x
  • ..

    for centered text

  • [r]..[/r] for right aligned text
  • [ll=x]..[/ll] for left aligned text starting from the x-th column at the left
  • [lr=x]..[/lr] for left aligned text starting from the x-th column at the right
  • [rl=x]..[/ll] for right aligned text starting from the x-th column at the left
  • [rr=x]..[/ll] for right aligned text starting from the x-th column at the right

Since the code is written to work on a Graphics2D device you can also render the formatted text on a jpanel etc instead of a printer device. Here is a screenshot of a generated bill (on a jpanel):

rpcode

Feel free to download, try and improve the rpcode.zip package.

Printing an array of strings

Yesterday i’ve been experimenting with Printing on the Java Platform. I needed to generate a printout of ordered menuitems on the default printer. It took a while before i found out there is translation needed between the coordinates of the Graphics device and the PageFormat. Here is my LinesPrinter. Here is an example of how you can use the class:

ArrayList<string> lines = new ArrayList<string>();

StringBuffer buf = new StringBuffer("De RegaPan\t");
buf.append(DateFormat.getDateTimeInstance().format(new Date()));
lines.add(buf.toString());
lines.add("");

DecimalFormat df = new DecimalFormat("##.00");

Enumeration e = billModel.elements();
while (e.hasMoreElements()) {
  Order o = (Order) e.nextElement();
  MenuItem mi = o.getMenuItem();

  buf = new StringBuffer(mi.getName());
  buf.append("\t");
  buf.append(df.format(mi.getPriceIncVat()));

  lines.add(buf.toString());
}

lines.add("\t----------");
lines.add("\t" + df.format(getTotal()));

LinesPrinter.print((String[]) lines.toArray(new String[0]));

JSpace

I wrote a simple shoot-em-up game: JSpace.zip.

Telnet server

A telnet server that writes the content of a file to it’s clients: banner.txt.

Formatted input

I was in need for formatted input and the decomposition of the input into a stream of tokens so i came up with the following:

And now i am ready for formatted input like this:

public class Main {
  public static void main(String[] args) {
    try {
      BufferedReader input = new BufferedReader(new FileReader("file.txt"));
      for (String in = input.readLine()) {
        TokSequence ts = new TokSequence(new StringTokenizer(in));
        int userId = ts.getIn();
        double score = ts.getDouble();
        String name = ts.getString();
        // do stuff with userId, score and name
      }
    } catch (Exception e) {
      System.err.println(e);
      System.exit(1);
    }
  }
}

TetriNet spectator client

I present a Spectator (Applet) for TetriNet: JSpectator.zip.

Sokoban

Today i finished my own version of the wellknown game Sokoban. Get sokoban.zip now!

TetriNet server

I hacked a TetriNet server together (with some really ugly code in it). Anyway, you can get it at: tserver.zip.