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

Commit 4d70ec11 authored by Ahmed Mehfooz's avatar Ahmed Mehfooz Committed by Android (Google) Code Review
Browse files

Merge "[SB][ComposeIcons] Add NextAlarmInteractor" into main

parents 670f3348 2e71d6bf
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.policy.domain.interactor

import android.app.AlarmManager
import android.app.PendingIntent
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.policy.fakeNextAlarmController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock

@SmallTest
@RunWith(AndroidJUnit4::class)
class NextAlarmInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()

    private val underTest: NextAlarmInteractor = kosmos.nextAlarmInteractor

    @Test
    fun isAlarmSet_initiallyFalseWhenNoAlarm() =
        kosmos.runTest {
            val isAlarmSet by collectLastValue(underTest.isAlarmSet)
            fakeNextAlarmController.setNextAlarm(null)
            assertThat(isAlarmSet).isFalse()
        }

    @Test
    fun isAlarmSet_trueWhenAlarmIsSet() =
        kosmos.runTest {
            val isAlarmSet by collectLastValue(underTest.isAlarmSet)
            val alarmClockInfo = AlarmManager.AlarmClockInfo(1L, mock<PendingIntent>())

            fakeNextAlarmController.setNextAlarm(alarmClockInfo)

            assertThat(isAlarmSet).isTrue()
        }

    @Test
    fun isAlarmSet_updatesWhenAlarmChanges() =
        kosmos.runTest {
            val isAlarmSet by collectLastValue(underTest.isAlarmSet)
            assertThat(isAlarmSet).isFalse()

            val alarmInfo = AlarmManager.AlarmClockInfo(1L, mock<PendingIntent>())
            fakeNextAlarmController.setNextAlarm(alarmInfo)
            assertThat(isAlarmSet).isTrue()

            fakeNextAlarmController.setNextAlarm(null)
            assertThat(isAlarmSet).isFalse()
        }
}
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.policy.domain.interactor

import android.app.AlarmManager
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.statusbar.policy.NextAlarmController
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn

/** Interactor responsible for determining if the alarm icon should be shown. */
@SysUISingleton
class NextAlarmInteractor
@Inject
constructor(
    @Background private val bgDispatcher: CoroutineDispatcher,
    @Background private val scope: CoroutineScope,
    private val nextAlarmController: NextAlarmController,
) {

    /**
     * A StateFlow that emits `true` if there is an upcoming alarm scheduled, and `false` otherwise.
     * This is driven by callbacks from [NextAlarmController].
     */
    val isAlarmSet: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val callback =
                    NextAlarmController.NextAlarmChangeCallback {
                        nextAlarm: AlarmManager.AlarmClockInfo? ->
                        // nextAlarm is non-null if an alarm is set, null otherwise.
                        trySend(nextAlarm != null)
                    }

                nextAlarmController.addCallback(callback)
                awaitClose { nextAlarmController.removeCallback(callback) }
            }
            .flowOn(bgDispatcher)
            .stateIn(
                initialValue = false,
                scope = scope,
                started = SharingStarted.WhileSubscribed(),
            )
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.policy

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.utils.leaks.FakeNextAlarmController

val Kosmos.fakeNextAlarmController by Fixture { FakeNextAlarmController(leakCheck) }
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.policy

import android.testing.LeakCheck
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture

val Kosmos.leakCheck by Fixture { LeakCheck() }
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.policy.domain.interactor

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.statusbar.policy.fakeNextAlarmController

val Kosmos.nextAlarmInteractor by Fixture {
    NextAlarmInteractor(
        scope = testScope.backgroundScope,
        bgDispatcher = testDispatcher,
        nextAlarmController = fakeNextAlarmController,
    )
}