@@ -120,18 +120,22 @@ object ApiUtil {
120120 context.applicationContext.getSystemService(Context .WIFI_SERVICE ) as WifiManager
121121 return wifiManager.removeNetwork(netId)
122122 }
123+
123124 fun enableLocation (context : Context , onEnabled : (() -> Unit )? = null): Boolean {
124125 val activity = context as ? MainActivity ? : return false
125126 if (! isLocationEnabled(context)) {
126127 activity.pendingLocationCallback = onEnabled
127- val locationRequest = LocationRequest .Builder (Priority .PRIORITY_HIGH_ACCURACY , 10000 ).build()
128- val builder = LocationSettingsRequest .Builder ().addLocationRequest(locationRequest).setAlwaysShow(true )
128+ val locationRequest =
129+ LocationRequest .Builder (Priority .PRIORITY_HIGH_ACCURACY , 10000 ).build()
130+ val builder = LocationSettingsRequest .Builder ().addLocationRequest(locationRequest)
131+ .setAlwaysShow(true )
129132 val client = LocationServices .getSettingsClient(activity)
130133
131134 client.checkLocationSettings(builder.build()).addOnFailureListener { exception ->
132135 if (exception is ResolvableApiException ) {
133136 try {
134- val intentSenderRequest = IntentSenderRequest .Builder (exception.resolution.intentSender).build()
137+ val intentSenderRequest =
138+ IntentSenderRequest .Builder (exception.resolution.intentSender).build()
135139 activity.locationLauncher.launch(intentSenderRequest)
136140 } catch (_: Exception ) {
137141 val intent = Intent (Settings .ACTION_LOCATION_SOURCE_SETTINGS )
@@ -146,22 +150,16 @@ object ApiUtil {
146150
147151
148152 @RequiresPermission(Manifest .permission.ACCESS_FINE_LOCATION )
149- fun getSavedWifiList (context : Context ): List <Pair < Int , String > > {
153+ fun getSavedWifiList (context : Context ): List <WifiConfiguration > {
150154 val wifiManager =
151155 context.applicationContext.getSystemService(Context .WIFI_SERVICE ) as WifiManager
152156 val configs = wifiManager.configuredNetworks ? : return emptyList()
153-
154- return configs.distinctBy { it.networkId }.mapNotNull { config ->
155- var ssid = config.SSID ? : return @mapNotNull null
156- if (ssid.startsWith(" \" " ) && ssid.endsWith(" \" " ) && ssid.length >= 2 ) {
157- ssid = ssid.substring(1 , ssid.length - 1 )
158- }
159- Pair (config.networkId, ssid)
160- }
157+ return configs.distinctBy { it.networkId }
161158 }
162159
163160 fun getNetIdBySsid (context : Context , ssid : String ): Int {
164- return getSavedWifiList(context).find { it.second == ssid }?.first ? : - 1
161+ val savedNetworks: List <WifiConfiguration > = getSavedWifiList(context)
162+ return savedNetworks.find { it.SSID .removeSurrounding(" \" " ) == ssid }?.networkId ? : - 1
165163 }
166164
167165 fun startScan (context : Context ): Boolean {
@@ -179,7 +177,7 @@ object ApiUtil {
179177 com.wifi.toolbox.structs.WifiInfo (
180178 ssid = it.SSID ,
181179 level = it.level,
182- bssid= " " ,
180+ bssid = " " ,
183181 capabilities = it.capabilities
184182 )
185183 }.sortedByDescending { it.level }
0 commit comments