Skip to content

Commit

Permalink
Add toggle for assistant app intent filter on Wear
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
jpelgrom committed Jul 16, 2023
1 parent 4b1be89 commit f53b96e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<string name="automation">Automation</string>
<string name="autoplay_video_summary">Autoplay Videos when dashboard is active. Enabling this setting may increase data usage unexpectedly, proceed with caution.</string>
<string name="autoplay_video">Autoplay Videos</string>
<string name="available_as_assistant_app">Available as assistant app</string>
<string name="background_access_disabled">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.</string>
<string name="background_access_enabled">Home Assistant has access to run in the background.</string>
<string name="background_access_title">Background Access</string>
Expand Down
9 changes: 7 additions & 2 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<activity android:name=".home.HomeActivity" />
<activity android:name=".splash.SplashActivity"
android:theme="@style/SplashTheme"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down Expand Up @@ -96,12 +95,18 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:name=".conversation.AssistantActivity"
android:targetActivity=".conversation.ConversationActivity"
android:label="@string/ha_assist"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<action android:name="android.intent.action.VOICE_ASSIST"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</activity-alias>

<!-- Tiles -->
<service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.homeassistant.companion.android.home

import android.app.Application
import android.content.ComponentName
import android.content.pm.PackageManager
import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
Expand All @@ -12,6 +14,7 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.HomeAssistantApplication
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.domain
Expand Down Expand Up @@ -55,6 +58,8 @@ class MainViewModel @Inject constructor(
LOADING, READY, ERROR
}

private val app = application

private lateinit var homePresenter: HomePresenter
private var areaRegistry: List<AreaRegistryResponse>? = null
private var deviceRegistry: List<DeviceRegistryResponse>? = null
Expand Down Expand Up @@ -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<String> = HomePresenterImpl.supportedDomains

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -287,8 +320,11 @@ private fun PreviewSettingsView() {
isHapticEnabled = true,
isToastEnabled = false,
isFavoritesOnly = false,
isAssistantAppAllowed = true,
onHapticEnabled = {},
onToastEnabled = {},
setFavoritesOnly = {}
) {}
setFavoritesOnly = {},
onClickTemplateTile = {},
onAssistantAppAllowed = {}
)
}

0 comments on commit f53b96e

Please sign in to comment.