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

Commit 9560c9a6 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "New Pipeline: Implement lifetime extension for guts"

parents 48a64d21 d5b52e01
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -523,7 +523,9 @@ public class NotifCollection implements Dumpable {
        }
    }

    private void onEndLifetimeExtension(NotifLifetimeExtender extender, NotificationEntry entry) {
    private void onEndLifetimeExtension(
            @NonNull NotifLifetimeExtender extender,
            @NonNull NotificationEntry entry) {
        Assert.isMainThread();
        if (!mAttached) {
            return;
+126 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.statusbar.notification.collection.coordinator

import android.util.ArraySet
import com.android.systemui.Dumpable
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dump.DumpManager
import com.android.systemui.statusbar.notification.collection.ListEntry
import com.android.systemui.statusbar.notification.collection.NotifPipeline
import com.android.systemui.statusbar.notification.collection.NotificationEntry
import com.android.systemui.statusbar.notification.collection.notifcollection.NotifLifetimeExtender
import com.android.systemui.statusbar.notification.collection.notifcollection.NotifLifetimeExtender.OnEndLifetimeExtensionCallback
import com.android.systemui.statusbar.notification.row.NotificationGuts
import com.android.systemui.statusbar.notification.row.NotificationGutsManager
import com.android.systemui.statusbar.notification.collection.render.NotifGutsViewListener
import com.android.systemui.statusbar.notification.collection.render.NotifGutsViewManager
import java.io.FileDescriptor
import java.io.PrintWriter
import javax.inject.Inject

private const val TAG = "GutsCoordinator"

/**
 * Coordinates the guts displayed by the [NotificationGutsManager] with the pipeline.
 * Specifically, this just adds the lifetime extension necessary to keep guts from disappearing.
 */
@SysUISingleton
class GutsCoordinator @Inject constructor(
    private val notifGutsViewManager: NotifGutsViewManager,
    private val logger: GutsCoordinatorLogger,
    dumpManager: DumpManager
) : Coordinator, Dumpable {

    /** Keys of any Notifications for which we've been told the guts are open  */
    private val notifsWithOpenGuts = ArraySet<String>()

    /** Keys of any Notifications we've extended the lifetime for, based on guts  */
    private val notifsExtendingLifetime = ArraySet<String>()

    /** Callback for ending lifetime extension  */
    private var onEndLifetimeExtensionCallback: OnEndLifetimeExtensionCallback? = null

    init {
        dumpManager.registerDumpable(TAG, this)
    }

    override fun attach(pipeline: NotifPipeline) {
        notifGutsViewManager.setGutsListener(mGutsListener)
        pipeline.addNotificationLifetimeExtender(mLifetimeExtender)
    }

    override fun dump(fd: FileDescriptor, pw: PrintWriter, args: Array<String>) {
        pw.println("  notifsWithOpenGuts: ${notifsWithOpenGuts.size}")
        for (key in notifsWithOpenGuts) {
            pw.println("   * $key")
        }
        pw.println("  notifsExtendingLifetime: ${notifsExtendingLifetime.size}")
        for (key in notifsExtendingLifetime) {
            pw.println("   * $key")
        }
        pw.println("  onEndLifetimeExtensionCallback: $onEndLifetimeExtensionCallback")
    }

    private val mLifetimeExtender: NotifLifetimeExtender = object : NotifLifetimeExtender {
        override fun getName(): String {
            return TAG
        }

        override fun setCallback(callback: OnEndLifetimeExtensionCallback) {
            onEndLifetimeExtensionCallback = callback
        }

        override fun shouldExtendLifetime(entry: NotificationEntry, reason: Int): Boolean {
            val isShowingGuts = isCurrentlyShowingGuts(entry)
            if (isShowingGuts) {
                notifsExtendingLifetime.add(entry.key)
            }
            return isShowingGuts
        }

        override fun cancelLifetimeExtension(entry: NotificationEntry) {
            notifsExtendingLifetime.remove(entry.key)
        }
    }

    private val mGutsListener: NotifGutsViewListener = object : NotifGutsViewListener {
        override fun onGutsOpen(entry: NotificationEntry, guts: NotificationGuts) {
            logger.logGutsOpened(entry.key, guts)
            if (guts.isLeavebehind) {
                // leave-behind guts should not extend the lifetime of the notification
                closeGutsAndEndLifetimeExtension(entry)
            } else {
                notifsWithOpenGuts.add(entry.key)
            }
        }

        override fun onGutsClose(entry: NotificationEntry) {
            logger.logGutsClosed(entry.key)
            closeGutsAndEndLifetimeExtension(entry)
        }
    }

    private fun isCurrentlyShowingGuts(entry: ListEntry) =
            notifsWithOpenGuts.contains(entry.key)

    private fun closeGutsAndEndLifetimeExtension(entry: NotificationEntry) {
        notifsWithOpenGuts.remove(entry.key)
        if (notifsExtendingLifetime.remove(entry.key)) {
            onEndLifetimeExtensionCallback?.onEndLifetimeExtension(mLifetimeExtender, entry)
        }
    }
}
+32 −0
Original line number Diff line number Diff line
package com.android.systemui.statusbar.notification.collection.coordinator

import com.android.systemui.log.LogBuffer
import com.android.systemui.log.LogLevel
import com.android.systemui.log.dagger.NotificationLog
import com.android.systemui.statusbar.notification.row.NotificationGuts
import javax.inject.Inject

private const val TAG = "GutsCoordinator"

class GutsCoordinatorLogger @Inject constructor(
    @NotificationLog private val buffer: LogBuffer
) {

    fun logGutsOpened(key: String, guts: NotificationGuts) {
        buffer.log(TAG, LogLevel.DEBUG, {
            str1 = key
            str2 = guts::class.simpleName
            bool1 = guts.isLeavebehind
        }, {
            "Guts of type $str2 (leave behind: $bool1) opened for class $str1"
        })
    }

    fun logGutsClosed(key: String) {
        buffer.log(TAG, LogLevel.DEBUG, {
            str1 = key
        }, {
            "Guts closed for class $str1"
        })
    }
}
+6 −5
Original line number Diff line number Diff line
@@ -19,7 +19,8 @@ package com.android.systemui.statusbar.notification.collection.coordinator;
import static com.android.systemui.statusbar.NotificationRemoteInputManager.FORCE_REMOTE_INPUT_HISTORY;
import static com.android.systemui.statusbar.notification.interruption.HeadsUpController.alertAgain;

import android.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.statusbar.NotificationRemoteInputManager;
@@ -164,17 +165,17 @@ public class HeadsUpCoordinator implements Coordinator {

    private final NotifLifetimeExtender mLifetimeExtender = new NotifLifetimeExtender() {
        @Override
        public String getName() {
        public @NonNull String getName() {
            return TAG;
        }

        @Override
        public void setCallback(OnEndLifetimeExtensionCallback callback) {
        public void setCallback(@NonNull OnEndLifetimeExtensionCallback callback) {
            mEndLifetimeExtension = callback;
        }

        @Override
        public boolean shouldExtendLifetime(NotificationEntry entry, int reason) {
        public boolean shouldExtendLifetime(@NonNull NotificationEntry entry, int reason) {
            boolean isShowingHun = isCurrentlyShowingHun(entry);
            if (isShowingHun) {
                mNotifExtendingLifetime = entry;
@@ -183,7 +184,7 @@ public class HeadsUpCoordinator implements Coordinator {
        }

        @Override
        public void cancelLifetimeExtension(NotificationEntry entry) {
        public void cancelLifetimeExtension(@NonNull NotificationEntry entry) {
            if (Objects.equals(mNotifExtendingLifetime, entry)) {
                mNotifExtendingLifetime = null;
            }
+2 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ public class NotifCoordinators implements Dumpable {
            DeviceProvisionedCoordinator deviceProvisionedCoordinator,
            BubbleCoordinator bubbleCoordinator,
            HeadsUpCoordinator headsUpCoordinator,
            GutsCoordinator gutsCoordinator,
            ConversationCoordinator conversationCoordinator,
            PreparationCoordinator preparationCoordinator,
            MediaCoordinator mediaCoordinator,
@@ -83,6 +84,7 @@ public class NotifCoordinators implements Dumpable {

        if (featureFlags.isNewNotifPipelineRenderingEnabled()) {
            mCoordinators.add(headsUpCoordinator);
            mCoordinators.add(gutsCoordinator);
            mCoordinators.add(preparationCoordinator);
        }

Loading