diff options
author | Martin Fietz <Martin.Fietz@gmail.com> | 2015-08-09 13:05:00 +0200 |
---|---|---|
committer | Martin Fietz <Martin.Fietz@gmail.com> | 2015-09-05 00:50:44 +0200 |
commit | 5b90ba91337beaff8b41489da2c8a7cc6c6039f4 (patch) | |
tree | a867d4dcc84ff5a5a6f018c59a6c0c9e133ad418 /app/src/main/java/de/danoeh/antennapod/dialog | |
parent | 98a6ab144bdf44bc9aa3c93c3ab04313180a9d53 (diff) | |
download | AntennaPod-5b90ba91337beaff8b41489da2c8a7cc6c6039f4.zip |
Sleep Timer dialog remembers settings
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/dialog')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java | 100 |
1 files changed, 57 insertions, 43 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java index 28364e4c4..a699706a8 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.dialog; import android.app.Dialog; import android.content.Context; +import android.content.SharedPreferences; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -22,11 +23,17 @@ import de.danoeh.antennapod.R; public abstract class SleepTimerDialog extends Dialog { - private static final String TAG = "TimeDialog"; + private static final String TAG = SleepTimerDialog.class.getSimpleName(); private static final int DEFAULT_SPINNER_POSITION = 1; private Context context; + private String PREF_NAME = "SleepTimerDialog"; + private String PREF_VALUE = "LastValue"; + private String PREF_TIME_UNIT = "LastTimeUnit"; + private String PREF_VIBRATE = "Vibrate"; + private String PREF_SHAKE_TO_RESET = "ShakeToReset"; + private SharedPreferences prefs; private EditText etxtTime; private Spinner spTimeUnit; @@ -40,15 +47,17 @@ public abstract class SleepTimerDialog extends Dialog { public SleepTimerDialog(Context context, int titleTextId, int leftButtonTextId) { super(context); this.context = context; + prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); - String[] spinnerContent = new String[]{context.getString(R.string.time_seconds), + String[] spinnerContent = new String[] { + context.getString(R.string.time_seconds), context.getString(R.string.time_minutes), - context.getString(R.string.time_hours)}; + context.getString(R.string.time_hours) }; setContentView(R.layout.time_dialog); etxtTime = (EditText) findViewById(R.id.etxtTime); @@ -58,60 +67,23 @@ public abstract class SleepTimerDialog extends Dialog { butConfirm = (Button) findViewById(R.id.butConfirm); butCancel = (Button) findViewById(R.id.butCancel); - butConfirm.setText(R.string.set_sleeptimer_label); - butCancel.setText(R.string.cancel_label); setTitle(R.string.set_sleeptimer_label); - ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>( - this.getContext(), android.R.layout.simple_spinner_item, - spinnerContent); - spinnerAdapter - .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - spTimeUnit.setAdapter(spinnerAdapter); - spTimeUnit.setSelection(DEFAULT_SPINNER_POSITION); - butCancel.setOnClickListener(new View.OnClickListener() { - - @Override - public void onClick(View v) { - dismiss(); - } - }); - butConfirm.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - try { - long input = readTimeMillis(); - onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked()); - dismiss(); - } catch (NumberFormatException e) { - e.printStackTrace(); - Toast toast = Toast.makeText(context, - R.string.time_dialog_invalid_input, - Toast.LENGTH_LONG); - toast.show(); - } - } - }); + etxtTime.setText(prefs.getString(PREF_VALUE, "15")); etxtTime.addTextChangedListener(new TextWatcher() { - @Override public void afterTextChanged(Editable s) { checkInputLength(s.length()); } @Override - public void beforeTextChanged(CharSequence s, int start, int count, - int after) { - + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override - public void onTextChanged(CharSequence s, int start, int before, - int count) { - + public void onTextChanged(CharSequence s, int start, int before, int count) { } }); - checkInputLength(etxtTime.getText().length()); etxtTime.postDelayed(new Runnable() { @Override public void run() { @@ -120,8 +92,41 @@ public abstract class SleepTimerDialog extends Dialog { } }, 100); + ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this.getContext(), + android.R.layout.simple_spinner_item, spinnerContent); + spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spTimeUnit.setAdapter(spinnerAdapter); + int selection = prefs.getInt(PREF_TIME_UNIT, DEFAULT_SPINNER_POSITION); + spTimeUnit.setSelection(selection); + + cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true)); + cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true)); + butConfirm.setText(R.string.set_sleeptimer_label); + butConfirm.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + try { + savePreferences(); + long input = readTimeMillis(); + onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked()); + dismiss(); + } catch (NumberFormatException e) { + e.printStackTrace(); + Toast toast = Toast.makeText(context, R.string.time_dialog_invalid_input, + Toast.LENGTH_LONG); + toast.show(); + } + } + }); + butCancel.setText(R.string.cancel_label); + butCancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); } private void checkInputLength(int length) { @@ -142,4 +147,13 @@ public abstract class SleepTimerDialog extends Dialog { return selectedUnit.toMillis(value); } + private void savePreferences() { + prefs.edit() + .putString(PREF_VALUE, etxtTime.getText().toString()) + .putInt(PREF_TIME_UNIT, spTimeUnit.getSelectedItemPosition()) + .putBoolean(PREF_SHAKE_TO_RESET, cbShakeToReset.isChecked()) + .putBoolean(PREF_VIBRATE, cbVibrate.isChecked()) + .apply(); + } + } |