Skip to content

Commit e829012

Browse files
committed
Use systemExempt foreground service type for VPNService
1 parent 9c7e0ff commit e829012

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1414
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
1515
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
16-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
16+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
1717
<uses-permission android:name="android.permission.INTERNET" />
1818
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
1919
<uses-permission android:name="android.permission.WAKE_LOCK" />
@@ -258,36 +258,33 @@
258258
<service
259259
android:name="io.nekohasekai.sagernet.bg.ProxyService"
260260
android:exported="false"
261-
android:foregroundServiceType="specialUse"
262-
android:process=":bg">
263-
<property
264-
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
265-
android:value="proxy" />
266-
</service>
261+
android:foregroundServiceType="systemExempted"
262+
android:process=":bg"
263+
tools:ignore="ForegroundServicePermission" />
267264

268265
<service
269266
android:name="io.nekohasekai.sagernet.bg.VpnService"
270267
android:exported="false"
271-
android:foregroundServiceType="specialUse"
268+
android:foregroundServiceType="systemExempted"
272269
android:label="@string/app_name"
273270
android:permission="android.permission.BIND_VPN_SERVICE"
274-
android:process=":bg">
271+
android:process=":bg"
272+
tools:ignore="ForegroundServicePermission">
275273

276274
<intent-filter>
277275
<action android:name="android.net.VpnService" />
278276
</intent-filter>
279-
<property
280-
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
281-
android:value="vpn" />
282277
</service>
283278

284279
<service
285280
android:name="io.nekohasekai.sagernet.bg.TileService"
286281
android:exported="true"
282+
android:foregroundServiceType="systemExempted"
287283
android:icon="@drawable/ic_service_active"
288284
android:label="@string/tile_title"
289285
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
290286
android:process=":bg"
287+
tools:ignore="ForegroundServicePermission"
291288
tools:targetApi="n">
292289
<intent-filter>
293290
<action android:name="android.service.quicksettings.action.QS_TILE" />

app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import android.content.BroadcastReceiver
66
import android.content.Context
77
import android.content.Intent
88
import android.content.IntentFilter
9-
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
9+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
1010
import android.os.Build
1111
import android.text.format.Formatter
12+
import android.widget.Toast
1213
import androidx.core.app.NotificationCompat
1314
import androidx.core.app.NotificationManagerCompat
1415
import io.nekohasekai.sagernet.Action
@@ -185,14 +186,22 @@ class ServiceNotification(
185186

186187
private suspend fun show() =
187188
useBuilder {
188-
if (Build.VERSION.SDK_INT >= 34) {
189-
(service as Service).startForeground(
190-
notificationId,
191-
it.build(),
192-
FOREGROUND_SERVICE_TYPE_SPECIAL_USE
193-
)
194-
} else {
195-
(service as Service).startForeground(notificationId, it.build())
189+
try {
190+
if (Build.VERSION.SDK_INT >= 34) {
191+
(service as Service).startForeground(
192+
notificationId,
193+
it.build(),
194+
FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
195+
)
196+
} else {
197+
(service as Service).startForeground(notificationId, it.build())
198+
}
199+
} catch (e: Exception) {
200+
Toast.makeText(
201+
SagerNet.application,
202+
"startForeground: $e",
203+
Toast.LENGTH_LONG
204+
).show()
196205
}
197206
}
198207

0 commit comments

Comments
 (0)