MIDlets and Ant April 3 2003 Shane Cantrell

  • Slides: 30
Download presentation
MIDlets and Ant April 3, 2003 Shane Cantrell Zach Crisman CSE 403: MIDlets and

MIDlets and Ant April 3, 2003 Shane Cantrell Zach Crisman CSE 403: MIDlets and Ant 1

MIDlet: Skeleton import javax. microedition. midlet. *; public class My. MIDlet extends MIDlet {

MIDlet: Skeleton import javax. microedition. midlet. *; public class My. MIDlet extends MIDlet { public My. MIDlet() {} // constructor public void start. App() {} // entering active state public void pause. App() {} // entering paused state // entering destroyed state public void destroy. App(boolean unconditional) {} } CSE 403: MIDlets and Ant 2

MIDlet: State Transitions Paused pause. App() start. App() Active destroy. App() Destroyed CSE 403:

MIDlet: State Transitions Paused pause. App() start. App() Active destroy. App() Destroyed CSE 403: MIDlets and Ant 3

MIDlet: Resources n Official J 2 ME WTK 1. 0. 4 Page n n

MIDlet: Resources n Official J 2 ME WTK 1. 0. 4 Page n n http: //java. sun. com/products/j 2 mewtoolkit/ API reference n file: //C: /WTK 104/doc/api/index. html CSE 403: MIDlets and Ant 4

MIDlet: JAD file MIDlet-Name: Introductory Midlets MIDlet-1: Properties, , PView MIDlet-2: Accounts, , Account.

MIDlet: JAD file MIDlet-Name: Introductory Midlets MIDlet-1: Properties, , PView MIDlet-2: Accounts, , Account. Viewer MIDlet-Vendor: Univ of Washington MIDlet-Version: 1. 13 MIDlet-Jar-Size: 23919 MIDlet-Jar-URL: HW 1. jar Image-1 -name: Ant Image-1 -location: /res/ant. gif CSE 403: MIDlets and Ant 5

MIDlet: Getting Properties n Class javax. microedition. midlet. MIDlet n String get. App. Property(String

MIDlet: Getting Properties n Class javax. microedition. midlet. MIDlet n String get. App. Property(String key) n property = get. App. Property(“Image-1 -name”); CSE 403: MIDlets and Ant 6

MIDlet: Displayable classes n n package javax. microedition. lcdui. * Class Screen n n

MIDlet: Displayable classes n n package javax. microedition. lcdui. * Class Screen n n abstract for classes Alert, Form, List, and Text. Box, which allows use of basic GUI elements Class Canvas n allows full control over painting and input handling CSE 403: MIDlets and Ant 7

MIDlet: Using Displayable // from within a MIDlet class. . . // Get the

MIDlet: Using Displayable // from within a MIDlet class. . . // Get the display for this MIDlet Display display = Display. get. Display(this); // Create the displayable object Displayable displayable = new My. Displayable. Class(); // Select the displayable object to show the user display. set. Current(displayable); CSE 403: MIDlets and Ant 8

MIDlet: Class Form n useful functions n int append(. . . ) n n

MIDlet: Class Form n useful functions n int append(. . . ) n n Choice. Group, Date. Field, Gauge, Image. Item, String. Item, Text. Field void set. Item. State. Listener(Item. State. Listener i. Listener) CSE 403: MIDlets and Ant 9

MIDlet: Interface Item. State. Listener n key function to implement n n item. State.

MIDlet: Interface Item. State. Listener n key function to implement n n item. State. Changed(Item item) Class Item n n void set. Label(string label) String get. Label() CSE 403: MIDlets and Ant 10

MIDlet: Class Canvas n key functions to implement n n n void paint(Graphics g)

MIDlet: Class Canvas n key functions to implement n n n void paint(Graphics g) void key. Pressed(int key. Code) other useful functions n int get. Game. Action(int key. Code) n n n Needed to use arrow buttons! int get. Width() int get. Height() CSE 403: MIDlets and Ant 11

MIDlet: Internet Classes n Class Connection n Datagram (Warning: A separate Datagram object is

MIDlet: Internet Classes n Class Connection n Datagram (Warning: A separate Datagram object is needed for both input and output!) n n = Connector. open(“datagram: //somewhere. com: 8000”) = Connector. open(“datagram: //: 8000”) Stream. Connection n o. Stream, i. Stream n = Connector. open(“socket: //somewhere. com: 8000”) Http. Connection n = Connector. open(“http: //www. somewhere. com: 80”) CSE 403: MIDlets and Ant 12

MIDlet: Class Record. Store n n package javax. microedition. rms. * useful functions n

MIDlet: Class Record. Store n n package javax. microedition. rms. * useful functions n n n n static void delete. Record. Store(. . . ) static Record. Store open. Record. Store(. . . ) int get. Size() int get. Size. Available() int add. Record(. . . ) int get. Record(. . . ) Record. Enumeration enumerate. Records(. . . ) CSE 403: MIDlets and Ant 13

Design Points n Limitations n n n MIDlet JARs: ~64 k or less awkward

Design Points n Limitations n n n MIDlet JARs: ~64 k or less awkward user input Good Design n n wait animations remember old inputs after error message n (for example, remember the user name if the password was entered incorrectly) CSE 403: MIDlets and Ant 14

Programming Guidelines n Use local variables instead of fields. n n Avoid unnecessary method

Programming Guidelines n Use local variables instead of fields. n n Avoid unnecessary method calls. Avoid string concatenation; make use of String. Buffer instead. Reuse (recycle) objects whenever possible. n n Local variables are faster than class members. Memory is limited and garbage collecting takes time. Use multi-threading to keep the MIDlet responsive. CSE 403: MIDlets and Ant 15

Apache Ant n xml based build tool using Java extended using Java classes Ant

Apache Ant n xml based build tool using Java extended using Java classes Ant Is Not a Scripting Language! n http: //ant. apache. org/ n n CSE 403: MIDlets and Ant 16

Ant: Basic Example <project default="dist" name=“Build. File" basedir=". "> <description> Build file description. .

Ant: Basic Example <project default="dist" name=“Build. File" basedir=". "> <description> Build file description. . . </description> <!–- This is a comment. --> <property location="dist" name="dist"/> <property file="build. properties"/> <target name="dist" description="Generate the distribution. ">. . . </target> </project> CSE 403: MIDlets and Ant 17

Ant: Properties File Example # This file defines properties for an Ant midlet builder.

Ant: Properties File Example # This file defines properties for an Ant midlet builder. # The properties can be changed at run time by specifying # new values on the ant command line with the -D switch. # For example, -Demulator=Default. Color. Phone would request the # Default Color Phone instead of the Motorola i 85 s. # # $Id: build. properties, v 1. 2 2003/04/01 04: 26: 18 finson Exp $ # Location of the Wireless Toolkit installation. wtk. home = c: /wtk 104 # Default emulator. Can be any valid device as listed # in the WTK 104/wtklib/devices directory. emulator = Motorola_i 85 s CSE 403: MIDlets and Ant 18

Ant: Properties n <property name=“src” location=“source” /> n n n <property name=“src” value=“source” />

Ant: Properties n <property name=“src” location=“source” /> n n n <property name=“src” value=“source” /> n n The name of the property. Specified by “${src}” in this case. The directory value that it represents is relative to the basedir (unless the location is absolute). The value stays as “source”. <property file=“build. properties”/> n Uses properties listed in the given file. CSE 403: MIDlets and Ant 19

Ant: Targets n n <target name=“A”> name=“B” depends=“A”> name=“C” depends=“A, C, B”> CSE 403:

Ant: Targets n n <target name=“A”> name=“B” depends=“A”> name=“C” depends=“A, C, B”> CSE 403: MIDlets and Ant 20

Ant: Filesets <fileset dir="${basedir}" casesensitive="yes" id="doc. files"> <include name="**/*. txt"/> <exclude name="**/*. zip"/> <exclude

Ant: Filesets <fileset dir="${basedir}" casesensitive="yes" id="doc. files"> <include name="**/*. txt"/> <exclude name="**/*. zip"/> <exclude name="build. xml"/> <exclude name="total. txt"/> </fileset> n Collects the list of files matching the provided attributes and gives them the reference provided by ‘id’. CSE 403: MIDlets and Ant 21

Ant: Pathconverts <pathconvert pathsep=“ ” property=“files” refid=“doc. files”/> n makes a list of the

Ant: Pathconverts <pathconvert pathsep=“ ” property=“files” refid=“doc. files”/> n makes a list of the files separated by spaces n n (Warning: If any of the paths have spaces, this will cause problems!) refid = fileset id property = destination id pathsep = separation string CSE 403: MIDlets and Ant 22

Ant: Executing Shell Programs <executable="F: Ant. Testcollector. appcollector. exe" dir="${basedir}"> <arg line="${basedir}/. . /total.

Ant: Executing Shell Programs <executable="F: Ant. Testcollector. appcollector. exe" dir="${basedir}"> <arg line="${basedir}/. . /total. txt"/> <arg line="${files}"/> </exec> n runs collector. exe from ${basedir} with the arguments providedmakes a list of the files separated by spaces CSE 403: MIDlets and Ant 23

Ant: Taskdefs <taskdef name=“wtkjad” classname=“de. pleumann. antenna. Wtk. Jad”/> n n allows you to

Ant: Taskdefs <taskdef name=“wtkjad” classname=“de. pleumann. antenna. Wtk. Jad”/> n n allows you to refer to the plugin class Wtk. Jad as wtkjad in your Ant build file inherit task classes from org. apache. tools. ant. Task n file: //C: /jakarta-ant-1. 5. 1/docs/manual/api/index. html CSE 403: MIDlets and Ant 24

Other Tasks n n n javac copy, delete, move, mkdir zip, unzip, jar, war

Other Tasks n n n javac copy, delete, move, mkdir zip, unzip, jar, war n n mail n n n It is possible to extract specific files from a zip file using a patternset. for SMTP email (must not require a password) waitfor echo CSE 403: MIDlets and Ant 25

Ant from the prompt n ant –buildfile build 403. xml clean file n n

Ant from the prompt n ant –buildfile build 403. xml clean file n n target a build file of build. xml is default ant run target CSE 403: MIDlets and Ant 26

Unix Setup n Ant Path n n /usr/local/ant/bin Wireless Toolkit Path n /usr/local/wtk-1. 0.

Unix Setup n Ant Path n n /usr/local/ant/bin Wireless Toolkit Path n /usr/local/wtk-1. 0. 4 CSE 403: MIDlets and Ant 27

Environment Variables n ANT_HOME = c: jakarta-ant-1. 5. 1 JAVA_HOME = c: j 2

Environment Variables n ANT_HOME = c: jakarta-ant-1. 5. 1 JAVA_HOME = c: j 2 sdk 1. 4. 1_01 Path = %PATH%; %ANT_HOME%bin; c: wtk 104bin; %JAVA_HOME%bin n Comments: n n Remember to avoid spaces in your target directory and file names! If your MIDlet does not run by selecting the JAD file, then it does not count as release worthy. (For example, running your MIDlet through the KToolbar does not count as complete. ) CSE 403: MIDlets and Ant 28

Homework Comments n n You are adding the “dist” target to the provided build.

Homework Comments n n You are adding the “dist” target to the provided build. xml file. The directory structure, ${dist. dir}, and other variables are defined in build. xml. Don’t forget to refer to the Ant web site (or Ant installation docs) for more task information and examples. Don’t forget to send questions to either the email list or all three of us. CSE 403: MIDlets and Ant 29

MIDlets n Free MIDlet programs n n http: //midlet. org/index 2. jsp Cannons: ME

MIDlets n Free MIDlet programs n n http: //midlet. org/index 2. jsp Cannons: ME (multiplayer game) n http: //games. macrospace. com/cannonsme/ CSE 403: MIDlets and Ant 30