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

Major changes to the way in which accelerometer detection works.

parent b8c22274
No related branches found
No related tags found
No related merge requests found
......@@ -75,6 +75,7 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
if(strategy == null){
strategy = defaultStrategy;
}
Intent intent1 = new Intent(context, strategy.getClass());
context.stopService(intent1);
//Restarts the chosen strategy process.
......
......@@ -36,19 +36,27 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
private AlarmManager am;
//private Context context;
private int startId;
private float[] history = new float[3];
private int historicMovement = 0;
private long historyTime;
private int prevMinutes = 0;
private int minutes;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ACCEL", "CREATED ACCEL");
historyTime = SystemClock.elapsedRealtimeNanos();
this.startId = startId;
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 60000000);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 600000000);
//Sampling period is still far too often here.
//Parameters to be set here such as delay etc.
//Slightly better when app is not in foreground
am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
minutes = sharedPref.getInt(getString(R.string.daily_progress), 0);
prevMinutes = minutes;
setUpClock();
return START_STICKY;
}
......@@ -77,47 +85,43 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
Intent i = new Intent(getBaseContext(), com.example.stimulus.SedentaryStimulus.class);
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
stepCounter();
setUpClock();
}
private void stepCounter(){
Intent intent = new Intent(this, StepCounter.class);
startService(intent);
}
@Override
public void onSensorChanged(SensorEvent event) {
//Needs major tuning currently vibration triggers motion
//Need to work out the maths on this
//Google API Activity Detection client??
//Maybe work maths out and use this as 3rd option
//2 Manual options and an existing option
float[] gravity = new float[3];
float[] linearAcc = new float[3];
//2 Manual options and an existing (API) option
int moved = 0;
final float alpha = (float) 0.8;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linearAcc[0] = event.values[0] - gravity[0];
linearAcc[1] = event.values[1] - gravity[1];
linearAcc[2] = event.values[2] - gravity[2];
// Log.d("ACCEL", "SENSOR EVENT");
// Log.d("LINEARACC1", String.valueOf(linearAcc[0]));
// Log.d("LINEARACC2", String.valueOf(linearAcc[1]));
// Log.d("LINEARACC3", String.valueOf(linearAcc[2]));
for(int i = 0; i < 3; i++){
if(linearAcc[i] > 5 || linearAcc[i] < -5){
moved++;
Log.d("ACCEL", "SENSOR EVENT");
for(int i = 0; i < history.length; i++) {
if (-0.2 > (history[i] - event.values[i])
|| (history[i] - event.values[i]) > 0.2) {
moved++;
}
}
if(moved >= 2) {
monitor();
}
for(int i = 0; i < history.length; i++){
history[i] = event.values[i];
}
if(moved >= 2) {
if(((event.timestamp - historyTime)/1000/1000/1000) > 60){
historyTime = event.timestamp;
minutes++;
}
} else if(minutes > prevMinutes){
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();
prevMinutes = minutes;
monitor();
}
}
@Override
......
......@@ -22,7 +22,7 @@ public class StepCounter extends Service implements SensorEventListener {
private Sensor stepCounter;
private float stepCount;
private float prevStepCount = 0;
private int minutes = 0;
private int minutes;
private SensorManager mSensorManager;
......@@ -31,6 +31,8 @@ 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, 60000000);
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
minutes = sharedPref.getInt(getString(R.string.daily_progress), 0);
return START_STICKY;
}
......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="daily_goal_minutes">75</integer>
<integer name="notify_period_minutes">15</integer>
<integer name="notify_period_minutes">60</integer>
<integer name="step_job">1</integer>
<integer name="alarm_rc">23</integer>
</resources>
\ 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