yggdrasil-android/app/src/main/java/eu/neilalexander/yggdrasil/BootUpReceiver.kt
Revertron a07412d02d
Implemented Yggdrasil start after device boot (#39)
Implemented Yggdrasil start after device boot (even if Always-On VPN disabled).
2022-12-14 11:41:23 +01:00

40 lines
No EOL
1.4 KiB
Kotlin

package eu.neilalexander.yggdrasil
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.VpnService
import android.util.Log
import androidx.preference.PreferenceManager
class BootUpReceiver : BroadcastReceiver() {
companion object {
const val TAG = "BootUpReceiver"
}
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != Intent.ACTION_BOOT_COMPLETED) {
Log.w(TAG, "Wrong action: ${intent.action}")
}
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (!preferences.getBoolean(PREF_KEY_ENABLED, false)) {
Log.i(TAG, "Yggdrasil disabled, not starting service")
return
}
Log.i(TAG, "Yggdrasil enabled, starting service")
val serviceIntent = Intent(context, PacketTunnelProvider::class.java)
serviceIntent.action = PacketTunnelProvider.ACTION_START
val vpnIntent = VpnService.prepare(context)
if (vpnIntent != null) {
Log.i(TAG, "Need to ask for VPN permission")
val notification = createPermissionMissingNotification(context)
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(444, notification)
} else {
context.startService(serviceIntent)
}
}
}