From 68daa4d2d607f73c1ef745256aa987f37d3f488f Mon Sep 17 00:00:00 2001 From: krlvm <51774833+krlvm@users.noreply.github.com> Date: Sun, 1 Sep 2024 18:56:11 +0300 Subject: [PATCH] Support for autorun on system boot --- app/src/main/AndroidManifest.xml | 12 +++++++ .../byedpi/receivers/BootReceiver.kt | 35 +++++++++++++++++++ .../byedpi/services/ServiceManager.kt | 4 +++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/main_settings.xml | 5 +++ 5 files changed, 57 insertions(+) create mode 100644 app/src/main/java/io/github/dovecoteescapee/byedpi/receivers/BootReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3dd4840..a7d8bdf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -74,6 +74,18 @@ android:name="android.service.quicksettings.action.QS_TILE"/> + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/receivers/BootReceiver.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/receivers/BootReceiver.kt new file mode 100644 index 0000000..b3371fb --- /dev/null +++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/receivers/BootReceiver.kt @@ -0,0 +1,35 @@ +package io.github.dovecoteescapee.byedpi.receivers + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.net.VpnService +import io.github.dovecoteescapee.byedpi.data.Mode +import io.github.dovecoteescapee.byedpi.services.ServiceManager +import io.github.dovecoteescapee.byedpi.utility.getPreferences +import io.github.dovecoteescapee.byedpi.utility.mode + +class BootReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val action = intent.action + if (Intent.ACTION_BOOT_COMPLETED != action + && Intent.ACTION_REBOOT != action + && "android.intent.action.QUICKBOOT_POWERON" != action + ) { + return + } + + val prefs = context.getPreferences() + + if (!prefs.getBoolean("autostart", false) || !prefs.getBoolean("was_running", false)) { + return + } + + val mode = prefs.mode() + if (prefs.mode() == Mode.VPN && VpnService.prepare(context) != null) { + return + } + + ServiceManager.start(context, mode) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/services/ServiceManager.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/services/ServiceManager.kt index 696f0b5..450e275 100644 --- a/app/src/main/java/io/github/dovecoteescapee/byedpi/services/ServiceManager.kt +++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/services/ServiceManager.kt @@ -4,9 +4,11 @@ import android.content.Context import android.content.Intent import android.util.Log import androidx.core.content.ContextCompat +import androidx.core.content.edit import io.github.dovecoteescapee.byedpi.data.Mode import io.github.dovecoteescapee.byedpi.data.START_ACTION import io.github.dovecoteescapee.byedpi.data.STOP_ACTION +import io.github.dovecoteescapee.byedpi.utility.getPreferences object ServiceManager { private val TAG: String = ServiceManager::class.java.simpleName @@ -27,6 +29,7 @@ object ServiceManager { ContextCompat.startForegroundService(context, intent) } } + context.getPreferences().edit { putBoolean("was_running", true) } } fun stop(context: Context) { @@ -46,5 +49,6 @@ object ServiceManager { ContextCompat.startForegroundService(context, intent) } } + context.getPreferences().edit { putBoolean("was_running", false) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dc26369..c4d5d28 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,4 +72,5 @@ HTTPS Protocols Uncheck all to desync all traffic + Autorun on system boot diff --git a/app/src/main/res/xml/main_settings.xml b/app/src/main/res/xml/main_settings.xml index e81fc6b..e83b882 100644 --- a/app/src/main/res/xml/main_settings.xml +++ b/app/src/main/res/xml/main_settings.xml @@ -23,6 +23,11 @@ android:defaultValue="vpn" app:useSimpleSummaryProvider="true" /> + +