Starting to move the history into the CordovaWebView, and getting the WebDriver working again

This commit is contained in:
Joe Bowser 2012-04-23 16:32:59 -07:00
parent 483bb53d9c
commit 99b3693f40
7 changed files with 90 additions and 41 deletions

View File

@ -161,7 +161,7 @@ public class App extends Plugin {
* Clear page history for the app. * Clear page history for the app.
*/ */
public void clearHistory() { public void clearHistory() {
((DroidGap)this.ctx).clearHistory(); webView.clearHistory();
} }
/** /**
@ -169,7 +169,7 @@ public class App extends Plugin {
* This is the same as pressing the backbutton on Android device. * This is the same as pressing the backbutton on Android device.
*/ */
public void backHistory() { public void backHistory() {
((DroidGap)this.ctx).backHistory(); webView.backHistory();
} }
/** /**

View File

@ -64,7 +64,10 @@ public class CordovaChromeClient extends WebChromeClient {
appView = app; appView = app;
} }
public void setWebView(CordovaWebView view)
{
appView = view;
}
/** /**
* Tell the client to display a javascript alert dialog. * Tell the client to display a javascript alert dialog.

View File

@ -270,6 +270,11 @@ public class CordovaWebView extends WebView {
return false; return false;
} }
/**
* We override loadUrl so that we can track the back history
* @see android.webkit.WebView#loadUrl(java.lang.String)
*/
@Override @Override
public void loadUrl(String url) public void loadUrl(String url)
{ {
@ -283,26 +288,54 @@ public class CordovaWebView extends WebView {
else { else {
this.baseUrl = this.url + "/"; 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();
// Create callback server and plugin manager this.urls.push(url);
if (callbackServer == null) {
callbackServer = new CallbackServer();
callbackServer.init(url);
} }
else {
callbackServer.reinit(url);
}
pluginManager.init();
this.urls.push(url);
} }
super.loadUrl(url); super.loadUrl(url);
} }
public void loadUrl(final String url, final int time)
{
// If not first page of app, then load immediately
if (this.urls.size() > 0) {
this.loadUrl(url);
}
if (!url.startsWith("javascript:")) {
LOG.d(TAG, "DroidGap.loadUrl(%s, %d)", url, time);
}
final CordovaWebView me = this;
Runnable runnable = new Runnable() {
public void run() {
try {
synchronized(this) {
this.wait(time);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//I'm pretty sure this has to be on the UI thread
me.loadUrl(url);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void sendJavascript(String statement) { public void sendJavascript(String statement) {
callbackServer.sendJavascript(statement); callbackServer.sendJavascript(statement);
} }
@ -330,4 +363,29 @@ public class CordovaWebView extends WebView {
public void pushUrl(String url) { public void pushUrl(String url) {
urls.push(url); urls.push(url);
} }
/**
* Go to previous page in history. (We manage our own history)
*
* @return true if we went back, false if we are already at top
*/
public boolean backHistory() {
// Check webview first to see if there is a history
// This is needed to support curPage#diffLink, since they are added to appView's history, but not our history url array (JQMobile behavior)
if (this.canGoBack()) {
this.goBack();
return true;
}
// If our managed history has prev url
if (this.urls.size() > 1) {
this.urls.pop(); // Pop current url
String url = this.urls.pop(); // Pop prev url that we want to load, since it will be added back by loadUrl()
loadUrl(url);
return true;
}
return false;
}
} }

View File

@ -61,6 +61,11 @@ public class CordovaWebViewClient extends WebViewClient {
appView = view; appView = view;
} }
public void setWebView(CordovaWebView view)
{
appView = view;
}
/** /**
* Give the host application a chance to take over the control when a new url * Give the host application a chance to take over the control when a new url
* is about to be loaded in the current WebView. * is about to be loaded in the current WebView.

View File

@ -531,30 +531,7 @@ public class DroidGap extends Activity implements CordovaInterface {
} }
} }
/**
* Go to previous page in history. (We manage our own history)
*
* @return true if we went back, false if we are already at top
*/
public boolean backHistory() {
// Check webview first to see if there is a history
// This is needed to support curPage#diffLink, since they are added to appView's history, but not our history url array (JQMobile behavior)
if (this.appView.canGoBack()) {
this.appView.goBack();
return true;
}
// If our managed history has prev url
if (this.urls.size() > 1) {
this.urls.pop(); // Pop current url
String url = this.urls.pop(); // Pop prev url that we want to load, since it will be added back by loadUrl()
this.loadUrl(url);
return true;
}
return false;
}
@Override @Override
/** /**
@ -1208,4 +1185,8 @@ public class DroidGap extends Activity implements CordovaInterface {
return this.bound; return this.bound;
} }
public boolean backHistory() {
return appView.backHistory();
}
} }

View File

@ -14,7 +14,7 @@ public class PhoneGapSplash extends Activity {
setContentView(R.layout.main); setContentView(R.layout.main);
phoneGap = (CordovaWebView) findViewById(R.id.phoneGapView); phoneGap = (CordovaWebView) findViewById(R.id.phoneGapView);
//phoneGap.loadUrl("file:///android_asset/index.html", 5000); phoneGap.loadUrl("file:///android_asset/index.html", 5000);
} }
public void onDestroy() public void onDestroy()

View File

@ -34,6 +34,8 @@ public class WebDriverTest extends ActivityInstrumentationTestCase2<CordovaDrive
viewHandler = new CordovaWebViewClient(testActivity); viewHandler = new CordovaWebViewClient(testActivity);
testDriver = new AndroidWebDriver(testActivity, viewFactory, viewHandler, appCode); testDriver = new AndroidWebDriver(testActivity, viewFactory, viewHandler, appCode);
testView = (CordovaWebView) testDriver.getWebView(); testView = (CordovaWebView) testDriver.getWebView();
viewHandler.setWebView(testView);
appCode.setWebView(testView);
} }
public void testPreconditions(){ public void testPreconditions(){