From 9e6e8154e4beada1c51dc3fe42220e50434b98fb Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Tue, 2 Jan 2024 23:24:02 +0100 Subject: WIP: tunnel, ui: startForeground --- gradle/libs.versions.toml | 1 + tunnel/build.gradle.kts | 1 + tunnel/src/main/AndroidManifest.xml | 3 +- .../com/wireguard/android/backend/GoBackend.java | 34 ++++++++++++++++++++++ ui/src/main/AndroidManifest.xml | 4 +++ 5 files changed, 42 insertions(+), 1 deletion(-) 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 @@ + android:exported="false" + android:foregroundServiceType="systemExempted"> 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 = 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 @@ + + -- cgit v1.2.3