From c7fb89170e249854ac314b7544e54abf41695fb9 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Wed, 25 Aug 2021 11:16:07 +0200 Subject: [PATCH] fix: Apply correct initial rotation on Android (#368) * fix: Apply correct initial rotation * Make `rotation` a property getter * Update CameraView.kt --- .../java/com/mrousavy/camera/CameraView.kt | 21 ++++------- .../camera/utils/Context+displayRotation.kt | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 android/src/main/java/com/mrousavy/camera/utils/Context+displayRotation.kt diff --git a/android/src/main/java/com/mrousavy/camera/CameraView.kt b/android/src/main/java/com/mrousavy/camera/CameraView.kt index f6dc227..13b5629 100644 --- a/android/src/main/java/com/mrousavy/camera/CameraView.kt +++ b/android/src/main/java/com/mrousavy/camera/CameraView.kt @@ -109,12 +109,14 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner { private val scaleGestureListener: ScaleGestureDetector.SimpleOnScaleGestureListener private val scaleGestureDetector: ScaleGestureDetector private val touchEventListener: OnTouchListener - private val orientationEventListener: OrientationEventListener private val lifecycleRegistry: LifecycleRegistry private var hostLifecycleState: Lifecycle.State - private var rotation: Int = Surface.ROTATION_0 + private val rotation: Int + get() { + return context.displayRotation + } private var minZoom: Float = 1f private var maxZoom: Float = 1f @@ -169,17 +171,7 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner { } scaleGestureDetector = ScaleGestureDetector(context, scaleGestureListener) touchEventListener = OnTouchListener { _, event -> return@OnTouchListener scaleGestureDetector.onTouchEvent(event) } - orientationEventListener = object : OrientationEventListener(context) { - override fun onOrientationChanged(orientation : Int) { - rotation = when (orientation) { - in 45..134 -> Surface.ROTATION_270 - in 135..224 -> Surface.ROTATION_180 - in 225..314 -> Surface.ROTATION_90 - else -> Surface.ROTATION_0 - } - } - } - orientationEventListener.enable() + hostLifecycleState = Lifecycle.State.INITIALIZED lifecycleRegistry = LifecycleRegistry(this) @@ -216,7 +208,6 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner { fun finalize() { mHybridData.resetNative() - orientationEventListener.disable() } private external fun initHybrid(): HybridData @@ -275,7 +266,7 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner { configureSession() } if (shouldReconfigureZoom) { - val zoomClamped = max(min(zoom.toFloat(), maxZoom), minZoom) + val zoomClamped = max(min(zoom, maxZoom), minZoom) camera!!.cameraControl.setZoomRatio(zoomClamped) } if (shouldReconfigureTorch) { diff --git a/android/src/main/java/com/mrousavy/camera/utils/Context+displayRotation.kt b/android/src/main/java/com/mrousavy/camera/utils/Context+displayRotation.kt new file mode 100644 index 0000000..92cde74 --- /dev/null +++ b/android/src/main/java/com/mrousavy/camera/utils/Context+displayRotation.kt @@ -0,0 +1,36 @@ +package com.mrousavy.camera.utils + +import android.content.Context +import android.os.Build +import android.view.Surface +import android.view.WindowManager +import com.facebook.react.bridge.ReactContext + +val Context.displayRotation: Int + get() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + // Context.display + this.display?.let { display -> + return display.rotation + } + + // ReactContext.currentActivity.display + if (this is ReactContext) { + currentActivity?.display?.let { display -> + return display.rotation + } + } + } + + // WindowManager.defaultDisplay + val windowManager = getSystemService(Context.WINDOW_SERVICE) as? WindowManager + if (windowManager != null) { + @Suppress("DEPRECATION") // deprecated since SDK 30 + windowManager.defaultDisplay?.let { display -> + return display.rotation + } + } + + // 0 + return Surface.ROTATION_0 + }