From 13679fe00d1f5d8801c21377b7226e1b215e3fb9 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 24 Aug 2010 13:19:22 -0500 Subject: [PATCH 1/2] Make sure JavaScript PhoneGap code is initialized and deviceready is fired for a new HTML page loaded from link in initial index.html. --- framework/assets/js/phonegap.js.base | 14 +++++++++++--- framework/src/com/phonegap/DroidGap.java | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/framework/assets/js/phonegap.js.base b/framework/assets/js/phonegap.js.base index d5c484ac..5b7cf816 100755 --- a/framework/assets/js/phonegap.js.base +++ b/framework/assets/js/phonegap.js.base @@ -110,7 +110,7 @@ PhoneGap.Channel.join = function(h, c) { (!c[j].fired?c[j].subscribeOnce(f):i--); } if (!i) h(); -} +}; /** @@ -152,7 +152,9 @@ PhoneGap.onNativeReady = new PhoneGap.Channel(); // _nativeReady is global variable that the native side can set // to signify that the native code is ready. It is a global since // it may be called before any PhoneGap JS is ready. -if (_nativeReady) { PhoneGap.onNativeReady.fire(); } +try { + if (_nativeReady) { PhoneGap.onNativeReady.fire(); } +} catch (e) { } /** * onDeviceReady is fired only after both onDOMContentLoaded and @@ -166,6 +168,11 @@ PhoneGap.onDeviceReady.subscribeOnce(function() { PhoneGap.Channel.join(function() { PhoneGap.onDeviceReady.fire(); + + // Fire the onresume event, since first one happens before JavaScript is loaded + var e = document.createEvent('Events'); + e.initEvent('onresume'); + document.dispatchEvent(e); }, [ PhoneGap.onDOMContentLoaded, PhoneGap.onNativeReady ]); @@ -326,4 +333,5 @@ PhoneGap.close = function(context, func, params) { return func.apply(context, params); } } -} \ No newline at end of file +}; + diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 9ec794b4..cd9bdb83 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -108,6 +108,8 @@ public class DroidGap extends Activity { appView.setWebChromeClient(new GapClient(this)); } + appView.setWebViewClient(new GapViewClient(this)); + appView.setInitialScale(100); appView.setVerticalScrollBarEnabled(false); @@ -387,7 +389,22 @@ public class DroidGap extends Activity { } - + public class GapViewClient extends WebViewClient { + + Context mCtx; + + public GapViewClient(Context ctx) { + mCtx = ctx; + } + + public void onPageFinished (WebView view, String url) { + // Try firing the onNativeReady event in JS. If it fails because the JS is + // not loaded yet then just set a flag so that the onNativeReady can be fired + // from the JS side when the JS gets to that code. + appView.loadUrl("javascript:try{ PhoneGap.onNativeReady.fire(); console.log('FIRE!');}catch(e){_nativeReady = true; console.log('native=TRUE');}"); + } + } + public boolean onKeyDown(int keyCode, KeyEvent event) { From ea8dc735258ffc8efad02e7d48472fb0de5494f5 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 24 Aug 2010 15:03:38 -0500 Subject: [PATCH 2/2] Don't need to fire native ready event during constructor. It gets fired when page has been loaded. --- framework/src/com/phonegap/DroidGap.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index cd9bdb83..b3150780 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -134,11 +134,6 @@ public class DroidGap extends Activity { root.addView(appView); - // Try firing the onNativeReady event in JS. If it fails because the JS is - // not loaded yet then just set a flag so that the onNativeReady can be fired - // from the JS side when the JS gets to that code. - appView.loadUrl("javascript:try{PhoneGap.onNativeReady.fire();}catch(e){_nativeReady = true;}"); - setContentView(root); }