RATCHET

RATCHET Labs

04/06/2011

How to create a ShakeListener for Android

Here is the implementation of my ShakeListener that I used for ShotShakr. I was very lucky to find this piece of code that does all the heavy lifting for me.

public class ShakeListener implements SensorListener 
{
  private static final int FORCE_THRESHOLD = 350;
  private static final int TIME_THRESHOLD = 100;
  private static final int SHAKE_TIMEOUT = 500;
  private static final int SHAKE_DURATION = 1000;
  private static final int SHAKE_COUNT = 3;

  private SensorManager mSensorMgr;
  private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
  private long mLastTime;
  private OnShakeListener mShakeListener;
  private Context mContext;
  private int mShakeCount = 0;
  private long mLastShake;
  private long mLastForce;

  public interface OnShakeListener
  {
    public void onShake();
  }

  public ShakeListener(Context context) 
  { 
    mContext = context;
    resume();
  }

  public void setOnShakeListener(OnShakeListener listener)
  {
    mShakeListener = listener;
  }

  public void resume() {
    mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    if (mSensorMgr == null) {
      throw new UnsupportedOperationException("Sensors not supported");
    }
    boolean supported = mSensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
    if (!supported) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      throw new UnsupportedOperationException("Accelerometer not supported");
    }
  }

  public void pause() {
    if (mSensorMgr != null) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      mSensorMgr = null;
    }
  }

  public void onAccuracyChanged(int sensor, int accuracy) { }

  public void onSensorChanged(int sensor, float[] values) 
  {
    if (sensor != SensorManager.SENSOR_ACCELEROMETER) return;
    long now = System.currentTimeMillis();

    if ((now - mLastForce) > SHAKE_TIMEOUT) {
      mShakeCount = 0;
    }

    if ((now - mLastTime) > TIME_THRESHOLD) {
      long diff = now - mLastTime;
      float speed = Math.abs(values[SensorManager.DATA_X] + values[SensorManager.DATA_Y] + values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
      if (speed > FORCE_THRESHOLD) {
        if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
          mLastShake = now;
          mShakeCount = 0;
          if (mShakeListener != null) { 
            mShakeListener.onShake(); 
          }
        }
        mLastForce = now;
      }
      mLastTime = now;
      mLastX = values[SensorManager.DATA_X];
      mLastY = values[SensorManager.DATA_Y];
      mLastZ = values[SensorManager.DATA_Z];
    }
  }

In my implementation of this code i bumped up the FORCE_THRESHOLD to 1050. This seemed like a good threshold for as it was not to sensitive where you would loose any shot currently on display if you accidentally bumped your arm.

Implementing your ShakerListener on your activity

ShakeListener mShake = new ShakeListener(this);
mShake.setOnShakeListener(new ShakeListener.OnShakeListener() {

            public void onShake() {
                 // DO WORK
            }
});

It is really that simple. I have had a lot of luck with this implementation and hope you will to.

credit to Matthew Wiggins for the code.

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

11/10/2010

1 Hour Dev Tic Tac Toe Challenge Submissions

Everyone did a great job with the limited time that was given.  It is amazing what can get done when a team is all working towards the same goal.

Here are the  submissions from the teams in no particular order. There were 4 html/css/js solutions and one mobile/droid. Click on the images to play(except the droid version, link coming soon to add to your phone).

The winners will be announced this Friday at the company meeting. Don’t forget to vote for your favorite at the bottom of the post.

Team Purple (Rachelle, Sean, Chris, Laura, Adam)

Team Blue (Alissa,Julian,Mathew Jones,Krebs,Chad)

Team Green(Noah,Charlie,Zoe,David)


Team Red (Ryan,Sarah,Dustin,Shane)

Team White (Brian,Mike,Larry,Jon)

Please vote for your favorite.

Which Tic Tac Toe game did you like best ?

View Results

Loading ... Loading ...

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

10/27/2010

ShotShakr Android Application

Released in May by Matt Krebs and Robb Akerson. The ShotShakr app surpassed  over 15,000 downloads  in the Android Market.  The  app suggest shots, provides recipes and allows the user to socialize the experience via Twitter and Facebook.

Check it out http://www.appbrain.com/app/com.ratchet.ShotShakr 

  • Facebook
  • Twitter
  • Digg
  • Print
  • email