diff options
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/service')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/service/ChromeCastController.java | 3 | ||||
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/service/DownloadService.java | 76 |
2 files changed, 51 insertions, 28 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/service/ChromeCastController.java b/app/src/main/java/github/daneren2005/dsub/service/ChromeCastController.java index 79312f44..670ea7d2 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/ChromeCastController.java +++ b/app/src/main/java/github/daneren2005/dsub/service/ChromeCastController.java @@ -490,8 +490,7 @@ public class ChromeCastController extends RemoteController { } } else if (mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_ERROR) { Log.e(TAG, "Idle due to unknown error"); - downloadService.setPlayerState(PlayerState.COMPLETED); - downloadService.next(); + downloadService.onSongCompleted(); } else { Log.w(TAG, "Idle reason: " + mediaStatus.getIdleReason()); downloadService.setPlayerState(PlayerState.IDLE); diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index a271c020..7c80ca56 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -77,6 +77,7 @@ import android.content.SharedPreferences; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.audiofx.AudioEffect; +import android.net.wifi.WifiManager; import android.os.Build; import android.os.Handler; import android.os.IBinder; @@ -154,6 +155,7 @@ public class DownloadService extends Service { private String suggestedPlaylistName; private String suggestedPlaylistId; private PowerManager.WakeLock wakeLock; + private WifiManager.WifiLock wifiLock; private boolean keepScreenOn; private int cachedPosition = 0; private boolean downloadOngoing = false; @@ -257,6 +259,9 @@ public class DownloadService extends Service { wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName()); wakeLock.setReferenceCounted(false); + WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); + wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "downloadServiceLock"); + try { timerDuration = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION, "5")); } catch(Throwable e) { @@ -1189,12 +1194,12 @@ public class DownloadService extends Service { } public void onSongCompleted() { - setPlayerState(PlayerState.COMPLETED); + setPlayerStateCompleted(); postPlayCleanup(); play(getNextPlayingIndex()); } public void onNextStarted(DownloadFile nextPlaying) { - setPlayerState(COMPLETED); + setPlayerStateCompleted(); postPlayCleanup(); setCurrentPlaying(nextPlaying, true); setPlayerState(STARTED); @@ -1402,15 +1407,6 @@ public class DownloadService extends Service { scrobbler.scrobble(this, currentPlaying, true); } - if(playerState == STARTED && positionCache == null && remoteState == LOCAL) { - positionCache = new LocalPositionCache(); - Thread thread = new Thread(positionCache, "PositionCache"); - thread.start(); - } else if(playerState != STARTED && positionCache != null) { - positionCache.stop(); - positionCache = null; - } - if(playerState == STARTED && positionCache == null) { if(remoteState == LOCAL) { positionCache = new LocalPositionCache(); @@ -1424,6 +1420,17 @@ public class DownloadService extends Service { positionCache = null; } + + if(remoteState != LOCAL) { + if(playerState == STARTED) { + if (!wifiLock.isHeld()) { + wifiLock.acquire(); + } + } else if(playerState == PAUSED && wifiLock.isHeld()) { + wifiLock.release(); + } + } + if(remoteController != null && remoteController.isNextSupported()) { if(playerState == PREPARING || playerState == IDLE) { nextPlayerState = IDLE; @@ -1433,6 +1440,21 @@ public class DownloadService extends Service { onStateUpdate(); } + public void setPlayerStateCompleted() { + // Acquire a temporary wakelock + acquireWakelock(); + + Log.i(TAG, this.playerState.name() + " -> " + PlayerState.COMPLETED + " (" + currentPlaying + ")"); + this.playerState = PlayerState.COMPLETED; + if(positionCache != null) { + positionCache.stop(); + positionCache = null; + } + scrobbler.scrobble(this, currentPlaying, true); + + onStateUpdate(); + } + private class PositionCache implements Runnable { boolean isRunning = true; @@ -1499,16 +1521,6 @@ public class DownloadService extends Service { } } - private void setPlayerStateCompleted() { - Log.i(TAG, this.playerState.name() + " -> " + PlayerState.COMPLETED + " (" + currentPlaying + ")"); - this.playerState = PlayerState.COMPLETED; - if(positionCache != null) { - positionCache.stop(); - positionCache = null; - } - scrobbler.scrobble(this, currentPlaying, true); - } - public synchronized void setNextPlayerState(PlayerState playerState) { Log.i(TAG, "Next: " + this.nextPlayerState.name() + " -> " + playerState.name() + " (" + nextPlaying + ")"); this.nextPlayerState = playerState; @@ -1650,9 +1662,20 @@ public class DownloadService extends Service { remoteController = (RemoteController) ref; break; case LOCAL: default: + if(wifiLock.isHeld()) { + wifiLock.release(); + } break; } + if(remoteState != LOCAL) { + if(!wifiLock.isHeld()) { + wifiLock.acquire(); + } + } else if(wifiLock.isHeld()) { + wifiLock.release(); + } + if(remoteController != null) { remoteController.create(isPlaying, position / 1000); } else { @@ -1928,11 +1951,6 @@ public class DownloadService extends Service { mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { - // Acquire a temporary wakelock, since when we return from - // this callback the MediaPlayer will release its wakelock - // and allow the device to go to sleep. - wakeLock.acquire(30000); - setPlayerStateCompleted(); int pos = getPlayerPosition(); @@ -2588,6 +2606,12 @@ public class DownloadService extends Service { } }); } + public void acquireWakelock() { + acquireWakelock(30000); + } + public void acquireWakelock(int ms) { + wakeLock.acquire(ms); + } public void addOnSongChangedListener(OnSongChangedListener listener) { addOnSongChangedListener(listener, false); |