MIDlet class - Sogang University Computer Security and Forensics

Download Report

Transcript MIDlet class - Sogang University Computer Security and Forensics

JAVA Programming Language
Chapter 11. Java Mobile Programming
Juho Kim
Department of Computer Science and Engineering
Sogang University
11 - 1
Wireless Application Protocol
Wireless Application Protocol
– Mobile phone, PDA, etc
– Text based internet and information service
• News, stock market, ticketing, and banking
– WML (Wireless Markup Language) tag
– Limitation
• Simple interface (text based)
• Security problem
• XHTML must be converted to WAP format
11-2
Java 2 Platform
Server
Desktop
Mobile Phones
Smart Phones
profiles
J2SE
J2EE
profiles
MIDP
CLDC
CDC
J2ME
Smart
Cards
JavaCard
Java Language
HotSpot
JVM
KVM
Card VM
J2EE (Java2 Enterprise Edition)
– Link to database, JSP (Java Server Page), Servlet, EJB (Enterprise Java Bean)
J2SE (Java2 Standard Edition)
– PC, Web browser
J2ME (Java2 Micro Edition)
– Mobile Platform
11-3
Structure of J2ME (Java2 Micro Edition)
Profiles
Configuration
Java Virtual Machine
Host Operating System
J2ME is Java specification for limited hardware environment having different
input, output, and display
Java virtual machine: interface software between compiled Java binary
code and hardware (microprocessor or hardware platform)
– KVM (Kilobyte Java Virtual Machine): Java virtual machine for mobile phone
Configuration: define essential java library and support java virtual machine
– CDC (Connected Device Configuration): library from J2SE and support JVM (tv,
navigation, PDA system)
– CLDC (Connected Limited Device Configuration): support KVM (mobile phone),
no support for floating-point operation, java.lang, java.io, java.util support
11-4
Structure of J2ME (Java2 Micro Edition)
Profile: supports application programs in limited computing environment
– MIDP (Mobile Information Device Profile): Java class library based on CLDC
configuration
Application
MIDP
KVM support machine structure
J2ME core APIs
KVM
CPU/Memory
– MIDP: responsible for application program installation, user interface, event
handling, storing data (part of API)
– CLDC: responsible for virtual machine, J2SE core class I/O, network model,
security model, international and localization
11-5
MIDlet
MIDlet
– MIDlet is a Java application framework for the MIDP that is typically
implemented on a Java-enabled cell phone, other embedded device or emulator.
– MIDlet will(should) run on devices that implements J2ME MIDP
– MIDlet is similar to J2SE applet
– Like all Java programs, MIDlets are "compile once, run anywhere“
11-6
Structure of MIDlet
Java
Java source
source
Javac
JAR
JavaManifest
source
Resource
Java
source
Java source
class
Java
. jar
JAR(Java Archive)
JAD(Java Application Descriptor)
MIDlet Suit
Every MIDlet Application has
– JAR(Java Archive): aggregates many files into one (Manifest, Class, Resource)
• Manifest file: a specific file contained within a JAR archive. It is used to define
extension and package related data
• Class file: Java application bytecode
• Resource: Different icons, images, and resources used in the class file
11-7
MIDlet
– Java Application Descriptor (JAD) files describe the Midlets(Java ME
applications) that are distributed as JAR files
• File which shows information about the program (JAR)
• Include MLDlet-Name, MIDlet-Version, MIDlet-Vendor, etc.
JAD files format
MIDlet-1: <Application name>, <icon path>, <midlet class>
MIDlet-Jar-Size: <Size in bytes>
MIDlet-Jar-URL: <Associated JAR file>
MIDlet-Name: <Application name>
MIDlet-Vendor: <Company>
MIDlet-Version: <Application version number>
MicroEdition-Configuration: <CLDC version>
MicroEdition-Profile: <MIDP version>
JAM(Java Application Manager)
1.Advertise App
on Web Page
Choose a game
Asteroids
Backgammon
Breakout
Missile
Back
Buy
( name,
version,
size )
2.User Selects App
3.JAM Downloads App
Installing…
Confirm
Buy[1]
Breakout
game tor:
$2.95
Back
Install
Stop
JAD
File
Network Transfer
JAR File
JAM(Java
Application
Manager)
JAM(Java Application Manager)
– MIDP device contains a program called JAM which downloads the MIDlet suite
from the server, opens it, and launches the user-specified MIDlet on the MIDP
device
11-9
MIDlet running process by JAM
User’s browser
1. User select JAD URL
WML PAGE
2. Browser starts JAM with URL
JAD
5. Store JAR
JAM
3. Download JAD
4. Download JAR and Icon files
6. Run KVM
Flash
JAR FILE
ICON FILE
7. Class Loading from JAR
Server
KVM
Mobile Phone
11-10
Life cycle of MIDlets
MIDlets are called by Java Application Manager
MIDlets move from one state to another of the lifecycle, as follows.
– Active: acquires resources and starts execution
– Pause: releases resources and becomes quiescent (wait)
– Destroy : releases all resources, destroys threads, and ends all
activity
JAM
startApp()
New myMIDlet()
Pause
destroyApp()
pauseApp()
Active
Destroyed
destroyApp()
11-11
MIDlet States and Events
Sequence of events
Event
Device
User starts
MIDlet
Device free
to initiate
MIDlet
Incoming
Call
Call Finished
Device
User stops
the MIDlet
JAM
Action
MIDlet calls
main app
Calls
startApp()
Calls
pauseApp()
Calls
startApp()
Calls
destroyApp()
MIDlet
State
Pause
Active
Paused
Active
Destroyed
Sequence of events
11-12
Building MIDlets
MIDlets must inherit from class javax.microedition.midlet.MIDlet
MIDlets must implement abstract methods in MIDlet class
– startApp(): this is run when the MIDlet is started
– pauseApp(): this is run when the application is paused e.g. incoming call
– destroyApp(): this is run when the MIDlet/application is closed
Additionally, it typically implements a default constructor and a
commandAction() from the CommandListener interface
11-13
MIDlet development tool
J2ME Wireless Toolkit
– Supports application development from Java source to MIDlet suite containing
.jar and .jad files, ready for deployment
– Provides several emulators that allows you to run the application on different
emulated target devices in PC platform
– Use KtoolBar to compile, build, and execute a MIDlet with the Emulator
– Download site: http://java.sun.com/products/sjwtoolkit/download.html
Create project
Compile
Emulation
Set target device
KtoolBar IDE in J2ME Wireless Toolkit
11-14
Device emulator
HelloMIDlet.java
//classes needed for MIDlet execution
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
Basic operating procedure
JAM call startApp()
public class HelloMIDlet extends MIDlet {
private Display display;
MIDlet entering to Active state
public HelloMIDlet() {
display = Display.getDisplay(this);
}
Get the Display object
public void startApp() {
//start application from here
TextBox t = new TextBox("Hello World ", 1024, 0);
}
display.setCurrent(t); //set the display
Set current screen to display
public void pauseApp() { //pause application }
}
public void destroyApp(boolean unconditional) { //stop application }
11-15
Command Objects
Command Object
– Provides methods to be executed when Soft-Buttons or keys of the Mobile
phone is pressed
– Command type: Back, Cancel, Exit, Help, Item, Ok, Stop…
Use of Soft-Buttons and keys
– Create a command object
Soft Button
private Command cmHelp
cmHelp = new Command("Help", Command.HELP, 1);
Control Key
– Add the command object to the form
fmMain.addCommand(cmHelp);
– Add a listener to the form
Navigation Key
fmMain.setCommandListener(this);
– Perform an action
public void commandAction(Command c, Displayable s)
{ if (c == cmHelp) display.setCurrent(tbHelp) }
11-16
Alphanumeric Key
CommandExample.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public void startApp() { display.setCurrent(form); }
public void pauseApp(){ }
public class CommandExample extends MIDlet
implements CommandListener {
private Display display;
private Form form;
private TextBox texthelp;
private Command exit; // Exit the MIDlet
private Command help; // Ask for Help
private Command back; // Go "back" to the main form
public void destroyApp(boolean unconditional){ }
public void commandAction(Command c, Displayable s){
public CommandExample() {
if (c == exit) {
destroyApp(false);
notifyDestroyed();
}
else if (c == help)
display.setCurrent(texthelp);
else if (c == back)
display.setCurrent(form);
}}
display = Display.getDisplay(this);
help = new Command("Help", Command.HELP, 1);
back = new Command("Back", Command.BACK, 1);
exit = new Command("Exit", Command.EXIT, 1);
//Create the Form, add Commands,listen for events
form = new Form("Command Example");
form.addCommand(exit);
form.addCommand(help);
form.setCommandListener(this);
}
//Create the help Textbox with a maximum of 25 characters
texthelp = new TextBox("Help", "Help text here...", 25, 0);
texthelp.addCommand(back);
texthelp.setCommandListener(this);
11-17
Commands
MIDP GUI
MIDP GUI (Display Classes)
– Library API related to the User interface are defined in javax.microedition.lcdui
– Display of the Mobile phone is defined as Displayable
– MIDP GUI is composed of high level GUI(Screen) and low level GUI(Canvas)
MIDlet
1:1
1:n
Display
0:n
Displayable
Screen
Alert
ChoiceGroup
TextBox
DateField
Command
Canvas
List
Form
Gauge
1:n
ImageItem
11-18
Item
StringItem
TextField
High level GUI
High level GUI (Screen)
– Screens including forms, lists, choice, textbox…
– All of these inherit: javax.microedition.lcdui.Screen
–
–
–
–
Easy to program: pre-defined objects
Not very flexible: difficult to customize objects
Used in creation of programs that use menu
Unsuitable for multimedia applications. Although, it can be used for menus and
settings
11-19
DateFieldTest.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class DateFieldTest extends MIDlet implements CommandListener{
Display
display;
Command
exitCommand;
public DateFieldTest() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
}
public void startApp() {
Form form
= new Form("DateFieldTest");
DateField dateField = new DateField("오늘의 날짜:", DateField.DATE);
dateField.setDate(new java.util.Date());
form.append(dateField);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
11-20
}}
Low level GUI
Low level GUI (Canvas)
– Base class for writing applications that need to handle low-level events and
respond to graphics calls for drawing to the display.
– Full control over screen display.
– Ability to process events and key presses.
Game Canvas (A subclass of Canvas)
– provides the basis for a game user interface
– provides game-specific capabilities
11-21
TimerTest.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class TimerTest extends MIDlet {
Display
display;
TimerCanvas tCanvas = new TimerCanvas();
timerTask tTask = new timerTask();
Timer
timer = new Timer();
public TimerTest() { display = Display.getDisplay( this ); }
protected void destroyApp( boolean unconditional ) { }
protected void startApp() {
display.setCurrent( tCanvas );
// set the display
timer.schedule( tTask, 1000, 1000 ); // start timer with one-second interval
}
protected void pauseApp() { }
public void exit(){
timer.cancel(); // stop timer
destroyApp( true );
notifyDestroyed();
}
class timerTask extends TimerTask {
public void run(){
tCanvas.action(); // move to new position after one second
}
}
11-22
TimerTest.java
class TimerCanvas extends Canvas { // define Canvas
int mX, mY, w, h;
Random pos = new Random();
public TimerCanvas(){
w = getWidth();
h = getHeight();
} // get size of the LCD
public void action() {
mX = (int)pos.nextInt()%w;
mY = (int)pos.nextInt()%h;
if(mX < 0) mX = -mX;
if(mX > (w-10)) mX = (w-10);
if(mY < 0) mY = -mY;
if(mY > (h-10)) mY = (h-10);
repaint();
} // set new random location
protected void paint( Graphics g ){
g.setColor( 0, 255, 0 );
g.fillRect( 0, 0, w, h);
g.setColor( 255, 0, 0 );
g.fillRect( mX, mY, 10, 10);
} // Draw a red rectangle
}
}
protected void keyPressed( int keyCode ){
exit();
}
11-23
J2ME Reference Site
Forum Nokia: http://www.forum.nokia.com
SamSung: http://innovator.samsungmobile.com
Orange: http://www.orangepartner.com/site/enuk/home/p_home.jsp
Sony Ericsson: http://developer.sonyericsson.com
Motorola: http://developer.motorola.com
Microsoft: http://developer.windowsphone.com/Default.aspx
Blackberry: http://na.blackberry.com/eng/developers
Several others available…
11-24