Newer
Older
package com.notificationFramework.sedentary.frontEnd;

Peter Joseph De Jonckheere CESM2014
committed

Peter Joseph De Jonckheere CESM2014
committed
import android.app.ActivityManager;

Peter Joseph De Jonckheere CESM2014
committed
import android.content.Context;

Peter Joseph De Jonckheere CESM2014
committed
import android.content.Intent;

Peter Joseph De Jonckheere CESM2014
committed
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;

Peter Joseph De Jonckheere CESM2014
committed
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

Peter Joseph De Jonckheere CESM2014
committed
import android.view.View;
import android.widget.Button;

Peter Joseph De Jonckheere CESM2014
committed
import android.widget.ProgressBar;

Peter Joseph De Jonckheere CESM2014
committed
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
* <p>
* 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>

Peter Joseph De Jonckheere CESM2014
committed
public class MainActivity extends AppCompatActivity {
/**
* 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
*/

Peter Joseph De Jonckheere CESM2014
committed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Peter Joseph De Jonckheere CESM2014
committed
setUpButtons();
/**
* Used to ensure a stimulus object is created when the activity is restarted rather than
* created for the first time.
*/
protected void onStart() {
super.onStart();

Peter Joseph De Jonckheere CESM2014
committed
}

Peter Joseph De Jonckheere CESM2014
committed
/**
* 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);

Peter Joseph De Jonckheere CESM2014
committed
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);

Peter Joseph De Jonckheere CESM2014
committed
progress.setMax((shared.getInt(getString(R.string.daily_goal_set), R.integer.daily_goal_minutes)));

Peter Joseph De Jonckheere CESM2014
committed
progress.setProgress(shared.getInt(getString(R.string.daily_progress), 0));

Peter Joseph De Jonckheere CESM2014
committed
}
/**
* 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;
}