Use systemExempt foreground service type for VPNService

This commit is contained in:
armv9 2025-04-08 15:01:22 +09:00
parent 9c7e0ff353
commit e829012ee6
2 changed files with 27 additions and 21 deletions

View File

@ -13,7 +13,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
@ -258,36 +258,33 @@
<service
android:name="io.nekohasekai.sagernet.bg.ProxyService"
android:exported="false"
android:foregroundServiceType="specialUse"
android:process=":bg">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="proxy" />
</service>
android:foregroundServiceType="systemExempted"
android:process=":bg"
tools:ignore="ForegroundServicePermission" />
<service
android:name="io.nekohasekai.sagernet.bg.VpnService"
android:exported="false"
android:foregroundServiceType="specialUse"
android:foregroundServiceType="systemExempted"
android:label="@string/app_name"
android:permission="android.permission.BIND_VPN_SERVICE"
android:process=":bg">
android:process=":bg"
tools:ignore="ForegroundServicePermission">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="vpn" />
</service>
<service
android:name="io.nekohasekai.sagernet.bg.TileService"
android:exported="true"
android:foregroundServiceType="systemExempted"
android:icon="@drawable/ic_service_active"
android:label="@string/tile_title"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:process=":bg"
tools:ignore="ForegroundServicePermission"
tools:targetApi="n">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />

View File

@ -6,9 +6,10 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
import android.os.Build
import android.text.format.Formatter
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import io.nekohasekai.sagernet.Action
@ -185,14 +186,22 @@ class ServiceNotification(
private suspend fun show() =
useBuilder {
if (Build.VERSION.SDK_INT >= 34) {
(service as Service).startForeground(
notificationId,
it.build(),
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
)
} else {
(service as Service).startForeground(notificationId, it.build())
try {
if (Build.VERSION.SDK_INT >= 34) {
(service as Service).startForeground(
notificationId,
it.build(),
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
)
} else {
(service as Service).startForeground(notificationId, it.build())
}
} catch (e: Exception) {
Toast.makeText(
SagerNet.application,
"startForeground: $e",
Toast.LENGTH_LONG
).show()
}
}