Extract sleep timer preferences into dedicated class

This commit is contained in:
Martin Fietz 2016-10-09 21:49:23 +02:00
parent 8accc12048
commit e75d60ef61
7 changed files with 120 additions and 50 deletions

View File

@ -19,19 +19,14 @@ import com.afollestad.materialdialogs.MaterialDialog;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
public abstract class SleepTimerDialog {
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 MaterialDialog dialog;
@ -40,12 +35,10 @@ public abstract class SleepTimerDialog {
private CheckBox cbShakeToReset;
private CheckBox cbVibrate;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public SleepTimerDialog(Context context) {
this.context = context;
prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public MaterialDialog createNewDialog() {
@ -75,8 +68,9 @@ public abstract class SleepTimerDialog {
spTimeUnit = (Spinner) view.findViewById(R.id.spTimeUnit);
cbShakeToReset = (CheckBox) view.findViewById(R.id.cbShakeToReset);
cbVibrate = (CheckBox) view.findViewById(R.id.cbVibrate);
chAutoEnable = (CheckBox) view.findViewById(R.id.chAutoEnable);
etxtTime.setText(prefs.getString(PREF_VALUE, "15"));
etxtTime.setText(SleepTimerPreferences.lastTimerValue());
etxtTime.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
@ -104,11 +98,10 @@ public abstract class SleepTimerDialog {
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);
spTimeUnit.setSelection(SleepTimerPreferences.lastTimerTimeUnit());
cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true));
cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true));
cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset());
cbVibrate.setChecked(SleepTimerPreferences.vibrate());
return dialog;
}
@ -132,12 +125,10 @@ public abstract class SleepTimerDialog {
}
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();
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
spTimeUnit.getSelectedItemPosition());
SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
}
}

View File

@ -41,15 +41,17 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="@string/timer_about_to_expire_label"/>
android:text="@string/timer_about_to_expire_label"
android:textSize="16sp" />
<CheckBox android:id="@+id/cbShakeToReset"
<CheckBox
android:id="@+id/cbShakeToReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shake_to_reset_label" />
<CheckBox android:id="@+id/cbVibrate"
<CheckBox
android:id="@+id/cbVibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" />

View File

@ -40,15 +40,17 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:text="@string/timer_about_to_expire_label"/>
android:text="@string/timer_about_to_expire_label"
android:textSize="16sp" />
<CheckBox android:id="@+id/cbShakeToReset"
<CheckBox
android:id="@+id/cbShakeToReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shake_to_reset_label" />
<CheckBox android:id="@+id/cbVibrate"
<CheckBox
android:id="@+id/cbVibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" />

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.core;
import android.content.Context;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -43,7 +44,7 @@ public class ClientConfig {
UpdateManager.init(context);
PlaybackPreferences.init(context);
NetworkUtils.init(context);
// CastManager.init(context);
SleepTimerPreferences.init(context);
initialized = true;
}

View File

@ -0,0 +1,71 @@
package de.danoeh.antennapod.core.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;
public class SleepTimerPreferences {
private static final String TAG = "SleepTimerPreferences";
private static final String PREF_NAME = "SleepTimerDialog";
private static final String PREF_VALUE = "LastValue";
private static final String PREF_TIME_UNIT = "LastTimeUnit";
private static final String PREF_VIBRATE = "Vibrate";
private static final String PREF_SHAKE_TO_RESET = "ShakeToReset";
private static final String PREF_AUTO_ENABLE = "AutoEnable";
private static final String DEFAULT_VALUE = "15";
private static final int DEFAULT_TIME_UNIT = 1;
private static Context context;
private static SharedPreferences prefs;
/**
* Sets up the UserPreferences class.
*
* @throws IllegalArgumentException if context is null
*/
public static void init(@NonNull Context context) {
Log.d(TAG, "Creating new instance of SleepTimerPreferences");
SleepTimerPreferences.prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static void setLastTimer(String value, int timeUnit) {
prefs.edit().putString(PREF_VALUE, value).putInt(PREF_TIME_UNIT, timeUnit).apply();
}
public static String lastTimerValue() {
return prefs.getString(PREF_VALUE, DEFAULT_VALUE);
}
public static int lastTimerTimeUnit() {
return prefs.getInt(PREF_TIME_UNIT, DEFAULT_TIME_UNIT);
}
public static void setVibrate(boolean vibrate) {
prefs.edit().putBoolean(PREF_VIBRATE, vibrate).apply();
}
public static boolean vibrate() {
return prefs.getBoolean(PREF_VIBRATE, true);
}
public static void setShakeToReset(boolean shakeToReset) {
prefs.edit().putBoolean(PREF_SHAKE_TO_RESET, shakeToReset).apply();
}
public static boolean shakeToReset() {
return prefs.getBoolean(PREF_SHAKE_TO_RESET, true);
}
public static void setAutoEnable(boolean autoEnable) {
prefs.edit().putBoolean(PREF_AUTO_ENABLE, autoEnable).apply();
}
public static boolean autoEnable() {
return prefs.getBoolean(PREF_AUTO_ENABLE, false);
}
}

View File

@ -482,6 +482,7 @@
<item quantity="one">1 hour</item>
<item quantity="other">%d hours</item>
</plurals>
<string name="auto_enable">Auto-enable</string>
<!-- gpodder.net -->
<string name="gpodnet_taglist_header">CATEGORIES</string>

View File

@ -4,6 +4,7 @@ import android.content.Context;
import de.danoeh.antennapod.core.cast.CastManager;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -45,6 +46,7 @@ public class ClientConfig {
PlaybackPreferences.init(context);
NetworkUtils.init(context);
CastManager.init(context);
SleepTimerPreferences.init(context);
initialized = true;
}