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

Commit 71c72498 authored by Catherine Liang's avatar Catherine Liang
Browse files

Fix workspace callback binder lifecycle (2/2)

Every time the app is paused and resumed, the workspace callback binder
would launch new coroutines, even though the old coroutine scope was not
yet cancelled, resulting in many extra messages being sent to Launcher
and Keyguard. This is because the workspace callback binder is launching
coroutines using the fragment lifecycle start and stop, even though it is
binded in the surfaceCreated function. Adjust to follow the surface's
lifecycle.

Also adjust to use view lifecycle in the fragment where appropriate.

Flag: com.android.systemui.shared.new_customization_picker_ui
Bug: 384765903
Test: manually verified with print statements
Change-Id: Ie965c643c9a1e65dd9d0053f401f702a875ddd95
parent 8a746a73
Loading
Loading
Loading
Loading
+115 −130
Original line number Diff line number Diff line
@@ -19,10 +19,6 @@ package com.android.wallpaper.picker.common.preview.ui.binder
import android.os.Bundle
import android.os.Message
import androidx.core.os.bundleOf
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.android.customization.model.grid.DefaultShapeGridManager.Companion.COL_GRID_NAME
import com.android.customization.model.grid.DefaultShapeGridManager.Companion.COL_SHAPE_KEY
import com.android.customization.picker.clock.shared.ClockSize
@@ -51,6 +47,7 @@ import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewMo
import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationOptionsViewModel
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch

@@ -62,12 +59,11 @@ constructor(
    private val materialColorsGenerator: MaterialColorsGenerator,
) : WorkspaceCallbackBinder {

    override fun bind(
    override suspend fun bind(
        workspaceCallback: Message,
        viewModel: CustomizationOptionsViewModel,
        colorUpdateViewModel: ColorUpdateViewModel,
        screen: Screen,
        lifecycleOwner: LifecycleOwner,
        clockViewFactory: ClockViewFactory,
    ) {
        defaultWorkspaceCallbackBinder.bind(
@@ -75,7 +71,6 @@ constructor(
            viewModel = viewModel,
            colorUpdateViewModel = colorUpdateViewModel,
            screen = screen,
            lifecycleOwner = lifecycleOwner,
            clockViewFactory = clockViewFactory,
        )

@@ -87,8 +82,7 @@ constructor(

        when (screen) {
            Screen.LOCK_SCREEN ->
                lifecycleOwner.lifecycleScope.launch {
                    lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                coroutineScope {
                    launch {
                        viewModel.selectedOption.collect {
                            when (it) {
@@ -112,8 +106,7 @@ constructor(
                    }

                    launch {
                            viewModel.keyguardQuickAffordancePickerViewModel2.selectedSlotId
                                .collect {
                        viewModel.keyguardQuickAffordancePickerViewModel2.selectedSlotId.collect {
                            workspaceCallback.sendMessage(
                                MESSAGE_ID_SLOT_SELECTED,
                                Bundle().apply { putString(KEY_SLOT_ID, it) },
@@ -122,8 +115,7 @@ constructor(
                    }

                    launch {
                            viewModel.keyguardQuickAffordancePickerViewModel2
                                .previewingQuickAffordances
                        viewModel.keyguardQuickAffordancePickerViewModel2.previewingQuickAffordances
                            .collect {
                                it[SLOT_ID_BOTTOM_START]?.let {
                                    workspaceCallback.sendMessage(
@@ -154,16 +146,12 @@ constructor(
                            )
                            .collect { (previewingClock, previewingClockSize) ->
                                val hideSmartspace =
                                        clockViewFactory
                                            .getController(previewingClock.clockId)
                                            .let {
                                    clockViewFactory.getController(previewingClock.clockId).let {
                                        when (previewingClockSize) {
                                            ClockSize.DYNAMIC ->
                                                        it.largeClock.config
                                                            .hasCustomWeatherDataDisplay
                                                it.largeClock.config.hasCustomWeatherDataDisplay
                                            ClockSize.SMALL ->
                                                        it.smallClock.config
                                                            .hasCustomWeatherDataDisplay
                                                it.smallClock.config.hasCustomWeatherDataDisplay
                                        }
                                    }
                                workspaceCallback.sendMessage(
@@ -188,10 +176,8 @@ constructor(
                            }
                    }
                }
                }
            Screen.HOME_SCREEN ->
                lifecycleOwner.lifecycleScope.launch {
                    lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                coroutineScope {
                    launch {
                        viewModel.shapeGridPickerViewModel.previewingShapeKey.collect {
                            workspaceCallback.sendMessage(
@@ -239,7 +225,6 @@ constructor(
                }
        }
    }
    }

    companion object {
        const val MESSAGE_ID_UPDATE_SHAPE = 2586