Tutorial Capuchin Flash Java Developers r2a

Embed Size (px)

Citation preview

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    1/41

    Tutorial

    October 2008

    Getting started with ProjectCapuchin for Flash and Javadevelopers

    on Sony Ericsson phones

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    2/41

    Tutorial | Project Capuchin for Flash and Java developers

    2 October 2008

    This document is published by Sony EricssonMobile Communications AB, without anywarranty*. Improvements and changes to this textnecessitated by typographical errors, inaccuraciesof current information or improvements toprograms and/or equipment, may be made bySony Ericsson Mobile Communications AB at anytime and without notice. Such changes will,however, be incorporated into new editions of thisdocument. Printed versions are to be regarded astemporary reference copies only.

    *All implied warranties, including without limitationthe implied warranties of merchantability or fitnessfor a particular purpose, are excluded. In no eventshall Sony Ericsson or its licensors be liable forincidental or consequential damages of anynature, including but not limited to lost profits orcommercial loss, arising out of the use of theinformation in this document.

    This Tutorial is published by:

    Sony Ericsson Mobile Communications AB,

    SE-221 88 Lund, Sweden

    Phone: +46 46 19 40 00Fax: +46 46 19 41 00www.sonyericsson.com/

    Sony Ericsson Mobile Communications AB,2008. All rights reserved. You are hereby granteda license to download and/or print a copy of thisdocument.Any rights not expressly granted herein arereserved.

    First revised edition (October 2008)Publication number: 1218-3995.1 (rev. B)

    Preface

    About this tutorial

    This tutorial illustrates in several examples how the Project Capuchin API can be used as a bridgebetween Java Platform, Micro Edition (Java ME) and Adobe Flash Lite in Sony Ericsson phones.

    Sony Ericsson Developer WorldAt www.sonyericsson.com/developer, developers find the latest technical documentation anddevelopment tools such as phone White papers, Developers guidelines for different technologies, Gettingstarted tutorials, SDKs (Software Development Kits) and tool plugins. The Web site also features newsarticles, go-to-market advice, moderated discussion forums offering free technical support and a Wikicommunity sharing expertise and code examples.

    For more information about these professional services, go to the Sony Ericsson Developer World Website.

    http://www.sonyericsson.com/developerhttp://www.sonyericsson.com/developer
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    3/41

    Tutorial | Project Capuchin for Flash and Java developers

    3 October 2008

    Prerequisites

    The examples of this tutorial relies on Java ME and Adobe Flash CS3 for creating mobile applications.

    Here is a short description of how to set up the development environment. See Setting up the Javaenvironment for Project Capuchin on page 28 for a detailed description of the setup procedures.

    Install Eclipse and the Java RuntimeEnvironment (JRE) on your system.Eclipse is available for download at http://www.eclipse.org/downloads/.JRE is available at http://www.java.com/en/download/manual.jsp.

    Install the Eclipse ME plug-in, available at http://eclipseme.org/docs/installation.html

    Install the Sony Ericsson SDK for Java ME,available athttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsp.

    Include the Project Capuchin classes in Eclipse.They are found in the tutorial_code.zip file,included with the download package of thisdocument.

    Install Flash CS3 or any Flash version that iscompatible with Flash Lite 2.1 and ActionScript2. Also make sure that the Adobe DeviceCentral is installed. It will be used for choosingthe targeted platform and for emulation duringthe test phase. Adobe Device Central isavailable for download atwww.adobe.com/products/creativesuite/devicecentral.

    Typographical conventions

    In this document code examples are written in Courier font:FlashPlayer.createFlashPlayer(flashImage,flashCanvas);

    Trademarks and acknowledgements

    Adobe, Adobe Flash Lite and Adobe Flash are either trademarks or registered trademarks of AdobeSystems Incorporated in United States and/or other countries.

    Java and all Java based trademarks and logos are trademarks or registered trademarks of SunMicrosystems, Inc, in the U.S. and other countries.

    Other product and company names mentioned herein may be the trademarks of their respective owners.

    http://www.eclipse.org/downloads/http://www.eclipse.org/downloads/http://www.java.com/en/download/manual.jsphttp://www.java.com/en/download/manual.jsphttp://eclipseme.org/docs/installation.htmlhttp://eclipseme.org/docs/installation.htmlhttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://www.adobe.com/products/creativesuite/devicecentralhttp://www.adobe.com/products/creativesuite/devicecentralhttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://eclipseme.org/docs/installation.htmlhttp://eclipseme.org/docs/installation.htmlhttp://www.java.com/en/download/manual.jsphttp://www.java.com/en/download/manual.jsphttp://www.eclipse.org/downloads/http://www.eclipse.org/downloads/http://www.adobe.com/products/creativesuite/devicecentralhttp://www.adobe.com/products/creativesuite/devicecentral
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    4/41

    Tutorial | Project Capuchin for Flash and Java developers

    4 October 2008

    Document history

    Change history

    2008-08-31 Doc. no. 1218-3995.1 First version published on Developer World

    2008-10-17 Doc. no. 1218-3995.1(rev. B)

    First revised version.New document layout

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    5/41

    Tutorial | Project Capuchin for Flash and Java developers

    5 October 2008

    Contents

    Introduction .................................................................................................................6

    Architecture .................................................................................................................7

    Tutorial .........................................................................................................................8

    The Clock application ................................................................................................8The Flash Lite part .................................................................................................8The Java part .......................................................................................................13SWF2JAR ............................................................................................................14

    Data transfer in Project Capuchin applications .......................................................14DataRequest example .........................................................................................15ExtendedEvents example ....................................................................................16

    Development ...........................................................................................................17Tasklist example ......................................................................................................18Application requirement and UI draft ..................................................................18Decide data transfer mechanisms .......................................................................18Format of data transfer ........................................................................................19Implement and test Flash UI ................................................................................19Java implementation ............................................................................................20Testing and releasing the application ..................................................................21

    StepCounter example .............................................................................................21Application requirement and UI draft ..................................................................21Deciding data transfer mechanism ......................................................................22Format of data transfer ........................................................................................22Implementing and testing the Flash UI ................................................................22Java implementation ............................................................................................23Testing and releasing the application ..................................................................24

    FAQ .............................................................................................................................25

    Appendix ................................................................................................................27

    Setting up the Java environment for Project Capuchin ..........................................28Installing the EclipseME plugin ............................................................................29Setting up the Sony Ericsson SDK with Eclipse Environment. ............................32Installing the Project Capuchin basic APIs ..........................................................35

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    6/41

    Tutorial | Project Capuchin for Flash and Java developers

    6 October 2008

    Introduction

    The Project Capuchin API provides a bridge between Java ME and Adobe Flash Lite. It enablesapplications to use Flash Lite as the front end and Java ME as the back end.

    This API is the base for applications where the UI presentation is designed in Flash Lite while the datalogic is provided by Java code. Data transmission is enabled both ways, from Java ME to Flash Lite andvice versa.

    The Project Capuchin API allows fast UI development and easy collaboration between graphic designersand code developers. Once all interfaces between the presentation and service layers have been defined,UI designers can develop and test the presentation layer in the Flash Lite environment independent of theJava service layer development and testing.

    Project Capuchin also improves Flash Lite content security, distribution and installation by embeddingFlash Lite content as a resource in MIDlet suites.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    7/41

    Tutorial | Project Capuchin for Flash and Java developers

    7 October 2008

    Architecture

    The Project Capuchin API defines several different classes that can be used for developing Java MIDlets,for example, classes to communicate directly with the native Flash Lite player in the phone, classes toplay Flash Lite content in a Java Canvas object, and classes for data transfer.

    The Flash file (.swf) shown in the picture above is included in the MIDlet jar and is started from the JavaMIDlet via a call to the Project Capuchin API which then invokes the Flash Lite content.

    Data transfer interfaces and corresponding classes and function calls should be defined before startingdevelopment of the actual Flash Lite and Java applications.

    There are two types of data transfer methods:

    DataRequest. Data requests where the MIDlet handles a request of data from the Flash Liteapplication, and returns data asynchronously. To handle the request, the MIDlet may use any of theJSRs available on the Java Platform in the phone.

    ExtendedEvent. Flash Lite content receives events triggered by the MIDlet.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    8/41

    Tutorial | Project Capuchin for Flash and Java developers

    8 October 2008

    Tutorial

    Project Capuchin development is illustrated by examples provided in the following applications:

    The Clock application gives an example of how to package a Flash Lite animation in a Java MIDlet.

    The Project Capuchin data transfer mechanisms are illustrated in two simple Hello world examples.

    The Tasklist example illustrates a MIDlet using a Flash Lite animation as user interface, presentingdata requested from a Java service layer.

    The Stepcounter example shows how a Flash Lite user interface communicates with the Javaframework via event handling.

    The Clock application

    This basic example illustrates how to create and display Flash Lite content inside a Java application. TheClock application was designed in Flash Lite but implemented as a Java MIDlet. The source files for thisproject can be found in the tutorial_code.zip file, included with the download package of this document.

    The Flash Lite part

    The Flash Lite project file, referred to as the .fla file, is created in Adobe Creative Suite 3 (CS3). It iscompiled into a Shockwave Flash file, .swf. To create a .swf file that is compatible with the phone, the .flafile content is exported/compiled to a Flash Lite application.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    9/41

    Tutorial | Project Capuchin for Flash and Java developers

    9 October 2008

    To create a new .fla file in Flash CS3, select File New from the menu. Then select the type Flash File(Mobile) and click OK.

    Select the targeted phone profile from the Adobe Device Central menu. A profile can be chosen eitherfrom the brand names in the list found on the left side or from the list of generic devices.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    10/41

    Tutorial | Project Capuchin for Flash and Java developers

    10 October 2008

    Use the dropdown lists in the New Documenttab to select Flash Player,ActionScript Version and ContentType. The appropriate Content Type for most configurations is "Standalone Player".

    Click the Create button to open up a new project space. This is the stage area where the Flash Litecontent such us movie clips, ActionScript, layers, bitmaps, and so on, can be placed.

    Note: In the stage properties inspector, the background colour can be modified. This will be the colour ofthe canvas when the Project Capuchin application runs on the mobile phone. Also note that the frame rateper second can be specified here. This setting affects how fast the animation will run.

    The Flash file in this section is for an analogue clock indicating current time and date. The .fla file consists

    of four movie clips:

    the background the hour hand the minute hand the second hand.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    11/41

    Tutorial | Project Capuchin for Flash and Java developers

    11 October 2008

    The background is a bitmap image showing the clock frame with a circular scale of 12 hours. There is alsosome ActionScript affecting the rotation of the clock hands using values and calculations in the Flash Liteplayer through fscommand2().

    To compile the Flash Lite content and publish it as a .swf file, select File Publish Settings from the menu.

    To avoid creating html and JavaScript files needed for web publishing and save some compiling time,select the Formats tab, and uncheck the HTML checkbox.

    To compile the Flash file and export it as a .swf file, select the Flash tab and verify that the Flash Lite andActionScript versions are set properly. All Sony Ericsson phones supporting Project Capuchin arecompatible with Flash Lite 2.1 and ActionScript 2.0.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    12/41

    Tutorial | Project Capuchin for Flash and Java developers

    12 October 2008

    The remaining options are not essential for this example to work. The Script time limitsetting affects theframerate. TheJPEG qualitysetting should be optimised properly selecting higher values gives higherimage quality but may have a negative impact on performance. .

    Click the Publish button to compile and export the project as a .swf file. This is the file needed for theremaining part of the Project Capuchin application to be completed in Eclipse.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    13/41

    Tutorial | Project Capuchin for Flash and Java developers

    13 October 2008

    The Java part

    The steps in Java are:

    First some classes from the MIDlet, lcdui and capuchin packages must be imported.import java.io.InputStream;import javax.microedition.lcdui.Display;import javax.microedition.midlet.MIDlet;import com.sonyericsson.capuchin.FlashImage;import com.sonyericsson.capuchin.FlashPlayer;import com.sonyericsson.capuchin.FlashCanvas;

    Create a FlashPlayer, FlashImage, and FlashCanvas.private FlashPlayer flashPlayer;private FlashImage flashImage;private FlashCanvas flashCanvas;

    Set the FlashCanvas to full screen mode.

    Use Display.getDisplay(this).setCurrent(flashCanvas) to show the FlashCanvas.

    Like all MIDP applications, the ClockExample class must extend the MIDlet class.

    FlashImage is created by providing an InputStream as an argument to the FlashImage factorymethod. The Flash Lite content is embedded in the MIDlet suite (.jar) and is read as a resource. Thesecond argument, ExternalResourceHandler , in the factory method is set to null because we use thedefault resource handler provided by the framework.

    The ClockExample constructor looks like:

    public ClockExample() {try {

    InputStream is = getClass().getResourceAsStream("/Clock.swf");flashImage = FlashImage.createImage(is,null);flashCanvas = new FlashCanvas(flashImage);flashPlayer = FlashPlayer.createFlashPlayer(flashImage,flashCanvas);flashCanvas.setFullScreenMode(true);

    } catch (Exception e) {e.printStackTrace();

    }}

    The file Clock.swf file is added as a resource to the project.

    When the MIDlet is started the first time, or when the MIDlet resumes from a paused state, thestartApp() method is called by the program manager and the Flash Lite content is displayed:

    protected void startApp() {display = Display.getDisplay (this);if (flashPlayer != null) {

    display.setCurrent(flashCanvas);};};

    }

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    14/41

    Tutorial | Project Capuchin for Flash and Java developers

    14 October 2008

    SWF2JAR

    SWF2JAR is a tool for automatically embedding a Flash Lite file in a .jar suite. It can be downloaded fromhttps://developer.sonyericsson.com/getDocument.do?docId=100372.

    This application can only handle Flash Lite files that do not use Project Capuchin data transfermechanisms. For example, the Flash Lite file created in the Clock example above, does not use datatransfer and could be packaged with SWF2JAR.

    If data transfer between Flash Lite and Java is used, as in the examples below, then SWF2JAR cannot beused, and the necessary code must be developed by the Java programmers.

    Data transfer in Project Capuchinapplications

    One of the most important features in Project Capuchin applications is the data transfer between Java andFlash Lite. Project Capuchin API provides two data transfer mechanisms:

    DataRequest is used for data transfer between the Java and Flash Lite layers. Data can betransferred in both directions.

    ExtendedEvents is used by Java to notify Flash event listeners when events occur.

    Both mechanisms can be implemented in the Flash content using ActionScript.

    The following two "Hello world" examples show how to use the two mechanisms.

    https://developer.sonyericsson.com/getDocument.do?docId=100372https://developer.sonyericsson.com/getDocument.do?docId=100372
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    15/41

    Tutorial | Project Capuchin for Flash and Java developers

    15 October 2008

    DataRequest example

    DataRequest is used for asynchronous communication when Flash content requests data from the Javaside.

    DataRequest in FlashADataRequest object is created in ActionScript:

    var myDataRequest = new DataRequest("greeting");

    The argument(s) passed in the constructor is available for Java later (this method can be used to pass theDataRequest identifier from Flash to Java). When passing more than one argument, commas are used toseparate them:

    var myDataRequest =new DataRequest("greeting","from","Flash");

    This DataRequest object has a function attached to it called request(). The function is used when therequest is sent to Java:

    myDataRequest.request();

    After sending the request, the onLoad() function handler is executed when the request for data iscompleted by Java.

    This mechanism is asynchronous and allows Flash content to remain responsive while data requests areserved.

    myDataRequest.onLoad = function(success:Boolean){myMovieClip.textBox = myDataRequest.greetingText;};

    As mentioned above the DataRequest is then initiated by myDataRequest.request() .

    greetingText is a property of the DataRequest object set by Java.

    The values do not need to be defined in ActionScript, their names and values will be set in Java. However,Java programmers and Flash Lite designers have to agree about the names and expected values.

    Note:DataRequest properties can be string or number types.

    DataRequest in JavaIn Java, the following classes from the Project Capuchin API are used:

    com.sonyericsson.capuchin.FlashDataRequest;com.sonyericsson.capuchin.FlashDataRequestListener;

    The MIDlet implements the FlashDataRequestListener interface and uses the FlashDataRequest object to set the properties and their corresponding values.

    class HelloWorldMidlet extends MIDlet implements FlashDataRequestListener {

    flashImage.setFlashDataRequestListener(this);

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    16/41

    Tutorial | Project Capuchin for Flash and Java developers

    16 October 2008

    The method requestedData() is called from Flash while setProperty() is used to return therequested data. The complete() method is called afterwards to finish the request and send the databack to Flash.

    public synchronized void dataRequested(FlashDataRequest dr) {String[] is = dr.getArgs();if("greeting".equals(is[0])){

    dr.setProperty("greetingText", "Hello World!");dr.complete();

    }}

    ExtendedEvents example

    The idea behind ExtendedEvents is to define a listener object in Flash and register this to an event inJava. When the event is triggered in Java, Flash is notified and data is passed from Java to Flash.

    ExtendedEvent in FlashAn object is defined in ActionScript. This object is then registered as a FlashEventListener .

    var myEventListener:Object = new Object();ExtendedEvents.CapuchinEvent.addListener(myEventListener);

    The name of the event, CapuchinEvent, can be replaced by a name of your choice.

    myEventListener has a function handler called onEvent, this function is called by Java when the eventis triggered.

    myEventListener.onEvent = function(receivedText){myMovieClip.textBox = receivedText;

    }

    With DataRequest, data was passed as properties of the object, but when using ExtendedEvents theFlash designer and Java developer must agree on the number of parameters in the function handler. Inthis example one string, receivedText, is used.

    ExtendedEvent in JavaIn Java, the following classes from the Project Capuchin API are used:

    com.sonyericsson.capuchin.FlashEventManager;com.sonyericsson.capuchin.FlashEventListener;

    The MIDlet implements FlashEventManager and registers itself in the FlashImage.

    class HelloWorldEventMidlet extends MIDlet implements FlashEventManager{

    FlashImage myFlashImage;

    FlashEventListener myFlashEventListener;

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    17/41

    Tutorial | Project Capuchin for Flash and Java developers

    17 October 2008

    // constructorHelloWorldEventMidlet(){

    myFlashImage.setFlashEventManager(this);}

    FlashImageListener implementation requires the method addFlashEventListener . This method iscalled when an event is defined in Flash.

    public synchronized void addFlashEventListener (String name, FlashEventListenerfe){

    if ("CapuchinEvent".equals(name)){//Save FlashEventListener handlemyFlashEventListener = fe;

    }}

    Data is then sent to Flash using the handleEvent method in FlashEventListener . ThehandleEventmethod takes a String[] as an argument.

    String[] str = new String[]{"Hello World again!"};myFlashEventListener.handleEvent(str);

    Then myEventListener.onEvent will be called in Flash.

    Development

    There are six fundamental steps when developing Capuchin projects.

    1. Applications requirement and UI draft.Collect the application requirements and plan the UI.

    2. Decide data transfer mechanisms.Decide which of the two available data transfer mechanisms to use, DataRequest or ExtendedEvent, orboth.If Flash content needs to request information from the device, for example, as a result of userinteraction, DataRequest mechanism is the most suitable option.

    If Flash content should be notified when something happens in the phone, then the ExtendedEventsmechanism should be used.

    3. Format of data transfer.Developers should agree to the format in which data will be passed. It is possible to pass string andnumber data types. Agreement on the formats and how these can be identified must be reachedbetween the Java programmer and the Flash designer.

    4. Implement and test Flash UI.The Flash designer/developer implements and simulates the Flash UI separately from Java usingloadVariables()ActionScript function to read dummy data from an external file such us a text file.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    18/41

    Tutorial | Project Capuchin for Flash and Java developers

    18 October 2008

    5. Java implementation.The Java programmer writes the Java code using the Project Capuchin API and implements therequired services, for example, by using some JSRs.

    6. Test and release the application.

    The Java programmer tests the application in the device and releases it.

    Tasklist example

    Application requirement and UI draft

    This project is a redesign of the application "Tasks", usually shipped in the phone within the organiser.

    The tasks are presented in a list and an animation, displaying two moving gears, has been added. Forsimplicity only three of the tasks already stored in the phone are listed.

    Decide data transfer mechanisms

    The Flash content requests information when the application is started, then the task list is updated withdata from Java. This can be done using a DataRequest.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    19/41

    Tutorial | Project Capuchin for Flash and Java developers

    19 October 2008

    Format of data transfer

    The Flash designer and the Java programmer agreed to have a DataRequest object with four properties,three strings representing each task (MyTask1 to 3) and one number that is representing the total number

    of tasks to be displayed (Rows).

    myDataRequest.MyTask1myDataRequest.MyTask2myDataRequest.MyTask3myDataRequest.Rows

    The above is not part of the code, it is just a way to describe the properties of the DataRequest object, soboth Java programmer and Flash designer know them.

    Implement and test Flash UI

    Here we follow the same pattern as in the data transfer example above, see Data transfer in ProjectCapuchin applications on page 14. It is useful to define a function handler that can be called from bothsimulation code and the Java part.

    We define our function as follows:

    function onTaskUpdate(){for (var i=1; i

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    20/41

    Tutorial | Project Capuchin for Flash and Java developers

    20 October 2008

    var myDataRequest:Object = new DataRequest("Tasks");myDataRequest.onLoad = function (success:Boolean){if(success){onTaskUpdate();

    }

    };myDataRequest.request();

    Note: When using the real DataRequest to connect with Java, the DataRequest class must be included inthe project. The class is intrinsic and is defined in the DataRequest.as file. When using this mechanism,this file should be copied to the project folder. The files are included in the Project Capuchin API downloadpackage, found at http://developer.sonyericsson.com/.

    Java implementation

    The task of the Java programmer is to implement the Java code behind the application.

    This project will be a MIDlet, and the MIDlet class will implement the FlashDataRequestListener interface for receiving FlashDataRequest .

    public class FlashTaskMIDlet extends MIDlet implements FlashDataRequestListener{

    The code for data transfer mechanism is implemented within the method dataRequested().

    public synchronized void dataRequested (FlashDataRequest dr){};

    The requested data is set using setProperty().

    dr.setProperty("MyTask" + new Integer(step + 1).toString(),t.getString(ToDo.SUMMARY, PIMItem.ATTR_NONE));

    JSR-75 is used for accessing PIM information in the phone. The JSR-75 specification is available athttp://jcp.org/aboutJava/communityprocess/final/jsr075/.

    Finally the request is completed with a call to complete().

    dr.complete();

    The complete code for the Java implementation can be found under CapuchinTask/Java in thetutorial_code.zip file, included with the download package of this document.

    This code includes two classes, the file FlashTaskMIDlet.java contains the MIDlet class that initialisesFlash and implements the FlashDataRequestListener and the file ToDoManager.java defines a classthat uses JSR-75 for reading the tasks information and setting the DataRequest properties.

    http://developer.sonyericsson.com/http://jcp.org/aboutJava/communityprocess/final/jsr075/http://developer.sonyericsson.com/http://jcp.org/aboutJava/communityprocess/final/jsr075/
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    21/41

    Tutorial | Project Capuchin for Flash and Java developers

    21 October 2008

    Testing and releasing the application

    The Java developer can test the application like any MIDLet using Eclipse or building a MIDlet suite (.jar)and running it in the phone.

    StepCounter example

    In his project a step counter is implemented using the JSR-256 API to retrieve accelerometer values. TheJSR-256 specification can be found at http://jcp.org/en/jsr/detail?id=256.

    Application requirement and UI draft

    The application UI includes a text box presenting the current step count, a soft key for resetting the stepcount and a graph displaying the accelerometer fluctuation.

    The phone movement has to be above a certain threshold to be counted as a step, the graph howeverdisplays every phone movement.

    http://jcp.org/en/jsr/detail?id=256http://jcp.org/en/jsr/detail?id=256
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    22/41

    Tutorial | Project Capuchin for Flash and Java developers

    22 October 2008

    Deciding data transfer mechanism

    The nature of the application is event driven, because we do not want to keep asking for theaccelerometer values using DataRequests, and instead we want to receive notifications from Java

    whenever the phone moves.

    Format of data transfer

    The accelerometer provides a set of data as X, Y and Z values. From this data a calculation can be madeto see if the movement should count as a step or not.

    The graph will fluctuate based on the unit vector of the movement. For optimisation purposes, it is betterto perform these calculations in the Java code and pass the result to Flash if the movement should be

    counted as a step or not.

    Implementing and testing the Flash UI

    In ActionScript an object is created and used as an EventListener. We chose to name it CapuchinEventbut the name is optional.

    var myEventListener = new Object();ExtendedEvents.CapuchinEvent.addListener(myEventListener);

    The function to be called upon receiving an event is defined.

    myEventListener.onEvent = function (Step,Length){

    var stp:Number = parseInt(Step,10);len = parseInt(Length,10);if(Step!=NaN && Length!=NaN && Step!=undefined && Length !=undefined){

    ready = true;if(Step == 1){

    nrSteps++;

    steps3.text = nrSteps;}

    }}

    The received data is validated and the variables within Flash are set according to these values. Thesevalues are then used to draw the graph.

    It is good to test and verify this application using dummy values before using the real event.

    The complete .fla file can be found in the project file under StepCounter/Flash in the tutorial_code.zipfile, included with the download package of this document.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    23/41

    Tutorial | Project Capuchin for Flash and Java developers

    23 October 2008

    Java implementation

    The following classes are required:

    import com.sonyericsson.capuchin.FlashEventManager;import com.sonyericsson.capuchin.FlashEventListener;

    The StepCounter class needs to implement FlashEventManager.

    Public class StepCounter extends MIDlet implementsFlashEventManager,DataListener{ }

    public StepCounter(){try {

    InputStream is = getClass().getResourceAsStream("/stepCounter_yg.swf ");flashImage = FlashImage.createImage(is,null);flashImage.setFlashEventManager(this);flashCanvas = new FlashCanvas(flashImage);flashPlayer = FlashPlayer.createFlashPlayer(flashImage,flashCanvas);flashCanvas.setFullScreenMode(true);

    } catch (Exception e) {e.printStackTrace();

    }}

    public void addFlashEventListener(String name, FlashEventListener fe) {

    if("CapuchinEvent".equals(name)){

    eventListener = fe;..

    }}

    The accelerometer values can be received by implementing dataReceived() defined in JSR-256 . Themethod is called upon events from the accelerometer. When the accelerometer data are received thenecessary calculations for unit vector and step are performed and the results are passed to Flash.

    public void dataReceived(SensorConnection sensor, Data[] data, booleanisDataLost){eventListener.handleEvent(types);}

    For optimisation purposes the step value is calculated in Java. Then this is passed as a string to Flashrepresenting if the movement should count as a step or not. The complete Java file can be found underStepCounter/Java in the tutorial_code.zip file, included with the download package of this document.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    24/41

    Tutorial | Project Capuchin for Flash and Java developers

    24 October 2008

    Testing and releasing the application

    The Flash part can be tested separately using dummy values.

    After testing, the final application is deployed as a .jar file and installed on the phone.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    25/41

    Tutorial | Project Capuchin for Flash and Java developers

    25 October 2008

    FAQ

    Question: What is the performance of Project Capuchin applications?

    Answer: In terms of performance, Project Capuchin renders Flash Lite content in the same way as aphone does with Flash Lite content running in the native browser. However, using Java for calculationsand heavy logic processing optimises the performance compared to native Flash Lite applications.

    When optimising a Project Capuchin application the following points are important to consider:

    1. The amount of data and the number of created DataRequest and ExtendedEvent constructions shouldbe kept to the minimum.

    If you, for example, have to update 100 notes, do not use one DataRequest for each note. This creates

    100 DataRequest objects in Flash Lite and consumes a lot of memory. On the other hand, oneDataRequest with 100 notes is also ineffective, since it leads to a large overhead if the notes are notgoing to be presented at the same time.

    2. Java optimises calculations more efficiently, why object creation and heavy calculation should be donein Java as much as possible.

    3. Flash Lite has a 2 MB dynamic memory limit for playing Flash Lite content, so it is very important tooptimise the Flash Lite content. The Flash Lite 2.1 Developers guidelines, found athttp://developer.sonyericsson.com gives some general optimisation tips for Flash Lite developers.

    Question: Is Flash Lite transparent stage supported by Project Capuchin?

    Answer: No, due to limitations in Flash Lite, there is currently no support for transparency in ProjectCapuchin applications.

    Question: Which image formats are supported by Flash Lite?

    Answer: Flash Lite supports .jpg, .png, .bmp and .gif images.

    Question: How do you display server-generated or downloaded images in Project Capuchin?

    Answer: Project Capuchin API provides access to images as external files (file:///), Internet resources(http://) and MIDlet resources (res://). Paths to these resources have to be absolute.

    In Flash Lite, use loadMovie:

    loadMovie("file:///c:/pictures/mysample.jpg",_root);

    When accessing external resources, Java security mechanisms apply.

    Question: Is it possible to combine Flash Lite and Java (lcdui) for building the UI?

    Answer: Yes, but avoid using the FlashPlayer class and instead use the FlashImage class directly.Call the render() method with the graphics from your lcdui component. This way you can use, forexample, a Form for displaying Flash Lite and combine it with Java lcdui components.

    http://developer.sonyericsson.com/http://developer.sonyericsson.com/
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    26/41

    Tutorial | Project Capuchin for Flash and Java developers

    26 October 2008

    Question: Can I render text in different languages with Project Capuchin?

    Answer: Yes.

    Question: Can I rotate text in Flash Lite with Project Capuchin?

    Answer: Yes. Put the text in a Flash MovieClip, then rotate it, just make sure that you embed the fontsinside the Flash file.

    Question: Are soft keys supported by Project Capuchin?

    Answer: Project Capuchin does not support setting the soft key labels from inside Flash Lite (usingActionScript), instead these can be set by Java.

    Question: Is there a standard or a guideline for defining the interface between Java and Flash content?Some functionality can be realised in Flash Lite content and some in Java, how do I judge and find whichway is better?

    Answer: There are really no guidelines, it is up to the Java programmer and the Flash Lite designer todefine an interface based on DataRequest and ExtendEvents objects.

    The Flash Lite designer specifies which information need to be updated in the UI, and then together withJava developer decide on the DataRequest properties or the parameters for ExtendEvents. MoreoverFlash Lite content optimisation has to be taken into consideration.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    27/41

    Tutorial | Project Capuchin for Flash and Java developers

    27 October 2008

    Appendix

    This appendix contains a step by step description of how to set up an environment for developing ProjectCapuchin applications.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    28/41

    Tutorial | Project Capuchin for Flash and Java developers

    28 October 2008

    Setting up the Java environment forProject Capuchin

    The Java environment described in this section consists of:

    Java Runtime Environment (JRE) 1.6 or later. JRE is available at http://www.java.com/en/download/manual.jsp. Download and install it according to the provided instructions.

    Sony Ericsson SDK for the Java ME platform. Sony Ericsson SDK for the Java ME Platform, found atSony Ericsson website at http://developer.sonyericsson.com/site/global/docstools/java/p_java.jsp.Follow the provided installation instructions.

    Eclipse. Download Eclipse SDE, available for download at http://www.eclipse.org/downloads/.

    EclipseME plugin. Can be installed by using the EclipseME update site,http://eclipseme.org/docs/installation.html.

    Project Capuchin Classes. The Project Capuchin API is available for download athttp://developer.sonyericsson.com.

    http://www.java.com/en/download/manual.jsphttp://www.java.com/en/download/manual.jsphttp://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://www.eclipse.org/downloads/http://eclipseme.org/docs/installation.htmlhttp://developer.sonyericsson.com/http://www.eclipse.org/downloads/http://eclipseme.org/docs/installation.htmlhttp://developer.sonyericsson.com/http://developer.sonyericsson.com/site/global/docstools/java/p_java.jsphttp://www.java.com/en/download/manual.jsphttp://www.java.com/en/download/manual.jsp
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    29/41

    Tutorial | Project Capuchin for Flash and Java developers

    29 October 2008

    Installing the EclipseME plugin

    1. Start Eclipse and select Help Software Updates Find and Installfrom the menu.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    30/41

    Tutorial | Project Capuchin for Flash and Java developers

    30 October 2008

    2. Select the Search for new features to installoption and click Next.

    3. All available update sites are listed. The EclipseME plugin is not in the list, so to be able to install it,click the New Remote Site... button.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    31/41

    Tutorial | Project Capuchin for Flash and Java developers

    31 October 2008

    4. Enter the following values in the text boxes:Name: EclipseME Update SiteURL: http://www.eclipseme.org/updates/

    5. Select EclipseME Update Site in the list and click the Finish button.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    32/41

    Tutorial | Project Capuchin for Flash and Java developers

    32 October 2008

    6. Eclipse verifies if any updates are available.

    7. If an update is available, Eclipse prompts for installing it. Select Yes and wait until the installation iscompleted. Then restart Eclipse.

    Setting up the Sony Ericsson SDK with EclipseEnvironment.

    1. After installing the Sony Ericsson SDK for the Java ME Platform, select Window Preferences from theEclipse menu.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    33/41

    Tutorial | Project Capuchin for Flash and Java developers

    33 October 2008

    2. Go toJ2MEthen select Device Managementin the tree structure to the left. Click the Importbutton.

    3. Click the Browse button.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    34/41

    Tutorial | Project Capuchin for Flash and Java developers

    34 October 2008

    4. Select the folder where the Sony Ericsson SDK is installed.

    5. After selecting the devices directory click the Refresh button.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    35/41

    Tutorial | Project Capuchin for Flash and Java developers

    35 October 2008

    6. A list of all available phone emulators is shown. Select a phone model, for example,"SonyEricsson_JP8_240x320_Emu".]

    Now the environment will run on the SonyEricsson Java Platform JP-8.0 emulator with a screen size of240x320 pixels.

    Installing the Project Capuchin basic APIs

    Project Capuchin basic APIs are needed when developing applications using Flash Lite and Java, and canbe found at http://developer.sonyericsson.com. These API classes will be included in future releases ofthe Sony Ericsson Java ME SDK. Currently the classes must be manually included in the project buildpath.

    http://developer.sonyericsson.com/http://developer.sonyericsson.com/
  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    36/41

    Tutorial | Project Capuchin for Flash and Java developers

    36 October 2008

    1. Create an Eclipse project. Select File New Otherfrom the Eclipse menu.

    2. Select J2ME Midlet Suite in the New dialog and click Next.

    3. Type the project name, and click Finish.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    37/41

    Tutorial | Project Capuchin for Flash and Java developers

    37 October 2008

    4. The Eclipse project is now ready to import the Project Capuchin APIs. Select File Import... from themenu.

    5. Select File System in the Generalfolder and click Next.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    38/41

    Tutorial | Project Capuchin for Flash and Java developers

    38 October 2008

    6. Unpack the zip file containing the Project Capuchin classes. In the Eclipse Import dialog, click theBrowse button and navigate to the folder where the Project Capuchin classes were unpacked. ClickFinish to import the classes.

    The Project Capuchin API classes have now been added to the project and should be included in theproject build path:

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    39/41

    Tutorial | Project Capuchin for Flash and Java developers

    39 October 2008

    7. In Eclipse, right click your project and select Properties in the pop up. In the Properties windowselectJava Build Path in the left panel, and select the Libraries tab.Click theAdd Class Folderbutton.

    8. Select the "com" folder under "Capuchin" and click the OKbutton.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    40/41

    Tutorial | Project Capuchin for Flash and Java developers

    40 October 2008

    9. The Project Capuchin library should now be found in the "Referenced Libraries/com" folder. Make surethat the name of the subfolder is com.sonyericsson.capuchin.

    10. If the name of the library is other than com.sonyericsson.capuchin, rename it by right clicking on thelibrary and select Refactor Rename... in the popup menu.

  • 7/29/2019 Tutorial Capuchin Flash Java Developers r2a

    41/41

    Tutorial | Project Capuchin for Flash and Java developers

    11. Enter the correct name, com.sonyericsson.capuchin and click OK.

    12. The subfolder now has the correct name.

    The Eclipse setup is complete and application development can start.

    Note: The current version of the Sony Ericsson SDK does not support running Capuchin applications inthe emulator, but developers may debug and test applications on device. For instructions on how to runon device debugging using USB, see https://developer.sonyericsson.com/docs/DOC-1734.

    https://developer.sonyericsson.com/docs/DOC-1734https://developer.sonyericsson.com/docs/DOC-1734