Commit e8c8cbe7 authored by cyberta's avatar cyberta Committed by kwadronaut
Browse files

add new flag FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED to start VPN service....

add new flag FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED to start VPN service. fixes crash on Android API 34+ after target API upgrade
parent eb92700e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED"/>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

    <queries>
@@ -54,6 +55,7 @@
        <service
            android:name="org.torproject.vpn.vpn.TorVpnService"
            android:exported="false"
            android:foregroundServiceType="systemExempted"
            android:permission="android.permission.BIND_VPN_SERVICE">
            <intent-filter>
                <action android:name="android.net.VpnService" />
+4 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package org.torproject.vpn.vpn
import android.app.Notification
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo.*
import android.net.VpnService
import android.os.Build
import android.os.Handler
@@ -10,6 +11,7 @@ import android.os.IBinder
import android.os.ParcelFileDescriptor
import android.system.OsConstants
import android.util.Log
import androidx.core.app.ServiceCompat
import androidx.lifecycle.Observer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -75,8 +77,8 @@ class TorVpnService : VpnService() {
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(TAG, "service: onStartCommand")

        val notification: Notification? = notificationManager.buildForegroundServiceNotification()
        startForeground(VpnNotificationManager.NOTIFICATION_ID, notification)
        val notification: Notification = notificationManager.buildForegroundServiceNotification()
        ServiceCompat.startForeground(this, VpnNotificationManager.NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED)
        val action = if (intent != null) intent.action else ""
        val isAlwaysOn =  (intent == null || intent.component == null || intent.component!!.packageName != packageName) && Build.VERSION.SDK_INT >= ALWAYS_ON_MIN_API_LEVEL
        if (isAlwaysOn) {