mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 15:12:51 +08:00
Hacking together a work-around for the default back behaviour
This commit is contained in:
parent
653190c6a1
commit
4c5f820316
@ -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();
|
||||
});
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user