@@ -9,13 +9,16 @@ import android.content.ComponentName
99import android.content.Context
1010import android.content.Intent
1111import android.content.IntentFilter
12+ import android.net.wifi.ScanResult
1213import android.net.wifi.WifiConfiguration
1314import android.net.wifi.WifiManager
1415import android.os.Bundle
1516import android.util.Log
17+ import android.view.LayoutInflater
1618import android.widget.ArrayAdapter
1719import android.widget.Button
1820import android.widget.EditText
21+ import android.widget.Toast
1922
2023class MainActivity : Activity () {
2124 private lateinit var devicePolicyManager: DevicePolicyManager
@@ -24,6 +27,8 @@ class MainActivity : Activity() {
2427 private lateinit var ssid: EditText
2528 private lateinit var password: EditText
2629 private lateinit var addButton: Button
30+ private lateinit var toggleWifiButton: Button
31+ private var loadingDialog: AlertDialog ? = null
2732
2833 override fun onCreate (savedInstanceState : Bundle ? ) {
2934 super .onCreate(savedInstanceState)
@@ -44,25 +49,42 @@ class MainActivity : Activity() {
4449 ssid = findViewById(R .id.ssid)
4550 password = findViewById(R .id.password)
4651 addButton = findViewById(R .id.add)
52+ toggleWifiButton = findViewById(R .id.toggle_wifi)
4753
4854 ssid.setOnClickListener {
4955 showAvailableSSIDs()
5056 }
5157
58+ ssid.setOnFocusChangeListener { _, hasFocus ->
59+ if (hasFocus) {
60+ showAvailableSSIDs()
61+ }
62+ }
63+
5264 addButton.setOnClickListener {
53- wifiManager = getSystemService(Context .WIFI_SERVICE ) as WifiManager
5465 val wifiConfig = WifiConfiguration ().apply {
5566 SSID = " \" ${ssid.text} \" "
5667 preSharedKey = " \" ${password.text} \" "
5768 }
5869 val netId = wifiManager.addNetwork(wifiConfig)
5970 wifiManager.enableNetwork(netId, true )
6071 }
72+
73+ // Toggle Wi-Fi on/off
74+ toggleWifiButton.setOnClickListener {
75+ val status = ! wifiManager.isWifiEnabled
76+ wifiManager.isWifiEnabled = status
77+ val statusText = if (status) " enabled" else " disabled"
78+ Toast .makeText(this , " Wi-Fi is now $statusText " , Toast .LENGTH_SHORT ).show()
79+ }
6180 }
6281
6382 private fun showAvailableSSIDs () {
6483 wifiManager.setWifiEnabled(true )
6584
85+ // Show loading dialog before starting the scan
86+ showLoadingDialog()
87+
6688 val wifiScanReceiver = object : BroadcastReceiver () {
6789 override fun onReceive (context : Context , intent : Intent ) {
6890 val success = intent.getBooleanExtra(WifiManager .EXTRA_RESULTS_UPDATED , false )
@@ -71,6 +93,8 @@ class MainActivity : Activity() {
7193 } else {
7294 Log .d(" MainActivity" , " WiFi Scan failed" )
7395 }
96+ // Dismiss the loading dialog once scan results are ready
97+ dismissLoadingDialog()
7498 unregisterReceiver(this )
7599 }
76100 }
@@ -82,26 +106,26 @@ class MainActivity : Activity() {
82106 val success = wifiManager.startScan()
83107 if (! success) {
84108 Log .d(" MainActivity" , " WiFi Scan initiation failed" )
109+ dismissLoadingDialog() // Dismiss loading dialog in case of failure
85110 }
86111 }
87112
88113 @SuppressLint(" MissingPermission" )
89114 private fun displaySSIDDialog () {
90115 val scanResults = wifiManager.scanResults
91- val ssidList = scanResults
92- .filter { it.SSID .isNotEmpty() }
93- .sortedByDescending { it.level }
94- .map { it.SSID }
95- .toSet()
96- .toList()
116+ val ssidList = scanResults.filter { it.SSID .isNotEmpty() }.sortedByDescending { it.level }
117+ .map { it to getSecurityType(it) }
118+ .map { " ${it.first.SSID } (${it.second} )" } // Display SSID and security type
119+ .toSet().toList()
97120
98121 var dialog: AlertDialog ? = null
99122 val builder = AlertDialog .Builder (this )
100123 builder.setTitle(" Select SSID" )
101124
102125 val adapter = ArrayAdapter (this , android.R .layout.simple_list_item_1, ssidList)
103126 builder.setAdapter(adapter) { _, which ->
104- ssid.setText(ssidList[which])
127+ val selectedSSID = ssidList[which].split(" (" )[0 ] // Extract the SSID
128+ ssid.setText(selectedSSID)
105129 dialog?.dismiss()
106130 password.requestFocus()
107131 }
@@ -110,6 +134,35 @@ class MainActivity : Activity() {
110134 dialog = builder.show()
111135 }
112136
137+ // Function to determine the security type of the Wi-Fi network
138+ private fun getSecurityType (scanResult : ScanResult ): String {
139+ val capabilities = scanResult.capabilities
140+ return when {
141+ capabilities.contains(" WEP" ) -> " WEP"
142+ capabilities.contains(" WPA" ) -> " WPA/WPA2"
143+ capabilities.contains(" SAE" ) -> " WPA3"
144+ else -> " Open" // No security
145+ }
146+ }
147+
148+ // Show a loading dialog with a progress bar
149+ private fun showLoadingDialog () {
150+ if (loadingDialog == null ) {
151+ val builder = AlertDialog .Builder (this )
152+ val inflater = LayoutInflater .from(this )
153+ val loadingView = inflater.inflate(R .layout.loading_dialog, null )
154+ builder.setView(loadingView)
155+ builder.setCancelable(false )
156+ loadingDialog = builder.create()
157+ }
158+ loadingDialog?.show()
159+ }
160+
161+ // Dismiss the loading dialog
162+ private fun dismissLoadingDialog () {
163+ loadingDialog?.dismiss()
164+ }
165+
113166 override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
114167 super .onActivityResult(requestCode, resultCode, data)
115168 if (requestCode == RESULT_ENABLE ) {
@@ -124,4 +177,4 @@ class MainActivity : Activity() {
124177 companion object {
125178 const val RESULT_ENABLE = 1
126179 }
127- }
180+ }
0 commit comments