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

Commit e84b9d74 authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Android (Google) Code Review
Browse files

Merge "Start ControlsControllerImpl on the correct user" into rvc-dev

parents 37d85827 574a1e40
Loading
Loading
Loading
Loading
+38 −23
Original line number Diff line number Diff line
@@ -89,35 +89,36 @@ class ControlsControllerImpl @Inject constructor (
            contentResolver, CONTROLS_AVAILABLE, DEFAULT_ENABLED, currentUserId) != 0
        private set

    private var file = Environment.buildPath(
        context.filesDir,
        ControlsFavoritePersistenceWrapper.FILE_NAME
    )
    private var auxiliaryFile = Environment.buildPath(
        context.filesDir,
        AuxiliaryPersistenceWrapper.AUXILIARY_FILE_NAME
    )
    private val persistenceWrapper = optionalWrapper.orElseGet {
    private val persistenceWrapper: ControlsFavoritePersistenceWrapper
    @VisibleForTesting
    internal var auxiliaryPersistenceWrapper: AuxiliaryPersistenceWrapper

    init {
        val userStructure = UserStructure(context, currentUser)

        persistenceWrapper = optionalWrapper.orElseGet {
            ControlsFavoritePersistenceWrapper(
            file,
                    userStructure.file,
                    executor,
            BackupManager(context)
                    BackupManager(userStructure.userContext)
            )
        }

    @VisibleForTesting
    internal var auxiliaryPersistenceWrapper = AuxiliaryPersistenceWrapper(auxiliaryFile, executor)
        auxiliaryPersistenceWrapper = AuxiliaryPersistenceWrapper(
                userStructure.auxiliaryFile,
                executor
        )
    }

    private fun setValuesForUser(newUser: UserHandle) {
        Log.d(TAG, "Changing to user: $newUser")
        currentUser = newUser
        val userContext = context.createContextAsUser(currentUser, 0)
        file = Environment.buildPath(
                userContext.filesDir, ControlsFavoritePersistenceWrapper.FILE_NAME)
        auxiliaryFile = Environment.buildPath(
            userContext.filesDir, AuxiliaryPersistenceWrapper.AUXILIARY_FILE_NAME)
        persistenceWrapper.changeFileAndBackupManager(file, BackupManager(userContext))
        auxiliaryPersistenceWrapper.changeFile(auxiliaryFile)
        val userStructure = UserStructure(context, currentUser)
        persistenceWrapper.changeFileAndBackupManager(
                userStructure.file,
                BackupManager(userStructure.userContext)
        )
        auxiliaryPersistenceWrapper.changeFile(userStructure.auxiliaryFile)
        available = Settings.Secure.getIntForUser(contentResolver, CONTROLS_AVAILABLE,
                DEFAULT_ENABLED, newUser.identifier) != 0
        resetFavorites(available)
@@ -564,6 +565,20 @@ class ControlsControllerImpl @Inject constructor (
    }
}

class UserStructure(context: Context, user: UserHandle) {
    val userContext = context.createContextAsUser(user, 0)

    val file = Environment.buildPath(
            context.filesDir,
            ControlsFavoritePersistenceWrapper.FILE_NAME
    )

    val auxiliaryFile = Environment.buildPath(
            context.filesDir,
            AuxiliaryPersistenceWrapper.AUXILIARY_FILE_NAME
    )
}

/**
 * Relies on immutable data for thread safety. When necessary to update favMap, use reassignment to
 * replace it, which will not disrupt any ongoing map traversal.