Introduction to Mobile Application Development
description
Transcript of Introduction to Mobile Application Development
Qusay H. Mahmoud 1
Introduction to Mobile Application Development
Qusay H. Mahmoud, Ph.D.Associate Professor
Department of Computing and Info ScienceUniversity of Guelph, Canada
http://www.cis.uoguelph.ca/~qmahmoud
Qusay H. Mahmoud 2
Mobile Applications
Mobile Apps are apps or services that can be pushed to a mobile device or downloaded and installed locally.
Classification• Browser-based: apps/services developed in a
markup language• Native: compiled applications (device has a
runtime environment). Interactive apps such as downloadable games.
• Hybrid: the best of both worlds (a browser is needed for discovery)
Qusay H. Mahmoud 3
Mobile PlatformsA wide variety of devices supporting different
platforms BlackBerry Palm OS Windows Mobile Symbian
Runtime environments & apps Browser-based apps (WAP) Flash-lite Java ME Qualcomm’s BREW Google’s Android
Having a choice is good…but not always… Device fragmentation
Qusay H. Mahmoud 4
Web Content for Mobile Devices
Markup languages to deliver Web content to device browsers: HDML
Phone.com (now Openwave)
Compact HTML (cHTML)NTT DoCoMo’s i-mode network
WAP Forum’s WMLA standard for content delivered to mobile
devicesNow: openmobilealliance.org
Qusay H. Mahmoud 5
WAP Network Structure
The WAP Gateway plays an important role
Qusay H. Mahmoud 6
WAP Programming Model
Similar to the Web programming model with extensions for the wireless environment
Qusay H. Mahmoud 7
WML Example
<?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <card id="MyFirstCard" title="First Card"> <p align="center"> My First WML Example </p> </card>
</wml>
Qusay H. Mahmoud 8
Java Platform
Qusay H. Mahmoud 9
Java 2 Platform
Virtual Machines and horizontal and vertical APIs specified in configurations and Profiles
Qusay H. Mahmoud 10
Configurations
A configuration defines the minimum APIs and VM capabilities for a family of devices: Similar requirements of memory size and
processing capabilitiesThe minimum APIs that an application
developer can expect to be available on implementing devices
May not contain any optional features
Qusay H. Mahmoud 11
Profiles
A profile is a collection of APIs that supplement a configuration to provide capabilities for a specific vertical market
Qusay H. Mahmoud 12
How do they fit together?
Profiles are built on top of configurations
Qusay H. Mahmoud 13
CLDC
Connected Limited Device Configuration Defines APIs and JVM capabilities APIs are subset of Java SE (java.lang, java.util, java.io) Also defines connectivity framework
(javax.microedition, io) but implementation left for profiles
CLDC 1.0 released in May 2000 (no support for floating point numbers). CLDC 1.1 in March 2003
Targets devices (such as cell phones) with: 160 (192) to 512 KB of total memory available for
Java technology Limited power (e.g. battery) Limited connectivity to a network (wireless) Constrained User Interface (small screen)
Qusay H. Mahmoud 14
MIDP
Targets mobile two-way communication devices implementing the CLDC
Built on top of CLDC to add other functionality like GUI, network implementation, local storage
It addresses: Display toolkit (user input) Persistent data storage HTTP based networking using CLDC generic connection
framework MIDP 1.0 released in December 2000, and MIDP 2.0
in November 2002 MIDP 2.0 new features: support for secure
networking (https), Game API, security mechanisms (code signing)
Qusay H. Mahmoud 15
KVM
Stands for Kilo Virtual MachineOriginated from a research project
called Spotless at Sun Research LabsImplements the classes defined in the
CLDC specificationNote: the UI classes are not part of the
CLDC
Qusay H. Mahmoud 16
KVM…
A complete runtime environment for small devices
Built from the ground up in CSmall footprint (40 – 80 KB)Class file verification takes place off-
deviceSupports multi-threadingSupports garbage collection
Qusay H. Mahmoud 17
KVM Security
VM level security Off-device pre-verification Small in-device verification
Application level security No Security Manager Sandbox security model:
Applications run in a closed environmentApplications can call classes supported by the
device
Qusay H. Mahmoud 18
Optional Packages
Core MIDP 2.0 functionality is limited. Vendors may include optional packages: JSR-75: File Connection and PIM APIs JSR-82: Bluetooth API JSR-120: Mobile Messaging API JSR-135: Mobile Media API JSR-179: Location API Many others…
Qusay H. Mahmoud 19
JTWI
JSR-185: Java Technology for Wireless Industry (umbrella specification)
Qusay H. Mahmoud 20
MSA
JSR-248: Mobile Service Architecture
Qusay H. Mahmoud 21
Wireless Device Stack
Host OSHost OS
MIDPMIDP
MIDlet
Other Profiles
Other Profiles
Train Schedule
UIHTTP
No floats (CLDC 1.0)KVM
CLDC
Qusay H. Mahmoud 22
CLDC Internals
The CLDC specification specifies VM features required by a CLDC implementation
Specifies requirements and APIs for Input/Output Networking
Qusay H. Mahmoud 23
Beyond the CLDC scope
Profiles implemented on top of CLDC specify APIs for: User Interface support Event handling Persistent support High-level application model
An example profile is the Mobile Information Device Profile (MIDP)
Qusay H. Mahmoud 24
Language & VM Compatibility
Goal: Full Java language and VM specification
compatibilityLanguage-level exception:
No floating point support in CLDC 1.0Manufacturers and developers can include their
own floating point
CLDC 1.1 supports floating point numbers
Qusay H. Mahmoud 25
CLDC vs. Java SE JVM
Limitations in CLDC supporting JVM: No floating point support (yes in CLDC 1.1) No finalization Limited error handling No Java Native Interface (JNI) No support for reflection No thread groups or daemon threads No weak references (yes in CLDC 1.1) No class loaders Partial bytecode verifier
Qusay H. Mahmoud 26
CLDC APIs
Classes inherited from J2SE v1.3 are in packages: java.lang java.io java.util
New classes introduced by the CLDC are in package: javax.microedition javax.lang.ref (CLDC 1.1 only)
CLDC 1.1 (JSR 139) has superseded the original CLDC 1.0 spec (JSR 30).
Qusay H. Mahmoud 27
CLDC Libraries: java.lang.*
BooleanByteCharacterClassIntegerLongMathObjectRunnable
RuntimeShortStringStringBufferSystemThreadThrowable
Qusay H. Mahmoud 28
CLDC Libraries: java.io.*
ByteArrayInputStreamByteArrayOutputStrea
mDataInputDataOutputDataInputStreamDataOutputStream InputStream
OutputStreamInputStreamReaderOutputStreamWrite
rPrintStreamReaderWriter
Qusay H. Mahmoud 29
CLDC Libraries: java.util.*
CalendarDateEnumerationHashtableRandomStackTimeZoneVector
No java.util.concurrent (use Threads)
Qusay H. Mahmoud 30
CLDC 1.0 vs. CLDC 1.1
Very similar. The most important differences are floating-point math capabilities and support for weak references. Classes Float and Double have been added A small subset of Java SE weak-reference
classes
Qusay H. Mahmoud 31
MIDP internals
Goal: MIDP implementation must fit in small
footprint (128KB ROM) Must run with limited heap size (32-200KB
RAM)
To be implemented by device manufacturers, operators, or developers
Qusay H. Mahmoud 32
MIDlets
A MIDlet consists of a class that extends the MIDlet class and other classes as needed
To handle events it must implement the CommandListener interface
public class MyMIDlet extends MIDlet implements CommandListener{
}
Qusay H. Mahmoud 33
MIDP Application Lifecycle
MIDlets move from state to state in the lifecycle: Start: acquire resources and start executing Pause: release resources and wait Destroyed: release all resources and end all
activities Paused
Active
Destroyed
startApp()
destroyApp()
destroyApp()
pauseApp()
Qusay H. Mahmoud 34
Example MIDlet
import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*;
public class FirstMIDlet extends MIDlet { Display display = null; TextBox tb = null; public FirstMIDlet() { display = Display.getDisplay(this); }
Qusay H. Mahmoud 35
Example MIDlet …
public void startApp() { tb = new TextBox("FirstMIDlet", "Welcome to MIDP Programming", 40, 0); display.setCurrent(tb); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { } }}
Qusay H. Mahmoud 36
MIDlet Packaging
Two or mode MIDlets form a MIDlet suiteOne or more MIDlets may be packaged
in a single JAR file that includes: A manifest describing the contents Java classes for the MIDlet(s) Resource file(s) used by the MIDlet(s)
Each jar file is accompanied by a Java Application Descriptor (JAD) file
Qusay H. Mahmoud 37
MIDlet Packaging
Java Application Descriptor (JAD) file provides info:
Configuration properties Pre-download properties
Size, version, storage requirements
Qusay H. Mahmoud 38
Example MIDlet…
Compile (javac) Preverify (off device preverification) Create a JAR file: first.jar Create a JAD file: first.jad
MIDlet-Name: MyFirst MIDlet-Version: 1.0.0 MIDlet-Vendor: Sun Microsystems, Inc. MIDlet-Description: My First MIDlet MIDlet-Info-URL: http://java.sun.com/j2me/ MIDlet-Jar-URL: first.jar MIDlet-Jar-Size: 1063 MicroEdition-Profile: MIDP-1.0 MicroEdition-Configuration: CLDC-1.0 MIDlet-1: MyFirst,, FirstMIDlet
Qusay H. Mahmoud 39
Example MIDlet: Testing
emulator –Xdescriptor first.jad
Qusay H. Mahmoud 40
MIDlet Example: Deploy
Local: USB, Bluetooth Web:
To deploy a MIDlet on a web server, you need to add a new MIME type:
text/vnd.sun.j2me.app-descriptor jad application/java-archive jar
Create an HTML file with link to the .jar file Use the following command to run:
emulator –Xdescriptor:<JAD file>
Push registry: incoming network connections can launch specific MIDlets
Qusay H. Mahmoud 41
Simplifying the Development Effort
Sun Java Wireless Toolkit for CLDC
Qusay H. Mahmoud 42
RIM’s BlackBerry
A smart phoneSelling feature has been instance, secure,
mobile access to emailAttracting a wider range of mobile consumersWell-engineering (consistency across
handhelds)Friction-free path to market applications (no
excessive certification)
Supports Java MERIM’s Java Development Environment (JDE)
Qusay H. Mahmoud 43
BlackBerry
BlackBerry file format (.cod)RIM’s JDE comes with tools to convert
existing Java ME apps (.jad & .jar) into .cod files
Example: Use rapc to convert .jad & .jar into .cod Use javaloader to load on BlackBerry
Qusay H. Mahmoud 44
BlackBerry
To utilize BlackBerry-specific features, use BlackBerry APIs
Application lifecycle is different
Qusay H. Mahmoud 45
BlackBerry
Rest of code…
Qusay H. Mahmoud 46
MIDP APIs
MIDP specifies APIs for:
User Interface Networking (based on CLDC) Persistent Storage Others…(Game API, security, etc)
Qusay H. Mahmoud 47
MIDP Packages
MIDP 1.0 java.lang Java.util Java.io Javax.microedition.io Javax.microedition.lcdui Javax.microedition.midlet Javax.microedition.rms
Qusay H. Mahmoud 48
MIDP Packages
MIDP 2.0 Javax.microedition.lcdui.game Javax.microedition.media Javax.microedition.media.control Javax.microedition.pki
Qusay H. Mahmoud 49
MIDP User Interface
Not a subset of AWT or Swing because: AWT is designed for desktop computers Assumes certain user interaction models
(pointing device such as a mouse) Window management (resizing overlapping
windows). This is impractical for cell phones
Consists of high-level and low-level APIs
Qusay H. Mahmoud 50
MIDP UI APIs
High-level API Applications should be runnable and usable
in all MIDP devices No direct access to native device features
Low-level API Provides access to native drawing primitives,
device key events, native input devices Allows developers to choose to compromise
portability for user experience
Qusay H. Mahmoud 51
MIDP UI Programming Model
The central abstraction is a screenOnly one screen may be visible at a timeThree types of screens:
Predefined screens with complex UI components (List, TextBox)
Generic screens (Form where you can add text, images, etc)
Screens used with low-level API (Canvas)
Qusay H. Mahmoud 52
MIDP UI and Display
The Display class is the display manager
It is instantiated for each active MIDletProvides methods to retrieve
information about the device’s display capabilities
A screen is made visible by calling: Display’s setCurrent(screen);
Qusay H. Mahmoud 53
MIDP UI Classes
javax.microedition.lcdui classes:Alert, AlertType, Canvas, ChoiceGroup, Command, DateField, Display, Displayable, Font, Form, Gauge, Graphics, Image, ImageItem, Item, List, Screen, StringItem, TextBox, TextField, Ticker
javax.microedition.lcdui interfaces:Choice, CommandListener, ItemStateListener
Qusay H. Mahmoud 54
MIDP UI Class Diagram
Major classes and interfaces:
Qusay H. Mahmoud 55
High-Level API Examples
List:Display display = Display.getDisplay(this);
List menu = new List(“Method of payment”, Choice.EXCLUSIVE);
menu.append(“Visa”);
menu.append(“MasterCard”);
menu.append(“Amex”);
display.setCurrent(menu);
Qusay H. Mahmoud 56
High-Level API Examples…
Form (Date/Time info):
DateField date = new DateField(“Today’s
date”, DateField.TIME);
Form form = new Form(“Date Info”);
form.append(date);
display.setCurrent(form);
Qusay H. Mahmoud 57
High-Level Examples…
Form (Sign in screen):Display display = Display.getDisplay(this);TextField userName = new TextField(“LoginID:”, “”,
10, TextField.ANY);TextField password = new TextField(“Password:”, “”,
10, TextField.PASSWORD);Form form = new Form(“Sign in”);form.append(userName);form.append(password);display.setCurrent(form);
Qusay H. Mahmoud 58
Low-level Example
Canvas:public class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(255, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 255, 255);
g.drawString("Hello World!", 0, 0, g.TOP | g.LEFT);
}
}
Qusay H. Mahmoud 59
Low-level Example…
Instantiate and display MyCanvaspublic class MyMidlet extends MIDlet { public MyMidlet() { // constructor } public void startApp() { Canvas canvas = new MyCanvas(); Display display = Display.getDisplay(this); display.setCurrent(canvas); } // pauseApp() and destroyApp()}
Qusay H. Mahmoud 60
Input Handling
High-Level API input is handled using abstract commands No direct access to soft buttons Commands are mapped to appropriate soft
buttons or menu items
Qusay H. Mahmoud 61
Input Handling: Example
TextBox screen with commands:Display display = Display.getDisplay(this);TextBox tb = new TextBox(“MIDP”, “Welcome to MIDP
Programming”, 40, TextField.ANY);Command exit = new Command(“Exit”, Command.SCREEN, 1);Command info = new Command(“Info”, Command.SCREEN, 2);Command buy = new Command(“Buy”, Command.SCREEN, 2);tb.addCommand(exit);tb.addComment(info);tb.addCommand(buy);display.setCurrent(tb);
Qusay H. Mahmoud 62
Event Handling: High-level
High-level Events:
Based on a listener model Screen objects can have listeners for
commands For an object to be a listener, it must
implement the CommandListener interface This interface has one method: commandAction
Qusay H. Mahmoud 63
Event Handling: Example
MIDlet implements CommandListenerpublic class MyMIDlet extends MIDlet implements
CommandListener { Command exitCommand = new Command(…); // other stmts public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }}
Qusay H. Mahmoud 64
Event Handling: Example
Handling List events:public void commandAction(Command c, Displayable d) { if (c == exitCommand) { ..
} else { List down = (List)display.getCurrent(); switch(down.getSelectedIndex()) { case 0: testTextBox();break; case 1: testList();break; case 2: testAlert();break; case 3: testDate();break; case 4: testForm();break; }
Qusay H. Mahmoud 65
Event Handling: Low-level
Low-level Events:
Low-level API gives developers access to key press events
Key events are reported with respect to key codes
MIDP defines key codes: KEY_NUM0 .. KEY_NUM9, KEY_STAR, KEY_POUND
Qusay H. Mahmoud 66
Handling Events: example
Low-level eventsprotected void keyPressed(int keyCode) { if (keyCode > 0) { System.out.println("keyPressed " +((char)keyCode)); } else { System.out.println("keyPressed action “
+getGameAction(keyCode)); } }
Qusay H. Mahmoud 67
MIDP UI Design Principles
Make the UI simple and easy to useUse the high-level API (portability)If you need to use low-level API, keep
to the platform-independent partMIDlets should not depend on any
specific screen sizeEntering data is tedious, so provide a
list of choices to select from
Qusay H. Mahmoud 68
Networking
J2SE and J2EE networking APIs are not suitable for handheld devices Require several megabytes of memory to run Device manufacturers who work with circuit-
switched networks require TCP support Device manufacturers who work with packet-
switched networks require UDP support Other devices have specific mechanisms for
communications
Qusay H. Mahmoud 69
CLDC Generic Connections
A set of related abstractions at the programming level
No abstractions for different forms of communications
All connections are created using the Connector.open()
If successful, it returns an object that implements one of the generic connection interfaces
Qusay H. Mahmoud 70
Connection Interfaces
MIDP 1.0
Qusay H. Mahmoud 71
MIDP 2.0
Qusay H. Mahmoud 72
Example Connections
HTTP: Connector.open(“http://www.host.com”);
Socket: Connector.open(“socket://host.com:80”);
Datagram: Connector.open(“datagram://address:port”);
File: Connector.open(“file:/myfile.txt”);
Qusay H. Mahmoud 73
Advantages of CLDC Generic Connections
Isolate the differences between the setup of one protocol and another
Most of the application code remains the same regardless of the protocol you use
Note: CLDC itself does not provide any protocol implementation
Qusay H. Mahmoud 74
MIDP Connectivity
It provides support for HTTP (HttpConnection)
Why? HTTP can be implemented using IP protocols or non-IP protocols
Qusay H. Mahmoud 75
HttpConnection
Part of the javax.microedition.ioDefines the necessary methods and
constants for an HTTP connection
HttpConnection c = (HttpConnection) Connector.open(“http://quotes.yahoo.com”);
C.setRequestMethod(HttpConnection.POST);C.setRequestProperty(“Content-Language”, “en-CA”);
Qusay H. Mahmoud 76
Invoking Remote Applications
A MIDlet may invoke remote applications:
Fetching a page Invoking a CGI script (GET or POST
method) Invoking a Servlet
Qusay H. Mahmoud 77
Example: Invoke a CGI Script
GET Method:
String url = “http://host/cgi-bin/getgrade?idnum=182061”;c = (HttpConnection) Connector.open(url);c.setRequestMethod(HttpConnection.GET);// set some request properties: c.setRequestPropert(“ “, “
“);is = c.openDataInputStream();while((ch = is.read()) != -1) { b.append((char)ch);}
Qusay H. Mahmoud 78
Example…
If you want to send data to a remote application:
String s = “stuffToSend”;byte postmsg[] = s.getBytes();for(int i=0;i<postmsg.length;i++) { os.writeBytes(postmsg[i]);}// ORos.write(s.getBytes());
Qusay H. Mahmoud 79
Databases
A persistent storage: a place to store the state of objects
Facilities provided in J2SE and J2EE are not suitable for handheld devices
MIDP provides a record-oriented database mechanism to persistently store data and retrieve it later
Qusay H. Mahmoud 80
MIDP’s RMS
Lightweight record-oriented database
Device independent API Unique recordID for each record within the
store A record is an array of bytes Shared within MIDlet suite Support for enumeration, sorting, and filtering
javax.microedition.rms
Qusay H. Mahmoud 81
MIDP RMS Methods
Record StoreopenRecordStore, closeRecordStore, listRecordStore, deleteRecordStore, getRecordSize, getNumRecords
Record DataaddRecord, deleteRecord, getRecord, setRecord, getRecordSize
Record SelectionRecordEnumeration, RecordFilter, RecordComparator
Qusay H. Mahmoud 82
RMS: Record Stores
To open a record store: RecordStore db =
RecordStore.openRecordStore(“myDB”, true);
To close a record store: db.closeRecordStore();
Qusay H. Mahmoud 83
Create/Add a new record
To create a new record:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(record);Byte b[] = baos.toByteArray();db.addRecord(b, 0, b.length);
Qusay H. Mahmoud 84
Building a Stock Database
See appendix for source code: Stock.java StockDB.java QuotesMIDlet.java
Qusay H. Mahmoud 85
StockQuotes MIDlet
Qusay H. Mahmoud 86
Protection DomainsMIDP provides a security model to prevent
bad MIDlets from misbehavingModel: protection domains (determine what
operations are permitted in a MIDlet) When a MIDlet suite is installed, it goes into
some protection domain (rules to determine what happens when trying to perform a sensitive operation) Net connection (costs money) Reading a file (privacy)
Operation: allowed, denied, user prompted for permission
Qusay H. Mahmoud 87
Protection Domains
The specification is flexible and leaves manufacturers and carriers room for implementing security models
Implementations vary: Capturing an image restricted on some
devices Playing audio or video restricted on some
deviceMIDlet suites can be signed or unsigned
(different protection domains)
Qusay H. Mahmoud 88
Protection DomainsWhat to expect?
Un-trusted domain where unsigned MIDlets are denied permission or user promoted for every sensitive operation
Manufacturer or carrier domain: allow MIDlets the most freedom
Identified 3rd party domain: signed MIDlets with keys obtained from known certification authorities (less freedom because no testing or certification by authorized parties)
Devices from different vendors behave differently…
Qusay H. Mahmoud 89
BlackBerry…
That is why the BLackBerry rules! If your application uses Controlled APIs
(e.g. email), your must sign your application using keys from RIM
Register for keysCost: $100Request through online formTakes a couple of daysCan be used for 1,000,000 signatures
http://www.blackberry.com/SignedKeys
Qusay H. Mahmoud 90
Java ME Resources Java ME:
http://java.sun.com/j2me Sun Java Wireless Toolkit for CLDC:
http://java.sun.com/products/sjwtoolkit RIM BlackBerry JDE:
http://na.blackberry.com/eng/developers/downloads/jde.jsp
NetBeans: http://www.netbeans.org
JCP:http://www.jcp.org
Resources for academicshttp://www.uoguelph.ca/~qmahmoud/javame & http://cmer.cis.uoguelph.ca
Qusay H. Mahmoud 91
Thank you
Q&A