Back to project page Do-not-get-annoyed.
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.
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); } } }