Skip to content
Snippets Groups Projects
Commit 6fc31806 authored by Peter Joseph De Jonckheere CESM2014's avatar Peter Joseph De Jonckheere CESM2014
Browse files

Think sig motion is fixed.

parent e46c9d3a
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,8 @@ import android.widget.Toast;
import com.notificationFramework.sedentary.frontEnd.R;
import java.util.Calendar;
/**
* Created by pharmacy on 17/01/2018.
*/
......@@ -28,6 +30,10 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
private Sensor md;
private TriggerEventListener tel;
private AlarmManager am;
private float prevStepCount;
private long prevTimeStamp;
private int minutes;
//Set up alarm manager to fire in one hour, and set up significant motion sensor to fire and call monitor which
......@@ -38,6 +44,7 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
Log.d("SIGMOT", "STARTED");
am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
setUpClock();
setUpDailyProgress();
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
//Need to test if this produces null to avoid NULLPOINTEREXCEPTION
md = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
......@@ -47,6 +54,7 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
monitor();
}
};
stepCounter();
mSensorManager.requestTriggerSensor(tel, md);
return START_STICKY;
}
......@@ -75,12 +83,20 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
Intent i = new Intent(this, com.notificationFramework.stimulus.SedentaryStimulus.class);
PendingIntent pi = PendingIntent.getBroadcast(this, R.integer.alarm_rc, i, 0);
am.cancel(pi);
stepCounter();
stepCalc();
setUpClock();
mSensorManager.requestTriggerSensor(tel, md);
}
private void stepCalc(){
float stepDifference = StepCounter.stepCount - prevStepCount;
long timeDifference = StepCounter.timestamp - prevTimeStamp;
float averageTime = ((timeDifference/3600)+ (stepDifference/60))/2;
minutes = minutes + Math.round(averageTime);
prevStepCount = StepCounter.stepCount;
prevTimeStamp = StepCounter.timestamp;
storeData();
}
private void stepCounter(){
Log.d("SIGMOT", "COUNTING");
......@@ -88,6 +104,29 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
startService(intent);
}
private void setUpDailyProgress(){
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
if(currentDay == sharedPref.getInt(getString(R.string.progress_day), 0)) {
minutes = sharedPref.getInt(getString(R.string.daily_progress), 0);
} else {
minutes = 0;
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.progress_day), currentDay);
editor.putInt(getString(R.string.daily_progress), minutes);
editor.commit();
}
}
private void storeData(){
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.daily_progress), minutes);
editor.commit();
}
@Nullable
......
......@@ -23,11 +23,9 @@ import java.util.Calendar;
public class StepCounter extends Service implements SensorEventListener {
private Sensor stepCounter;
private float stepCount;
private float prevStepCount = 0;
private int minutes;
static float stepCount;
static long timestamp;
private SensorManager mSensorManager;
private int seconds = 0;
@Override
......@@ -37,55 +35,20 @@ public class StepCounter extends Service implements SensorEventListener {
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mSensorManager.registerListener(this, stepCounter, 600000000);
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
setUpDailyProgress();
return START_STICKY;
}
private void setUpDailyProgress(){
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
if(currentDay == sharedPref.getInt(getString(R.string.progress_day), 0)) {
minutes = sharedPref.getInt(getString(R.string.daily_progress), 0);
} else {
minutes = 0;
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.progress_day), currentDay);
editor.putInt(getString(R.string.daily_progress), minutes);
editor.commit();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
stepCount = event.values[0];
timestamp = event.timestamp/1000000000;
Log.d("SIGMOT", "STEPCHANGING");
if (prevStepCount == 0) {
prevStepCount = event.values[0];
} else {
stepCount = event.values[0];
if(stepCount > prevStepCount){
seconds++;
prevStepCount = stepCount;
if(seconds == 60) {
minutes++;
seconds = 0;
Toast saved = Toast.makeText(this, R.string.minute_progress,Toast.LENGTH_LONG);
saved.show();
}
} else {
mSensorManager.unregisterListener(this);
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.daily_progress), minutes);
editor.commit();
Log.d("SIGMOT", "STOPPEDSTEPPING");
stopSelf();
}
}
}
@Override
......@@ -98,4 +61,4 @@ public class StepCounter extends Service implements SensorEventListener {
public IBinder onBind(Intent intent) {
return null;
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment