1 /* 2 * Copyright (C) 2023 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.systemui.statusbar.connectivity 18 19 import android.content.Context 20 import android.net.ConnectivityManager 21 import android.net.wifi.WifiManager 22 import android.os.Handler 23 import android.os.SimpleClock 24 import androidx.lifecycle.Lifecycle 25 import com.android.systemui.Flags.multiuserWifiPickerTrackerSupport 26 import com.android.systemui.dagger.SysUISingleton 27 import com.android.systemui.dagger.qualifiers.Main 28 import com.android.systemui.util.concurrency.ThreadFactory 29 import com.android.systemui.util.time.SystemClock 30 import com.android.wifitrackerlib.WifiPickerTracker 31 import com.android.wifitrackerlib.WifiPickerTracker.WifiPickerTrackerCallback 32 import java.time.Clock 33 import java.time.ZoneOffset 34 import javax.inject.Inject 35 36 /** 37 * Factory for creating [WifiPickerTracker] for SysUI. 38 * 39 * Uses the same time intervals as the Settings page for Wifi. 40 */ 41 @SysUISingleton 42 class WifiPickerTrackerFactory 43 @Inject 44 constructor( 45 private val applicationContext: Context, 46 private val wifiManager: WifiManager?, 47 private val connectivityManager: ConnectivityManager, 48 private val systemClock: SystemClock, 49 @Main private val mainHandler: Handler, 50 private val threadFactory: ThreadFactory, 51 ) { 52 private val clock: Clock = 53 object : SimpleClock(ZoneOffset.UTC) { millisnull54 override fun millis(): Long { 55 return systemClock.elapsedRealtime() 56 } 57 } 58 val isSupported: Boolean 59 get() = wifiManager != null 60 61 /** 62 * Creates a [WifiPickerTracker] instance. 63 * 64 * @param name a name to identify the worker thread used for [WifiPickerTracker] operations. 65 * @return a new [WifiPickerTracker] or null if [WifiManager] is null. 66 */ createnull67 fun create( 68 userContext: Context, 69 lifecycle: Lifecycle, 70 listener: WifiPickerTrackerCallback, 71 name: String, 72 ): WifiPickerTracker? { 73 return if (wifiManager == null) { 74 null 75 } else { 76 val contextToUse = 77 if (multiuserWifiPickerTrackerSupport()) { 78 userContext 79 } else { 80 applicationContext 81 } 82 WifiPickerTracker( 83 lifecycle, 84 contextToUse, 85 wifiManager, 86 connectivityManager, 87 mainHandler, 88 // WifiPickerTracker can take tens of seconds to finish operations, so it can't use 89 // the default background handler (it would block all other background operations). 90 // Use a custom handler instead. 91 threadFactory.buildHandlerOnNewThread("WifiPickerTracker-$name"), 92 clock, 93 MAX_SCAN_AGE_MILLIS, 94 SCAN_INTERVAL_MILLIS, 95 listener, 96 ) 97 } 98 } 99 100 companion object { 101 /** Max age of tracked WifiEntries. */ 102 private const val MAX_SCAN_AGE_MILLIS: Long = 15000 103 /** Interval between initiating WifiPickerTracker scans. */ 104 private const val SCAN_INTERVAL_MILLIS: Long = 10000 105 } 106 } 107