Skip to content

Commit

Permalink
Really fix crashes with keepalive job with API <= 23 (closes #1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
schwabe committed Aug 23, 2023
1 parent 707acc9 commit a3607c6
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions main/src/main/java/de/blinkt/openvpn/core/keepVPNAlive.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,44 @@ public static void scheduleKeepVPNAliveJobService(Context c, VpnProfile vp) {
* 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");
}
Expand Down

0 comments on commit a3607c6

Please sign in to comment.