-
-
Notifications
You must be signed in to change notification settings - Fork 928
Add haptic message support for FrontendScreen #6635
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
7
commits into
main
Choose a base branch
from
feature/haptic
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
7 commits
Select commit
Hold shift + click to select a range
0ae0a72
Add HapticMessage and HapticType
TimoPtr 9146773
Expose haptics out of the FrontendViewModel
TimoPtr ec2f79b
Consume haptics events in the FrontendScreen
TimoPtr 303e409
Update log
TimoPtr f684512
Apply Copilot feedbacks
TimoPtr a82cd43
Update lint baseline
TimoPtr 96822b9
Apply PR comment
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
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
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
59 changes: 59 additions & 0 deletions
59
...ain/kotlin/io/homeassistant/companion/android/frontend/externalbus/incoming/HapticType.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,59 @@ | ||
| package io.homeassistant.companion.android.frontend.externalbus.incoming | ||
|
|
||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.JsonClassDiscriminator | ||
| import kotlinx.serialization.modules.SerializersModule | ||
|
|
||
| /** | ||
| * Represents the haptic feedback types sent by the Home Assistant frontend. | ||
| * | ||
| * Each type maps to a specific Android haptic feedback constant or vibration pattern. | ||
| * The frontend sends these as string identifiers via the external bus `haptic` message. | ||
| * | ||
| * Unknown haptic types from newer frontend versions are deserialized as [Unknown] | ||
| * instead of failing, allowing graceful forward compatibility. | ||
| * | ||
| * @see <a href="https://developers.home-assistant.io/docs/frontend/external-bus/#trigger-haptic-haptic">Haptic feedback documentation</a> | ||
| */ | ||
| @OptIn(ExperimentalSerializationApi::class) | ||
| @Serializable | ||
| @JsonClassDiscriminator("hapticType") | ||
| sealed interface HapticType { | ||
| @Serializable | ||
| @SerialName("success") | ||
| data object Success : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("warning") | ||
| data object Warning : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("failure") | ||
| data object Failure : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("light") | ||
| data object Light : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("medium") | ||
| data object Medium : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("heavy") | ||
| data object Heavy : HapticType | ||
|
|
||
| @Serializable | ||
| @SerialName("selection") | ||
| data object Selection : HapticType | ||
|
|
||
| @Serializable data object Unknown : HapticType | ||
|
|
||
| companion object { | ||
| internal val serializersModule = SerializersModule { | ||
| polymorphicDefaultDeserializer(HapticType::class) { Unknown.serializer() } | ||
| } | ||
| } | ||
| } |
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
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
88 changes: 88 additions & 0 deletions
88
...main/kotlin/io/homeassistant/companion/android/frontend/haptic/HapticFeedbackPerformer.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,88 @@ | ||
| package io.homeassistant.companion.android.frontend.haptic | ||
|
|
||
| import android.os.Build | ||
| import android.os.VibrationEffect | ||
| import android.os.Vibrator | ||
| import android.view.HapticFeedbackConstants | ||
| import android.view.View | ||
| import androidx.core.content.getSystemService | ||
| import io.homeassistant.companion.android.frontend.externalbus.incoming.HapticType | ||
| import kotlin.time.Duration | ||
| import kotlin.time.Duration.Companion.milliseconds | ||
| import kotlin.time.Duration.Companion.seconds | ||
| import timber.log.Timber | ||
|
|
||
| private val SUCCESS_FALLBACK_DURATION = 500.milliseconds | ||
| private val FAILURE_FALLBACK_DURATION = 1.seconds | ||
| private val WARNING_FALLBACK_DURATION = 1.5.seconds | ||
| private val SELECTION_FALLBACK_DURATION = 50.milliseconds | ||
|
|
||
| /** | ||
| * Performs haptic feedback on a [View] based on a [HapticType]. | ||
| * | ||
| * Uses `View.performHapticFeedback()` with semantic constants where available, | ||
| * falling back to `Vibrator` patterns for API levels that lack the specific constant. | ||
| */ | ||
| object HapticFeedbackPerformer { | ||
|
|
||
| /** | ||
| * Performs the haptic feedback corresponding to [hapticType] on the given [view]. | ||
| * | ||
| * Uses `View.performHapticFeedback` for types that have a matching `HapticFeedbackConstants` | ||
| * value on the current API level, and falls back to `Vibrator` for types that require it | ||
| * (e.g., `warning` always uses Vibrator, `success`/`failure`/`selection` fall back to | ||
| * Vibrator on pre-API 30). | ||
| */ | ||
| fun perform(view: View, hapticType: HapticType) { | ||
| Timber.d("Performing haptic feedback: $hapticType") | ||
| when (hapticType) { | ||
| is HapticType.Success -> performSuccess(view) | ||
| is HapticType.Warning -> performWarning(view) | ||
| is HapticType.Failure -> performFailure(view) | ||
| is HapticType.Light -> view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP) | ||
| is HapticType.Medium -> view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) | ||
| is HapticType.Heavy -> view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) | ||
| is HapticType.Selection -> performSelection(view) | ||
| is HapticType.Unknown -> Timber.w("Ignoring unknown haptic type") | ||
| } | ||
| } | ||
|
|
||
| private fun performSuccess(view: View) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| view.performHapticFeedback(HapticFeedbackConstants.CONFIRM) | ||
| } else { | ||
| vibrate(view, duration = SUCCESS_FALLBACK_DURATION) | ||
| } | ||
| } | ||
|
|
||
| private fun performWarning(view: View) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
|
||
| view.context.getSystemService<Vibrator>()?.vibrate( | ||
| VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK), | ||
jpelgrom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } else { | ||
| vibrate(view, duration = WARNING_FALLBACK_DURATION) | ||
| } | ||
| } | ||
|
|
||
| private fun performFailure(view: View) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| view.performHapticFeedback(HapticFeedbackConstants.REJECT) | ||
| } else { | ||
| vibrate(view, duration = FAILURE_FALLBACK_DURATION) | ||
| } | ||
| } | ||
|
|
||
| private fun performSelection(view: View) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| view.performHapticFeedback(HapticFeedbackConstants.GESTURE_START) | ||
| } else { | ||
| vibrate(view, duration = SELECTION_FALLBACK_DURATION) | ||
| } | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| private fun vibrate(view: View, duration: Duration) { | ||
| view.context.getSystemService<Vibrator>()?.vibrate(duration.inWholeMilliseconds) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LaunchedEffectis keyed only onwebView, but it captures/collectshapticEvents. IfhapticEventschanges across recompositions (eg a new ViewModel instance while the same WebView is kept), this effect will keep collecting the old Flow and haptics may stop working (and the old collector may leak). Key the effect on bothwebViewandhapticEvents(or userememberUpdatedStatefor the Flow) so the collector restarts when the Flow reference changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is valid to key on the webview but happy to discuss with @jpelgrom