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

Commit 63100949 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[SB][Screen Chips] Finish adding logs to repo, interactor, and VM flows.

Bug: 351785188
Bug: 332662551
Flag: com.android.systemui.status_bar_screen_sharing_chips

Test: Dump `MediaRouter` log buffer, make sure it's readable
Test: Dump `StatusBarChips` log buffer, make sure it's readable and has
all the info
Test: atest MediaRouterRepositoryTest

Change-Id: I2b192ee8a01be5133b5da63677693fce6fb6c185
parent 76a10167
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.mediarouter

import javax.inject.Qualifier

/** Logs for events related to MediaRouter APIs. */
@Qualifier
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class MediaRouterLog
+13 −0
Original line number Diff line number Diff line
@@ -16,12 +16,25 @@

package com.android.systemui.mediarouter

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.LogBufferFactory
import com.android.systemui.mediarouter.data.repository.MediaRouterRepository
import com.android.systemui.mediarouter.data.repository.MediaRouterRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.Provides

@Module
interface MediaRouterModule {
    @Binds fun mediaRouterRepository(impl: MediaRouterRepositoryImpl): MediaRouterRepository

    companion object {
        @Provides
        @SysUISingleton
        @MediaRouterLog
        fun provideMediaRouterLogBuffer(factory: LogBufferFactory): LogBuffer {
            return factory.create("MediaRouter", 50)
        }
    }
}
+20 −8
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ package com.android.systemui.mediarouter.data.repository

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.mediarouter.MediaRouterLog
import com.android.systemui.statusbar.policy.CastController
import com.android.systemui.statusbar.policy.CastDevice
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
@@ -26,6 +29,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn

/** A repository for data coming from MediaRouter APIs. */
@@ -43,23 +49,29 @@ class MediaRouterRepositoryImpl
constructor(
    @Application private val scope: CoroutineScope,
    private val castController: CastController,
    @MediaRouterLog private val logger: LogBuffer,
) : MediaRouterRepository {
    override val castDevices: StateFlow<List<CastDevice>> =
        conflatedCallbackFlow {
                val callback =
                    CastController.Callback {
                        val mediaRouterCastDevices =
                            castController.castDevices.filter {
                                it.origin == CastDevice.CastOrigin.MediaRouter
                            }
                        trySend(mediaRouterCastDevices)
                    }
                val callback = CastController.Callback { trySend(castController.castDevices) }
                castController.addCallback(callback)
                awaitClose { castController.removeCallback(callback) }
            }
            // The CastController.Callback is pretty noisy and sends the same values multiple times
            // in a row, so use a distinctUntilChanged before logging.
            .distinctUntilChanged()
            .onEach { allDevices ->
                val logString = allDevices.map { it.shortLogString }.toString()
                logger.log(TAG, LogLevel.INFO, { str1 = logString }, { "All cast devices: $str1" })
            }
            .map { it.filter { device -> device.origin == CastDevice.CastOrigin.MediaRouter } }
            .stateIn(scope, SharingStarted.WhileSubscribed(), emptyList())

    override fun stopCasting(device: CastDevice) {
        castController.stopCasting(device)
    }

    companion object {
        private const val TAG = "MediaRouterRepo"
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@ package com.android.systemui.statusbar.chips.casttootherdevice.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.mediarouter.data.repository.MediaRouterRepository
import com.android.systemui.statusbar.chips.StatusBarChipsLog
import com.android.systemui.statusbar.chips.casttootherdevice.domain.model.MediaRouterCastModel
import com.android.systemui.statusbar.policy.CastDevice
import javax.inject.Inject
@@ -38,6 +41,7 @@ class MediaRouterChipInteractor
constructor(
    @Application private val scope: CoroutineScope,
    private val mediaRouterRepository: MediaRouterRepository,
    @StatusBarChipsLog private val logger: LogBuffer,
) {
    private val activeCastDevice: StateFlow<CastDevice?> =
        mediaRouterRepository.castDevices
@@ -49,8 +53,10 @@ constructor(
        activeCastDevice
            .map {
                if (it != null) {
                    logger.log(TAG, LogLevel.INFO, { str1 = it.name }, { "State: Casting($str1)" })
                    MediaRouterCastModel.Casting(deviceName = it.name)
                } else {
                    logger.log(TAG, LogLevel.INFO, {}, { "State: DoingNothing" })
                    MediaRouterCastModel.DoingNothing
                }
            }
@@ -60,4 +66,8 @@ constructor(
    fun stopCasting() {
        activeCastDevice.value?.let { mediaRouterRepository.stopCasting(it) }
    }

    companion object {
        private const val TAG = "MediaRouter"
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -22,7 +22,10 @@ import com.android.systemui.common.shared.model.ContentDescription
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.res.R
import com.android.systemui.statusbar.chips.StatusBarChipsLog
import com.android.systemui.statusbar.chips.casttootherdevice.domain.interactor.MediaRouterChipInteractor
import com.android.systemui.statusbar.chips.casttootherdevice.domain.model.MediaRouterCastModel
import com.android.systemui.statusbar.chips.casttootherdevice.ui.view.EndCastScreenToOtherDeviceDialogDelegate
@@ -58,6 +61,7 @@ constructor(
    private val mediaRouterChipInteractor: MediaRouterChipInteractor,
    private val systemClock: SystemClock,
    private val endMediaProjectionDialogHelper: EndMediaProjectionDialogHelper,
    @StatusBarChipsLog private val logger: LogBuffer,
) : OngoingActivityChipViewModel {
    /**
     * The cast chip to show, based only on MediaProjection API events.
@@ -123,6 +127,16 @@ constructor(

    override val chip: StateFlow<OngoingActivityChipModel> =
        combine(projectionChip, routerChip) { projection, router ->
                logger.log(
                    TAG,
                    LogLevel.INFO,
                    {
                        str1 = projection.logName
                        str2 = router.logName
                    },
                    { "projectionChip=$str1 > routerChip=$str2" }
                )

                // A consequence of b/269975671 is that MediaRouter and MediaProjection APIs fire at
                // different times when *screen* casting:
                //
@@ -212,5 +226,6 @@ constructor(

    companion object {
        @DrawableRes val CAST_TO_OTHER_DEVICE_ICON = R.drawable.ic_cast_connected
        private const val TAG = "CastToOtherVM"
    }
}
Loading