Skip to content
Snippets Groups Projects
MainActivity.java 3.19 KiB
package com.notificationFramework.sedentary.frontEnd;


import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.notificationFramework.stimulus.Stimulus;
import com.notificationFramework.stimulus.SedentaryStimulus;
import com.notificationFramework.stimulusStrategy.Accelerometer;
import com.notificationFramework.stimulusStrategy.SigMotionDetect;

/**
 * Created by Peter De Jonckheere 21/11/2017
 *
 * Main activity is the front page of the application and also creates the stimulus with the chosen
 * strategy. It allows navigation to settings and displays the user's daily progress in minutes and
 * also has text boxes available to display a daily goal.
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpButtons();
    }

    @Override
    protected void onStart(){
        super.onStart();
        setUpStimulus();
    }

    @Override
    protected void onStop(){
    super.onStop();

    }

    private void setUpButtons(){
        Button settings = (Button)findViewById(R.id.settings);
        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), Settings.class);
                startActivity(intent);
            }
        });
        SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setMax((shared.getInt(getString(R.string.daily_goal_set), R.integer.daily_goal_minutes)));
        progress.setProgress(shared.getInt(getString(R.string.daily_progress), 0));
    }

    private void setUpStimulus(){
        if(!(isServiceRunning(com.notificationFramework.stimulusStrategy.Accelerometer.class))
                && !(isServiceRunning(com.notificationFramework.stimulusStrategy.SigMotionDetect.class))) {
           SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
           if (sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null){
               Stimulus stimulus = new SedentaryStimulus(this, new SigMotionDetect());
           } else {
               Stimulus stimulus = new SedentaryStimulus(this, new Accelerometer());
           }
        }
    }

    private boolean isServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

}