|
|
@ -29,6 +29,7 @@ import android.content.SharedPreferences |
|
|
import android.media.AudioManager |
|
|
import android.media.AudioManager |
|
|
import android.support.v4.media.session.PlaybackStateCompat |
|
|
import android.support.v4.media.session.PlaybackStateCompat |
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
|
|
|
import androidx.annotation.MainThread |
|
|
import com.squareup.moshi.Moshi |
|
|
import com.squareup.moshi.Moshi |
|
|
import io.ktor.server.routing.Routing |
|
|
import io.ktor.server.routing.Routing |
|
|
import io.ktor.server.websocket.WebSocketServerSession |
|
|
import io.ktor.server.websocket.WebSocketServerSession |
|
|
@ -84,7 +85,9 @@ object RemoteAccessWebSockets { |
|
|
send(Frame.Text(convertToJson(RemoteAccessServer.WebSocketAuthorization("forbidden", initialMessage = message)))) |
|
|
send(Frame.Text(convertToJson(RemoteAccessServer.WebSocketAuthorization("forbidden", initialMessage = message)))) |
|
|
} else { |
|
|
} else { |
|
|
val service = RemoteAccessServer.getInstance(context).service |
|
|
val service = RemoteAccessServer.getInstance(context).service |
|
|
manageIncomingMessages(incomingMessage, settings, service, context) |
|
|
withContext(Dispatchers.Main) { |
|
|
|
|
|
manageIncomingMessages(incomingMessage, settings, service, context) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
Log.e(TAG, e.message, e) |
|
|
Log.e(TAG, e.message, e) |
|
|
@ -98,7 +101,9 @@ object RemoteAccessWebSockets { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Manage incoming messages from the client, either from websockets or long polling |
|
|
* Manage incoming messages from the client, either from websockets or long polling. |
|
|
|
|
|
* |
|
|
|
|
|
* Run this handler on the main thread to simplify calls to PlaybackService. |
|
|
* |
|
|
* |
|
|
* @param incomingMessage the incoming message |
|
|
* @param incomingMessage the incoming message |
|
|
* @param settings the shared preferences |
|
|
* @param settings the shared preferences |
|
|
@ -106,7 +111,8 @@ object RemoteAccessWebSockets { |
|
|
* @param context the context |
|
|
* @param context the context |
|
|
* @return true if the message has been handled, false if playback control is not allowed |
|
|
* @return true if the message has been handled, false if playback control is not allowed |
|
|
*/ |
|
|
*/ |
|
|
fun manageIncomingMessages( |
|
|
@MainThread |
|
|
|
|
|
suspend fun manageIncomingMessages( |
|
|
incomingMessage: WSIncomingMessage, |
|
|
incomingMessage: WSIncomingMessage, |
|
|
settings: SharedPreferences, |
|
|
settings: SharedPreferences, |
|
|
service: PlaybackService?, |
|
|
service: PlaybackService?, |
|
|
@ -149,10 +155,8 @@ object RemoteAccessWebSockets { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
GET_VOLUME -> { |
|
|
GET_VOLUME -> { |
|
|
AppScope.launch { |
|
|
val message = Frame.Text(getVolumeMessage(context, service)) |
|
|
val message = Frame.Text(getVolumeMessage(context, service)) |
|
|
webSocketSessions.forEach { (_, session) -> session.send(message) } |
|
|
webSocketSessions.forEach { (_, session) -> session.send(message) } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
SET_VOLUME -> { |
|
|
SET_VOLUME -> { |
|
|
incomingMessage.id?.let { volume -> |
|
|
incomingMessage.id?.let { volume -> |
|
|
@ -167,49 +171,33 @@ object RemoteAccessWebSockets { |
|
|
SPEED -> incomingMessage.floatValue?.let { speed -> service?.setRate(speed, true) } |
|
|
SPEED -> incomingMessage.floatValue?.let { speed -> service?.setRate(speed, true) } |
|
|
SLEEP_TIMER -> { |
|
|
SLEEP_TIMER -> { |
|
|
incomingMessage.longValue?.let { sleepTimerEnd -> |
|
|
incomingMessage.longValue?.let { sleepTimerEnd -> |
|
|
AppScope.launch(Dispatchers.Main) { |
|
|
val sleepTime = Calendar.getInstance() |
|
|
val sleepTime = Calendar.getInstance() |
|
|
sleepTime.timeInMillis += sleepTimerEnd |
|
|
sleepTime.timeInMillis += sleepTimerEnd |
|
|
sleepTime.set(Calendar.SECOND, 0) |
|
|
sleepTime.set(Calendar.SECOND, 0) |
|
|
service?.setSleepTimer(sleepTime) |
|
|
service?.setSleepTimer(sleepTime) |
|
|
service?.sleepTimerInterval = sleepTimerEnd |
|
|
service?.sleepTimerInterval = sleepTimerEnd |
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { sendToAll(it) } |
|
|
} |
|
|
|
|
|
AppScope.launch { |
|
|
|
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { nowPlaying -> |
|
|
|
|
|
AppScope.launch { sendToAll(nowPlaying) } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
SLEEP_TIMER_WAIT -> { |
|
|
SLEEP_TIMER_WAIT -> { |
|
|
incomingMessage.stringValue?.let { waitForMediaEnd -> |
|
|
incomingMessage.stringValue?.let { waitForMediaEnd -> |
|
|
service?.waitForMediaEnd = (waitForMediaEnd == "true") |
|
|
service?.waitForMediaEnd = (waitForMediaEnd == "true") |
|
|
AppScope.launch { |
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { sendToAll(it) } |
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { nowPlaying -> |
|
|
|
|
|
AppScope.launch { sendToAll(nowPlaying) } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
SLEEP_TIMER_RESET -> { |
|
|
SLEEP_TIMER_RESET -> { |
|
|
incomingMessage.stringValue?.let { resetOnInteraction -> |
|
|
incomingMessage.stringValue?.let { resetOnInteraction -> |
|
|
service?.resetOnInteraction = (resetOnInteraction == "true") |
|
|
service?.resetOnInteraction = (resetOnInteraction == "true") |
|
|
AppScope.launch { |
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { sendToAll(it) } |
|
|
RemoteAccessServer.getInstance(context).generateNowPlaying()?.let { nowPlaying -> |
|
|
|
|
|
AppScope.launch { sendToAll(nowPlaying) } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
ADD_BOOKMARK -> { |
|
|
ADD_BOOKMARK -> { |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
service?.let { |
|
|
service?.let { |
|
|
it.currentMediaWrapper?.let { media -> |
|
|
it.currentMediaWrapper?.let { media -> |
|
|
AppScope.launch { |
|
|
AppScope.launch(Dispatchers.IO) { |
|
|
withContext(Dispatchers.IO) { |
|
|
val bookmark = media.addBookmark(bookmarkTime) |
|
|
val bookmark = media.addBookmark(bookmarkTime) |
|
|
bookmark?.setName(context.getString(R.string.bookmark_default_name, Tools.millisToString(it.getTime()))) |
|
|
bookmark?.setName(context.getString(R.string.bookmark_default_name, Tools.millisToString(it.getTime()))) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -218,10 +206,8 @@ object RemoteAccessWebSockets { |
|
|
DELETE_BOOKMARK -> { |
|
|
DELETE_BOOKMARK -> { |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
service?.currentMediaWrapper?.let { media -> |
|
|
service?.currentMediaWrapper?.let { media -> |
|
|
AppScope.launch { |
|
|
AppScope.launch(Dispatchers.IO) { |
|
|
withContext(Dispatchers.IO) { |
|
|
media.removeBookmark(bookmarkTime) |
|
|
media.removeBookmark(bookmarkTime) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -230,22 +216,14 @@ object RemoteAccessWebSockets { |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
incomingMessage.longValue?.let { bookmarkTime -> |
|
|
incomingMessage.stringValue?.let { bookmarkName -> |
|
|
incomingMessage.stringValue?.let { bookmarkName -> |
|
|
service?.currentMediaWrapper?.let { media -> |
|
|
service?.currentMediaWrapper?.let { media -> |
|
|
AppScope.launch { |
|
|
AppScope.launch(Dispatchers.IO) { |
|
|
withContext(Dispatchers.IO) { |
|
|
media.bookmarks.firstOrNull { it.time == bookmarkTime }?.setName(bookmarkName) |
|
|
media.bookmarks.firstOrNull { it.time == bookmarkTime }?.setName(bookmarkName) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
PLAY_MEDIA -> { |
|
|
PLAY_MEDIA -> incomingMessage.id?.let { index -> service?.playIndex(index) } |
|
|
incomingMessage.id?.let { index -> |
|
|
|
|
|
AppScope.launch(Dispatchers.Main) { |
|
|
|
|
|
service?.playIndex(index) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
DELETE_MEDIA -> incomingMessage.id?.let { index -> service?.remove(index) } |
|
|
DELETE_MEDIA -> incomingMessage.id?.let { index -> service?.remove(index) } |
|
|
MOVE_MEDIA_BOTTOM -> { |
|
|
MOVE_MEDIA_BOTTOM -> { |
|
|
incomingMessage.id?.let { index -> |
|
|
incomingMessage.id?.let { index -> |
|
|
@ -261,13 +239,7 @@ object RemoteAccessWebSockets { |
|
|
service?.moveItem(index, index - 1) |
|
|
service?.moveItem(index, index - 1) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
REMOTE -> { |
|
|
REMOTE -> incomingMessage.stringValue?.let { action -> VideoPlayerActivity.videoRemoteFlow.emit(action) } |
|
|
incomingMessage.stringValue?.let { action -> |
|
|
|
|
|
AppScope.launch { |
|
|
|
|
|
VideoPlayerActivity.videoRemoteFlow.emit(action) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
|