File promises with Adobe AIR 2.0

2010 January 26
tags:
by Piotr Walczyszyn

File promises is one of the new AIR 2.0 features already available in beta on Adobe Labs. “A file promise is a drag-and-drop clipboard format that allows a user to drag a file that does not yet exist out of an AIR application. AIR uses the methods and properties defined by the IFilePromise interface to access the data to be written when the file promise is dropped.” To check out how it works, I built a sample application that allows you drag an AIR icon out of the app and drop it somewhere in your system (e.g. on your desktop). Dropping it will initiate a download of the current stable AIR runtime installer (at this point in time it is AIR 1.5.3). The downloaded file containing the runtime will be appropriate for your operating system. This logic is implemented in the fileUrl function and it uses the Capablities.os property with a simple regexp search for “win”, “lin”, or “mac” patterns.

Here is how the application looks:

Below is the source code:

<?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/mx"
					   xmlns:halo="library://ns.adobe.com/flex/halo"
					   currentState="START_STATE" height="200" width="300" viewSourceURL="srcview/index.html">
 
	<s:layout>
		<s:VerticalLayout horizontalAlign="center" gap="15"/>
	</s:layout>
 
	<fx:Script>
		<![CDATA[
			import flash.events.Event;
			import flash.events.MouseEvent;
			import flash.system.Capabilities;
 
			protected var filePromise:URLFilePromise;
 
			protected function imgAirIcon_mouseDownHandler(event:MouseEvent):void
			{
				// Instantiating new file promise
				filePromise = new URLFilePromise();
				// Registering OPEN event listener, to switch to 
				// DOWNLOAD_STATE when downloading starts
				filePromise.addEventListener(Event.OPEN, onOpen);
 
				// Setting URLRequest pointing to remote file
				filePromise.request = new URLRequest(fileUrl);
				// Setting relativePath with fileName to be saved locally
				filePromise.relativePath = fileName;
 
				// Array of promises with single promise in this case
				var promises:Array = new Array();
				promises.push(filePromise);
 
				// Instantiating clipboard object pointing to the promise
				var clipboard:Clipboard = new Clipboard();
				clipboard.setData(ClipboardFormats.FILE_PROMISE_LIST_FORMAT, promises);
 
				// Dragging with NativeDragManager
				NativeDragManager.doDrag(imgAirIcon, clipboard);
			}
 
			protected function onOpen(event:Event):void
			{
				currentState = "DOWNLOAD_STATE";
				prgBar.source = filePromise;
			}
 
			protected function get fileUrl():String
			{
				// Returns remote file URL based on current operating system
				if (Capabilities.os.search(/mac/i) > -1)
					return "http://airdownload.adobe.com/air/mac/download/latest/AdobeAIR.dmg";
				else if (Capabilities.os.search(/win/i) > -1)
					return "http://airdownload.adobe.com/air/win/download/latest/AdobeAIRInstaller.exe";
				else 
					return "http://airdownload.adobe.com/air/lin/download/latest/AdobeAIRInstaller.bin";
			}
 
			protected function get fileName():String
			{
				var fileUrl:String = fileUrl;
				return fileUrl.slice(fileUrl.lastIndexOf("/") + 1);
			}
		]]>
	</fx:Script>
 
	<s:states>
		<s:State name="START_STATE"/>
		<s:State name="DOWNLOAD_STATE"/>
	</s:states>
 
	<halo:Image id="imgAirIcon" source="assets/air_icon_special.gif" mouseDown="imgAirIcon_mouseDownHandler(event)" toolTip="{fileUrl}" />
 
	<s:Label text="(Drag it out to start download)" />
 
	<halo:ProgressBar id="prgBar" bottom="10" horizontalCenter="0"  visible="false" visible.DOWNLOAD_STATE="true" label="Downloading {int(prgBar.percentComplete)}%"/>
 
</s:WindowedApplication>
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS