Skip to content

Commit a067d0f

Browse files
committed
Avoid replacing ongoing notification with "building config.." notification
1 parent b4b37f1 commit a067d0f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Diff for: main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static de.blinkt.openvpn.core.NetworkSpace.IpAddress;
1414

1515
import android.Manifest.permission;
16+
import android.annotation.TargetApi;
1617
import android.app.Notification;
1718
import android.app.NotificationManager;
1819
import android.app.PendingIntent;
@@ -39,6 +40,7 @@
3940
import android.os.Message;
4041
import android.os.ParcelFileDescriptor;
4142
import android.os.RemoteException;
43+
import android.service.notification.StatusBarNotification;
4244
import android.system.OsConstants;
4345
import android.text.TextUtils;
4446
import android.util.Base64;
@@ -113,7 +115,6 @@ static class TunConfig {
113115
private Thread mProcessThread = null;
114116
private VpnProfile mProfile;
115117

116-
117118
private DeviceStateReceiver mDeviceStateReceiver;
118119
private boolean mDisplayBytecount = false;
119120
private boolean mStarting = false;
@@ -537,17 +538,28 @@ public int onStartCommand(Intent intent, int flags, int startId) {
537538

538539
// Always show notification here to avoid problem with startForeground timeout
539540
VpnStatus.logInfo(R.string.building_configration);
540-
VpnStatus.updateStateString("VPN_GENERATE_CONFIG", "", R.string.building_configration, ConnectionStatus.LEVEL_START);
541-
showNotification(VpnStatus.getLastCleanLogMessage(this),
542-
VpnStatus.getLastCleanLogMessage(this), NOTIFICATION_CHANNEL_NEWSTATUS_ID, 0, ConnectionStatus.LEVEL_START, null);
543541

542+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M || (!foregroundNotificationVisible())) {
543+
544+
VpnStatus.updateStateString("VPN_GENERATE_CONFIG", "", R.string.building_configration, ConnectionStatus.LEVEL_START);
545+
showNotification(VpnStatus.getLastCleanLogMessage(this),
546+
VpnStatus.getLastCleanLogMessage(this), NOTIFICATION_CHANNEL_NEWSTATUS_ID, 0, ConnectionStatus.LEVEL_START, null);
547+
}
544548

545549
/* start the OpenVPN process itself in a background thread */
546550
mCommandHandler.post(() -> startOpenVPN(intent, startId));
547551

548552
return START_STICKY;
549553
}
550554

555+
@TargetApi(Build.VERSION_CODES.M)
556+
private boolean foregroundNotificationVisible() {
557+
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
558+
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
559+
/* Assume for simplicity that all our notifications are foreground */
560+
return notifications.length > 0;
561+
}
562+
551563
@RequiresApi(Build.VERSION_CODES.N_MR1)
552564
private void updateShortCutUsage(VpnProfile profile) {
553565
if (profile == null)
@@ -652,7 +664,7 @@ private void startOpenVPN(Intent intent, int startId) {
652664
if (mProfile != null && mProfile == vp && (intent == null || noReplaceRequested))
653665
{
654666
/* we do not want to replace the running VPN */
655-
VpnStatus.logInfo("VPN already running. Ignoring request to start VPN");
667+
VpnStatus.logInfo(R.string.ignore_vpn_start_request, mProfile.getName());
656668
return;
657669
}
658670

Diff for: main/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,5 @@
510510
<string name="proxy_auth_username">Username</string>
511511
<string name="permission_requested">Permission to start a VPN connection is required</string>
512512
<string name="missing_vpn_permission_log">VPN Service is missing permission to connect a VPN. Requesting permission via notification.</string>
513+
<string name="ignore_vpn_start_request">VPN already running (%s). Ignoring request to start VPN.</string>
513514
</resources>

0 commit comments

Comments
 (0)