Skip to content

Commit

Permalink
Add boot receiver to start daemon service
Browse files Browse the repository at this point in the history
  • Loading branch information
sabercodic committed Dec 12, 2023
1 parent ce78024 commit be342cf
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class VpnSettingsScreenTest {
uiState =
VpnSettingsUiState.createDefault(
quantumResistant = QuantumResistantState.Auto,
isConnectOnBootEnabled = false
),
onSelectQuantumResistanceSetting = mockSelectQuantumResistantSettingListener,
toastMessagesSharedFlow = MutableSharedFlow<String>().asSharedFlow()
Expand Down
9 changes: 9 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- https://developer.android.com/guide/components/fg-service-types#system-exempted -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature android:name="android.hardware.faketouch"
Expand Down Expand Up @@ -97,5 +98,13 @@
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<receiver android:name="net.mullvad.mullvadvpn.compose.util.BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private fun PreviewVpnSettings() {
onRestoreMtuClick = {},
onCancelMtuDialogClick = {},
onToggleAutoConnect = {},
onToggleConnectOnBoot = {},
onToggleLocalNetworkSharing = {},
onToggleDnsClick = {},
onToggleBlockAds = {},
Expand Down Expand Up @@ -143,6 +144,7 @@ fun VpnSettingsScreen(
onRestoreMtuClick: () -> Unit = {},
onCancelMtuDialogClick: () -> Unit = {},
onToggleAutoConnect: (Boolean) -> Unit = {},
onToggleConnectOnBoot: (Boolean) -> Unit = {},
onToggleLocalNetworkSharing: (Boolean) -> Unit = {},
onToggleDnsClick: (Boolean) -> Unit = {},
onToggleBlockAds: (Boolean) -> Unit = {},
Expand Down Expand Up @@ -281,6 +283,17 @@ fun VpnSettingsScreen(
onCellClicked = { newValue -> onToggleAutoConnect(newValue) }
)
}
if (uiState.isConnectOnBootEnabled != null) {
item {
Divider()
HeaderSwitchComposeCell(
title = stringResource(R.string.connect_on_boot),
isToggled = uiState.isConnectOnBootEnabled,
isEnabled = true,
onCellClicked = { newValue -> onToggleConnectOnBoot(newValue) }
)
}
}
item {
SwitchComposeSubtitleCell(text = stringResource(id = R.string.auto_connect_footer))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.mullvad.mullvadvpn.viewmodel.StagedDns
data class VpnSettingsUiState(
val mtu: String,
val isAutoConnectEnabled: Boolean,
val isConnectOnBootEnabled: Boolean?,
val isLocalNetworkSharingEnabled: Boolean,
val isCustomDnsEnabled: Boolean,
val customDnsItems: List<CustomDnsItem>,
Expand All @@ -28,6 +29,7 @@ data class VpnSettingsUiState(
fun createDefault(
mtu: String = "",
isAutoConnectEnabled: Boolean = false,
isConnectOnBootEnabled: Boolean? = null,
isLocalNetworkSharingEnabled: Boolean = false,
isCustomDnsEnabled: Boolean = false,
customDnsItems: List<CustomDnsItem> = emptyList(),
Expand All @@ -42,6 +44,7 @@ data class VpnSettingsUiState(
VpnSettingsUiState(
mtu,
isAutoConnectEnabled,
isConnectOnBootEnabled,
isLocalNetworkSharingEnabled,
isCustomDnsEnabled,
customDnsItems,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.mullvad.mullvadvpn.compose.util

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import net.mullvad.mullvadvpn.di.APP_PREFERENCES_NAME
import net.mullvad.mullvadvpn.lib.common.constant.KEY_CONNECT_ACTION
import net.mullvad.mullvadvpn.lib.common.constant.VPN_SERVICE_CLASS
import net.mullvad.mullvadvpn.repository.IS_CONNECT_ON_BOOT_ENABLED_KEY

private const val TAG = "AAAAAAAAAAAABootBroadCast"

class BootCompletedReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, mBootIntent: Intent?) {
Log.d(TAG, "AAAAA @Boot ")
if ("android.intent.action.BOOT_COMPLETED" == mBootIntent?.action) {
Log.d(TAG, "AAAAA @Boot actionCaught :" + mBootIntent.action)
// Now you are getting your Boot receiver
context?.let {
Log.d(TAG, "AAAAA @Boot context :" + mBootIntent.action)
if (isConnectOnBootEnabled(it)) {
Log.d(TAG, "AAAAA @Boot Service :" + mBootIntent.action)
startDaemonService(it)
} else {

Log.d(TAG, "AAAAA @Boot isConnectOnBootEnabled is false:")
}
}
}
}

private fun isConnectOnBootEnabled(context: Context): Boolean {
return context
.getSharedPreferences(APP_PREFERENCES_NAME, Context.MODE_PRIVATE)
.getBoolean(IS_CONNECT_ON_BOOT_ENABLED_KEY, false)
}

private fun startDaemonService(context: Context) {
val intent =
Intent().apply {
setClassName(context.packageName, VPN_SERVICE_CLASS)
action = KEY_CONNECT_ACTION
}
context.startForegroundService(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ val uiModule = module {
androidContext().getSharedPreferences(APP_PREFERENCES_NAME, Context.MODE_PRIVATE)
)
}
single { SettingsRepository(get()) }
single {
SettingsRepository(
get(),
androidContext().getSharedPreferences(APP_PREFERENCES_NAME, Context.MODE_PRIVATE),
get()
)
}
single { MullvadProblemReport(get()) }

single { AccountExpiryNotificationUseCase(get()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.mullvad.mullvadvpn.repository

import android.content.SharedPreferences
import android.content.pm.PackageManager
import java.net.InetAddress
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOf
Expand All @@ -22,10 +25,22 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.settingsListener
import net.mullvad.mullvadvpn.util.callbackFlowFromNotifier
import net.mullvad.mullvadvpn.util.flatMapReadyConnectionOrDefault

internal const val IS_CONNECT_ON_BOOT_ENABLED_KEY = "is_connect_on_boot_enabled"

class SettingsRepository(
private val serviceConnectionManager: ServiceConnectionManager,
private val sharedPreferences: SharedPreferences,
packageManager: PackageManager,
dispatcher: CoroutineDispatcher = Dispatchers.IO
) {

var isConnectOnBootEnabled: MutableStateFlow<Boolean?> =
MutableStateFlow(
if (packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK))
sharedPreferences.getBoolean(IS_CONNECT_ON_BOOT_ENABLED_KEY, false)
else null
)

val settingsUpdates: StateFlow<Settings?> =
serviceConnectionManager.connectionState
.flatMapReadyConnectionOrDefault(flowOf()) { state ->
Expand All @@ -46,8 +61,8 @@ class SettingsRepository(
DnsOptions(
state = if (isCustomDnsEnabled) DnsState.Custom else DnsState.Default,
customOptions = CustomDnsOptions(ArrayList(dnsList)),
defaultOptions = contentBlockersOptions
)
defaultOptions = contentBlockersOptions,
),
)
}

Expand All @@ -67,6 +82,11 @@ class SettingsRepository(
serviceConnectionManager.settingsListener()?.autoConnect = isEnabled
}

fun setConnectOnBoot(isEnabled: Boolean) {
sharedPreferences.edit().putBoolean(IS_CONNECT_ON_BOOT_ENABLED_KEY, isEnabled).apply()
isConnectOnBootEnabled.value = isEnabled
}

fun setLocalNetworkSharing(isEnabled: Boolean) {
serviceConnectionManager.settingsListener()?.allowLan = isEnabled
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VpnSettingsFragment : BaseFragment() {
onRestoreMtuClick = vm::onRestoreMtuClick,
onCancelMtuDialogClick = vm::onCancelDialogClick,
onToggleAutoConnect = vm::onToggleAutoConnect,
onToggleConnectOnBoot = vm::onToggleConnectOnBoot,
onToggleLocalNetworkSharing = vm::onToggleLocalNetworkSharing,
onToggleDnsClick = vm::onToggleDnsClick,
onToggleBlockAds = vm::onToggleBlockAds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ class VpnSettingsViewModel(
private val dialogState = MutableStateFlow<VpnSettingsDialogState?>(null)

private val vmState =
combine(repository.settingsUpdates, portRangeUseCase.portRanges(), dialogState) {
settings,
portRanges,
dialogState ->
combine(
repository.settingsUpdates,
portRangeUseCase.portRanges(),
dialogState,
repository.isConnectOnBootEnabled
) { settings, portRanges, dialogState, isConnectOnBootEnabled ->
VpnSettingsViewModelState(
mtuValue = settings?.mtuString() ?: "",
isAutoConnectEnabled = settings?.autoConnect ?: false,
isConnectOnBootEnabled = isConnectOnBootEnabled,
isLocalNetworkSharingEnabled = settings?.allowLan ?: false,
isCustomDnsEnabled = settings?.isCustomDnsEnabled() ?: false,
customDnsList = settings?.addresses()?.asStringAddressList() ?: listOf(),
Expand Down Expand Up @@ -227,6 +230,10 @@ class VpnSettingsViewModel(
viewModelScope.launch(dispatcher) { repository.setAutoConnect(isEnabled) }
}

fun onToggleConnectOnBoot(isEnabled: Boolean) {
viewModelScope.launch(dispatcher) { repository.setConnectOnBoot(isEnabled) }
}

fun onToggleLocalNetworkSharing(isEnabled: Boolean) {
viewModelScope.launch(dispatcher) { repository.setLocalNetworkSharing(isEnabled) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.mullvad.mullvadvpn.model.SelectedObfuscation
data class VpnSettingsViewModelState(
val mtuValue: String,
val isAutoConnectEnabled: Boolean,
val isConnectOnBootEnabled: Boolean?,
val isLocalNetworkSharingEnabled: Boolean,
val isCustomDnsEnabled: Boolean,
val isAllowLanEnabled: Boolean,
Expand All @@ -27,6 +28,7 @@ data class VpnSettingsViewModelState(
VpnSettingsUiState(
mtuValue,
isAutoConnectEnabled,
isConnectOnBootEnabled,
isLocalNetworkSharingEnabled,
isCustomDnsEnabled,
customDnsList,
Expand All @@ -46,6 +48,7 @@ data class VpnSettingsViewModelState(
VpnSettingsViewModelState(
mtuValue = EMPTY_STRING,
isAutoConnectEnabled = false,
isConnectOnBootEnabled = null,
isLocalNetworkSharingEnabled = false,
isCustomDnsEnabled = false,
customDnsList = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class VpnSettingsViewModelTest {
@Before
fun setUp() {
every { mockSettingsRepository.settingsUpdates } returns mockSettingsUpdate
every { mockSettingsRepository.isConnectOnBootEnabled } returns MutableStateFlow(null)
every { mockPortRangeUseCase.portRanges() } returns portRangeFlow

viewModel =
Expand Down
1 change: 1 addition & 0 deletions android/lib/resource/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,5 @@
<string name="loading_connecting">Connecting...</string>
<string name="loading_verifying">Verifying purchase...</string>
<string name="copied_logs_to_clipboard">Copied logs to clipboard</string>
<string name="connect_on_boot">Connect on boot</string>
</resources>
3 changes: 3 additions & 0 deletions gui/locales/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,9 @@ msgstr ""
msgid "Changes to DNS related settings might not go into effect immediately due to cached results."
msgstr ""

msgid "Connect on boot"
msgstr ""

msgid "Connecting..."
msgstr ""

Expand Down

0 comments on commit be342cf

Please sign in to comment.