From 27ed2416c9b16f43dba24233f4823a4d97f7d3fd Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 25 Aug 2014 15:28:34 -0700 Subject: Work around for KitKat returning wrong position On KitKat after a few songs have played, songs start playing as song position + constant arbitrary amount. This situation gets worse as playback continues on. I am working around this by getting the starting position and subtracting the cachedPosition from it. This should be a pretty close approximation, though not exact. --- src/github/daneren2005/dsub/service/DownloadService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index 54df07ac..f7ae6cd0 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -147,6 +147,10 @@ public class DownloadService extends Service { private boolean autoPlayStart = false; private MediaRouteManager mediaRouter; + + // Variables to manage getCurrentPosition sometimes starting from an arbitrary non-zero number + private long subtractNextPosition = 0; + private int subtractPosition = 0; @Override public void onCreate() { @@ -818,6 +822,7 @@ public class DownloadService extends Service { Util.broadcastPlaybackStatusChange(this, currentPlaying.getSong(), PlayerState.PREPARED); // Swap the media players since nextMediaPlayer is ready to play + subtractPosition = 0; if(start) { nextMediaPlayer.start(); } else if(!nextMediaPlayer.isPlaying()) { @@ -825,6 +830,9 @@ public class DownloadService extends Service { nextMediaPlayer.start(); } else { Log.i(TAG, "nextMediaPlayer already playing"); + + // Next time the cachedPosition is updated, use that as position 0 + subtractNextPosition = System.currentTimeMillis(); } MediaPlayer tmp = mediaPlayer; mediaPlayer = nextMediaPlayer; @@ -1027,7 +1035,7 @@ public class DownloadService extends Service { if (remoteState != RemoteControlState.LOCAL) { return remoteController.getRemotePosition() * 1000; } else { - return cachedPosition; + return cachedPosition - subtractPosition; } } catch (Exception x) { handleError(x); @@ -1124,6 +1132,11 @@ public class DownloadService extends Service { try { if(mediaPlayer != null && playerState == STARTED) { cachedPosition = mediaPlayer.getCurrentPosition(); + if(subtractNextPosition > 0) { + // Subtraction amount is current position - how long ago onCompletionListener was called + subtractPosition = cachedPosition - (System.currentTimeMillis() - subtractNextPosition); + subtractNextPosition = 0; + } } Thread.sleep(1000L); } -- cgit v1.2.3 From 9b71ca2a9d2d013662400b1613a06440238c49b1 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 25 Aug 2014 20:53:59 -0700 Subject: Make sure position never goes below 0 --- src/github/daneren2005/dsub/service/DownloadService.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index f7ae6cd0..3cc152fe 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -1134,7 +1134,10 @@ public class DownloadService extends Service { cachedPosition = mediaPlayer.getCurrentPosition(); if(subtractNextPosition > 0) { // Subtraction amount is current position - how long ago onCompletionListener was called - subtractPosition = cachedPosition - (System.currentTimeMillis() - subtractNextPosition); + subtractPosition = cachedPosition - (int) (System.currentTimeMillis() - subtractNextPosition); + if(subtractPosition < 0) { + subtractPosition = 0; + } subtractNextPosition = 0; } } -- cgit v1.2.3 From 09b7d39b6afe5a72b158ed28f4a38f7bddb58f05 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 25 Aug 2014 21:50:21 -0700 Subject: Fix various ways the subtractPosition could screw things up --- src/github/daneren2005/dsub/service/DownloadService.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index 3cc152fe..7228c050 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -873,6 +873,7 @@ public class DownloadService extends Service { } else { mediaPlayer.seekTo(position); cachedPosition = position; + subtractPosition = 0; } } catch (Exception x) { handleError(x); @@ -1035,7 +1036,7 @@ public class DownloadService extends Service { if (remoteState != RemoteControlState.LOCAL) { return remoteController.getRemotePosition() * 1000; } else { - return cachedPosition - subtractPosition; + return Math.max(0, cachedPosition - subtractPosition); } } catch (Exception x) { handleError(x); @@ -1367,6 +1368,7 @@ public class DownloadService extends Service { boolean isPartial = file.equals(downloadFile.getPartialFile()); downloadFile.updateModificationDate(); + subtractPosition = 0; mediaPlayer.setOnCompletionListener(null); mediaPlayer.reset(); setPlayerState(IDLE); @@ -1491,7 +1493,7 @@ public class DownloadService extends Service { mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { Log.w(TAG, "Error on playing file " + "(" + what + ", " + extra + "): " + downloadFile); - int pos = cachedPosition; + int pos = getPlayerPosition(); reset(); if (!isPartial || (downloadFile.isWorkDone() && (Math.abs(duration - pos) < 10000))) { playNext(); @@ -1514,7 +1516,7 @@ public class DownloadService extends Service { setPlayerStateCompleted(); - int pos = cachedPosition; + int pos = getPlayerPosition(); Log.i(TAG, "Ending position " + pos + " of " + duration); if (!isPartial || (downloadFile.isWorkDone() && (Math.abs(duration - pos) < 10000)) || nextSetup) { playNext(); @@ -1805,7 +1807,7 @@ public class DownloadService extends Service { private boolean isPastCutoff() { int duration = getPlayerDuration(); int cutoffPoint = (int) (duration * DELETE_CUTOFF); - return duration > 0 && cachedPosition > cutoffPoint; + return duration > 0 && getPlayerPosition() > cutoffPoint; } private void clearCurrentBookmark() { -- cgit v1.2.3 From d293d6cfa20d8c435c7a2ac1ad073e014d081349 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 26 Aug 2014 20:53:24 -0700 Subject: Add SNI Support (courtesy of jimyx17) --- .../dsub/service/ssl/SSLSocketFactory.java | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/ssl/SSLSocketFactory.java b/src/github/daneren2005/dsub/service/ssl/SSLSocketFactory.java index 2ffed048..2f11730a 100644 --- a/src/github/daneren2005/dsub/service/ssl/SSLSocketFactory.java +++ b/src/github/daneren2005/dsub/service/ssl/SSLSocketFactory.java @@ -27,6 +27,8 @@ package github.daneren2005.dsub.service.ssl; +import android.util.Log; + import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.scheme.HostNameResolver; import org.apache.http.conn.scheme.LayeredSocketFactory; @@ -140,7 +142,7 @@ import java.security.UnrecoverableKeyException; * @since 4.0 */ public class SSLSocketFactory implements LayeredSocketFactory { - + private static final String TAG = SSLSocketFactory.class.getSimpleName(); public static final String TLS = "TLS"; public static final String SSL = "SSL"; public static final String SSLV2 = "SSLv2"; @@ -341,13 +343,17 @@ public class SSLSocketFactory implements LayeredSocketFactory { @SuppressWarnings("cast") public Socket createSocket(final HttpParams params) throws IOException { // the cast makes sure that the factory is working as expected - return (SSLSocket) this.socketfactory.createSocket(); + SSLSocket sslsocket = (SSLSocket) this.socketfactory.createSocket(); + sslsocket.setEnabledProtocols(sslsocket.getSupportedProtocols()); + return sslsocket; } @SuppressWarnings("cast") public Socket createSocket() throws IOException { // the cast makes sure that the factory is working as expected - return (SSLSocket) this.socketfactory.createSocket(); + SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(); + sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols()); + return sslSocket; } /** @@ -370,6 +376,7 @@ public class SSLSocketFactory implements LayeredSocketFactory { sslsock.bind(localAddress); } + setHostName(sslsock, remoteAddress.getHostName()); int connTimeout = HttpConnectionParams.getConnectionTimeout(params); int soTimeout = HttpConnectionParams.getSoTimeout(params); @@ -437,6 +444,7 @@ public class SSLSocketFactory implements LayeredSocketFactory { port, autoClose ); + sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols()); if (this.hostnameVerifier != null) { this.hostnameVerifier.verify(host, sslSocket); } @@ -491,7 +499,18 @@ public class SSLSocketFactory implements LayeredSocketFactory { final Socket socket, final String host, int port, boolean autoClose) throws IOException, UnknownHostException { - return createLayeredSocket(socket, host, port, autoClose); + SSLSocket sslSocket = (SSLSocket) this.socketfactory.createSocket(socket, host, port, autoClose); + sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols()); + setHostName(sslSocket, host); + return sslSocket; } + private void setHostName(SSLSocket sslsock, String hostname){ + try { + java.lang.reflect.Method setHostnameMethod = sslsock.getClass().getMethod("setHostname", String.class); + setHostnameMethod.invoke(sslsock, hostname); + } catch (Exception e) { + Log.w(TAG, "SNI not useable", e); + } + } } -- cgit v1.2.3 From 73fc8c68dc2729a9e1ee9b6b1d5a31efa8eca444 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Thu, 4 Sep 2014 20:45:16 -0700 Subject: Reset subtract position on reset --- src/github/daneren2005/dsub/service/DownloadService.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index 480bee7a..43eac93d 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -989,6 +989,7 @@ public class DownloadService extends Service { mediaPlayer.setOnErrorListener(null); mediaPlayer.setOnCompletionListener(null); mediaPlayer.reset(); + subtractPosition = 0; } catch (Exception x) { handleError(x); } -- cgit v1.2.3