-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto-start on launch to devices without always-on setting
- Loading branch information
Showing
9 changed files
with
152 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
android/app/src/main/kotlin/net/mullvad/mullvadvpn/receiver/BootCompletedReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.mullvad.mullvadvpn.receiver | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.VpnService | ||
import net.mullvad.mullvadvpn.lib.common.constant.KEY_CONNECT_ACTION | ||
import net.mullvad.mullvadvpn.lib.common.constant.VPN_SERVICE_CLASS | ||
|
||
class BootCompletedReceiver : BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (intent?.action == "android.intent.action.BOOT_COMPLETED") { | ||
context?.let { startAndConnectTunnel(context) } | ||
} | ||
} | ||
|
||
private fun startAndConnectTunnel(context: Context) { | ||
// Check for vpn permission | ||
if (VpnService.prepare(context) == null) { | ||
val intent = | ||
Intent().apply { | ||
setClassName(context.packageName, VPN_SERVICE_CLASS) | ||
action = KEY_CONNECT_ACTION | ||
} | ||
context.startForegroundService(intent) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ConnectOnStartRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.mullvad.mullvadvpn.repository | ||
|
||
import android.content.ComponentName | ||
import android.content.pm.PackageManager | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED | ||
import android.content.pm.PackageManager.DONT_KILL_APP | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
class ConnectOnStartRepository( | ||
private val packageManager: PackageManager, | ||
private val bootCompletedComponentName: ComponentName | ||
) { | ||
val connectOnStart = MutableStateFlow(isConnectOnStart()) | ||
|
||
fun setConnectOnStart(connect: Boolean) { | ||
packageManager.setComponentEnabledSetting( | ||
bootCompletedComponentName, | ||
if (connect) { | ||
COMPONENT_ENABLED_STATE_ENABLED | ||
} else { | ||
COMPONENT_ENABLED_STATE_DISABLED | ||
}, | ||
DONT_KILL_APP | ||
) | ||
|
||
connectOnStart.value = isConnectOnStart() | ||
} | ||
|
||
private fun isConnectOnStart(): Boolean = | ||
when (packageManager.getComponentEnabledSetting(bootCompletedComponentName)) { | ||
COMPONENT_ENABLED_STATE_DEFAULT -> BOOT_COMPLETED_DEFAULT_STATE | ||
COMPONENT_ENABLED_STATE_ENABLED -> true | ||
COMPONENT_ENABLED_STATE_DISABLED -> false | ||
else -> error("Unknown component enabled setting") | ||
} | ||
|
||
companion object { | ||
private const val BOOT_COMPLETED_DEFAULT_STATE = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters