The SysPropForm
class is responsible for displaying
the details of the individual system properties. The SystemProperties
class calls the constructor of the SysPropForm
class
when a command is issued.
The SysPropFullCanvas
class displays the property
details in full screen mode. It renders a full screen canvas and draws
the details on it.
Note: To ensure backwards compatibility with earlier Series 40 devices, the Orientation API code is commented out in the MIDlet download package. To enable Orientation API support, uncomment the Orientation API code (as has been done in the following code snippets) and rebuild the MIDlet. The Orientation API is supported from Java Runtime 2.0.0 for Series 40 onwards.
To implement
the SysPropForm
class:
Create the SysPropForm.java
class file.
Import the required classes.
import javax.bluetooth.BluetoothStateException; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.bluetooth.LocalDevice; import javax.microedition.m3g.Graphics3D; import java.util.Hashtable; import com.nokia.mid.ui.orientation.*;
Set SysPropForm
to extend Form
and implement CommandListener
and OrientationListener
. MIDlets use the CommandListener
interface to receive high-level UI events
from the platform, and the OrientationListener
interface
to implement a listener for receiving a notification whenever the
display orientation changes.
public class SysPropForm extends Form implements CommandListener, OrientationListener {
Create the required
variables and the SysPropForm
class constructor.
private Command backCommand; private Command exitCommand; private SystemProperties midlet; private String[] items; public SysPropForm(String title, SystemProperties midlet, String[] list) { super(title); items = list; this.midlet = midlet; backCommand = new Command("Back", Command.BACK, 1); exitCommand = new Command("Exit", Command.EXIT, 1); addCommand(backCommand); addCommand(exitCommand); setCommandListener(this); Orientation.addOrientationListener(this); }
Use the update
method to handle the different details that are displayed.
Most details are displayed the same way, but the Bluetooth and M3G
1.1 properties are special cases that need to be handled differently.
protected void update(String title, String[] list) { this.setTitle(title); items = list; this.deleteAll(); //Clear the screen //Special case for Bluetooth details if (midlet.bt) { try { append("BT address: " + LocalDevice.getLocalDevice().getBluetoothAddress() + "\n"); append("Friendly name: " + LocalDevice.getLocalDevice().getFriendlyName() + "\n"); } catch (BluetoothStateException ex) { append("BluetoothStateException: " + ex.getMessage()); } } //Default way of displaying details for (int i = 0; i < items.length; i++) { append(items[i] + ":\n"); if (midlet.bt) { append(LocalDevice.getProperty(items[i]) + "\n"); } else if (midlet.m3g) { // Properties "maxViewportWidth" and "maxViewportHeight" were // added in M3G 1.1, in case of M3G 1.0 this MIDlet returns // "null" for these properties. // Property "m3gRelease" is not mandated by the specification, but // it is a implementation specific property. Hashtable g3dtable = new Hashtable(); g3dtable = Graphics3D.getProperties(); Object property = g3dtable.get(list[i]); if (property != null) { append(property.toString() + "\n"); } else { append("null\n"); } } else if (midlet.index == 14) { try { append(System.getProperty(items[i]) + "\n"); } catch (SecurityException se) { // In case of untrusted or trusted 3rd party MIDlet // SecurityException should be thrown for all the // PROTECTED_SYSPROPS properties. append("SecurityException: " + se.getMessage()); } } else { append(System.getProperty(items[i]) + "\n"); } } }
Implement the commandAction
method for handling commands.
public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.showList(); midlet.bt = false; midlet.m3g = false; } if (c == exitCommand) { midlet.notifyDestroyed(); } }
Implement the OrientationListener
by defining the displayOrientationChanged
method. The method is called every time the display orientation
changes. Use the displayOrientationChanged
method
to set the UI orientation by calling the static Orientation.setAppOrientation
method.
/** * This is method for changing orientation based on the device orientation. * @param newOrientation */ public void displayOrientationChanged(int newOrientation) { Orientation.setAppOrientation(newOrientation); switch( newOrientation ){ case Orientation.ORIENTATION_PORTRAIT: // 1 break; case Orientation.ORIENTATION_LANDSCAPE: // 2 break; case Orientation.ORIENTATION_LANDSCAPE_180: // Not supported currently break; case Orientation.ORIENTATION_PORTRAIT_180: // Not supported currently break; } } }
Note: To adjust the MIDlet UI orientation using the
Orientation API, declare the Nokia-MIDlet-App-Orientation
JAD attribute with the value manual
in the MIDlet
JAD file:
Nokia-MIDlet-App-Orientation: manual
To implement the SysPropFullCanvas
class:
Create the SysPropFullCanvas.java
class file.
Import the required classes.
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Font;
Set SysPropFullCanvas
to extend Canvas
and implement CommandListener
. MIDlets use the CommandListener
interface to receive
high-level UI events from the platform.
public class SysPropFullCanvas extends Canvas implements CommandListener {
Create the required
variables and the SysPropFullCanvas
class constructor.
private SystemProperties midlet; private Command backCommand; private Command exitCommand; private int w, h, fontheight; private Font font; private String value; public SysPropFullCanvas(SystemProperties midlet) { this.midlet = midlet; this.setFullScreenMode(true); backCommand = new Command("Back", Command.BACK, 1); exitCommand = new Command("Exit", Command.EXIT, 1); this.setCommandListener(this); this.addCommand(backCommand); this.addCommand(exitCommand); font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); fontheight = font.getHeight(); }
Use the paint
method to draw the background and call the update
method.
protected void paint(Graphics g) { w = this.getWidth(); h = this.getHeight(); g.setColor(255, 255, 255); // Background color g.fillRect(0, 0, w, h); g.setColor(0, 0, 0); // Text color g.setFont(font); update(SystemProperties.NOKIA_CANVAS_UI, g); }
Implement the commandAction
method for handling commands.
public void commandAction(Command c, Displayable d) { if (c == backCommand) { midlet.showList(); } if (c == exitCommand) { midlet.notifyDestroyed(); } }
Use the update
method to draw the actual property details.
protected void update(String[] list, Graphics g) { int length = list.length; for (int i = 0; i < length; i++) { value = System.getProperty(list[i]); if (value == null) { value = "null"; } g.drawString(list[i], 0, (i * 2) * fontheight, Graphics.LEFT | Graphics.TOP); g.drawString(value, 0, (i * 2) * fontheight + fontheight, Graphics.LEFT | Graphics.TOP); } } }