Newer
Older
package com.notificationFramework.sedentary.frontEnd;

Peter Joseph De Jonckheere CESM2014
committed

Peter Joseph De Jonckheere CESM2014
committed

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;

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

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

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

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

Peter Joseph De Jonckheere CESM2014
committed
import android.widget.SeekBar;
import android.widget.Spinner;

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

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

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

Peter Joseph De Jonckheere CESM2014
committed
/**
* Created by Peter De Jonckheere on 21/11/2017.
* <p>
* Contains the buttons and menus which set a variety of user defined data which is used throughout
* the application. Settings are saved either when the activity is stopped or when navigation is
* made back to the main activity page.

Peter Joseph De Jonckheere CESM2014
committed
*/
public class Settings extends AppCompatActivity {

Peter Joseph De Jonckheere CESM2014
committed
/**
* Called when the Settings activity is navigated to from the main activity.Sets up the layout
* using that which is defined in settings.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);

Peter Joseph De Jonckheere CESM2014
committed
setContentView(R.layout.settings);

Peter Joseph De Jonckheere CESM2014
committed
setUpButtons();
retrieveSettings();
}
/**
* Saves the settings of this page to the Shared Preferences if the activity is stopped.
*
* @see android.support.v7.app.AppCompatActivity
*/
super.onStop();
saveSharedPreferenceData();
}
/**
* Obtains settings which have been previously stored in Shared Preferences and initialise the
* page with these values.
*/
SharedPreferences shared =
this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
((CheckBox) findViewById(R.id.Audio)).setChecked(shared.getBoolean(getString(R.string.audio), false));
((CheckBox) findViewById(R.id.Vibration)).
setChecked(shared.getBoolean(getString(R.string.vibration), false));
((CheckBox) findViewById(R.id.LEDFlash)).
setChecked(shared.getBoolean(getString(R.string.led), false));
((Spinner) findViewById(R.id.PeriodSpinner)).
setSelection(shared.getInt(getString(R.string.notify_me), 0));
((EditText) findViewById(R.id.notificationMessage)).
setText(shared.getString(getString(R.string.daily_goal_text),
getString(R.string.notf_text)));
((SeekBar) findViewById(R.id.seekBar2)).
setProgress(shared.getInt(getString(R.string.daily_goal_set), getResources().getInteger(R.integer.daily_goal_minutes)));
((Switch) findViewById(R.id.NotSwitch)).
setChecked(shared.getBoolean(getString(R.string.notf_switch), true));

Peter Joseph De Jonckheere CESM2014
committed
}
/**
* Sets up the buttons and spinners of the activity with the background code which makes them
* operate as expected.
*/
//Sets the onClickListener for the back button to navigate to the main activity
ImageButton back = findViewById(R.id.back);

Peter Joseph De Jonckheere CESM2014
committed
back.bringToFront();

Peter Joseph De Jonckheere CESM2014
committed
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveSharedPreferenceData();

Peter Joseph De Jonckheere CESM2014
committed
Intent intent = new Intent(view.getContext(), MainActivity.class);
displaySavedToast(view);
startActivity(intent);
}
});
//Sets the onClickListener for the button which navigates forwards to the advanced settings
//activity
Button adv = findViewById(R.id.button);
adv.setOnClickListener(new View.OnClickListener() {
Intent intent = new Intent(view.getContext(), AdvancedSettings.class);
startActivity(intent);
}
});

Peter Joseph De Jonckheere CESM2014
committed
setUpSeekBar();
populateSpinner((Spinner) findViewById(R.id.PeriodSpinner), R.array.notification_periods);

Peter Joseph De Jonckheere CESM2014
committed
}

Peter Joseph De Jonckheere CESM2014
committed
/**
* Sets the seek bar so that it can scrolled to set the daily goal in minutes. A text box is
* also set to display above the bar to show the value which the seek bar is currently at.
*
* @see android.widget.SeekBar
*/
SeekBar bar = findViewById(R.id.seekBar2);

Peter Joseph De Jonckheere CESM2014
committed
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

Peter Joseph De Jonckheere CESM2014
committed
{
progressText = findViewById(R.id.seekBarValue);

Peter Joseph De Jonckheere CESM2014
committed
}
@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
int val = (progress * (bar.getWidth() - 2 * bar.getThumbOffset())) / bar.getMax();
progressText.setText(String.valueOf(progress));

Peter Joseph De Jonckheere CESM2014
committed
progressText.setX(bar.getX() + val + bar.getThumbOffset() / 2);
}

Peter Joseph De Jonckheere CESM2014
committed
@Override
public void onStartTrackingTouch(SeekBar bar) {

Peter Joseph De Jonckheere CESM2014
committed
progressText.setVisibility(View.VISIBLE);

Peter Joseph De Jonckheere CESM2014
committed
@Override
public void onStopTrackingTouch(SeekBar bar) {
progressText.setVisibility(View.INVISIBLE);
}

Peter Joseph De Jonckheere CESM2014
committed
});
}
/**
* Using the params provided fills the spinner with the desired data specififed by the arrayID.
*
* @param spinner the spinner which is to be populated with data
* @param arrayID the resource ID of the array which contains the data to be added to the
* spinner
*/
private void populateSpinner(Spinner spinner, int arrayID) {
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, arrayID,
android.R.layout.simple_spinner_dropdown_item);

Peter Joseph De Jonckheere CESM2014
committed
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Peter Joseph De Jonckheere CESM2014
committed
}
/**
* Displays a Toast object which informs the user that the information entered on this page has
* been stored.
*
* @param view the view object with which the Toast object is displayed within
*/
private void displaySavedToast(View view) {
Toast saved = Toast.makeText(view.getContext(), R.string.settings_saved, Toast.LENGTH_SHORT);

Peter Joseph De Jonckheere CESM2014
committed
saved.show();
}
/**
* Stores the data of this page in the Shared Preferences which allows it to be used elsewhere
* in the program and also allows this page to be reconstructed in the correct form when it is
* returned to.
*/
private void saveSharedPreferenceData() {
boolean audio = ((CheckBox) findViewById(R.id.Audio)).isChecked();
boolean vibration = ((CheckBox) findViewById(R.id.Vibration)).isChecked();
boolean led = ((CheckBox) findViewById(R.id.LEDFlash)).isChecked();
int spinnerPos = ((Spinner) findViewById(R.id.PeriodSpinner)).getSelectedItemPosition();
((Spinner) findViewById(R.id.PeriodSpinner)).getSelectedItem().toString());
String message = ((EditText) findViewById(R.id.notificationMessage)).getText().toString();

Peter Joseph De Jonckheere CESM2014
committed
int progress = ((SeekBar) findViewById(R.id.seekBar2)).getProgress();
boolean nswitch = ((Switch) findViewById(R.id.NotSwitch)).isChecked();
SharedPreferences sharedPref =
this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.audio), audio);
editor.putBoolean(getString(R.string.vibration), vibration);
editor.putBoolean(getString(R.string.led), led);
editor.putInt(getString(R.string.notify_me), spinnerPos);
editor.putInt(getString(R.string.daily_goal), notifyGap);
if (!message.matches(getString(R.string.notf_text))) {
editor.putString(getString(R.string.daily_goal_text), message);
}

Peter Joseph De Jonckheere CESM2014
committed
editor.putInt(getString(R.string.daily_goal_set), progress);
editor.putBoolean(getString(R.string.notf_switch), nswitch);
editor.commit();
}