Hacking together a work-around for the default back behaviour

This commit is contained in:
Joe Bowser 2010-06-08 15:55:57 -07:00
parent b65a0b1bd6
commit bb501de22c
3 changed files with 56 additions and 32 deletions

View File

@ -28,6 +28,32 @@ function Device() {
}
}
/*
* You must explicitly override the back button.
*/
Device.prototype.overrideBackButton = function()
{
BackButton.override();
}
/*
* This resets the back button to the default behaviour
*/
Device.prototype.resetBackButton = function()
{
BackButton.reset();
}
/*
* This terminates the activity!
*/
Device.prototype.exitApp = function()
{
BackButton.exitApp();
}
PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device();
});

View File

@ -2,17 +2,10 @@ function KeyEvent()
{
}
KeyEvent.prototype.menuTrigger = function()
KeyEvent.prototype.backTrigger = function()
{
var e = document.createEvent('Events');
e.initEvent('menuKeyDown');
document.dispatchEvent(e);
}
KeyEvent.prototype.searchTrigger= function()
{
var e = document.createEvent('Events');
e.initEvent('searchKeyDown');
e.initEvent('backKeyDown');
document.dispatchEvent(e);
}

View File

@ -69,6 +69,7 @@ public class DroidGap extends Activity {
private CompassListener mCompass;
private Storage cupcakeStorage;
private CryptoHandler crypto;
private BrowserKey mKey;
private Uri imageUri;
@ -150,6 +151,7 @@ public class DroidGap extends Activity {
netMan = new NetworkManager(appView, this);
mCompass = new CompassListener(appView, this);
crypto = new CryptoHandler(appView);
mKey = new BrowserKey(appView, this);
// This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(gap, "DroidGap");
@ -161,6 +163,7 @@ public class DroidGap extends Activity {
appView.addJavascriptInterface(netMan, "NetworkManager");
appView.addJavascriptInterface(mCompass, "CompassHook");
appView.addJavascriptInterface(crypto, "GapCrypto");
appView.addJavascriptInterface(mKey, "BackButton");
if (android.os.Build.VERSION.RELEASE.startsWith("1."))
@ -219,6 +222,8 @@ public class DroidGap extends Activity {
}
else
{
//We clear the back button state
mKey.reset();
view.loadUrl(url);
return false;
}
@ -309,43 +314,43 @@ public class DroidGap extends Activity {
quotaUpdater.updateQuota(currentQuota);
}
}
// This is a test of console.log, because we don't have this in Android 2.01
public void addMessageToConsole(String message, int lineNumber, String sourceID)
{
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
}
// console.log in api level 7: http://developer.android.com/guide/developing/debug-tasks.html
public void onConsoleMessage(String message, int lineNumber, String sourceID)
{
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
}
public void onConsoleMessage(String message, int lineNumber, String sourceID)
{
// This is a kludgy hack!!!!
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
}
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
String testUrl = appView.getUrl();
appView.goBack();
if(appView.getUrl() == testUrl)
{
return super.onKeyDown(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mKey.isBound())
{
//We fire an event here!
appView.loadUrl("javascript:document.keyEvent.backTrigger()");
}
else
{
String testUrl = appView.getUrl();
appView.goBack();
if(appView.getUrl() == testUrl)
{
return super.onKeyDown(keyCode, event);
}
}
}
if (keyCode == KeyEvent.KEYCODE_MENU)
{
// This is where we launch the menu
appView.loadUrl("javascript:keyEvent.menuTrigger()");
}
if (keyCode == KeyEvent.KEYCODE_SEARCH)
{
appView.loadUrl("javascript:keyEvent.searchTrigger()");
}
return false;
}