mirror of
https://github.com/apache/cordova-android.git
synced 2025-05-06 02:33:02 +08:00
Formalize document and window event listeners and allow plugins to override eventListeners.
This commit is contained in:
parent
8c7db9aa32
commit
a735a631f6
@ -27,7 +27,7 @@ var Connection = function() {
|
|||||||
// set a timer if still offline at the end of timer send the offline event
|
// set a timer if still offline at the end of timer send the offline event
|
||||||
me._timer = setTimeout(function(){
|
me._timer = setTimeout(function(){
|
||||||
me.type = type;
|
me.type = type;
|
||||||
PhoneGap.fireEvent('offline');
|
PhoneGap.fireDocumentEvent('offline');
|
||||||
me._timer = null;
|
me._timer = null;
|
||||||
}, me.timeout);
|
}, me.timeout);
|
||||||
} else {
|
} else {
|
||||||
@ -37,7 +37,7 @@ var Connection = function() {
|
|||||||
me._timer = null;
|
me._timer = null;
|
||||||
}
|
}
|
||||||
me.type = type;
|
me.type = type;
|
||||||
PhoneGap.fireEvent('online');
|
PhoneGap.fireDocumentEvent('online');
|
||||||
}
|
}
|
||||||
|
|
||||||
// should only fire this once
|
// should only fire this once
|
||||||
|
@ -46,7 +46,9 @@ var PhoneGap = {
|
|||||||
ready: true,
|
ready: true,
|
||||||
commands: [],
|
commands: [],
|
||||||
timer: null
|
timer: null
|
||||||
}
|
},
|
||||||
|
documentEventHandler: {}, // Collection of custom document event handlers
|
||||||
|
windowEventHandler: {} // Collection of custom window event handlers
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -381,6 +383,36 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
// Intercept calls to document.addEventListener and watch for deviceready
|
// Intercept calls to document.addEventListener and watch for deviceready
|
||||||
PhoneGap.m_document_addEventListener = document.addEventListener;
|
PhoneGap.m_document_addEventListener = document.addEventListener;
|
||||||
|
|
||||||
|
// Intercept calls to window.addEventListener
|
||||||
|
PhoneGap.m_window_addEventListener = window.addEventListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a custom window event handler.
|
||||||
|
*
|
||||||
|
* @param {String} event The event name that callback handles
|
||||||
|
* @param {Function} callback The event handler
|
||||||
|
*/
|
||||||
|
PhoneGap.addWindowEventHandler = function(event, callback) {
|
||||||
|
PhoneGap.windowEventHandler[event] = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a custom document event handler.
|
||||||
|
*
|
||||||
|
* @param {String} event The event name that callback handles
|
||||||
|
* @param {Function} callback The event handler
|
||||||
|
*/
|
||||||
|
PhoneGap.addDocumentEventHandler = function(event, callback) {
|
||||||
|
PhoneGap.documentEventHandler[event] = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercept adding document event listeners and handle our own
|
||||||
|
*
|
||||||
|
* @param {Object} evt
|
||||||
|
* @param {Function} handler
|
||||||
|
* @param capture
|
||||||
|
*/
|
||||||
document.addEventListener = function(evt, handler, capture) {
|
document.addEventListener = function(evt, handler, capture) {
|
||||||
var e = evt.toLowerCase();
|
var e = evt.toLowerCase();
|
||||||
if (e === 'deviceready') {
|
if (e === 'deviceready') {
|
||||||
@ -399,14 +431,51 @@ document.addEventListener = function(evt, handler, capture) {
|
|||||||
PhoneGap.exec(null, null, "App", "overrideBackbutton", [true]);
|
PhoneGap.exec(null, null, "App", "overrideBackbutton", [true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If subscribing to an event that is handled by a plugin
|
||||||
|
else if (typeof PhoneGap.documentEventHandler[e] !== "undefined") {
|
||||||
|
if (PhoneGap.documentEventHandler[e](e, handler, true)) {
|
||||||
|
return; // Stop default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PhoneGap.m_document_addEventListener.call(document, evt, handler, capture);
|
PhoneGap.m_document_addEventListener.call(document, evt, handler, capture);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercept adding window event listeners and handle our own
|
||||||
|
*
|
||||||
|
* @param {Object} evt
|
||||||
|
* @param {Function} handler
|
||||||
|
* @param capture
|
||||||
|
*/
|
||||||
|
window.addEventListener = function(evt, handler, capture) {
|
||||||
|
var e = evt.toLowerCase();
|
||||||
|
|
||||||
|
// If subscribing to an event that is handled by a plugin
|
||||||
|
if (typeof PhoneGap.windowEventHandler[e] !== "undefined") {
|
||||||
|
if (PhoneGap.windowEventHandler[e](e, handler, true)) {
|
||||||
|
return; // Stop default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PhoneGap.m_window_addEventListener.call(window, evt, handler, capture);
|
||||||
|
};
|
||||||
|
|
||||||
// Intercept calls to document.removeEventListener and watch for events that
|
// Intercept calls to document.removeEventListener and watch for events that
|
||||||
// are generated by PhoneGap native code
|
// are generated by PhoneGap native code
|
||||||
PhoneGap.m_document_removeEventListener = document.removeEventListener;
|
PhoneGap.m_document_removeEventListener = document.removeEventListener;
|
||||||
|
|
||||||
|
// Intercept calls to window.removeEventListener
|
||||||
|
PhoneGap.m_window_removeEventListener = window.removeEventListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercept removing document event listeners and handle our own
|
||||||
|
*
|
||||||
|
* @param {Object} evt
|
||||||
|
* @param {Function} handler
|
||||||
|
* @param capture
|
||||||
|
*/
|
||||||
document.removeEventListener = function(evt, handler, capture) {
|
document.removeEventListener = function(evt, handler, capture) {
|
||||||
var e = evt.toLowerCase();
|
var e = evt.toLowerCase();
|
||||||
|
|
||||||
@ -415,18 +484,70 @@ document.removeEventListener = function(evt, handler, capture) {
|
|||||||
PhoneGap.exec(null, null, "App", "overrideBackbutton", [false]);
|
PhoneGap.exec(null, null, "App", "overrideBackbutton", [false]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If unsubcribing from an event that is handled by a plugin
|
||||||
|
if (typeof PhoneGap.documentEventHandler[e] !== "undefined") {
|
||||||
|
if (PhoneGap.documentEventHandler[e](e, handler, false)) {
|
||||||
|
return; // Stop default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PhoneGap.m_document_removeEventListener.call(document, evt, handler, capture);
|
PhoneGap.m_document_removeEventListener.call(document, evt, handler, capture);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to fire event from native code
|
* Intercept removing window event listeners and handle our own
|
||||||
|
*
|
||||||
|
* @param {Object} evt
|
||||||
|
* @param {Function} handler
|
||||||
|
* @param capture
|
||||||
*/
|
*/
|
||||||
PhoneGap.fireEvent = function(type) {
|
window.removeEventListener = function(evt, handler, capture) {
|
||||||
|
var e = evt.toLowerCase();
|
||||||
|
|
||||||
|
// If unsubcribing from an event that is handled by a plugin
|
||||||
|
if (typeof PhoneGap.windowEventHandler[e] !== "undefined") {
|
||||||
|
if (PhoneGap.windowEventHandler[e](e, handler, false)) {
|
||||||
|
return; // Stop default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PhoneGap.m_window_removeEventListener.call(window, evt, handler, capture);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to fire document event
|
||||||
|
*
|
||||||
|
* @param {String} type The event type to fire
|
||||||
|
* @param {Object} data Data to send with event
|
||||||
|
*/
|
||||||
|
PhoneGap.fireDocumentEvent = function(type, data) {
|
||||||
var e = document.createEvent('Events');
|
var e = document.createEvent('Events');
|
||||||
e.initEvent(type);
|
e.initEvent(type);
|
||||||
|
if (data) {
|
||||||
|
for (var i in data) {
|
||||||
|
e[i] = data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
document.dispatchEvent(e);
|
document.dispatchEvent(e);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to fire window event
|
||||||
|
*
|
||||||
|
* @param {String} type The event type to fire
|
||||||
|
* @param {Object} data Data to send with event
|
||||||
|
*/
|
||||||
|
PhoneGap.fireWindowEvent = function(type, data) {
|
||||||
|
var e = document.createEvent('Events');
|
||||||
|
e.initEvent(type);
|
||||||
|
if (data) {
|
||||||
|
for (var i in data) {
|
||||||
|
e[i] = data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.dispatchEvent(e);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If JSON not included, use our own stringify. (Android 1.6)
|
* If JSON not included, use our own stringify. (Android 1.6)
|
||||||
* The restriction on ours is that it must be an array of simple types.
|
* The restriction on ours is that it must be an array of simple types.
|
||||||
|
10
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
10
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
@ -1256,7 +1256,7 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
|
|
||||||
// If back key is bound, then send event to JavaScript
|
// If back key is bound, then send event to JavaScript
|
||||||
if (this.bound) {
|
if (this.bound) {
|
||||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('backbutton');");
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('backbutton');");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1278,13 +1278,13 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
|
|
||||||
// If menu key
|
// If menu key
|
||||||
else if (keyCode == KeyEvent.KEYCODE_MENU) {
|
else if (keyCode == KeyEvent.KEYCODE_MENU) {
|
||||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('menubutton');");
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('menubutton');");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If search key
|
// If search key
|
||||||
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
|
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
|
||||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('searchbutton');");
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('searchbutton');");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1478,13 +1478,13 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
// gone away.
|
// gone away.
|
||||||
else if (height > oldHeight) {
|
else if (height > oldHeight) {
|
||||||
Log.d(LOG_TAG, "Throw hide keyboard event");
|
Log.d(LOG_TAG, "Throw hide keyboard event");
|
||||||
callbackServer.sendJavascript("PhoneGap.fireEvent('hidekeyboard');");
|
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('hidekeyboard');");
|
||||||
}
|
}
|
||||||
// If the height as gotten smaller then we will assume the soft keyboard has
|
// If the height as gotten smaller then we will assume the soft keyboard has
|
||||||
// been displayed.
|
// been displayed.
|
||||||
else if (height < oldHeight) {
|
else if (height < oldHeight) {
|
||||||
Log.d(LOG_TAG, "Throw show keyboard event");
|
Log.d(LOG_TAG, "Throw show keyboard event");
|
||||||
callbackServer.sendJavascript("PhoneGap.fireEvent('showkeyboard');");
|
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the old height for the next event
|
// Update the old height for the next event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user