Android Open Source - Do-not-get-annoyed Shake Detector






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mn100013d.pmu.services;
//ww  w.j ava 2 s.  c  o  m
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;

public abstract class ShakeDetector {

  public static final int INIFITE = 1;
  public static final int SINGLE_SHAKE = 2;
  
  private SensorManager mSensorManager;
  private float mAccel;         // acceleration apart from gravity
  private float mAccelCurrent;     // current acceleration including gravity
  private float mAccelLast;       // last acceleration including gravity
  private long shakeStartTime = System.currentTimeMillis();
  private Context context;
  private Boolean shakeStarted = false;
  private Boolean active = false;
  private int mode;

  public ShakeDetector(Context context) {
    this.context = context;
    mSensorManager = (SensorManager) this.context
        .getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_GAME);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;
  }

  private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
      if (active){
        float x = se.values[0];
        float y = se.values[1];
        float z = se.values[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta; 
        long currTime = System.currentTimeMillis();
        if (mAccel > 2) {
          if (!shakeStarted) {
            shakeStarted = true;
            shakeDetected();
          }
          shakeStartTime = currTime;
        } else {
          if (currTime - shakeStartTime > 1000 && shakeStarted) {
            shakeStarted = false;
            shakeCompleted();
            shakeStartTime = currTime;
          }
  
        }
      }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
      
    }
  };
  public void setSensitivity(int sensitivity){
    switch(sensitivity){
    case 0: 
      mSensorManager.unregisterListener(mSensorListener);
      break;
    case 1:
      mSensorManager.unregisterListener(mSensorListener);
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_NORMAL);
      break;
    case 2:
      mSensorManager.unregisterListener(mSensorListener);
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_GAME);
      break;
    case 3:
      mSensorManager.unregisterListener(mSensorListener);
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_FASTEST);
      break;
    }
  }
  public Boolean getState(){
    return active;
  }
  public void activate(int mode) {
    this.mode = mode;
    active = true;
  }
  public void deactivate() {
    active = false;
  }
  public void setMode(int mode){
    this.mode = mode;
  }
  public abstract void onShakeDetected();
  public abstract void onShakeCompletion();
  /**
   * Time between two active detector states if mode is set to INFINITE detection 
   * */
  public abstract int setTimeout();
  private void shakeDetected(){
    onShakeDetected();
  }
  public int getMode(){
    return this.mode;
  }
  private int getTimeout(){
    return setTimeout();
  }
  private void shakeCompleted(){
    this.deactivate();
      if (getTimeout() > 0){
        new Postponer().execute(this);
      }
    onShakeCompletion();
  }
  private class Postponer extends AsyncTask<ShakeDetector, Void, Void>{
    ShakeDetector sDetector;
    @Override
    protected Void doInBackground(ShakeDetector... params) {
      this.sDetector = params[0];
      try {
        Thread.sleep(sDetector.getTimeout());
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return null;
    }
    @Override
    protected void onPostExecute(Void result) {
      sDetector.activate(sDetector.getMode());
      super.onPostExecute(result);
    }
    
    
  }
}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java