Skip to content
Nishant Srivastava edited this page Oct 15, 2016 · 15 revisions

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

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

Sensey.getInstance().init(context);

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
   }
};
  • 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 , pass an int as value

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

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);

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 finger single tap
   }

   @Override public void onThreeFingerSingleTap() {
         // Three finger 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

Clone this wiki locally