Skip to content

Commit

Permalink
Custom targets for device control
Browse files Browse the repository at this point in the history
The UI command item in Main UI uses the same URL for `navigate:` than
Main UI itself.
Therefore use the same URLs as the browser uses. When a Basic UI URL is
detected, open it in Sitemap, otherwise in Main UI Webview.

If no `link_to_more` is set, default to a list of members for groups.

Closes openhab#3140
Closes openhab#3529

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jan 26, 2024
1 parent efbb75a commit 740689a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions mobile/src/main/java/org/openhab/habdroid/model/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -280,7 +281,8 @@ fun Node.toItem(): Item? {
groupNames = emptyList(),
minimum = null,
maximum = null,
step = null
step = null,
linkToMore = null
)
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
)
}

Expand Down
24 changes: 24 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 740689a

Please sign in to comment.