New AIR 1.5.2 API Examples and Features

August 3rd, 2009 by ryanstewart

Despite the ‘.2′ naming this is a pretty cool release for Adobe AIR developers and there are a few important new APIs that I think you’ll find very useful as well as a big change that should make every developer’s life a lot easier.

Getting Set Up
The first thing you need to do is download the new SDK and replace the old AIR 1.5 or 1.5.1 SDK. It’s pretty easy and it just consists of dropping the new AIR SDK folder into your Flex SDK (back it up first). The ditto command on the Mac works beautifully. You also need to make sure to change the namespace in your appname-app.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.5.2">

Catching the “ESC” Key in Full Screen Mode
Now you’re ready to go. One oft-requested feature for AIR (and Flash Player) is the ability to capture the “escape” key and not have it exit full screen mode. With AIR 1.5.2 this is easy. You set an event handler for the keyDown event and then inside of the event handler you call event.preventDefault(). Here’s a quick example:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 
	xmlns:fx="http://ns.adobe.com/mxml/2009" 
	xmlns:s="library://ns.adobe.com/flex/spark" 
	xmlns:mx="library://ns.adobe.com/flex/halo"
	keyDown="windowedapplication1_keyDownHandler(event)">
 
	<fx:Script>
		<![CDATA[
 
			protected function btn_fullScreen_clickHandler(event:MouseEvent):void
			{
				stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
				btn_fullScreen.label = "Try pushing escape!"
 
			}
 
			protected function windowedapplication1_keyDownHandler(event:KeyboardEvent):void
			{
				event.preventDefault();
				if( event.keyCode == Keyboard.ESCAPE )
				{
					btn_fullScreen.label = "Nope, escape won't work. Try pushing the Enter Key";
				}
				if( event.keyCode == Keyboard.ENTER )
				{
					stage.displayState = StageDisplayState.NORMAL;
					btn_fullScreen.label = "Go Full Screen!";
				} 
			}
 
		]]>
	</fx:Script>
 
	<s:Button id="btn_fullScreen" label="Go Full Screen!" click="btn_fullScreen_clickHandler(event)" />	
</s:WindowedApplication>

Once you call the event.preventDefault() method you can use the Escape keyCode like any other key.

Garbage Collection of XML Data
Another one that will be huge for performance optimization is the ability to mark an XML object for immediate garbage collection. To do this you can use the new System.disposeXML(xml:XML) method. Oliver Goldman gave me a great explanation of what’s happening.

Even then, the disposeXML() call doesn’t immediately dispose of the XML. The XML object is backed by a graph of objects with parent/child pointers between them. Those pointers make it difficult for the GC to collect all of those objects. The disposeXML() call traverses the graph, setting all of those pointers to null, and making it much easier for collection to occur. The objects still aren’t collected right away, however—that’s still pending on the GC activity.

To use the new API you just have to call the method and then null out your XML variable. That will make it easier for the garbage collector to clean up. In the example below I load in a big XML file, do some parsing to it so that it loads into memory, then call the System.disposeXML() method.

public var xml:XML;
 
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
	var file:File = File.applicationDirectory.resolvePath("assets/Untitled.gpx");
	var stream:FileStream = new FileStream();
		stream.open(file,FileMode.READ);
	var str:String = stream.readUTFBytes(file.size);
		stream.close();	
	xml = new XML(str);			
}
 
protected function btn_loadXML_clickHandler(event:MouseEvent):void
{
	xml.normalize();
	var xmlList:XMLList = xml.children();
	var text:XMLList = xml.text();
	var xmlStr:String = xml.toString();
}
 
 
protected function btn_disposeXML_clickHandler(event:MouseEvent):void
{
	System.disposeXML( xml );
	xml = null;
}
Memory Profiler using the disposeXML() method

Memory Profiler using the disposeXML() method

You can see how this behaves in the profiler screenshot above. If you want to immediately garbage collect you can call System.gc() but if you use this often it can have negative performance implications (thanks to Ethan for the tip).

The last one I wanted to touch on was a new, friendlier install screen as blogged about by Joseph Labrecque and Oliver Goldman. We got rid of the “System Access: Unrestricted” for signed applications so that if you sign your app your end users will have a nicer install experience.

Adobe AIR 1.5.2 Signed App Install Screen

Adobe AIR 1.5.2 Signed App Install Screen

Those are three of the biggies. You can download the sample project here if you want to run the full screen example and the System.disposeXML() example.

Posted in Adobe, Adobe AIR

12 Responses

  1. Ammar Mardawi

    Will self signed air apps also remove the message on 1.5.2?

  2. Breizo

    Thank you for the tips!
    But where can we find the AdobeAIRSDK.zip file? Also, if you are using the SDK plugin for Eclipse, how is the install procedure different?
    Thanks!

  3. Breizo

    OK, I found the AIR SDK there:
    http://www.adobe.com/cfusion/entitlement/index.cfm?e=airsdk.

  4. ryanstewart

    @Ammar, no the self-signed apps will still look the same.

    =Ryan
    ryan@adobe.com

  5. antonio

    Ryan,
    Is this sdk not ready for Flash? Adobe documentation does not reference 1.5.2 for Flash. Or at least, I’m not able to find any.
    Thanks.

  6. ryanstewart

    @Antonio, I don’t think so, but I’ll ask and double check.

    =Ryan
    ryan@adobe.com

  7. antonio

    @Ryan, Thanks!

  8. Brent

    I’ve tried this several times, each time I receive an error in Flex Builder: “Unable to locate specified base class ‘mx.core.WindowedApplication’”

    Looks like its closed in the Bug Database under SDK-15359 but no solution is listed. Unsure what to try next.

  9. skwasha

    disabling the ‘esc’ key is a start. But to make a true full kiosk app, any system key cmds would need to be intercepted and dealt with (e.g. ctrl-alt-del.) Is there any way to do that currently?

    thx!

  10. Cesare

    @brent same problem here. Did you find any solution?

  11. Richard Leggett

    Possible caution when using this on Windows, I’ve found calling System.disposeXML() terminates the code following that statement, works fine on OSX (both using AIR 1.5.2). So it’s safest to make it the last line in the method.

    This example works on OSX, fails on Windows.

    http://pastebin.com/f6313f070

  12. chris

    Good post, but I’m having difficulty with capturing the ESC key — if the focus is removed from the stage, say, if the user clicks a button, then the event handler no longer will prevent the app from exiting full screen mode.

    I’m designing a kiosk app and it’s a requirement that it stay in full screen mode, but I’m having trouble figuring out a way of preventing it from exiting full screen mode short of disabling every control’s default keyDown handlers…

    ideas?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Ryan Stewart – Rich Internet Application Mountaineer

A blog by a Platform Evangelist at Adobe covering Adobe's RIA platform. Includes posts about Adobe Flex, Adobe AIR, ColdFusion, LiveCycle, Thermo, and everything in between.