Skip to content
Snippets Groups Projects
MainActivity.java 4.91 KiB
Newer Older
package com.notificationFramework.sedentary.frontEnd;
import android.app.ActivityManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import com.notificationFramework.stimulus.SedentaryStimulus;
import com.notificationFramework.stimulus.Stimulus;
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 in
 * the progress bar. There is currently (26/3/18) also a placeholder for a logo in the centre of
 * the page.
 * </p>

    /**
     * Called when the MainActivity activity is started from somewhere else in the application, or
     * the manifest file on startup. Sets up the layout using that which is defined in
     * activity_main.xml and the delegates to further methods to set up the buttons for the page.
     *
     * @param savedInstanceState contains information pertaining to previous states of the activity
     *                           if it has been used before
     * @see android.support.v7.app.AppCompatActivity
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    /**
     * Used to ensure a stimulus object is created when the activity is restarted rather than
     * created for the first time.
     */
    /**
     * Sets up the buttons for this page and also adds the current minute value to the progress bar.
     */
    private void setUpButtons() {
        Button settings = 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 = 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));
    /**
     * Creates the stimulus object which initiates the background processes of the application. Uses
     * the strategy here to begin monitoring sedentary behaviour. This strategy is currently
     * selected based on the hardware available on the device.
     */
    private void setUpStimulus() {
        if (!(isServiceRunning(com.notificationFramework.stimulusStrategy.Accelerometer.class))
                && !(isServiceRunning(
                com.notificationFramework.stimulusStrategy.SigMotionDetect.class))) {
            SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            try {
                if (sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null
                        && sm.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION) != null) {
                    Stimulus stimulus = new SedentaryStimulus(this, new SigMotionDetect());
                } else {
                    Stimulus stimulus = new SedentaryStimulus(this, new Accelerometer());
                }
            } catch (NullPointerException e) {
                Stimulus stimulus = new SedentaryStimulus(this, new Accelerometer());
            }
    /**
     * Checks the currently running services to establish if the chosen straegy is already running
     * on the deivce as a service.
     *
     * @param serviceClass the name of the class to be checked as a running service
     * @return true if 'servoiceClass' is running, otheriwse false
     */
    private boolean isServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        try {
            for (ActivityManager.RunningServiceInfo service :
                    manager.getRunningServices(Integer.MAX_VALUE)) {
                if (serviceClass.getName().equals(service.service.getClassName())) {
                    return true;
                }

            return false;
        } catch (NullPointerException e) {
            return false;