Implemented sleep timer in playaback service and created time dialog

This commit is contained in:
daniel oeh 2012-07-27 13:52:54 +02:00
parent b93c5c9b63
commit 791acc935c
9 changed files with 205 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/etxtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:ems="10"
android:hint="@string/enter_time_here_label"
android:inputType="number"
android:maxLength="2" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/spTimeUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp" />
</LinearLayout>
<LinearLayout
style="@android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/butConfirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1" />
<Button
android:id="@+id/butCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
<item android:id="@+id/disable_sleeptimer_item" android:icon="@drawable/device_access_time" android:title="@string/disable_sleeptimer_label" android:showAsAction="always"></item><item android:id="@+id/set_sleeptimer_item" android:showAsAction="collapseActionView" android:title="@string/set_sleeptimer_label"></item><item
android:id="@+id/share_link_item"
android:showAsAction="collapseActionView"
android:title="@string/share_link_label">
@ -19,5 +19,7 @@
android:title="@string/support_label"
android:visible="false">
</item>
</menu>

View File

@ -153,6 +153,9 @@
<string name="export_error_label">Export error</string>
<string name="opml_export_success_title">Opml export successful.</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="disable_sleeptimer_label">Disable sleep timer</string>
<string name="enter_time_here_label">Enter time here</string>
</resources>

View File

@ -142,6 +142,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();
menu.findItem(R.id.set_sleeptimer_item).setVisible(sleepTimerNotSet);
menu.findItem(R.id.disable_sleeptimer_item).setVisible(sleepTimerSet);
return true;
}

View File

@ -0,0 +1,73 @@
package de.danoeh.antennapod.dialog;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
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;
private Button butConfirm;
private Button butCancel;
private String[] spinnerContent = { "min", "h" };
private TimeUnit[] units = { TimeUnit.MINUTES, TimeUnit.HOURS };
public TimeDialog(Context context, int TitleTextId, int leftButtonTextId) {
super(context);
this.leftButtonTextId = leftButtonTextId;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
butCancel.setText(R.string.cancel_label);
setTitle(titleTextId);
spTimeUnit.setAdapter(new ArrayAdapter<String>(this.getContext(), 0,
spinnerContent));
butCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
butConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
onTimeEntered(readTimeMillis());
}
});
}
public abstract void onTimeEntered(long millis);
private long readTimeMillis() {
TimeUnit selectedUnit = units[spTimeUnit.getSelectedItemPosition()];
long value = Long.valueOf(etxtTime.getText().toString());
return selectedUnit.toMillis(value);
}
}

View File

@ -70,10 +70,13 @@ 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;
public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
/** Is true if service is running. */
public static boolean isRunning = false;
private SleepTimer sleepTimer;
private static final int NOTIFICATION_ID = 1;
private NotificationCompat.Builder notificationBuilder;
@ -134,6 +137,7 @@ public class PlaybackService extends Service {
public void onDestroy() {
super.onDestroy();
isRunning = false;
disableSleepTimer();
unregisterReceiver(headsetDisconnected);
if (AppConfig.DEBUG)
Log.d(TAG, "Service is about to be destroyed");
@ -454,6 +458,25 @@ public class PlaybackService extends Service {
}
};
public void setSleepTimer(long waitingTime) {
if (sleepTimer != null) {
sleepTimer.interrupt();
}
sleepTimer = new SleepTimer(waitingTime);
sleepTimer.start();
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
}
public void disableSleepTimer() {
if (sleepTimer != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Disabling sleep timer");
sleepTimer.interrupt();
sleepTimer = null;
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
}
}
/**
* Saves the current position and pauses playback
*
@ -591,6 +614,7 @@ public class PlaybackService extends Service {
}
}
@SuppressLint("NewApi")
private void setupWidgetUpdater() {
if (widgetUpdater == null || widgetUpdater.isCancelled()) {
widgetUpdater = new WidgetUpdateWorker();
@ -608,6 +632,10 @@ public class PlaybackService extends Service {
PlaybackService.this.sendBroadcast(new Intent(
PlayerWidget.FORCE_WIDGET_UPDATE));
}
public boolean sleepTimerActive() {
return sleepTimer == null || sleepTimer.isWaiting();
}
/**
* Pauses playback when the headset is disconnected and the preference is
@ -660,7 +688,7 @@ public class PlaybackService extends Service {
} catch (InterruptedException e) {
if (AppConfig.DEBUG)
Log.d(TAG,
"Thread was interrupted while waiting. Finishing now...");
"Thre¿ad was interrupted while waiting. Finishing now...");
return null;
} catch (IllegalStateException e) {
if (AppConfig.DEBUG)
@ -699,6 +727,44 @@ public class PlaybackService extends Service {
}
/** Sleeps for a given time and then pauses playback. */
class SleepTimer extends Thread {
private static final String TAG = "SleepTimer";
private long waitingTime;
private boolean isWaiting;
public SleepTimer(long waitingTime) {
super();
this.waitingTime = waitingTime;
}
@Override
public void run() {
isWaiting = true;
if (AppConfig.DEBUG)
Log.d(TAG, "Starting");
try {
Thread.sleep(waitingTime);
if (status == PlayerStatus.PLAYING) {
Log.d(TAG, "Pausing playback");
pause(true);
}
} catch (InterruptedException e) {
Log.d(TAG, "Thread was interrupted while waiting");
}
isWaiting = false;
}
public long getWaitingTime() {
return waitingTime;
}
public boolean isWaiting() {
return isWaiting;
}
}
public boolean isPlayingVideo() {
return playingVideo;
}