dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Automate Your J2ME Application Porting with Preprocessing : Page 4

Got porting nightmares? If you're considering automating the porting your J2ME applications, you may want to think about using a preprocessor. Find out why it's the only technique open-ended enough to handle porting to multiple device models.


advertisement
Creating the SoundManager Class
The SoundManager class contains the instructions to play a basic sound and to set the backlight. It supports the following generic devices:
  • MIDP 1: This device doesn't support sounds and backlight, so the methods will be empty.
  • MIDP 2: This device supports sounds and backlight, so the lines of code between //#ifdef MIDP2 and //#endif will be selected.
  • MIDP 1 NokiaUI: This device supports sounds and backlight, so the lines of the code between //#ifdef NOKIAUI and //#endif will be selected.
  • MIDP 1 Motorola: This device supports only backlight, so the method to play a sound will be empty.
Listing 1 shows the code.

Creating the BUILD.XML File
In the BUILD.XML file, you will select the appropriate directories. It's a manual operation, so, it's best to create the XML files for each device and keep them.

In the case of the MIDP 2 profile, the preprocessing of the file SoundManager.java will produce this output:


import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.media.control.ToneControl;
import javax.microedition.lcdui.*;
import java.io.*;

public class SoundManager {

  Display display;
  
  public SoundManager(Display display) {
    this.display = display;
  }

  public void doLight() {
    display.flashBacklight (duration);
  }

  public void doSound() {
    try {
      InputStream is = getClass().getResourceAsStream("music.mid");
      Player audioPlayer = Manager.createPlayer(is, "audio/midi");
      audioPlayer.start();
    } catch (IOException ioe) {
    }
  }
}
For the MIDP 1 profile, it produces:

import javax.microedition.lcdui.*;
import java.io.*;

public class SoundManager {

  Display display;
  
  public SoundManager(Display display) {
    this.display = display;
  }

  public void doLight() {
  }

  public void doSound() {
  }
}
You select the profile of the device in this line:

<wtkpreprocess srcdir="src" destdir="output\src" symbols="MIDP2" verbose="false"/>
Antenna takes the content of the attribute symbols (MIDP 2 in this example) and inserts //#define MIDP2 at the beginning of each file, like this:

//#define MIDP2
//#ifdef MIDP2
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.media.control.ToneControl;
//#endif
//#ifdef NOKIAUI
import com.nokia.mid.sound.*;
import com.nokia.mid.ui.*;
//#endif
//#ifdef MOTOROLA
import com.motorola.multimedia.*;
//#endif
import javax.microedition.lcdui.*;
import java.io.*;



Thanks for your registration, follow us on our social networks to keep up-to-date