listening to volume events now override default behaviour

This commit is contained in:
Julien Bouquillon 2012-05-24 02:01:27 +02:00
parent 3b27d89c4a
commit 0c9295f8bc
3 changed files with 91 additions and 6 deletions

View File

@ -639,6 +639,9 @@ Channel.prototype.unsubscribe = function(g) {
if (handler) { if (handler) {
this.handlers[g] = null; this.handlers[g] = null;
delete this.handlers[g]; delete this.handlers[g];
}
// useful when same handler has been set multiple times and already deleted
if (this.numHandlers > 0) {
this.numHandlers--; this.numHandlers--;
if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this); if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this);
} }
@ -1047,6 +1050,28 @@ module.exports = {
cordova.addDocumentEventHandler('menubutton'); cordova.addDocumentEventHandler('menubutton');
cordova.addDocumentEventHandler('searchbutton'); cordova.addDocumentEventHandler('searchbutton');
function bindButtonChannel(buttonName) {
// generic button bind used for volumeup/volumedown buttons
return cordova.addDocumentEventHandler(buttonName + 'button', {
onSubscribe:function() {
// If we just attached the first handler, let native know we need to override the button.
if (this.numHandlers === 1) {
exec(null, null, "App", "overrideButton", [buttonName, true]);
}
},
onUnsubscribe:function() {
// If we just detached the last handler, let native know we no longer override the volumeup button.
if (this.numHandlers === 0) {
exec(null, null, "App", "overrideButton", [buttonName, false]);
}
}
});
}
// Inject a listener for the volume buttons on the document.
var volumeUpButtonChannel = bindButtonChannel('volumeup');
var volumeDownButtonChannel = bindButtonChannel('volumedown');
// Figure out if we need to shim-in localStorage and WebSQL // Figure out if we need to shim-in localStorage and WebSQL
// support from the native side. // support from the native side.
var storage = require('cordova/plugin/android/storage'); var storage = require('cordova/plugin/android/storage');
@ -3586,6 +3611,20 @@ module.exports = {
exec(null, null, "App", "overrideBackbutton", [override]); exec(null, null, "App", "overrideBackbutton", [override]);
}, },
/**
* Override the default behavior of the Android volume button.
* If overridden, when the volume button is pressed, the "volume[up|down]button" JavaScript event will be fired.
*
* Note: The user should not have to call this method. Instead, when the user
* registers for the "volume[up|down]button" event, this is automatically done.
*
* @param button volumeup, volumedown
* @param override T=override, F=cancel override
*/
overrideButton:function(button, override) {
exec(null, null, "App", "overrideButton", [button, override]);
},
/** /**
* Exit and terminate the application. * Exit and terminate the application.
*/ */

View File

@ -62,6 +62,9 @@ public class App extends Plugin {
} }
else if (action.equals("overrideBackbutton")) { else if (action.equals("overrideBackbutton")) {
this.overrideBackbutton(args.getBoolean(0)); this.overrideBackbutton(args.getBoolean(0));
}
else if (action.equals("overrideButton")) {
this.overrideButton(args.getString(0), args.getBoolean(1));
} }
else if (action.equals("isBackbuttonOverridden")) { else if (action.equals("isBackbuttonOverridden")) {
boolean b = this.isBackbuttonOverridden(); boolean b = this.isBackbuttonOverridden();
@ -180,6 +183,17 @@ public class App extends Plugin {
((DroidGap)this.ctx).bound = override; ((DroidGap)this.ctx).bound = override;
} }
/**
* Override the default behavior of the Android volume buttons.
* If overridden, when the volume button is pressed, the "volume[up|down]button" JavaScript event will be fired.
*
* @param button volumeup, volumedown
* @param override T=override, F=cancel 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!");
((DroidGap)this.ctx).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

@ -158,6 +158,8 @@ public class DroidGap extends Activity implements CordovaInterface {
protected LinearLayout root; protected LinearLayout root;
public boolean bound = false; public boolean bound = false;
public boolean volumeupBound = false;
public boolean volumedownBound = false;
public CallbackServer callbackServer; public CallbackServer callbackServer;
protected PluginManager pluginManager; protected PluginManager pluginManager;
protected boolean cancelLoadUrl = false; protected boolean cancelLoadUrl = false;
@ -1087,18 +1089,38 @@ public class DroidGap extends Activity implements CordovaInterface {
return true; 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 volumedown key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');"); if (this.volumedownBound==true) {
return true; // only override default behaviour is event bound
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
return true;
}
} }
// If volumeup key // If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');"); if (this.volumeupBound==true) {
return true; // only override default behaviour is event bound
this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
return true;
}
} }
return false; return false;
} }
@ -1416,6 +1438,16 @@ public class DroidGap extends Activity implements CordovaInterface {
return this.bound; return this.bound;
} }
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;
}
}
protected Dialog splashDialog; protected Dialog splashDialog;
/** /**