Skip to content

Commit

Permalink
Avoid replacing ongoing notification with "building config.." notific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
schwabe committed Nov 30, 2023
1 parent b4b37f1 commit a067d0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
22 changes: 17 additions & 5 deletions main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -537,17 +538,28 @@ 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));

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)
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions main/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit a067d0f

Please sign in to comment.