Skip to content
Snippets Groups Projects
Accelerometer.java 5.83 KiB
Newer Older
package com.notificationFramework.stimulusStrategy;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v4.content.LocalBroadcastManager;
import com.notificationFramework.sedentary.frontEnd.R;
import com.notificationFramework.sedentary.frontEnd.SaveFile;
 * Created by Peter De Jonckheere on 09/01/2018.
public class Accelerometer extends Service implements StimulusStrategy, SensorEventListener {
    private SharedPreferences preferences;
    private float[] history = new float[3];
    private long historyTime;
    private int prevMinutes = 0;
    private int minutes;
    private double sensitivity = 0.8;
    private enum sensitivityLevel{
        ZERO, VERY_HIGH, HIGH, NORMAL, LOW, VERY_LOW;
    public int onStartCommand(Intent intent, int flags, int startId) {
        historyTime = SystemClock.elapsedRealtimeNanos();
        preferences = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        sensitivity = sensitivity * sensitivityLevel.valueOf(preferences.getString(getString(R.string.accel_sensitivity), "NORMAL")).ordinal();
        mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 600000000);
        am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        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);
            if(minutes >= sharedPref.getInt(getString(R.string.daily_goal_set), getResources().getInteger(R.integer.daily_goal_minutes))
                    && !(sharedPref.getBoolean((getString(R.string.goal_met)), false))){
                goalNotify();
                sharedPref.edit().putBoolean(getString(R.string.goal_met), true).commit();
            }
        } 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.putBoolean(getString(R.string.goal_met), false);
            editor.commit();
        }
        prevMinutes = minutes;
    }

        SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        Intent i = new Intent(getBaseContext(), com.notificationFramework.stimulus.SedentaryStimulus.class);
        PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
        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);
        Intent i = new Intent(getBaseContext(), com.notificationFramework.stimulus.SedentaryStimulus.class);
        PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
        am.cancel(pi);
        SaveFile.recordNotification(0,0,1, this);
    public void goalNotify(){
        Intent i = new Intent(getBaseContext(), com.notificationFramework.stimulus.GoalStimulus.class);
        SaveFile.recordNotification(1,1,0, this);
        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
        lbm.sendBroadcast(i);
    }
            if ((-1*sensitivity) > (history[i] - event.values[i])
                    || (history[i] - event.values[i]) > sensitivity) {
                if(!((history[i] - event.values[i]) > 10
                && !(-10 > (history[i]) - event.values[i]))){
            for(int i = 0; i < history.length; i++){
            history[i] = event.values[i];
            }

                if(((event.timestamp - historyTime)/1000000000) > 60){
                    historyTime = event.timestamp;
                    minutes++;
                }
            } else if(minutes > prevMinutes){
                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();
                prevMinutes = minutes;
                monitor();
            }
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

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