diff options
author | Martin Fietz <Martin.Fietz@gmail.com> | 2016-10-30 19:12:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-30 19:12:25 +0100 |
commit | cba6e728d2b6c6b791b37604e60557fb405e3b71 (patch) | |
tree | df01d31f66f7821436be8a392503f10d3b82785a | |
parent | 56318ae99ce9965dcadd411b6f123d52da766901 (diff) | |
parent | 0062cab1066d42d546d4a2e13012db9d2071db16 (diff) | |
download | AntennaPod-cba6e728d2b6c6b791b37604e60557fb405e3b71.zip |
Merge pull request #2152 from mfietz/2085-auto-enable-sleep-timer
Option to auto-enable sleep timer
13 files changed, 215 insertions, 70 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 15313d772..1e03f99fa 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -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 diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 48b578be7..1cb936296 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -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); } /** diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java index d9b2d10da..4a24f0329 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java @@ -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() { 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 8a13a75d9..0ddee9f61 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java @@ -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()); } } diff --git a/app/src/main/res/layout-v14/time_dialog.xml b/app/src/main/res/layout-v14/time_dialog.xml index 06c2cce14..ba4249268 100644 --- a/app/src/main/res/layout-v14/time_dialog.xml +++ b/app/src/main/res/layout-v14/time_dialog.xml @@ -33,26 +33,34 @@ </LinearLayout> <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:text="@string/timer_about_to_expire_label" + android:textSize="16sp" /> + + <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" + 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:orientation="vertical"> - - <TextView - 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"/> - - <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" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/timer_vibration_label"/> + android:text="@string/auto_enable_label" /> </LinearLayout> diff --git a/app/src/main/res/layout/time_dialog.xml b/app/src/main/res/layout/time_dialog.xml index b270e82f7..0290ce708 100644 --- a/app/src/main/res/layout/time_dialog.xml +++ b/app/src/main/res/layout/time_dialog.xml @@ -36,22 +36,30 @@ android:layout_height="wrap_content" android:orientation="vertical"> - <TextView - 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"/> - - <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" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/timer_vibration_label"/> + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:text="@string/timer_about_to_expire_label" + android:textSize="16sp" /> + + <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" + 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> diff --git a/build.gradle b/build.gradle index eddcd91d5..ad88bb7bf 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } diff --git a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java index eb2e6fc9e..88ae6d6bc 100644 --- a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java +++ b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java @@ -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; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java b/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java new file mode 100644 index 000000000..9fc488fbc --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/event/MessageEvent.java @@ -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; + } + +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java new file mode 100644 index 000000000..b7ed890f5 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/SleepTimerPreferences.java @@ -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); + } + +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index e67dc9d0a..33aec0ee0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -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() { diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 8a92ab5b2..621fc547b 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -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> diff --git a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java index 3dfd6ea65..f12f1d806 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java +++ b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java @@ -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; } |