Skip to content

Commit 1fbe47d

Browse files
committed
Replace orientation API calls with sensor calculated orientation
The current APIs we use are deprecated/always return 0 since the orientation is fixed for main activity. The rationale behind using accelerometer to reliably calculate orientation is that gravity constantly has small impacts on the accelerometer causing it to continuously generate new values on a real device on start
1 parent b7eb89b commit 1fbe47d

3 files changed

Lines changed: 16 additions & 36 deletions

File tree

app/src/main/java/app/grapheneos/camera/CamConfig.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.provider.MediaStore
99
import android.util.Log
1010
import android.util.Size
1111
import android.view.MotionEvent
12+
import android.view.Surface
1213
import android.view.View
1314
import android.view.animation.AlphaAnimation
1415
import android.view.animation.Animation
@@ -1061,17 +1062,7 @@ class CamConfig(private val mActivity: MainActivity) {
10611062
mActivity.exposureBar.hidePanel()
10621063
modePref = mActivity.getSharedPreferences(currentMode.name, Context.MODE_PRIVATE)
10631064

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

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

app/src/main/java/app/grapheneos/camera/notifier/SensorOrientationChangeNotifier.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.hardware.Sensor
55
import android.hardware.SensorEvent
66
import android.hardware.SensorEventListener
77
import android.hardware.SensorManager
8+
import android.view.Surface
89
import android.view.View
910
import app.grapheneos.camera.ui.activities.MainActivity
1011
import java.lang.ref.WeakReference
@@ -54,7 +55,7 @@ class SensorOrientationChangeNotifier private constructor(
5455
private const val Z_EXIT_MAX = 45F
5556
}
5657

57-
var mOrientation = mainActivity.getRotation()
58+
var mOrientation = 0
5859
private set
5960

6061
private val mListeners = ArrayList<WeakReference<Listener?>>(3)
@@ -70,7 +71,7 @@ class SensorOrientationChangeNotifier private constructor(
7071
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
7172
SensorManager.SENSOR_DELAY_NORMAL
7273
)
73-
notifyListeners(true)
74+
notifyListeners()
7475
}
7576

7677
/**
@@ -160,6 +161,15 @@ class SensorOrientationChangeNotifier private constructor(
160161
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
161162
}
162163

164+
fun getSurfaceRotation() : Int {
165+
return when(mOrientation) {
166+
90 -> Surface.ROTATION_90
167+
180 -> Surface.ROTATION_180
168+
270 -> Surface.ROTATION_270
169+
else -> Surface.ROTATION_0
170+
}
171+
}
172+
163173
fun forceUpdateGyro() {
164174
mSensorEventListener.let {
165175
it.updateGyro(it.lastX, it.lastZ)
@@ -195,11 +205,7 @@ class SensorOrientationChangeNotifier private constructor(
195205
return null
196206
}
197207

198-
fun notifyListeners(manualUpdate: Boolean = false) {
199-
200-
if (manualUpdate) {
201-
mOrientation = mainActivity.getRotation()
202-
}
208+
fun notifyListeners() {
203209

204210
val deadLinksArr = ArrayList<WeakReference<Listener?>>()
205211
for (wr in mListeners) {

app/src/main/java/app/grapheneos/camera/ui/activities/MainActivity.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,31 +1542,14 @@ open class MainActivity : AppCompatActivity(),
15421542
}
15431543

15441544
fun forceUpdateOrientationSensor() {
1545-
sensorNotifier?.notifyListeners(true)
1545+
sensorNotifier?.notifyListeners()
15461546
}
15471547

15481548
val sensorNotifier: SensorOrientationChangeNotifier?
15491549
get() {
15501550
return SensorOrientationChangeNotifier.getInstance(this)
15511551
}
15521552

1553-
fun getRotation(): Int {
1554-
val rotation = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
1555-
display?.rotation ?: @Suppress("DEPRECATION")
1556-
windowManager.defaultDisplay.rotation
1557-
} else {
1558-
@Suppress("DEPRECATION")
1559-
windowManager.defaultDisplay.rotation
1560-
}
1561-
1562-
return when (rotation) {
1563-
Surface.ROTATION_90 -> 270
1564-
Surface.ROTATION_180 -> 180
1565-
Surface.ROTATION_270 -> 90
1566-
else -> 0
1567-
}
1568-
}
1569-
15701553
fun onDeviceAngleChange(xDegrees: Float, zDegrees: Float) {
15711554

15721555
val reverseDirection = sensorNotifier?.mOrientation == 270 || sensorNotifier?.mOrientation == 180

0 commit comments

Comments
 (0)