@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Test public void testObserveTrigger() throws Exception { when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor); final ArgumentCaptor<TriggerEventListener> argumentCaptor = ArgumentCaptor.forClass(TriggerEventListener.class); when(mockSensorManager.requestTriggerSensor( argumentCaptor.capture(), eq(mockSensor) )).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { argumentCaptor.getValue().onTrigger(mock(TriggerEvent.class)); return true; } }); rxSensorManager.observeTrigger(Sensor.TYPE_ACCELEROMETER) .test() .assertValueCount(1); }
/** * Create a {@link Single} that notifies subscribers of a {@link TriggerEvent} on a given {@link Sensor}. * <p> * This is modeled as a {@link Single} because once the sensor detects a trigger event condition, the provided * {@link TriggerEventListener} will be invoked once and then cancelled. To continue receiving trigger events, the * client must call this method for each event intended to be received. * * @param type the {@link Sensor} type to request {@link TriggerEvent}s for * @return A {@link Single} that notifies subscribers of a {@link TriggerEvent} on a given {@link Sensor}. * @since 0.8.0 */ @NonNull @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public Single<TriggerEvent> observeTrigger(final int type) { return Single.create(new SingleOnSubscribe<TriggerEvent>() { @Override public void subscribe(final SingleEmitter<TriggerEvent> emitter) { final Sensor sensor = sensorManager.getDefaultSensor(type); if (sensor == null) { emitter.onError(new SensorNotFoundException(type)); return; } final TriggerEventListener triggerEventListener = new TriggerEventListener() { @Override public void onTrigger(TriggerEvent triggerEvent) { emitter.onSuccess(triggerEvent); } }; final boolean sensorEnabled = sensorManager.requestTriggerSensor(triggerEventListener, sensor); if (!sensorEnabled) { emitter.onError(new SensorListenerException(sensor)); return; } emitter.setDisposable(new Disposable() { boolean disposed = false; @Override public void dispose() { sensorManager.cancelTriggerSensor(triggerEventListener, sensor); disposed = true; } @Override public boolean isDisposed() { return disposed; } }); } }); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Test public void testObserveTriggerOnErrorSensorListenerException() throws Exception { when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor); when(mockSensorManager.requestTriggerSensor( any(TriggerEventListener.class), eq(mockSensor) )).thenReturn(false); rxSensorManager.observeTrigger(Sensor.TYPE_ACCELEROMETER) .test() .assertError(SensorListenerException.class); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Test public void testObserveTriggerDisposable() throws Exception { when(mockSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(mockSensor); when(mockSensorManager.requestTriggerSensor(any(TriggerEventListener.class), eq(mockSensor))).thenReturn(true); Disposable disposable = rxSensorManager.observeTrigger(Sensor.TYPE_ACCELEROMETER).test(); disposable.dispose(); verify(mockSensorManager).cancelTriggerSensor( any(TriggerEventListener.class), eq(mockSensor) ); assertEquals(disposable.isDisposed(), true); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public boolean requestTriggerSensor(TriggerEventListener listener, Sensor sensor) //------------------------------------------------------------------------------- { final int type = sensor.getType(); if (! isEmulatingType(type)) return sensorManager.requestTriggerSensor(listener, sensor); throw new UnsupportedOperationException("Trigger sensor emulation not supported"); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public boolean cancelTriggerSensor(TriggerEventListener listener, Sensor sensor) //------------------------------------------------------------------------------ { final int type = sensor.getType(); if (! isEmulatingType(type)) return sensorManager.cancelTriggerSensor(listener, sensor); throw new UnsupportedOperationException("Trigger sensor emulation not supported"); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public TriggerEventListenerWrapper(@NonNull TriggerEventListener sensorEventListener) { super(Sensor.TYPE_SIGNIFICANT_MOTION, sensorEventListener); }