Repository navigation
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f744d6a
feat: add onDisplayChanged event (listenForDisplayChanges)
gladiuscode a318f64
docs: document listenForDisplayChanges
gladiuscode 464624e
fix: address display change review comments
gladiuscode d3260ad
feat: add useDisplay hook
gladiuscode 4141d3f
chore(example): use useDisplay in Explore screen
gladiuscode 26466fb
fix(android): compare display size in dp to detect density changes
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
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
| 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 size in dp, | ||
| * e.g. when a foldable device is folded or unfolded. | ||
| * | ||
| * DisplayManager notifies any display change, rotations included, so the | ||
| * size is compared regardless of its orientation. It is compared in dp so that | ||
| * density changes with the same pixel size are notified as well. | ||
| */ | ||
| 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 lastSizeDp: Pair<Double, Double>? = 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 | ||
| sync() | ||
| } | ||
|
|
||
| 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) { | ||
| sync() | ||
| } | ||
|
|
||
| /** | ||
| * Compares the current display with the last known one. | ||
| * It is also needed when the activity moves between existing displays, | ||
| * as DisplayManager doesn't notify it since no display has changed. | ||
| */ | ||
| fun sync() { | ||
| val size = computeDisplaySize() ?: return | ||
| val density = computeDensity() | ||
| val widthDp = size.x / density | ||
| val heightDp = size.y / density | ||
| val sizeDp = Pair(minOf(widthDp, heightDp), maxOf(widthDp, heightDp)) | ||
|
|
||
| val previousSizeDp = lastSizeDp | ||
| lastSizeDp = sizeDp | ||
|
|
||
| // First sync, nothing changed yet | ||
| if (previousSizeDp == null || previousSizeDp == sizeDp) { | ||
| return | ||
| } | ||
|
|
||
| onDisplayChangedCallback?.invoke(widthDp, heightDp) | ||
| } | ||
|
|
||
| /** | ||
| * 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()) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.