Fix WIFI state permission

This commit is contained in:
世界 2024-02-02 17:53:54 +08:00
parent f6b3fd9907
commit 3224f56f89
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 50 additions and 18 deletions

View file

@ -12,6 +12,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <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_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission <uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES" android:name="android.permission.QUERY_ALL_PACKAGES"
@ -143,7 +144,7 @@
<service <service
android:name=".bg.VPNService" android:name=".bg.VPNService"
android:exported="false" android:exported="false"
android:foregroundServiceType="specialUse" android:foregroundServiceType="specialUse|location"
android:permission="android.permission.BIND_VPN_SERVICE"> android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter> <intent-filter>
<action android:name="android.net.VpnService" /> <action android:name="android.net.VpnService" />
@ -155,7 +156,7 @@
<service <service
android:name=".bg.ProxyService" android:name=".bg.ProxyService"
android:exported="false" android:exported="false"
android:foregroundServiceType="specialUse"> android:foregroundServiceType="specialUse|location">
<property <property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="proxy" /> android:value="proxy" />

View file

@ -4,6 +4,7 @@ import android.net.Network
import android.os.Build import android.os.Build
import io.nekohasekai.libbox.InterfaceUpdateListener import io.nekohasekai.libbox.InterfaceUpdateListener
import io.nekohasekai.sfa.Application import io.nekohasekai.sfa.Application
import java.net.NetworkInterface
object DefaultNetworkMonitor { object DefaultNetworkMonitor {
@ -35,8 +36,22 @@ object DefaultNetworkMonitor {
newNetwork: Network? newNetwork: Network?
) { ) {
val listener = listener ?: return val listener = listener ?: return
val link = Application.connectivity.getLinkProperties(newNetwork ?: return) ?: return if (newNetwork != null) {
listener.updateDefaultInterface(link.interfaceName, -1) val interfaceName =
(Application.connectivity.getLinkProperties(newNetwork) ?: return).interfaceName
for (times in 0 until 10) {
var interfaceIndex: Int
try {
interfaceIndex = NetworkInterface.getByName(interfaceName).index
} catch (e: Exception) {
Thread.sleep(100)
continue
}
listener.updateDefaultInterface(interfaceName, interfaceIndex)
}
} else {
listener.updateDefaultInterface("", -1)
}
} }

View file

@ -101,7 +101,7 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
profile.host profile.host
) )
) )
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(R.string.ok) { _, _ ->
startActivity(Intent(this, NewProfileActivity::class.java).apply { startActivity(Intent(this, NewProfileActivity::class.java).apply {
putExtra("importName", profile.name) putExtra("importName", profile.name)
putExtra("importURL", profile.url) putExtra("importURL", profile.url)
@ -121,7 +121,7 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
content.name content.name
) )
) )
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(R.string.ok) { _, _ ->
lifecycleScope.launch { lifecycleScope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
runCatching { runCatching {
@ -186,14 +186,14 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun startService(skipRequestFineLocation: Boolean = false) { fun startService(skipRequestLocation: Boolean = false) {
if (!ServiceNotification.checkPermission()) { if (!ServiceNotification.checkPermission()) {
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
return return
} }
// MIUI always return false for shouldShowRequestPermissionRationale // MIUI always return false for shouldShowRequestPermissionRationale
if (!skipRequestFineLocation && ContextCompat.checkSelfPermission( if (!skipRequestLocation && ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION this, Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED ) != PackageManager.PERMISSION_GRANTED
) { ) {
@ -201,9 +201,20 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
this, Manifest.permission.ACCESS_FINE_LOCATION this, Manifest.permission.ACCESS_FINE_LOCATION
) )
) { ) {
fineLocationPermissionLauncher.launch( MaterialAlertDialogBuilder(this)
Manifest.permission.ACCESS_FINE_LOCATION .setTitle(R.string.location_permission_title)
.setMessage(R.string.location_permission_description)
.setPositiveButton(R.string.ok) { _, _ ->
locationPermissionLauncher.launch(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
) )
)
}
.setCancelable(false)
.show()
return
} }
} }
@ -233,8 +244,8 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
} }
} }
private val fineLocationPermissionLauncher = registerForActivityResult( private val locationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission() ActivityResultContracts.RequestMultiplePermissions()
) { ) {
startService(true) startService(true)
} }
@ -278,7 +289,7 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
override fun onServiceAlert(type: Alert, message: String?) { override fun onServiceAlert(type: Alert, message: String?) {
val builder = MaterialAlertDialogBuilder(this) val builder = MaterialAlertDialogBuilder(this)
builder.setPositiveButton(android.R.string.ok, null) builder.setPositiveButton(R.string.ok, null)
when (type) { when (type) {
Alert.RequestVPNPermission -> { Alert.RequestVPNPermission -> {
builder.setMessage(getString(R.string.service_error_missing_permission)) builder.setMessage(getString(R.string.service_error_missing_permission))

View file

@ -48,7 +48,7 @@ class EditProfileActivity : AbstractActivity() {
}.onFailure { }.onFailure {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
errorDialogBuilder(it) errorDialogBuilder(it)
.setPositiveButton(android.R.string.ok) { _, _ -> finish() } .setPositiveButton(R.string.ok) { _, _ -> finish() }
.show() .show()
} }
} }

View file

@ -50,7 +50,7 @@ class EditProfileContentActivity : AbstractActivity() {
}.onFailure { }.onFailure {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
errorDialogBuilder(it) errorDialogBuilder(it)
.setPositiveButton(android.R.string.ok) { _, _ -> finish() } .setPositiveButton(R.string.ok) { _, _ -> finish() }
.show() .show()
} }
} }

View file

@ -185,7 +185,7 @@ class PerAppProxyActivity : AbstractActivity() {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(R.string.menu_import_from_clipboard) .setTitle(R.string.menu_import_from_clipboard)
.setMessage(R.string.message_import_from_clipboard) .setMessage(R.string.message_import_from_clipboard)
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(R.string.ok) { _, _ ->
importFromClipboard() importFromClipboard()
} }
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
@ -284,7 +284,7 @@ class PerAppProxyActivity : AbstractActivity() {
MaterialAlertDialogBuilder(this@PerAppProxyActivity) MaterialAlertDialogBuilder(this@PerAppProxyActivity)
.setTitle(R.string.title_scan_result) .setTitle(R.string.title_scan_result)
.setMessage(R.string.message_scan_app_no_apps_found) .setMessage(R.string.message_scan_app_no_apps_found)
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(R.string.ok, null)
.show() .show()
return@launch return@launch
} }

View file

@ -0,0 +1,3 @@
<resources>
<string name="ok"></string>
</resources>

View file

@ -145,5 +145,7 @@
<string name="sponsor_message">If I\'ve defended your modern life, please consider sponsoring me.</string> <string name="sponsor_message">If I\'ve defended your modern life, please consider sponsoring me.</string>
<string name="other_methods">Other methods</string> <string name="other_methods">Other methods</string>
<string name="action_start">Start</string> <string name="action_start">Start</string>
<string name="location_permission_title">Location permission</string>
<string name="location_permission_description">sing-box uses the location permission to provide `wifi_ssid` and `wifi_bssid` routing rule entries, deny it if you don\'t need this feature.</string>
</resources> </resources>