Skip to content

Commit

Permalink
Implement auto discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosOlivo authored and Maxr1998 committed Sep 20, 2020
1 parent b213910 commit df24a1d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="org.jellyfin.mobile">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Expand Down
47 changes: 45 additions & 2 deletions app/src/main/java/org/jellyfin/mobile/webapp/ConnectionHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jellyfin.mobile.webapp

import android.app.AlertDialog
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
Expand All @@ -15,10 +16,16 @@ import androidx.core.view.doOnNextLayout
import androidx.core.view.isVisible
import androidx.core.view.postDelayed
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.jellyfin.apiclient.Jellyfin
import org.jellyfin.apiclient.discovery.DiscoveryServerInfo
import org.jellyfin.apiclient.discovery.ServerDiscovery
import org.jellyfin.apiclient.interaction.ApiClient
import org.jellyfin.apiclient.model.system.PublicSystemInfo
import org.jellyfin.mobile.AppPreferences
Expand Down Expand Up @@ -48,6 +55,9 @@ class ConnectionHelper(private val activity: MainActivity) : KoinComponent {
private val hostInput: EditText get() = connectServerBinding.hostInput
private val connectionErrorText: TextView get() = connectServerBinding.connectionErrorText
private val connectButton: Button get() = connectServerBinding.connectButton
private val chooseServerButton: Button get() = connectServerBinding.chooseServerButton

private val serverList = ArrayList<DiscoveryServerInfo>(ServerDiscovery.DISCOVERY_MAX_SERVERS)

fun initialize() {
appPreferences.instanceUrl?.toHttpUrlOrNull().also { url ->
Expand Down Expand Up @@ -100,6 +110,9 @@ class ConnectionHelper(private val activity: MainActivity) : KoinComponent {
connectButton.setOnClickListener {
connect()
}
chooseServerButton.setOnClickListener {
chooseServer()
}

// Show keyboard
serverSetupLayout.doOnNextLayout {
Expand All @@ -108,36 +121,66 @@ class ConnectionHelper(private val activity: MainActivity) : KoinComponent {
activity.getSystemService<InputMethodManager>()?.showSoftInput(hostInput, InputMethodManager.SHOW_IMPLICIT)
}
}

discoverServers()
}

private fun connect() {
private fun connect(enteredUrl: String = hostInput.text.toString()) {
hostInput.isEnabled = false
connectButton.isEnabled = false
clearConnectionError()
activity.lifecycleScope.launch {
val httpUrl = checkServerUrlAndConnection(hostInput.text.toString())
val httpUrl = checkServerUrlAndConnection(enteredUrl)
if (httpUrl != null) {
appPreferences.instanceUrl = httpUrl.toString()
webView.clearHistory()
webView.loadUrl("about:blank")
rootView.removeView(serverSetupLayout)
webView.isVisible = true
webView.loadUrl(httpUrl.toString())
clearServerList()
}
hostInput.isEnabled = true
connectButton.isEnabled = true
}
}

private fun discoverServers() {
activity.lifecycleScope.launch {
withContext(Dispatchers.IO) {
jellyfin.discovery.discover().onEach {
serverList.add(it)
}.collect()
}
chooseServerButton.isVisible = serverList.isNotEmpty()
}
}

private fun chooseServer() {
AlertDialog.Builder(activity).apply {
setTitle(R.string.available_servers_title)
setItems(serverList.map { "${it.name}\n${it.address}" }.toTypedArray()) { _, index ->
connect(serverList[index].address)
}
}.show()
}

private fun showConnectionError(@StringRes errorString: Int = R.string.connection_error_cannot_connect) {
connectionErrorText.setText(errorString)
connectionErrorText.isVisible = true
clearServerList()
discoverServers()
}

private fun clearConnectionError() {
connectionErrorText.isVisible = false
}

private fun clearServerList() {
serverList.clear()
chooseServerButton.isVisible = false
}

private suspend fun checkServerUrlAndConnection(enteredUrl: String): HttpUrl? {
val normalizedUrl = enteredUrl.run {
if (lastOrNull() == '/') this
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/connect_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/connection_error_text" />

<Button
android:id="@+id/choose_server_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/choose_server_button_text"
android:textColor="@android:color/white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/connect_button"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="connection_error_invalid_format">Host has an invalid format</string>
<string name="connection_error_cannot_connect">Connection cannot be established.\nPlease check the hostname and your network connection.</string>
<string name="connect_button_text">Connect</string>
<string name="choose_server_button_text">Choose server</string>
<string name="available_servers_title">Available servers</string>

<string name="battery_optimizations_message">Please disable battery optimizations for media playback while the screen is off.</string>
<string name="network_title">Allowed Network Types</string>
Expand Down

0 comments on commit df24a1d

Please sign in to comment.