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