Skip to content

Commit

Permalink
Restructured initialization of device to ensure phonegap available wh…
Browse files Browse the repository at this point in the history
…en 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);
  • Loading branch information
Jesse MacFadyen committed Nov 30, 2009
1 parent 8ac7b32 commit fd7147e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 55 deletions.
37 changes: 21 additions & 16 deletions iphone/PhoneGapLib/Classes/PhoneGapDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) {
Expand Down Expand Up @@ -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;

Expand Down
34 changes: 15 additions & 19 deletions iphone/PhoneGapLib/javascripts/core/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
63 changes: 43 additions & 20 deletions iphone/PhoneGapLib/javascripts/core/phonegap.js.base
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
})();

Expand Down

0 comments on commit fd7147e

Please sign in to comment.