diff options
author | Arne Schwabe <arne@rfc2549.org> | 2023-11-30 11:56:16 +0100 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2023-11-30 12:53:17 +0100 |
commit | a067d0f480cda0e1d92572c411df1a8f6efac125 (patch) | |
tree | f89b11ffbfbef279c668a73dad6a6a54f50b9f2f /main/src | |
parent | b4b37f1531447c3abf3ef6df25d833724402713f (diff) |
Avoid replacing ongoing notification with "building config.." notification
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java | 22 | ||||
-rwxr-xr-x | main/src/main/res/values/strings.xml | 1 |
2 files changed, 18 insertions, 5 deletions
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 @@ <string name="proxy_auth_username">Username</string> <string name="permission_requested">Permission to start a VPN connection is required</string> <string name="missing_vpn_permission_log">VPN Service is missing permission to connect a VPN. Requesting permission via notification.</string> + <string name="ignore_vpn_start_request">VPN already running (%s). Ignoring request to start VPN.</string> </resources> |