package com.notificationFramework.sedentary.frontEnd; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; 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.ImageButton; import android.widget.Spinner; import android.widget.TimePicker; import android.widget.Toast; import java.io.File; /** * Created by Peter De Jonckheere on 16/02/2018. * <p> * The activity which is used to set the advanced, in depth, settings for the application. This * activity contains more involved settings than the standard settings page and can be used by * users to obtain further personalisation in certain features. * </p> */ public class AdvancedSettings extends AppCompatActivity { /** * Called when the AdvancedSettings activity is started from somewhere else in the application. * Sets up the layout using that which is defined in advanced_settings.xml and the delegates to * further methods to set up the buttons and settings 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.advanced_settings); setUpButtons(); retrieveSettings(); } /** * Called when the activity is stopped by the user or the OS to save the settings of this page. * * @see android.support.v7.app.AppCompatActivity */ @Override protected void onStop() { super.onStop(); saveSharedPreferenceData(); } /** * Gets the data stored in Shared Preferences which is required to initialise the page to a * previous state. */ private void retrieveSettings() { SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE); ((Spinner) findViewById(R.id.PeriodSpinner)) .setSelection(shared.getInt(getString(R.string.led_period_pos), 0)); ((Spinner) findViewById(R.id.ColourSpinner)) .setSelection(shared.getInt(getString(R.string.led_colour_pos), 0)); ((Spinner) findViewById(R.id.AccelSpinner)) .setSelection(shared.getInt(getString(R.string.accel_sensitivity_pos), 0)); } /** * Used to set up the background processes for ech of the buttons and spinners on this page. */ private void setUpButtons() { //Adds the onClickListener for the button which navigates back to the standard settings page 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(), Settings.class); displaySavedToast(view); startActivity(intent); } }); //Defines the code which allows the data collcted to be uploaded via an intent which calls //on another application to share the file. the file is a zip file created here containing //the three data files stored throughout the application's use Button upload = findViewById(R.id.upload); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File file = SaveFile.zipFiles(view.getContext()); Intent sendIntent = new Intent(Intent.ACTION_SEND); if (file.exists()) { //The extras added to the intent to obtain the best possible options for sharing //the zip file. sendIntent.setType("notificationapp/zip"); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath())); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Sharing File..."); sendIntent.putExtra(Intent.EXTRA_TEXT, "Sharing File..."); startActivity(Intent.createChooser(sendIntent, "Share File")); } } }); //Sets up the button which allows the user to take the post-user trial survey. As above this //is done through an intent designed to open a web browser with the provided URL Button survey = findViewById(R.id.survey); survey.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent webIntent = new Intent(Intent.ACTION_VIEW); webIntent.setData( Uri.parse(getString(R.string.survey_link))); startActivity(webIntent); } }); //Sets the code for the button for the start of the do not disturb period which creates a //time picker pop-up then stores the time chosen by the user in Shared Preferences Button startTime = findViewById(R.id.startTime); startTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final SharedPreferences shared = view.getContext(). getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE); int hour = shared.getInt(getString(R.string.dnd_shour), 23); int minute = shared.getInt(getString(R.string.dnd_smin), 0); TimePickerDialog dialog = new TimePickerDialog(view.getContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selHour, int selMin) { SharedPreferences.Editor editor = shared.edit(); editor.putInt(getString(R.string.dnd_shour), selHour); editor.putInt(getString(R.string.dnd_smin), selMin); editor.commit(); } }, hour, minute, true); dialog.setTitle(getString(R.string.dnd_stitle)); dialog.show(); } }); //As above but for the end time Button endTime = findViewById(R.id.endTime); endTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final SharedPreferences shared = view.getContext(). getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE); int hour = shared.getInt(getString(R.string.dnd_ehour), 9); int minute = shared.getInt(getString(R.string.dnd_emin), 0); TimePickerDialog dialog = new TimePickerDialog(view.getContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selHour, int selMin) { SharedPreferences.Editor editor = shared.edit(); editor.putInt(getString(R.string.dnd_ehour), selHour); editor.putInt(getString(R.string.dnd_emin), selMin); editor.commit(); } }, hour, minute, true); dialog.setTitle(getString(R.string.dnd_etitle)); dialog.show(); } }); //Calls the populate spinner method for each spinner which adds the relevant data to each //spinner populateSpinner((Spinner) findViewById(R.id.PeriodSpinner), R.array.LED_periods); populateSpinner((Spinner) findViewById(R.id.ColourSpinner), R.array.LED_colours); populateSpinner((Spinner) findViewById(R.id.AccelSpinner), R.array.accel_sensitivities); } /** * 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() { SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE); int spinnerPPos = ((Spinner) findViewById(R.id.PeriodSpinner)).getSelectedItemPosition(); int notifyGap = Integer.parseInt( ((Spinner) findViewById(R.id.PeriodSpinner)).getSelectedItem().toString()); int spinnerCPos = ((Spinner) findViewById(R.id.ColourSpinner)).getSelectedItemPosition(); String colour = ((Spinner) findViewById(R.id.ColourSpinner)).getSelectedItem().toString(); int spinnerAPos = ((Spinner) findViewById(R.id.AccelSpinner)).getSelectedItemPosition(); String sensitivity = ((Spinner) findViewById(R.id.AccelSpinner)).getSelectedItem().toString(); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.led_period_pos), spinnerPPos); editor.putInt(getString(R.string.led_period), notifyGap); editor.putInt(getString(R.string.led_colour_pos), spinnerCPos); editor.putString(getString(R.string.led_colour), colour); editor.putInt(getString(R.string.accel_sensitivity_pos), spinnerAPos); editor.putString(getString(R.string.accel_sensitivity), sensitivity); editor.commit(); } }