From 8a8f8142112c91cc4c22f58fc135c96037691283 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 9 Jun 2023 00:13:54 +0200 Subject: WIP: AlarmManager --- .../com/wireguard/android/backend/GoBackend.java | 34 ++++++++++++++++++---- 1 file 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); + } + } + }; } -- cgit v1.2.3