diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-09 00:13:54 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-10-19 22:54:24 +0200 |
commit | 7e8e4019e959242555a3d7acd2c701dc6220a202 (patch) | |
tree | 1ef1b7295bf51a72a0b9d77989b316edc5cc81d4 | |
parent | 26de288e15d020d8a099b54d782532c1933c6563 (diff) |
WIP: AlarmManager
-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); + } + } + }; } |