Menu

[r11]: / trunk / SWFInvestigator / src / updateManager / NativeFileUpdater.as  Maximize  Restore  History

Download this file

190 lines (165 with data), 6.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/****************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2012 Adobe Systems Incorporated and it’s licensors
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
* ****************************************************************************/
package updateManager
{
import air.update.events.DownloadErrorEvent;
import air.update.events.StatusUpdateErrorEvent;
import air.update.events.StatusUpdateEvent;
import com.riaspace.nativeApplicationUpdater.NativeApplicationUpdater;
import flash.desktop.NativeApplication;
import flash.events.ErrorEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import mx.controls.Alert;
import mx.events.CloseEvent;
public class NativeFileUpdater implements IUpdater
{
namespace updater = "http://updater.riaspace.com";
private var updater:NativeApplicationUpdater;
private var manualUpdate:Boolean;
/**
* @public
* This constructor will load the config file necessary for the AirFileUpdater.
*
* @param configFile A string showing the path of the config file relative to the app: directory
*/
public function NativeFileUpdater(configFile:String)
{
var config:File = File.applicationDirectory.resolvePath(configFile);
var cStream:FileStream = new FileStream();
cStream.open(config, FileMode.READ);
var configXML:XML = new XML(cStream.readUTFBytes(cStream.bytesAvailable));
cStream.close();
//There is probably a more elegant way to do this...
var configList:XMLList = configXML.elements();
for each (var item:XML in configList) {
if (item.toXMLString().indexOf("native_url") > 0) {
var updateURL:String = item.toString();
}
}
this.updater = new NativeApplicationUpdater();
this.updater.updateURL = updateURL;
this.updater.isNewerVersionFunction = isNewerFunction;
this.updater.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR,updater_errorHandler);
this.updater.addEventListener(StatusUpdateErrorEvent.UPDATE_ERROR, updater_errorHandler);
this.updater.addEventListener(StatusUpdateEvent.UPDATE_STATUS, updater_statusHandler);
this.updater.addEventListener(ErrorEvent.ERROR,updater_errorHandler);
}
/**
* @private
* Compares versions to see if there is a newer version available.
* Assumes version is in X.Y.Z format where X,Y and Z are numbers
*
* @param currentVersion A string representing the current version
* @param updateVersion A string represneting the updated version
* @return True if there is a newer version, false if the data is bad or it is not new
*/
private function isNewerFunction(currentVersion:String, updateVersion:String):Boolean
{
var curNums:Array = currentVersion.split(".");
var updateNums:Array = updateVersion.split(".");
if (updateNums == null || updateNums.length != 3) {
return false;
}
var testNum:Number = parseInt(updateNums[0]);
var curNum:Number = parseInt(curNums[0]);
if (isNaN(testNum)) {
return false;
} else if (curNum > testNum) {
return false;
}
testNum = parseInt(updateNums[1]);
curNum = parseInt(curNums[1]);
if (isNaN(testNum)) {
return false;
} else if (curNum > testNum) {
return false;
}
testNum = parseInt(updateNums[2]);
curNum = parseInt(curNums[2]);
if (isNaN(testNum)) {
return false;
} else if (curNum >= testNum) {
return false;
}
return true;
}
/**
* @public
* Initialize the application updater
*/
public function initialize():void {
this.updater.initialize();
}
/**
* @public
* This function will check for new native installer updates
* If it is user initiated, then the system will show more errors.
*
* @param userInitiated Indicates whether this was a request from the user.
*/
public function checkForUpdates(userInitiated:Boolean):void {
this.manualUpdate = userInitiated;
if (userInitiated) {
if (this.updater.currentState == NativeApplicationUpdater.DOWNLOADING || this.updater.currentState == NativeApplicationUpdater.INSTALLING) {
mx.controls.Alert.show("The updater appears to be " + this.updater.currentState + ". Please wait if you have a slow connection or try restarting the app. This also may be caused by the application being unable to find the update on the server.","Sorry");
return;
}
//This is a hack since the NativeApplicationUpdater doesn't support resetting
this.updater.currentState = NativeApplicationUpdater.READY;
}
this.updater.checkNow();
}
/**
* @private
* This error handler will show errors if it is a manual update
*
* @param event The ErrorEvent from the updater
*/
private function updater_errorHandler(event:ErrorEvent):void {
if (this.manualUpdate) {
mx.controls.Alert.show("There was an error in the update process:\n\n" + event.toString(),"Update Error");
}
}
/**
* @private
* Handler for the update Alert dialogue. If the user clicked "Install", then the dowload will begin.
*
* @param event The closeEvent from the Alert dialogue
*/
private function alertHandler(event:CloseEvent):void {
if (event.detail==Alert.YES) {
updater.downloadUpdate();
}
}
/**
* @private
* This error handler will show errors if it is a manual update
*
* @param event The ErrorEvent from the updater
*/
private function updater_statusHandler(event:StatusUpdateEvent):void {
if (this.manualUpdate && event.available == false) {
if (this.updater.isNewerVersionFunction.call(this.updater,this.updater.currentVersion,this.updater.updateVersion) == false) {
mx.controls.Alert.show("No updates available","Sorry!");
}
} else if (event.available) {
Alert.yesLabel = "Install";
Alert.noLabel = "Cancel"
var Message:String = "The newest version is: " + this.updater.updateVersion + "\n";
Message += "Your version is: " + this.updater.currentVersion;
Alert.show(Message,"Update Available",3,null,alertHandler);
Alert.yesLabel = "Yes";
Alert.noLabel = "No"
event.preventDefault();
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.