package com.notificationFramework.sedentary.frontEnd; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.SeekBar; import android.widget.Spinner; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; /** * 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. * </p> */ public class Settings extends AppCompatActivity { /** * 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 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.settings); setUpButtons(); retrieveSettings(); } /** * Saves the settings of this page to the Shared Preferences if the activity is stopped. * * @see android.support.v7.app.AppCompatActivity */ @Override protected void onStop() { super.onStop(); saveSharedPreferenceData(); } /** * Obtains settings which have been previously stored in Shared Preferences and initialise the * page with these values. */ private void retrieveSettings() { 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)); } /** * Sets up the buttons and spinners of the activity with the background code which makes them * operate as expected. */ private void setUpButtons() { //Sets the onClickListener for the back button to navigate to the main activity ImageButton back = findViewById(R.id.back); back.bringToFront(); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { saveSharedPreferenceData(); 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() { @Override public void onClick(View view) { Intent intent = new Intent(view.getContext(), AdvancedSettings.class); startActivity(intent); } }); setUpSeekBar(); populateSpinner((Spinner) findViewById(R.id.PeriodSpinner), R.array.notification_periods); } /** * 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 */ private void setUpSeekBar() { SeekBar bar = findViewById(R.id.seekBar2); bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { private TextView progressText; { progressText = findViewById(R.id.seekBarValue); } @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)); progressText.setX(bar.getX() + val + bar.getThumbOffset() / 2); } @Override public void onStartTrackingTouch(SeekBar bar) { progressText.setVisibility(View.VISIBLE); } @Override public void onStopTrackingTouch(SeekBar bar) { progressText.setVisibility(View.INVISIBLE); } }); } /** * 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); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } /** * 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); 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(); int notifyGap = Integer.parseInt( ((Spinner) findViewById(R.id.PeriodSpinner)).getSelectedItem().toString()); String message = ((EditText) findViewById(R.id.notificationMessage)).getText().toString(); 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); } editor.putInt(getString(R.string.daily_goal_set), progress); editor.putBoolean(getString(R.string.notf_switch), nswitch); editor.commit(); } }