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 java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
public abstract class SleepTimerDialog { public abstract class SleepTimerDialog {
private static final String TAG = SleepTimerDialog.class.getSimpleName(); private static final String TAG = SleepTimerDialog.class.getSimpleName();
private static final int DEFAULT_SPINNER_POSITION = 1;
private Context context; 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 SharedPreferences prefs;
private MaterialDialog dialog; private MaterialDialog dialog;
@ -40,12 +35,10 @@ public abstract class SleepTimerDialog {
private CheckBox cbShakeToReset; private CheckBox cbShakeToReset;
private CheckBox cbVibrate; private CheckBox cbVibrate;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS }; private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public SleepTimerDialog(Context context) { public SleepTimerDialog(Context context) {
this.context = context; this.context = context;
prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
} }
public MaterialDialog createNewDialog() { public MaterialDialog createNewDialog() {
@ -75,8 +68,9 @@ public abstract class SleepTimerDialog {
spTimeUnit = (Spinner) view.findViewById(R.id.spTimeUnit); spTimeUnit = (Spinner) view.findViewById(R.id.spTimeUnit);
cbShakeToReset = (CheckBox) view.findViewById(R.id.cbShakeToReset); cbShakeToReset = (CheckBox) view.findViewById(R.id.cbShakeToReset);
cbVibrate = (CheckBox) view.findViewById(R.id.cbVibrate); 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() { etxtTime.addTextChangedListener(new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
@ -104,11 +98,10 @@ public abstract class SleepTimerDialog {
android.R.layout.simple_spinner_item, spinnerContent); android.R.layout.simple_spinner_item, spinnerContent);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setAdapter(spinnerAdapter);
int selection = prefs.getInt(PREF_TIME_UNIT, DEFAULT_SPINNER_POSITION); spTimeUnit.setSelection(SleepTimerPreferences.lastTimerTimeUnit());
spTimeUnit.setSelection(selection);
cbShakeToReset.setChecked(prefs.getBoolean(PREF_SHAKE_TO_RESET, true)); cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset());
cbVibrate.setChecked(prefs.getBoolean(PREF_VIBRATE, true)); cbVibrate.setChecked(SleepTimerPreferences.vibrate());
return dialog; return dialog;
} }
@ -132,12 +125,10 @@ public abstract class SleepTimerDialog {
} }
private void savePreferences() { private void savePreferences() {
prefs.edit() SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
.putString(PREF_VALUE, etxtTime.getText().toString()) spTimeUnit.getSelectedItemPosition());
.putInt(PREF_TIME_UNIT, spTimeUnit.getSelectedItemPosition()) SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
.putBoolean(PREF_SHAKE_TO_RESET, cbShakeToReset.isChecked()) SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
.putBoolean(PREF_VIBRATE, cbVibrate.isChecked())
.apply();
} }
} }

View File

@ -33,26 +33,28 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:layout_marginTop="8dp"
android:text="@string/timer_about_to_expire_label"
android:textSize="16sp" />
<TextView <CheckBox
android:layout_width="wrap_content" android:id="@+id/cbShakeToReset"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginTop="8dp" android:layout_height="wrap_content"
android:textSize="16sp" android:text="@string/shake_to_reset_label" />
android:text="@string/timer_about_to_expire_label"/>
<CheckBox android:id="@+id/cbShakeToReset" <CheckBox
android:layout_width="wrap_content" android:id="@+id/cbVibrate"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="@string/shake_to_reset_label"/> android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" />
<CheckBox android:id="@+id/cbVibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timer_vibration_label"/>
</LinearLayout> </LinearLayout>

View File

@ -36,22 +36,24 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" 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:layout_width="wrap_content" android:id="@+id/cbShakeToReset"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="@string/shake_to_reset_label"/> android:layout_height="wrap_content"
android:text="@string/shake_to_reset_label" />
<CheckBox android:id="@+id/cbVibrate" <CheckBox
android:layout_width="wrap_content" android:id="@+id/cbVibrate"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="@string/timer_vibration_label"/> android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" />
</LinearLayout> </LinearLayout>

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.core;
import android.content.Context; import android.content.Context;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences; 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.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.NetworkUtils;
@ -43,7 +44,7 @@ public class ClientConfig {
UpdateManager.init(context); UpdateManager.init(context);
PlaybackPreferences.init(context); PlaybackPreferences.init(context);
NetworkUtils.init(context); NetworkUtils.init(context);
// CastManager.init(context); SleepTimerPreferences.init(context);
initialized = true; 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="one">1 hour</item>
<item quantity="other">%d hours</item> <item quantity="other">%d hours</item>
</plurals> </plurals>
<string name="auto_enable">Auto-enable</string>
<!-- gpodder.net --> <!-- gpodder.net -->
<string name="gpodnet_taglist_header">CATEGORIES</string> <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.cast.CastManager;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences; 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.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.NetworkUtils;
@ -45,6 +46,7 @@ public class ClientConfig {
PlaybackPreferences.init(context); PlaybackPreferences.init(context);
NetworkUtils.init(context); NetworkUtils.init(context);
CastManager.init(context); CastManager.init(context);
SleepTimerPreferences.init(context);
initialized = true; initialized = true;
} }