Air update manager

I had to implement update mechanism into one of my Air applications. After searching in Flex help and googling the only reasonable solution I've found was one from Rich Tretola (everythingflex.com). It is really easy to use library but with that simplicity I missed some freedom while using it:

  1. no events - main application that uses Rich's UpdateManager doesn't know if update required or not, if UpdateManager started downloading update etc...
  2. notifying user about update file downloading progress
So I've decided to write my own UpdateManager and there is the result:
The API is quite simple
  1. Constructor has 3 arguments
    1. _versionXmlUrl: String
    2. autoUpdate: Boolean = true ( optional)
    3. autoCheck: Boolean = false (optional)
  2. Public methods:
    1. checkForUpdate (): void
    2. update (): void
  3. Events:
    1. public static const EVENT_UPDATE_AVAILABLE: String;
    2. public static const EVENT_UPDATE_DOWNLOAD_STARTED: String;
    3. public static const EVENT_UPDATE_DOWNLOAD_COMPLETE: String;
    4. public static const EVENT_UPDATE_DOWNLOAD_PROGRESS: String;
    5. public static const EVENT_UPDATE_NOT_REQUIRED: String;
    6. public static const EVENT_UPDATE_FAULT: String;
  4. Read only properties:
    1. get currentVersionString (): String
    2. get newVersionString (): String
    3. get updateType (): String - critical, major, minor
    4. get updateMessage (): String
    5. get bytesLoaded (): int
    6. get bytesTotal (): int

Simple (silent update) example:
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="doInit ()"/>

<mx:Script>
<![CDATA[
import com.cloud17.air.managers.UpdateManager;

private var um: UpdateManager;

private function doInit(): void
{
um = new UpdateManager ( "http://mysite.com/air/getlatestversion.xml", true, true );
um.addEventListener ( UpdateManager.EVENT_UPDATE_FAULT, start );
um.addEventListener ( UpdateManager.EVENT_UPDATE_NOT_REQUIRED, start );

}

private function start ( evt: Event = null ): void
{
//main app logic
}
]]>
</mx:Script>
<mx:WindowedApplication>


Complex example:
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="doInit ()"/>
<mx:Script>
<![CDATA[
import com.cloud17.air.managers.UpdateManager;
import com.justversus.controls.ProgressWindow;
import com.justversus.utils.Prompt;
import mx.managers.PopUpManager;

private var um: UpdateManager;
private var progressWindow: ProgressWindow;

private function doInit(): void
{
um = new UpdateManager ( "http://mysite.com/air/getlatestversion.xml", false, true );
um.addEventListener ( UpdateManager.EVENT_UPDATE_AVAILABLE, updateAvailableHandler );
um.addEventListener ( UpdateManager.EVENT_UPDATE_DOWNLOAD_STARTED, updateDownloadStartedHandler );
um.addEventListener ( UpdateManager.EVENT_UPDATE_DOWNLOAD_PROGRESS, updateDownloadProgressHandler );
um.addEventListener ( UpdateManager.EVENT_UPDATE_DOWNLOAD_COMPLETE, updateDownloadCompleHandler );
um.addEventListener ( UpdateManager.EVENT_UPDATE_FAULT, start );
um.addEventListener ( UpdateManager.EVENT_UPDATE_NOT_REQUIRED, start );

}

private function updateAvailableHandler ( evt: Event ): void
{
Prompt.show( "You have installed " + um.currentVersionString + " version of application. \n" +
"Would you like to update your application to " + um.newVersionString + "?\n\n" + um.updateMessage,
"New version available -- " + um.newVersionString, doUpdate );
}

private function doUpdate(): void
{
um.update();
}


private function updateDownloadStartedHandler ( evt: Event ): void
{
progressWindow = PopUpManager.createPopUp ( this, ProgressWindow, true ) as ProgressWindow;
progressWindow.title = "Updating application...";
PopUpManager.centerPopUp( progressWindow );
}

private function updateDownloadCompleHandler ( evt: Event ): void
{
PopUpManager.removePopUp( progressWindow );
}

private function updateDownloadProgressHandler ( evt: Event ): void
{
progressWindow.setProgress ( um.bytesLoaded, um.bytesTotal );
}



public function start( event: Event = null): void
{
//do some application logic

}
]]>
</mx:Script>
<mx:WindowedApplication>

Feel free to use it...