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

Commit 7088fab5 authored by Fengjiang Li's avatar Fengjiang Li
Browse files

Make RotationChangeProvider Threadsafe

Follow up of ag/27767415, lastRotation field is accessed from different thread so should use AtomicInteger

Bug: 344626706
Test: RotationChangeProviderTest
Flag: NONE bugfix
Change-Id: Ide5f62028cc29b595cb0266e664d051a27c1263c
parent 0bf84c39
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 {