diff options
author | cyberta <cyberta@riseup.net> | 2024-02-23 18:47:31 +0000 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2024-02-23 18:47:31 +0000 |
commit | d097d463ba8204eae76f7dc718b362f5e1e9701b (patch) | |
tree | e3e3bf14a8bba36a6128cb4e5008aa01449d2165 /app/src/main | |
parent | 69446d5074fb5991130560abe2304a4210c2968e (diff) | |
parent | b3caf0f435c5a37dcccab73cf27d29ac31f38556 (diff) |
Merge branch 'fix_foreground_service_permissions_api_34' into 'master'
fix foreground service permissions for Android API 34
Closes #9154
See merge request leap/bitmask_android!266
Diffstat (limited to 'app/src/main')
4 files changed, 23 insertions, 5 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7d1ee39f..8cf2e6f4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,11 @@ <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" + android:minSdkVersion="34" /> + <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" + android:minSdkVersion="34" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /> <!-- Used to show all apps in the allowed Apps selection --> @@ -36,6 +41,7 @@ <service android:name="de.blinkt.openvpn.core.OpenVPNService" android:exported="false" + android:foregroundServiceType="systemExempted" android:permission="android.permission.BIND_VPN_SERVICE"> <intent-filter> <action android:name="android.net.VpnService" /> @@ -44,6 +50,7 @@ <service android:name=".eip.VoidVpnService" android:exported="false" + android:foregroundServiceType="systemExempted" android:permission="android.permission.BIND_VPN_SERVICE"> <intent-filter> <action android:name="android.net.VpnService" /> diff --git a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index 5a618f10..506b04a6 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -5,6 +5,7 @@ package de.blinkt.openvpn.core; +import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_CONNECTED; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT; import static de.blinkt.openvpn.core.NetworkSpace.IpAddress; @@ -33,6 +34,7 @@ import android.util.Log; import android.widget.Toast; import androidx.annotation.RequiresApi; +import androidx.core.app.ServiceCompat; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -1098,7 +1100,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac @Override public void onNotificationBuild(int notificationId, Notification notification) { - startForeground(notificationId, notification); + ServiceCompat.startForeground(this, notificationId, notification, FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED); } public void trigger_url_open(String info) { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index f08371c6..8e15ca3e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -16,6 +16,7 @@ */ package se.leap.bitmaskclient.eip; +import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED; import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START_ALWAYS_ON_VPN; import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START_BLOCKING_VPN; import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_STOP_BLOCKING_VPN; @@ -33,11 +34,11 @@ import android.os.ParcelFileDescriptor; import android.system.OsConstants; import android.util.Log; +import androidx.core.app.ServiceCompat; + import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.IOException; -import java.util.Observable; -import java.util.Observer; import de.blinkt.openvpn.core.ConnectionStatus; import de.blinkt.openvpn.core.VpnStatus; @@ -210,7 +211,7 @@ public class VoidVpnService extends VpnService implements PropertyChangeListener @Override public void onNotificationBuild(int notificationId, Notification notification) { - startForeground(notificationId, notification); + ServiceCompat.startForeground(this, notificationId, notification, FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED); } public void startWithForegroundNotification() { diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceCommand.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceCommand.java index 9e95700c..abc029ff 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceCommand.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceCommand.java @@ -16,6 +16,7 @@ package se.leap.bitmaskclient.tor; * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC; import static se.leap.bitmaskclient.tor.TorNotificationManager.TOR_SERVICE_NOTIFICATION_ID; import static se.leap.bitmaskclient.tor.TorStatusObservable.waitUntil; @@ -26,6 +27,7 @@ import android.os.Build; import android.util.Log; import androidx.annotation.WorkerThread; +import androidx.core.app.ServiceCompat; import org.torproject.jni.TorService; @@ -62,9 +64,15 @@ public class TorServiceCommand { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Notification notification = TorNotificationManager.buildTorForegroundNotification(context.getApplicationContext()); + if (notification == null) { + if (torServiceConnection != null) { + torServiceConnection.close(); + } + return false; + } //noinspection NewApi context.getApplicationContext().startForegroundService(torServiceIntent); - torServiceConnection.getService().startForeground(TOR_SERVICE_NOTIFICATION_ID, notification); + ServiceCompat.startForeground(torServiceConnection.getService(), TOR_SERVICE_NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC); } else { context.getApplicationContext().startService(torServiceIntent); } |