summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2024-01-02 23:24:02 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2024-01-21 00:01:22 +0100
commit9e6e8154e4beada1c51dc3fe42220e50434b98fb (patch)
treef7ff74edd66372c969e7af972eecabf28405e7e4
parent8ab1a26560868f947adf9a2774d3712ae45c9ac0 (diff)
WIP: tunnel, ui: startForeground
-rw-r--r--gradle/libs.versions.toml1
-rw-r--r--tunnel/build.gradle.kts1
-rw-r--r--tunnel/src/main/AndroidManifest.xml3
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java34
-rw-r--r--ui/src/main/AndroidManifest.xml4
5 files changed, 42 insertions, 1 deletions
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index f6500fef..6fc0a2ea 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -14,6 +14,7 @@ androidx-biometric = "androidx.biometric:biometric:1.1.0"
androidx-collection = "androidx.collection:collection:1.3.0"
androidx-constraintlayout = "androidx.constraintlayout:constraintlayout:2.1.4"
androidx-coordinatorlayout = "androidx.coordinatorlayout:coordinatorlayout:1.2.0"
+androidx-core = "androidx.core:core:1.12.0"
androidx-core-ktx = "androidx.core:core-ktx:1.12.0"
androidx-datastore-preferences = "androidx.datastore:datastore-preferences:1.0.0"
androidx-fragment-ktx = "androidx.fragment:fragment-ktx:1.6.1"
diff --git a/tunnel/build.gradle.kts b/tunnel/build.gradle.kts
index 44a10696..625959e6 100644
--- a/tunnel/build.gradle.kts
+++ b/tunnel/build.gradle.kts
@@ -70,6 +70,7 @@ dependencies {
implementation(project(":bgp-java"))
implementation(libs.androidx.annotation)
implementation(libs.androidx.collection)
+ implementation(libs.androidx.core)
implementation(libs.grpc.android)
implementation(libs.grpc.okhttp)
implementation(libs.grpc.protobuf.lite)
diff --git a/tunnel/src/main/AndroidManifest.xml b/tunnel/src/main/AndroidManifest.xml
index 99509a20..bbefdb1c 100644
--- a/tunnel/src/main/AndroidManifest.xml
+++ b/tunnel/src/main/AndroidManifest.xml
@@ -9,7 +9,8 @@
<service
android:name="com.wireguard.android.backend.GoBackend$VpnService"
android:permission="android.permission.BIND_VPN_SERVICE"
- android:exported="false">
+ android:exported="false"
+ android:foregroundServiceType="systemExempted">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
index 0e9668d0..dec3021c 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -6,9 +6,12 @@
package com.wireguard.android.backend;
import android.app.AlarmManager;
+import android.app.Notification;
+import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
+import android.content.pm.ServiceInfo;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.LocalSocketAddress;
@@ -101,6 +104,10 @@ import javax.net.SocketFactory;
import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
+import androidx.core.app.NotificationCompat;
+import androidx.core.app.NotificationChannelCompat;
+import androidx.core.app.NotificationManagerCompat;
+import androidx.core.app.ServiceCompat;
/**
* Implementation of {@link Backend} that uses the wireguard-go userspace implementation to provide
@@ -113,6 +120,7 @@ public final class GoBackend implements Backend {
private static final int STATS_TAG = 2;
private static final int MSG_DHCP_EXPIRE = 1;
private static final int MSG_CAPABILITIES_CHANGED = 2;
+ private static final String NOTIFICATION_CHANNEL_ID = "WireGuardVPN";
@Nullable private static AlwaysOnCallback alwaysOnCallback;
private static GhettoCompletableFuture<VpnService> vpnService = new GhettoCompletableFuture<>();
private final Context context;
@@ -143,6 +151,18 @@ public final class GoBackend implements Backend {
String socketName = socketFile.getAbsolutePath();
Log.i(TAG, "wgStartGrpc: " + wgStartGrpc(socketName));
channel = UdsChannelBuilder.forPath(socketName, LocalSocketAddress.Namespace.FILESYSTEM).build();
+
+ NotificationManagerCompat notificationManager =
+ NotificationManagerCompat.from(context);
+
+ NotificationChannelCompat channel =
+ new NotificationChannelCompat
+ .Builder(NOTIFICATION_CHANNEL_ID,
+ NotificationManager.IMPORTANCE_DEFAULT)
+ .setName(NOTIFICATION_CHANNEL_ID)
+ .build();
+
+ notificationManager.createNotificationChannel(channel);
}
/**
@@ -911,6 +931,20 @@ public final class GoBackend implements Backend {
public void onCreate() {
vpnService.complete(this);
super.onCreate();
+
+ Notification notification =
+ new NotificationCompat.Builder(this,
+ GoBackend.NOTIFICATION_CHANNEL_ID)
+ .build();
+
+ int foregroundServiceType =
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
+ ? ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED : 0;
+
+ ServiceCompat.startForeground(this,
+ 100,
+ notification,
+ foregroundServiceType);
}
@Override
diff --git a/ui/src/main/AndroidManifest.xml b/ui/src/main/AndroidManifest.xml
index 902c6530..cf70f494 100644
--- a/ui/src/main/AndroidManifest.xml
+++ b/ui/src/main/AndroidManifest.xml
@@ -6,6 +6,10 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<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_SYSTEM_EXEMPTED"
+ android:minSdkVersion="34" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />