diff --git a/framework/assets/js/cordova.android.js b/framework/assets/js/cordova.android.js index cdf619df..f43e67b0 100644 --- a/framework/assets/js/cordova.android.js +++ b/framework/assets/js/cordova.android.js @@ -639,6 +639,9 @@ Channel.prototype.unsubscribe = function(g) { if (handler) { this.handlers[g] = null; delete this.handlers[g]; + } + // useful when same handler has been set multiple times and already deleted + if (this.numHandlers > 0) { this.numHandlers--; if (this.events.onUnsubscribe) this.events.onUnsubscribe.call(this); } @@ -1047,6 +1050,28 @@ module.exports = { cordova.addDocumentEventHandler('menubutton'); 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 // support from the native side. var storage = require('cordova/plugin/android/storage'); @@ -3586,6 +3611,20 @@ module.exports = { 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. */ diff --git a/framework/src/org/apache/cordova/App.java b/framework/src/org/apache/cordova/App.java index 9b1c96a9..ea698f9c 100755 --- a/framework/src/org/apache/cordova/App.java +++ b/framework/src/org/apache/cordova/App.java @@ -62,6 +62,9 @@ public class App extends Plugin { } else if (action.equals("overrideBackbutton")) { this.overrideBackbutton(args.getBoolean(0)); + } + else if (action.equals("overrideButton")) { + this.overrideButton(args.getString(0), args.getBoolean(1)); } else if (action.equals("isBackbuttonOverridden")) { boolean b = this.isBackbuttonOverridden(); @@ -180,6 +183,17 @@ public class App extends Plugin { ((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. * diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index e94a44e2..9b6ce131 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -158,6 +158,8 @@ public class DroidGap extends Activity implements CordovaInterface { protected LinearLayout root; public boolean bound = false; + public boolean volumeupBound = false; + public boolean volumedownBound = false; public CallbackServer callbackServer; protected PluginManager pluginManager; protected boolean cancelLoadUrl = false; @@ -1087,18 +1089,38 @@ public class DroidGap extends Activity implements CordovaInterface { 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 - else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { - this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');"); - return true; + if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { + if (this.volumedownBound==true) { + // only override default behaviour is event bound + this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');"); + return true; + } } // If volumeup key else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { - this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');"); - return true; + if (this.volumeupBound==true) { + // only override default behaviour is event bound + this.appView.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');"); + return true; + } } - return false; } @@ -1416,6 +1438,16 @@ public class DroidGap extends Activity implements CordovaInterface { 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; /**