Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8ca89787 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Make RotationChangeProvider Threadsafe" into main

parents c7b2bc3e 7088fab5
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicInteger

private const val INVALID_ROTATION = -1

/**
 * Allows to subscribe to rotation changes. Updates are provided for the display associated to
@@ -45,7 +48,7 @@ constructor(
    private val listeners = CopyOnWriteArrayList<RotationListener>()

    private val displayListener = RotationDisplayListener()
    private var lastRotation: Int? = null
    private val lastRotation = AtomicInteger(INVALID_ROTATION)

    override fun addCallback(listener: RotationListener) {
        bgHandler.post {
@@ -61,7 +64,7 @@ constructor(
            listeners -= listener
            if (listeners.isEmpty()) {
                unsubscribeToRotation()
                lastRotation = null
                lastRotation.set(INVALID_ROTATION)
            }
        }
    }
@@ -100,9 +103,8 @@ constructor(

                if (displayId == display.displayId) {
                    val currentRotation = display.rotation
                    if (lastRotation == null || lastRotation != currentRotation) {
                    if (lastRotation.compareAndSet(lastRotation.get(), currentRotation)) {
                        listeners.forEach { it.onRotationChanged(currentRotation) }
                        lastRotation = currentRotation
                    }
                }
            } finally {