Notes: AIR

Tuesday, April 7th, 2009 | Notes

Some notes I’ve gathered over time in regards to AIR. As per usual, just posted up here for future reference.

Multiple Monitors

When building an app for multiple monitors, be sure to make use of the flash.display.Screen class. This class lets you iterate through all of the monitors available with access to the size and resolution of each. It also can tell you which is the primary monitor. Below is a useful function to grab the current screen.

	private function getCurrentScreen():Screen {
		return Screen.getScreensForRectangle(stage.nativeWindow.bounds)[0];
	}
	


Closing App

To close an app gracefully try to use the Event.EXITING event from NativeApplication.nativeApplication. Or for a window, the Event.CLOSING event from stage.nativeWindow. Both of these dispatch events before closing giving you the opportunity to close any open connections or even animate a closing animation. All code that you execute from these events are processed synchronously.

Below is some code that will stop the application from closing, then close any open windows. If you have NativeApplication.autoExit set to true, this will eventually close your application as well. If you leave any windows open when closing your app, it will not actually exit.

	this.nativeWindow.addEventListener(Event.CLOSING,
	function(e:Event):void {
		e.preventDefault();
		for (var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i) {
			NativeWindow(NativeApplication.nativeApplication.openedWindows[i]).close();
		}
	});
	


Starting App

To start your app at computer startup (when a user logs in), set this property to true.

	NativeApplication.nativeApplication.startAtLogin = true;
	


PDF

Detect Capability
	import flash.html.HTMLPDFCapability;
	if(HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK) {
		// Yes
	}
	
Limitations
  • The user has to have a current version of Acrobat installed
  • When displaying a PDF, the PDF always sites at the top of the display order
  • PDF won’t be displayed in a custom chrome window
  • PDF content cannot be used in a window that is set to the FULL_SCREEN or FULL_SCREEN_INTERACTIVE state
  • Cannot adjust alpha, scaling, or rotation of the PDF inside of the HTMLLoader class
  • tageScaleMode must be set to NO_SCALE

HTML

Flash
  • Renders small subset of HTML tags
  • Supports limited set of css (subset of CSS1)
  • Limited to loading external content on servers that allow it (crossDomain policy file)
  • No internal JS support
AIR
  • Fully supports all standard HTML tags
  • Fuly supports standard css and WebKit extensions
  • Can load external content without a policy file
  • JS support
AIR WebKit versions
  • 1.5.3 : WebKit version 34190
  • 1.5.2 : WebKit version 34190
  • 1.5.1 : WebKit version 34190
  • 1.5.0 : WebKit version ? (SquirrelFish)
  • 1.1.0 : WebKit version ?
  • 1.0.0 : WebKit version ?

Clipboard

Formats
  • BITMAP_FORMAT (AIR)
  • FILE_LIST_FORMAT (AIR)
  • HTML_FORMAT
  • RICH_TEXT_FORMAT
  • TEXT_FORMAT
  • URL_FORMAT (AIR)
Get Data
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	Clipboard.generalClipboard.getData(ClipboardFormats.BITMAP_FORMAT);
	
Set Data

Setting data to the clipboard AT paste time (in a copy paste operation) :

	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	function copyLater(event:MouseEvent):void {
		// getBitmapdata() just returns the data you want set in this example
		Clipboard.generalClipboard.setDataHandler(ClipboardFormats.BITMAP_FORMAT, getBitmapdata());
	}
	


Dragging Items Out

To drag items out, pass an instance of the clipboard class and add whatever data you want to be transferred to it. Then pass that instance to the doDrag method. Optionally, create a BitmapData object to serve as a proxy image during the drag.

	import flash.desktop.Clipboard;
	import flash.desktop.NativeDragManager;
	var cb:Clipboard = new Clipboard();
	cb.setData(ClipboardFormats.BITMAP_FORMAT, bitmapdata);
	NativeDragManager.doDrag(dragInitiator:InteractiveObject, cb, bitmapdata); // bitmap data for use when dragging
	


Files

File Type Associations

To set file type associations you can set it in the application descriptor. If a different program is set as the primary program for that extensions you can check to see if your program is set for it.

	var isSet:Boolean = NativeApplication.nativeApplication.isSetAsDefaultApplication("mp4");
	

If it isn’t you can reset the associations with the code below. But you can only be used with file types declared in the fileTypes statement in the application descriptor.

	NativeApplication.nativeApplication.setAsDefaultApplication("mp4");
	
Copy a File
	var f:File = new File();
	f.addEventListener(Event.SELECT, onCopySelect);
	f.browseForSave("Copy File To");

	funciton onCopySelect(event:Event):void {
		var f:File = event.target as File;
		file.copyToAsync(f);
	}
	
Delete a File
	file.addEventListener(Event.COMPLETE, onFileActionComplete);
	file.deleteFileAsync();

	function onFileActionComplete(event:Event):void {
		// do something
	}
	
Move File to Trash
	file.addEventListener(Event.COMPLETE, onFileActionComplete);
	file.moveToTrashAsync();

	function onFileActionComplete(event:Event):void {
		// do something
	}
	
Write a File

This code will save a prefs.xml to the application’s directory.

	var fs:FileStream = new FileStream();
	fs.addEventListener(Event.COMPLETE, onFileStreamComplete);
	fs.openAsync(new File("app:/prefs.xml"), FileMode.WRITE);
	var filedata:String = '<?xml version="1.0" encoding="utf-8"?>';
	filedata += "<prefs><defaultDirectory>" + curDir.nativePath;
	filedata += "</defaultDirector></prefs>";
	fs.writeUTFBytes(filedata);

	function onFileStreamComplete(event:Event):void {
		var fs:FileStream = event.target as FileStream;
		fs.close();
	}
	
Write a Temp File

Makes a uniquely named temp file.

	fs.openAsync(new File().createTempFile(), FileMode.WRITE);
	


Encrypted Local Store

The encrypted local store is a unique feature that is not saved as a file. It also is limited to 10mb of data per application. You can save any secure data here safely.

	import flash.data.EncryptedLocalStore;
	import flash.utils.ByteArray;

	//Setting data
	var ba:ByteArray = new ByteArray();
	ba.writeUTFBytes("password");
	EncryptedLocalStore.setItem(username.text, ba);

	//Getting data
	var ba:ByteArray = EncryptedLocalStore.getItem(username.text);
	if(ba) {
		var dataPW:String = ba.readUTFBytes(ba.bytesAvailable);
		if(password.text == dataPW) {
			// login
		} else {
			// wrong password
		}
	} else {
		// No data found
	}
	


Service Monitor

When using the Service Monitor Framework, it requires that the ServiceMonitorShim component be in your FLA’s library.

Tags:

No comments yet.

Leave a comment

Please upgrade your Flash Player To submit a comment, you must have Flash Player 9.0.0 or higher installed. I use a flash form here to help prevent spam.

Search

Categories