forked from linglongxin24/DylanStepCount
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64dbc96
commit c84ceed
Showing
10 changed files
with
297 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
app/src/main/java/cn/bluemobi/dylan/step/pedometer/StepDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cn.bluemobi.dylan.step.pedometer; | ||
|
||
/* | ||
* Pedometer - Android App | ||
* Copyright (C) 2009 Levente Bagi | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
import android.hardware.Sensor; | ||
import android.hardware.SensorEvent; | ||
import android.hardware.SensorEventListener; | ||
import android.hardware.SensorManager; | ||
|
||
/** | ||
* Detects steps and notifies all listeners (that implement StepListener). | ||
* @author Levente Bagi | ||
* @todo REFACTOR: SensorListener is deprecated | ||
*/ | ||
public class StepDetector implements SensorEventListener { | ||
|
||
private float mLimit = 10; | ||
private float mLastValues[] = new float[3*2]; | ||
private float mScale[] = new float[2]; | ||
private float mYOffset; | ||
|
||
private float mLastDirections[] = new float[3*2]; | ||
private float mLastExtremes[][] = { new float[3*2], new float[3*2] }; | ||
private float mLastDiff[] = new float[3*2]; | ||
private int mLastMatch = -1; | ||
|
||
private StepListener mListener; | ||
|
||
public StepDetector() { | ||
int h = 480; | ||
mYOffset = h * 0.5f; | ||
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2))); | ||
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX))); | ||
} | ||
|
||
public void setSensitivity(float sensitivity) { | ||
mLimit = sensitivity; | ||
} | ||
|
||
public void setStepListener(StepListener sl) { | ||
mListener = sl; | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
public void onSensorChanged(SensorEvent event) { | ||
Sensor sensor = event.sensor; | ||
synchronized (this) { | ||
if (sensor.getType() == Sensor.TYPE_ORIENTATION) { | ||
} | ||
else { | ||
int j = (sensor.getType() == Sensor.TYPE_ACCELEROMETER) ? 1 : 0; | ||
if (j == 1) { | ||
float vSum = 0; | ||
for (int i=0 ; i<3 ; i++) { | ||
final float v = mYOffset + event.values[i] * mScale[j]; | ||
vSum += v; | ||
} | ||
int k = 0; | ||
float v = vSum / 3; | ||
|
||
float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0)); | ||
if (direction == - mLastDirections[k]) { | ||
|
||
int extType = (direction > 0 ? 0 : 1); | ||
mLastExtremes[extType][k] = mLastValues[k]; | ||
float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]); | ||
|
||
if (diff > mLimit) { | ||
|
||
boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3); | ||
boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3); | ||
boolean isNotContra = (mLastMatch != 1 - extType); | ||
|
||
if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) { | ||
if(mListener != null){ | ||
mListener.onStep(); | ||
} | ||
mLastMatch = extType; | ||
} else { | ||
mLastMatch = -1; | ||
} | ||
} | ||
mLastDiff[k] = diff; | ||
} | ||
mLastDirections[k] = direction; | ||
mLastValues[k] = v; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void onAccuracyChanged(Sensor sensor, int accuracy) { | ||
|
||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
app/src/main/java/cn/bluemobi/dylan/step/pedometer/StepListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cn.bluemobi.dylan.step.pedometer; | ||
public interface StepListener { | ||
public void onStep(); | ||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/cn/bluemobi/dylan/step/pedometer/StepsDetectService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cn.bluemobi.dylan.step.pedometer; | ||
|
||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.hardware.Sensor; | ||
import android.hardware.SensorManager; | ||
import android.os.IBinder; | ||
|
||
public class StepsDetectService extends Service { | ||
|
||
private SensorManager mSensorManager; | ||
private Sensor mSensor; | ||
private StepDetector mStepDetector; | ||
|
||
public static int steps = 0; | ||
|
||
@Override | ||
public void onCreate() { | ||
|
||
mStepDetector = new StepDetector(); | ||
registerDetector(); | ||
mStepDetector.setStepListener(new StepListener() { | ||
@Override | ||
public void onStep(){ | ||
steps ++; | ||
if(mOnStepDetectListener != null){ | ||
mOnStepDetectListener.onStepDetect(steps); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
steps = 0; | ||
return super.onStartCommand(intent, flags, startId); | ||
} | ||
|
||
public void registerDetector() { | ||
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | ||
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | ||
mSensorManager.registerListener(mStepDetector, mSensor, SensorManager.SENSOR_DELAY_FASTEST); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
mOnStepDetectListener = null; | ||
unRegisterDector(); | ||
steps = 0; | ||
} | ||
|
||
public void unRegisterDector(){ | ||
if(mStepDetector != null && mSensorManager != null){ | ||
mStepDetector.setStepListener(null); | ||
mSensorManager.unregisterListener(mStepDetector); | ||
} | ||
} | ||
|
||
public interface OnStepDetectListener { | ||
public void onStepDetect(int steps); | ||
} | ||
|
||
public static OnStepDetectListener mOnStepDetectListener = null; | ||
|
||
public static void setOnStepDetectListener(OnStepDetectListener mListener){ | ||
mOnStepDetectListener = mListener; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...uemobi/dylan/step/step/pojo/StepData.java → ...uemobi/dylan/step/step/bean/StepData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.