Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions app/src/main/java/app/grapheneos/camera/CamConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.provider.MediaStore
import android.util.Log
import android.util.Size
import android.view.MotionEvent
import android.view.Surface
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
Expand Down Expand Up @@ -1061,17 +1062,7 @@ class CamConfig(private val mActivity: MainActivity) {
mActivity.exposureBar.hidePanel()
modePref = mActivity.getSharedPreferences(currentMode.name, Context.MODE_PRIVATE)

val rotation = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val display = mActivity.display
display?.rotation ?: @Suppress("DEPRECATION")
mActivity.windowManager.defaultDisplay.rotation
} else {
// We don't really have any option here, but this initialization
// ensures that the app doesn't break later when the below
// deprecated option gets removed post Android R
@Suppress("DEPRECATION")
mActivity.windowManager.defaultDisplay.rotation
}
val rotation = mActivity.sensorNotifier?.getSurfaceRotation() ?: Surface.ROTATION_0

if (mActivity.isDestroyed || mActivity.isFinishing) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.view.Surface
import android.view.View
import app.grapheneos.camera.ui.activities.MainActivity
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -54,7 +55,7 @@ class SensorOrientationChangeNotifier private constructor(
private const val Z_EXIT_MAX = 45F
}

var mOrientation = mainActivity.getRotation()
var mOrientation = 0
private set

private val mListeners = ArrayList<WeakReference<Listener?>>(3)
Expand All @@ -70,7 +71,7 @@ class SensorOrientationChangeNotifier private constructor(
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL
)
notifyListeners(true)
notifyListeners()
}

/**
Expand Down Expand Up @@ -160,6 +161,15 @@ class SensorOrientationChangeNotifier private constructor(
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
}

fun getSurfaceRotation() : Int {
return when(mOrientation) {
90 -> Surface.ROTATION_90
180 -> Surface.ROTATION_180
270 -> Surface.ROTATION_270
else -> Surface.ROTATION_0
}
}

fun forceUpdateGyro() {
mSensorEventListener.let {
it.updateGyro(it.lastX, it.lastZ)
Expand Down Expand Up @@ -195,11 +205,7 @@ class SensorOrientationChangeNotifier private constructor(
return null
}

fun notifyListeners(manualUpdate: Boolean = false) {

if (manualUpdate) {
mOrientation = mainActivity.getRotation()
}
fun notifyListeners() {

val deadLinksArr = ArrayList<WeakReference<Listener?>>()
for (wr in mListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1542,31 +1542,14 @@ open class MainActivity : AppCompatActivity(),
}

fun forceUpdateOrientationSensor() {
sensorNotifier?.notifyListeners(true)
sensorNotifier?.notifyListeners()
}

val sensorNotifier: SensorOrientationChangeNotifier?
get() {
return SensorOrientationChangeNotifier.getInstance(this)
}

fun getRotation(): Int {
val rotation = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
display?.rotation ?: @Suppress("DEPRECATION")
windowManager.defaultDisplay.rotation
} else {
@Suppress("DEPRECATION")
windowManager.defaultDisplay.rotation
}

return when (rotation) {
Surface.ROTATION_90 -> 270
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 90
else -> 0
}
}

fun onDeviceAngleChange(xDegrees: Float, zDegrees: Float) {

val reverseDirection = sensorNotifier?.mOrientation == 270 || sensorNotifier?.mOrientation == 180
Expand Down