Skip to content
Nishant Srivastava edited this page Dec 21, 2017 · 15 revisions

The way listeners are passed, registered and unregistered has been changed since version 1.4.0.

Setup

Initialize Sensey,under your onCreate() in the activity/service, call

Sensey.getInstance().init(context);

If you wish to alter the sampling period of data by Sensey, use the below init function

Sensey.getInstance().init(context,samplingPeriod)

where samplingPeriod can be anyone from the below options

  • Sensey.SAMPLING_PERIOD_FASTEST
  • Sensey. SAMPLING_PERIOD_GAME
  • Sensey. SAMPLING_PERIOD_NORMAL // Default
  • Sensey. SAMPLING_PERIOD_UI

To stop Sensey, under your onDestroy() in the activity/service, call

 // *** IMPORTANT ***
 // Stop Sensey and release the context held by it
 Sensey.getInstance().stop();

Next to enable detection for

Shake

  • Create an instance of ShakeListener
ShakeDetector.ShakeListener shakeListener=new ShakeDetector.ShakeListener() {
   @Override public void onShakeDetected() {
       // Shake detected, do something
   }
   
   @Override public void onShakeStopped() {
       // Shake stopped, do something
   }

};
  • Now to start listening for Shake gesture, pass the instance shakeListener to startShakeDetection() function
Sensey.getInstance().startShakeDetection(shakeListener);

If you want to modify the threshold and time before declaring that shake gesture is stopped, use

Sensey.getInstance().startShakeDetection(threshold,timeBeforeDeclaringShakeStopped,shakeListener);
  • To stop listening for Shake gesture, pass the instance shakeListener to stopShakeDetection() function
Sensey.getInstance().stopShakeDetection(shakeListener);

Movement

  • Create an instance of MovementListener
MovementDetector.MovementListener movementListener=new MovementDetector.MovementListener() {
   @Override public void onMovement() {
       // Movement detected, do something
   }
   
   @Override public void onStationary() {
       // Movement stopped, do something
   }

};
  • Now to start listening for Movement gesture, pass the instance movementListener to startMovementDetection() function
Sensey.getInstance().startMovementDetection(movementListener);

If you want to modify the threshold and time before declaring that movement gesture is stopped, use

Sensey.getInstance().startMovementDetection(threshold,timeBeforeDeclaringStationary,movementListener);
  • To stop listening for Movement gesture, pass the instance movementListener to stopMovementDetection() function
Sensey.getInstance().stopMovementDetection(movementListener);

PickupDevice

  • Create an instance of PickupDeviceListener
PickupDeviceDetector.PickupDeviceListener pickupDeviceListener = new PickupDeviceDetector.PickupDeviceListener() {
        @Override
        public void onDevicePickedUp() {
            // Device picked up
        }

        @Override
        public void onDevicePutDown() {
           // Device put down
        }
};
  • Now to start listening for PickupDevice gesture, pass the instance pickupDeviceListener to startPickupDeviceDetection() function
Sensey.getInstance().startPickupDeviceDetection(pickupDeviceListener);
  • To stop listening for PickupDevice gesture, pass the instance pickupDeviceListener to stopPickupDeviceDetection() function
Sensey.getInstance().stopPickupDeviceDetection(pickupDeviceListener);

Scoop

  • Create an instance of ScoopListener
ScoopDetector.ScoopListener scoopListener=new ScoopDetector.ScoopListener() {
        @Override
        public void onScooped() {
            // Scoop gesture detected, do something
        }
};
  • Now to start listening for Scoop gesture, pass the instance scoopListener to startScoopDetection() function
Sensey.getInstance().startScoopDetection(scoopListener);
  • To stop listening for Scoop gesture, pass the instance scoopListener to stopScoopDetection() function
Sensey.getInstance().stopScoopDetection(scoopListener);

Steps

  • Create an instance of ScoopListener
StepListener stepListener= new StepListener() {
        @Override
        public void stepInformation(final int noOfSteps, 
                                    final float distanceInMeter, 
                                    final int stepActivityType) {
        // Steps are detected
        String typeOfActivity;
        switch (stepActivityType) {
            case StepDetectorUtil.ACTIVITY_RUNNING:
                // Running;
                break;
            case StepDetectorUtil.ACTIVITY_WALKING:
                // Walking;
                break;
            default:
                // Not moving, still;
                break;
        }
            
        }
};
  • Now to start listening for Steps, pass the instance stepListener to startStepDetection() function
Sensey.getInstance().startStepDetection(context, stepListener, StepDetectorUtil.MALE);

where StepDetectorUtil has 2 possible values MALE and FEMALE as the third argument.

  • To stop listening for Steps, pass the instance stepListener to stopStepDetection() function
Sensey.getInstance().stopStepDetection(stepListener);

Chop

  • Create an instance of ChopListener
ChopDetector.ChopListener chopListener=new ChopDetector.ChopListener() {
   @Override public void onChop() {
       // Chop gesture detected, do something
   }
};
  • Now to start listening for Chop gesture, pass the instance chopListener to startChopDetection() function
Sensey.getInstance().startChopDetection(chopListener);

If you want to modify the threshold and time for chop gesture, use

Sensey.getInstance().startChopDetection(threshold,timeForChopGesture,chopListener);
  • To stop listening for Chop gesture, pass the instance chopListener to stopChopDetection() function
Sensey.getInstance().stopChopDetection(chopListener);

WristTwist

  • Create an instance of WristTwistListener
WristTwistDetector.WristTwistListener wristTwistListener=new WristTwistDetector.WristTwistListener() {
   @Override public void onWristTwist() {
       // Wrist Twist gesture detected, do something
   }
};
  • Now to start listening for Wrist Twist gesture, pass the instance wristTwistListener to startWristTwistDetection() function
Sensey.getInstance().startWristTwistDetection(wristTwistListener);

If you want to modify the threshold and time for wrist twist gesture, use

Sensey.getInstance().startWristTwistDetection(threshold,timeForWristTwistGesture,wristTwistListener);
  • To stop listening for Wrist Twist gesture, pass the instance wristTwistListener to stopWristTwistDetection() function
Sensey.getInstance().stopWristTwistDetection(wristTwistListener);

SoundLevel

  • Create an instance of SoundLevelListener
SoundLevelDetector.SoundLevelListener soundLevelListener=new SoundLevelDetector.SoundLevelListener() {
   @Override public void onSoundDetected(float level) {
       //Sound Level in dB is detected, do something
   }
};
  • Now to start listening for Sound Level, pass the instance soundLevelListener to startSoundLevelDetection() function
Sensey.getInstance().startSoundLevelDetection(soundLevelListener);
  • To stop listening for Sound Level, pass the instance soundLevelListener to stopSoundLevelDetection() function
Sensey.getInstance().stopSoundLevelDetection(soundLevelListener);

Flip

  • Create an instance of FlipListener
FlipDetector.FlipListener flipListener=new FlipDetector.FlipListener() {
    @Override public void onFaceUp() {
       // Device Facing up
    }

    @Override public void onFaceDown() {
      // Device Facing down
    }
};
  • Now to start listening for Flip gesture, pass the instance flipListener to startFlipDetection() function
Sensey.getInstance().startFlipDetection(flipListener);
  • To stop listening for Flip gesture, pass the instance flipListener to stopFlipDetection() function
Sensey.getInstance().stopFlipDetection(flipListener);

RotationAngle

  • Create an instance of RotationAngleListener
RotationAngleDetector.RotationAngleListener rotationAngleListener =new RotationAngleDetector.RotationAngleListener() {
    @Override
    public void onRotation(float angleInAxisX, float angleInAxisY, float angleInAxisZ) {
      // Do something with the angles, values are in degrees
  }
};
  • Now to start listening for RotationAngle gesture, pass the instance rotationAngleListener to startRotationAngleDetection() function
Sensey.getInstance().startRotationAngleDetection(rotationAngleListener);
  • To stop listening for RotationAngle gesture, pass the instance rotationAngleListener to stopRotationAngleDetection() function
Sensey.getInstance().stopRotationAngleDetection(rotationAngleListener);

TiltDirection

  • Create an instance of TiltDirectionListener
TiltDirectionDetector.TiltDirectionListener tiltDirectionListener=new TiltDirectionDetector.TiltDirectionListener() {
  @Override
  public void onTiltInAxisX(int direction) {
    // Do something with tilt direction on x-axis
  }

  @Override
  public void onTiltInAxisY(int direction) {
    // Do something with tilt direction on y-axis
  }

  @Override
  public void onTiltInAxisZ(int direction) {
    // Do something with tilt direction on z-axis
  }
};

where direction can have either of the value:TiltDirectionDetector.DIRECTION_CLOCKWISE or TiltDirectionDetector.DIRECTION_ANTICLOCKWISE

  • Now to start listening for TiltDirection gesture, pass the instance TiltDirectionListener to startTiltDirectionDetection() function
Sensey.getInstance().startTiltDirectionDetection(TiltDirectionListener);
  • To stop listening for TiltDirection gesture, pass the instance TiltDirectionListener to stopTiltDirectionDetection() function
Sensey.getInstance().stopTiltDirectionDetection(TiltDirectionListener);

Orientation

  • Create an instance of OrientationListener
OrientationDetector.OrientationListener orientationListener=new OrientationDetector.OrientationListener() {
  @Override public void onTopSideUp() {
     // Top side of device is up
  }

  @Override public void onBottomSideUp() {
     // Bottom side of device is up
  }

  @Override public void onRightSideUp() {
     // Right side of device is up
  }

  @Override public void onLeftSideUp() {
     // Left side of device is up       
  }
};
  • Now to start listening for Orientation gesture, pass the instance orientationListener to startOrientationDetection() function
Sensey.getInstance().startOrientationDetection(orientationListener);
  • To stop listening for Orientation gesture, pass the instance orientationListener to stopOrientationDetection() function
Sensey.getInstance().stopOrientationDetection(orientationListener);

Proximity

  • Create an instance of ProximityListener
ProximityDetector.ProximityListener proximityListener=new ProximityDetector.ProximityListener() {
   @Override public void onNear() {
        // Near to device
   }

   @Override public void onFar() {
        // Far from device
   }
};
  • Now to start listening for Orientation gesture, pass the instance proximityListener to startProximityDetection() function
Sensey.getInstance().startProximityDetection(proximityListener);
  • To stop listening for Orientation gesture, pass the instance proximityListener to stopProximityDetection() function
Sensey.getInstance().stopProximityDetection(proximityListener);

Wave

  • Create an instance of WaveListener
WaveDetector.WaveListener waveListener=new WaveDetector.WaveListener() {
   @Override public void onWave() {
        // Wave of hand gesture detected
   }
};
  • Now to start listening for Wave gesture, pass the instance waveListener to startWaveDetection() function
Sensey.getInstance().startWaveDetection(waveListener);
  • To stop listening for Wave gesture, pass the instance waveListener to stopWaveDetection() function
Sensey.getInstance().stopWaveDetection(waveListener);

Light

  • Create an instance of LightListener
LightDetector.LightListener lightListener=new LightDetector.LightListener() {
   @Override public void onDark() {
      // Dark
   }

   @Override public void onLight() {
      // Not Dark
   }
};
  • Now to start listening for Orientation gesture, pass the instance lightListener to startLightDetection() function
Sensey.getInstance().startLightDetection(lightListener);
  • To stop listening for Orientation gesture, pass the instance lightListener to stopLightDetection() function
Sensey.getInstance().stopLightDetection(lightListener);

For Touch-based gestures

IMPORTANT : Implement this to intercept touch actions in activity by overriding the dispatchTouchEvent.

 @Override public boolean dispatchTouchEvent(MotionEvent event) {
    // Setup onTouchEvent for detecting type of touch gesture
    Sensey.getInstance().setupDispatchTouchEvent(event);
    return super.dispatchTouchEvent(event);
 }

PinchScale

  • Create an instance of PinchScaleListener
PinchScaleDetector.PinchScaleListener pinchScaleListener=new PinchScaleDetector.PinchScaleListener() {
   @Override public void onScale(ScaleGestureDetector scaleGestureDetector, boolean isScalingOut) {
        if (isScalingOut) {
           // Scaling Out;
        } else {
          // Scaling In
        }
   }

   @Override public void onScaleStart(ScaleGestureDetector scaleGestureDetector) {
          // Scaling Started
   }

   @Override public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
          // Scaling Stopped
   }
};
  • Now to start listening for PinchScale gesture, pass the instance pinchScaleListener to startPinchScaleDetection() function
Sensey.getInstance().startPinchScaleDetection(pinchScaleListener);
  • To stop listening for PinchScale gesture, simply call stopPinchScaleDetection() function
Sensey.getInstance().stopPinchScaleDetection();

Don't forget to implement dispatchTouchEvent() as explained here

TouchType

  • Create an instance of TouchTypListener
TouchTypeDetector.TouchTypListener touchTypListener=new TouchTypeDetector.TouchTypListener() {
   @Override public void onTwoFingerSingleTap() {
         // Two fingers single tap
   }

   @Override public void onThreeFingerSingleTap() {
         // Three fingers single tap
   }

   @Override public void onDoubleTap() {
         // Double tap
   }

   @Override public void onScroll(int scrollDirection) {
     switch (scrollDirection) {
      case TouchTypeDetector.SCROLL_DIR_UP:
        // Scrolling Up
        break;
      case TouchTypeDetector.SCROLL_DIR_DOWN:
        // Scrolling Down
        break;
      case TouchTypeDetector.SCROLL_DIR_LEFT:
        // Scrolling Left
        break;
      case TouchTypeDetector.SCROLL_DIR_RIGHT:
        // Scrolling Right
        break;
      default:
        // Do nothing
        break;
    }
   }

   @Override public void onSingleTap() {
         // Single tap
   }

   @Override public void onSwipe(int swipeDirection) {
     switch (swipeDirection) {
      case TouchTypeDetector.SWIPE_DIR_UP:
        // Swipe Up
        break;
      case TouchTypeDetector.SWIPE_DIR_DOWN:
        // Swipe Down
        break;
      case TouchTypeDetector.SWIPE_DIR_LEFT:
        // Swipe Left
        break;
      case TouchTypeDetector.SWIPE_DIR_RIGHT:
        // Swipe Right
        break;
      default:
        //do nothing
        break;
    }
   }

   @Override public void onLongPress() {
         // Long press
   }
};
  • Now to start listening for TouchType gesture, pass the instance touchTypListener to startTouchTypeDetection() function
Sensey.getInstance().startTouchTypeDetection(touchTypListener);
  • To stop listening for TouchType gesture, simply call stopTouchTypeDetection() function
Sensey.getInstance().stopTouchTypeDetection();

Don't forget to implement dispatchTouchEvent() as explained here