|
13 | 13 | import static de.blinkt.openvpn.core.NetworkSpace.IpAddress;
|
14 | 14 |
|
15 | 15 | import android.Manifest.permission;
|
| 16 | +import android.annotation.TargetApi; |
16 | 17 | import android.app.Notification;
|
17 | 18 | import android.app.NotificationManager;
|
18 | 19 | import android.app.PendingIntent;
|
|
39 | 40 | import android.os.Message;
|
40 | 41 | import android.os.ParcelFileDescriptor;
|
41 | 42 | import android.os.RemoteException;
|
| 43 | +import android.service.notification.StatusBarNotification; |
42 | 44 | import android.system.OsConstants;
|
43 | 45 | import android.text.TextUtils;
|
44 | 46 | import android.util.Base64;
|
@@ -113,7 +115,6 @@ static class TunConfig {
|
113 | 115 | private Thread mProcessThread = null;
|
114 | 116 | private VpnProfile mProfile;
|
115 | 117 |
|
116 |
| - |
117 | 118 | private DeviceStateReceiver mDeviceStateReceiver;
|
118 | 119 | private boolean mDisplayBytecount = false;
|
119 | 120 | private boolean mStarting = false;
|
@@ -537,17 +538,28 @@ public int onStartCommand(Intent intent, int flags, int startId) {
|
537 | 538 |
|
538 | 539 | // Always show notification here to avoid problem with startForeground timeout
|
539 | 540 | 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); |
543 | 541 |
|
| 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 | + } |
544 | 548 |
|
545 | 549 | /* start the OpenVPN process itself in a background thread */
|
546 | 550 | mCommandHandler.post(() -> startOpenVPN(intent, startId));
|
547 | 551 |
|
548 | 552 | return START_STICKY;
|
549 | 553 | }
|
550 | 554 |
|
| 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 | + |
551 | 563 | @RequiresApi(Build.VERSION_CODES.N_MR1)
|
552 | 564 | private void updateShortCutUsage(VpnProfile profile) {
|
553 | 565 | if (profile == null)
|
@@ -652,7 +664,7 @@ private void startOpenVPN(Intent intent, int startId) {
|
652 | 664 | if (mProfile != null && mProfile == vp && (intent == null || noReplaceRequested))
|
653 | 665 | {
|
654 | 666 | /* 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()); |
656 | 668 | return;
|
657 | 669 | }
|
658 | 670 |
|
|
0 commit comments