Merge pull request #2152 from mfietz/2085-auto-enable-sleep-timer

Option to auto-enable sleep timer
This commit is contained in:
Martin Fietz 2016-10-30 19:12:25 +01:00 committed by GitHub
commit cba6e728d2
13 changed files with 210 additions and 65 deletions

View File

@ -10,6 +10,7 @@ import android.database.DataSetObserver;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
@ -38,6 +39,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.event.ProgressEvent;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
@ -733,6 +735,18 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
}
}
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
View parentLayout = findViewById(R.id.drawer_layout);
Snackbar snackbar = Snackbar.make(parentLayout, event.message, Snackbar.LENGTH_SHORT);
if(event.action != null) {
snackbar.setAction(getString(R.string.undo), v -> {
event.action.run();
});
}
snackbar.show();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override

View File

@ -47,6 +47,7 @@ import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.dialog.SleepTimerDialog;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
@ -217,6 +218,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
controller.pause();
}
super.onPause();
EventBus.getDefault().unregister(this);
}
/**
@ -580,6 +582,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
if(controller != null) {
controller.init();
}
EventBus.getDefault().register(this);
}
/**

View File

@ -8,6 +8,7 @@ import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
@ -36,6 +37,7 @@ import de.danoeh.antennapod.adapter.ChaptersListAdapter;
import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedMedia;
@ -464,7 +466,17 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
View parentLayout = findViewById(R.id.drawer_layout);
Snackbar snackbar = Snackbar.make(parentLayout, event.message, Snackbar.LENGTH_SHORT);
if (event.action != null) {
snackbar.setAction(getString(R.string.undo), v -> {
event.action.run();
});
}
snackbar.show();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -1,7 +1,6 @@
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
@ -16,36 +15,25 @@ import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
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;
private EditText etxtTime;
private Spinner spTimeUnit;
private CheckBox cbShakeToReset;
private CheckBox cbVibrate;
private CheckBox chAutoEnable;
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() {
@ -58,7 +46,7 @@ public abstract class SleepTimerDialog {
builder.onPositive((dialog, which) -> {
try {
savePreferences();
long input = readTimeMillis();
long input = SleepTimerPreferences.timerMillis();
onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked());
dialog.dismiss();
} catch (NumberFormatException e) {
@ -75,8 +63,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 +93,11 @@ 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());
chAutoEnable.setChecked(SleepTimerPreferences.autoEnable());
return dialog;
}
@ -125,19 +114,12 @@ public abstract class SleepTimerDialog {
public abstract void onTimerSet(long millis, boolean shakeToReset, boolean vibrate);
private long readTimeMillis() {
TimeUnit selectedUnit = units[spTimeUnit.getSelectedItemPosition()];
long value = Long.parseLong(etxtTime.getText().toString());
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();
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
spTimeUnit.getSelectedItemPosition());
SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
SleepTimerPreferences.setAutoEnable(chAutoEnable.isChecked());
}
}

View File

@ -41,19 +41,27 @@
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" />
<CheckBox
android:id="@+id/chAutoEnable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_enable_label" />
</LinearLayout>
</LinearLayout>

View File

@ -40,19 +40,27 @@
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" />
<CheckBox
android:id="@+id/chAutoEnable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_enable_label" />
</LinearLayout>
</LinearLayout>

View File

@ -6,10 +6,10 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "com.android.tools.build:gradle:2.2.2"
classpath "me.tatarka:gradle-retrolambda:3.3.1"
classpath "me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2"
classpath 'com.github.triplet.gradle:play-publisher:1.1.4'
classpath "com.github.triplet.gradle:play-publisher:1.1.4"
// Exclude the version that the android plugin depends on.
configurations.classpath.exclude group: "com.android.tools.external.lombok"
}

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,21 @@
package de.danoeh.antennapod.core.event;
import android.support.annotation.Nullable;
public class MessageEvent {
public final String message;
@Nullable
public final Runnable action;
public MessageEvent(String message) {
this(message, null);
}
public MessageEvent(String message, Runnable action) {
this.message = message;
this.action = action;
}
}

View File

@ -0,0 +1,80 @@
package de.danoeh.antennapod.core.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.concurrent.TimeUnit;
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 TimeUnit[] UNITS = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
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 long timerMillis() {
long value = Long.parseLong(lastTimerValue());
return UNITS[lastTimerTimeUnit()].toMillis(value);
}
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

@ -47,12 +47,14 @@ import java.util.List;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
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.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.storage.DBReader;
@ -62,6 +64,7 @@ import de.danoeh.antennapod.core.util.IntList;
import de.danoeh.antennapod.core.util.QueueAccess;
import de.danoeh.antennapod.core.util.playback.ExternalMedia;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.greenrobot.event.EventBus;
/**
* Controls the MediaPlayer that plays a FeedMedia-file
@ -605,6 +608,11 @@ public class PlaybackService extends MediaBrowserServiceCompat {
writePlayerStatusPlaybackPreferences();
setupNotification(newInfo);
started = true;
// set sleep timer if auto-enabled
if(SleepTimerPreferences.autoEnable() && !sleepTimerActive()) {
setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(),
SleepTimerPreferences.vibrate());
}
break;
case ERROR:
@ -846,11 +854,14 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Log.d(TAG, "Setting sleep timer to " + Long.toString(waitingTime) + " milliseconds");
taskManager.setSleepTimer(waitingTime, shakeToReset, vibrate);
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label),
() -> disableSleepTimer()));
}
public void disableSleepTimer() {
taskManager.disableSleepTimer();
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_disabled_label)));
}
private void writePlaybackPreferencesNoMediaPlaying() {

View File

@ -484,6 +484,9 @@
<item quantity="one">1 hour</item>
<item quantity="other">%d hours</item>
</plurals>
<string name="auto_enable_label">Auto-enable</string>
<string name="sleep_timer_enabled_label">Sleep timer enabled</string>
<string name="sleep_timer_disabled_label">Sleep timer disabled</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;
}