From f53b96e5d8be038fe016619755ee6e79608d245d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Sun, 16 Jul 2023 13:04:49 +0200 Subject: [PATCH] Add toggle for assistant app intent filter on Wear - Adds a toggle for enabling/disabling Assist as an assistant app (= intent filter on Assist activity), as users may not be able to change the app used after choosing 'Always' and Wear OS 2 doesn't ask the user which app to use which can result in Assist instead of the Google Assistant being forced. --- common/src/main/res/values/strings.xml | 1 + wear/src/main/AndroidManifest.xml | 9 +++- .../companion/android/home/MainViewModel.kt | 27 ++++++++++++ .../companion/android/home/views/HomeView.kt | 7 +++- .../android/home/views/SettingsView.kt | 42 +++++++++++++++++-- 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 36996164be4..d5965f252d0 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -35,6 +35,7 @@ Automation Autoplay Videos when dashboard is active. Enabling this setting may increase data usage unexpectedly, proceed with caution. Autoplay Videos + Available as assistant app Home Assistant does not have access to run in the background. Without this permission the app will not be able to reliably send data back to your server. Click here to request permissions. Home Assistant has access to run in the background. Background Access diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml index 7ed9a100545..eecc0ce13ff 100644 --- a/wear/src/main/AndroidManifest.xml +++ b/wear/src/main/AndroidManifest.xml @@ -64,7 +64,6 @@ @@ -96,12 +95,18 @@ + + - + ? = null private var deviceRegistry: List? = null @@ -114,6 +119,8 @@ class MainViewModel @Inject constructor( private set var isFavoritesOnly by mutableStateOf(false) private set + var isAssistantAppAllowed by mutableStateOf(true) + private set fun supportedDomains(): List = HomePresenterImpl.supportedDomains @@ -136,6 +143,13 @@ class MainViewModel @Inject constructor( templateTileContent.value = homePresenter.getTemplateTileContent() templateTileRefreshInterval.value = homePresenter.getTemplateTileRefreshInterval() isFavoritesOnly = homePresenter.getWearFavoritesOnly() + + val assistantAppComponent = ComponentName( + BuildConfig.APPLICATION_ID, + "io.homeassistant.companion.android.conversation.AssistantActivity" + ) + isAssistantAppAllowed = + app.packageManager.getComponentEnabledSetting(assistantAppComponent) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED } } @@ -480,6 +494,19 @@ class MainViewModel @Inject constructor( } } + fun setAssistantApp(allowed: Boolean) { + val assistantAppComponent = ComponentName( + BuildConfig.APPLICATION_ID, + "io.homeassistant.companion.android.conversation.AssistantActivity" + ) + app.packageManager.setComponentEnabledSetting( + assistantAppComponent, + if (allowed) PackageManager.COMPONENT_ENABLED_STATE_DEFAULT else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, + PackageManager.DONT_KILL_APP + ) + isAssistantAppAllowed = allowed + } + fun logout() { homePresenter.onLogoutClicked() diff --git a/wear/src/main/java/io/homeassistant/companion/android/home/views/HomeView.kt b/wear/src/main/java/io/homeassistant/companion/android/home/views/HomeView.kt index b2011623a78..3e879f41e6b 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/home/views/HomeView.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/home/views/HomeView.kt @@ -142,10 +142,13 @@ fun LoadHomePage( isHapticEnabled = mainViewModel.isHapticEnabled.value, isToastEnabled = mainViewModel.isToastEnabled.value, isFavoritesOnly = mainViewModel.isFavoritesOnly, + isAssistantAppAllowed = mainViewModel.isAssistantAppAllowed, onHapticEnabled = { mainViewModel.setHapticEnabled(it) }, onToastEnabled = { mainViewModel.setToastEnabled(it) }, - setFavoritesOnly = { mainViewModel.setWearFavoritesOnly(it) } - ) { swipeDismissableNavController.navigate(SCREEN_SET_TILE_TEMPLATE) } + setFavoritesOnly = { mainViewModel.setWearFavoritesOnly(it) }, + onClickTemplateTile = { swipeDismissableNavController.navigate(SCREEN_SET_TILE_TEMPLATE) }, + onAssistantAppAllowed = mainViewModel::setAssistantApp + ) } composable(SCREEN_SET_FAVORITES) { SetFavoritesView( diff --git a/wear/src/main/java/io/homeassistant/companion/android/home/views/SettingsView.kt b/wear/src/main/java/io/homeassistant/companion/android/home/views/SettingsView.kt index fcb51cd92f3..19f65ca0803 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/home/views/SettingsView.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/home/views/SettingsView.kt @@ -70,10 +70,12 @@ fun SettingsView( isHapticEnabled: Boolean, isToastEnabled: Boolean, isFavoritesOnly: Boolean, + isAssistantAppAllowed: Boolean, onHapticEnabled: (Boolean) -> Unit, onToastEnabled: (Boolean) -> Unit, setFavoritesOnly: (Boolean) -> Unit, - onClickTemplateTile: () -> Unit + onClickTemplateTile: () -> Unit, + onAssistantAppAllowed: (Boolean) -> Unit ) { val scalingLazyListState: ScalingLazyListState = rememberScalingLazyListState() @@ -237,6 +239,37 @@ fun SettingsView( onClick = onClickSensors ) } + item { + ListHeader( + id = commonR.string.assist + ) + } + item { + ToggleChip( + modifier = Modifier.fillMaxWidth(), + checked = isAssistantAppAllowed, + onCheckedChange = onAssistantAppAllowed, + label = { + Text(stringResource(commonR.string.available_as_assistant_app)) + }, + appIcon = { + Image( + asset = CommunityMaterial.Icon.cmd_comment_processing_outline, + colorFilter = ColorFilter.tint(wearColorPalette.onSurface) + ) + }, + toggleControl = { + Icon( + imageVector = ToggleChipDefaults.switchIcon(isAssistantAppAllowed), + contentDescription = if (isFavoritesOnly) { + stringResource(commonR.string.enabled) + } else { + stringResource(commonR.string.disabled) + } + ) + } + ) + } item { ListHeader( id = commonR.string.account @@ -287,8 +320,11 @@ private fun PreviewSettingsView() { isHapticEnabled = true, isToastEnabled = false, isFavoritesOnly = false, + isAssistantAppAllowed = true, onHapticEnabled = {}, onToastEnabled = {}, - setFavoritesOnly = {} - ) {} + setFavoritesOnly = {}, + onClickTemplateTile = {}, + onAssistantAppAllowed = {} + ) }