Add dialog option to auto-enable sleep timer

This commit is contained in:
Martin Fietz 2016-10-09 22:40:40 +02:00
parent e75d60ef61
commit 21799ab22c
9 changed files with 78 additions and 14 deletions

View File

@ -26,6 +26,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -38,6 +39,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.NavListAdapter; import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover; import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog; 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.ProgressEvent;
import de.danoeh.antennapod.core.event.QueueEvent; import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.EventDistributor;
@ -733,6 +735,11 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
} }
} }
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() { private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override @Override

View File

@ -31,6 +31,7 @@ import com.joanzapata.iconify.fonts.FontAwesomeIcons;
import java.util.Locale; import java.util.Locale;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
@ -47,6 +48,7 @@ import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.dialog.SleepTimerDialog; import de.danoeh.antennapod.dialog.SleepTimerDialog;
import de.danoeh.antennapod.dialog.VariableSpeedDialog; import de.danoeh.antennapod.dialog.VariableSpeedDialog;
import de.greenrobot.event.EventBus;
import rx.Observable; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@ -217,6 +219,12 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
controller.pause(); controller.pause();
} }
super.onPause(); super.onPause();
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show();
} }
/** /**
@ -580,6 +588,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
if(controller != null) { if(controller != null) {
controller.init(); controller.init();
} }
EventBus.getDefault().register(this);
} }
/** /**

View File

@ -1,7 +1,6 @@
package de.danoeh.antennapod.dialog; package de.danoeh.antennapod.dialog;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
@ -16,8 +15,6 @@ import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
@ -25,17 +22,15 @@ public abstract class SleepTimerDialog {
private static final String TAG = SleepTimerDialog.class.getSimpleName(); private static final String TAG = SleepTimerDialog.class.getSimpleName();
private Context context; private Context context;
private SharedPreferences prefs;
private MaterialDialog dialog; private MaterialDialog dialog;
private EditText etxtTime; private EditText etxtTime;
private Spinner spTimeUnit; private Spinner spTimeUnit;
private CheckBox cbShakeToReset; private CheckBox cbShakeToReset;
private CheckBox cbVibrate; private CheckBox cbVibrate;
private CheckBox chAutoEnable;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public SleepTimerDialog(Context context) { public SleepTimerDialog(Context context) {
this.context = context; this.context = context;
@ -51,7 +46,7 @@ public abstract class SleepTimerDialog {
builder.onPositive((dialog, which) -> { builder.onPositive((dialog, which) -> {
try { try {
savePreferences(); savePreferences();
long input = readTimeMillis(); long input = SleepTimerPreferences.timerMillis();
onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked()); onTimerSet(input, cbShakeToReset.isChecked(), cbVibrate.isChecked());
dialog.dismiss(); dialog.dismiss();
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -102,6 +97,7 @@ public abstract class SleepTimerDialog {
cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset()); cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset());
cbVibrate.setChecked(SleepTimerPreferences.vibrate()); cbVibrate.setChecked(SleepTimerPreferences.vibrate());
chAutoEnable.setChecked(SleepTimerPreferences.autoEnable());
return dialog; return dialog;
} }
@ -118,17 +114,12 @@ public abstract class SleepTimerDialog {
public abstract void onTimerSet(long millis, boolean shakeToReset, boolean vibrate); 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() { private void savePreferences() {
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(), SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
spTimeUnit.getSelectedItemPosition()); spTimeUnit.getSelectedItemPosition());
SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked()); SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
SleepTimerPreferences.setVibrate(cbVibrate.isChecked()); SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
SleepTimerPreferences.setAutoEnable(chAutoEnable.isChecked());
} }
} }

View File

@ -56,6 +56,12 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" /> 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>
</LinearLayout> </LinearLayout>

View File

@ -55,6 +55,12 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/timer_vibration_label" /> 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>
</LinearLayout> </LinearLayout>

View File

@ -0,0 +1,24 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.danoeh.antennapod.core.event;
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}

View File

@ -5,6 +5,8 @@ import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import java.util.concurrent.TimeUnit;
public class SleepTimerPreferences { public class SleepTimerPreferences {
private static final String TAG = "SleepTimerPreferences"; private static final String TAG = "SleepTimerPreferences";
@ -16,6 +18,8 @@ public class SleepTimerPreferences {
private static final String PREF_SHAKE_TO_RESET = "ShakeToReset"; private static final String PREF_SHAKE_TO_RESET = "ShakeToReset";
private static final String PREF_AUTO_ENABLE = "AutoEnable"; 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 String DEFAULT_VALUE = "15";
private static final int DEFAULT_TIME_UNIT = 1; private static final int DEFAULT_TIME_UNIT = 1;
@ -44,6 +48,11 @@ public class SleepTimerPreferences {
return prefs.getInt(PREF_TIME_UNIT, DEFAULT_TIME_UNIT); 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) { public static void setVibrate(boolean vibrate) {
prefs.edit().putBoolean(PREF_VIBRATE, vibrate).apply(); prefs.edit().putBoolean(PREF_VIBRATE, vibrate).apply();
} }

View File

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

View File

@ -482,7 +482,9 @@
<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> <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 --> <!-- gpodder.net -->
<string name="gpodnet_taglist_header">CATEGORIES</string> <string name="gpodnet_taglist_header">CATEGORIES</string>