mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Updated tweaks to get up and running
This commit is contained in:
parent
1794f2e047
commit
f2526bbc78
@ -22,6 +22,7 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
@ -44,7 +45,7 @@ public class CordovaChromeClient extends WebChromeClient {
|
|||||||
|
|
||||||
private String TAG = "CordovaLog";
|
private String TAG = "CordovaLog";
|
||||||
private long MAX_QUOTA = 100 * 1024 * 1024;
|
private long MAX_QUOTA = 100 * 1024 * 1024;
|
||||||
private DroidGap ctx;
|
private Activity ctx;
|
||||||
private CordovaWebView appView;
|
private CordovaWebView appView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,16 +54,18 @@ public class CordovaChromeClient extends WebChromeClient {
|
|||||||
* @param ctx
|
* @param ctx
|
||||||
*/
|
*/
|
||||||
public CordovaChromeClient(Context ctx) {
|
public CordovaChromeClient(Context ctx) {
|
||||||
this.ctx = (DroidGap) ctx;
|
this.ctx = (Activity) ctx;
|
||||||
appView = this.ctx.appView;
|
//appView = this.ctx.appView;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CordovaChromeClient(Context ctx, CordovaWebView app)
|
public CordovaChromeClient(Context ctx, CordovaWebView app)
|
||||||
{
|
{
|
||||||
this.ctx = (DroidGap) ctx;
|
this.ctx = (Activity) ctx;
|
||||||
appView = app;
|
appView = app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tell the client to display a javascript alert dialog.
|
* 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
|
// Security check to make sure any requests are coming from the page initially
|
||||||
// loaded in webview and not another loaded in an iframe.
|
// loaded in webview and not another loaded in an iframe.
|
||||||
boolean reqOk = false;
|
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;
|
reqOk = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,8 +227,8 @@ public class CordovaChromeClient extends WebChromeClient {
|
|||||||
// Cordova JS has initialized, so show webview
|
// Cordova JS has initialized, so show webview
|
||||||
// (This solves white flash seen when rendering HTML)
|
// (This solves white flash seen when rendering HTML)
|
||||||
else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) {
|
else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) {
|
||||||
ctx.appView.setVisibility(View.VISIBLE);
|
appView.setVisibility(View.VISIBLE);
|
||||||
ctx.spinnerStop();
|
//ctx.spinnerStop();
|
||||||
result.confirm("OK");
|
result.confirm("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ public class CordovaWebView extends WebView {
|
|||||||
|
|
||||||
//This is for the polyfil history
|
//This is for the polyfil history
|
||||||
private String url;
|
private String url;
|
||||||
private String baseUrl;
|
String baseUrl;
|
||||||
private Stack<String> urls = new Stack<String>();
|
private Stack<String> urls = new Stack<String>();
|
||||||
|
|
||||||
protected int loadUrlTimeout;
|
protected int loadUrlTimeout;
|
||||||
@ -274,9 +274,32 @@ public class CordovaWebView extends WebView {
|
|||||||
public void loadUrl(String url)
|
public void loadUrl(String url)
|
||||||
{
|
{
|
||||||
if (!url.startsWith("javascript:")) {
|
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);
|
this.urls.push(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
super.loadUrl(url);
|
super.loadUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,4 +310,24 @@ public class CordovaWebView extends WebView {
|
|||||||
public void postMessage(String id, String data) {
|
public void postMessage(String id, String data) {
|
||||||
pluginManager.postMessage(id, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package org.apache.cordova;
|
|||||||
|
|
||||||
import org.apache.cordova.api.LOG;
|
import org.apache.cordova.api.LOG;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
@ -41,7 +42,7 @@ import android.webkit.WebViewClient;
|
|||||||
public class CordovaWebViewClient extends WebViewClient {
|
public class CordovaWebViewClient extends WebViewClient {
|
||||||
|
|
||||||
private static final String TAG = "Cordova";
|
private static final String TAG = "Cordova";
|
||||||
DroidGap ctx;
|
Activity ctx;
|
||||||
CordovaWebView appView;
|
CordovaWebView appView;
|
||||||
private boolean doClearHistory = false;
|
private boolean doClearHistory = false;
|
||||||
|
|
||||||
@ -50,14 +51,13 @@ public class CordovaWebViewClient extends WebViewClient {
|
|||||||
*
|
*
|
||||||
* @param ctx
|
* @param ctx
|
||||||
*/
|
*/
|
||||||
public CordovaWebViewClient(DroidGap ctx) {
|
public CordovaWebViewClient(Activity ctx) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
appView = ctx.appView;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CordovaWebViewClient(Context ctx, CordovaWebView view)
|
public CordovaWebViewClient(Context ctx, CordovaWebView view)
|
||||||
{
|
{
|
||||||
this.ctx = (DroidGap) ctx;
|
this.ctx = (Activity) ctx;
|
||||||
appView = view;
|
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.
|
// 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.
|
// 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://") || url.indexOf(appView.baseUrl) == 0 || appView.isUrlWhiteListed(url)) {
|
||||||
this.ctx.loadUrl(url);
|
appView.loadUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not our application, let default viewer handle
|
// If not our application, let default viewer handle
|
||||||
@ -221,26 +221,26 @@ public class CordovaWebViewClient extends WebViewClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clear timeout flag
|
// Clear timeout flag
|
||||||
this.ctx.loadUrlTimeout++;
|
appView.loadUrlTimeout++;
|
||||||
|
|
||||||
// Try firing the onNativeReady event in JS. If it fails because the JS is
|
// 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
|
// 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.
|
// from the JS side when the JS gets to that code.
|
||||||
if (!url.equals("about:blank")) {
|
if (!url.equals("about:blank")) {
|
||||||
ctx.appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
|
appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
|
||||||
this.ctx.postMessage("onNativeReady", null);
|
appView.postMessage("onNativeReady", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly
|
// 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() {
|
Thread t = new Thread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
ctx.runOnUiThread(new Runnable() {
|
ctx.runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
ctx.appView.setVisibility(View.VISIBLE);
|
appView.setVisibility(View.VISIBLE);
|
||||||
ctx.spinnerStop();
|
//ctx.spinnerStop();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -256,7 +256,8 @@ public class CordovaWebViewClient extends WebViewClient {
|
|||||||
if (appView.callbackServer != null) {
|
if (appView.callbackServer != null) {
|
||||||
appView.callbackServer.destroy();
|
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);
|
LOG.d(TAG, "DroidGap: GapViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl);
|
||||||
|
|
||||||
// Clear timeout flag
|
// Clear timeout flag
|
||||||
this.ctx.loadUrlTimeout++;
|
//this.ctx.loadUrlTimeout++;
|
||||||
|
|
||||||
// Stop "app loading" spinner if showing
|
// Stop "app loading" spinner if showing
|
||||||
this.ctx.spinnerStop();
|
//this.ctx.spinnerStop();
|
||||||
|
|
||||||
// Handle error
|
// Handle error
|
||||||
this.ctx.onReceivedError(errorCode, description, failingUrl);
|
//this.ctx.onReceivedError(errorCode, description, failingUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
|
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
|
* 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.
|
* so we do a check here to see if the url should be pushed.
|
||||||
*/
|
*/
|
||||||
if (!this.ctx.peekAtUrlStack().equals(url)) {
|
if (!appView.peekAtUrlStack().equals(url)) {
|
||||||
this.ctx.pushUrl(url);
|
appView.pushUrl(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,16 +346,6 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
LOG.d(TAG, "DroidGap.loadUrl(%s)", url);
|
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:")) {
|
if (!url.startsWith("javascript:")) {
|
||||||
LOG.d(TAG, "DroidGap: url=%s baseUrl=%s", url, baseUrl);
|
LOG.d(TAG, "DroidGap: url=%s baseUrl=%s", url, baseUrl);
|
||||||
}
|
}
|
||||||
@ -370,24 +360,12 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
me.init();
|
me.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle activity parameters
|
// Handle activity parameters (TODO: Somehow abstract this)
|
||||||
me.handleActivityParameters();
|
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
|
// 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;
|
String loading = null;
|
||||||
if (me.urls.size() == 1) {
|
if (me.urls.size() == 1) {
|
||||||
loading = me.getStringProperty("loadingDialog", null);
|
loading = me.getStringProperty("loadingDialog", null);
|
||||||
@ -413,6 +391,7 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
}
|
}
|
||||||
me.spinnerStart(title, message);
|
me.spinnerStart(title, message);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Create a timeout timer for loadUrl
|
// Create a timeout timer for loadUrl
|
||||||
final int currentLoadUrlTimeout = me.loadUrlTimeout;
|
final int currentLoadUrlTimeout = me.loadUrlTimeout;
|
||||||
@ -1184,25 +1163,7 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
* URL stack manipulators
|
* 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
|
* Hook in DroidGap for menu plugins
|
||||||
|
@ -32,6 +32,8 @@ import android.content.res.Resources;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.hardware.SensorManager;
|
import android.hardware.SensorManager;
|
||||||
import android.net.Uri;
|
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);
|
public abstract void bindBackButton(boolean override);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hook required to check if the Back Button is bound
|
* A hook required to check if the Back Button is bound
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public abstract boolean isBackButtonBound();
|
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
|
* @deprecated
|
||||||
* Add services to res/xml/plugins.xml instead.
|
* Add services to res/xml/plugins.xml instead.
|
||||||
|
@ -69,7 +69,7 @@ public class ActivityPlugin extends Plugin {
|
|||||||
|
|
||||||
public void startActivity(String className) {
|
public void startActivity(String className) {
|
||||||
try {
|
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);
|
LOG.d(TAG, "Starting activity %s", className);
|
||||||
this.ctx.startActivity(intent);
|
this.ctx.startActivity(intent);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user