-
-
Notifications
You must be signed in to change notification settings - Fork 226
Add foldable device constraints using Sensor API #1853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de8ad46
Initial plan
Copilot e1a315a
Add hinge constraint implementation for foldable devices
Copilot 7248549
Add error handling and documentation for hinge constraint
Copilot 17cf309
Implement feedback: Use Sensor API, rename to FoldableAdapter, remove…
Copilot faa66c9
Refine FoldableAdapter: sealed class, StateFlow, API level check
Copilot cea95d4
Merge branch 'develop' into copilot/hinge-closed-open-constraint
sds100 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,5 @@ enum class ConstraintDependency { | |
| LOCK_SCREEN_SHOWING, | ||
| PHONE_STATE, | ||
| CHARGING_STATE, | ||
| HINGE_STATE, | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -49,5 +49,8 @@ enum class ConstraintId { | |
| CHARGING, | ||
| DISCHARGING, | ||
|
|
||
| HINGE_CLOSED, | ||
| HINGE_OPEN, | ||
|
|
||
| TIME, | ||
| } | ||
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
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
68 changes: 68 additions & 0 deletions
68
system/src/main/java/io/github/sds100/keymapper/system/hinge/AndroidFoldableAdapter.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,68 @@ | ||
| package io.github.sds100.keymapper.system.hinge | ||
|
|
||
| import android.content.Context | ||
| import android.hardware.Sensor | ||
| import android.hardware.SensorEvent | ||
| import android.hardware.SensorEventListener | ||
| import android.hardware.SensorManager | ||
| import android.os.Build | ||
| import androidx.annotation.RequiresApi | ||
| import androidx.core.content.getSystemService | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import timber.log.Timber | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.R) | ||
| @Singleton | ||
| class AndroidFoldableAdapter @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| ) : FoldableAdapter { | ||
|
|
||
| private val _hingeState = MutableStateFlow<HingeState>(HingeState.Unavailable) | ||
| override val hingeState: StateFlow<HingeState> = _hingeState.asStateFlow() | ||
|
|
||
| private val sensorManager: SensorManager? = context.getSystemService() | ||
| private val hingeSensor: Sensor? = sensorManager?.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE) | ||
|
|
||
| private val sensorEventListener = object : SensorEventListener { | ||
| override fun onSensorChanged(event: SensorEvent?) { | ||
| event?.let { | ||
| if (it.sensor.type == Sensor.TYPE_HINGE_ANGLE && it.values.isNotEmpty()) { | ||
| val angle = it.values[0] | ||
| _hingeState.value = HingeState.Available(angle) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { | ||
| // Not needed for hinge angle sensor | ||
| } | ||
| } | ||
|
|
||
| init { | ||
| startMonitoring() | ||
| } | ||
|
|
||
| private fun startMonitoring() { | ||
| if (hingeSensor != null) { | ||
| try { | ||
| sensorManager?.registerListener( | ||
| sensorEventListener, | ||
| hingeSensor, | ||
| SensorManager.SENSOR_DELAY_NORMAL, | ||
| ) | ||
| Timber.d("Hinge angle sensor monitoring started") | ||
| } catch (e: Exception) { | ||
| Timber.e(e, "Failed to start hinge angle sensor monitoring") | ||
| _hingeState.value = HingeState.Unavailable | ||
| } | ||
| } else { | ||
| Timber.d("Hinge angle sensor not available on this device") | ||
| _hingeState.value = HingeState.Unavailable | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
system/src/main/java/io/github/sds100/keymapper/system/hinge/FoldableAdapter.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,31 @@ | ||
| package io.github.sds100.keymapper.system.hinge | ||
|
|
||
| import androidx.annotation.RequiresApi | ||
| import android.os.Build | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
||
| /** | ||
| * Represents the state of a foldable device hinge. | ||
| */ | ||
| sealed class HingeState { | ||
| /** | ||
| * Hinge sensor is not available on this device. | ||
| */ | ||
| data object Unavailable : HingeState() | ||
|
|
||
| /** | ||
| * Hinge sensor is available and reporting angle. | ||
| * @param angle The angle in degrees of the hinge. | ||
| * 0 degrees means the device is closed/flat. | ||
| * 180 degrees means the device is fully open. | ||
| */ | ||
| data class Available(val angle: Float) : HingeState() | ||
| } | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.R) | ||
| interface FoldableAdapter { | ||
| /** | ||
| * StateFlow that emits the current hinge state. | ||
| */ | ||
| val hingeState: StateFlow<HingeState> | ||
| } |
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.