Bug 738131 - implement device proximity. r=smaug

* * *
Bug 738131 - implement device proximity. gonk implementation. r=mwu
This commit is contained in:
Doug Turner 2012-05-02 09:43:45 -07:00
parent 35a5afdea6
commit 63408865d2
22 changed files with 251 additions and 9 deletions

View File

@ -1711,8 +1711,9 @@ GK_ATOM(onMozTouchMove, "onMozTouchMove")
GK_ATOM(onMozTouchUp, "onMozTouchUp")
// orientation support
GK_ATOM(ondeviceorientation, "ondeviceorientation")
GK_ATOM(ondevicemotion, "ondevicemotion")
GK_ATOM(ondeviceorientation, "ondeviceorientation")
GK_ATOM(ondeviceproximity, "ondeviceproximity")
//---------------------------------------------------------------------------
// Special atoms

View File

@ -455,6 +455,10 @@ WINDOW_ONLY_EVENT(deviceorientation,
NS_DEVICE_ORIENTATION,
EventNameType_None,
NS_EVENT)
WINDOW_ONLY_EVENT(deviceproximity,
NS_DEVICE_PROXIMITY,
EventNameType_None,
NS_EVENT)
TOUCH_EVENT(touchstart,
NS_TOUCH_START,

View File

@ -92,6 +92,8 @@ NS_NewDOMMutationEvent(nsIDOMEvent** aResult NS_OUTPARAM, nsPresContext* aPresCo
nsresult
NS_NewDOMPopupBlockedEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);
nsresult
NS_NewDOMDeviceProximityEvent(nsIDOMEvent** aInstancePtrResult, nsPresContext* aPresContext, nsEvent *aEvent);
nsresult
NS_NewDOMDeviceOrientationEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);
nsresult
NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aResult, nsPresContext* aPresContext, nsEvent* aEvent);

View File

@ -51,7 +51,7 @@ EXPORTS = \
nsEventListenerManager.h \
nsDOMEventTargetHelper.h \
$(NULL)
CPPSRCS = \
nsEventListenerManager.cpp \
nsEventStateManager.cpp \
@ -65,6 +65,7 @@ CPPSRCS = \
nsDOMDragEvent.cpp \
nsDOMMutationEvent.cpp \
nsDOMPopupBlockedEvent.cpp \
nsDOMDeviceProximityEvent.cpp \
nsDOMDeviceOrientationEvent.cpp \
nsDOMDeviceMotionEvent.cpp \
nsDOMBeforeUnloadEvent.cpp \

View File

@ -0,0 +1,81 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsDOMDeviceProximityEvent.h"
#include "nsContentUtils.h"
#include "DictionaryHelpers.h"
NS_IMPL_ADDREF_INHERITED(nsDOMDeviceProximityEvent, nsDOMEvent)
NS_IMPL_RELEASE_INHERITED(nsDOMDeviceProximityEvent, nsDOMEvent)
DOMCI_DATA(DeviceProximityEvent, nsDOMDeviceProximityEvent)
NS_INTERFACE_MAP_BEGIN(nsDOMDeviceProximityEvent)
NS_INTERFACE_MAP_ENTRY(nsIDOMDeviceProximityEvent)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DeviceProximityEvent)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
NS_IMETHODIMP
nsDOMDeviceProximityEvent::InitDeviceProximityEvent(const nsAString & aEventTypeArg,
bool aCanBubbleArg,
bool aCancelableArg,
double aValue,
double aMin,
double aMax)
{
nsresult rv = nsDOMEvent::InitEvent(aEventTypeArg, aCanBubbleArg, aCancelableArg);
NS_ENSURE_SUCCESS(rv, rv);
mValue = aValue;
mMin = aMin;
mMax = aMax;
return NS_OK;
}
NS_IMETHODIMP
nsDOMDeviceProximityEvent::GetValue(double *aValue)
{
NS_ENSURE_ARG_POINTER(aValue);
*aValue = mValue;
return NS_OK;
}
NS_IMETHODIMP
nsDOMDeviceProximityEvent::GetMin(double *aMin)
{
NS_ENSURE_ARG_POINTER(aMin);
*aMin = mMin;
return NS_OK;
}
NS_IMETHODIMP
nsDOMDeviceProximityEvent::GetMax(double *aMax)
{
NS_ENSURE_ARG_POINTER(aMax);
*aMax = mMax;
return NS_OK;
}
nsresult
nsDOMDeviceProximityEvent::InitFromCtor(const nsAString& aType,
JSContext* aCx, jsval* aVal)
{
mozilla::dom::DeviceProximityEventInit d;
nsresult rv = d.Init(aCx, aVal);
NS_ENSURE_SUCCESS(rv, rv);
return InitDeviceProximityEvent(aType, d.bubbles, d.cancelable, d.value, d.min, d.max);
}
nsresult
NS_NewDOMDeviceProximityEvent(nsIDOMEvent** aInstancePtrResult,
nsPresContext* aPresContext,
nsEvent *aEvent)
{
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
nsDOMDeviceProximityEvent* it = new nsDOMDeviceProximityEvent(aPresContext, aEvent);
return CallQueryInterface(it, aInstancePtrResult);
}

View File

@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsDOMDeviceProximityEvent_h__
#define nsDOMDeviceProximityEvent_h__
#include "nsIDOMDeviceProximityEvent.h"
#include "nsDOMEvent.h"
class nsDOMDeviceProximityEvent
: public nsDOMEvent
, public nsIDOMDeviceProximityEvent
{
public:
nsDOMDeviceProximityEvent(nsPresContext* aPresContext, nsEvent* aEvent)
: nsDOMEvent(aPresContext, aEvent),
mValue(-1),
mMin(0),
mMax(0) {}
NS_DECL_ISUPPORTS_INHERITED
// Forward to nsDOMEvent
NS_FORWARD_TO_NSDOMEVENT
// nsIDOMDeviceProximityEvent Interface
NS_DECL_NSIDOMDEVICEPROXIMITYEVENT
virtual nsresult InitFromCtor(const nsAString& aType,
JSContext* aCx,
jsval* aVal);
protected:
double mValue, mMin, mMax;
};
#endif

View File

@ -126,7 +126,8 @@ static const char* const sEventNames[] = {
"animationend",
"animationiteration",
"devicemotion",
"deviceorientation"
"deviceorientation",
"deviceproximity"
};
static char *sPopupAllowedEvents;
@ -1553,6 +1554,8 @@ const char* nsDOMEvent::GetEventName(PRUint32 aEventType)
return sEventNames[eDOMEvents_devicemotion];
case NS_DEVICE_ORIENTATION:
return sEventNames[eDOMEvents_deviceorientation];
case NS_DEVICE_PROXIMITY:
return sEventNames[eDOMEvents_deviceproximity];
case NS_FULLSCREENCHANGE:
return sEventNames[eDOMEvents_mozfullscreenchange];
case NS_FULLSCREENERROR:

View File

@ -209,7 +209,8 @@ public:
eDOMEvents_animationend,
eDOMEvents_animationiteration,
eDOMEvents_devicemotion,
eDOMEvents_deviceorientation
eDOMEvents_deviceorientation,
eDOMEvents_deviceproximity
};
nsDOMEvent(nsPresContext* aPresContext, nsEvent* aEvent);

View File

@ -290,6 +290,8 @@ nsEventListenerManager::AddEventListener(nsIDOMEventListener *aListener,
}
} else if (aTypeAtom == nsGkAtoms::ondeviceorientation) {
EnableDevice(NS_DEVICE_ORIENTATION);
} else if (aTypeAtom == nsGkAtoms::ondeviceproximity) {
EnableDevice(NS_DEVICE_PROXIMITY);
} else if (aTypeAtom == nsGkAtoms::ondevicemotion) {
EnableDevice(NS_DEVICE_MOTION);
} else if ((aType >= NS_MOZTOUCH_DOWN && aType <= NS_MOZTOUCH_UP) ||
@ -346,6 +348,9 @@ nsEventListenerManager::EnableDevice(PRUint32 aType)
case NS_DEVICE_ORIENTATION:
window->EnableDeviceSensor(SENSOR_ORIENTATION);
break;
case NS_DEVICE_PROXIMITY:
window->EnableDeviceSensor(SENSOR_PROXIMITY);
break;
case NS_DEVICE_MOTION:
window->EnableDeviceSensor(SENSOR_ACCELERATION);
window->EnableDeviceSensor(SENSOR_LINEAR_ACCELERATION);
@ -376,6 +381,9 @@ nsEventListenerManager::DisableDevice(PRUint32 aType)
window->DisableDeviceSensor(SENSOR_LINEAR_ACCELERATION);
window->DisableDeviceSensor(SENSOR_GYROSCOPE);
break;
case NS_DEVICE_PROXIMITY:
window->DisableDeviceSensor(SENSOR_PROXIMITY);
break;
default:
NS_WARNING("Disabling an unknown device sensor.");
break;

View File

@ -369,6 +369,17 @@ is(e.storageArea, localStorage, "Wrong value");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// DeviceProximityEvent
e = new DeviceProximityEvent("hello", {min: 0, value: 1, max: 2});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.value, 1, "value should be 1");
is(e.min, 0, "min should be 0");
is(e.max, 2, "max should be 2");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// MouseEvent
try {

View File

@ -325,6 +325,7 @@
#include "nsIDOMCSSStyleRule.h"
#include "nsIDOMCSSStyleSheet.h"
#include "nsDOMCSSValueList.h"
#include "nsIDOMDeviceProximityEvent.h"
#include "nsIDOMDeviceOrientationEvent.h"
#include "nsIDOMDeviceMotionEvent.h"
#include "nsIDOMRange.h"
@ -813,6 +814,9 @@ static nsDOMClassInfoData sClassInfoData[] = {
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(PopupBlockedEvent, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
// Device Proximity
NS_DEFINE_CLASSINFO_DATA(DeviceProximityEvent, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
// Device Orientation
NS_DEFINE_CLASSINFO_DATA(DeviceOrientationEvent, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
@ -1692,6 +1696,8 @@ NS_DEFINE_EVENT_CTOR(CloseEvent)
NS_DEFINE_EVENT_CTOR(MozSettingsEvent)
NS_DEFINE_EVENT_CTOR(UIEvent)
NS_DEFINE_EVENT_CTOR(MouseEvent)
NS_DEFINE_EVENT_CTOR(DeviceProximityEvent)
nsresult
NS_DOMStorageEventCtor(nsISupports** aInstancePtrResult)
{
@ -1725,6 +1731,7 @@ static const nsConstructorFuncMapData kConstructorFuncMap[] =
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MozSettingsEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(UIEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MouseEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(DeviceProximityEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(StorageEvent)
NS_DEFINE_CONSTRUCTOR_FUNC_DATA(MozSmsFilter, sms::SmsFilter::NewSmsFilter)
};
@ -2605,6 +2612,11 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_EVENT_MAP_ENTRIES
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(DeviceProximityEvent, nsIDOMDeviceProximityEvent)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMDeviceProximityEvent)
DOM_CLASSINFO_EVENT_MAP_ENTRIES
DOM_CLASSINFO_MAP_END
DOM_CLASSINFO_MAP_BEGIN(DeviceOrientationEvent, nsIDOMDeviceOrientationEvent)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMDeviceOrientationEvent)
DOM_CLASSINFO_EVENT_MAP_ENTRIES

View File

@ -79,6 +79,7 @@ DOMCI_CLASS(DragEvent)
DOMCI_CLASS(KeyboardEvent)
DOMCI_CLASS(CompositionEvent)
DOMCI_CLASS(PopupBlockedEvent)
DOMCI_CLASS(DeviceProximityEvent)
DOMCI_CLASS(DeviceOrientationEvent)
DOMCI_CLASS(DeviceMotionEvent)
DOMCI_CLASS(DeviceAcceleration)

View File

@ -69,7 +69,7 @@ interface nsIDOMMozURLProperty : nsISupports
* @see <http://www.whatwg.org/html/#window>
*/
[scriptable, uuid(f6e3b10d-d5f4-4fcd-aa4c-5f98626d428a)]
[scriptable, uuid(627fa383-cb3c-498a-a5ed-ef2e9a827839)]
interface nsIDOMWindow : nsISupports
{
// the current browsing context
@ -464,6 +464,7 @@ interface nsIDOMWindow : nsISupports
*/
[implicit_jscontext] attribute jsval ondevicemotion;
[implicit_jscontext] attribute jsval ondeviceorientation;
[implicit_jscontext] attribute jsval ondeviceproximity;
[implicit_jscontext] attribute jsval onmouseenter;
[implicit_jscontext] attribute jsval onmouseleave;

View File

@ -75,7 +75,8 @@ XPIDLSRCS = \
nsIDOMPaintRequestList.idl \
nsIDOMSimpleGestureEvent.idl \
nsIDOMMozTouchEvent.idl \
nsIDOMDeviceOrientationEvent.idl\
nsIDOMDeviceProximityEvent.idl \
nsIDOMDeviceOrientationEvent.idl \
nsIDOMDeviceMotionEvent.idl \
nsIDOMScrollAreaEvent.idl \
nsIDOMTransitionEvent.idl \

View File

@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsIDOMEvent.idl"
[scriptable, uuid(4bb21c9b-381f-4c73-9000-5eb838f58738)]
interface nsIDOMDeviceProximityEvent : nsIDOMEvent
{
[noscript] void initDeviceProximityEvent(in DOMString eventTypeArg,
in boolean canBubbleArg,
in boolean cancelableArg,
in double value,
in double min,
in double max);
readonly attribute double value;
readonly attribute double min;
readonly attribute double max;
};
dictionary DeviceProximityEventInit : EventInit
{
double value;
double min;
double max;
};

View File

@ -220,10 +220,37 @@ nsDeviceSensors::Notify(const mozilla::hal::SensorData& aSensorData)
FireDOMMotionEvent(domdoc, target, type, x, y, z);
else if (type == nsIDeviceSensorData::TYPE_ORIENTATION)
FireDOMOrientationEvent(domdoc, target, x, y, z);
else if (type == nsIDeviceSensorData::TYPE_PROXIMITY)
FireDOMProximityEvent(target, x, y, z);
}
}
}
void
nsDeviceSensors::FireDOMProximityEvent(nsIDOMEventTarget *aTarget,
double aValue,
double aMin,
double aMax)
{
nsCOMPtr<nsIDOMEvent> event;
NS_NewDOMDeviceProximityEvent(getter_AddRefs(event), nsnull, nsnull);
nsCOMPtr<nsIDOMDeviceProximityEvent> oe = do_QueryInterface(event);
oe->InitDeviceProximityEvent(NS_LITERAL_STRING("deviceproximity"),
true,
false,
aValue,
aMin,
aMax);
nsCOMPtr<nsIPrivateDOMEvent> privateEvent = do_QueryInterface(event);
if (privateEvent) {
privateEvent->SetTrusted(true);
}
bool defaultActionEnabled;
aTarget->DispatchEvent(event, &defaultActionEnabled);
}
void
nsDeviceSensors::FireDOMOrientationEvent(nsIDOMDocument *domdoc,
nsIDOMEventTarget *target,

View File

@ -44,6 +44,7 @@
#include "nsCOMPtr.h"
#include "nsITimer.h"
#include "nsIDOMDeviceOrientationEvent.h"
#include "nsIDOMDeviceProximityEvent.h"
#include "nsIDOMDeviceMotionEvent.h"
#include "nsDOMDeviceMotionEvent.h"
#include "mozilla/TimeStamp.h"
@ -73,7 +74,12 @@ public:
private:
// sensor -> window listener
nsTArray<nsTArray<nsIDOMWindow*>* > mWindowListeners;
void FireDOMProximityEvent(nsIDOMEventTarget *aTarget,
double aValue,
double aMin,
double aMax);
void FireDOMOrientationEvent(class nsIDOMDocument *domDoc,
class nsIDOMEventTarget *target,
double alpha,

View File

@ -109,6 +109,18 @@ public:
mSensorValues.AppendElement(radToDeg(data.data[0]));
mSensorValues.AppendElement(radToDeg(data.data[1]));
mSensorValues.AppendElement(radToDeg(data.data[2]));
} else if (mSensorData.sensor() == SENSOR_PROXIMITY) {
mSensorValues.AppendElement(data.data[0]);
mSensorValues.AppendElement(0);
// Determine the maxRange for this sensor.
const sensor_t* sensors = NULL;
size_t size = SensorDevice::getInstance().getSensorList(&sensors);
for (size_t i = 0; i < size; i++) {
if (sensors[i].type == SENSOR_TYPE_PROXIMITY) {
mSensorValues.AppendElement(sensors[i].maxRange);
}
}
} else {
mSensorValues.AppendElement(data.data[0]);
mSensorValues.AppendElement(data.data[1]);

View File

@ -14,7 +14,8 @@ dictionaries = [
[ 'BlobPropertyBag', 'nsIDOMFile.idl' ],
[ 'MutationObserverInit', 'nsIDOMMutationObserver.idl' ],
[ 'SettingsEventInit', 'nsIDOMSettingsManager.idl' ],
[ 'GeoPositionOptions', 'nsIDOMGeoGeolocation.idl']
[ 'GeoPositionOptions', 'nsIDOMGeoGeolocation.idl' ],
[ 'DeviceProximityEventInit', 'nsIDOMDeviceProximityEvent.idl' ]
]
# include file names

View File

@ -690,7 +690,7 @@ public class GeckoAppShell
if(gProximitySensor == null)
gProximitySensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (gProximitySensor != null)
sm.registerListener(GeckoApp.mAppContext, gProximitySensor, sDefaultSensorHint);
sm.registerListener(GeckoApp.mAppContext, gProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
break;
case GeckoHalDefines.SENSOR_LINEAR_ACCELERATION:

View File

@ -357,7 +357,10 @@ public class GeckoEvent {
case Sensor.TYPE_PROXIMITY:
event = new GeckoEvent(SENSOR_EVENT);
event.mFlags = GeckoHalDefines.SENSOR_PROXIMITY;
event.mMetaState = HalSensorAccuracyFor(s.accuracy);
event.mX = s.values[0];
event.mY = 0;
event.mZ = s.sensor.getMaximumRange();
break;
}
return event;

View File

@ -542,6 +542,7 @@ class nsHashKey;
#define NS_DEVICE_ORIENTATION_START 4900
#define NS_DEVICE_ORIENTATION (NS_DEVICE_ORIENTATION_START)
#define NS_DEVICE_MOTION (NS_DEVICE_ORIENTATION_START+1)
#define NS_DEVICE_PROXIMITY (NS_DEVICE_ORIENTATION_START+2)
#define NS_SHOW_EVENT 5000