L’update d’une application AIR est une de ces fonctionnalités bien pratique, et elle n’échappe pas à la vague de modifications apporté par la beta 3.
Cette classe est une évolution de la classe updateManager de Rich Tretola, elle intègre les changements apporté par la beta 3
- var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
- var ns:Namespace = appXml.namespace();
- var appId:String = appXml.ns::id[0];
- appVersion = appXml.ns::version[0];
- var appName:String = appXml.ns::filename[0];
On voit que maintenant la lecture du fichier application est intégrée dans AIR
- package com.mykii.utils
- {
- import flash.desktop.NativeApplication;
- import flash.desktop.Updater;
- import flash.events.Event;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.net.URLRequest;
- import flash.net.URLStream;
- import flash.utils.ByteArray;
- import mx.controls.Alert;
- import mx.events.CloseEvent;
- import mx.rpc.events.ResultEvent;
- import mx.rpc.http.HTTPService;
- public class UpdateManager {
- private var _constanteApplicationName:String;
- public var versionURL:String;
- private var version:XML;
- private var urlStream:URLStream = new URLStream();
- private var fileData:ByteArray = new ByteArray();
- [Bindable] private var _currentVersion:String;
- [Bindable] private var appVersion:String;
- /*
- constructor
- */
- public function UpdateManager(versionURL:String):void{
- this.versionURL = versionURL;
- }
- /*
- load the application.xml file
- */
- public function loadApplicationFile():void{
- var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
- var ns:Namespace = appXml.namespace();
- var appId:String = appXml.ns::id[0];
- appVersion = appXml.ns::version[0];
- var appName:String = appXml.ns::filename[0];
- loadRemoteFile();
- }
- /*
- load a remote xml file to check for new version
- */
- private function loadRemoteFile():void{
- var http:HTTPService = new HTTPService();
- http.url = versionURL;
- http.useProxy=false;
- http.method = "GET";
- http.resultFormat="xml";
- http.send();
- http.addEventListener(ResultEvent.RESULT,testVersion2);
- }
- /*
- check to see if an update exists and either
- force the update or give the user the option
- */
- private function testVersion2(event:ResultEvent):void{
- version = XML(event.result);
- if((appVersion != version.@version) && version.@forceUpdate == true){
- getUpdate();
- }else if(appVersion != version.@version){
- Alert.show("Il y a une mise à jour disponible,\nVoulez vous la télécharger maintenant? \n\nDetails:\n" +
- version.@message, "Choissisez Oui ou Non", 3, null, alertClickHandler);
- } else {
- trace("There are no new updates available");
- }
- }
- /*
- if user wants update, call getUpdate()
- */
- private function alertClickHandler(event:CloseEvent):void {
- if (event.detail==Alert.YES){
- getUpdate();
- }
- }
- /*
- download the new AIR installer file
- */
- private function getUpdate():void{
- var urlReq:URLRequest=new URLRequest(version.@downloadLocation);
- urlStream.addEventListener(Event.COMPLETE, loaded);
- urlStream.load(urlReq);
- }
- /*
- read the new AIR file
- */
- private function loaded(event:Event):void {
- urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
- writeAirFile();
- }
- /*
- write the new AIR installer to disk
- */
- private function writeAirFile():void {
- var file:File = File.applicationStorageDirectory.resolvePath(_constanteApplicationName);
- var fileStream:FileStream = new FileStream();
- fileStream.addEventListener(Event.CLOSE, fileClosed);
- fileStream.openAsync(file, FileMode.WRITE);
- fileStream.writeBytes(fileData, 0, fileData.length);
- fileStream.close();
- }
- /* close the FileStream and use the
- Updater class to update the application
- */
- private function fileClosed(event:Event):void {
- var updater:Updater = new Updater();
- var airFile:File = File.applicationStorageDirectory.resolvePath(_constanteApplicationName);
- updater.update(airFile,version.@version);
- }
- /*
- Setter for the name of the AIR file
- */
- public function set constanteApplicationName(value:String):void{
- _constanteApplicationName = value;
- }
- }
- }
Le fichier distant se présente sous cette forme
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <currentVersion version="0.220"
- downloadLocation="http://www.mykii.eu/AIR/KIISB/KIISB.air"
- forceUpdate="false"
- message="Passage en beta 3"/>