×
Namespaces

Variants
Actions
(Difference between revisions)

Java ME Porting using preprocessor directives

From Nokia Developer Wiki
Jump to: navigation, search
jaaura (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Add ArticleMetadata)
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20071127
 +
|author= [[User:Arunjyothiskp]]
 +
}}
 
In-house Porting Solutions
 
In-house Porting Solutions
 
So, you've decided you do need to automate porting your application. Sure, you could outsource it, but if you're thinking about doing it in-house, you've basically got four methods from which to choose.  
 
So, you've decided you do need to automate porting your application. Sure, you could outsource it, but if you're thinking about doing it in-house, you've basically got four methods from which to choose.  
Line 8: Line 30:
  
  
try {
+
<code java>try
 +
{
 
   Class.forName("com.nokia.mid.ui.FullCanvas");
 
   Class.forName("com.nokia.mid.ui.FullCanvas");
 
   Class myClass = Class.forName("NokiaCanvas");
 
   Class myClass = Class.forName("NokiaCanvas");
 
   myCanvas = (ICanvas)(myClass.newInstance());
 
   myCanvas = (ICanvas)(myClass.newInstance());
} catch (Exception exception1) {
+
}  
  try {
+
catch (Exception exception1)
      Class.forName("com.siemens.mp.color_game.GameCanvas");
+
{  
      Class myClass = Class.forName("SiemensCanvas");
+
try
      myCanvas = (ICanvas)(myClass.newInstance());
+
{
  } catch (Exception exception2) {  
+
Class.forName("com.siemens.mp.color_game.GameCanvas");
      myCanvas = (ICanvas) new StandardCanvas();
+
Class myClass = Class.forName("SiemensCanvas");
  }
+
myCanvas = (ICanvas)(myClass.newInstance());
}
+
}
 +
catch (Exception exception2)
 +
{  
 +
myCanvas = (ICanvas) new StandardCanvas();
 +
}
 +
}</code>
  
 
You basically create an interface, Icanvas, and three implementations, one for Nokia devices, one for Siemens devices, and another one for standard MIDP devices.  
 
You basically create an interface, Icanvas, and three implementations, one for Nokia devices, one for Siemens devices, and another one for standard MIDP devices.  
Line 30: Line 58:
  
  
//#ifdef NOKIA
+
<code java>//#ifdef NOKIA
 
   extends com.nokia.mid.ui.FullCanvas
 
   extends com.nokia.mid.ui.FullCanvas
 
//#else
 
//#else
Line 39: Line 67:
 
//#ifndef MIDP2
 
//#ifndef MIDP2
 
       setFullScreenMode(true);
 
       setFullScreenMode(true);
//#endif
+
//#endif</code>
  
 
A preprocessor processes this source code, then you set the directives. So, to generate the application for a Nokia device:  
 
A preprocessor processes this source code, then you set the directives. So, to generate the application for a Nokia device:  
  
//#define NOKIA
+
<code java>//#define NOKIA</code>
  
 
The preprocessor produces:  
 
The preprocessor produces:  
  
  extends com.nokia.mid.ui.FullCanvas
+
<code java>  extends com.nokia.mid.ui.FullCanvas
   {
+
   {</code>
  
 
For a MIDP 2 device, (“//#define MIDP 2”), it produces:  
 
For a MIDP 2 device, (“//#define MIDP 2”), it produces:  
  
  extends Canvas
+
<code java>  extends Canvas
 
   {
 
   {
       setFullScreenMode(true);
+
       setFullScreenMode(true);</code>
  
 
This solution allows for one body of source code to be adapted to each device model. You need only develop to the reference source code, including the directives. All other modifications made to the processed files will be lost after the next preprocessing.  
 
This solution allows for one body of source code to be adapted to each device model. You need only develop to the reference source code, including the directives. All other modifications made to the processed files will be lost after the next preprocessing.  
Though this solution relies on the old concept of preprocessing, this is the only technique open-ended enough to solve all the problems you'll encounter trying to port to multiple device models. [[Category:Java]][[Category:Porting]]
+
Though this solution relies on the old concept of preprocessing, this is the only technique open-ended enough to solve all the problems you'll encounter trying to port to multiple device models.
 +
 
 +
[[Category:Java ME]][[Category:Porting]]

Revision as of 09:53, 18 July 2012

Article Metadata
Article
Created: arunjyothiskp (27 Nov 2007)
Last edited: hamishwillee (18 Jul 2012)

In-house Porting Solutions So, you've decided you do need to automate porting your application. Sure, you could outsource it, but if you're thinking about doing it in-house, you've basically got four methods from which to choose. Build one Version per Device Series: This approach is to simply develop an application for a specific series of models, for example, the Nokia Serie40 Edition 1. The problem here is that the more APIs you support, or the more your application stresses the device, the more you fragment the series since support for advanced APIs highlights the small differences within the series. For example, two similar devices will have wildly varying performance results due to the number of images on the screen.


Dynamic Detection of the Handset: This option involves testing your application during execution. For example, suppose your model is a Nokia handset. Your application would detect the device model during execution and select the appropriate behaviour depending on the model. This allows you to call methods only available on specific handsets—like fullscreen mode. You have to create one class for each specific implementation (NokiaCanvas, SiemensCanvas, and StandardCanvas). The following code demonstrates:


try
{
Class.forName("com.nokia.mid.ui.FullCanvas");
Class myClass = Class.forName("NokiaCanvas");
myCanvas = (ICanvas)(myClass.newInstance());
}
catch (Exception exception1)
{
try
{
Class.forName("com.siemens.mp.color_game.GameCanvas");
Class myClass = Class.forName("SiemensCanvas");
myCanvas = (ICanvas)(myClass.newInstance());
}
catch (Exception exception2)
{
myCanvas = (ICanvas) new StandardCanvas();
}
}

You basically create an interface, Icanvas, and three implementations, one for Nokia devices, one for Siemens devices, and another one for standard MIDP devices. Then you use Class.forName in order to determine whether a proprietary API is available. If no exception is thrown, you use the NokiaCanvas. Otherwise, it means the current device doesn't support this API. In this case, you test another API (for example, Siemens). If another exception is thrown, it means you have to use the standard canvas.


Using a Preprocessor: Using a preprocessor, your source code will be automatically activated or deactivated depending on certain conditions. For example, to set the full screen mode on a Nokia device, you have to extend FullCanvas, not Canvas. On a MIDP 2 device, you have to call setFullScreenMode. On a MIDP 1 device, this isn't possible, so you stay in a non-fullscreen mode.


//#ifdef NOKIA
extends com.nokia.mid.ui.FullCanvas
//#else
extends Canvas
//#endif
{
...
//#ifndef MIDP2
setFullScreenMode(true);
//#endif

A preprocessor processes this source code, then you set the directives. So, to generate the application for a Nokia device:

//#define NOKIA

The preprocessor produces:

   extends com.nokia.mid.ui.FullCanvas
{

For a MIDP 2 device, (“//#define MIDP 2”), it produces:

   extends Canvas
{
setFullScreenMode(true);

This solution allows for one body of source code to be adapted to each device model. You need only develop to the reference source code, including the directives. All other modifications made to the processed files will be lost after the next preprocessing. Though this solution relies on the old concept of preprocessing, this is the only technique open-ended enough to solve all the problems you'll encounter trying to port to multiple device models.

86 page views in the last 30 days.
×