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.dagger.SysUISingleton
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.dagger.qualifiers.Main
28 import com.android.systemui.util.time.SystemClock
29 import com.android.wifitrackerlib.WifiPickerTracker
30 import com.android.wifitrackerlib.WifiPickerTracker.WifiPickerTrackerCallback
31 import java.time.Clock
32 import java.time.ZoneOffset
33 import javax.inject.Inject
34 
35 /**
36  * Factory for creating [WifiPickerTracker] for SysUI.
37  *
38  * Uses the same time intervals as the Settings page for Wifi.
39  */
40 @SysUISingleton
41 class WifiPickerTrackerFactory
42 @Inject
43 constructor(
44     private val context: Context,
45     private val wifiManager: WifiManager?,
46     private val connectivityManager: ConnectivityManager,
47     private val systemClock: SystemClock,
48     @Main private val mainHandler: Handler,
49     @Background private val workerHandler: Handler,
50 ) {
51     private val clock: Clock =
52         object : SimpleClock(ZoneOffset.UTC) {
53             override fun millis(): Long {
54                 return systemClock.elapsedRealtime()
55             }
56         }
57     val isSupported: Boolean
58         get() = wifiManager != null
59 
60     /**
61      * Creates a [WifiPickerTracker] instance.
62      *
63      * @return a new [WifiPickerTracker] or null if [WifiManager] is null.
64      */
65     fun create(
66         lifecycle: Lifecycle,
67         listener: WifiPickerTrackerCallback,
68     ): WifiPickerTracker? {
69         return if (wifiManager == null) {
70             null
71         } else
72             WifiPickerTracker(
73                 lifecycle,
74                 context,
75                 wifiManager,
76                 connectivityManager,
77                 mainHandler,
78                 workerHandler,
79                 clock,
80                 MAX_SCAN_AGE_MILLIS,
81                 SCAN_INTERVAL_MILLIS,
82                 listener,
83             )
84     }
85 
86     companion object {
87         /** Max age of tracked WifiEntries. */
88         private const val MAX_SCAN_AGE_MILLIS: Long = 15000
89         /** Interval between initiating WifiPickerTracker scans. */
90         private const val SCAN_INTERVAL_MILLIS: Long = 10000
91     }
92 }
93