11/// <reference path='ReconnectingWebSocket.ts' />
22module openflow {
33 // "use strict";
4-
5- var doLoadMobileData : boolean = false ;
64 interface IHashTable < T > {
75 [ key : string ] : T ;
86 }
@@ -39,6 +37,7 @@ module openflow {
3937 public jwt : string = null ;
4038 public device : any = null ;
4139 public usingCordova : boolean = false ;
40+ public scanCount : number = 0 ;
4241 public oneSignalId : string = null ;
4342 public location : any ;
4443 static $inject = [ "$rootScope" , "$location" , "$window" ] ;
@@ -174,7 +173,92 @@ module openflow {
174173 } ) ;
175174 } ) ;
176175 }
176+ scanForCordova ( ) {
177+ try {
178+ if ( cordova !== undefined ) {
179+ console . log ( "Found cordova" ) ;
180+ this . usingCordova = true ;
181+ document . addEventListener ( "deviceready" , async ( ) => {
182+ console . log ( "deviceready" ) ;
183+ if ( ( window as any ) . plugins ) {
184+ var oneSignal = ( window as any ) . plugins . OneSignal ;
185+ if ( oneSignal ) {
186+ try {
187+ console . debug ( "window.plugins.OneSignal exists" ) ;
188+
189+ const sender_id = "906036108091" ; // google_project_number
190+ const oneSignalAppId = "cfdefd08-d4ad-4593-8173-4ba4ebf4d67a" ; // onesignal_app_id
191+ var iosSettings = { } ;
192+ iosSettings [ "kOSSettingsKeyAutoPrompt" ] = true ;
193+ iosSettings [ "kOSSettingsKeyInAppLaunchURL" ] = true ;
194+
195+ console . debug ( "oneSignal.startInit" ) ;
196+ oneSignal . startInit ( oneSignalAppId , sender_id ) .
197+ iOSSettings ( iosSettings ) .
198+ inFocusDisplaying ( oneSignal . OSInFocusDisplayOption . Notification ) .
199+ handleNotificationOpened ( this . notificationOpenedCallback ) .
200+ handleNotificationReceived ( this . notificationReceivedCallback ) .
201+ endInit ( ) ;
202+ this . oneSignalId = await this . getids ( oneSignal ) ;
203+ console . log ( "oneSignalId: " + this . oneSignalId ) ;
204+
205+ } catch ( error ) {
206+ console . error ( error ) ;
207+ }
208+ } else {
209+ console . log ( "Missing oneSignal plugin" ) ;
210+ }
211+ }
212+ try {
213+ if ( device ) {
214+ console . debug ( "device exists" ) ;
215+ this . device = device ;
216+ } else {
217+ console . debug ( "device does NOT exists" ) ;
218+ }
219+ } catch ( error ) {
220+ console . error ( error ) ;
221+ }
222+ document . addEventListener ( "pause" , this . onPause , false ) ;
223+ document . addEventListener ( "resume" , this . onResume , false ) ;
224+ document . addEventListener ( "menubutton" , this . onMenuKeyDown , false ) ;
225+ document . addEventListener ( "backbutton" , this . onbackbutton , false ) ;
226+
227+
228+ if ( cordova . plugins && cordova . plugins . diagnostic ) {
229+ console . debug ( "Check if authorized for location" ) ;
230+ var isAuthorized = await this . isLocationAuthorized ( ) ;
231+ if ( ! isAuthorized ) {
232+ console . debug ( "Not authorized for location is not , request authorization" ) ;
233+ await this . requestLocationAuthorization ( ) ;
234+ }
235+
236+ var isAvailable = await this . isLocationAvailable ( ) ;
237+ if ( ! isAvailable ) {
238+ console . debug ( "Location is not available" ) ;
239+ } else {
240+ console . debug ( "Location is available, get current location" ) ;
241+ this . location = await this . getLocation ( ) ;
242+ }
243+ } else {
244+ console . debug ( "diagnostic is missing" ) ;
245+ }
246+ this . gettoken ( ) ;
247+ } ) ;
248+ } else {
249+ console . debug ( "cordova not definded" ) ;
250+ if ( this . scanCount < ( 5 * 4 ) ) {
251+ this . scanCount ++ ;
252+ setTimeout ( this . scanForCordova , 200 ) ;
253+ }
254+ }
255+ } catch ( error ) {
256+ console . error ( error ) ;
257+ console . debug ( "Failed locating cordova" ) ;
258+ }
259+ }
177260 init ( ) {
261+ setTimeout ( this . scanForCordova . bind ( this ) , 200 ) ;
178262 this . getJSON ( "/config" , async ( error : any , data : any ) => {
179263 var parser = document . createElement ( 'a' ) ;
180264 parser . href = data . wshost ;
@@ -185,6 +269,7 @@ module openflow {
185269 parser . search ; // => "?search=test"
186270 parser . hash ; // => "#hash"
187271 parser . host ; // => "example.com:3000"
272+ console . log ( data ) ;
188273 if ( location . protocol == 'https:' && parser . protocol == "ws:" ) {
189274 data . wshost = "wss://" + parser . hostname ;
190275 console . log ( "new wshost: " + data . wshost ) ;
@@ -265,88 +350,6 @@ module openflow {
265350 timeout ( ms ) {
266351 return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
267352 }
268- private async LoadMobileData ( ) {
269- console . log ( "this.usingCordova: " + this . usingCordova ) ;
270- if ( ! this . usingCordova ) {
271- console . log ( "wait 1 seconds and test for Cordova again" ) ;
272- let counter : number = 0 ;
273- while ( ! this . usingCordova && counter < 15 ) {
274- counter ++ ;
275- await this . timeout ( 100 ) ;
276- var win : any = window ;
277- this . usingCordova = ! ! win . cordova ;
278- }
279- console . log ( "this.usingCordova: " + this . usingCordova ) ;
280- }
281- if ( this . usingCordova ) {
282- document . addEventListener ( "deviceready" , async ( ) => {
283- console . log ( "deviceready" ) ;
284- if ( ( window as any ) . plugins ) {
285- var oneSignal = ( window as any ) . plugins . OneSignal ;
286- if ( oneSignal ) {
287- try {
288- console . debug ( "window.plugins.OneSignal exists" ) ;
289-
290- const sender_id = "906036108091" ; // google_project_number
291- const oneSignalAppId = "cfdefd08-d4ad-4593-8173-4ba4ebf4d67a" ; // onesignal_app_id
292- var iosSettings = { } ;
293- iosSettings [ "kOSSettingsKeyAutoPrompt" ] = true ;
294- iosSettings [ "kOSSettingsKeyInAppLaunchURL" ] = true ;
295-
296- console . debug ( "oneSignal.startInit" ) ;
297- oneSignal . startInit ( oneSignalAppId , sender_id ) .
298- iOSSettings ( iosSettings ) .
299- inFocusDisplaying ( oneSignal . OSInFocusDisplayOption . Notification ) .
300- handleNotificationOpened ( this . notificationOpenedCallback ) .
301- handleNotificationReceived ( this . notificationReceivedCallback ) .
302- endInit ( ) ;
303- this . oneSignalId = await this . getids ( oneSignal ) ;
304- console . log ( "oneSignalId: " + this . oneSignalId ) ;
305-
306- } catch ( error ) {
307- console . error ( error ) ;
308- }
309- } else {
310- console . log ( "Missing oneSignal plugin" ) ;
311- }
312- }
313- try {
314- if ( device ) {
315- console . debug ( "device exists" ) ;
316- this . device = device ;
317- } else {
318- console . debug ( "device does NOT exists" ) ;
319- }
320- } catch ( error ) {
321- console . error ( error ) ;
322- }
323- document . addEventListener ( "pause" , this . onPause , false ) ;
324- document . addEventListener ( "resume" , this . onResume , false ) ;
325- document . addEventListener ( "menubutton" , this . onMenuKeyDown , false ) ;
326- document . addEventListener ( "backbutton" , this . onbackbutton , false ) ;
327-
328-
329- if ( cordova . plugins && cordova . plugins . diagnostic ) {
330- console . debug ( "Check if authorized for location" ) ;
331- var isAuthorized = await this . isLocationAuthorized ( ) ;
332- if ( ! isAuthorized ) {
333- console . debug ( "Not authorized for location is not , request authorization" ) ;
334- await this . requestLocationAuthorization ( ) ;
335- }
336-
337- var isAvailable = await this . isLocationAvailable ( ) ;
338- if ( ! isAvailable ) {
339- console . debug ( "Location is not available" ) ;
340- } else {
341- console . debug ( "Location is available, get current location" ) ;
342- this . location = await this . getLocation ( ) ;
343- }
344- } else {
345- console . debug ( "diagnostic is missing" ) ;
346- }
347- } ) ;
348- }
349- }
350353 private onopen ( evt : Event ) {
351354 console . log ( "WebSocketClient::onopen: connected" ) ;
352355 this . gettoken ( ) ;
@@ -375,9 +378,6 @@ module openflow {
375378 }
376379 return ;
377380 }
378- if ( doLoadMobileData == true ) {
379- await this . LoadMobileData ( )
380- }
381381 q . jwt = data . jwt ;
382382 q . rawAssertion = data . rawAssertion ;
383383 q . realm = "browser" ;
0 commit comments