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.os.Handler 21 import android.os.HandlerThread 22 import android.os.Looper 23 import android.os.Process 24 import android.os.SystemClock 25 import android.util.Log 26 import com.android.settings.overlay.FeatureFactory.Companion.featureFactory 27 import com.android.wifitrackerlib.WifiEntry 28 import com.android.wifitrackerlib.WifiPickerTracker 29 import kotlinx.coroutines.Dispatchers 30 import kotlinx.coroutines.channels.awaitClose 31 import kotlinx.coroutines.flow.Flow 32 import kotlinx.coroutines.flow.callbackFlow 33 import kotlinx.coroutines.flow.conflate 34 import kotlinx.coroutines.flow.flowOn 35 import kotlinx.coroutines.flow.onEach 36 37 /** Repository that listeners to wifi picker callback and provide wifi picker flow to client. */ 38 class WifiPickerRepository( 39 private val context: Context, 40 private val createWifiPickerTracker: 41 ( 42 workerThread: HandlerThread, callback: WifiPickerTracker.WifiPickerTrackerCallback 43 ) -> WifiPickerTracker = 44 { workerThread, callback -> 45 featureFactory.wifiTrackerLibProvider.createWifiPickerTracker( 46 null, 47 context, 48 Handler(Looper.getMainLooper()), 49 workerThread.getThreadHandler(), 50 SystemClock.elapsedRealtimeClock(), 51 MAX_SCAN_AGE_MILLIS, 52 SCAN_INTERVAL_MILLIS, 53 callback, 54 ) 55 } 56 ) { 57 connectedWifiEntryFlownull58 fun connectedWifiEntryFlow(): Flow<WifiEntry?> = 59 callbackFlow { 60 val workerThread = 61 HandlerThread( 62 /* name = */ "$TAG{${Integer.toHexString(System.identityHashCode(this))}}", 63 /* priority = */ Process.THREAD_PRIORITY_BACKGROUND, 64 ) 65 workerThread.start() 66 var tracker: WifiPickerTracker? = null 67 val callback = 68 object : WifiPickerTracker.WifiPickerTrackerCallback { 69 override fun onWifiEntriesChanged() { 70 trySend(tracker?.connectedWifiEntry) 71 } 72 73 override fun onWifiStateChanged() {} 74 75 override fun onNumSavedNetworksChanged() {} 76 77 override fun onNumSavedSubscriptionsChanged() {} 78 } 79 80 tracker = createWifiPickerTracker(workerThread, callback) 81 tracker.onStart() 82 83 awaitClose { 84 tracker.onStop() 85 tracker.onDestroy() 86 workerThread.quit() 87 } 88 } 89 .conflate() <lambda>null90 .onEach { Log.d(TAG, "connectedWifiEntryFlow: $it") } 91 .flowOn(Dispatchers.Default) 92 93 companion object { 94 private const val TAG = "WifiPickerRepository" 95 96 /** Max age of tracked WifiEntries */ 97 private const val MAX_SCAN_AGE_MILLIS: Long = 15000 98 /** Interval between initiating WifiPickerTracker scans */ 99 private const val SCAN_INTERVAL_MILLIS: Long = 10000 100 } 101 } 102