From 43dfbc377f6f7feab3e8cf610b61c7e258127ab9 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 12 Jun 2023 09:11:40 +0200 Subject: Fix crashes with keepalive job with API <= 23 (closes #1619) --- .../java/de/blinkt/openvpn/core/keepVPNAlive.java | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java b/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java index 2b75b2fe..5bff9eb2 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java +++ b/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java @@ -13,6 +13,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.VpnService; +import android.os.Build; import android.os.PersistableBundle; import de.blinkt.openvpn.LaunchVPN; @@ -88,8 +89,8 @@ public class keepVPNAlive extends JobService implements VpnStatus.StateListener * but we use a minimum of 5 minutes and 2 minutes to avoid problems if there is some * strange Android build that allows lower lmits. */ - long initervalMillis = Math.max(JobInfo.getMinPeriodMillis(), 5 * 60 * 1000L); - long flexMillis = Math.max(JobInfo.getMinFlexMillis(), 2 * 60 * 1000L); + long initervalMillis = Math.max(getMinPeriodMillis(), 5 * 60 * 1000L); + long flexMillis = Math.max(getMinFlexMillis(), 2 * 60 * 1000L); jib.setPeriodic(initervalMillis, flexMillis); jib.setPersisted(true); @@ -98,6 +99,24 @@ public class keepVPNAlive extends JobService implements VpnStatus.StateListener VpnStatus.logDebug("Scheduling VPN keep alive for VPN " + vp.mName); } + private static long getMinPeriodMillis() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return JobInfo.getMinPeriodMillis(); + } else { + return 15 * 60 * 1000L; // 15 minutes + } + } + + private static long getMinFlexMillis() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return JobInfo.getMinFlexMillis(); + } + else + { + return 5 * 60 * 1000L; // 5 minutes + } + } + public static void unscheduleKeepVPNAliveJobService(Context c) { JobScheduler jobScheduler = c.getSystemService(JobScheduler.class); jobScheduler.cancel(JOBID_KEEPVPNALIVE); -- cgit v1.2.3