1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.wifi.repository 18 19 import android.content.Context 20 import android.content.IntentFilter 21 import android.net.ConnectivityManager 22 import android.net.NetworkScoreManager 23 import android.net.wifi.WifiManager 24 import com.android.settingslib.spaprivileged.framework.common.broadcastReceiverFlow 25 import com.android.settingslib.wifi.WifiStatusTracker 26 import kotlinx.coroutines.Dispatchers 27 import kotlinx.coroutines.channels.awaitClose 28 import kotlinx.coroutines.flow.Flow 29 import kotlinx.coroutines.flow.callbackFlow 30 import kotlinx.coroutines.flow.conflate 31 import kotlinx.coroutines.flow.flowOn 32 import kotlinx.coroutines.flow.launchIn 33 import kotlinx.coroutines.flow.onEach 34 35 /** Repository that listeners to wifi callback and provide wifi status flow to client. */ 36 class WifiStatusRepository( 37 private val context: Context, 38 private val wifiStatusTrackerFactory: (callback: Runnable) -> WifiStatusTracker = { callback -> 39 WifiStatusTracker( 40 context, 41 context.getSystemService(WifiManager::class.java), 42 context.getSystemService(NetworkScoreManager::class.java), 43 context.getSystemService(ConnectivityManager::class.java), 44 callback, 45 ) 46 }, 47 ) { wifiStatusTrackerFlownull48 fun wifiStatusTrackerFlow(): Flow<WifiStatusTracker> = 49 callbackFlow { 50 var wifiStatusTracker: WifiStatusTracker? = null 51 wifiStatusTracker = wifiStatusTrackerFactory { wifiStatusTracker?.let(::trySend) } 52 53 context 54 .broadcastReceiverFlow(INTENT_FILTER) 55 .onEach { intent -> wifiStatusTracker.handleBroadcast(intent) } 56 .launchIn(this) 57 58 wifiStatusTracker.setListening(true) 59 wifiStatusTracker.fetchInitialState() 60 trySend(wifiStatusTracker) 61 62 awaitClose { wifiStatusTracker.setListening(false) } 63 } 64 .conflate() 65 .flowOn(Dispatchers.Default) 66 67 private companion object { 68 val INTENT_FILTER = <lambda>null69 IntentFilter().apply { 70 addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) 71 addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION) 72 addAction(WifiManager.RSSI_CHANGED_ACTION) 73 } 74 } 75 } 76