diff options
Diffstat (limited to 'tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 34 |
1 files changed, 34 insertions, 0 deletions
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 |