Skip to content

Commit f0136f7

Browse files
Easy switching to saved networks
1 parent 11e35d0 commit f0136f7

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId = "com.cgprograms.dnslock"
1212
minSdk = 29
1313
targetSdk = 34
14-
versionCode = 4
15-
versionName = "3.0.1"
14+
versionCode = 5
15+
versionName = "3.1.0"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables {
-26 Bytes
Binary file not shown.
-25 Bytes
Binary file not shown.

app/release/output-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"type": "SINGLE",
1212
"filters": [],
1313
"attributes": [],
14-
"versionCode": 4,
15-
"versionName": "3.0.1",
14+
"versionCode": 5,
15+
"versionName": "3.1.0",
1616
"outputFile": "app-release.apk"
1717
}
1818
],

app/src/main/java/com/cgprograms/dnslock/MainActivity.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.widget.Button
2020
import android.widget.EditText
2121
import android.widget.Toast
2222

23+
@SuppressLint("MissingPermission")
2324
class MainActivity : Activity() {
2425
private lateinit var devicePolicyManager: DevicePolicyManager
2526
private lateinit var compName: ComponentName
@@ -73,8 +74,13 @@ class MainActivity : Activity() {
7374
addButton.setOnClickListener {
7475
val selectedSSID = ssid.text.toString()
7576
val scanResult = wifiManager.scanResults.find { it.SSID == selectedSSID }
77+
val configuredNetwork =
78+
wifiManager.configuredNetworks.find { it.SSID == "\"$selectedSSID\"" }
7679

77-
if (scanResult != null) {
80+
if (configuredNetwork != null) {
81+
wifiManager.enableNetwork(configuredNetwork.networkId, true)
82+
Toast.makeText(this, "Switched to $selectedSSID", Toast.LENGTH_SHORT).show()
83+
} else if (scanResult != null) {
7884
val isOpenNetwork = getSecurityType(scanResult) == "Open"
7985

8086
val wifiConfig = WifiConfiguration().apply {
@@ -91,7 +97,8 @@ class MainActivity : Activity() {
9197
wifiManager.enableNetwork(netId, true)
9298
Toast.makeText(this, "Connected to $selectedSSID", Toast.LENGTH_SHORT).show()
9399
} else {
94-
Toast.makeText(this, "Failed to connect to $selectedSSID", Toast.LENGTH_SHORT).show()
100+
Toast.makeText(this, "Failed to connect to $selectedSSID", Toast.LENGTH_SHORT)
101+
.show()
95102
}
96103
} else {
97104
Toast.makeText(this, "SSID not found in scan results", Toast.LENGTH_SHORT).show()
@@ -110,7 +117,6 @@ class MainActivity : Activity() {
110117
private fun showAvailableSSIDs() {
111118
wifiManager.setWifiEnabled(true)
112119

113-
// Show loading dialog before starting the scan
114120
showLoadingDialog()
115121

116122
val wifiScanReceiver = object : BroadcastReceiver() {
@@ -121,7 +127,6 @@ class MainActivity : Activity() {
121127
} else {
122128
Log.d("MainActivity", "WiFi Scan failed")
123129
}
124-
// Dismiss the loading dialog once scan results are ready
125130
dismissLoadingDialog()
126131
unregisterReceiver(this)
127132
}
@@ -134,25 +139,30 @@ class MainActivity : Activity() {
134139
val success = wifiManager.startScan()
135140
if (!success) {
136141
Log.d("MainActivity", "WiFi Scan initiation failed")
137-
dismissLoadingDialog() // Dismiss loading dialog in case of failure
142+
dismissLoadingDialog()
138143
}
139144
}
140145

141-
@SuppressLint("MissingPermission")
142146
private fun displaySSIDDialog() {
143147
val scanResults = wifiManager.scanResults
144-
val ssidList = scanResults.filter { it.SSID.isNotEmpty() }.sortedByDescending { it.level }
145-
.map { it to getSecurityType(it) }
146-
.map { "${it.first.SSID} (${it.second})" } // Display SSID and security type
147-
.toSet().toList()
148+
val configuredNetworks = wifiManager.configuredNetworks
149+
150+
val ssidList =
151+
scanResults.asSequence().filter { it.SSID.isNotEmpty() }.sortedByDescending { it.level }
152+
.map { scanResult ->
153+
val isKnown = configuredNetworks.any { it.SSID == "\"${scanResult.SSID}\"" }
154+
val securityType = getSecurityType(scanResult)
155+
val indicator = if (isKnown) "Known" else "Unknown"
156+
"${scanResult.SSID} ($securityType) - $indicator"
157+
}.toSet().toList()
148158

149159
var dialog: AlertDialog? = null
150160
val builder = AlertDialog.Builder(this)
151161
builder.setTitle("Select SSID")
152162

153163
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ssidList)
154164
builder.setAdapter(adapter) { _, which ->
155-
val selectedSSID = ssidList[which].split(" (")[0] // Extract the SSID
165+
val selectedSSID = ssidList[which].split(" (")[0]
156166
ssid.setText(selectedSSID)
157167
dialog?.dismiss()
158168
password.requestFocus()
@@ -162,18 +172,16 @@ class MainActivity : Activity() {
162172
dialog = builder.show()
163173
}
164174

165-
// Function to determine the security type of the Wi-Fi network
166175
private fun getSecurityType(scanResult: ScanResult): String {
167176
val capabilities = scanResult.capabilities
168177
return when {
169178
capabilities.contains("WEP") -> "WEP"
170179
capabilities.contains("WPA") -> "WPA/WPA2"
171180
capabilities.contains("SAE") -> "WPA3"
172-
else -> "Open" // No security
181+
else -> "Open"
173182
}
174183
}
175184

176-
// Show a loading dialog with a progress bar
177185
private fun showLoadingDialog() {
178186
if (loadingDialog == null) {
179187
val builder = AlertDialog.Builder(this)
@@ -186,7 +194,6 @@ class MainActivity : Activity() {
186194
loadingDialog?.show()
187195
}
188196

189-
// Dismiss the loading dialog
190197
private fun dismissLoadingDialog() {
191198
loadingDialog?.dismiss()
192199
}

0 commit comments

Comments
 (0)