Add option to start repeater on boot

Fix #9.
This commit is contained in:
Mygod
2018-07-22 00:53:00 +08:00
parent 2578c1c6ec
commit a0f8012e5b
7 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package be.mygod.vpnhotspot
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import be.mygod.vpnhotspot.App.Companion.app
class BootReceiver : BroadcastReceiver() {
companion object {
private val componentName by lazy { ComponentName(app, BootReceiver::class.java) }
var enabled: Boolean
get() = app.packageManager.getComponentEnabledSetting(componentName) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED
set(value) = app.packageManager.setComponentEnabledSetting(componentName,
if (value) PackageManager.COMPONENT_ENABLED_STATE_ENABLED
else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
private var started = false
}
override fun onReceive(context: Context, intent: Intent) {
if (started) return
when (intent.action) {
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_LOCKED_BOOT_COMPLETED -> started = true
else -> return
}
ContextCompat.startForegroundService(context, Intent(context, RepeaterService::class.java))
}
}

View File

@@ -8,6 +8,7 @@ import androidx.core.content.FileProvider
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.UpstreamMonitor
@@ -27,6 +28,12 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref)
addPreferencesFromResource(R.xml.pref_settings)
val mainActivity = activity as MainActivity
val boot = findPreference("service.repeater.startOnBoot") as SwitchPreference
boot.setOnPreferenceChangeListener { _, value ->
BootReceiver.enabled = value as Boolean
true
}
boot.isChecked = BootReceiver.enabled
findPreference("service.clean").setOnPreferenceClickListener {
if (Routing.clean() == null) mainActivity.snackbar().setText(R.string.root_unavailable).show()
else app.cleanRoutings()