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.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
@ -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,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() {
@Override

View File

@ -31,6 +31,7 @@ import com.joanzapata.iconify.fonts.FontAwesomeIcons;
import java.util.Locale;
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.FeedMedia;
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.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 +219,12 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
controller.pause();
}
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) {
controller.init();
}
EventBus.getDefault().register(this);
}
/**

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,8 +15,6 @@ 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;
@ -25,17 +22,15 @@ public abstract class SleepTimerDialog {
private static final String TAG = SleepTimerDialog.class.getSimpleName();
private Context context;
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;
@ -51,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) {
@ -102,6 +97,7 @@ public abstract class SleepTimerDialog {
cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset());
cbVibrate.setChecked(SleepTimerPreferences.vibrate());
chAutoEnable.setChecked(SleepTimerPreferences.autoEnable());
return dialog;
}
@ -118,17 +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() {
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
spTimeUnit.getSelectedItemPosition());
SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
SleepTimerPreferences.setAutoEnable(chAutoEnable.isChecked());
}
}

View File

@ -56,6 +56,12 @@
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

@ -55,6 +55,12 @@
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

@ -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.util.Log;
import java.util.concurrent.TimeUnit;
public class 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_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;
@ -44,6 +48,11 @@ public class SleepTimerPreferences {
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();
}

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,13 @@ 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)));
}
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

@ -482,7 +482,9 @@
<item quantity="one">1 hour</item>
<item quantity="other">%d hours</item>
</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 -->
<string name="gpodnet_taglist_header">CATEGORIES</string>