Add read WIFI state support

This commit is contained in:
世界 2023-11-18 14:40:56 +08:00
parent fdb8288331
commit eb3bdfa415
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 41 additions and 2 deletions

View file

@ -9,6 +9,9 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission <uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES" android:name="android.permission.QUERY_ALL_PACKAGES"

View file

@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.PowerManager import android.os.PowerManager
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import go.Seq import go.Seq
@ -46,6 +47,7 @@ class Application : Application() {
val packageManager by lazy { application.packageManager } val packageManager by lazy { application.packageManager }
val powerManager by lazy { application.getSystemService<PowerManager>()!! } val powerManager by lazy { application.getSystemService<PowerManager>()!! }
val notificationManager by lazy { application.getSystemService<NotificationManager>()!! } val notificationManager by lazy { application.getSystemService<NotificationManager>()!! }
val wifiManager by lazy { application.getSystemService<WifiManager>()!! }
} }
} }

View file

@ -9,6 +9,7 @@ import io.nekohasekai.libbox.NetworkInterfaceIterator
import io.nekohasekai.libbox.PlatformInterface import io.nekohasekai.libbox.PlatformInterface
import io.nekohasekai.libbox.StringIterator import io.nekohasekai.libbox.StringIterator
import io.nekohasekai.libbox.TunOptions import io.nekohasekai.libbox.TunOptions
import io.nekohasekai.libbox.WIFIState
import io.nekohasekai.sfa.Application import io.nekohasekai.sfa.Application
import java.net.Inet6Address import java.net.Inet6Address
import java.net.InetSocketAddress import java.net.InetSocketAddress
@ -101,6 +102,15 @@ interface PlatformInterfaceWrapper : PlatformInterface {
override fun clearDNSCache() { override fun clearDNSCache() {
} }
override fun readWIFIState(): WIFIState? {
val wifiInfo = Application.wifiManager.connectionInfo ?: return null
var ssid = wifiInfo.ssid
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length - 1)
}
return WIFIState(ssid, wifiInfo.bssid)
}
private class InterfaceArray(private val iterator: Enumeration<NetworkInterface>) : private class InterfaceArray(private val iterator: Enumeration<NetworkInterface>) :
NetworkInterfaceIterator { NetworkInterfaceIterator {

View file

@ -4,10 +4,12 @@ import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.net.VpnService import android.net.VpnService
import android.os.Bundle import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@ -187,6 +189,22 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
return return
} }
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
this, Manifest.permission.ACCESS_FINE_LOCATION
)
) {
fineLocationPermissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION
)
}
}
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
if (Settings.rebuildServiceMode()) { if (Settings.rebuildServiceMode()) {
reconnect() reconnect()
@ -213,6 +231,12 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
} }
} }
private val fineLocationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) {
startService()
}
private val prepareLauncher = registerForActivityResult(PrepareService()) { private val prepareLauncher = registerForActivityResult(PrepareService()) {
if (it) { if (it) {
startService() startService()

View file

@ -6,8 +6,8 @@ buildscript {
} }
plugins { plugins {
id 'com.android.application' version '8.1.3' apply false id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.3' apply false id 'com.android.library' version '8.1.4' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
id 'com.google.devtools.ksp' version '1.9.0-1.0.12' apply false id 'com.google.devtools.ksp' version '1.9.0-1.0.12' apply false
id 'com.github.triplet.play' version '3.8.4' apply false id 'com.github.triplet.play' version '3.8.4' apply false