Skip to content

Commit

Permalink
small improvements/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed May 29, 2019
1 parent da8e18b commit 227e675
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}

Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -224,7 +223,7 @@ class ApiClient private constructor(val context: Context) {
cb: (ArrayList<MediaBrowserCompat.MediaItem>?, 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) }
Expand Down
28 changes: 21 additions & 7 deletions app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 227e675

Please sign in to comment.