New AIR 1.5.2 API Examples and Features

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.

Show Comments

Hide Comments

Comments are closed.