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

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.example.pharmacy.frontEnd.R;

import java.util.Calendar;

/**
 * Created by pharmacy on 13/02/2018.
 */
public class StepCounter extends Service implements SensorEventListener {

    private Sensor stepCounter;
    private float stepCount;
    private float prevStepCount = 0;
    private SensorManager mSensorManager;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        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);
        setUpDailyProgress();
    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();
        }

    }

    @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();
                stopSelf();
            }
        }
    }

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

    }

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