From 4c5f8203166608dd8f9463c8b0f2156b09a41626 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 8 Jun 2010 15:55:57 -0700 Subject: [PATCH] Hacking together a work-around for the default back behaviour --- framework/assets/js/device.js | 26 ++++++++++++ framework/assets/js/keyevent.js | 11 +---- framework/src/com/phonegap/DroidGap.java | 51 +++++++++++++----------- 3 files changed, 56 insertions(+), 32 deletions(-) diff --git a/framework/assets/js/device.js b/framework/assets/js/device.js index d6be6016..4a786a8d 100644 --- a/framework/assets/js/device.js +++ b/framework/assets/js/device.js @@ -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(); }); diff --git a/framework/assets/js/keyevent.js b/framework/assets/js/keyevent.js index 26f789f1..9b72ac0c 100644 --- a/framework/assets/js/keyevent.js +++ b/framework/assets/js/keyevent.js @@ -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); } diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index b0ac1cd2..6a746e72 100644 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -63,6 +63,7 @@ public class DroidGap extends Activity { private CompassListener mCompass; private Storage cupcakeStorage; private CryptoHandler crypto; + private BrowserKey mKey; /** Called when the activity is first created. */ @Override @@ -142,6 +143,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"); @@ -153,6 +155,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.")) @@ -211,6 +214,8 @@ public class DroidGap extends Activity { } else { + //We clear the back button state + mKey.reset(); view.loadUrl(url); return false; } @@ -301,43 +306,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; }