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.EditText; import android.widget.ImageButton; import android.widget.CheckBox; 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. * * Contains the buttons and menus which set a variety of user defined data which is used througout * the application. Settings are saved either when the activity is stopped or when navigation is * made back to the main activity page. * */ public class Settings extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.settings); setUpButtons(); retrieveSettings(); } @Override protected void onStop(){ super.onStop(); saveSharedPreferenceData(); } 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), R.integer.daily_goal_minutes)); ((Switch)findViewById(R.id.NotSwitch)).setChecked(shared.getBoolean(getString(R.string.notf_switch), true)); } private void setUpButtons(){ ImageButton back = (ImageButton)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); } }); 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(); } private void setUpSeekBar(){ SeekBar bar = ((SeekBar)findViewById(R.id.seekBar2)); bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { private TextView progressText; { progressText = ((TextView) 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); } }); } private void populateSpinner(){ Spinner spinner = findViewById(R.id.PeriodSpinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.notification_periods, android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } private void displaySavedToast(View view){ Toast saved = Toast.makeText(view.getContext(), R.string.settings_saved,Toast.LENGTH_SHORT); saved.show(); } 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(); } }