From fd7147e9764e42b938078b67881e11db4300c5cf Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Mon, 30 Nov 2009 12:49:59 -0800 Subject: [PATCH] Restructured initialization of device to ensure phonegap available when navigating between pages. Implemented the 'deviceready' event, do not expect any phonegap exec calls to be executed until the deviceready event has been fired. Use this: document.addEventListener('deviceready',YOUR_INIT_FUNCTION ,false); --- iphone/PhoneGapLib/Classes/PhoneGapDelegate.m | 37 ++++++----- iphone/PhoneGapLib/javascripts/core/device.js | 34 +++++----- .../javascripts/core/phonegap.js.base | 63 +++++++++++++------ 3 files changed, 79 insertions(+), 55 deletions(-) diff --git a/iphone/PhoneGapLib/Classes/PhoneGapDelegate.m b/iphone/PhoneGapLib/Classes/PhoneGapDelegate.m index a92b5dc3..5153a0ac 100755 --- a/iphone/PhoneGapLib/Classes/PhoneGapDelegate.m +++ b/iphone/PhoneGapLib/Classes/PhoneGapDelegate.m @@ -195,23 +195,9 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application When web application loads Add stuff to the DOM, mainly the user-defined settings from the Settings.plist file, and the device's data such as device ID, platform version, etc. */ -- (void)webViewDidStartLoad:(UIWebView *)theWebView { - NSDictionary *deviceProperties = [[self getCommandInstance:@"Device"] deviceProperties]; - NSMutableString *result = [[NSMutableString alloc] initWithFormat:@"DeviceInfo = %@;", [deviceProperties JSONFragment]]; - - /* Settings.plist - * Read the optional Settings.plist file and push these user-defined settings down into the web application. - * This can be useful for supplying build-time configuration variables down to the app to change its behaviour, - * such as specifying Full / Lite version, or localization (English vs German, for instance). - */ - NSDictionary *temp = [PhoneGapDelegate getBundlePlist:@"Settings"]; - if ([temp respondsToSelector:@selector(JSONFragment)]) { - [result appendFormat:@"\nwindow.Settings = %@;", [temp JSONFragment]]; - } +- (void)webViewDidStartLoad:(UIWebView *)theWebView +{ - NSLog(@"Device initialization: %@", result); - [theWebView stringByEvaluatingJavaScriptFromString:result]; - [result release]; // Play any default movie if(![[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"]) { @@ -303,6 +289,25 @@ - (void)webViewDidFinishLoad:(UIWebView *)theWebView { /* * Hide the Top Activity THROBER in the Battery Bar */ + + NSDictionary *deviceProperties = [[self getCommandInstance:@"Device"] deviceProperties]; + NSMutableString *result = [[NSMutableString alloc] initWithFormat:@"DeviceInfo = %@;", [deviceProperties JSONFragment]]; + + /* Settings.plist + * Read the optional Settings.plist file and push these user-defined settings down into the web application. + * This can be useful for supplying build-time configuration variables down to the app to change its behaviour, + * such as specifying Full / Lite version, or localization (English vs German, for instance). + */ + + NSDictionary *temp = [PhoneGapDelegate getBundlePlist:@"Settings"]; + if ([temp respondsToSelector:@selector(JSONFragment)]) { + [result appendFormat:@"\nwindow.Settings = %@;", [temp JSONFragment]]; + } + + NSLog(@"Device initialization: %@", result); + [theWebView stringByEvaluatingJavaScriptFromString:result]; + [result release]; + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; activityView.hidden = YES; diff --git a/iphone/PhoneGapLib/javascripts/core/device.js b/iphone/PhoneGapLib/javascripts/core/device.js index 06b110f3..36a4f7ba 100644 --- a/iphone/PhoneGapLib/javascripts/core/device.js +++ b/iphone/PhoneGapLib/javascripts/core/device.js @@ -3,31 +3,27 @@ * phone, etc. * @constructor */ -function Device() { - this.available = PhoneGap.available; +function Device() +{ this.platform = null; this.version = null; this.name = null; this.gap = null; this.uuid = null; - try { - if (window.DroidGap) { - this.available = true; - this.uuid = window.DroidGap.getUuid(); - this.version = window.DroidGap.getOSVersion(); - this.gapVersion = window.DroidGap.getVersion(); - this.platform = window.DroidGap.getPlatform(); - this.name = window.DroidGap.getProductName(); - } else { - this.platform = DeviceInfo.platform; - this.version = DeviceInfo.version; - this.name = DeviceInfo.name; - this.gap = DeviceInfo.gap; - this.uuid = DeviceInfo.uuid; - } - } catch(e) { - this.available = false; + try + { + this.platform = DeviceInfo.platform; + this.version = DeviceInfo.version; + this.name = DeviceInfo.name; + this.gap = DeviceInfo.gap; + this.uuid = DeviceInfo.uuid; + + } + catch(e) + { + // TODO: } + this.available = PhoneGap.available = this.uuid != null; } PhoneGap.addConstructor(function() { diff --git a/iphone/PhoneGapLib/javascripts/core/phonegap.js.base b/iphone/PhoneGapLib/javascripts/core/phonegap.js.base index b4b0625c..ecb64783 100644 --- a/iphone/PhoneGapLib/javascripts/core/phonegap.js.base +++ b/iphone/PhoneGapLib/javascripts/core/phonegap.js.base @@ -17,7 +17,7 @@ PhoneGap = { /** * Boolean flag indicating if the PhoneGap API is available and initialized. - */ + */ // TODO: Remove this, it is unused here ... -jm PhoneGap.available = DeviceInfo.uuid != undefined; /** @@ -27,28 +27,51 @@ PhoneGap.available = DeviceInfo.uuid != undefined; */ PhoneGap.addConstructor = function(func) { var state = document.readyState; - if (state != 'loaded' && state != 'complete') - PhoneGap._constructors.push(func); + if ( ( state == 'loaded' || state == 'complete' ) && DeviceInfo.uuid != null ) + { + func(); + } else - func(); + { + PhoneGap._constructors.push(func); + } }; -(function() { - var timer = setInterval(function() { - var state = document.readyState; - if (state != 'loaded' && state != 'complete') - return; - clearInterval(timer); - while (PhoneGap._constructors.length > 0) { - var constructor = PhoneGap._constructors.shift(); - try { - constructor(); - } catch(e) { - if (typeof(debug['log']) == 'function') - debug.log("Failed to run constructor: " + debug.processMessage(e)); - else - alert("Failed to run constructor: " + e.message); + +(function() + { + var timer = setInterval(function() + { + + var state = document.readyState; + + if ( ( state == 'loaded' || state == 'complete' ) && DeviceInfo.uuid != null ) + { + clearInterval(timer); // stop looking + // run our constructors list + while (PhoneGap._constructors.length > 0) + { + var constructor = PhoneGap._constructors.shift(); + try + { + constructor(); + } + catch(e) + { + if (typeof(debug['log']) == 'function') + { + debug.log("Failed to run constructor: " + debug.processMessage(e)); + } + else + { + alert("Failed to run constructor: " + e.message); + } + } } - } + // all constructors run, now fire the deviceready event + var e = document.createEvent('Events'); + e.initEvent('deviceready'); + document.dispatchEvent(e); + } }, 1); })();