-
Notifications
You must be signed in to change notification settings - Fork 9
/
SquatSensorEventListener.java
119 lines (107 loc) · 3.85 KB
/
SquatSensorEventListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import static android.content.Context.SENSOR_SERVICE;
public class SquatSensorEventListener implements SensorEventListener {
private final String TAG = SquatSensorEventListener.class.getSimpleName();
private static int mCount;
private static long mTotalTime;
private static int mAdjustTime;
private static boolean mayBeSquat;
private static long mLastTime;
private static SensorManager mSensorManager;
public Runnable mRunnable;
private boolean hasSensor = false;
public SquatSensorEventListener(Context context, Runnable runnable) {
mayBeSquat = true;
mCount = 0;
mTotalTime = 0;
mRunnable = runnable;
mLastTime = 0;
mSensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
if (mSensorManager == null) {
Log.v(TAG, "Sensors not supported");
}
}
public void reset() {
mayBeSquat = true;
mCount = 0;
mTotalTime = 0;
mLastTime = 0;
}
public int getSquatCount() {
return mCount;
}
public void setSquatCount(int count) {
mCount = count;
}
public void registerListener() {
hasSensor = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 1);
}
public void unregisterListener() {
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
hasSensor = false;
}
}
public boolean hasSensor() {
return hasSensor;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
long currentTimeMillis = System.currentTimeMillis();
long diffTime = currentTimeMillis - mLastTime;
if (diffTime > 100) {
diffTime = 0;
}
mLastTime = currentTimeMillis;
double sqrt = Math.sqrt((sensorEvent.values[0] * sensorEvent.values[0]) + (sensorEvent.values[1] * sensorEvent.values[1]));
sqrt = Math.sqrt((sqrt * sqrt) + (sensorEvent.values[2] * sensorEvent.values[2])) - 9.706650161743164d;
if (mTotalTime == 0) {
if (sqrt < -0.6d) {
mTotalTime = diffTime;
mAdjustTime = 0;
}
} else if (sqrt < -0.2d) {
mTotalTime += diffTime;
mAdjustTime = 0;
} else if (mTotalTime > 0) {
mTotalTime -= diffTime;
}
if (sqrt > 0.0d) {
if (mTotalTime > 225) {
mAdjustTime = (int) (mAdjustTime + diffTime);
mTotalTime += diffTime;
if (sqrt > 3.0d) {
mAdjustTime = (int) (mAdjustTime + diffTime);
}
if (mayBeSquat && mAdjustTime > 450) {
mayBeSquat = false;
mCount += 1;
mTotalTime = 0;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("count: ");
stringBuilder.append(mCount);
Log.v(TAG, stringBuilder.toString());
mRunnable.run();
return;
}
return;
}
mTotalTime = 0;
mAdjustTime = 0;
mayBeSquat = true;
} else if (mAdjustTime > 450) {
diffTime *= 2;
mTotalTime -= diffTime;
mAdjustTime = (int) (mAdjustTime - diffTime);
mayBeSquat = true;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}