diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-09 00:13:54 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-07-05 21:41:01 +0200 |
commit | 0ab56fafb97614bec7b7ee479bbf227744363320 (patch) | |
tree | 801b73f312cd4d168c4fad4c4e71ea5bd84ed817 /tunnel/src/main/java | |
parent | b6557cc57a97cf543c69251e00fe82794b93dcff (diff) |
WIP: AlarmManager
Diffstat (limited to 'tunnel/src/main/java')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index 6695c81e..11928d46 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -5,6 +5,7 @@ package com.wireguard.android.backend; +import android.app.AlarmManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -23,6 +24,7 @@ import android.os.HandlerThread; import android.os.Message; import android.os.ParcelFileDescriptor; import android.os.Process; +import android.os.SystemClock; import android.system.OsConstants; import android.util.Log; @@ -404,6 +406,10 @@ public final class GoBackend implements Backend { } private void Dhcp(VpnService service) throws Exception{ + if (currentConfig == null || currentTunnel == null || currentTunnelHandle < 0) { + return; + } + obtainDhcpLease = false; // Heuristics: Use first ULA address as client address @@ -462,15 +468,17 @@ public final class GoBackend implements Backend { service.protect(wgGetSocketV6(currentTunnelHandle)); Log.i(TAG, "Dhcp done"); - Message msg = Message.obtain(handler, MSG_DHCP_EXPIRE); - // TODO fetch expiration time from Dhcp leases - long delayMillis = 1000 * 3600 * 12 * 3 / 4; // Renew 3/4 * 12h + long delayMillis = 1000 * 3600 * 12; // Renew 3/4 * 12h //long delayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000; - handler.sendMessageDelayed(msg, delayMillis); - bgp = new Bgp(this, channel, currentTunnel, currentTunnelHandle); - bgp.startServer(); + AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + am.setWindow(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis * 3 / 4, delayMillis / 4, null, alarmListener, handler); + + if (bgp != null) { + bgp = new Bgp(this, channel, currentTunnel, currentTunnelHandle); + bgp.startServer(); + } currentTunnel.onDhcpChange(dhcp); } @@ -759,6 +767,9 @@ public final class GoBackend implements Backend { currentTunnel = null; currentTunnelHandle = -1; currentConfig = null; + + AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + am.cancel(alarmListener); if (thread != null) { handler = null; thread.quit(); @@ -843,6 +854,8 @@ public final class GoBackend implements Backend { owner.bgp.stopServer(); owner.bgp = null; } + AlarmManager am = (AlarmManager)owner.context.getSystemService(Context.ALARM_SERVICE); + am.cancel(owner.alarmListener); if (owner.thread != null) { owner.handler = null; owner.thread.quit(); @@ -936,4 +949,13 @@ public final class GoBackend implements Backend { } } } + + private AlarmManager.OnAlarmListener alarmListener = new AlarmManager.OnAlarmListener() { + @Override + public void onAlarm() { + if (handler != null) { + handler.sendEmptyMessage(MSG_DHCP_EXPIRE); + } + } + }; } |