PlayBook QNXApplication SWIPE_DOWN in a multi platform SWF

Posted on March 8, 2011 | 5 comments

A question came up from a developer that was creating a Flex Hero (the 4+ Flex SDK geared towards mobile) mobile application about tapping into the PlayBook’s bezel swipe down event, but also be able to use the SWF on other platforms. PlayBook is a great platform that QNX, a subsidary of RIM, has created PlayBook specific AIR apis and classes.

One of the PlayBook specific API’s is the ability to tap into the bezel event when the user swipes from the top bezel into the tablet screen. As a developer you catch this specific PlayBook event with the following code:

QNXApplication.qnxApplication.addEventListener(QNXApplicationEvent.SWIPE_DOWN, myFunction);

The problem on non-PlayBook platforms is they do not implement the classes that talk from QNXApplication to the OS to handle this PlayBook specific event. If you run this code on a non-PlayBook platform you will have a VerifyError that talks about not implementing qnx.pps.PPSChannel, i have a blog post about how to workaround this for UI testing while on the desktop.

In the use case of this blog post we actually want the functionality of handling the swipe down event to work on the PlayBook and then not throw a runtime VerifyError on non-PlayBook platforms, basically one SWF that will work across platforms.

Using QNX UI components in a Flex application requires some specialized classes, read more on that topic here. The QNXApplication class and listening for the swipe down event is not trying to mix UI components but just listening for an event. This becomes much easier to integrate this functionality into the Flex application. The example below is in ActionScript but you can just put this in the respective script area of your Flex application.

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.utils.getDefinitionByName;
   
    import qnx.system.QNXApplication;
   
    public class TestCC extends Sprite
    {
        public function TestCC()
        {
            super();
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
           
            try
            {
                var c:Class = flash.utils.getDefinitionByName("qnx.system.QNXApplication") as Class;
                var q:QNXApplication;
                c.qnxApplication.addEventListener("swipeDown", swipeDown);
            }
            catch(error:Error)
            {
                graphics.beginFill(0xFF3333, 1);
                graphics.drawRect(50, 50, 200, 200);
                graphics.endFill();
            }
        }
       
        protected function swipeDown(event:Event):void
        {
            graphics.beginFill(0x3333FF, 1);
            graphics.drawRect(50, 50, 200, 200);
            graphics.endFill();
        }
    }
}

What the code is doing is checking if the QNXApplication class is available and then if it is not available goes to the catch code block and does not try and call QNXApplication.qnxApplication singleton, which throws the VerifyError. You will notice the instance declaration, but no instantiation, of var q:QNXApplication;, this is needed to make sure a class reference is compiled into the SWF to test for at runtime. When PlayBook is present the class comes back fine because the qnx-air.swc (extended AIR apis) is present, but on non-PlayBook platforms the classes in qnx-air.swc that QNXApplication (which we compiled into the SWF) are not present so it throws an error on the getDefinitionByName() call.

Give it a try and let me know if it works for you.

The other option is to use conditional compile config statements. Some people don’t prefer this way because it complicates building different SWFs for each platform, but its a very viable option.

  • Pingback: Develop in a multi platform world with FDT « Patrick Kulling

  • http://www.flashinyourface.com chris

    Hi. I am the user group manager for Flash Dallas. Some people have been trying to wrap up their playbook apps in time to get them submitted to get a device. I am one of those people. Some of us have made our apps entirely in Flash Pro and are compiling through the command line. This works great until we try to add anything QNX related. I was really hoping to get the swipe down working, but it won’t compile once I add the QNX code. Any thoughts? Is there anything special you need to do to get QNX to work with Flash Pro. I tested it in Flash Builder and it worked fine. Both programs are pulling from the same SDKs.

    Thanks,
    Chris

    • http://www.renaun.com Renaun Erickson

      What does you import of the SWC’s look like?
      http://renaun.com/blog/2010/11/building-blackberry-playbook-apps-with-flash-professional-cs5/
      In the link above pay attention to which ones are set to extern and which ones are set to merge.
      It will error on the desktop so you will have to test only on the PlayBook.

      • http://www.flashinyourface.com chris

        Thanks. I was merging the AIR into the code. It’s no longer throwing any errors when I compile. I still haven’t gotten the QNX code to work, but at least it is compiling so I should be able to figure it out Thanks!

        One more question. I’ve reached out to forums and to RIM but I haven’t gotten a clear answer. Do you have any idea if navigateToURL will work on the actual device? The simulator definitely doesn’t like it.

  • http://shawnblais.com Shawn

    A simpler way of doing this that I’ve found, is this:

    if(Capabilities.os.indexOf(“PlayBook”) > -1){
    addSwipeListener();
    }

    protected function addSwipeListener():void{ QNXApplication.qnxApplication.addEventListener(QNXApplicationEvent.SWIPE_DOWN, onSwipeDown);
    }

    By encapsulating it in a function, you can safely include it in your class, only calling the function if you’re actually on the device.

  • http://us.blackberry.com/playbook-tablet/ Tablet

    Thank you for the info!