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

Commit 22dbd320 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov Committed by Automerger Merge Worker
Browse files

Merge "[SB Refactor] Create an airplane mode repo/interactor/view model and...

Merge "[SB Refactor] Create an airplane mode repo/interactor/view model and use it to determine when to show the wifi<->airplane spacer." into tm-qpr-dev am: fd6c3092 am: 502cf6a5

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/20208616



Change-Id: I283b84c9319298dbd939e864d465f4cbd33b4bae
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 27caec1f 502cf6a5
Loading
Loading
Loading
Loading
+93 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.pipeline.airplane.data.repository

import android.os.Handler
import android.os.UserHandle
import android.provider.Settings.Global
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.Background
import com.android.systemui.qs.SettingObserver
import com.android.systemui.statusbar.pipeline.shared.ConnectivityPipelineLogger
import com.android.systemui.statusbar.pipeline.shared.ConnectivityPipelineLogger.Companion.logInputChange
import com.android.systemui.util.settings.GlobalSettings
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.stateIn

/**
 * Provides data related to airplane mode.
 *
 * IMPORTANT: This is currently *not* used to render any airplane mode information anywhere. It is
 * only used to help [com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.WifiViewModel]
 * determine what parts of the wifi icon view should be shown.
 *
 * TODO(b/238425913): Consider migrating the status bar airplane mode icon to use this repo.
 */
interface AirplaneModeRepository {
    /** Observable for whether the device is currently in airplane mode. */
    val isAirplaneMode: StateFlow<Boolean>
}

@SysUISingleton
@Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
@OptIn(ExperimentalCoroutinesApi::class)
class AirplaneModeRepositoryImpl
@Inject
constructor(
    @Background private val bgHandler: Handler,
    private val globalSettings: GlobalSettings,
    logger: ConnectivityPipelineLogger,
    @Application scope: CoroutineScope,
) : AirplaneModeRepository {
    // TODO(b/254848912): Replace this with a generic SettingObserver coroutine once we have it.
    override val isAirplaneMode: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val observer =
                    object :
                        SettingObserver(
                            globalSettings,
                            bgHandler,
                            Global.AIRPLANE_MODE_ON,
                            UserHandle.USER_ALL
                        ) {
                        override fun handleValueChanged(value: Int, observedChange: Boolean) {
                            trySend(value == 1)
                        }
                    }

                observer.isListening = true
                trySend(observer.value == 1)
                awaitClose { observer.isListening = false }
            }
            .distinctUntilChanged()
            .logInputChange(logger, "isAirplaneMode")
            .stateIn(
                scope,
                started = SharingStarted.WhileSubscribed(),
                // When the observer starts listening, the flow will emit the current value so the
                // initialValue here is irrelevant.
                initialValue = false,
            )
}
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.pipeline.airplane.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.pipeline.airplane.data.repository.AirplaneModeRepository
import com.android.systemui.statusbar.pipeline.shared.data.model.ConnectivitySlot
import com.android.systemui.statusbar.pipeline.shared.data.repository.ConnectivityRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
 * The business logic layer for airplane mode.
 *
 * IMPORTANT: This is currently *not* used to render any airplane mode information anywhere. See
 * [AirplaneModeRepository] for more details.
 */
@SysUISingleton
class AirplaneModeInteractor
@Inject
constructor(
    airplaneModeRepository: AirplaneModeRepository,
    connectivityRepository: ConnectivityRepository,
) {
    /** True if the device is currently in airplane mode. */
    val isAirplaneMode: Flow<Boolean> = airplaneModeRepository.isAirplaneMode

    /** True if we're configured to force-hide the airplane mode icon and false otherwise. */
    val isForceHidden: Flow<Boolean> =
        connectivityRepository.forceHiddenSlots.map { it.contains(ConnectivitySlot.AIRPLANE) }
}
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.pipeline.airplane.ui.viewmodel

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.statusbar.pipeline.airplane.domain.interactor.AirplaneModeInteractor
import com.android.systemui.statusbar.pipeline.shared.ConnectivityPipelineLogger
import com.android.systemui.statusbar.pipeline.shared.ConnectivityPipelineLogger.Companion.logOutputChange
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.stateIn

/**
 * Models the UI state for the status bar airplane mode icon.
 *
 * IMPORTANT: This is currently *not* used to render any airplane mode information anywhere. See
 * [com.android.systemui.statusbar.pipeline.airplane.data.repository.AirplaneModeRepository] for
 * more details.
 */
@SysUISingleton
class AirplaneModeViewModel
@Inject
constructor(
    interactor: AirplaneModeInteractor,
    logger: ConnectivityPipelineLogger,
    @Application private val scope: CoroutineScope,
) {
    /** True if the airplane mode icon is currently visible in the status bar. */
    val isAirplaneModeIconVisible: StateFlow<Boolean> =
        combine(interactor.isAirplaneMode, interactor.isForceHidden) {
                isAirplaneMode,
                isAirplaneIconForceHidden ->
                isAirplaneMode && !isAirplaneIconForceHidden
            }
            .distinctUntilChanged()
            .logOutputChange(logger, "isAirplaneModeIconVisible")
            .stateIn(scope, started = SharingStarted.WhileSubscribed(), initialValue = false)
}
+5 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.systemui.statusbar.pipeline.dagger

import com.android.systemui.statusbar.pipeline.airplane.data.repository.AirplaneModeRepository
import com.android.systemui.statusbar.pipeline.airplane.data.repository.AirplaneModeRepositoryImpl
import com.android.systemui.statusbar.pipeline.mobile.data.repository.MobileSubscriptionRepository
import com.android.systemui.statusbar.pipeline.mobile.data.repository.MobileSubscriptionRepositoryImpl
import com.android.systemui.statusbar.pipeline.mobile.data.repository.UserSetupRepository
@@ -29,6 +31,9 @@ import dagger.Module

@Module
abstract class StatusBarPipelineModule {
    @Binds
    abstract fun airplaneModeRepository(impl: AirplaneModeRepositoryImpl): AirplaneModeRepository

    @Binds
    abstract fun connectivityRepository(impl: ConnectivityRepositoryImpl): ConnectivityRepository

+7 −0
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ object WifiViewBinder {
        val activityInView = view.requireViewById<ImageView>(R.id.wifi_in)
        val activityOutView = view.requireViewById<ImageView>(R.id.wifi_out)
        val activityContainerView = view.requireViewById<View>(R.id.inout_container)
        val airplaneSpacer = view.requireViewById<View>(R.id.wifi_airplane_spacer)

        view.isVisible = true
        iconView.isVisible = true
@@ -142,6 +143,12 @@ object WifiViewBinder {
                        activityContainerView.isVisible = visible
                    }
                }

                launch {
                    viewModel.isAirplaneSpacerVisible.distinctUntilChanged().collect { visible ->
                        airplaneSpacer.isVisible = visible
                    }
                }
            }
        }

Loading