Skip to content
Snippets Groups Projects
SigMotionDetect.java 4.76 KiB
Newer Older
package com.example.stimulusStrategy;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.app.job.JobWorkItem;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.List;

/**
 * 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);
        //Need to test if this produces null to avoid NULLPOINTEREXCEPTION
            md = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
        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");
    }

    public void monitor() {
        //Use step counter sensor? to detect if movement lasted for a prolonged period of time.
        //Anything over 60 steps per minute counts as a minute of activity.
        //Check every minute until the change is less than this.
        //Then register to daily progress for number of minutes > 60.
        Toast saved = Toast.makeText(this, R.string.motion,Toast.LENGTH_LONG);
        saved.show();
        stepCounter();
    private void stepCounter(){
        Intent intent = new Intent(this, StepCounter.class);
        startActivity(intent);
    }

    private class StepCounter extends Service implements SensorEventListener{

        private Sensor stepCounter;
        private float stepCount;
        private float prevStepCount = 0;
        private int minutes = 0;


        public void onCreate(){
            stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
            mSensorManager.registerListener(this, stepCounter, 60000000);

        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (prevStepCount == 0) {
                prevStepCount = event.values[0];
            } else {
                stepCount = event.values[0];
                if(stepCount - 60 > prevStepCount){
                    minutes++;
                    prevStepCount = stepCount;
                    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();
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {

        }
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }