From 1a8b8ad52fd2b26c632085a145d006d365e8bf73 Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Thu, 1 Sep 2022 22:28:39 +0200 Subject: [PATCH] Fix capture stopped with always-on and start on boot When both always-on VPN and start-on boot are enabled, PCAPdroid receives the ACTION_BOOT_COMPLETED intent when the capture is already running. This caused it to try to restart the capture, which is not supported, so the capture was just stopped Fixes #250 --- .../remote_capture/BootReceiver.java | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/emanuelef/remote_capture/BootReceiver.java b/app/src/main/java/com/emanuelef/remote_capture/BootReceiver.java index 6a565f82..e74e51ad 100644 --- a/app/src/main/java/com/emanuelef/remote_capture/BootReceiver.java +++ b/app/src/main/java/com/emanuelef/remote_capture/BootReceiver.java @@ -46,22 +46,30 @@ public class BootReceiver extends BroadcastReceiver { return; } - if(Prefs.startAtBoot(prefs)) { - CaptureSettings settings = new CaptureSettings(prefs); + if(!Prefs.startAtBoot(prefs)) + return; - if(!settings.root_capture) { - Intent vpnPrepareIntent = VpnService.prepare(context); - if(vpnPrepareIntent != null) { - // Cannot perform the VPN setup without an Activity - Utils.showToastLong(context, R.string.vpn_setup_failed); - return; - } - } - - Log.i(TAG, "Starting capture service"); - Intent capIntent = new Intent(context, CaptureService.class); - capIntent.putExtra("settings", settings); - ContextCompat.startForegroundService(context, capIntent); + if(CaptureService.isServiceActive()) { + // this can happen, for example, if always-on VPN is enabled, which causes PCAPdroid + // to be started early + Log.i(TAG, "Service already active, nothing to do"); + return; } + + CaptureSettings settings = new CaptureSettings(prefs); + + if(!settings.root_capture) { + Intent vpnPrepareIntent = VpnService.prepare(context); + if(vpnPrepareIntent != null) { + // Cannot perform the VPN setup without an Activity + Utils.showToastLong(context, R.string.vpn_setup_failed); + return; + } + } + + Log.i(TAG, "Starting capture service"); + Intent capIntent = new Intent(context, CaptureService.class); + capIntent.putExtra("settings", settings); + ContextCompat.startForegroundService(context, capIntent); } }