diff --git a/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt b/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt index b30bf3ccc9..b7971950d2 100644 --- a/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt +++ b/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt @@ -281,6 +281,13 @@ class ItemsControlsProviderService : ControlsProviderService() { } Pair(intent, item.hashCode()) } + item.linkToMore != null -> { + val intent = Intent(context, MainActivity::class.java).apply { + putExtra(MainActivity.EXTRA_LINK, item.linkToMore) + putExtra(MainActivity.EXTRA_SERVER_ID, primaryServerId) + } + Pair(intent, item.hashCode()) + } else -> { val intent = Intent(context, MainActivity::class.java).apply { putExtra(MainActivity.EXTRA_SERVER_ID, primaryServerId) diff --git a/mobile/src/main/java/org/openhab/habdroid/model/Item.kt b/mobile/src/main/java/org/openhab/habdroid/model/Item.kt index b92faabdc7..9b9f7fa700 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/Item.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/Item.kt @@ -43,6 +43,7 @@ data class Item internal constructor( val minimum: Float?, val maximum: Float?, val step: Float?, + val linkToMore: String? ) : Parcelable { val label get() = rawLabel?.split("[", "]")?.getOrNull(0)?.trim() @@ -280,7 +281,8 @@ fun Node.toItem(): Item? { groupNames = emptyList(), minimum = null, maximum = null, - step = null + step = null, + linkToMore = null ) } @@ -338,11 +340,17 @@ fun JSONObject.toItem(): Item { emptyList() } + val type = getString("type").toItemType() + var linkToMore = optJSONObject("metadata")?.optJSONObject("link_to_more")?.optStringOrNull("value") + if (linkToMore == null && type == Item.Type.Group) { + linkToMore = "/basicui/app?w=$name" + } + return Item( name = name, rawLabel = optStringOrNull("label"), category = optStringOrNull("category")?.lowercase(Locale.US), - type = getString("type").toItemType(), + type = type, groupType = optString("groupType").toItemType(), link = optStringOrNull("link"), readOnly = readOnly, @@ -353,7 +361,8 @@ fun JSONObject.toItem(): Item { groupNames = groupNames, minimum = stateDescription?.optFloatOrNull("minimum"), maximum = stateDescription?.optFloatOrNull("maximum"), - step = stateDescription?.optFloatOrNull("step") + step = stateDescription?.optFloatOrNull("step"), + linkToMore = linkToMore ) } diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt index 44b52ec768..dd6bd747f7 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt @@ -74,6 +74,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import org.openhab.habdroid.BuildConfig import org.openhab.habdroid.R import org.openhab.habdroid.background.BackgroundTasksManager @@ -798,6 +799,27 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { intent.action = prefs.getStringOrNull(PrefKeys.START_PAGE) } + if (!intent.getStringExtra(EXTRA_LINK).isNullOrEmpty()) { + var link = intent.getStringExtra(EXTRA_LINK) ?: return + if (!link.startsWith("/")) { + link = "/$link" + } + if (link.startsWith("/basicui/app")) { + intent.action = ACTION_SITEMAP_SELECTED + // Add a host here to be able to parse as HttpUrl + val httpLink = "https://openhab.org$link".toHttpUrlOrNull() ?: return + val serverId = intent.getIntExtra(EXTRA_SERVER_ID, prefs.getPrimaryServerId()) + val sitemap = httpLink.queryParameter("sitemap") + ?: prefs.getDefaultSitemap(connection, serverId)?.name + val subpage = httpLink.queryParameter("w") + intent.putExtra(EXTRA_SITEMAP_URL, "/$sitemap/$subpage") + intent.putExtra(EXTRA_SERVER_ID, serverId) + } else { + intent.action = ACTION_MAIN_UI_SELECTED + intent.putExtra(EXTRA_SUBPAGE, link) + } + } + when (intent.action) { NfcAdapter.ACTION_NDEF_DISCOVERED, Intent.ACTION_VIEW -> { val tag = intent.data?.toTagData() @@ -1497,6 +1519,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { } companion object { + const val ACTION_LINK_OPENED = "org.openhab.habdroid.action.LINK_OPENED" const val ACTION_NOTIFICATION_SELECTED = "org.openhab.habdroid.action.NOTIFICATION_SELECTED" const val ACTION_HABPANEL_SELECTED = "org.openhab.habdroid.action.HABPANEL_SELECTED" const val ACTION_MAIN_UI_SELECTED = "org.openhab.habdroid.action.OH3_UI_SELECTED" @@ -1506,6 +1529,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { const val EXTRA_SITEMAP_URL = "sitemapUrl" const val EXTRA_SERVER_ID = "serverId" const val EXTRA_SUBPAGE = "subpage" + const val EXTRA_LINK = "link" const val EXTRA_PERSISTED_NOTIFICATION_ID = "persistedNotificationId" const val SNACKBAR_TAG_DEMO_MODE_ACTIVE = "demoModeActive"