Search in sources :

Example 11 with SensorEvent

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);
    }
}
Also used : SensorEventListener(android.hardware.SensorEventListener) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 12 with SensorEvent

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();
        }
    });
}
Also used : OnProximityListener(com.example.android.recipe071.ProximityManager.OnProximityListener) SensorEvent(android.hardware.SensorEvent)

Example 13 with SensorEvent

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;
}
Also used : ClassParameter(org.robolectric.util.ReflectionHelpers.ClassParameter) SensorEvent(android.hardware.SensorEvent)

Example 14 with 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);
}
Also used : SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor) Test(org.junit.Test)

Example 15 with SensorEvent

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);
}
Also used : SensorEvent(android.hardware.SensorEvent) Test(org.junit.Test)

Aggregations

SensorEvent (android.hardware.SensorEvent)18 Sensor (android.hardware.Sensor)11 SensorEventListener (android.hardware.SensorEventListener)8 Test (org.junit.Test)6 TextView (android.widget.TextView)3 View (android.view.View)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SensorManager (android.hardware.SensorManager)1 Message (android.os.Message)1 ViewGroup (android.view.ViewGroup)1 OnProximityListener (com.example.android.recipe071.ProximityManager.OnProximityListener)1 LangTestActivity (com.example.test.andlang.test.LangTestActivity)1 ShakeSensorUtil (com.example.test.andlang.util.ShakeSensorUtil)1 CircularProgressView (com.github.rahatarmanahmed.cpv.CircularProgressView)1 Field (java.lang.reflect.Field)1 ClassParameter (org.robolectric.util.ReflectionHelpers.ClassParameter)1