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

Commit 78d1d951 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[CS][Disable] Add DisableFlagsRepo and use it for notif alerts status.

Bug: 287076786
Test: manual: When DISABLE_NOTIFICATION_ALERTS comes through
CommandQueue, verify that repo and StatusBarNotificationPresenter get
the right value
Test: manual: When DISABLE_NOTIFICATION_ALERTS is on, send notif ->
verify it doesn't HUN
Test: manual: When DISABLE_NOTIFICATION_ALERTS is off, send notif ->
verify it *does* HUN
Test: `adb shell dumpsys activity service
com.android.systemui/.SystemUIService DisableFlagsRepository` -> dumps
disable flags

Test: atest DisableFlagsRepositoryTest
Test: atest NotificationsInteractorTest
StatusBarNotificationPresenterTest

Change-Id: I44eb2da2ab88bc0d007b7cc913396f0f7f2c257f
parent 6f4622fd
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.log.dagger

import com.android.systemui.log.LogBuffer
import javax.inject.Qualifier

/** A [LogBuffer] for changes to [DisableFlagsRepository]. */
@Qualifier
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class DisableFlagsRepositoryLog
+9 −0
Original line number Diff line number Diff line
@@ -224,6 +224,15 @@ public class LogModule {
                false /* systrace */);
    }

    /** Provides a logging buffer for the disable flags repository. */
    @Provides
    @SysUISingleton
    @DisableFlagsRepositoryLog
    public static LogBuffer provideDisableFlagsRepositoryLogBuffer(LogBufferFactory factory) {
        return factory.create("DisableFlagsRepository", 40 /* maxSize */,
                false /* systrace */);
    }

    /** Provides a logging buffer for logs related to swipe up gestures. */
    @Provides
    @SysUISingleton
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.disableflags.data.model

import android.app.StatusBarManager.DISABLE2_NONE
import android.app.StatusBarManager.DISABLE_NONE
import android.app.StatusBarManager.DISABLE_NOTIFICATION_ALERTS
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.LogLevel
import com.android.systemui.statusbar.disableflags.DisableFlagsLogger

/**
 * Model for the disable flags that come from [IStatusBar].
 *
 * For clients of the disable flags: do *not* refer to the disable integers directly. Instead,
 * re-use or define a helper method that internally processes the flags. (We want to hide the
 * bitwise logic here so no one else has to worry about it.)
 */
data class DisableFlagsModel(
    private val disable1: Int = DISABLE_NONE,
    private val disable2: Int = DISABLE2_NONE,
) {
    /** Returns true if notification alerts are allowed based on the flags. */
    fun areNotificationAlertsEnabled(): Boolean {
        return (disable1 and DISABLE_NOTIFICATION_ALERTS) == 0
    }

    /** Logs the change to the provided buffer. */
    fun logChange(buffer: LogBuffer, disableFlagsLogger: DisableFlagsLogger) {
        buffer.log(
            TAG,
            LogLevel.INFO,
            {
                int1 = disable1
                int2 = disable2
            },
            {
                disableFlagsLogger.getDisableFlagsString(
                    new = DisableFlagsLogger.DisableState(int1, int2),
                )
            }
        )
    }

    private companion object {
        const val TAG = "DisableFlagsModel"
    }
}
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.disableflags.data.repository

import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.DisplayId
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.dagger.DisableFlagsRepositoryLog
import com.android.systemui.statusbar.CommandQueue
import com.android.systemui.statusbar.disableflags.DisableFlagsLogger
import com.android.systemui.statusbar.disableflags.data.model.DisableFlagsModel
import javax.inject.Inject
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.onEach
import kotlinx.coroutines.flow.stateIn

/** Repository for the disable flags received from external systems. See [IStatusBar.disable]. */
@SysUISingleton
class DisableFlagsRepository
@Inject
constructor(
    commandQueue: CommandQueue,
    @DisplayId private val displayId: Int,
    @Application scope: CoroutineScope,
    @DisableFlagsRepositoryLog private val logBuffer: LogBuffer,
    private val disableFlagsLogger: DisableFlagsLogger,
) {
    val disableFlags: StateFlow<DisableFlagsModel> =
        conflatedCallbackFlow {
                val callback =
                    object : CommandQueue.Callbacks {
                        override fun disable(
                            displayId: Int,
                            state1: Int,
                            state2: Int,
                            animate: Boolean,
                        ) {
                            if (displayId != this@DisableFlagsRepository.displayId) {
                                return
                            }
                            trySend(DisableFlagsModel(state1, state2))
                        }
                    }
                commandQueue.addCallback(callback)
                awaitClose { commandQueue.removeCallback(callback) }
            }
            .distinctUntilChanged()
            .onEach { it.logChange(logBuffer, disableFlagsLogger) }
            // Use Eagerly because we always need to know about disable flags
            .stateIn(scope, SharingStarted.Eagerly, DisableFlagsModel())
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.disableflags.data.repository.DisableFlagsRepository
import javax.inject.Inject

/** Interactor for notifications in general. */
@SysUISingleton
class NotificationsInteractor
@Inject
constructor(
    private val disableFlagsRepository: DisableFlagsRepository,
) {
    /** Returns true if notification alerts are allowed. */
    fun areNotificationAlertsEnabled(): Boolean {
        return disableFlagsRepository.disableFlags.value.areNotificationAlertsEnabled()
    }
}
Loading