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

Commit 068416ae authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Individually track vis duration of unseen notifs" into udc-qpr-dev am: f58e1e47

parents e4377a7a f58e1e47
Loading
Loading
Loading
Loading
+124 −70
Original line number Diff line number Diff line
@@ -28,12 +28,9 @@ import com.android.systemui.dump.DumpManager
import com.android.systemui.keyguard.data.repository.KeyguardRepository
import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepository
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.TransitionState
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.statusbar.StatusBarState
import com.android.systemui.statusbar.expansionChanges
import com.android.systemui.statusbar.notification.NotifPipelineFlags
import com.android.systemui.statusbar.notification.collection.NotifPipeline
import com.android.systemui.statusbar.notification.collection.NotificationEntry
import com.android.systemui.statusbar.notification.collection.coordinator.dagger.CoordinatorScope
@@ -50,30 +47,29 @@ import com.android.systemui.util.settings.SecureSettings
import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import java.io.PrintWriter
import javax.inject.Inject
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.transformLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield

/**
 * Filters low priority and privacy-sensitive notifications from the lockscreen, and hides section
 * headers on the lockscreen.
 * headers on the lockscreen. If enabled, it will also track and hide seen notifications on the
 * lockscreen.
 */
@CoordinatorScope
class KeyguardCoordinator
@@ -86,7 +82,6 @@ constructor(
    private val keyguardRepository: KeyguardRepository,
    private val keyguardTransitionRepository: KeyguardTransitionRepository,
    private val logger: KeyguardCoordinatorLogger,
    private val notifPipelineFlags: NotifPipelineFlags,
    @Application private val scope: CoroutineScope,
    private val sectionHeaderVisibilityProvider: SectionHeaderVisibilityProvider,
    private val secureSettings: SecureSettings,
@@ -95,6 +90,8 @@ constructor(
) : Coordinator, Dumpable {

    private val unseenNotifications = mutableSetOf<NotificationEntry>()
    private val unseenEntryAdded = MutableSharedFlow<NotificationEntry>(extraBufferCapacity = 1)
    private val unseenEntryRemoved = MutableSharedFlow<NotificationEntry>(extraBufferCapacity = 1)
    private var unseenFilterEnabled = false

    override fun attach(pipeline: NotifPipeline) {
@@ -109,79 +106,130 @@ constructor(
    private fun attachUnseenFilter(pipeline: NotifPipeline) {
        pipeline.addFinalizeFilter(unseenNotifFilter)
        pipeline.addCollectionListener(collectionListener)
        scope.launch { trackUnseenNotificationsWhileUnlocked() }
        scope.launch { invalidateWhenUnseenSettingChanges() }
        scope.launch { trackUnseenFilterSettingChanges() }
        dumpManager.registerDumpable(this)
    }

    private suspend fun trackUnseenNotificationsWhileUnlocked() {
        // Whether or not we're actively tracking unseen notifications to mark them as seen when
        // appropriate.
        val isTrackingUnseen: Flow<Boolean> =
            keyguardRepository.isKeyguardShowing
                // transformLatest so that we can cancel listening to keyguard transitions once
                // isKeyguardShowing changes (after a successful transition to the keyguard).
                .transformLatest { isShowing ->
                    if (isShowing) {
                        // If the keyguard is showing, we're not tracking unseen.
                        emit(false)
                    } else {
                        // If the keyguard stops showing, then start tracking unseen notifications.
                        emit(true)
                        // If the screen is turning off, stop tracking, but if that transition is
                        // cancelled, then start again.
                        emitAll(
                            keyguardTransitionRepository.transitions.map { step ->
                                !step.isScreenTurningOff
                            }
                        )
                    }
                }
                // Prevent double emit of `false` caused by transition to AOD, followed by keyguard
                // showing
    private suspend fun trackSeenNotifications() {
        // Whether or not keyguard is visible (or occluded).
        val isKeyguardPresent: Flow<Boolean> =
            keyguardTransitionRepository.transitions
                .map { step -> step.to != KeyguardState.GONE }
                .distinctUntilChanged()
                .onEach { trackingUnseen -> logger.logTrackingUnseen(trackingUnseen) }

        // Use collectLatest so that trackUnseenNotifications() is cancelled when the keyguard is
        // showing again
        var clearUnseenOnBeginTracking = false
        isTrackingUnseen.collectLatest { trackingUnseen ->
            if (!trackingUnseen) {
                // Wait for the user to spend enough time on the lock screen before clearing unseen
                // set when unlocked
                awaitTimeSpentNotDozing(SEEN_TIMEOUT)
                clearUnseenOnBeginTracking = true
                logger.logSeenOnLockscreen()
        // Separately track seen notifications while the device is locked, applying once the device
        // is unlocked.
        val notificationsSeenWhileLocked = mutableSetOf<NotificationEntry>()

        // Use [collectLatest] to cancel any running jobs when [trackingUnseen] changes.
        isKeyguardPresent.collectLatest { isKeyguardPresent: Boolean ->
            if (isKeyguardPresent) {
                // Keyguard is not gone, notifications need to be visible for a certain threshold
                // before being marked as seen
                trackSeenNotificationsWhileLocked(notificationsSeenWhileLocked)
            } else {
                if (clearUnseenOnBeginTracking) {
                    clearUnseenOnBeginTracking = false
                    logger.logAllMarkedSeenOnUnlock()
                    unseenNotifications.clear()
                // Mark all seen-while-locked notifications as seen for real.
                if (notificationsSeenWhileLocked.isNotEmpty()) {
                    unseenNotifications.removeAll(notificationsSeenWhileLocked)
                    logger.logAllMarkedSeenOnUnlock(
                        seenCount = notificationsSeenWhileLocked.size,
                        remainingUnseenCount = unseenNotifications.size
                    )
                    notificationsSeenWhileLocked.clear()
                }
                unseenNotifFilter.invalidateList("keyguard no longer showing")
                trackUnseenNotifications()
                // Keyguard is gone, notifications can be immediately marked as seen when they
                // become visible.
                trackSeenNotificationsWhileUnlocked()
            }
        }
    }

    private suspend fun awaitTimeSpentNotDozing(duration: Duration) {
        keyguardRepository.isDozing
            // Use transformLatest so that the timeout delay is cancelled if the device enters doze,
            // and is restarted when doze ends.
            .transformLatest { isDozing ->
    /**
     * Keep [notificationsSeenWhileLocked] updated to represent which notifications have actually
     * been "seen" while the device is on the keyguard.
     */
    private suspend fun trackSeenNotificationsWhileLocked(
        notificationsSeenWhileLocked: MutableSet<NotificationEntry>,
    ) = coroutineScope {
        // Remove removed notifications from the set
        launch {
            unseenEntryRemoved.collect { entry ->
                if (notificationsSeenWhileLocked.remove(entry)) {
                    logger.logRemoveSeenOnLockscreen(entry)
                }
            }
        }
        // Use collectLatest so that the timeout delay is cancelled if the device enters doze, and
        // is restarted when doze ends.
        keyguardRepository.isDozing.collectLatest { isDozing ->
            if (!isDozing) {
                    delay(duration)
                    // Signal timeout has completed
                    emit(Unit)
                trackSeenNotificationsWhileLockedAndNotDozing(notificationsSeenWhileLocked)
            }
        }
            // Suspend until the first emission
            .first()
    }

    // Track "unseen" notifications, marking them as seen when either shade is expanded or the
    /**
     * Keep [notificationsSeenWhileLocked] updated to represent which notifications have actually
     * been "seen" while the device is on the keyguard and not dozing. Any new and existing unseen
     * notifications are not marked as seen until they are visible for the [SEEN_TIMEOUT] duration.
     */
    private suspend fun trackSeenNotificationsWhileLockedAndNotDozing(
        notificationsSeenWhileLocked: MutableSet<NotificationEntry>
    ) = coroutineScope {
        // All child tracking jobs will be cancelled automatically when this is cancelled.
        val trackingJobsByEntry = mutableMapOf<NotificationEntry, Job>()

        /**
         * Wait for the user to spend enough time on the lock screen before removing notification
         * from unseen set upon unlock.
         */
        suspend fun trackSeenDurationThreshold(entry: NotificationEntry) {
            if (notificationsSeenWhileLocked.remove(entry)) {
                logger.logResetSeenOnLockscreen(entry)
            }
            delay(SEEN_TIMEOUT)
            notificationsSeenWhileLocked.add(entry)
            trackingJobsByEntry.remove(entry)
            logger.logSeenOnLockscreen(entry)
        }

        /** Stop any unseen tracking when a notification is removed. */
        suspend fun stopTrackingRemovedNotifs(): Nothing =
            unseenEntryRemoved.collect { entry ->
                trackingJobsByEntry.remove(entry)?.let {
                    it.cancel()
                    logger.logStopTrackingLockscreenSeenDuration(entry)
                }
            }

        /** Start tracking new notifications when they are posted. */
        suspend fun trackNewUnseenNotifs(): Nothing = coroutineScope {
            unseenEntryAdded.collect { entry ->
                logger.logTrackingLockscreenSeenDuration(entry)
                // If this is an update, reset the tracking.
                trackingJobsByEntry[entry]?.let {
                    it.cancel()
                    logger.logResetSeenOnLockscreen(entry)
                }
                trackingJobsByEntry[entry] = launch { trackSeenDurationThreshold(entry) }
            }
        }

        // Start tracking for all notifications that are currently unseen.
        logger.logTrackingLockscreenSeenDuration(unseenNotifications)
        unseenNotifications.forEach { entry ->
            trackingJobsByEntry[entry] = launch { trackSeenDurationThreshold(entry) }
        }

        launch { trackNewUnseenNotifs() }
        launch { stopTrackingRemovedNotifs() }
    }

    // Track "seen" notifications, marking them as such when either shade is expanded or the
    // notification becomes heads up.
    private suspend fun trackUnseenNotifications() {
    private suspend fun trackSeenNotificationsWhileUnlocked() {
        coroutineScope {
            launch { clearUnseenNotificationsWhenShadeIsExpanded() }
            launch { markHeadsUpNotificationsAsSeen() }
@@ -212,7 +260,7 @@ constructor(
        }
    }

    private suspend fun invalidateWhenUnseenSettingChanges() {
    private suspend fun trackUnseenFilterSettingChanges() {
        secureSettings
            // emit whenever the setting has changed
            .observerFlow(
@@ -228,17 +276,23 @@ constructor(
                    UserHandle.USER_CURRENT,
                ) == 1
            }
            // don't emit anything if nothing has changed
            .distinctUntilChanged()
            // perform lookups on the bg thread pool
            .flowOn(bgDispatcher)
            // only track the most recent emission, if events are happening faster than they can be
            // consumed
            .conflate()
            .collectLatest { setting ->
                // update local field and invalidate if necessary
            .collect { setting ->
                if (setting != unseenFilterEnabled) {
                    unseenFilterEnabled = setting
                    unseenNotifFilter.invalidateList("unseen setting changed")
                }
                // if the setting is enabled, then start tracking and filtering unseen notifications
                if (setting) {
                    trackSeenNotifications()
                }
            }
    }

@@ -250,6 +304,7 @@ constructor(
                ) {
                    logger.logUnseenAdded(entry.key)
                    unseenNotifications.add(entry)
                    unseenEntryAdded.tryEmit(entry)
                }
            }

@@ -259,12 +314,14 @@ constructor(
                ) {
                    logger.logUnseenUpdated(entry.key)
                    unseenNotifications.add(entry)
                    unseenEntryAdded.tryEmit(entry)
                }
            }

            override fun onEntryRemoved(entry: NotificationEntry, reason: Int) {
                if (unseenNotifications.remove(entry)) {
                    logger.logUnseenRemoved(entry.key)
                    unseenEntryRemoved.tryEmit(entry)
                }
            }
        }
@@ -347,6 +404,3 @@ constructor(
        private val SEEN_TIMEOUT = 5.seconds
    }
}

private val TransitionStep.isScreenTurningOff: Boolean
    get() = transitionState == TransitionState.STARTED && to != KeyguardState.GONE
+74 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.statusbar.notification.collection.coordinator
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.log.dagger.UnseenNotificationLog
import com.android.systemui.statusbar.notification.collection.NotificationEntry
import javax.inject.Inject

private const val TAG = "KeyguardCoordinator"
@@ -28,11 +29,14 @@ class KeyguardCoordinatorLogger
constructor(
    @UnseenNotificationLog private val buffer: LogBuffer,
) {
    fun logSeenOnLockscreen() =
    fun logSeenOnLockscreen(entry: NotificationEntry) =
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            "Notifications on lockscreen will be marked as seen when unlocked."
            messageInitializer = { str1 = entry.key },
            messagePrinter = {
                "Notification [$str1] on lockscreen will be marked as seen when unlocked."
            },
        )

    fun logTrackingUnseen(trackingUnseen: Boolean) =
@@ -43,11 +47,21 @@ constructor(
            messagePrinter = { "${if (bool1) "Start" else "Stop"} tracking unseen notifications." },
        )

    fun logAllMarkedSeenOnUnlock() =
    fun logAllMarkedSeenOnUnlock(
        seenCount: Int,
        remainingUnseenCount: Int,
    ) =
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            "Notifications have been marked as seen now that device is unlocked."
            messageInitializer = {
                int1 = seenCount
                int2 = remainingUnseenCount
            },
            messagePrinter = {
                "$int1 Notifications have been marked as seen now that device is unlocked. " +
                    "$int2 notifications remain unseen."
            },
        )

    fun logShadeExpanded() =
@@ -96,4 +110,60 @@ constructor(
            messageInitializer = { str1 = key },
            messagePrinter = { "Unseen notif has become heads up: $str1" },
        )

    fun logTrackingLockscreenSeenDuration(unseenNotifications: Set<NotificationEntry>) {
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            messageInitializer = {
                str1 = unseenNotifications.joinToString { it.key }
                int1 = unseenNotifications.size
            },
            messagePrinter = {
                "Tracking $int1 unseen notifications for lockscreen seen duration threshold: $str1"
            },
        )
    }

    fun logTrackingLockscreenSeenDuration(entry: NotificationEntry) {
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            messageInitializer = { str1 = entry.key },
            messagePrinter = {
                "Tracking new notification for lockscreen seen duration threshold: $str1"
            },
        )
    }

    fun logStopTrackingLockscreenSeenDuration(entry: NotificationEntry) {
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            messageInitializer = { str1 = entry.key },
            messagePrinter = {
                "Stop tracking removed notification for lockscreen seen duration threshold: $str1"
            },
        )
    }

    fun logResetSeenOnLockscreen(entry: NotificationEntry) {
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            messageInitializer = { str1 = entry.key },
            messagePrinter = {
                "Reset tracking updated notification for lockscreen seen duration threshold: $str1"
            },
        )
    }

    fun logRemoveSeenOnLockscreen(entry: NotificationEntry) {
        buffer.log(
            TAG,
            LogLevel.DEBUG,
            messageInitializer = { str1 = entry.key },
            messagePrinter = { "Notification marked as seen on lockscreen removed: $str1" },
        )
    }
}
+258 −39

File changed.

Preview size limit exceeded, changes collapsed.