Newer
Older

Peter Joseph De Jonckheere CESM2014
committed
package com.example.stimulusStrategy;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

Peter Joseph De Jonckheere CESM2014
committed
import android.widget.Toast;

Peter Joseph De Jonckheere CESM2014
committed
import com.example.notification.Notification;

Peter Joseph De Jonckheere CESM2014
committed
import com.example.pharmacy.frontEnd.R;

Peter Joseph De Jonckheere CESM2014
committed
/**
* Created by pharmacy on 17/01/2018.
*/
public class SigMotionDetect extends Service implements StimulusStrategy {
private SensorManager mSensorManager;
private Sensor md;
private TriggerEventListener tel;
private AlarmManager am;
//Set up alarm manager to fire in one hour, and set up significant motion sensor to fire and call monitor which
// will then cancel the alarm and reschedule it for one hour from now. Monitor should also use step-counter? to monitor the length
// of unsedentary behaviour for progress monitoring.
@Override
public void onCreate(){
am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
setUpClock();
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);

Peter Joseph De Jonckheere CESM2014
committed
//Need to test if this produces null to avoid NULLPOINTEREXCEPTION
md = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);

Peter Joseph De Jonckheere CESM2014
committed
tel = new TriggerEventListener() {
@Override
public void onTrigger(TriggerEvent triggerEvent) {
monitor();
}
};
}
private void setUpClock() {
Intent i = new Intent(this, Notification.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
am.set(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_HOUR, pi);
// Log.i("myTag", "Alarm SET");

Peter Joseph De Jonckheere CESM2014
committed
}
public void monitor() {
//Use step counter sensor? to detect if movement lasted for a prolonged period of time.

Peter Joseph De Jonckheere CESM2014
committed
Log.d("MOTION", "HUZZAH");

Peter Joseph De Jonckheere CESM2014
committed
am.cancel(am.getNextAlarmClock().getShowIntent());
setUpClock();
mSensorManager.requestTriggerSensor(tel, md);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}