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

Commit 377e81d5 authored by amehfooz's avatar amehfooz Committed by Ahmed Mehfooz
Browse files

[SB][ComposeIcons] Add LocationActiveStatusInteractor

Test: LocationActiveStatusInteractorTest
Bug: 417815422
Flag: com.android.systemui.status_bar_system_status_icons_in_compose
Change-Id: I17803a8736eab4a5e82f954ce17706b8aaa4d901
parent a9263cdb
Loading
Loading
Loading
Loading
+66 −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 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.fakeLocationController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

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

    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val underTest = kosmos.locationActiveStatusInteractor

    @Test
    fun isLocationActive_flowStartsWithControllerState_True() =
        kosmos.runTest {
            fakeLocationController.isLocationActive = true
            val isLocationActive by collectLastValue(underTest.isLocationActive)
            assertThat(isLocationActive).isTrue()
        }

    @Test
    fun isLocationActive_flowStartsWithControllerState_False() =
        kosmos.runTest {
            fakeLocationController.isLocationActive = false
            val isLocationActive by collectLastValue(underTest.isLocationActive)
            assertThat(isLocationActive).isFalse()
        }

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

            fakeLocationController.isLocationActive = true
            assertThat(isLocationActive).isTrue()

            fakeLocationController.isLocationActive = false
            assertThat(isLocationActive).isFalse()
        }
}
+61 −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.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.statusbar.policy.LocationController
import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback
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 location usage status. */
@SysUISingleton
class LocationActiveStatusInteractor
@Inject
constructor(
    @Background private val bgDispatcher: CoroutineDispatcher,
    @Background private val scope: CoroutineScope,
    private val locationController: LocationController,
) {
    /** Flow emitting true if location is currently active, false otherwise. */
    val isLocationActive: StateFlow<Boolean> =
        conflatedCallbackFlow {
                trySend(locationController.isLocationActive)
                val callback =
                    object : LocationChangeCallback {
                        override fun onLocationActiveChanged(active: Boolean) {
                            trySend(active)
                        }
                    }
                locationController.addCallback(callback)
                awaitClose { locationController.removeCallback(callback) }
            }
            .flowOn(bgDispatcher)
            .stateIn(
                scope = scope,
                started = SharingStarted.WhileSubscribed(),
                initialValue = locationController.isLocationActive,
            )
}
+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.FakeLocationController

val Kosmos.fakeLocationController by Fixture { FakeLocationController(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.fakeLocationController

val Kosmos.locationActiveStatusInteractor by Fixture {
    LocationActiveStatusInteractor(
        bgDispatcher = testDispatcher,
        scope = testScope.backgroundScope,
        locationController = fakeLocationController,
    )
}
+9 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ public class FakeLocationController extends BaseLeakChecker<LocationChangeCallba

    private final List<LocationChangeCallback> mCallbacks = new ArrayList<>();
    private boolean mLocationEnabled = false;
    private boolean mLocationActive = false;

    public FakeLocationController(LeakCheck test) {
        super(test, "location");
@@ -34,7 +35,14 @@ public class FakeLocationController extends BaseLeakChecker<LocationChangeCallba

    @Override
    public boolean isLocationActive() {
        return false;
        return mLocationActive;
    }

    public void setLocationActive(boolean active) {
        mLocationActive = active;
        for (LocationChangeCallback callback : new ArrayList<>(mCallbacks)) {
            callback.onLocationActiveChanged(mLocationActive);
        }
    }

    @Override