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

Commit 796d3ffc authored by Alexander Roederer's avatar Alexander Roederer
Browse files

Run ktfmt on RemoteInputCoordinator

This will help simplify future changes to this file.

Bug: 360090006
Test: atest RemoteInputCoordinatorTest
Flag: EXEMPT refactor ktfmt
Change-Id: I20f8a977d7903fc9903265bf02681d782aacc57c
parent 435ec085
Loading
Loading
Loading
Loading
+119 −89
Original line number Diff line number Diff line
@@ -45,17 +45,16 @@ private const val TAG = "RemoteInputCoordinator"

/**
 * How long to wait before auto-dismissing a notification that was kept for active remote input, and
 * has now sent a remote input. We auto-dismiss, because the app may not cannot cancel
 * these given that they technically don't exist anymore. We wait a bit in case the app issues
 * an update, and to also give the other lifetime extenders a beat to decide they want it.
 * has now sent a remote input. We auto-dismiss, because the app may not cannot cancel these given
 * that they technically don't exist anymore. We wait a bit in case the app issues an update, and to
 * also give the other lifetime extenders a beat to decide they want it.
 */
private const val REMOTE_INPUT_ACTIVE_EXTENDER_AUTO_CANCEL_DELAY: Long = 500

/**
 * How long to wait before releasing a lifetime extension when requested to do so due to a user
 * interaction (such as tapping another action).
 * We wait a bit in case the app issues an update in response to the action, but not too long or we
 * risk appearing unresponsive to the user.
 * interaction (such as tapping another action). We wait a bit in case the app issues an update in
 * response to the action, but not too long or we risk appearing unresponsive to the user.
 */
private const val REMOTE_INPUT_EXTENDER_RELEASE_DELAY: Long = 200

@@ -63,22 +62,21 @@ private const val REMOTE_INPUT_EXTENDER_RELEASE_DELAY: Long = 200
private val DEBUG: Boolean by lazy { Log.isLoggable(TAG, Log.DEBUG) }

@CoordinatorScope
class RemoteInputCoordinator @Inject constructor(
class RemoteInputCoordinator
@Inject
constructor(
    dumpManager: DumpManager,
    private val mRebuilder: RemoteInputNotificationRebuilder,
    private val mNotificationRemoteInputManager: NotificationRemoteInputManager,
    @Main private val mMainHandler: Handler,
    private val mSmartReplyController: SmartReplyController
    private val mSmartReplyController: SmartReplyController,
) : Coordinator, RemoteInputListener, Dumpable {

    @VisibleForTesting val mRemoteInputHistoryExtender = RemoteInputHistoryExtender()
    @VisibleForTesting val mSmartReplyHistoryExtender = SmartReplyHistoryExtender()
    @VisibleForTesting val mRemoteInputActiveExtender = RemoteInputActiveExtender()
    private val mRemoteInputLifetimeExtenders = listOf(
            mRemoteInputHistoryExtender,
            mSmartReplyHistoryExtender,
            mRemoteInputActiveExtender
    )
    private val mRemoteInputLifetimeExtenders =
        listOf(mRemoteInputHistoryExtender, mSmartReplyHistoryExtender, mRemoteInputActiveExtender)

    private lateinit var mNotifUpdater: InternalNotifUpdater

@@ -93,9 +91,7 @@ class RemoteInputCoordinator @Inject constructor(
        if (lifetimeExtensionRefactor()) {
            pipeline.addNotificationLifetimeExtender(mRemoteInputActiveExtender)
        } else {
            mRemoteInputLifetimeExtenders.forEach {
                pipeline.addNotificationLifetimeExtender(it)
            }
            mRemoteInputLifetimeExtenders.forEach { pipeline.addNotificationLifetimeExtender(it) }
        }
        mNotifUpdater = pipeline.getInternalNotifUpdater(TAG)
        pipeline.addCollectionListener(mCollectionListener)
@@ -105,37 +101,56 @@ class RemoteInputCoordinator @Inject constructor(
     * Listener that updates the appearance of the notification if it has been lifetime extended
     * by a a direct reply or a smart reply, and cancelled.
     */
    val mCollectionListener = object : NotifCollectionListener {
    val mCollectionListener =
        object : NotifCollectionListener {
            override fun onEntryUpdated(entry: NotificationEntry, fromSystem: Boolean) {
                if (DEBUG) {
                Log.d(TAG, "mCollectionListener.onEntryUpdated(entry=${entry.key}," +
                        " fromSystem=$fromSystem)")
                    Log.d(
                        TAG,
                        "mCollectionListener.onEntryUpdated(entry=${entry.key}," +
                            " fromSystem=$fromSystem)",
                    )
                }
                if (fromSystem) {
                    if (lifetimeExtensionRefactor()) {
                    if ((entry.getSbn().getNotification().flags
                                    and FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY) > 0) {
                        if (mNotificationRemoteInputManager.shouldKeepForRemoteInputHistory(
                                        entry)) {
                        if (
                            (entry.getSbn().getNotification().flags and
                                FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY) > 0
                        ) {
                            if (
                                mNotificationRemoteInputManager.shouldKeepForRemoteInputHistory(
                                    entry
                                )
                            ) {
                                val newSbn = mRebuilder.rebuildForRemoteInputReply(entry)
                                entry.onRemoteInputInserted()
                            mNotifUpdater.onInternalNotificationUpdate(newSbn,
                                    "Extending lifetime of notification with remote input")
                        } else if (mNotificationRemoteInputManager.shouldKeepForSmartReplyHistory(
                                        entry)) {
                                mNotifUpdater.onInternalNotificationUpdate(
                                    newSbn,
                                    "Extending lifetime of notification with remote input",
                                )
                            } else if (
                                mNotificationRemoteInputManager.shouldKeepForSmartReplyHistory(
                                    entry
                                )
                            ) {
                                val newSbn = mRebuilder.rebuildForCanceledSmartReplies(entry)
                                mSmartReplyController.stopSending(entry)
                            mNotifUpdater.onInternalNotificationUpdate(newSbn,
                                    "Extending lifetime of notification with smart reply")
                                mNotifUpdater.onInternalNotificationUpdate(
                                    newSbn,
                                    "Extending lifetime of notification with smart reply",
                                )
                            } else {
                                // The app may have re-cancelled a notification after it had already
                                // been lifetime extended.
                            // Rebuild the notification with the replies it already had to ensure
                                // Rebuild the notification with the replies it already had to
                                // ensure
                                // those replies continue to be displayed.
                                val newSbn = mRebuilder.rebuildWithExistingReplies(entry)
                            mNotifUpdater.onInternalNotificationUpdate(newSbn,
                                mNotifUpdater.onInternalNotificationUpdate(
                                    newSbn,
                                    "Extending lifetime of notification that has already been " +
                                            "lifetime extended.")
                                        "lifetime extended.",
                                )
                            }
                        } else {
                            // Notifications updated without FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY
@@ -153,10 +168,12 @@ class RemoteInputCoordinator @Inject constructor(
            override fun onEntryRemoved(entry: NotificationEntry, reason: Int) {
                if (DEBUG) Log.d(TAG, "mCollectionListener.onEntryRemoved(entry=${entry.key})")
                // We're removing the notification, the smart reply controller can forget about it.
            // TODO(b/145659174): track 'sending' state on the entry to avoid having to clear it.
                // TODO(b/145659174): track 'sending' state on the entry to avoid having to clear
                // it.
                mSmartReplyController.stopSending(entry)

            // When we know the entry will not be lifetime extended, clean up the remote input view
                // When we know the entry will not be lifetime extended, clean up the remote input
                // view
                // TODO: Share code with NotifCollection.cannotBeLifetimeExtended
                if (reason == REASON_CANCEL || reason == REASON_CLICK) {
                    mNotificationRemoteInputManager.cleanUpRemoteInputForUserRemoval(entry)
@@ -183,22 +200,25 @@ class RemoteInputCoordinator @Inject constructor(
        // view it is already canceled, so we'll need to cancel it on the apps behalf
        // now that a reply has been sent. However, delay so that the app has time to posts an
        // update in the mean time, and to give another lifetime extender time to pick it up.
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(entry.key,
                REMOTE_INPUT_ACTIVE_EXTENDER_AUTO_CANCEL_DELAY)
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(
            entry.key,
            REMOTE_INPUT_ACTIVE_EXTENDER_AUTO_CANCEL_DELAY,
        )
    }

    private fun onSmartReplySent(entry: NotificationEntry, reply: CharSequence) {
        if (DEBUG) Log.d(TAG, "onSmartReplySent(entry=${entry.key})")
        val newSbn = mRebuilder.rebuildForSendingSmartReply(entry, reply)
        mNotifUpdater.onInternalNotificationUpdate(newSbn,
                "Adding smart reply spinner for sent")
        mNotifUpdater.onInternalNotificationUpdate(newSbn, "Adding smart reply spinner for sent")

        // If we're extending for remote input being active, then from the apps point of
        // view it is already canceled, so we'll need to cancel it on the apps behalf
        // now that a reply has been sent. However, delay so that the app has time to posts an
        // update in the mean time, and to give another lifetime extender time to pick it up.
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(entry.key,
                REMOTE_INPUT_ACTIVE_EXTENDER_AUTO_CANCEL_DELAY)
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(
            entry.key,
            REMOTE_INPUT_ACTIVE_EXTENDER_AUTO_CANCEL_DELAY,
        )
    }

    override fun onPanelCollapsed() {
@@ -214,13 +234,19 @@ class RemoteInputCoordinator @Inject constructor(
    override fun releaseNotificationIfKeptForRemoteInputHistory(entry: NotificationEntry) {
        if (DEBUG) Log.d(TAG, "releaseNotificationIfKeptForRemoteInputHistory(entry=${entry.key})")
        if (!lifetimeExtensionRefactor()) {
            mRemoteInputHistoryExtender.endLifetimeExtensionAfterDelay(entry.key,
                    REMOTE_INPUT_EXTENDER_RELEASE_DELAY)
            mSmartReplyHistoryExtender.endLifetimeExtensionAfterDelay(entry.key,
                    REMOTE_INPUT_EXTENDER_RELEASE_DELAY)
            mRemoteInputHistoryExtender.endLifetimeExtensionAfterDelay(
                entry.key,
                REMOTE_INPUT_EXTENDER_RELEASE_DELAY,
            )
            mSmartReplyHistoryExtender.endLifetimeExtensionAfterDelay(
                entry.key,
                REMOTE_INPUT_EXTENDER_RELEASE_DELAY,
            )
        }
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(entry.key,
                REMOTE_INPUT_EXTENDER_RELEASE_DELAY)
        mRemoteInputActiveExtender.endLifetimeExtensionAfterDelay(
            entry.key,
            REMOTE_INPUT_EXTENDER_RELEASE_DELAY,
        )
    }

    override fun setRemoteInputController(remoteInputController: RemoteInputController) {
@@ -237,8 +263,10 @@ class RemoteInputCoordinator @Inject constructor(
        override fun onStartedLifetimeExtension(entry: NotificationEntry) {
            val newSbn = mRebuilder.rebuildForRemoteInputReply(entry)
            entry.onRemoteInputInserted()
            mNotifUpdater.onInternalNotificationUpdate(newSbn,
                    "Extending lifetime of notification with remote input")
            mNotifUpdater.onInternalNotificationUpdate(
                newSbn,
                "Extending lifetime of notification with remote input",
            )
            // TODO: Check if the entry was removed due perhaps to an inflation exception?
        }
    }
@@ -253,8 +281,10 @@ class RemoteInputCoordinator @Inject constructor(
        override fun onStartedLifetimeExtension(entry: NotificationEntry) {
            val newSbn = mRebuilder.rebuildForCanceledSmartReplies(entry)
            mSmartReplyController.stopSending(entry)
            mNotifUpdater.onInternalNotificationUpdate(newSbn,
                    "Extending lifetime of notification with smart reply")
            mNotifUpdater.onInternalNotificationUpdate(
                newSbn,
                "Extending lifetime of notification with smart reply",
            )
            // TODO: Check if the entry was removed due perhaps to an inflation exception?
        }