-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add onDisplayChanged event (listenForDisplayChanges) #120
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
gladiuscode
wants to merge
9
commits into
main
Choose a base branch
from
feat/display-changes-event
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
9 commits
Select commit
Hold shift + click to select a range
fd21f1b
fix(ios): report correct orientations on foldable devices (iPhone Duo)
gladiuscode 39542ce
fix(ios): report the orientation applied by the system after lockTo/r…
gladiuscode bf3c82e
docs: document foldable devices behavior and iOS 27 scene orientations
gladiuscode 6bdefa3
chore: update README.md
gladiuscode 07ab6cc
fix(ios): report the device orientation as provided by iOS
gladiuscode d816662
docs: document raw device orientation on foldables
gladiuscode 8bea7cf
fix(ios): read geometry from the observed application scene
gladiuscode 83b59df
feat: add onDisplayChanged event (listenForDisplayChanges)
gladiuscode 3683270
docs: document listenForDisplayChanges
gladiuscode 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
106 changes: 106 additions & 0 deletions
106
android/src/main/java/com/orientationdirector/implementation/DisplayChangesListener.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,106 @@ | ||
| package com.orientationdirector.implementation | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Point | ||
| import android.hardware.display.DisplayManager | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.view.WindowManager | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
|
|
||
| /** | ||
| * Notifies when the app moves to a display with a different physical size, | ||
| * e.g. when a foldable device is folded or unfolded. | ||
| * | ||
| * DisplayManager notifies any display change, rotations included, so the | ||
| * physical size is compared regardless of its orientation. | ||
| */ | ||
| class DisplayChangesListener internal constructor(private val context: ReactApplicationContext) : | ||
| DisplayManager.DisplayListener { | ||
|
|
||
| private val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager | ||
| private val handler = Handler(Looper.getMainLooper()) | ||
| private var isRegistered = false | ||
| private var lastPhysicalSize: Pair<Int, Int>? = null | ||
|
|
||
| private var onDisplayChangedCallback: ((widthDp: Double, heightDp: Double) -> Unit)? = null | ||
|
|
||
| fun setOnDisplayChangedCallback(callback: (widthDp: Double, heightDp: Double) -> Unit) { | ||
| onDisplayChangedCallback = callback | ||
| } | ||
|
|
||
| fun register() { | ||
| if (isRegistered) { | ||
| return | ||
| } | ||
|
|
||
| displayManager.registerDisplayListener(this, handler) | ||
| isRegistered = true | ||
|
|
||
| // The display might have changed while the listener was unregistered | ||
| checkDisplay() | ||
| } | ||
|
|
||
| fun unregister() { | ||
| if (!isRegistered) { | ||
| return | ||
| } | ||
|
|
||
| displayManager.unregisterDisplayListener(this) | ||
| isRegistered = false | ||
| } | ||
|
|
||
| override fun onDisplayAdded(displayId: Int) = Unit | ||
|
|
||
| override fun onDisplayRemoved(displayId: Int) = Unit | ||
|
|
||
| override fun onDisplayChanged(displayId: Int) { | ||
| checkDisplay() | ||
| } | ||
|
|
||
| private fun checkDisplay() { | ||
| val size = computeDisplaySize() ?: return | ||
| val physicalSize = Pair(minOf(size.x, size.y), maxOf(size.x, size.y)) | ||
|
|
||
| val previousPhysicalSize = lastPhysicalSize | ||
| lastPhysicalSize = physicalSize | ||
|
|
||
| // First sync, nothing changed yet | ||
| if (previousPhysicalSize == null || previousPhysicalSize == physicalSize) { | ||
| return | ||
| } | ||
|
|
||
| val density = computeDensity() | ||
| onDisplayChangedCallback?.invoke(size.x / density, size.y / density) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the size of the display the activity is on, in pixels, | ||
| * in the orientation it is currently displayed. | ||
| */ | ||
| private fun computeDisplaySize(): Point? { | ||
| val activity = context.currentActivity ?: return null | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| val bounds = activity.windowManager.maximumWindowMetrics.bounds | ||
| return Point(bounds.width(), bounds.height()) | ||
| } | ||
|
|
||
| val windowManager = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager | ||
| val size = Point() | ||
| @Suppress("DEPRECATION") | ||
| windowManager.defaultDisplay.getRealSize(size) | ||
| return size | ||
| } | ||
|
|
||
| private fun computeDensity(): Double { | ||
| val activity = context.currentActivity | ||
|
|
||
| if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
| return activity.windowManager.maximumWindowMetrics.density.toDouble() | ||
| } | ||
|
|
||
| return (activity?.resources ?: context.resources).displayMetrics.density.toDouble() | ||
| } | ||
| } | ||
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
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Check the display when the activity moves between existing displays.
DisplayListener.onDisplayChangedreports changes to a display’s properties, not movement of an activity. An activity that handles configuration changes can move between existing displays without either display changing. (developer.android.com)If the activity remains resumed during that move, neither callback nor
register()checks the new display. The existing configuration-change receiver only checks interface orientation. The display event can therefore be missed until a later display change or pause/resume.Expose a display resynchronization method and call it from the existing configuration-change receiver. Keep the normalized-size comparison to suppress rotations and window resizes.
🤖 Prompt for AI Agents