diff options
author | Arne Schwabe <arne@rfc2549.org> | 2023-08-23 17:43:25 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2023-08-23 18:12:02 +0200 |
commit | a3607c6850280f6c0144f95f2b5d2674de93a510 (patch) | |
tree | 1a6efc58c1f3d52a6e8ee4055d8af3c822146d95 /main | |
parent | 707acc99ac525b61bab813bb56d0573e6bed1306 (diff) |
Really fix crashes with keepalive job with API <= 23 (closes #1619)
Diffstat (limited to 'main')
-rw-r--r-- | main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java | 35 |
1 files changed, 22 insertions, 13 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 5bff9eb2..b4264aba 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java +++ b/main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java @@ -90,35 +90,44 @@ public class keepVPNAlive extends JobService implements VpnStatus.StateListener * strange Android build that allows lower lmits. */ long initervalMillis = Math.max(getMinPeriodMillis(), 5 * 60 * 1000L); - long flexMillis = Math.max(getMinFlexMillis(), 2 * 60 * 1000L); - jib.setPeriodic(initervalMillis, flexMillis); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + long flexMillis = Math.max(JobInfo.getMinFlexMillis(), 2 * 60 * 1000L); + jib.setPeriodic(initervalMillis, flexMillis); + } + else + { + jib.setPeriodic(initervalMillis); + } jib.setPersisted(true); - JobScheduler jobScheduler = c.getSystemService(JobScheduler.class); + JobScheduler jobScheduler = null; + jobScheduler = getJobScheduler(c); + jobScheduler.schedule(jib.build()); VpnStatus.logDebug("Scheduling VPN keep alive for VPN " + vp.mName); } - private static long getMinPeriodMillis() { + private static JobScheduler getJobScheduler(Context c) { + JobScheduler jobScheduler; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return JobInfo.getMinPeriodMillis(); + jobScheduler = c.getSystemService(JobScheduler.class); + } else { - return 15 * 60 * 1000L; // 15 minutes + jobScheduler = (JobScheduler) c.getSystemService(JOB_SCHEDULER_SERVICE); } + return jobScheduler; } - private static long getMinFlexMillis() { + private static long getMinPeriodMillis() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - return JobInfo.getMinFlexMillis(); - } - else - { - return 5 * 60 * 1000L; // 5 minutes + return JobInfo.getMinPeriodMillis(); + } else { + return 15 * 60 * 1000L; // 15 minutes } } public static void unscheduleKeepVPNAliveJobService(Context c) { - JobScheduler jobScheduler = c.getSystemService(JobScheduler.class); + JobScheduler jobScheduler = getJobScheduler(c); jobScheduler.cancel(JOBID_KEEPVPNALIVE); VpnStatus.logDebug("Unscheduling VPN keep alive"); } |