Skip to content

Commit

Permalink
Fix WIFI state permission again
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Feb 16, 2024
1 parent 4504447 commit 185fc2b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 38 deletions.
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<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 android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
Expand Down Expand Up @@ -144,7 +144,7 @@
<service
android:name=".bg.VPNService"
android:exported="false"
android:foregroundServiceType="specialUse|location"
android:foregroundServiceType="specialUse"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService" />
Expand All @@ -156,7 +156,7 @@
<service
android:name=".bg.ProxyService"
android:exported="false"
android:foregroundServiceType="specialUse|location">
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="proxy" />
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.IBinder
import android.os.ParcelFileDescriptor
Expand Down Expand Up @@ -169,6 +170,18 @@ class BoxService(
}

newService.start()

if (newService.needWIFIState()) {
if (ContextCompat.checkSelfPermission(
service, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
newService.close()
stopAndAlert(Alert.RequestLocationPermission)
return
}
}

boxService = newService
commandServer?.setService(boxService)
status.postValue(Status.Started)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/nekohasekai/sfa/constant/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.nekohasekai.sfa.constant
enum class Alert {
RequestVPNPermission,
RequestNotificationPermission,
RequestLocationPermission,
EmptyConfiguration,
StartCommandServer,
CreateService,
Expand Down
85 changes: 51 additions & 34 deletions app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.net.VpnService
import android.os.Bundle
import android.os.Process
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -194,32 +194,6 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
return
}

// MIUI always return false for shouldShowRequestPermissionRationale
if (!skipRequestLocation && ContextCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
this, Manifest.permission.ACCESS_FINE_LOCATION
)
) {
MaterialAlertDialogBuilder(this)
.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
}
}

lifecycleScope.launch(Dispatchers.IO) {
if (Settings.rebuildServiceMode()) {
reconnect()
Expand All @@ -246,12 +220,6 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
}
}

private val locationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) {
startService(true)
}

private val prepareLauncher = registerForActivityResult(PrepareService()) {
if (it) {
startService()
Expand Down Expand Up @@ -290,6 +258,11 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
}

override fun onServiceAlert(type: Alert, message: String?) {
when (type) {
Alert.RequestLocationPermission -> return requestLocationPermission()
else -> {}
}

val builder = MaterialAlertDialogBuilder(this)
builder.setPositiveButton(R.string.ok, null)
when (type) {
Expand Down Expand Up @@ -320,10 +293,54 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback {
builder.setMessage(message)

}

else -> {}
}
builder.show()
}

private fun requestLocationPermission() {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.location_permission_title)
.setMessage(R.string.location_permission_description)
.setPositiveButton(R.string.ok) { _, _ ->
openPermissionSettings()
}
.setCancelable(false)
.show()
}

private fun openPermissionSettings() {
if (!getSystemProperty("ro.miui.ui.version.name").isNullOrBlank()) {
val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
intent.putExtra("extra_package_uid", Process.myUid())
intent.putExtra("extra_pkgname", packageName)
try {
startActivity(intent)
return
} catch (ignored: Exception) {
}
}

try {
val intent = Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
} catch (e: Exception) {
errorDialogBuilder(e).show()
}
}

@SuppressLint("PrivateApi")
fun getSystemProperty(key: String?): String? {
try {
return Class.forName("android.os.SystemProperties").getMethod("get", String::class.java)
.invoke(null, key) as String
} catch (ignored: Exception) {
}
return null
}

private var paused = false
override fun onPause() {
super.onPause()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@
<string name="other_methods">Other methods</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>
<string name="location_permission_description">sing-box uses the **background location** permission to implement the `wifi_ssid` and `wifi_bssid` routing rule items that **you are using**.</string>

</resources>

0 comments on commit 185fc2b

Please sign in to comment.