Bug 512237 - Device Orientation issues and cleanup. r=olli

This commit is contained in:
Doug Turner 2009-08-26 16:57:08 -07:00
parent 10b601ff6c
commit de7964045c
3 changed files with 62 additions and 21 deletions

View File

@ -151,7 +151,7 @@ void nsAccelerometerX::Startup()
if (mUpdateTimer)
mUpdateTimer->InitWithFuncCallback(UpdateHandler,
this,
200, /* todo -- we want to pref this? or maybe add this to some API? */
mUpdateInterval,
nsITimer::TYPE_REPEATING_SLACK);
}

View File

@ -45,8 +45,8 @@
#include "nsIPrivateDOMEvent.h"
#include "nsIDOMDocumentEvent.h"
#include "nsIDOMOrientationEvent.h"
#include "nsIServiceManager.h"
#include "nsIPrefService.h"
class nsAcceleration : public nsIAcceleration
{
@ -100,25 +100,40 @@ NS_IMETHODIMP nsAcceleration::GetZ(double *aZ)
NS_IMPL_ISUPPORTS1(nsAccelerometer, nsIAccelerometer)
nsAccelerometer::nsAccelerometer()
: mStarted(PR_FALSE)
: mLastX(10), /* initialize to values that can't be possible */
mLastY(10),
mLastZ(10),
mStarted(PR_FALSE),
mNewListener(PR_FALSE),
mUpdateInterval(50) /* default to 50 ms */
{
nsCOMPtr<nsIPrefBranch> prefSrv = do_GetService(NS_PREFSERVICE_CONTRACTID);
PRInt32 value;
if (prefSrv) {
nsresult rv = prefSrv->GetIntPref("accelerometer.update.interval", &value);
if (NS_SUCCEEDED(rv))
mUpdateInterval = value;
}
}
nsAccelerometer::~nsAccelerometer()
{
if (mTimer)
mTimer->Cancel();
if (mTimeoutTimer)
mTimeoutTimer->Cancel();
}
void
nsAccelerometer::startDisconnectTimer()
nsAccelerometer::StartDisconnectTimer()
{
mTimer = do_CreateInstance("@mozilla.org/timer;1");
if (mTimer)
mTimer->InitWithFuncCallback(TimeoutHandler,
this,
2000,
nsITimer::TYPE_REPEATING_SLACK);
if (mTimeoutTimer)
mTimeoutTimer->Cancel();
mTimeoutTimer = do_CreateInstance("@mozilla.org/timer;1");
if (mTimeoutTimer)
mTimeoutTimer->InitWithFuncCallback(TimeoutHandler,
this,
2000,
nsITimer::TYPE_ONE_SHOT);
}
void
@ -143,9 +158,9 @@ nsAccelerometer::TimeoutHandler(nsITimer *aTimer, void *aClosure)
NS_IMETHODIMP nsAccelerometer::AddListener(nsIAccelerationListener *aListener)
{
if (mStarted == PR_FALSE) {
startDisconnectTimer();
Startup();
mStarted = PR_TRUE;
mNewListener = PR_TRUE;
Startup();
}
mListeners.AppendObject(aListener);
@ -155,15 +170,16 @@ NS_IMETHODIMP nsAccelerometer::AddListener(nsIAccelerationListener *aListener)
NS_IMETHODIMP nsAccelerometer::RemoveListener(nsIAccelerationListener *aListener)
{
mListeners.RemoveObject(aListener);
StartDisconnectTimer();
return NS_OK;
}
NS_IMETHODIMP nsAccelerometer::AddWindowListener(nsIDOMWindow *aWindow)
{
if (mStarted == PR_FALSE) {
startDisconnectTimer();
Startup();
mStarted = PR_TRUE;
mStarted = PR_TRUE;
mNewListener = PR_TRUE;
Startup();
}
mWindowListeners.AppendObject(aWindow);
@ -173,12 +189,29 @@ NS_IMETHODIMP nsAccelerometer::AddWindowListener(nsIDOMWindow *aWindow)
NS_IMETHODIMP nsAccelerometer::RemoveWindowListener(nsIDOMWindow *aWindow)
{
mWindowListeners.RemoveObject(aWindow);
StartDisconnectTimer();
return NS_OK;
}
void
nsAccelerometer::AccelerationChanged(double x, double y, double z)
{
if (x > 1 || y > 1 || z > 1 || x < -1 || y < -1 || z < -1)
return;
if (!mNewListener) {
if (PR_ABS(mLastX - x) < .01 &&
PR_ABS(mLastY - y) < .01 &&
PR_ABS(mLastZ - z) < .01)
return;
}
mLastX = x;
mLastY = y;
mLastZ = z;
mNewListener = PR_FALSE;
for (PRUint32 i = mListeners.Count(); i > 0 ; ) {
--i;
nsRefPtr<nsIAcceleration> a = new nsAcceleration(x, y, z);

View File

@ -57,17 +57,25 @@ public:
/* must be called on the main thread or else */
void AccelerationChanged(double x, double y, double z);
double mLastX;
double mLastY;
double mLastZ;
private:
nsCOMArray<nsIAccelerationListener> mListeners;
nsCOMArray<nsIDOMWindow> mWindowListeners;
void startDisconnectTimer();
void StartDisconnectTimer();
PRBool mStarted;
nsCOMPtr<nsITimer> mTimer;
PRBool mNewListener;
nsCOMPtr<nsITimer> mTimeoutTimer;
static void TimeoutHandler(nsITimer *aTimer, void *aClosure);
protected:
protected:
PRUint32 mUpdateInterval;
virtual void Startup() = 0;
virtual void Shutdown() = 0;