Home  >  Development  >  blogs

AIR API - Performing Updates in JavaScript

AddThis Social Bookmark Button
airLogo-Shadow.jpg

In the previous post, I examined the process for updating AIR applications in ActionScript. This was an extension to my original post about the Updater API inside of Adobe AIR. If you are not familiar with the update functionality inside of AIR, you will probably want to read the introduction.

One of the items that I covered in the introduction was the update process. As a quick review, here are the five steps that I mentioned. These steps are followed by the actual process that will be scripted in JavaScript.

  1. 1. Determine the update version string for your application (the new version you will be updating to)
  2. 2. Download the correct version of that AIR application to local computer (preferably in the application resource directory)
  3. 3. Create an instance of the Updater class
  4. 4. Call the update method of the Updater class
  5. 5. AIR will shut down your AIR application, install the new version, and then restart the application

These steps are very general and apply to both ActionScript and JavaScript. Let's now look at the specific steps in JavaScript needed to update an application.

  • 1. Determine the URL and version string of the application to which you will be updating.
  • 2. Create an instance of the URLStream class and pass it an instance of the URLRequest class (that points the URL from step 1). Since the URLStream.load method is asynchronous, you will need to add an event listener that will listen for then the URLStream is completed.
function getApplicationVersion() {
 	// Get Application Descriptor File
 	var appXML = air.NativeApplication.nativeApplication.applicationDescriptor;
 	// Parse the Application Descriptor File as XML
 	var xmlObject = (new DOMParser()).parseFromString(appXML, "text/xml");
 	// Get the Needed Values from the XML
 	return xmlObject.getElementsByTagName('version')[0].firstChild.nodeValue;
}
  • 3. After the download is complete, you will read the contents of the URLStream into a ByteArray.
function onDownloadComplete( ev ) {
 	var ba = new air.ByteArray();
 	stream.readBytes( ba, 0, stream.bytesAvailable );
 	updateFile = air.File.applicationStorageDirectory.resolvePath("update-" + date.getMilliseconds() + ".air");
 	fileStream = new air.FileStream();
 	fileStream.addEventListener( air.Event.CLOSE, performUpdate );
 	fileStream.openAsync( updateFile, air.FileMode.WRITE );
 	// Write the data to the file
 	fileStream.writeBytes( ba, 0, ba.length );
 	// Close the connection to the file
 	fileStream.close();
}
  • 4. You will now create a new File object in the application storage directory. You will also create a new FileStream object and read the ByteArray into the File object. Since this is an asynchronous process, you will also create an event listener for when this process completes.
function onDownloadComplete( ev ) {
 	var ba = new air.ByteArray();
 	stream.readBytes( ba, 0, stream.bytesAvailable );
 	updateFile = air.File.applicationStorageDirectory.resolvePath("update-" + date.getMilliseconds() + ".air");
 	fileStream = new air.FileStream();
 	fileStream.addEventListener( air.Event.CLOSE, performUpdate );
 	fileStream.openAsync( updateFile, air.FileMode.WRITE );
 	// Write the data to the file
 	fileStream.writeBytes( ba, 0, ba.length );
 	// Close the connection to the file
 	fileStream.close();
}
  • 5. Finally, you will create and instance of the Updater class and pass it the File object and the version you are updating to. AIR takes it from there.
function performUpdate() {
 	var updater = new air.Updater();
 	updater.update( updateFile, updateData.version );
 	air.Introspector.Console.log("Perform Update");
}

The Example Application

While there are many steps to this process, I have provided code and a sample application that you can use to get started. This application allows you to switch between three versions of an application (that are identical except for their version string).



Application Source Code
Download (5 kb)

Additional Resources
Updating AIR Applications
ActionScript basics for JavaScript developers
Working with Byte Arrays

Comments

Leave a comment


Type the characters you see in the picture above.

Poll: ECMAScript Reaction

The ECMA organization recently decided to stop work on ECMAScript 4 and begin a new version, tentatively described as ES "Harmony." How would you like to see this affect the evolution of ActionScript?

Vote | View Poll Results | Read Related Blog Entry

Tag Cloud

Related Books

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

Anatomy of an Enterprise Flex RIA

Recent Comments

Archives


 
 


Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.

About Us
Meet the Experts
Meet Our Contributors
Send Us Feedback