Sleep Timer dialog remembers settings
This commit is contained in:
parent
98a6ab144b
commit
5b90ba9133
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,14 +51,12 @@
|
||||
<CheckBox android:id="@+id/cbShakeToReset"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/shake_to_reset_label"
|
||||
android:checked="true" />
|
||||
android:text="@string/shake_to_reset_label"/>
|
||||
|
||||
<CheckBox android:id="@+id/cbVibrate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/timer_vibration_label"
|
||||
android:checked="true" />
|
||||
android:text="@string/timer_vibration_label"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@ -52,14 +52,12 @@
|
||||
<CheckBox android:id="@+id/cbShakeToReset"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/shake_to_reset_label"
|
||||
android:checked="true" />
|
||||
android:text="@string/shake_to_reset_label"/>
|
||||
|
||||
<CheckBox android:id="@+id/cbVibrate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/timer_vibration_label"
|
||||
android:checked="true" />
|
||||
android:text="@string/timer_vibration_label"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user