Skip to content
Snippets Groups Projects
SigMotionDetect.java 4.90 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.util.Log;
import android.widget.Toast;

import com.notificationFramework.sedentary.frontEnd.R;
import com.notificationFramework.sedentary.frontEnd.SaveFile;

import java.util.Calendar;

/**
 * Created by Peter De Jonckheere on 17/01/2018.
 */

public class SigMotionDetect extends Service implements StimulusStrategy {

    private SensorManager mSensorManager;
    private Sensor md;
    private TriggerEventListener tel;
    private AlarmManager am;
    private float prevStepCount;
    private long prevTimeStamp;
    private int minutes;
    private int prevMinutes = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        setUpClock();
        setUpDailyProgress();
        prevMinutes = minutes;
        mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
            md = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
        tel = new TriggerEventListener() {
            @Override
            public void onTrigger(TriggerEvent triggerEvent) {
                monitor();
            }
        };
        stepCounter();
        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.daily_goal), getResources().getInteger(R.integer.notify_period_minutes));
       // am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000*60*interval), pi);
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pi);
    }

    public void monitor() {
        stepCalc();
        if(minutes > prevMinutes) {
            Intent i = new Intent(this, com.notificationFramework.stimulus.SedentaryStimulus.class);
            PendingIntent pi = PendingIntent.getBroadcast(this, R.integer.alarm_rc, i, 0);
            am.cancel(pi);
            SaveFile.recordNotification(0,0,1, this);
            setUpClock();
            storeData();
            prevStepCount = StepCounter.stepCount;
            prevTimeStamp = StepCounter.timestamp;
        prevMinutes = minutes;
        }
        mSensorManager.requestTriggerSensor(tel, md);
    }

    private void stepCalc() {
        float stepDifference = 0;
        long timeDifference = 0;
        if(prevStepCount != 0) {
            stepDifference = StepCounter.stepCount - prevStepCount;
        } else {
            prevStepCount = StepCounter.stepCount;
        }
        if(prevTimeStamp != 0) {
            timeDifference = StepCounter.timestamp - prevTimeStamp;
        } else{
            prevTimeStamp = StepCounter.timestamp;
        }
        float averageTime = ((timeDifference / 3600) + (stepDifference / 60)) / 2;
        if(averageTime > 1) {
            minutes = minutes + Math.round(averageTime);
        }
    }
    private void stepCounter(){
        Intent intent = new Intent(this, StepCounter.class);
        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
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}