mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-12-01 12:31:45 +00:00
Implemented sleep timer menu item + fixed problems with dialog
This commit is contained in:
@ -11,8 +11,9 @@
|
|||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/etxtTime"
|
android:id="@+id/etxtTime"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
android:layout_margin="8dp"
|
android:layout_margin="8dp"
|
||||||
android:ems="10"
|
android:ems="10"
|
||||||
android:hint="@string/enter_time_here_label"
|
android:hint="@string/enter_time_here_label"
|
||||||
@ -24,10 +25,9 @@
|
|||||||
|
|
||||||
<Spinner
|
<Spinner
|
||||||
android:id="@+id/spTimeUnit"
|
android:id="@+id/spTimeUnit"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="100dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="8dp"
|
android:layout_marginBottom="8dp"
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginTop="8dp" />
|
android:layout_marginTop="8dp" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
@ -155,7 +155,7 @@
|
|||||||
<string name="opml_export_success_sum">The .opml file was written to:\u0020</string>
|
<string name="opml_export_success_sum">The .opml file was written to:\u0020</string>
|
||||||
<string name="set_sleeptimer_label">Set sleep timer</string>
|
<string name="set_sleeptimer_label">Set sleep timer</string>
|
||||||
<string name="disable_sleeptimer_label">Disable sleep timer</string>
|
<string name="disable_sleeptimer_label">Disable sleep timer</string>
|
||||||
<string name="enter_time_here_label">Enter time here</string>
|
<string name="enter_time_here_label">Enter time</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
@ -47,6 +47,7 @@ import de.danoeh.antennapod.AppConfig;
|
|||||||
import de.danoeh.antennapod.PodcastApp;
|
import de.danoeh.antennapod.PodcastApp;
|
||||||
import de.danoeh.antennapod.R;
|
import de.danoeh.antennapod.R;
|
||||||
import de.danoeh.antennapod.adapter.SCListAdapter;
|
import de.danoeh.antennapod.adapter.SCListAdapter;
|
||||||
|
import de.danoeh.antennapod.dialog.TimeDialog;
|
||||||
import de.danoeh.antennapod.feed.FeedManager;
|
import de.danoeh.antennapod.feed.FeedManager;
|
||||||
import de.danoeh.antennapod.feed.FeedMedia;
|
import de.danoeh.antennapod.feed.FeedMedia;
|
||||||
import de.danoeh.antennapod.feed.SimpleChapter;
|
import de.danoeh.antennapod.feed.SimpleChapter;
|
||||||
@ -142,9 +143,11 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||||||
media != null && media.getItem().getLink() != null);
|
media != null && media.getItem().getLink() != null);
|
||||||
menu.findItem(R.id.visit_website_item).setVisible(
|
menu.findItem(R.id.visit_website_item).setVisible(
|
||||||
media != null && media.getItem().getLink() != null);
|
media != null && media.getItem().getLink() != null);
|
||||||
|
|
||||||
boolean sleepTimerSet = playbackService != null && playbackService.sleepTimerActive();
|
boolean sleepTimerSet = playbackService != null
|
||||||
boolean sleepTimerNotSet = playbackService != null && !playbackService.sleepTimerActive();
|
&& playbackService.sleepTimerActive();
|
||||||
|
boolean sleepTimerNotSet = playbackService != null
|
||||||
|
&& !playbackService.sleepTimerActive();
|
||||||
menu.findItem(R.id.set_sleeptimer_item).setVisible(sleepTimerNotSet);
|
menu.findItem(R.id.set_sleeptimer_item).setVisible(sleepTimerNotSet);
|
||||||
menu.findItem(R.id.disable_sleeptimer_item).setVisible(sleepTimerSet);
|
menu.findItem(R.id.disable_sleeptimer_item).setVisible(sleepTimerSet);
|
||||||
return true;
|
return true;
|
||||||
@ -157,6 +160,28 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||||||
startActivity(new Intent(MediaplayerActivity.this,
|
startActivity(new Intent(MediaplayerActivity.this,
|
||||||
MainActivity.class));
|
MainActivity.class));
|
||||||
break;
|
break;
|
||||||
|
case R.id.disable_sleeptimer_item:
|
||||||
|
if (playbackService != null) {
|
||||||
|
playbackService.disableSleepTimer();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case R.id.set_sleeptimer_item:
|
||||||
|
if (playbackService != null) {
|
||||||
|
TimeDialog td = new TimeDialog(this,
|
||||||
|
R.string.set_sleeptimer_label,
|
||||||
|
R.string.set_sleeptimer_label) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTimeEntered(long millis) {
|
||||||
|
if (playbackService != null) {
|
||||||
|
playbackService.setSleepTimer(millis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
td.show();
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return FeedItemMenuHandler.onMenuItemClicked(this, item,
|
return FeedItemMenuHandler.onMenuItemClicked(this, item,
|
||||||
media.getItem());
|
media.getItem());
|
||||||
@ -642,6 +667,9 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||||||
mediaInfoLoaded = false;
|
mediaInfoLoaded = false;
|
||||||
queryService();
|
queryService();
|
||||||
|
|
||||||
|
break;
|
||||||
|
case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
|
||||||
|
invalidateOptionsMenu();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,14 +8,13 @@ import android.content.Context;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.Window;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
|
|
||||||
public abstract class TimeDialog extends Dialog {
|
public abstract class TimeDialog extends Dialog {
|
||||||
private int leftButtonTextId;
|
|
||||||
private int titleTextId;
|
|
||||||
|
|
||||||
private EditText etxtTime;
|
private EditText etxtTime;
|
||||||
private Spinner spTimeUnit;
|
private Spinner spTimeUnit;
|
||||||
@ -25,25 +24,28 @@ public abstract class TimeDialog extends Dialog {
|
|||||||
private String[] spinnerContent = { "min", "h" };
|
private String[] spinnerContent = { "min", "h" };
|
||||||
private TimeUnit[] units = { TimeUnit.MINUTES, TimeUnit.HOURS };
|
private TimeUnit[] units = { TimeUnit.MINUTES, TimeUnit.HOURS };
|
||||||
|
|
||||||
public TimeDialog(Context context, int TitleTextId, int leftButtonTextId) {
|
public TimeDialog(Context context, int titleTextId, int leftButtonTextId) {
|
||||||
super(context);
|
super(context);
|
||||||
this.leftButtonTextId = leftButtonTextId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(R.layout.time_dialog);
|
setContentView(R.layout.time_dialog);
|
||||||
etxtTime = (EditText) findViewById(R.id.etxtTime);
|
etxtTime = (EditText) findViewById(R.id.etxtTime);
|
||||||
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
|
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
|
||||||
butConfirm = (Button) findViewById(R.id.butConfirm);
|
butConfirm = (Button) findViewById(R.id.butConfirm);
|
||||||
butCancel = (Button) findViewById(R.id.butCancel);
|
butCancel = (Button) findViewById(R.id.butCancel);
|
||||||
butConfirm.setText(leftButtonTextId);
|
|
||||||
|
butConfirm.setText(R.string.set_sleeptimer_label);
|
||||||
butCancel.setText(R.string.cancel_label);
|
butCancel.setText(R.string.cancel_label);
|
||||||
setTitle(titleTextId);
|
setTitle(R.string.set_sleeptimer_label);
|
||||||
spTimeUnit.setAdapter(new ArrayAdapter<String>(this.getContext(), 0,
|
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item,
|
||||||
spinnerContent));
|
spinnerContent);
|
||||||
|
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||||
|
spTimeUnit.setAdapter(spinnerAdapter);
|
||||||
|
spTimeUnit.setSelection(0);
|
||||||
butCancel.setOnClickListener(new View.OnClickListener() {
|
butCancel.setOnClickListener(new View.OnClickListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -70,6 +70,7 @@ public class PlaybackService extends Service {
|
|||||||
public static final int NOTIFICATION_TYPE_INFO = 1;
|
public static final int NOTIFICATION_TYPE_INFO = 1;
|
||||||
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
|
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
|
||||||
public static final int NOTIFICATION_TYPE_RELOAD = 3;
|
public static final int NOTIFICATION_TYPE_RELOAD = 3;
|
||||||
|
/** The state of the sleeptimer changed. */
|
||||||
public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
|
public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
|
||||||
|
|
||||||
/** Is true if service is running. */
|
/** Is true if service is running. */
|
||||||
@ -459,11 +460,14 @@ public class PlaybackService extends Service {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public void setSleepTimer(long waitingTime) {
|
public void setSleepTimer(long waitingTime) {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "Setting sleep timer to " + Long.toString(waitingTime)
|
||||||
|
+ " milliseconds");
|
||||||
if (sleepTimer != null) {
|
if (sleepTimer != null) {
|
||||||
sleepTimer.interrupt();
|
sleepTimer.cancel(true);
|
||||||
}
|
}
|
||||||
sleepTimer = new SleepTimer(waitingTime);
|
sleepTimer = new SleepTimer(waitingTime);
|
||||||
sleepTimer.start();
|
sleepTimer.executeAsync();
|
||||||
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,7 +475,7 @@ public class PlaybackService extends Service {
|
|||||||
if (sleepTimer != null) {
|
if (sleepTimer != null) {
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Disabling sleep timer");
|
Log.d(TAG, "Disabling sleep timer");
|
||||||
sleepTimer.interrupt();
|
sleepTimer.cancel(true);
|
||||||
sleepTimer = null;
|
sleepTimer = null;
|
||||||
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||||
}
|
}
|
||||||
@ -632,9 +636,9 @@ public class PlaybackService extends Service {
|
|||||||
PlaybackService.this.sendBroadcast(new Intent(
|
PlaybackService.this.sendBroadcast(new Intent(
|
||||||
PlayerWidget.FORCE_WIDGET_UPDATE));
|
PlayerWidget.FORCE_WIDGET_UPDATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sleepTimerActive() {
|
public boolean sleepTimerActive() {
|
||||||
return sleepTimer == null || sleepTimer.isWaiting();
|
return sleepTimer != null && sleepTimer.isWaiting();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -728,8 +732,9 @@ public class PlaybackService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Sleeps for a given time and then pauses playback. */
|
/** Sleeps for a given time and then pauses playback. */
|
||||||
class SleepTimer extends Thread {
|
class SleepTimer extends AsyncTask<Void, Void, Void> {
|
||||||
private static final String TAG = "SleepTimer";
|
private static final String TAG = "SleepTimer";
|
||||||
|
private static final long UPDATE_INTERVALL = 1000L;
|
||||||
private long waitingTime;
|
private long waitingTime;
|
||||||
private boolean isWaiting;
|
private boolean isWaiting;
|
||||||
|
|
||||||
@ -739,22 +744,47 @@ public class PlaybackService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
protected Void doInBackground(Void... params) {
|
||||||
isWaiting = true;
|
isWaiting = true;
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Starting");
|
Log.d(TAG, "Starting");
|
||||||
try {
|
while (!isCancelled()) {
|
||||||
Thread.sleep(waitingTime);
|
try {
|
||||||
if (status == PlayerStatus.PLAYING) {
|
Thread.sleep(UPDATE_INTERVALL);
|
||||||
Log.d(TAG, "Pausing playback");
|
waitingTime -= UPDATE_INTERVALL;
|
||||||
pause(true);
|
|
||||||
|
if (waitingTime <= 0 && status == PlayerStatus.PLAYING) {
|
||||||
|
Log.d(TAG, "Pausing playback");
|
||||||
|
pause(true);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Log.d(TAG, "Thread was interrupted while waiting");
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Log.d(TAG, "Thread was interrupted while waiting");
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
|
public void executeAsync() {
|
||||||
|
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
|
||||||
|
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
} else {
|
||||||
|
execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCancelled() {
|
||||||
isWaiting = false;
|
isWaiting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void result) {
|
||||||
|
isWaiting = false;
|
||||||
|
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||||
|
}
|
||||||
|
|
||||||
public long getWaitingTime() {
|
public long getWaitingTime() {
|
||||||
return waitingTime;
|
return waitingTime;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user