Skip to content

Commit 9086d38

Browse files
Show network security, wifi toggle button and tileservice
1 parent e846f0f commit 9086d38

12 files changed

Lines changed: 194 additions & 20 deletions

File tree

app/build.gradle.kts

Lines changed: 3 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 = 2
15-
versionName = "2.0.0"
14+
versionCode = 3
15+
versionName = "3.0.0"
1616

1717
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables {
@@ -59,6 +59,7 @@ dependencies {
5959
implementation(libs.androidx.ui.graphics)
6060
implementation(libs.androidx.ui.tooling.preview)
6161
implementation(libs.androidx.material3)
62+
implementation(libs.androidx.constraintlayout)
6263
testImplementation(libs.junit)
6364
androidTestImplementation(libs.androidx.junit)
6465
androidTestImplementation(libs.androidx.espresso.core)
123 Bytes
Binary file not shown.
122 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": 2,
15-
"versionName": "2.0.0",
14+
"versionCode": 3,
15+
"versionName": "3.0.0",
1616
"outputFile": "app-release.apk"
1717
}
1818
],

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,25 @@
2020
android:supportsRtl="true"
2121
android:theme="@style/Theme.DNSLock"
2222
tools:targetApi="31">
23+
24+
<service
25+
android:name=".WifiTileService"
26+
android:exported="true"
27+
android:label="Wi-Fi Toggle"
28+
android:icon="@drawable/baseline_wifi_24"
29+
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
30+
<intent-filter>
31+
<action android:name="android.service.quicksettings.action.QS_TILE" />
32+
</intent-filter>
33+
</service>
34+
2335
<activity
2436
android:name=".MainActivity"
2537
android:exported="true">
2638
<intent-filter>
2739
<action android:name="android.intent.action.MAIN" />
2840
<category android:name="android.intent.category.LAUNCHER" />
41+
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
2942
</intent-filter>
3043
</activity>
3144

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

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import android.content.ComponentName
99
import android.content.Context
1010
import android.content.Intent
1111
import android.content.IntentFilter
12+
import android.net.wifi.ScanResult
1213
import android.net.wifi.WifiConfiguration
1314
import android.net.wifi.WifiManager
1415
import android.os.Bundle
1516
import android.util.Log
17+
import android.view.LayoutInflater
1618
import android.widget.ArrayAdapter
1719
import android.widget.Button
1820
import android.widget.EditText
21+
import android.widget.Toast
1922

2023
class 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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.cgprograms.dnslock
2+
3+
import android.content.Context
4+
import android.graphics.drawable.Icon
5+
import android.net.wifi.WifiManager
6+
import android.service.quicksettings.Tile
7+
import android.service.quicksettings.TileService
8+
import android.widget.Toast
9+
10+
class WifiTileService : TileService() {
11+
12+
private lateinit var wifiManager: WifiManager
13+
14+
override fun onTileAdded() {
15+
super.onTileAdded()
16+
updateTile()
17+
}
18+
19+
override fun onStartListening() {
20+
super.onStartListening()
21+
updateTile()
22+
}
23+
24+
override fun onClick() {
25+
super.onClick()
26+
toggleWifi()
27+
updateTile()
28+
}
29+
30+
// Toggle Wi-Fi on/off
31+
private fun toggleWifi() {
32+
wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
33+
val status = !wifiManager.isWifiEnabled
34+
wifiManager.isWifiEnabled = status
35+
val statusText = if (status) "enabled" else "disabled"
36+
Toast.makeText(this, "Wi-Fi is now $statusText", Toast.LENGTH_SHORT).show()
37+
}
38+
39+
// Update the tile based on the current Wi-Fi state
40+
private fun updateTile() {
41+
wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
42+
val tile = qsTile
43+
44+
// Check Wi-Fi state and update tile icon and label
45+
if (wifiManager.isWifiEnabled) {
46+
tile.state = Tile.STATE_ACTIVE
47+
tile.icon = Icon.createWithResource(this, R.drawable.baseline_wifi_24)
48+
tile.label = "Wi-Fi ON"
49+
} else {
50+
tile.state = Tile.STATE_INACTIVE
51+
tile.icon = Icon.createWithResource(this, R.drawable.baseline_wifi_off_24)
52+
tile.label = "Wi-Fi OFF"
53+
}
54+
55+
tile.updateTile() // Refresh the tile UI
56+
}
57+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M1,9l2,2c4.97,-4.97 13.03,-4.97 18,0l2,-2C16.93,2.93 7.08,2.93 1,9zM9,17l3,3 3,-3c-1.65,-1.66 -4.34,-1.66 -6,0zM5,13l2,2c2.76,-2.76 7.24,-2.76 10,0l2,-2C15.14,9.14 8.87,9.14 5,13z"/>
4+
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M22.99,9C19.15,5.16 13.8,3.76 8.84,4.78l2.52,2.52c3.47,-0.17 6.99,1.05 9.63,3.7l2,-2zM18.99,13c-1.29,-1.29 -2.84,-2.13 -4.49,-2.56l3.53,3.53 0.96,-0.97zM2,3.05L5.07,6.1C3.6,6.82 2.22,7.78 1,9l1.99,2c1.24,-1.24 2.67,-2.16 4.2,-2.77l2.24,2.24C7.81,10.89 6.27,11.73 5,13v0.01L6.99,15c1.36,-1.36 3.14,-2.04 4.92,-2.06L18.98,20l1.27,-1.26L3.29,1.79 2,3.05zM9,17l3,3 3,-3c-1.65,-1.66 -4.34,-1.66 -6,0z"/>
4+
5+
</vector>
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
56
android:layout_margin="16dp"
@@ -9,19 +10,34 @@
910
android:id="@+id/ssid"
1011
android:layout_width="match_parent"
1112
android:layout_height="wrap_content"
12-
android:hint="SSID"/>
13+
android:hint="SSID" />
1314

1415
<EditText
1516
android:id="@+id/password"
1617
android:layout_width="match_parent"
1718
android:layout_height="wrap_content"
18-
android:hint="Password"/>
19+
android:hint="Password" />
1920

20-
<Button
21-
android:id="@+id/add"
22-
android:layout_width="wrap_content"
21+
<androidx.constraintlayout.widget.ConstraintLayout
22+
android:layout_width="match_parent"
2323
android:layout_height="wrap_content"
24-
android:layout_marginTop="15dp"
25-
android:text="Add" />
24+
android:layout_marginTop="15dp">
25+
26+
<Button
27+
android:id="@+id/add"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="Add"
31+
app:layout_constraintStart_toStartOf="parent" />
32+
33+
<Button
34+
android:id="@+id/toggle_wifi"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:text="Toggle Wifi"
38+
app:layout_constraintEnd_toEndOf="parent" />
39+
40+
</androidx.constraintlayout.widget.ConstraintLayout>
41+
2642

2743
</LinearLayout>

0 commit comments

Comments
 (0)