Merge pull request #3167 from ByteHamster/threading

Fixed threading bugs in Sonic
This commit is contained in:
H. Lehmann 2019-05-07 15:47:40 +02:00 committed by GitHub
commit 86129a9686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 24 deletions

View File

@ -1088,25 +1088,17 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
mp -> genericSeekCompleteListener();
private void genericSeekCompleteListener() {
Runnable r = () -> {
Log.d(TAG, "genericSeekCompleteListener");
if(seekLatch != null) {
seekLatch.countDown();
}
playerLock.lock();
if (playerStatus == PlayerStatus.PLAYING) {
callback.onPlaybackStart(media, getPosition());
}
if (playerStatus == PlayerStatus.SEEKING) {
setPlayerStatus(statusBeforeSeeking, media, getPosition());
}
playerLock.unlock();
};
if (useCallerThread) {
r.run();
} else {
new Thread(r).start();
Log.d(TAG, "genericSeekCompleteListener");
if (seekLatch != null) {
seekLatch.countDown();
}
playerLock.lock();
if (playerStatus == PlayerStatus.PLAYING) {
callback.onPlaybackStart(media, getPosition());
}
if (playerStatus == PlayerStatus.SEEKING) {
setPlayerStatus(statusBeforeSeeking, media, getPosition());
}
playerLock.unlock();
}
}

View File

@ -16,6 +16,7 @@ import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.greenrobot.event.EventBus;
@ -320,7 +321,7 @@ public class PlaybackServiceTaskManager {
private final boolean shakeToReset;
private final boolean vibrate;
private ShakeListener shakeListener;
private Handler handler;
private final Handler handler;
public SleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) {
super();
@ -328,7 +329,21 @@ public class PlaybackServiceTaskManager {
this.timeLeft = waitingTime;
this.shakeToReset = shakeToReset;
this.vibrate = vibrate;
this.handler = new Handler(); // Use the same thread for callbacks (ExoPlayer)
if (UserPreferences.useExoplayer() && Looper.myLooper() == Looper.getMainLooper()) {
// Run callbacks in main thread so they can call ExoPlayer methods themselves
this.handler = new Handler();
} else {
this.handler = null;
}
}
private void postCallback(Runnable r) {
if (handler == null) {
r.run();
} else {
handler.post(r);
}
}
@Override
@ -354,7 +369,7 @@ public class PlaybackServiceTaskManager {
if(shakeListener == null && shakeToReset) {
shakeListener = new ShakeListener(context, this);
}
handler.post(callback::onSleepTimerAlmostExpired);
postCallback(callback::onSleepTimerAlmostExpired);
notifiedAlmostExpired = true;
}
if (timeLeft <= 0) {
@ -364,7 +379,7 @@ public class PlaybackServiceTaskManager {
shakeListener = null;
}
if (!Thread.currentThread().isInterrupted()) {
handler.post(callback::onSleepTimerExpired);
postCallback(callback::onSleepTimerExpired);
} else {
Log.d(TAG, "Sleep timer interrupted");
}
@ -382,7 +397,7 @@ public class PlaybackServiceTaskManager {
}
public void onShake() {
handler.post(() -> {
postCallback(() -> {
setSleepTimer(waitingTime, shakeToReset, vibrate);
callback.onSleepTimerReset();
});