From a067d0f480cda0e1d92572c411df1a8f6efac125 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 30 Nov 2023 11:56:16 +0100 Subject: Avoid replacing ongoing notification with "building config.." notification --- .../de/blinkt/openvpn/core/OpenVPNService.java | 22 +++++++++++++++++----- main/src/main/res/values/strings.xml | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'main/src') diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index c555fd27..6f20d414 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -13,6 +13,7 @@ import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_WAITING_FOR_USER_INP import static de.blinkt.openvpn.core.NetworkSpace.IpAddress; import android.Manifest.permission; +import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -39,6 +40,7 @@ import android.os.IBinder; import android.os.Message; import android.os.ParcelFileDescriptor; import android.os.RemoteException; +import android.service.notification.StatusBarNotification; import android.system.OsConstants; import android.text.TextUtils; import android.util.Base64; @@ -113,7 +115,6 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac private Thread mProcessThread = null; private VpnProfile mProfile; - private DeviceStateReceiver mDeviceStateReceiver; private boolean mDisplayBytecount = false; private boolean mStarting = false; @@ -537,10 +538,13 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac // Always show notification here to avoid problem with startForeground timeout VpnStatus.logInfo(R.string.building_configration); - VpnStatus.updateStateString("VPN_GENERATE_CONFIG", "", R.string.building_configration, ConnectionStatus.LEVEL_START); - showNotification(VpnStatus.getLastCleanLogMessage(this), - VpnStatus.getLastCleanLogMessage(this), NOTIFICATION_CHANNEL_NEWSTATUS_ID, 0, ConnectionStatus.LEVEL_START, null); + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M || (!foregroundNotificationVisible())) { + + VpnStatus.updateStateString("VPN_GENERATE_CONFIG", "", R.string.building_configration, ConnectionStatus.LEVEL_START); + showNotification(VpnStatus.getLastCleanLogMessage(this), + VpnStatus.getLastCleanLogMessage(this), NOTIFICATION_CHANNEL_NEWSTATUS_ID, 0, ConnectionStatus.LEVEL_START, null); + } /* start the OpenVPN process itself in a background thread */ mCommandHandler.post(() -> startOpenVPN(intent, startId)); @@ -548,6 +552,14 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac return START_STICKY; } + @TargetApi(Build.VERSION_CODES.M) + private boolean foregroundNotificationVisible() { + NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications(); + /* Assume for simplicity that all our notifications are foreground */ + return notifications.length > 0; + } + @RequiresApi(Build.VERSION_CODES.N_MR1) private void updateShortCutUsage(VpnProfile profile) { if (profile == null) @@ -652,7 +664,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac if (mProfile != null && mProfile == vp && (intent == null || noReplaceRequested)) { /* we do not want to replace the running VPN */ - VpnStatus.logInfo("VPN already running. Ignoring request to start VPN"); + VpnStatus.logInfo(R.string.ignore_vpn_start_request, mProfile.getName()); return; } diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index 88f98a17..fd0b30ce 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -510,4 +510,5 @@ Username Permission to start a VPN connection is required VPN Service is missing permission to connect a VPN. Requesting permission via notification. + VPN already running (%s). Ignoring request to start VPN. -- cgit v1.2.3