From 27b49b18b26809abd4a68278eaab1613e407352a Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:06:17 +0200 Subject: [PATCH 1/2] Adds app activity shortcuts to ShortcutsTile --- .../android/tiles/TileActionReceiver.kt | 34 +++++++---- .../android/views/ChooseEntityView.kt | 60 +++++++++++++++++++ 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt b/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt index 4250f8604fe..03f3adf4c5e 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt @@ -8,6 +8,8 @@ import dagger.hilt.android.AndroidEntryPoint import io.homeassistant.companion.android.common.data.integration.onEntityPressedWithoutState import io.homeassistant.companion.android.common.data.prefs.WearPrefsRepository import io.homeassistant.companion.android.common.data.servers.ServerManager +import io.homeassistant.companion.android.conversation.ConversationActivity +import io.homeassistant.companion.android.home.HomeActivity import javax.inject.Inject import kotlinx.coroutines.runBlocking @@ -28,16 +30,28 @@ class TileActionReceiver : BroadcastReceiver() { val entityId: String? = intent?.getStringExtra("entity_id") if (entityId != null) { - runBlocking { - if (wearPrefsRepository.getWearHapticFeedback() && context != null) hapticClick(context) - - try { - onEntityPressedWithoutState( - entityId = entityId, - integrationRepository = serverManager.integrationRepository() - ) - } catch (e: Exception) { - Log.e(TAG, "Cannot call tile service", e) + if (entityId.split(".")[0] == "app_shortcut" && context != null) { + val m = mapOf( + "assist" to ConversationActivity.newInstance(context), + "home_assistant" to HomeActivity.newInstance(context) + ) + m[entityId.split(".")[1]]?.let { + it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(it) + } + } + else { + runBlocking { + if (wearPrefsRepository.getWearHapticFeedback() && context != null) hapticClick(context) + + try { + onEntityPressedWithoutState( + entityId = entityId, + integrationRepository = serverManager.integrationRepository() + ) + } catch (e: Exception) { + Log.e(TAG, "Cannot call tile service", e) + } } } } diff --git a/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt b/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt index ef858a3ce82..86683f1e357 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt @@ -52,6 +52,7 @@ fun ChooseEntityView( // Remember expanded state of each header val expandedStates = rememberExpandedStates(entitiesByDomainOrder) var expandedFavorites: Boolean by rememberSaveable { mutableStateOf(false) } + var expandedAppShortcuts: Boolean by rememberSaveable { mutableStateOf(false) } WearAppTheme { ThemeLazyColumn { @@ -118,6 +119,65 @@ fun ChooseEntityView( } } } + + // App Shortcuts + item { + ExpandableListHeader( + string = stringResource(commonR.string.shortcuts), + expanded = expandedAppShortcuts, + onExpandChanged = { expandedAppShortcuts = it } + ) + } + if (expandedAppShortcuts) { + // HomeAssistant app shortcut + item { + Button( + modifier = Modifier + .fillMaxWidth(), + icon = { + Image( + asset = CommunityMaterial.Icon2.cmd_home_assistant, + colorFilter = ColorFilter.tint(Color.White) + ) + }, + label = { Text(stringResource(id = commonR.string.app_name)) }, + onClick = { + onEntitySelected( + SimplifiedEntity( + "app_shortcut.home_assistant", + "Home Assistant", + "mdi:home-assistant" + ) + ) + }, + colors = getFilledTonalButtonColors() + ) + } + // Assist shortcut + item { + Button( + modifier = Modifier + .fillMaxWidth(), + icon = { + Image( + asset = CommunityMaterial.Icon.cmd_comment_processing_outline, + colorFilter = ColorFilter.tint(Color.White) + ) + }, + label = { Text(stringResource(id = commonR.string.assist)) }, + onClick = { + onEntitySelected( + SimplifiedEntity( + "app_shortcut.assist", + "Assist", + "mdi:comment-processing-outline" + ) + ) + }, + colors = getFilledTonalButtonColors() + ) + } + } } } } From d3c35f47e1256907967732b0d7283147f0f6057b Mon Sep 17 00:00:00 2001 From: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Date: Mon, 5 Aug 2024 21:52:39 +0200 Subject: [PATCH 2/2] Linter fixes --- .../companion/android/tiles/TileActionReceiver.kt | 3 +-- .../companion/android/views/ChooseEntityView.kt | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt b/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt index 03f3adf4c5e..2a1c56f7d40 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/tiles/TileActionReceiver.kt @@ -39,8 +39,7 @@ class TileActionReceiver : BroadcastReceiver() { it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(it) } - } - else { + } else { runBlocking { if (wearPrefsRepository.getWearHapticFeedback() && context != null) hapticClick(context) diff --git a/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt b/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt index 86683f1e357..44190b9b156 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/views/ChooseEntityView.kt @@ -130,11 +130,11 @@ fun ChooseEntityView( } if (expandedAppShortcuts) { // HomeAssistant app shortcut - item { + item { Button( modifier = Modifier .fillMaxWidth(), - icon = { + icon = { Image( asset = CommunityMaterial.Icon2.cmd_home_assistant, colorFilter = ColorFilter.tint(Color.White) @@ -154,11 +154,11 @@ fun ChooseEntityView( ) } // Assist shortcut - item { + item { Button( modifier = Modifier .fillMaxWidth(), - icon = { + icon = { Image( asset = CommunityMaterial.Icon.cmd_comment_processing_outline, colorFilter = ColorFilter.tint(Color.White)