Updated tweaks to get up and running

This commit is contained in:
Joe Bowser 2012-04-12 14:18:06 -07:00
parent 1794f2e047
commit f2526bbc78
6 changed files with 94 additions and 71 deletions

View File

@ -22,6 +22,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
@ -44,7 +45,7 @@ public class CordovaChromeClient extends WebChromeClient {
private String TAG = "CordovaLog";
private long MAX_QUOTA = 100 * 1024 * 1024;
private DroidGap ctx;
private Activity ctx;
private CordovaWebView appView;
/**
@ -53,16 +54,18 @@ public class CordovaChromeClient extends WebChromeClient {
* @param ctx
*/
public CordovaChromeClient(Context ctx) {
this.ctx = (DroidGap) ctx;
appView = this.ctx.appView;
this.ctx = (Activity) ctx;
//appView = this.ctx.appView;
}
public CordovaChromeClient(Context ctx, CordovaWebView app)
{
this.ctx = (DroidGap) ctx;
this.ctx = (Activity) ctx;
appView = app;
}
/**
* Tell the client to display a javascript alert dialog.
*
@ -176,7 +179,7 @@ public class CordovaChromeClient extends WebChromeClient {
// 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://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) {
if (url.startsWith("file://") || url.indexOf(appView.baseUrl) == 0 || appView.isUrlWhiteListed(url)) {
reqOk = true;
}
@ -224,8 +227,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:")) {
ctx.appView.setVisibility(View.VISIBLE);
ctx.spinnerStop();
appView.setVisibility(View.VISIBLE);
//ctx.spinnerStop();
result.confirm("OK");
}

View File

@ -42,7 +42,7 @@ public class CordovaWebView extends WebView {
//This is for the polyfil history
private String url;
private String baseUrl;
String baseUrl;
private Stack<String> urls = new Stack<String>();
protected int loadUrlTimeout;
@ -274,9 +274,32 @@ public class CordovaWebView extends WebView {
public void loadUrl(String url)
{
if (!url.startsWith("javascript:")) {
this.url = url;
if (this.baseUrl == null) {
int i = url.lastIndexOf('/');
if (i > 0) {
this.baseUrl = url.substring(0, i+1);
}
else {
this.baseUrl = this.url + "/";
}
}
// Create callback server and plugin manager
if (callbackServer == null) {
callbackServer = new CallbackServer();
callbackServer.init(url);
}
else {
callbackServer.reinit(url);
}
pluginManager.init();
this.urls.push(url);
}
super.loadUrl(url);
}
@ -287,4 +310,24 @@ public class CordovaWebView extends WebView {
public void postMessage(String id, String data) {
pluginManager.postMessage(id, data);
}
/**
* Returns the top url on the stack without removing it from
* the stack.
*/
public String peekAtUrlStack() {
if (urls.size() > 0) {
return urls.peek();
}
return "";
}
/**
* Add a url to the stack
*
* @param url
*/
public void pushUrl(String url) {
urls.push(url);
}
}

View File

@ -20,6 +20,7 @@ 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;
@ -41,7 +42,7 @@ import android.webkit.WebViewClient;
public class CordovaWebViewClient extends WebViewClient {
private static final String TAG = "Cordova";
DroidGap ctx;
Activity ctx;
CordovaWebView appView;
private boolean doClearHistory = false;
@ -50,14 +51,13 @@ public class CordovaWebViewClient extends WebViewClient {
*
* @param ctx
*/
public CordovaWebViewClient(DroidGap ctx) {
public CordovaWebViewClient(Activity ctx) {
this.ctx = ctx;
appView = ctx.appView;
}
public CordovaWebViewClient(Context ctx, CordovaWebView view)
{
this.ctx = (DroidGap) ctx;
this.ctx = (Activity) ctx;
appView = view;
}
@ -146,8 +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)) {
this.ctx.loadUrl(url);
if (url.startsWith("file://") || url.indexOf(appView.baseUrl) == 0 || appView.isUrlWhiteListed(url)) {
appView.loadUrl(url);
}
// If not our application, let default viewer handle
@ -221,26 +221,26 @@ public class CordovaWebViewClient extends WebViewClient {
}
// Clear timeout flag
this.ctx.loadUrlTimeout++;
appView.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")) {
ctx.appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
this.ctx.postMessage("onNativeReady", null);
appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
appView.postMessage("onNativeReady", null);
}
// Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly
if (ctx.appView.getVisibility() == View.INVISIBLE) {
if (appView.getVisibility() == View.INVISIBLE) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
ctx.runOnUiThread(new Runnable() {
public void run() {
ctx.appView.setVisibility(View.VISIBLE);
ctx.spinnerStop();
appView.setVisibility(View.VISIBLE);
//ctx.spinnerStop();
}
});
} catch (InterruptedException e) {
@ -256,7 +256,8 @@ public class CordovaWebViewClient extends WebViewClient {
if (appView.callbackServer != null) {
appView.callbackServer.destroy();
}
this.ctx.endActivity();
//this.ctx.endActivity();
this.ctx.finish();
}
}
@ -274,13 +275,13 @@ public class CordovaWebViewClient extends WebViewClient {
LOG.d(TAG, "DroidGap: GapViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl);
// Clear timeout flag
this.ctx.loadUrlTimeout++;
//this.ctx.loadUrlTimeout++;
// Stop "app loading" spinner if showing
this.ctx.spinnerStop();
//this.ctx.spinnerStop();
// Handle error
this.ctx.onReceivedError(errorCode, description, failingUrl);
//this.ctx.onReceivedError(errorCode, description, failingUrl);
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
@ -310,8 +311,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.ctx.peekAtUrlStack().equals(url)) {
this.ctx.pushUrl(url);
if (!appView.peekAtUrlStack().equals(url)) {
appView.pushUrl(url);
}
}
}

View File

@ -346,16 +346,6 @@ public class DroidGap extends Activity implements CordovaInterface {
LOG.d(TAG, "DroidGap.loadUrl(%s)", url);
}
this.url = url;
if (this.baseUrl == null) {
int i = url.lastIndexOf('/');
if (i > 0) {
this.baseUrl = url.substring(0, i+1);
}
else {
this.baseUrl = this.url + "/";
}
}
if (!url.startsWith("javascript:")) {
LOG.d(TAG, "DroidGap: url=%s baseUrl=%s", url, baseUrl);
}
@ -370,24 +360,12 @@ public class DroidGap extends Activity implements CordovaInterface {
me.init();
}
// Handle activity parameters
// Handle activity parameters (TODO: Somehow abstract this)
me.handleActivityParameters();
// Track URLs loaded instead of using appView history
me.urls.push(url);
me.appView.clearHistory();
// Create callback server and plugin manager
if (appView.callbackServer == null) {
appView.callbackServer = new CallbackServer();
appView.callbackServer.init(url);
}
else {
appView.callbackServer.reinit(url);
}
appView.pluginManager.init();
// If loadingDialog property, then show the App loading dialog for first page of app
// (This doesn't seem to actually do anything here)
/*
String loading = null;
if (me.urls.size() == 1) {
loading = me.getStringProperty("loadingDialog", null);
@ -413,6 +391,7 @@ public class DroidGap extends Activity implements CordovaInterface {
}
me.spinnerStart(title, message);
}
*/
// Create a timeout timer for loadUrl
final int currentLoadUrlTimeout = me.loadUrlTimeout;
@ -1184,25 +1163,7 @@ public class DroidGap extends Activity implements CordovaInterface {
* URL stack manipulators
*/
/**
* Returns the top url on the stack without removing it from
* the stack.
*/
public String peekAtUrlStack() {
if (urls.size() > 0) {
return urls.peek();
}
return "";
}
/**
* Add a url to the stack
*
* @param url
*/
public void pushUrl(String url) {
urls.push(url);
}
/*
* Hook in DroidGap for menu plugins

View File

@ -32,6 +32,8 @@ import android.content.res.Resources;
import android.database.Cursor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuItem;
/**
@ -64,13 +66,26 @@ public interface CordovaInterface {
*/
public abstract void bindBackButton(boolean override);
/**
* A hook required to check if the Back Button is bound
* @return
*/
public abstract boolean isBackButtonBound();
/*
* Hook in DroidGap for menu plugins
* (This is in the Android SDK, do we need this on the Interface?)
*/
public abstract boolean onCreateOptionsMenu(Menu menu);
public abstract boolean onPrepareOptionsMenu(Menu menu);
public abstract boolean onOptionsItemSelected(MenuItem item);
/**
* @deprecated
* Add services to res/xml/plugins.xml instead.

View File

@ -69,7 +69,7 @@ public class ActivityPlugin extends Plugin {
public void startActivity(String className) {
try {
Intent intent = new Intent().setClass(this.ctx.getContext(), Class.forName(className));
Intent intent = new Intent().setClass(this.ctx, Class.forName(className));
LOG.d(TAG, "Starting activity %s", className);
this.ctx.startActivity(intent);
} catch (ClassNotFoundException e) {