Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
package org.openhab.habdroid.core.connection

import android.util.Log
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.json.JSONException
import org.json.JSONObject
import org.openhab.habdroid.core.connection.AbstractConnection.Companion.TAG
import org.openhab.habdroid.util.HttpClient

class CloudConnection internal constructor(baseConnection: AbstractConnection, val messagingSenderId: String) :
DefaultConnection(baseConnection, Connection.TYPE_CLOUD) {
class CloudConnection internal constructor(
baseConnection: AbstractConnection,
val messagingSenderId: String,
val proxyUrl: HttpUrl
) : DefaultConnection(baseConnection, Connection.TYPE_CLOUD) {

override fun equals(other: Any?) = super.equals(other) && other is CloudConnection
}
Expand Down Expand Up @@ -49,5 +54,17 @@ suspend fun AbstractConnection.toCloudConnection(): CloudConnection {
throw NotACloudServerException()
}

return CloudConnection(this, senderId)
val proxyUrl = try {
val bar = httpClient.get("api/v1/proxyurl").asText()
val json = JSONObject(bar.response)
json.getString("url").toHttpUrl()
} catch (e: HttpClient.HttpException) {
Log.d(TAG, "Error getting proxy URL from cloud server, falling back to base URL", e)
httpClient.buildUrl("/")
} catch (e: JSONException) {
Log.i(TAG, "Error parsing proxy URL endpoint response", e)
throw NotACloudServerException()
}

return CloudConnection(this, senderId, proxyUrl)
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ class ConnectionFactory internal constructor(
val cloud: CloudConnectionResult?,
val hasLocal: Boolean,
val hasRemote: Boolean
)
) {
val usableConnection get() = when {
conn?.connection == null -> null
conn.connection.connectionType != Connection.TYPE_REMOTE -> conn.connection
cloud?.connection == null -> conn.connection
else -> cloud.connection
}
}

private data class StateHolder(
val intermediate: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.CloudConnection
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.util.PrefKeys
import org.openhab.habdroid.util.getActiveServerId
Expand All @@ -43,9 +44,10 @@ import org.openhab.habdroid.util.getStringOrNull
import org.openhab.habdroid.util.isDemoModeEnabled
import org.openhab.habdroid.util.openInBrowser

open class ConnectionWebViewClient(val connection: Connection, private val targetHost: String?) : WebViewClient() {
open class ConnectionWebViewClient(val connection: Connection) : WebViewClient() {
override fun onReceivedHttpAuthRequest(view: WebView, handler: HttpAuthHandler, host: String, realm: String) {
if (host == targetHost) {
val proxyHost = (connection as? CloudConnection)?.proxyUrl?.host
if ((proxyHost != null && host == proxyHost) || host == connection.httpClient.targetHost) {
handler.proceed(connection.username, connection.password)
} else {
super.onReceivedHttpAuthRequest(view, handler, host, realm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.net.toUri
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import okhttp3.HttpUrl
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.util.openInBrowser
Expand All @@ -44,15 +43,15 @@ fun SwipeRefreshLayout.applyColors() {
setColorSchemeColors(*colors)
}

fun WebView.setUpForConnection(connection: Connection, url: HttpUrl) {
fun WebView.setUpForConnection(connection: Connection) {
with(settings) {
domStorageEnabled = true
@SuppressLint("SetJavaScriptEnabled")
javaScriptEnabled = true
mixedContentMode = MIXED_CONTENT_COMPATIBILITY_MODE
}

webViewClient = ConnectionWebViewClient(connection, url.host)
webViewClient = ConnectionWebViewClient(connection)
}

fun ImageView.setupHelpIcon(url: String, contentDescriptionRes: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ class WidgetAdapter(
loadUrl(ConnectionWebViewClient.EMPTY_PAGE)

if (url != null) {
setUpForConnection(connection, url)
setUpForConnection(connection)
loadUrl(url.toString())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,18 @@ abstract class AbstractWebViewFragment :
fun canGoBack(): Boolean = webView?.canGoBack() == true

private fun loadWebsite(urlToLoad: String = this.urlToLoad) {
val conn = requireContext().getConnectionFactory().currentActive?.conn?.connection
val conn = requireContext().getConnectionFactory().currentActive?.usableConnection
if (conn == null) {
updateViewVisibility(true, null)
return
}
updateViewVisibility(false, 0)

val webView = webView ?: return
val url = modifyUrl(conn.httpClient.buildUrl(urlToLoad))
val url = buildUrl(conn, urlToLoad)

webView.setUpForConnection(conn, url)
Log.d(TAG, "Loading web page $url")
webView.setUpForConnection(conn)
webView.setBackgroundColor(Color.TRANSPARENT)

val jsInterface = if (ShortcutManagerCompat.isRequestPinShortcutSupported(requireContext())) {
Expand All @@ -348,7 +349,7 @@ abstract class AbstractWebViewFragment :
}
webView.addJavascriptInterface(jsInterface, "OHApp")

webView.webViewClient = object : ConnectionWebViewClient(conn, url.host) {
webView.webViewClient = object : ConnectionWebViewClient(conn) {
private fun handleError(url: Uri) {
if (url.path == pathForError) {
updateViewVisibility(true, null)
Expand Down Expand Up @@ -380,7 +381,7 @@ abstract class AbstractWebViewFragment :
webView.loadUrl(url.toString())
}

open fun modifyUrl(orig: HttpUrl): HttpUrl = orig
open fun buildUrl(connection: Connection, url: String): HttpUrl = connection.httpClient.buildUrl(url)

/**
* Change the visibility of the progress and error indicators and the WebView.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.util.Log
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.ui.MainActivity
import org.openhab.habdroid.util.loadActiveServerConfig

Expand All @@ -29,20 +30,15 @@ class FrontailWebViewFragment : AbstractWebViewFragment() {
override val shortcutIcon = R.mipmap.ic_shortcut_frontail
override val shortcutAction = MainActivity.ACTION_FRONTAIL_SELECTED

override fun modifyUrl(orig: HttpUrl): HttpUrl {
override fun buildUrl(connection: Connection, url: String): HttpUrl {
val connectionUrl = connection.httpClient.buildUrl(url)
val frontailUrl = context?.loadActiveServerConfig()?.frontailUrl?.toHttpUrlOrNull()

val builder = orig.newBuilder()
return connectionUrl.newBuilder()
.scheme(frontailUrl?.scheme ?: "http")
.port(frontailUrl?.port ?: 9001)

if (frontailUrl != null) {
builder.host(frontailUrl.host)
}

return builder.build().also {
Log.d(TAG, "Use url '$it'")
}
.host(frontailUrl?.host ?: connectionUrl.host)
.build()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package org.openhab.habdroid.ui.activity

import okhttp3.HttpUrl
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.CloudConnection
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.ui.MainActivity
import org.openhab.habdroid.util.loadActiveServerConfig

Expand All @@ -27,19 +29,21 @@ class MainUiWebViewFragment : AbstractWebViewFragment() {
override val shortcutIcon = R.mipmap.ic_shortcut_main_ui
override val shortcutAction = MainActivity.ACTION_MAIN_UI_SELECTED

override fun modifyUrl(orig: HttpUrl): HttpUrl {
var modified = orig
if (orig.host == "myopenhab.org") {
modified = modified.newBuilder()
.host("home.myopenhab.org")
.build()
override fun buildUrl(connection: Connection, url: String): HttpUrl {
val connectionUrl = connection.httpClient.buildUrl(url)
val urlBuilder = connectionUrl.newBuilder()
if (connection is CloudConnection && connectionUrl.host == connection.httpClient.targetHost) {
urlBuilder
.scheme(connection.proxyUrl.scheme)
.host(connection.proxyUrl.host)
.port(connection.proxyUrl.port)
}

val mainUiStartPage = context?.loadActiveServerConfig()?.mainUiStartPage
if (!mainUiStartPage.isNullOrEmpty()) {
modified = modified.newBuilder()
.encodedPath(mainUiStartPage)
.build()
urlBuilder.encodedPath(mainUiStartPage)
}
return modified

return urlBuilder.build()
}
}
Loading