-
-
Notifications
You must be signed in to change notification settings - Fork 957
Add support for Add to feature within FrontendScreen #6792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimoPtr
wants to merge
4
commits into
main
Choose a base branch
from
feature/add-to
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88756f8
Add Add to message in/out and improve Outgoing Result class usage
TimoPtr 17b3388
Replace EntityAddToHandler per FrontendEntityAddToHandler
TimoPtr 34f67f8
Add support for Add to in FrontendScreen
TimoPtr 45f78f5
Add missing test and fix after rebase
TimoPtr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...in/kotlin/io/homeassistant/companion/android/frontend/addto/FrontendEntityAddToHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package io.homeassistant.companion.android.frontend.addto | ||
|
|
||
| import android.content.Context | ||
| import androidx.annotation.VisibleForTesting | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import io.homeassistant.companion.android.BuildConfig | ||
| import io.homeassistant.companion.android.common.R as commonR | ||
| import io.homeassistant.companion.android.common.data.integration.IntegrationDomains.CAMERA_DOMAIN | ||
| import io.homeassistant.companion.android.common.data.integration.IntegrationDomains.IMAGE_DOMAIN | ||
| import io.homeassistant.companion.android.common.data.integration.IntegrationDomains.MEDIA_PLAYER_DOMAIN | ||
| import io.homeassistant.companion.android.common.data.integration.IntegrationDomains.TODO_DOMAIN | ||
| import io.homeassistant.companion.android.common.data.prefs.AutoFavorite | ||
| import io.homeassistant.companion.android.common.data.prefs.PrefsRepository | ||
| import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
| import io.homeassistant.companion.android.common.util.FailFast | ||
| import io.homeassistant.companion.android.di.qualifiers.IsAutomotive | ||
| import io.homeassistant.companion.android.frontend.navigation.FrontendEvent | ||
| import io.homeassistant.companion.android.frontend.navigation.WidgetType | ||
| import io.homeassistant.companion.android.util.QuestUtil | ||
| import io.homeassistant.companion.android.util.vehicle.isVehicleDomain | ||
| import io.homeassistant.companion.android.webview.addto.EntityAddToAction | ||
| import io.homeassistant.companion.android.webview.externalbus.ExternalEntityAddToAction | ||
| import javax.inject.Inject | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import timber.log.Timber | ||
|
|
||
| /** | ||
| * Handles the "Add To" functionality for Home Assistant entities, allowing users to add entities | ||
| * to various Android platform features and connected devices. | ||
| * | ||
| * This class provides two main capabilities: | ||
| * 1. Determining which actions are available for a given entity based on its type and domain | ||
| * 2. Executing the selected action to add the entity to the chosen platform feature | ||
| * | ||
| * The available actions depend on the entity's domain and current system state (for example, watch connectivity, | ||
| * shortcut limits). | ||
| */ | ||
| class FrontendEntityAddToHandler @VisibleForTesting constructor( | ||
| private val context: Context, | ||
| private val serverManager: ServerManager, | ||
| private val prefsRepository: PrefsRepository, | ||
| private val isAutomotive: Boolean, | ||
| private val isQuest: Boolean, | ||
| private val isFullFlavor: Boolean, | ||
| ) { | ||
|
|
||
| @Inject | ||
| constructor( | ||
| @ApplicationContext context: Context, | ||
| serverManager: ServerManager, | ||
| prefsRepository: PrefsRepository, | ||
| @IsAutomotive isAutomotive: Boolean, | ||
| ) : this( | ||
| context = context, | ||
| serverManager = serverManager, | ||
| prefsRepository = prefsRepository, | ||
| isAutomotive = isAutomotive, | ||
| isQuest = QuestUtil.isQuest, | ||
| isFullFlavor = BuildConfig.FLAVOR == "full", | ||
| ) | ||
|
|
||
| /** | ||
| * Returns the list of available actions for the specified entity. | ||
| * | ||
| * The available actions depend on the entity's domain. For example, media player entities | ||
| * will include both the general entity widget and a specialized media player widget option. | ||
| * Vehicle-related entities will include Android Auto favorites. Camera and image entities | ||
| * will include camera widget options. | ||
| * | ||
| * @param entityId The entity ID to get available actions for (for example, "light.living_room") | ||
| * @return List of actions that can be performed for this entity. Returns an empty list if the | ||
| * entity is not found or if the server is unavailable. | ||
| */ | ||
| suspend fun getActionsForEntity(entityId: String): List<ExternalEntityAddToAction> = | ||
| withContext(Dispatchers.Default) { | ||
| val server = serverManager.getServer() ?: return@withContext emptyList() | ||
| val entity = try { | ||
| serverManager.integrationRepository(server.id).getEntity(entityId) | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| Timber.e(e, "Failed to get entity for id $entityId") | ||
| null | ||
|
TimoPtr marked this conversation as resolved.
|
||
| } ?: return@withContext emptyList() | ||
|
|
||
| val actions = mutableListOf<EntityAddToAction>() | ||
|
|
||
| if (!isAutomotive && !isQuest) { | ||
| actions.add(EntityAddToAction.EntityWidget) | ||
|
|
||
| if (entity.domain == MEDIA_PLAYER_DOMAIN) { | ||
| actions.add(EntityAddToAction.MediaPlayerWidget) | ||
| } | ||
| if (entity.domain == TODO_DOMAIN) { | ||
| actions.add(EntityAddToAction.TodoWidget) | ||
| } | ||
| if (entity.domain == CAMERA_DOMAIN || entity.domain == IMAGE_DOMAIN) { | ||
| actions.add(EntityAddToAction.CameraWidget) | ||
| } | ||
| } | ||
|
|
||
| if (isVehicleDomain(entity) && (isFullFlavor || isAutomotive)) { | ||
| actions.add(EntityAddToAction.AndroidAutoFavorite) | ||
| } | ||
|
|
||
| actions.map { action -> ExternalEntityAddToAction.fromAction(context, action) } | ||
| } | ||
|
|
||
| /** | ||
| * Executes an EntityAddTo action and returns the corresponding [FrontendEvent]. | ||
| * | ||
| * For [EntityAddToAction.AndroidAutoFavorite], persists the favorite and returns a snackbar event. | ||
| * For widget actions, returns a [FrontendEvent.LaunchWidgetConfig]. | ||
| * For unimplemented actions (Shortcut, Tile, Watch), returns null. | ||
| */ | ||
| suspend fun execute(entityId: String, action: EntityAddToAction): FrontendEvent? { | ||
| return when (action) { | ||
| is EntityAddToAction.AndroidAutoFavorite -> { | ||
| addToAndroidAutoFavorite(entityId) | ||
| FrontendEvent.ShowSnackbar(commonR.string.add_to_android_auto_success) | ||
| } | ||
| is EntityAddToAction.EntityWidget -> | ||
| FrontendEvent.LaunchWidgetConfig(entityId, WidgetType.Entity) | ||
| is EntityAddToAction.MediaPlayerWidget -> | ||
| FrontendEvent.LaunchWidgetConfig(entityId, WidgetType.MediaPlayer) | ||
| is EntityAddToAction.CameraWidget -> | ||
| FrontendEvent.LaunchWidgetConfig(entityId, WidgetType.Camera) | ||
| is EntityAddToAction.TodoWidget -> | ||
| FrontendEvent.LaunchWidgetConfig(entityId, WidgetType.Todo) | ||
| is EntityAddToAction.Shortcut, | ||
| is EntityAddToAction.Tile, | ||
| is EntityAddToAction.Watch, | ||
| -> null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Persists the entity as an Android Auto favorite for the active server. | ||
| * | ||
| * Called from both the frontend [execute] path and the legacy webview handler so | ||
| * the favorite-storage logic stays in a single place. | ||
| */ | ||
| private suspend fun addToAndroidAutoFavorite(entityId: String) { | ||
| val serverId = serverManager.getServer()?.id | ||
| if (serverId != null) { | ||
| prefsRepository.addAutoFavorite(AutoFavorite(serverId, entityId)) | ||
| } else { | ||
| FailFast.fail { "Server is null when adding auto favorite" } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.