-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(android): workaround on orientation for Android
- Loading branch information
1 parent
efd904d
commit 3ae0597
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
48 changes: 48 additions & 0 deletions
48
android/src/main/java/video/api/reactnative/livestream/utils/OrientationManager.kt
This file contains 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,48 @@ | ||
package video.api.reactnative.livestream.utils | ||
|
||
import android.content.Context | ||
import android.view.OrientationEventListener | ||
import java.io.Closeable | ||
|
||
class OrientationManager(context: Context) : Closeable { | ||
private val orientationEventListener = object : OrientationEventListener(context) { | ||
override fun onOrientationChanged(orientation: Int) { | ||
currentOrientation = (orientation + 45) / 90 * 90 % 360 | ||
} | ||
}.apply { | ||
enable() | ||
} | ||
|
||
private var currentOrientation: Int = OrientationEventListener.ORIENTATION_UNKNOWN | ||
set(value) { | ||
if (field != value) { | ||
if (field == OrientationEventListener.ORIENTATION_UNKNOWN) { | ||
// First time we get an orientation value, don't consider it as a change | ||
field = value | ||
return | ||
} else { | ||
field = value | ||
_orientationHasChanged = true | ||
} | ||
} | ||
} | ||
|
||
|
||
private var _orientationHasChanged: Boolean = false | ||
|
||
val orientationHasChanged: Boolean | ||
get() { | ||
if (!orientationEventListener.canDetectOrientation()) { | ||
// Assume orientation has changed if we can't detect it | ||
return true | ||
} | ||
|
||
val orientationHasChanged = _orientationHasChanged | ||
_orientationHasChanged = false | ||
return orientationHasChanged | ||
} | ||
|
||
override fun close() { | ||
orientationEventListener.disable() | ||
} | ||
} |