1: package com.jonniespratley.air.managers
2: {
3: import air.net.URLMonitor;
4:
5: import flash.events.StatusEvent;
6: import flash.net.URLRequest;
7:
8: import mx.controls.Alert;
9:
10: public class ConnectionManager
11: {
12: private var statusEvent:StatusEvent;
13: private var urlMonitor:URLMonitor;
14: private var showAlert:Boolean;
15: private var message:String;
16:
17: [Bindable] public var connectionURL:String;
18: [Bindable] public var isConnected:Boolean;
19:
20: //3 Arguments, alert, connection URL, and the message to display
21: public function ConnectionManager( showAlert:Boolean = true,
22: connectionURL:String = "http://google.com",
23: message:String = "This Air Application must be connected to the internet, \nConnection Error" )
24:
25: {
26: this.showAlert = showAlert;
27: this.connectionURL = connectionURL;
28: this.message = message;
29:
30: startMonitor();
31: }
32:
33: //Send a request to google, and see if we get a response
34: public function startMonitor():void
35: {
36: var urlRequest:URLRequest = new URLRequest( connectionURL );
37: urlRequest.method = "HEAD";
38:
39: urlMonitor = new URLMonitor( urlRequest );
40: urlMonitor.addEventListener( StatusEvent.STATUS, statusChanged );
41: urlMonitor.start();
42: }
43:
44: //Dispatch a change event, whenever the status of our connection changes
45: public function statusChanged( event:StatusEvent ):void
46: {
47: this.isConnected = urlMonitor.available;
48:
49: if ( !this.isConnected && this.showAlert )
50: {
51: Alert.show( this.message, "Connection Failure" );
52: }
53:
54: statusEvent = new StatusEvent( StatusEvent.STATUS );
55:
56: dispatchEvent( statusEvent );
57:
58: }
59:
60: }
61: }