Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Developing MIDlets Dr. Miguel A. Labrador Department of Computer Science & Engineering

Similar presentations


Presentation on theme: "11 Developing MIDlets Dr. Miguel A. Labrador Department of Computer Science & Engineering"— Presentation transcript:

1 11 Developing MIDlets Dr. Miguel A. Labrador Department of Computer Science & Engineering

2 2 Copyright© Dr. Miguel A. Labrador 2 2 Outline MIDlet life cycle Hello World example User interface classes and APIs Lists, text boxes, forms, alerts Media API Record Management Systems Security

3 3 Copyright© Dr. Miguel A. Labrador 3 3 MIDlets Java program compiled using the APIs included in the CLDC and MIDP specifications After compilation, several steps need to be done before using the MIDlet in a real device –Debugged in emulators –Passed through the offline preverifier –Packaged Creates the Java Archive file (JAR) and Java Application Descriptor (JAD) file JAR file contains the manifest file –Automatically generated by the jar tool –Information about the MIDlet, such as name, vendor, version and configuration and profile utilized JAD file contains additional information, such as URL and size –Very useful for the mobile decide to decide whether to download the MIDlet or not

4 4 Copyright© Dr. Miguel A. Labrador 4 4 Manifest and JAD Examples Manifest File Example JAD File Example Manifest-Version: 1.0 Ant-Version: Apache Ant Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.) MIDlet-2: CalculatorWebService,, edu.cse.usf.book.ws.CalculatorWebS ervice MIDlet-1: TCPTest,, edu.cse.usf.book.TCPTest MIDlet-Vendor: Vendor MIDlet-Name: TCPTest MIDlet-Version: 1.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0 MIDlet-1: TCPTest,, edu.cse.usf.book.TCPTest MIDlet-2: CalculatorWebService,, edu.cse.usf.book.ws.CalculatorWe bService MIDlet-Jar-Size: MIDlet-Jar-URL: TCPTest.jar MIDlet-Name: TCPTest MIDlet-Vendor: Vendor MIDlet-Version: 1.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0

5 5 Copyright© Dr. Miguel A. Labrador 5 5 MIDlets All MIDlets have the same life cycle Paused Active Destroyed destroyApp() pauseApp() startApp() New Application Instance End

6 6 Copyright© Dr. Miguel A. Labrador 6 6 A Hello World MIDlet Example import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloWorld extends MIDlet implements CommandListener{ private Command exitCommand; private TextBox tbox; // MIDlet constructor public HelloWorld() { // Create "Exit" Command exitCommand = new Command("Exit", Command.Exit, 1); // Create TextBox to display the output tbox = new TextBox ("Hello World MIDlet", "Hello, World!", 15, 0); // Include the Exit Command in the interface and set its Listener tbox.addCommand(exitCommand); tbox.setCommandListener(this); // Set the TextBox as the current screen Display.getDisplay(this).setCurrent(tbox); }

7 7 Copyright© Dr. Miguel A. Labrador 7 7 A Hello World MIDlet Example // The system calls this function to start the MIDlet protected void startApp() {} // The application is switched to the paused state protected void pauseApp() {} // The application is destroyed protected void destroyApp() {boolean force} // MIDlet destroys itself if user gives the Exit Command public void commandAction (Command c, Displayable d) { if (c==exitCommand) { destroyApp(false); notifyDestroyed{}; }

8 8 Copyright© Dr. Miguel A. Labrador 8 8 Hello World MIDlet

9 9 Copyright© Dr. Miguel A. Labrador 9 9 The User Interface Hierarchy of Classes The package javax.microedition.lcdui contains most of the classes and methods needed to design GUIs Display Displayable Command Canvas Screen List StringItem TextBox Alert Form Item CustomItem Spacer ChoiceGroup TextField ImageItem DateField Gauge n

10 10 Copyright© Dr. Miguel A. Labrador 10 Copyright© Dr. Miguel A. Labrador 10 The User Interface API Hierarchy of classes –Display class at the top Manages the display and input devices of the system –Contains methods to retrieve the properties of the device and to request the display of object Only one instance of Display per MIDlet Reference of that instance can be obtained by getDisplay() method –Displayable object contains the UI objects that are shown in the display setCurrent() and getCurrent() utilized to set and retrieve the current Displayable The application changes the current Displayable based on user action Displayable object may have listener and command objects associated –User uses these objects to interact with the UI –When the user selects a command, the application is automatically notified –The application may react changing the Displayable by another one

11 11 Copyright© Dr. Miguel A. Labrador 11 Copyright© Dr. Miguel A. Labrador 11 The User Interface API –Commands provide users with a way to navigate through the Displayables of an application If a Displayable has no command associated with it, the user has no way to change the current Displayable All commands have a string label, priority, and command type Commands are added or removed using the addCommand() and removeCommand() methods Six command types: –BACK, OK, CANCEL, HELP, EXIT, STOP Following example shows the implementation of three commands, two generic commands, save and delete, and one specific command, the exit command

12 12 Copyright© Dr. Miguel A. Labrador 12 Copyright© Dr. Miguel A. Labrador 12 Commands Example class ExampleCommand extends Screen implements CommandListener { Command save = new Command ("Save", Command.SCREEN, 2}; Command delete = new Command ("Delete", Command.SCREEN, 3}; Command exit = new Command ("Exit", Command.EXIT, 4}; MIDlet midlet; public ExampleCommand (MIDlet mymidlet) { midlet = mymidlet; setCommandListener(this); addCommand(save); addCommand(delete); addCommand(exit); } public void commandAction (Command c, Displayable d) { if (c == save) { \\ Save data } else if (c == delete) { \\ Delete data } else if (c == exit) { \\ Exit the application midlet.notifyDestroyed(); }

13 13 Copyright© Dr. Miguel A. Labrador 13 Copyright© Dr. Miguel A. Labrador 13 Lists, Text Boxes, Forms, and Alerts Displayable class has two subclasses: Canvas and Screen Canvas contains objects that allow the developer to have precise control of what is drawn on the display Screen contains high-level objects that implement complete user interface components such as lists, text boxes, and forms –List class is a Screen that displays a list of choice elements Each element includes a string and may have an icon Lists can be implicit, exclusive, and multiple choice –Append, delete, insert, set, getString, and getImage methods –The type of list is selected using the interface class Choice –TextBox class is a Screen that allows the user to input and edit text Application can set input constraints –ANY, NUMERIC, DECIMAL, PHONENUMBER, URL, ADDR A text box must have commands

14 14 Copyright© Dr. Miguel A. Labrador 14 Copyright© Dr. Miguel A. Labrador 14 Lists, Text Boxes, Forms, and Alerts –Form is a Screen that may contain StringItems, ImageItems, DateFields, TextFields, Gauges, and ChoiceGroups Any of the subclasses of class Item Form can be manipulated using the insert, append, delete, set, get, size, and deteleAll methods –Alerts are Screens that can inform the user about errors and other exceptions, or as short informational notes and reminders Alerts are displayed for certain amount of time using the setTimeout method, or modal, which requires the user input to close it –ALARM, CONFIRMATION, ERROR, INFO, and WARNING types

15 15 Copyright© Dr. Miguel A. Labrador 15 Copyright© Dr. Miguel A. Labrador 15 List Example List list = new List (String title, int listType, String[ ] stringElements, Image[ ] imageElements); where listType can be IMPLICIT, EXCLUSIVE, or MULTIPLE; stringElements (imageElements) is the initial array of elements(images) e.g., List list = new List (`` list'', Choice.IMPLICIT, null);

16 16 Copyright© Dr. Miguel A. Labrador 16 Copyright© Dr. Miguel A. Labrador 16 Alert Example Alert alert = new Alert (String title, String alertText, Image alertImage, AlertType.XXX); where XXX can be any of the alertType e.g., Alert alert = new Alert (``Warning'', ``Delete all?'', null, AlertType.WARNING);

17 17 Copyright© Dr. Miguel A. Labrador 17 Copyright© Dr. Miguel A. Labrador 17 The Media API Designed to support sound in resource-constrained devices –Subset of the Mobile Media API, an optional package meant for Java ME devices with advanced sound and multimedia capabilities Supports tone generation and media flow control (audio playback) –Implemented in two packages The javax.microedition.media package, which is a fully compatible subset of classes included in the Mobile Media API The javax.microedition.media.control package that defines specific control types that can be used with a Player Three main components –Manager: used by the application to request Players –Player: plays the media Wav, MP3, MIDI files and tone sequences –Controls: implement the controls of the Player Start, stop, close

18 18 Copyright© Dr. Miguel A. Labrador 18 Copyright© Dr. Miguel A. Labrador 18 Creating a Player The createPlayer method of the Manager class can create a player in two different ways Player Manager.createPlayer (String url) where url specifies the protocol and content of the data as follows: : e.g., Player p = Manager.createPlayer(``http://hello.wav'');

19 19 Copyright© Dr. Miguel A. Labrador 19 Copyright© Dr. Miguel A. Labrador 19 Creating a Player A player can also be created to playback media from an inputStream Start(), stop(), close() methods Player Manager.createPlayer (InputStream stream, String type); e.g., InputStream istream = getClass().getResourceAsStream(``hello.wav''); Player p = Manager.createPlayer(istream, ``audio/X-wav''); p.start(); Where type can be: Wave audio files (audio/x-wav), AU audio files (audio/basic), MP3 audio files(audio/mpeg), MIDI files (audio/midi), Tone sequences (audio/x-tone-seq)

20 20 Copyright© Dr. Miguel A. Labrador 20 Copyright© Dr. Miguel A. Labrador 20 Generating Tones The Manager class can also be used to generate tones Volume is a value from 0 to 100 Duration is the duration of the tone in milliseconds Note defines the tone of the note –Number from 0 to 127 –note = (12 x log 2 (f/440)) + 69 –Frequency 440 Hz corresponds to note 69, which is MIDI note A Manager.playTone (int note, int duration, int volume)

21 21 Copyright© Dr. Miguel A. Labrador 21 Copyright© Dr. Miguel A. Labrador 21 The Record Management System (RMS) Simple record-oriented database that allows MIDlets to persistently store data in the mobile device –Included in the javax.microedition.rms package Uses the concept of record store, a collection of persistent records Records are arrays of bytes of different lengths and types within a record store Records are automatically identified by a recordID –Monotonically increasing-by-one mechanism with no wrap around –Adjacent records in a record store do not necessarily have subsequent record IDs Record stores are uniquely named using the name of the MIDlet suite plus the name of the record store –MIDlet suites are identified using the attributes MIDlet-vendor and MIDlet-name of the application descriptor

22 22 Copyright© Dr. Miguel A. Labrador 22 Copyright© Dr. Miguel A. Labrador 22 The Record Management System (RMS) The RMS API does not include locking mechanisms but ensures that record store operations are atomic, synchronous, and serialized –It is the programmer’s responsibility to coordinate access when multiple threads within the same MIDlet attempt to access the same record simultaneously No corruption of data guarantee but the serialization mechanism might give MIDlets access to the record in an undesired sequence Methods to manipulate record stores –listRecordStores, deleteRecordStore, openRecordStore, closeRecordStore, getNumRecords, getSize, getSizeAvailable, getNextRecordID, getVersion, getLastModified Methods to manipulate records –addRecord, deleteRecord, getRecordSize, getRecord, setRecord

23 23 Copyright© Dr. Miguel A. Labrador 23 Copyright© Dr. Miguel A. Labrador 23 Opening a New Record Store public void openRecStore(String recordStore_name) { try{ RecordStore rs = RecordStore.openRecordStore(recordStore_name,true); } catch(Exception e) { System.err.println(e.toString()); }

24 24 Copyright© Dr. Miguel A. Labrador 24 Copyright© Dr. Miguel A. Labrador 24 Adding a New Record to a Record Store public void writeRecord(String str) { byte[] rec = str.getBytes(); try { rs.addRecord(rec, 0, rec.length); } catch (Exception e) { System.err.println(e.toString()); }

25 25 Copyright© Dr. Miguel A. Labrador 25 Copyright© Dr. Miguel A. Labrador 25 Security Security is guided by the following goals –Confidentiality Disclosure of information only to authorized users or systems –Encryption is a common mechanism to provide confidentiality »Symmetric and Asymmetric encryption –Integrity Data cannot be modified without proper authorization –Achieved by cryptographic methods plus additional information –Authenticity Making sure the message is authentic; it comes from the real source –Digital signatures using asymmetric encryption –Availability Making sure information is available when needed

26 26 Copyright© Dr. Miguel A. Labrador 26 Copyright© Dr. Miguel A. Labrador 26 MIDlet Security MIDP 1.0 specification used the sandbox model –Run in a controlled and separate environment and do not interfere with each other MIDP 2.0 expands this model including the concepts of trusted MIDlet and protection domains Untrusted MIDlet suite is one whose authenticity and integrity of JAR file cannot be trusted by the device –Executed in untrusted and restrictive domain MIDP 2.0 includes the mechanisms to identify and trust a MIDlet suite and the concept of protection domain for trusted MIDlets Protection domain is a set of permissions associated with a root certificate in the device –A specific domain can be defined in the device using the public key of the domain entity, e.g., a software development company Then, a MIDlet signed by the company will be given access to all those resources included in the permissions of the domain

27 27 Copyright© Dr. Miguel A. Labrador 27 Copyright© Dr. Miguel A. Labrador 27 MIDlet Security Digital signatures and authentication methods are utilized to decide whether to trust or not a MIDlet suite –Internet X.509 Public Key Infrastructure standard included in RFC 2459 and RFC 2437 “PKCS #1: RSA Cryptography Specifications, version 2.0” –First, the MIDlet is signed; then, at download time, it is authenticated Entire process consists of the following steps –Sender creates a signing certificate sending its Distinguished Name and public key to a Certificate Authority (CA) to obtain a RSA X.509 certificate –Sender encodes and inserts certificate(s) in the JAD file –Sender signs the JAR file with its private key to provide MIDlet integrity; however, it does not include the JAD file –Receiver downloads the MIDlet, checks the JAD file and verifies signer certificates and JAR signature

28 28 Copyright© Dr. Miguel A. Labrador 28 Copyright© Dr. Miguel A. Labrador 28 MIDlet Security –Receiver verifies signer certificates by looking at the CA in the JAD file and the root certificate authorities stored in the device If authentication fails, MIDlet is not installed –Receiver verifies JAR signature Gets signer’s public key from the CA and uses this key, the signature included in the JAD file, and the digest included in the JAR file to verify it Certificates are used in MIDP 2.0 security for authentication –Package javax.microedition.pki provides the Certificate interface getIssuer(), getNotAfter(), getNotBefore(), getSerialNumber(), getSigAlgName(), getSubject(), getType(), and getVersion() methods Networking security achieved by Secure Socket Layer (SSL) and the Transport Layer Security (TLS) protocols –HttpsConnection and SecureConnection interfaces –Method getSecurityInfo can be applied to an open connection to fill a SecurityInfo object Obtain protocol name and version, cipher suite, certificate of the connection


Download ppt "11 Developing MIDlets Dr. Miguel A. Labrador Department of Computer Science & Engineering"

Similar presentations


Ads by Google