Add listener latency to CompassProvider

parent 6be466cb
...@@ -125,11 +125,11 @@ void QAndroidCompass::preloadJavaClasses() ...@@ -125,11 +125,11 @@ void QAndroidCompass::preloadJavaClasses()
} }
void QAndroidCompass::start(int32_t delayMks /*= -1*/) void QAndroidCompass::start(int32_t delayUs /*= -1*/, int32_t latencyUs /*= -1*/)
{ {
if (handler_) if (handler_)
{ {
handler_->callParamVoid("start", "I", delayMks); handler_->callParamVoid("start", "II", delayUs, latencyUs);
} }
} }
......
...@@ -49,7 +49,7 @@ public: ...@@ -49,7 +49,7 @@ public:
virtual ~QAndroidCompass(); virtual ~QAndroidCompass();
public slots: public slots:
void start(int32_t delayMks = -1); void start(int32_t delayUs = -1, int32_t latencyUs = -1);
void stop(); void stop();
signals: signals:
......
...@@ -77,7 +77,10 @@ public class CompassProvider implements SensorEventListener ...@@ -77,7 +77,10 @@ public class CompassProvider implements SensorEventListener
} }
public void start(int delayMksec) private static final int advancedListenerApiLevel = 19;
public void start(int samplingPeriodUs, int maxReportLatencyUs)
{ {
if (null == mSensorManager || null == mOrientation) if (null == mSensorManager || null == mOrientation)
{ {
...@@ -85,19 +88,48 @@ public class CompassProvider implements SensorEventListener ...@@ -85,19 +88,48 @@ public class CompassProvider implements SensorEventListener
return; return;
} }
if (delayMksec < 0) /* API 9, not 19. That is not a mistake. */
if ((android.os.Build.VERSION.SDK_INT < 9) || (samplingPeriodUs < 0))
{ {
delayMksec = SensorManager.SENSOR_DELAY_NORMAL; samplingPeriodUs = SensorManager.SENSOR_DELAY_UI;
} }
boolean registered = false;
try try
{ {
mSensorManager.registerListener(this, mOrientation, delayMksec); if (android.os.Build.VERSION.SDK_INT >= advancedListenerApiLevel)
{
if (maxReportLatencyUs < 0)
{
maxReportLatencyUs = samplingPeriodUs / 5;
}
Log.i(TAG, "Registering orientation listener for API >= " + advancedListenerApiLevel +
" with samplingPeriodUs = " + samplingPeriodUs +
", maxReportLatencyUs = " + maxReportLatencyUs);
registered = mSensorManager.registerListener(this, mOrientation, samplingPeriodUs, maxReportLatencyUs);
}
else
{
Log.i(TAG, "Registering orientation listener for API < " + advancedListenerApiLevel +
" with samplingPeriodUs = " + samplingPeriodUs);
registered = mSensorManager.registerListener(this, mOrientation, samplingPeriodUs);
}
} }
catch(Exception e) catch(Exception e)
{ {
Log.e(TAG, e.getMessage()); Log.e(TAG, e.getMessage());
} }
if (registered)
{
Log.i(TAG, "Sensor listener registered successfully");
}
else
{
Log.e(TAG, "Sensor listener failed to register");
}
} }
...@@ -111,6 +143,7 @@ public class CompassProvider implements SensorEventListener ...@@ -111,6 +143,7 @@ public class CompassProvider implements SensorEventListener
try try
{ {
Log.i(TAG, "Unregistering orientation listener");
// to stop the listener and save battery // to stop the listener and save battery
mSensorManager.unregisterListener(this); mSensorManager.unregisterListener(this);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment