Skip to content
Snippets Groups Projects
SigMotionDetect.java 3.43 KiB
package com.notificationFramework.stimulusStrategy;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.notificationFramework.sedentary.frontEnd.R;

/**
 * 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 int onStartCommand(Intent intent, int flags, int startId){
        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();
            }
        };
        mSensorManager.requestTriggerSensor(tel, md);
        return START_STICKY;
    }

    private void setUpClock() {
        Intent i = new Intent(this, com.notificationFramework.stimulus.SedentaryStimulus.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, R.integer.alarm_rc, i, 0);
        SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
         int interval = shared.getInt(getString(R.string.notify_me), R.integer.notify_period_minutes);
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000*60*interval), pi);
       // 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();
        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();
        setUpClock();
        mSensorManager.requestTriggerSensor(tel, md);
    }

    private void stepCounter(){
        Intent intent = new Intent(this, StepCounter.class);
        startService(intent);
    }



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