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
This commit is contained in:
emanuele-f 2022-09-01 22:28:39 +02:00
parent 234bd21867
commit 1a8b8ad52f

View File

@ -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);
}
}