From e77f9bb8fc4dacecf8b17e8dac6b040dc0b11a69 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Wed, 11 Apr 2012 11:44:53 -0700 Subject: [PATCH] Rolling back half-baked change that broke the code in the branch, we need to rethink the Callback Server --- .../apache/cordova/CordovaChromeClient.java | 31 +++------ .../org/apache/cordova/CordovaWebView.java | 33 +-------- .../apache/cordova/CordovaWebViewClient.java | 67 +++++++------------ .../src/org/apache/cordova/DroidGap.java | 16 ++--- 4 files changed, 47 insertions(+), 100 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java index 95c8bc9d..49ec1b5d 100755 --- a/framework/src/org/apache/cordova/CordovaChromeClient.java +++ b/framework/src/org/apache/cordova/CordovaChromeClient.java @@ -44,7 +44,7 @@ public class CordovaChromeClient extends WebChromeClient { private String TAG = "CordovaLog"; private long MAX_QUOTA = 100 * 1024 * 1024; - private Context ctx; + private DroidGap ctx; private CordovaWebView appView; /** @@ -53,7 +53,8 @@ public class CordovaChromeClient extends WebChromeClient { * @param ctx */ public CordovaChromeClient(Context ctx) { - this.ctx = ctx; + this.ctx = (DroidGap) ctx; + appView = this.ctx.appView; } public CordovaChromeClient(Context ctx, CordovaWebView app) @@ -172,15 +173,10 @@ public class CordovaChromeClient extends WebChromeClient { @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { - if(appView == null) - { - appView = (CordovaWebView) view; - } - // Security check to make sure any requests are coming from the page initially // loaded in webview and not another loaded in an iframe. boolean reqOk = false; - if (url.startsWith("file://") || appView.isUrlWhiteListed(url)) { + if (url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) { reqOk = true; } @@ -203,9 +199,7 @@ public class CordovaChromeClient extends WebChromeClient { // Polling for JavaScript messages else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) { - - - String r = appView.callbackServer.getJavascript(); + String r = ctx.callbackServer.getJavascript(); result.confirm(r); } @@ -213,16 +207,16 @@ public class CordovaChromeClient extends WebChromeClient { else if (reqOk && defaultValue != null && defaultValue.equals("gap_callbackServer:")) { String r = ""; if (message.equals("usePolling")) { - r = ""+ appView.callbackServer.usePolling(); + r = ""+ ctx.callbackServer.usePolling(); } else if (message.equals("restartServer")) { - appView.callbackServer.restartServer(); + ctx.callbackServer.restartServer(); } else if (message.equals("getPort")) { - r = Integer.toString(appView.callbackServer.getPort()); + r = Integer.toString(ctx.callbackServer.getPort()); } else if (message.equals("getToken")) { - r = appView.callbackServer.getToken(); + r = ctx.callbackServer.getToken(); } result.confirm(r); } @@ -230,11 +224,8 @@ public class CordovaChromeClient extends WebChromeClient { // Cordova JS has initialized, so show webview // (This solves white flash seen when rendering HTML) else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) { - if (ctx.getClass().equals(DroidGap.class) && ((DroidGap) ctx).splashscreen != 0) { - ((DroidGap) ctx).root.setBackgroundResource(0); - ((DroidGap) ctx).spinnerStop(); - } - appView.setVisibility(View.VISIBLE); + ctx.appView.setVisibility(View.VISIBLE); + ctx.spinnerStop(); result.confirm("OK"); } diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index fad4ffea..aaa11eda 100644 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -15,11 +15,9 @@ import org.xmlpull.v1.XmlPullParserException; import android.content.Context; import android.content.res.XmlResourceParser; import android.util.AttributeSet; -import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebSettings.LayoutAlgorithm; -import android.webkit.WebViewClient; public class CordovaWebView extends WebView { @@ -32,8 +30,7 @@ public class CordovaWebView extends WebView { private ArrayList whiteList = new ArrayList(); private HashMap whiteListCache = new HashMap(); protected PluginManager pluginManager; - public CallbackServer callbackServer; - + /** Actvities and other important classes **/ private Context mCtx; private CordovaWebViewClient viewClient; @@ -42,7 +39,7 @@ public class CordovaWebView extends WebView { public CordovaWebView(Context context) { super(context); mCtx = context; - //setup(); + setup(); } public CordovaWebView(Context context, AttributeSet attrs) { @@ -64,7 +61,7 @@ public class CordovaWebView extends WebView { setup(); } - public void setup() + private void setup() { this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); @@ -246,28 +243,4 @@ public class CordovaWebView extends WebView { return false; } - @Override - public void setWebViewClient(WebViewClient client) { - if(client.getClass().equals(CordovaWebView.class)) { - viewClient = (CordovaWebViewClient) client; - super.setWebViewClient(viewClient); - } - else - { - //This should throw an exception! - } - } - - @Override - public void setWebChromeClient(WebChromeClient client) { - if(client.getClass().equals(CordovaWebView.class)) { - chromeClient = (CordovaChromeClient) client; - super.setWebChromeClient(chromeClient); - } - else - { - //This should throw an exception! - } - } - } diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java index b7fdd0ea..1daf7597 100755 --- a/framework/src/org/apache/cordova/CordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java @@ -20,7 +20,6 @@ package org.apache.cordova; import org.apache.cordova.api.LOG; -import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; @@ -42,8 +41,7 @@ import android.webkit.WebViewClient; public class CordovaWebViewClient extends WebViewClient { private static final String TAG = "Cordova"; - Context ctx; - DroidGap droidGap; + DroidGap ctx; CordovaWebView appView; private boolean doClearHistory = false; @@ -52,16 +50,14 @@ public class CordovaWebViewClient extends WebViewClient { * * @param ctx */ - public CordovaWebViewClient(Context ctx) { + public CordovaWebViewClient(DroidGap ctx) { this.ctx = ctx; - //appView = ctx.appView; + appView = ctx.appView; } public CordovaWebViewClient(Context ctx, CordovaWebView view) { - this.ctx = ctx; - if(ctx.getClass().equals(DroidGap.class)) - this.droidGap = (DroidGap) ctx; + this.ctx = (DroidGap) ctx; appView = view; } @@ -150,9 +146,8 @@ public class CordovaWebViewClient extends WebViewClient { // If our app or file:, then load into a new Cordova webview container by starting a new instance of our activity. // Our app continues to run. When BACK is pressed, our app is redisplayed. - //if (url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) { - if (url.startsWith("file://") || appView.isUrlWhiteListed(url)) { - appView.loadUrl(url); + if (url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) { + this.ctx.loadUrl(url); } // If not our application, let default viewer handle @@ -226,35 +221,28 @@ public class CordovaWebViewClient extends WebViewClient { } // Clear timeout flag - //this.ctx.loadUrlTimeout++; + this.ctx.loadUrlTimeout++; // 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. if (!url.equals("about:blank")) { - appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}"); - //appView.postMessage("onNativeReady", null); + ctx.appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}"); + this.ctx.postMessage("onNativeReady", null); } // Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly - if (appView.getVisibility() == View.INVISIBLE) { + if (ctx.appView.getVisibility() == View.INVISIBLE) { Thread t = new Thread(new Runnable() { public void run() { try { Thread.sleep(2000); - ((Activity) ctx).runOnUiThread(new Runnable() { + ctx.runOnUiThread(new Runnable() { public void run() { - if(droidGap != null) - { - if (droidGap.splashscreen != 0) { - droidGap.root.setBackgroundResource(0); - } - - appView.setVisibility(View.VISIBLE); - - } + ctx.appView.setVisibility(View.VISIBLE); + ctx.spinnerStop(); } - }); + }); } catch (InterruptedException e) { } } @@ -265,11 +253,10 @@ public class CordovaWebViewClient extends WebViewClient { // Shutdown if blank loaded if (url.equals("about:blank")) { - if (appView.callbackServer != null) { - appView.callbackServer.destroy(); + if (this.ctx.callbackServer != null) { + this.ctx.callbackServer.destroy(); } - if(droidGap != null) - droidGap.endActivity(); + this.ctx.endActivity(); } } @@ -286,17 +273,14 @@ public class CordovaWebViewClient extends WebViewClient { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { LOG.d(TAG, "DroidGap: GapViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl); - if(droidGap != null) - { - // Clear timeout flag - this.droidGap.loadUrlTimeout++; + // Clear timeout flag + this.ctx.loadUrlTimeout++; - // Stop "app loading" spinner if showing - this.droidGap.spinnerStop(); + // Stop "app loading" spinner if showing + this.ctx.spinnerStop(); - // Handle error - this.droidGap.onReceivedError(errorCode, description, failingUrl); - } + // Handle error + this.ctx.onReceivedError(errorCode, description, failingUrl); } public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { @@ -326,9 +310,8 @@ public class CordovaWebViewClient extends WebViewClient { * If you do a document.location.href the url does not get pushed on the stack * so we do a check here to see if the url should be pushed. */ - - if (this.droidGap != null && !this.droidGap.peekAtUrlStack().equals(url)) { - droidGap.pushUrl(url); + if (!this.ctx.peekAtUrlStack().equals(url)) { + this.ctx.pushUrl(url); } } } diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index f91e5a48..5f9e3bd9 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -1,5 +1,5 @@ /* -f Licensed to the Apache Software Foundation (ASF) under one + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file @@ -155,6 +155,7 @@ public class DroidGap extends Activity implements CordovaInterface { protected LinearLayout root; public boolean bound = false; + public CallbackServer callbackServer; protected boolean cancelLoadUrl = false; protected ProgressDialog spinnerDialog = null; @@ -277,7 +278,6 @@ public class DroidGap extends Activity implements CordovaInterface { // white list of allowed URLs // debug setting this.loadConfiguration(); - this.appView.setup(); this.appView.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, @@ -379,12 +379,12 @@ public class DroidGap extends Activity implements CordovaInterface { me.appView.clearHistory(); // Create callback server and plugin manager - if (me.appView.callbackServer == null) { - me.appView.callbackServer = new CallbackServer(); - me.appView.callbackServer.init(url); + if (me.callbackServer == null) { + me.callbackServer = new CallbackServer(); + me.callbackServer.init(url); } else { - me.appView.callbackServer.reinit(url); + me.callbackServer.reinit(url); } appView.pluginManager.init(); @@ -832,8 +832,8 @@ public class DroidGap extends Activity implements CordovaInterface { */ public void sendJavascript(String statement) { //We need to check for the null case on the Kindle Fire beacuse it changes the width and height on load - if(this.appView.callbackServer != null) - this.appView.callbackServer.sendJavascript(statement); + if(this.callbackServer != null) + this.callbackServer.sendJavascript(statement); } /**