35 lines
1.3 KiB
Kotlin
35 lines
1.3 KiB
Kotlin
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
|
|
}
|
|
if (RepeaterService.supported) {
|
|
ContextCompat.startForegroundService(context, Intent(context, RepeaterService::class.java))
|
|
}
|
|
}
|
|
}
|