Support for autorun on system boot

This commit is contained in:
krlvm 2024-09-01 18:56:11 +03:00
parent 9d289f096e
commit 68daa4d2d6
No known key found for this signature in database
GPG Key ID: B8552A91FD265536
5 changed files with 57 additions and 0 deletions

View File

@ -74,6 +74,18 @@
android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter>
</service>
<receiver
android:name=".receivers.BootReceiver"
android:exported="false">
<intent-filter android:priority="999">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>

View File

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

View File

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

View File

@ -72,4 +72,5 @@
<string name="desync_https_category">HTTPS</string>
<string name="byedpi_protocols_category">Protocols</string>
<string name="byedpi_protocols_hint">Uncheck all to desync all traffic</string>
<string name="autostart_setting">Autorun on system boot</string>
</resources>

View File

@ -23,6 +23,11 @@
android:defaultValue="vpn"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:key="autostart"
android:title="@string/autostart_setting"
android:defaultValue="false" />
<com.takisoft.preferencex.EditTextPreference
android:key="dns_ip"
android:title="@string/dbs_ip_setting"