|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Francesco Azzola |
| 3 | + * Surviving with Android (http://www.survivingwithandroid.com) |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.survivingwithandroid.shaketorefresh.app; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.hardware.Sensor; |
| 21 | +import android.hardware.SensorEvent; |
| 22 | +import android.hardware.SensorEventListener; |
| 23 | +import android.hardware.SensorManager; |
| 24 | +import android.util.Log; |
| 25 | + |
| 26 | +public class ShakeEventManager implements SensorEventListener { |
| 27 | + |
| 28 | + private SensorManager sManager; |
| 29 | + private Sensor s; |
| 30 | + |
| 31 | + |
| 32 | + private static final int MOV_COUNTS = 2; |
| 33 | + private static final int MOV_THRESHOLD = 4; |
| 34 | + private static final float ALPHA = 0.8F; |
| 35 | + private static final int SHAKE_WINDOW_TIME_INTERVAL = 500; // milliseconds |
| 36 | + |
| 37 | + // Gravity force on x,y,z axis |
| 38 | + private float gravity[] = new float[3]; |
| 39 | + |
| 40 | + private int counter; |
| 41 | + private long firstMovTime; |
| 42 | + private ShakeListener listener; |
| 43 | + |
| 44 | + public ShakeEventManager() { |
| 45 | + } |
| 46 | + |
| 47 | + public void setListener(ShakeListener listener) { |
| 48 | + this.listener = listener; |
| 49 | + } |
| 50 | + |
| 51 | + public void init(Context ctx) { |
| 52 | + sManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); |
| 53 | + s = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 54 | + register(); |
| 55 | + } |
| 56 | + |
| 57 | + public void register() { |
| 58 | + sManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onSensorChanged(SensorEvent sensorEvent) { |
| 63 | + float maxAcc = calcMaxAcceleration(sensorEvent); |
| 64 | + Log.d("SwA", "Max Acc ["+maxAcc+"]"); |
| 65 | + if (maxAcc >= MOV_THRESHOLD) { |
| 66 | + if (counter == 0) { |
| 67 | + counter++; |
| 68 | + firstMovTime = System.currentTimeMillis(); |
| 69 | + Log.d("SwA", "First mov.."); |
| 70 | + } else { |
| 71 | + long now = System.currentTimeMillis(); |
| 72 | + if ((now - firstMovTime) < SHAKE_WINDOW_TIME_INTERVAL) |
| 73 | + counter++; |
| 74 | + else { |
| 75 | + resetAllData(); |
| 76 | + counter++; |
| 77 | + return; |
| 78 | + } |
| 79 | + Log.d("SwA", "Mov counter ["+counter+"]"); |
| 80 | + |
| 81 | + if (counter >= MOV_COUNTS) |
| 82 | + if (listener != null) |
| 83 | + listener.onShake(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onAccuracyChanged(Sensor sensor, int i) {} |
| 91 | + |
| 92 | + public void deregister() { |
| 93 | + sManager.unregisterListener(this); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private float calcMaxAcceleration(SensorEvent event) { |
| 98 | + gravity[0] = calcGravityForce(event.values[0], 0); |
| 99 | + gravity[1] = calcGravityForce(event.values[1], 1); |
| 100 | + gravity[2] = calcGravityForce(event.values[2], 2); |
| 101 | + |
| 102 | + float accX = event.values[0] - gravity[0]; |
| 103 | + float accY = event.values[1] - gravity[1]; |
| 104 | + float accZ = event.values[2] - gravity[2]; |
| 105 | + |
| 106 | + float max1 = Math.max(accX, accY); |
| 107 | + return Math.max(max1, accZ); |
| 108 | + } |
| 109 | + |
| 110 | + // Low pass filter |
| 111 | + private float calcGravityForce(float currentVal, int index) { |
| 112 | + return ALPHA * gravity[index] + (1 - ALPHA) * currentVal; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + private void resetAllData() { |
| 117 | + Log.d("SwA", "Reset all data"); |
| 118 | + counter = 0; |
| 119 | + firstMovTime = System.currentTimeMillis(); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + public static interface ShakeListener { |
| 124 | + public void onShake(); |
| 125 | + } |
| 126 | +} |
0 commit comments