-
-
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
Conversation
Co-authored-by: sds100 <[email protected]>
Co-authored-by: sds100 <[email protected]>
sds100
left a comment
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.
@copilot Please implement my comments
| * APIs work best with an active Activity context. Apps running in the background | ||
| * may not receive real-time hinge state updates. | ||
| */ | ||
| fun startMonitoring(activity: Activity) { |
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.
Is there no API to monitor this in the background? Can you not use the sensors API?
https://developer.android.com/reference/android/hardware/Sensor#STRING_TYPE_HINGE_ANGLE
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.
Switched to using Sensor.TYPE_HINGE_ANGLE from the SensorManager API. This provides real-time angle detection and works in the background without requiring an Activity context. The sensor automatically starts monitoring on initialization. (17cf309)
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class AndroidHingeAdapter @Inject constructor( |
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.
Rename this and the interface FoldableAdapter
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.
Renamed to FoldableAdapter and AndroidFoldableAdapter. (17cf309)
|
|
||
| ConstraintEntity.HINGE_CLOSED -> ConstraintData.HingeClosed | ||
| ConstraintEntity.HINGE_OPEN -> ConstraintData.HingeOpen | ||
| ConstraintEntity.HINGE_ANGLE -> { |
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.
Do not do the hinge angle constraint.
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.
Removed the HingeAngle constraint. Only HingeClosed and HingeOpen constraints remain. (17cf309)
… HingeAngle constraint - Switch from WindowManager to SensorManager for background support - Use Sensor.TYPE_HINGE_ANGLE for real-time angle detection - Rename AndroidHingeAdapter to AndroidFoldableAdapter - Rename HingeAdapter interface to FoldableAdapter - Remove HingeAngle constraint (keep only HingeClosed and HingeOpen) - Remove androidx.window dependency - Update all references throughout codebase Co-authored-by: sds100 <[email protected]>
sds100
left a comment
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.
Please review my comments.
| /** | ||
| * Flow that emits the current hinge state. | ||
| */ | ||
| val hingeState: Flow<HingeState> |
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.
@copilot Just make one StateFlow instead of a separate cached method.
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.
Removed getCachedHingeState() method. Now using hingeState.value directly from the StateFlow. (faa66c9)
base/src/main/java/io/github/sds100/keymapper/base/constraints/ConstraintUtils.kt
Show resolved
Hide resolved
| /** | ||
| * True if the device has a hinge and it is currently available. | ||
| */ | ||
| val isAvailable: Boolean, |
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.
Create separate sealed class for Unavailable.
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.
Created sealed class for HingeState with Unavailable and Available(angle: Float) states. Updated ConstraintSnapshot to use pattern matching with the sealed class. (faa66c9)
- Convert HingeState to sealed class with Unavailable and Available(angle) states - Remove getCachedHingeState() method, use StateFlow.value directly - Add @RequiresApi(Build.VERSION_CODES.R) annotations - Add API level 30 minimum requirement check in CreateConstraintUseCase - Update ConstraintSnapshot to use sealed class pattern matching Co-authored-by: sds100 <[email protected]>
Adds constraint support for detecting hinge state on foldable devices (Galaxy Fold, Pixel Fold, etc.), enabling key mappings that trigger based on device fold position.
Implementation
New Constraints
HINGE_CLOSED- angle < 30°HINGE_OPEN- angle ≥ 150°Architecture
FoldableAdapterinterface withAndroidFoldableAdapterimplementation using Android Sensor APISensor.TYPE_HINGE_ANGLEfor precise, real-time angle detectionHingeStatewithUnavailableandAvailable(angle)states for type-safe state representationConstraintData,ConstraintEntity,DetectConstraintsUseCase,ConstraintSnapshot@RequiresApiannotationsDetection Logic
Uses hardware sensor for precise angle measurements:
SensorEventListenerforTYPE_HINGE_ANGLEConstraint evaluation:
Benefits
Files Changed
16 files: 2 new adapter files (FoldableAdapter with sealed class, AndroidFoldableAdapter), constraint system updates (Constraint.kt, ConstraintEntity.kt, CreateConstraintUseCase.kt with API validation, etc.), detection logic (DetectConstraintsUseCase.kt, ConstraintSnapshot.kt with pattern matching), UI layer (strings, icons, viewmodels), build dependencies
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.