use of android.hardware.SensorEvent in project android_frameworks_base by DirtyUnicorns.
the class PowerManagerService method runPostProximityCheck.
private void runPostProximityCheck(final Runnable r) {
if (mSensorManager == null) {
r.run();
return;
}
synchronized (mProximityWakeLock) {
mProximityWakeLock.acquire();
mProximityListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
cleanupProximityLocked();
if (!mHandler.hasMessages(MSG_WAKE_UP)) {
Slog.w(TAG, "Proximity sensor took too long, wake event already triggered!");
return;
}
mHandler.removeMessages(MSG_WAKE_UP);
final float distance = event.values[0];
if (distance >= PROXIMITY_NEAR_THRESHOLD || distance >= mProximitySensor.getMaximumRange()) {
r.run();
} else {
Slog.w(TAG, "Not waking up. Proximity sensor is blocked.");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do nothing
}
};
mSensorManager.registerListener(mProximityListener, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
}
use of android.hardware.SensorEvent in project AndroidSDK-RecipeBook by gabu.
the class Recipe071 method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mManager = new ProximityManager(this);
mManager.setOnProximityListener(new OnProximityListener() {
// 近接センサーの値が変わる度に呼び出されます。
public void onSensorChanged(SensorEvent event) {
}
// 近接センサーに近づいたら呼び出されます。
public void onNear(float value) {
Toast.makeText(getApplicationContext(), "onNear!", Toast.LENGTH_SHORT).show();
}
// 近接センサーから遠ざかったら呼び出されます。
public void onFar(float value) {
Toast.makeText(getApplicationContext(), "onFar!", Toast.LENGTH_SHORT).show();
}
});
}
use of android.hardware.SensorEvent in project robolectric by robolectric.
the class ShadowSensorManager method createSensorEvent.
/**
* Creates a {@link SensorEvent} for the given {@link Sensor} type with the given value array
* size, which the caller should set based on the type of sensor which is being emulated.
*
* <p>Callers can then specify individual values for the event. For example, for a proximity event
* a caller may wish to specify the distance value:
*
* <pre>{@code
* event.values[0] = distance;
* }</pre>
*
* <p>See {@link SensorEvent#values} for more information about values.
*/
public static SensorEvent createSensorEvent(int valueArraySize, int sensorType) {
checkArgument(valueArraySize > 0);
ClassParameter<Integer> valueArraySizeParam = new ClassParameter<>(int.class, valueArraySize);
SensorEvent sensorEvent = ReflectionHelpers.callConstructor(SensorEvent.class, valueArraySizeParam);
sensorEvent.sensor = ShadowSensor.newInstance(sensorType);
return sensorEvent;
}
use of android.hardware.SensorEvent in project robolectric by robolectric.
the class ShadowSensorManagerTest method shouldNotCauseConcurrentModificationExceptionSendSensorEvent.
@Test
public void shouldNotCauseConcurrentModificationExceptionSendSensorEvent() {
TestSensorEventListener listener1 = new TestSensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
super.onSensorChanged(event);
sensorManager.unregisterListener(this);
}
};
Sensor sensor = sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
sensorManager.registerListener(listener1, sensor, SensorManager.SENSOR_DELAY_NORMAL);
SensorEvent event = shadow.createSensorEvent();
shadow.sendSensorEventToListeners(event);
assertThat(listener1.getLatestSensorEvent()).hasValue(event);
}
use of android.hardware.SensorEvent in project robolectric by robolectric.
the class ShadowSensorManagerTest method shouldCreateSensorEventWithValueArray.
@Test
public void shouldCreateSensorEventWithValueArray() {
SensorEvent event = ShadowSensorManager.createSensorEvent(3);
assertThat(event.values.length).isEqualTo(3);
}
Aggregations