Tuesday, May 18, 2010

LWUIT BlackBerry ANT script refactored

My last article about using ANT script to build LWUIT blackberry application was intereted by some people, which is encouraging. I spent some time to review the ant script again and made some improvements. Here I posted my updated build script, it is cleaner and easier to use, you can just use it as template, copy and paste it in your NetBeans project by just configure some parameters.

<property name="app.title" value="helloLWUIT" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

    <target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">
              </isset>
         </condition>
    </target>

  
   <target name="post-jar" if="do.rapc">
     <antcall target="bbbuild" />
   </target>
  

<target name="post-clean">
   <delete failonerror="false">
      <fileset dir="${platform.home}/simulator">
       <include name="**/${name}.*">
       </include>
      </fileset>
   </delete>
</target>

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init">
          <echo message="rapc build, dir=${dist.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >
          </jdp>
          </rapc>

        <!-- sigtool jdehome="C:Program FilesResearch In MotionBlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="${app.title}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
        </fileset>
       </jadtool>
 </target>

<target name="copyBBSimulator" depends="bbbuild" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
            <fileset dir="${dist.dir}/bbant">
                  <include name="*.cod"/>
                  <include name="*.jad" />
            </fileset>
    </copy>
</target>

<target name="bb-run" depends="copyBBSimulator">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

It has following improvements:
    1. Separate out the customized parameters in the build scriciptThen entire build script only needs following customized parameters: app.title, app.version, app.icon, app.vendor. You only need to configure them with your own application parameters.
    2. Simplify the build script.  In the old build script, you have to use target "copyBBTouchSources" and "copyBBSources" to copy and update the LWUIT library source code into specific folder, then call rapc compiler to build the entire application. But when you investigate a little bit deeper, since NetBeans IDE already generate jar file for you, which already included the preprocessed application and the LWUIT blackbery library class files, why not just pass this jar file to rapc and then generate the cod file? based on this idea, I modified the target "bbbuild" and it works.
    3. One build script supports both BlackBerry touch and non-touch build. since the build script is simplified and only use jar file to generate cod file,the touch or non-touch specific files are already embedded in the jar file, the build script itself becomes generic. To tested it, I installed JDE 4.5 and JDE 4.7 two different JDE in the NetBeans, and set up 4.5 and 4.7 different configurations in my project, and I found both works.
    4. Added the ant target "bb-run", which allow you run the application in the blackberry simulator. To make it work, you had better to check the nbproject\project.properties file, if the simualtor device name is configured properly, In JDE 4.7, the device name is 9500, like this:




      configs.BlackBerryTouch.platform.device=9500
      In 4.5, the device name is 8320, like this:




      configs.BlackBerry.platform.device=8320
    Update: Sample project
    Please click here to download the sample project.

    Tuesday, May 4, 2010

    Why should managers embrace agile?

    When you look at the articles about Agile/Scrum/Lean, they are all about self-organizing, decentralized, democracy, etc.Those are welcomed by the developers but conflict with managers benefit, because our conventional management is about hierarchy, command and control, chasing the power, the manager cares more about how his boss think instead of how his subordinates think. For managers, embracing agile means loosing power, weaken their benefit, that is why manager don't like agile.
    On the other hand, agile is about changing, changing means risky, but the managers care more about the stability of their position, their power, they don't have much enthusiasm to make changes.

    So we have a conclusion that developer and manager like two different classes, developers like agile because it brings them benefits; managers dislike agile because it will lose managers' benefit. The progress of agile movement is quite similar with the progress of human's democracy movement.

    This sounds discouraging, but this is the fact, that will explain why agile has not become mainstream after 10 years of its movement.Then what can we do? Try to educate as many developers as possible, gradually if agile principles are accepted by most people, then it is quite possible to force the managers make changes. This will take time, but please remember that the manager won't truely embrace agile,just like no ruler will truely embrace democracy.

    Saturday, May 1, 2010

    ANT build script for LWUIT blackberry application

    Update:
      This article is more than a year old, now LWUIT project uses a new tool to build BlackBerry application - NetBeans BlackBerry Plugin, please refer my two latest post:

      Developing Blackberry application using NetBeans Plugin - part 1, first impression
      Developing Blackberry application using NetBeans Plugin - part 2, fixing debug issue


    Shai's article about build script for LWUIT in BlackBerry is great, but it has so many errors,I spend two days trying to fix the errors, and finally make it work.

    Here I posted my modified ant buil.xml and application source code. The application is very simple, just display a "Hello World" text. Since this article is based on Shai's article, for details such as project set up, configuration and tools installation etc, please check his article.

    application source code
    HelloLWUITMidlet.java

    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.layouts.BorderLayout;
    
    
    public class HelloLWUITMidlet extends
    //#ifdef RIM
         net.rim.device.api.ui.UiApplication
    //#else
    //#      javax.microedition.midlet.MIDlet
    //#endif
     {
        public void startApp() {
    
            Display.init(this);
            Form f = new Form();
            f.setTitle("Hello World");
            f.setLayout(new BorderLayout());
            f.addComponent("Center",
            new Label("I am a Label"));
            f.show();
    
        }
    //#ifdef BlackBerry
       public static void main(String[] argv)
       {
           new HelloLWUITMidlet().startApp();
       }
    
       public void notifyDestroyed()
       {
           System.exit(0);
       }
    
       public void platformRequest(String s) {
          net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
         }
    //#endif
    
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    }
    

    ANT build script
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- You may freely edit this file. See commented blocks below for -->
    <!-- some examples of how to customize the build. -->
    <!-- (If you delete it and reopen the project it will be recreated.) -->
    <project name="helloLWUIT" default="jar" basedir=".">
        <description>Builds, tests, and runs the project .</description>
        <import file="nbproject/build-impl.xml"/>
       
    
       
         <target name="post-init">
            <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
            <available file="${platform.home}/simulator/9500.bat" property="bbtouch" />
            <condition property="jpda.port" value="8000">
               <isset property="do.rapc">
               </isset>
            </condition>
         </target>
    
    
            
            <target name="post-preprocess" depends="copyBBSources,copyBBTouchSources">
            </target>
            
    
           <typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
           <target name="bbbuild" description="blackberry build" depends="init,copyBBSources,copyBBTouchSources">
              <echo message="Compiling ${preprocessed.dir}"></echo>
              <mkdir dir="${dist.dir}/bbant" />
              <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
              <src location="${basedir}/${preprocessed.dir}">
              </src>
              <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
              </jdp>
              </rapc>
              
            <!-- sigtool jdehome="C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
            <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="helloLWUIT">
           <codset>
            <fileset dir="${dist.dir}/bbant" includes="*.cod">
           </fileset>
           </codset>
           </application>
           </alx>
    
          
           <mkdir dir="${dist.dir}/bbant-final" />
           <jadtool description="desc" id="appId" input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
            <fileset dir="${dist.dir}/bbant" includes="*.cod">
            </fileset>
           </jadtool>
           
          </target>
    
     <target name="copyBBTouchSources" if="bbtouch">
     <echo message="Copying blackberry touch sources"></echo>
     <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/touch/preprocessed" dest="${basedir}/${preprocessed.dir}" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java" />
     <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java" />
     
     </target>
    
     <target name="copyBBSources" if="do.rapc">
       <echo message="Copying blackberry sources ${preprocessed.dir}"></echo>
       <copydir src="${project.LWUIT}/src" dest="${basedir}/${preprocessed.dir}" />
       <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/preprocessed" dest="${basedir}/${preprocessed.dir}" />
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java"/>
       <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java"/>
     </target>
    
     <target name="post-jar" if="do.rapc">
       <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${platform.home}/simulator/" noconvert="true">
         <src location="${basedir}/${preprocessed.dir}">
         </src>
         
         <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
         </jdp>
       </rapc>
     </target>
     
     <target name="post-clean">
        <delete failonerror="false">
           <fileset dir="${platform.home}/simulator">
            <include name="**/${name}.*">
            </include>
           </fileset>
        </delete>
     </target>
    
    </project>
    


    Note:
    This build script is for touch screen build, for non touch sreen build you need to
    1.Comment out "bbtouch" property in target "post-init" :
    <!-- <available file="${platform.home}/simulator/9500.bat" property="bbtouch">  --> </available>
    2.Remove the "copyBBTouchSources" dependency in target  "bbbuild".

    Wednesday, April 21, 2010

    Build LWUIT BlackBerry CLDC application

    Last week finally I have time to review the Shai's blog  about building LWUIT project on Blackberry platform. At first I tried to follow the step by step in his article, but I am  disappointed because of so many errors of his build.xml. After a couple hours fix, I still could not make the build, so I decided to give up Shai's script, and choose an easier way.
    My solution is just using NetBeans and BlackBerry JDE, it is much easier to build a demo.
    Here is the step by step description:

    Build LWUIT BlackBerry library
    1. Checkout the LWUIT source code from SVN, (https://lwuit.dev.java.net/svn/lwuit/trunk), the LWUIT source includes the NetBeans BlackBerry port project.
    2. Install JDE 4.7 and Configure BlackBerry support in NetBeans, you can follow this link to configure;
    3. Start NetBeans, open the "BlackBerryPort" poject under the "BlackBerry" sub folder of lwuit project source, there are two configuration for this project, the default is for non touch screen build, the "touch" configuration is for touch screen device like STORM.for non touch device, just choose DefaultConfiguration, build the project, you will see BlackberryPort.jad and BlackberryPort.jar in \dist folder, they are the LWUIT library for BlackBerry devices;

    Build LWUIT Demo into BlackBerry CLDC application
    1. Open Blackberry JDE, create a new workspace, called "lwuitdemo.jdw";
    2. add a new project under the workspace, name it "lwuit",  choose its Application type as "Library", import the BlackberryPort.jad and BlackBerryPort.jar file into the project. then build it, the JDE will generate lwuit.cod and update BlackBerryPort.jad file.  
    3. download LWUIT 1.3 from sun website, it comes with the famous LWUITDemo source code, open the LWUITDemo project using NetBeans, build it;
    4. add another new project in the JDE workspace, name it "lwuitdemo", choose application type as "CLDC Application";
       set up the correct value with title, version and Vendor information;
       next you need to add project dependency to "lwuit" project, right click project, choose "Project Dependencies..", choose "lwuit project";
       copy all the java source code from LWUITDemo src folder, add them into the project, ignore those png files and .res files;
       go to  build\preverified folder, copy all the png files and .res files, add them into your JDE project;
       right click icon.png, choose "use as application icon";
    5.Modify the UIDemoMIDlet.java
    Made following changes:
    make sure the UIDemoMIDlet extends from uiApplication, like this:
    public class UIDemoMIDlet extends net.rim.device.api.ui.UiApplication  {...}
    
    Add the following methods, copied from Shai's blog:
    public static void main(String[] argv)
        {
            new UIDemoMIDlet().startApp();
        }
     
        public void notifyDestroyed()
        {
            System.exit(0);
        }
     
        public void platformRequest(String s) {
           net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
        }
        
    In startApp() method, comment out the following statement, because non touch device need to disable the virtual keypad:
    // VKBImplementationFactory.init();
    
    6. Build the lwuitdemo project.
    7. Sign both lwuitdemo.cod and lwuit.cod fle, load them both into your blackberry device.
    8. If you want to provide OTA download, that is a litttle bit tricky:
       You need to download two artifacts, one for lwuit libray, the other for lwuitdemo, which means user need to download two links:
       1) lwuti library:
          BlackBerryPort.jad
          lwuit.cod
          lwuit-1.cod
          ...
       2) lwuitdemo application:
          lwuitdemo.jad
          lwuitdemo.cod
          lwuitdemo-1.cod
          ...


    You can download the whole source code of the JDE project from here.


    In future I would like to share:
      1. Provide an updated build.xml based on Shai's blog;
      2. provide how to build LWUITDemo MIDlet project for BlackBerry device.

    Thursday, April 8, 2010

    How to send email in BlackBerry Simulator

    Recently I am working on a BlackBerry Email app, I need to setup a test environment so I can test mail send in the BlackBerry simulator.
    I did some research, I read the RIM related document it does not help, then I find a solution from a good book about blackberry - Advanced BlackBerry Development, by Chris King.
    I just follow the method from the book , and finally get it working, the solution is quite straight forward.

    You need BlackBerry ESS ( Email Server Simulator) in order to simulate mail from your simulator, it comes with the JDE SDK and JDE component, currently I used JDK 4.5 , I prefer it because it's simulator is much faster to launch.


    There are two mode you can choose - Connected mode or Standalone mode.
    I tired the connected mode, it does not working.
    The book recommend using the Standalone mode:  ESS will listen the localhost port, send out email to your smtp email client. If you set up your email client, choose localhost as server address, and configure the same pop3 and smtp port as ESS configured, your email client will receive the email.
     Choose "Standandalone mode",  set Pop3 port and Smtp port,  I choose default value, which are 995 and 465.
    Click Launch, the ESS will start, and you will say a dos command window displaying the server log information.

    Then we need to configure your email client,  I choose OutLook Express.
    Add a new account,  you can set up any email address,
    Set localhost  for both SMTP and POP3 server.
    for POP3 port number ,use the same value of ESS: 465
    for SMTP port number, use the same value of ESS:995


    So far the configuration is finished, all you can do is launching the simulator.
    You can start the blackberry email client, type any address,  then send.  Check your OutLook Express, you will find an incoming email.
    And you can write an email from your outlook express, type any address, then send it.  You will find the email in your blackberry simulator.
    The only drawback is you can only send and receive email from your local machine. which is not real, but it is good enough to test the mail function in the simulator.
    It is really good, I tested over 100 email using the solution above.
    Finally I want to talk about a little about the book, it covers many good topics, such as BES, BIS which confuses me for a long time.  Even I haven't finished it, but I think it is one of the best  blackberry book so far.

    Monday, March 15, 2010

    My mistakes to the J2ME Location API

    Last week when I tried to test the location API for J2ME devices, I made big mistakes.
    The 2 devices I tested are: NOKIA N93 and BlackBerry 8320. Actually I don't have any experience for J2ME location API before.Before testing, I checked the spec of these two devices, they both support Location API - JSR 179, so I thought they both have GPS receiver,but when I tested several location applications on theses two devices and even simple demos, none of them works on either devices: N93 keep looking for Blue tooth devices, and then pop up Location exception or pop up timeout exception; while the applications in blackberry keep hanging no response.

    I spent the whole afternoon trying to make them works, but no luck. Finally I googled them again, then I found that these two devices do not have internal GPS receiver! They support location API means they need external blue tooth GPS receiver!!! Which explains why N93 keeps looking for blue tooth devices.

    So I learned that even if a device supports Location API does not means it has GPS, then it means there is no way to check a device supports GPS or not via programmatic way, because it is impossible to distinguish if a device has built-in GPS with a device without GPS.

    A solution for non GPS Blackberry devices
    Another reason I made the mistakes is because I saw the Google Map works perfectly in my 8320 devices. Now I understand it might use cell id to get the location information.This will give the developer another hint to enable the loation service to the non GPS blackberry phones.

    I found a open source project: OpenCellId, which maintain a cell id database, so you can query your location by providing your device's cell id.
    Here is the API.

    I checked the BlackBerry API, it has a GPRSInfo class, which provides the GPRSCellInfo class, GPRSCellInfo contains all the cell id information you have.
    You can use the following code snippet to get the cell id:
       import net.rim.device.api.system.GPRSInfo;
       ...
       int cellId = GPRSInfo.getCellInfo().getCellId();
    You can also get MNC, MCC, LAC, RAC etc.
    With these information, you can use OpenCellId to retrieve your location.

    This sounds interesting, I haven't try it yet, I am sure I will check it in future.

    Sunday, February 7, 2010

    Software Quality: internal vs. external

    Let's check following questions:
    1)How to define the quality of a software product?
    2)Is a bug free software a high quality software?
    3)Is software quality determined by QA testing?
    4)Can QA testing help improve the quality?

    The software quality can be divided into two parts: external and internal.

    External quality is explicit, transparent and easy for user to experience, easy to test, the bug can be easily found;
    Internal quality is implicit, it is hard to describe and visualize by the end user, but it is the true quality of the software, usually related clarity, flexibility, maintainability, readability etc, it is a gene of a software.
    The relationship between external and internal quality like a iceberg: external quality is above the water, while most of the stuff under the water are belong to internal quality.
    We say that Internal quality determines the external quality:
    If internal quality is good, then external quality are also good,
    If we found more external bugs, then there must be more internal bugs, we just haven't found them yet.

    Now we can answer question 1) and 2):
    To determine a software is high quality or not, the most important is how much it can add value to customer, which is based on its internal quality; if it does not bring much value, even if it is no bug, still it can not be treated as a high quality software, because it is useless.

    To answer question 3) and 4), Let's check another metaphor:
    A human being is healthy is determined by his internal physical condition,or his gene, not by the physical examination. The annual checkup will give you a feedback of your body. The testing itself does not improve your body condition.
    So the answer will be: the internal quality determine the software quality; the QA testing just give the feedback of the quality, itself can not improve the quality.

    We just pay too much attention on the external quality, but ignore the internal quality; if we want to truly improve the software quality, we need to pay more attention on the internal quality, pay attention on external quality is too late and not efficient.

    How to measure the internal quality? How to improve the transparency and visualize the internal quality?
    Static analysis tool can tell you how good your code is.

    How to improve the internal quality?
    Use iterative development process, encourage team reflection, keep constantly refactoring, code review, use design patterns, etc.