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 c555fd27f..6f20d4145 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.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.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 @@ static class TunConfig { private Thread mProcessThread = null; private VpnProfile mProfile; - private DeviceStateReceiver mDeviceStateReceiver; private boolean mDisplayBytecount = false; private boolean mStarting = false; @@ -537,10 +538,13 @@ public int onStartCommand(Intent intent, int flags, int startId) { // 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 int onStartCommand(Intent intent, int flags, int startId) { 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 @@ private void startOpenVPN(Intent intent, int startId) { 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 88f98a174..fd0b30ce7 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.