Starting the Buttons Branch

This commit is contained in:
Joe Bowser 2012-06-18 11:31:53 -07:00
parent 451afabfbb
commit 40b9810a63
3 changed files with 97 additions and 83 deletions

View File

@ -188,7 +188,7 @@ public class App extends Plugin {
*/ */
public void overrideBackbutton(boolean override) { public void overrideBackbutton(boolean override) {
LOG.i("App", "WARNING: Back Button Default Behaviour will be overridden. The backbutton event will be fired!"); LOG.i("App", "WARNING: Back Button Default Behaviour will be overridden. The backbutton event will be fired!");
this.ctx.bindBackButton(override); webView.bindButton(override);
} }
/** /**
@ -200,7 +200,7 @@ public class App extends Plugin {
*/ */
public void overrideButton(String button, boolean override) { public void overrideButton(String button, boolean override) {
LOG.i("DroidGap", "WARNING: Volume Button Default Behaviour will be overridden. The volume event will be fired!"); LOG.i("DroidGap", "WARNING: Volume Button Default Behaviour will be overridden. The volume event will be fired!");
((DroidGap)this.ctx).bindButton(button, override); webView.bindButton(button, override);
} }
/** /**
* Return whether the Android back button is overridden by the user. * Return whether the Android back button is overridden by the user.

View File

@ -39,6 +39,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager; import android.view.WindowManager;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
@ -70,6 +71,12 @@ public class CordovaWebView extends WebView {
// Flag to track that a loadUrl timeout occurred // Flag to track that a loadUrl timeout occurred
int loadUrlTimeout = 0; int loadUrlTimeout = 0;
private boolean bound;
private boolean volumedownBound;
private boolean volumeupBound;
/** /**
* Constructor. * Constructor.
* *
@ -660,4 +667,91 @@ public class CordovaWebView extends WebView {
} }
return p.toString(); return p.toString();
} }
/*
* onKeyDown
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// If volumedown key
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (this.volumedownBound==true) {
// only override default behaviour is event bound
LOG.d(TAG, "Down Key Hit");
this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
return true;
}
}
// If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (this.volumeupBound==true) {
// only override default behaviour is event bound
LOG.d(TAG, "Up Key Hit");
this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
return true;
}
}
return false;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
Log.d(TAG, "KeyDown has been triggered on the view");
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
// If back key is bound, then send event to JavaScript
if (this.bound) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
} else {
// If not bound
// Go to previous page in webview if it is possible to go back
if (this.backHistory()) {
return true;
}
// If not, then invoke default behaviour
else {
//this.activityState = ACTIVITY_EXITING;
return false;
}
}
}
// If menu key
else if (keyCode == KeyEvent.KEYCODE_MENU) {
this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
return super.onKeyUp(keyCode, event);
}
// If search key
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
return true;
}
Log.d(TAG, "KeyUp has been triggered on the view");
return false;
}
public void bindButton(boolean override)
{
this.bound = override;
}
public void bindButton(String button, boolean override) {
// TODO Auto-generated method stub
if (button.compareTo("volumeup")==0) {
this.volumeupBound = override;
}
else if (button.compareTo("volumedown")==0) {
this.volumedownBound = override;
}
}
} }

View File

@ -784,86 +784,6 @@ public class DroidGap extends Activity implements CordovaInterface {
super.finish(); super.finish();
} }
/**
* Called when a key is released. (Key UP)
*
* @param keyCode
* @param event
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (this.appView == null) {
return super.onKeyUp(keyCode, event);
}
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
// If back key is bound, then send event to JavaScript
if (this.bound) {
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
} else {
// If not bound
// Go to previous page in webview if it is possible to go back
if (this.backHistory()) {
return true;
}
// If not, then invoke behavior of super class
else {
this.activityState = ACTIVITY_EXITING;
return super.onKeyUp(keyCode, event);
}
}
}
// If menu key
else if (keyCode == KeyEvent.KEYCODE_MENU) {
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
return super.onKeyUp(keyCode, event);
}
// If search key
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
return true;
}
return false;
}
/**
* Called when a key is pressed. (Key DOWN)
*
* @param keyCode
* @param event
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (this.appView == null) {
return super.onKeyDown(keyCode, event);
}
// If volumedown key
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (this.volumedownBound==true) {
// only override default behaviour is event bound
LOG.d(TAG, "Down Key Hit");
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
return true;
}
}
// If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (this.volumeupBound==true) {
// only override default behaviour is event bound
LOG.d(TAG, "Up Key Hit");
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/** /**
* Launch an activity for which you would like a result when it finished. When this activity exits, * Launch an activity for which you would like a result when it finished. When this activity exits,