@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); }
@Override public void onTrigger(TriggerEvent event) { Log.i("BumpMonitor", "Sensor triggered"); //value[0] = 1.0 when the sensor triggers. 1.0 is the only allowed value. long curTime = System.currentTimeMillis(); // only allow one update every 100ms. if (event.sensor.getType() == Sensor.TYPE_SIGNIFICANT_MOTION) { if ((curTime - lastUpdate) > CHECK_INTERVAL) { lastUpdate = curTime; /* * Send Alert */ Message message = new Message(); message.what = EventTrigger.BUMP; try { if (serviceMessenger != null) { serviceMessenger.send(message); } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //re-register the listener (it finishes after each event) boolean registered = sensorMgr.requestTriggerSensor(sensorListener, bumpSensor); Log.i("BumpMonitor", "Significant motion sensor re-registered: "+registered); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private void bindTriggerSwitch() { checkedChanges(triggerSwitch) .switchMap(new Function<Boolean, Observable<TriggerEvent>>() { @Override public Observable<TriggerEvent> apply(Boolean isChecked) { if (isChecked) { return rxSensorManager.observeTrigger(Sensor.TYPE_SIGNIFICANT_MOTION) .toObservable(); } else { return Observable.empty(); } } }) .subscribe(new Observer<TriggerEvent>() { @Override public void onSubscribe(Disposable disposable) { compositeDisposable.add(disposable); } @Override public void onNext(TriggerEvent event) { toast("Trigger event: " + event.toString()); triggerSwitch.setChecked(false); } @Override public void onError(Throwable e) { toast("Trigger event error: " + e.getMessage()); } @Override public void onComplete() { toast("This should not have just completed! UNPOSSIBLE!"); } }); }
/** * 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; } }); } }); }
@Override public void onTrigger(TriggerEvent event) { if (event.values[0] == 1) { mTextView.append(mContext.getString(R.string.sig_motion) + "\n"); mTextView.append(mContext.getString(R.string.sig_motion_auto_disabled) + "\n"); } // Sensor is auto disabled. }
@Override public void onTrigger(TriggerEvent event) { Logger.e(TAG, "NAME = " + event.sensor.getName()); Logger.e(TAG, "TIME = " + event.timestamp); }
@Override public void onTrigger(TriggerEvent event) { triggerCallback.handleOnSensorTriggerEvent(event.sensor, event.sensor.getType(), event.values); }
@Override public void onTrigger(TriggerEvent event) { mTextView.setText("Significant Motion Triggered!, Click again to request again."); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @OnTrigger public void testSignificantMotionTrigger(@NonNull TriggerEvent event) { updateTextViewWithEventData(mSignificantMotionEventOutputTextView, event); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) void updateTextViewWithEventData(@NonNull TextView textView, @NonNull TriggerEvent event) { updateTextViewWithEventData(textView, event.timestamp, event.values); }
@Override public void onTrigger( TriggerEvent event ) { Log.i( "SENSOR", "Significant movement" ); }
public void onTrigger(TriggerEvent event) { // being a one shot sensor trigger, it is inherently cancelled // if you are interested in more such events, call the request trigger again // SensorManager.requestTriggerSensor(this, Motion); Log.d ( TAG, "SignificantMotionTriggerListener::onTrigger() ... " + new Date() ) ; //startActivity (handleSignificantMotionIntent) ; //startActivity (handleSignificantMotionIntent) ; //context.startActivity final Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()) .setContentTitle( "Motion ! ") .setContentText("Motions !") .setPriority(Notification.PRIORITY_MAX) .setSmallIcon(R.mipmap.ic_launcher) .setDefaults(Notification.DEFAULT_VIBRATE); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1346436, notificationBuilder.build()); SignificantMotionTriggerListener significantMotionTriggerListener = new SignificantMotionTriggerListener(); sensorManager.requestTriggerSensor ( significantMotionTriggerListener, sensorSignificantMotion ); }