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

Commit 7c2c0618 authored by amehfooz's avatar amehfooz
Browse files

[SB][ComposeIcons] Fix potential crash in VpnRepositoryImpl

We need to ensure vpnNetworkRequest is ready before we
register the NetworkCallback.

Test: N/A
Bug: 406922954
Flag: com.android.systemui.status_bar_system_status_icons_in_compose
Change-Id: I0595d309d250774df30ca725310994ac144fb7f1
parent ab70951a
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.statusbar.policy.vpn.data.repository.impl

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.LinkProperties
@@ -45,6 +46,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.stateIn

@SuppressLint("MissingPermission")
@SysUISingleton
class VpnRepositoryImpl
@Inject
@@ -64,6 +66,12 @@ constructor(
     */
    private val internalState: StateFlow<VpnInternalState> =
        conflatedCallbackFlow<(VpnInternalState) -> VpnInternalState> {
                val vpnNetworkRequest =
                    NetworkRequest.Builder()
                        .clearCapabilities()
                        .addTransportType(NetworkCapabilities.TRANSPORT_VPN)
                        .build()

                val callback =
                    object : ConnectivityManager.NetworkCallback() {
                        override fun onAvailable(network: Network) {
@@ -147,7 +155,7 @@ constructor(
            }
            .scan(VpnInternalState()) { state, transform -> transform(state) }
            .flowOn(bgDispatcher)
            .stateIn(scope, SharingStarted.Eagerly, VpnInternalState())
            .stateIn(scope, SharingStarted.WhileSubscribed(), VpnInternalState())

    override val vpnState: StateFlow<VpnState> =
        internalState
@@ -169,12 +177,6 @@ constructor(
        return VpnState(isEnabled = isEnabled, isBranded = isBranded, isValidated = isValidated)
    }

    private val vpnNetworkRequest =
        NetworkRequest.Builder()
            .clearCapabilities()
            .addTransportType(NetworkCapabilities.TRANSPORT_VPN)
            .build()

    /** Internal state holder that contains both the property cache and the final VPN state. */
    private data class VpnInternalState(
        val networkProperties: Map<Network, NetworkProperties> = emptyMap(),