From 227e6755b563019c3bfb67c2841b8eb135876c52 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 29 May 2019 12:07:55 +0200 Subject: [PATCH] small improvements/fixes --- app/build.gradle | 4 +-- .../eu/zderadicka/audioserve/MainActivity.kt | 1 + .../fragments/ControllerFragment.kt | 6 ++-- .../eu/zderadicka/audioserve/net/ApiClient.kt | 5 ++-- .../audioserve/net/PositionClient.kt | 28 ++++++++++++++----- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c00a2b4..81a9c03 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ android { applicationId "eu.zderadicka.audioserve" minSdkVersion 21 targetSdkVersion 28 - versionCode 32 - versionName "0.8.2" + versionCode 33 + versionName "0.8.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt b/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt index 80f6d0b..4093193 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt @@ -569,6 +569,7 @@ class MainActivity : AppCompatActivity(), items, err -> if (err!= null) { Log.e(LOG_TAG, "Error querying position $err") + Toast.makeText(this, "Cann't get remote position: $err",Toast.LENGTH_SHORT).show() } else { checkRemotePositions(items, false,null) } diff --git a/app/src/main/java/eu/zderadicka/audioserve/fragments/ControllerFragment.kt b/app/src/main/java/eu/zderadicka/audioserve/fragments/ControllerFragment.kt index ebf14e6..297c55a 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/fragments/ControllerFragment.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/fragments/ControllerFragment.kt @@ -243,8 +243,8 @@ class ControllerFragment : MediaFragment(), SharedPreferences.OnSharedPreference mediaController?.apply { val mediaId = metadata?.description?.mediaId val mainActivity = activity - if (mediaId != null && mainActivity is MainActivity) { - val api = ApiClient.getInstance(context!!) + val api = ApiClient.getInstance(context!!) + if (mediaId != null && mainActivity is MainActivity && api.isPositionEnabled) { api.queryPositionForMediaId(mediaId, currentPlayTime) { res, err -> if (err != null) { @@ -258,7 +258,7 @@ class ControllerFragment : MediaFragment(), SharedPreferences.OnSharedPreference transportControls?.play() } } else { - Toast.makeText(context, "No more recent remote positions",Toast.LENGTH_SHORT).show() + //Toast.makeText(context, "No more recent remote positions",Toast.LENGTH_SHORT).show() transportControls?.play() } diff --git a/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt b/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt index 4e27989..0b10890 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt @@ -4,7 +4,6 @@ import android.content.Context import android.content.SharedPreferences import android.graphics.Bitmap import android.net.* -import android.net.Network import android.os.Build import android.os.Handler import android.os.Looper @@ -208,7 +207,7 @@ class ApiClient private constructor(val context: Context) { fun queryLastPosition(cb: (MediaBrowserCompat.MediaItem?, PositionClientError?) -> Unit) { positionClient.apply { if (this == null) { - cb(null, PositionClientError.NotReady) + cb(null, PositionClientError.NotConnected) } else { sendQuery(null) { res, err -> cb(if (res?.last != null) positionToMediaItem(res.last) else null, err) @@ -224,7 +223,7 @@ class ApiClient private constructor(val context: Context) { cb: (ArrayList?, PositionClientError?) -> Unit) { positionClient.apply { if (this == null) { - cb(ArrayList(), PositionClientError.NotReady) + cb(ArrayList(), PositionClientError.NotConnected) } else { val folderPath = mediaId?.let { mediaIdToFolderPath(it) } ?: folderId?.let { folderIdToFolderPath(folderId) } diff --git a/app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt b/app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt index f43e010..90915b9 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt @@ -15,9 +15,9 @@ private const val NORMAL_CLOSE = 1000 private const val TIMEOUT_DURATION = 2_000L enum class PositionClientError { - Socket, + SocketError, CanceledByNext, - NotReady, + NotConnected, Timeout, InvalidResponse } @@ -41,12 +41,26 @@ class PositionClient(val serverUrl:String, val token:String, val group: String?) private val timeout = object: Runnable { override fun run() { - finishPendingQuery(PositionClientError.Timeout) + pendingQuery?.apply { + if (errors>0) { + finishPendingQuery(PositionClientError.Timeout) + } else { + // try to open the websocketsocket + errors+=1; + if (! closed) { + close(stayClosed = false) + open() + } else { + finishPendingQuery(PositionClientError.Timeout) + } + } + } + } } - private val reopen = object : Runnable { + private val reopen = object : Runnable { override fun run() { open() } @@ -91,7 +105,7 @@ class PositionClient(val serverUrl:String, val token:String, val group: String?) finishPendingQuery(PositionClientError.CanceledByNext) handler.removeCallbacks(timeout) if (socket == null) { - cb(null, PositionClientError.NotReady) + cb(null, PositionClientError.NotConnected) } else { socket?.apply { pendingQuery = PendingQuery(cb, folderPath, System.currentTimeMillis()) @@ -126,9 +140,9 @@ class PositionClient(val serverUrl:String, val token:String, val group: String?) } - fun close() { + fun close(stayClosed:Boolean = true) { handler.removeCallbacks(reopen) - closed = true + closed = stayClosed socket?.close(NORMAL_CLOSE, null) }