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 653190c6a1
commit 4c5f820316
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() { PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device(); 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'); var e = document.createEvent('Events');
e.initEvent('menuKeyDown'); e.initEvent('backKeyDown');
document.dispatchEvent(e);
}
KeyEvent.prototype.searchTrigger= function()
{
var e = document.createEvent('Events');
e.initEvent('searchKeyDown');
document.dispatchEvent(e); document.dispatchEvent(e);
} }

View File

@ -63,6 +63,7 @@ public class DroidGap extends Activity {
private CompassListener mCompass; private CompassListener mCompass;
private Storage cupcakeStorage; private Storage cupcakeStorage;
private CryptoHandler crypto; private CryptoHandler crypto;
private BrowserKey mKey;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
@Override @Override
@ -142,6 +143,7 @@ public class DroidGap extends Activity {
netMan = new NetworkManager(appView, this); netMan = new NetworkManager(appView, this);
mCompass = new CompassListener(appView, this); mCompass = new CompassListener(appView, this);
crypto = new CryptoHandler(appView); crypto = new CryptoHandler(appView);
mKey = new BrowserKey(appView, this);
// This creates the new javascript interfaces for PhoneGap // This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(gap, "DroidGap"); appView.addJavascriptInterface(gap, "DroidGap");
@ -153,6 +155,7 @@ public class DroidGap extends Activity {
appView.addJavascriptInterface(netMan, "NetworkManager"); appView.addJavascriptInterface(netMan, "NetworkManager");
appView.addJavascriptInterface(mCompass, "CompassHook"); appView.addJavascriptInterface(mCompass, "CompassHook");
appView.addJavascriptInterface(crypto, "GapCrypto"); appView.addJavascriptInterface(crypto, "GapCrypto");
appView.addJavascriptInterface(mKey, "BackButton");
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) if (android.os.Build.VERSION.RELEASE.startsWith("1."))
@ -211,6 +214,8 @@ public class DroidGap extends Activity {
} }
else else
{ {
//We clear the back button state
mKey.reset();
view.loadUrl(url); view.loadUrl(url);
return false; return false;
} }
@ -301,43 +306,43 @@ public class DroidGap extends Activity {
quotaUpdater.updateQuota(currentQuota); 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 // console.log in api level 7: http://developer.android.com/guide/developing/debug-tasks.html
public void onConsoleMessage(String message, int lineNumber, String sourceID) public void onConsoleMessage(String message, int lineNumber, String sourceID)
{ {
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message); // This is a kludgy hack!!!!
} Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
}
} }
public boolean onKeyDown(int keyCode, KeyEvent event) public boolean onKeyDown(int keyCode, KeyEvent event)
{ {
if (keyCode == KeyEvent.KEYCODE_BACK) {
String testUrl = appView.getUrl(); if (keyCode == KeyEvent.KEYCODE_BACK) {
appView.goBack(); if (mKey.isBound())
if(appView.getUrl() == testUrl) {
{ //We fire an event here!
return super.onKeyDown(keyCode, event); 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) if (keyCode == KeyEvent.KEYCODE_MENU)
{ {
// This is where we launch the menu
appView.loadUrl("javascript:keyEvent.menuTrigger()"); appView.loadUrl("javascript:keyEvent.menuTrigger()");
} }
if (keyCode == KeyEvent.KEYCODE_SEARCH)
{
appView.loadUrl("javascript:keyEvent.searchTrigger()");
}
return false; return false;
} }