Implemented sleep timer menu item + fixed problems with dialog
This commit is contained in:
parent
791acc935c
commit
e23c1a0143
|
@ -11,8 +11,9 @@
|
|||
|
||||
<EditText
|
||||
android:id="@+id/etxtTime"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:layout_margin="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/enter_time_here_label"
|
||||
|
@ -24,10 +25,9 @@
|
|||
|
||||
<Spinner
|
||||
android:id="@+id/spTimeUnit"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginTop="8dp" />
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
@ -155,7 +155,7 @@
|
|||
<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="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>
|
|
@ -47,6 +47,7 @@ import de.danoeh.antennapod.AppConfig;
|
|||
import de.danoeh.antennapod.PodcastApp;
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.adapter.SCListAdapter;
|
||||
import de.danoeh.antennapod.dialog.TimeDialog;
|
||||
import de.danoeh.antennapod.feed.FeedManager;
|
||||
import de.danoeh.antennapod.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.feed.SimpleChapter;
|
||||
|
@ -142,9 +143,11 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||
media != null && media.getItem().getLink() != null);
|
||||
menu.findItem(R.id.visit_website_item).setVisible(
|
||||
media != null && media.getItem().getLink() != null);
|
||||
|
||||
boolean sleepTimerSet = playbackService != null && playbackService.sleepTimerActive();
|
||||
boolean sleepTimerNotSet = playbackService != null && !playbackService.sleepTimerActive();
|
||||
|
||||
boolean sleepTimerSet = playbackService != null
|
||||
&& playbackService.sleepTimerActive();
|
||||
boolean sleepTimerNotSet = playbackService != null
|
||||
&& !playbackService.sleepTimerActive();
|
||||
menu.findItem(R.id.set_sleeptimer_item).setVisible(sleepTimerNotSet);
|
||||
menu.findItem(R.id.disable_sleeptimer_item).setVisible(sleepTimerSet);
|
||||
return true;
|
||||
|
@ -157,6 +160,28 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||
startActivity(new Intent(MediaplayerActivity.this,
|
||||
MainActivity.class));
|
||||
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:
|
||||
return FeedItemMenuHandler.onMenuItemClicked(this, item,
|
||||
media.getItem());
|
||||
|
@ -642,6 +667,9 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
|
|||
mediaInfoLoaded = false;
|
||||
queryService();
|
||||
|
||||
break;
|
||||
case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
|
||||
invalidateOptionsMenu();
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,14 +8,13 @@ import android.content.Context;
|
|||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
|
||||
public abstract class TimeDialog extends Dialog {
|
||||
private int leftButtonTextId;
|
||||
private int titleTextId;
|
||||
|
||||
private EditText etxtTime;
|
||||
private Spinner spTimeUnit;
|
||||
|
@ -25,25 +24,28 @@ public abstract class TimeDialog extends Dialog {
|
|||
private String[] spinnerContent = { "min", "h" };
|
||||
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);
|
||||
this.leftButtonTextId = leftButtonTextId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(R.layout.time_dialog);
|
||||
etxtTime = (EditText) findViewById(R.id.etxtTime);
|
||||
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
|
||||
butConfirm = (Button) findViewById(R.id.butConfirm);
|
||||
butCancel = (Button) findViewById(R.id.butCancel);
|
||||
butConfirm.setText(leftButtonTextId);
|
||||
|
||||
butConfirm.setText(R.string.set_sleeptimer_label);
|
||||
butCancel.setText(R.string.cancel_label);
|
||||
setTitle(titleTextId);
|
||||
spTimeUnit.setAdapter(new ArrayAdapter<String>(this.getContext(), 0,
|
||||
spinnerContent));
|
||||
setTitle(R.string.set_sleeptimer_label);
|
||||
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item,
|
||||
spinnerContent);
|
||||
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spTimeUnit.setAdapter(spinnerAdapter);
|
||||
spTimeUnit.setSelection(0);
|
||||
butCancel.setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -70,6 +70,7 @@ public class PlaybackService extends Service {
|
|||
public static final int NOTIFICATION_TYPE_INFO = 1;
|
||||
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
|
||||
public static final int NOTIFICATION_TYPE_RELOAD = 3;
|
||||
/** The state of the sleeptimer changed. */
|
||||
public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
|
||||
|
||||
/** Is true if service is running. */
|
||||
|
@ -459,11 +460,14 @@ public class PlaybackService extends Service {
|
|||
};
|
||||
|
||||
public void setSleepTimer(long waitingTime) {
|
||||
if (AppConfig.DEBUG)
|
||||
Log.d(TAG, "Setting sleep timer to " + Long.toString(waitingTime)
|
||||
+ " milliseconds");
|
||||
if (sleepTimer != null) {
|
||||
sleepTimer.interrupt();
|
||||
sleepTimer.cancel(true);
|
||||
}
|
||||
sleepTimer = new SleepTimer(waitingTime);
|
||||
sleepTimer.start();
|
||||
sleepTimer.executeAsync();
|
||||
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||
}
|
||||
|
||||
|
@ -471,7 +475,7 @@ public class PlaybackService extends Service {
|
|||
if (sleepTimer != null) {
|
||||
if (AppConfig.DEBUG)
|
||||
Log.d(TAG, "Disabling sleep timer");
|
||||
sleepTimer.interrupt();
|
||||
sleepTimer.cancel(true);
|
||||
sleepTimer = null;
|
||||
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||
}
|
||||
|
@ -632,9 +636,9 @@ public class PlaybackService extends Service {
|
|||
PlaybackService.this.sendBroadcast(new Intent(
|
||||
PlayerWidget.FORCE_WIDGET_UPDATE));
|
||||
}
|
||||
|
||||
|
||||
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. */
|
||||
class SleepTimer extends Thread {
|
||||
class SleepTimer extends AsyncTask<Void, Void, Void> {
|
||||
private static final String TAG = "SleepTimer";
|
||||
private static final long UPDATE_INTERVALL = 1000L;
|
||||
private long waitingTime;
|
||||
private boolean isWaiting;
|
||||
|
||||
|
@ -739,22 +744,47 @@ public class PlaybackService extends Service {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
protected Void doInBackground(Void... params) {
|
||||
isWaiting = true;
|
||||
if (AppConfig.DEBUG)
|
||||
Log.d(TAG, "Starting");
|
||||
try {
|
||||
Thread.sleep(waitingTime);
|
||||
if (status == PlayerStatus.PLAYING) {
|
||||
Log.d(TAG, "Pausing playback");
|
||||
pause(true);
|
||||
while (!isCancelled()) {
|
||||
try {
|
||||
Thread.sleep(UPDATE_INTERVALL);
|
||||
waitingTime -= UPDATE_INTERVALL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
isWaiting = false;
|
||||
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
|
||||
}
|
||||
|
||||
public long getWaitingTime() {
|
||||
return waitingTime;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue