diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt index 9336778..4a6c9b2 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt @@ -74,9 +74,6 @@ class MainActivity : AppCompatActivity() { lifecycleScope.launch { try { dataStoreManager.init() - if (settingsRepository.getAll().isEmpty()) { - settingsRepository.save(com.zaneschepke.wireguardautotunnel.data.model.Settings()) - } WireGuardAutoTunnel.requestTileServiceStateUpdate() } catch (e: IOException) { Timber.e("Failed to load preferences") @@ -93,35 +90,18 @@ class MainActivity : AppCompatActivity() { val snackbarHostState = remember { SnackbarHostState() } - val notificationPermissionState = - rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS) + val notificationPermissionState = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) + rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS) else null fun requestNotificationPermission() { - if ( - !notificationPermissionState.status.isGranted && - Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU + if (notificationPermissionState != null && !notificationPermissionState.status.isGranted ) { notificationPermissionState.launchPermissionRequest() } } - var vpnIntent by remember { mutableStateOf(GoBackend.VpnService.prepare(this)) } - val vpnActivityResultState = - rememberLauncherForActivityResult( - ActivityResultContracts.StartActivityForResult(), - onResult = { - val accepted = (it.resultCode == RESULT_OK) - if (accepted) { - vpnIntent = null - } - }, - ) - LaunchedEffect(vpnIntent) { - if (vpnIntent != null) { - vpnActivityResultState.launch(vpnIntent) - } else { - requestNotificationPermission() - } + LaunchedEffect(Unit) { + requestNotificationPermission() } fun showSnackBarMessage(message: String) { @@ -156,7 +136,7 @@ class MainActivity : AppCompatActivity() { }, modifier = Modifier.focusable().focusProperties { up = focusRequester }, bottomBar = - if (vpnIntent == null && notificationPermissionState.status.isGranted) { + if (notificationPermissionState != null && notificationPermissionState.status.isGranted) { { BottomNavBar( navController, @@ -171,16 +151,7 @@ class MainActivity : AppCompatActivity() { {} }, ) { padding -> - if (vpnIntent != null) { - PermissionRequestFailedScreen( - padding = padding, - onRequestAgain = { vpnActivityResultState.launch(vpnIntent) }, - message = getString(R.string.vpn_permission_required), - getString(R.string.retry), - ) - return@Scaffold - } - if (!notificationPermissionState.status.isGranted) { + if (notificationPermissionState != null && !notificationPermissionState.status.isGranted) { PermissionRequestFailedScreen( padding = padding, onRequestAgain = { @@ -231,7 +202,6 @@ class MainActivity : AppCompatActivity() { composable("${Screen.Config.route}/{id}") { val id = it.arguments?.getString("id") if (!id.isNullOrBlank()) { - // https://dagger.dev/hilt/view-model#assisted-injection ConfigScreen( navController = navController, id = id, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt index 88d9855..5a2255b 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt @@ -7,6 +7,7 @@ import android.content.pm.PackageManager import android.os.Build import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AppCompatActivity import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut @@ -88,6 +89,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavController import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanOptions +import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.Tunnel import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel @@ -131,6 +133,17 @@ fun MainScreen( var selectedTunnel by remember { mutableStateOf(null) } val uiState by viewModel.uiState.collectAsStateWithLifecycle() + var vpnIntent by remember { mutableStateOf(GoBackend.VpnService.prepare(WireGuardAutoTunnel.instance)) } + val vpnActivityResultState = + rememberLauncherForActivityResult( + ActivityResultContracts.StartActivityForResult(), + onResult = { + val accepted = (it.resultCode == AppCompatActivity.RESULT_OK) + if (accepted) { + vpnIntent = null + } + }, + ) LaunchedEffect(uiState.loading) { if (!uiState.loading && WireGuardAutoTunnel.isRunningOnAndroidTv()) { delay(Constants.FOCUS_REQUEST_DELAY) @@ -254,6 +267,9 @@ fun MainScreen( } fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) { + if (vpnIntent != null) { + return vpnActivityResultState.launch(vpnIntent) + } if (checked) viewModel.onTunnelStart(tunnel) else viewModel.onTunnelStop() } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bd190a8..fbab400 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,7 +21,7 @@ navigationCompose = "2.7.6" roomVersion = "2.6.1" timber = "5.0.1" tunnel = "1.0.20230706" -androidGradlePlugin = "8.2.2" +androidGradlePlugin = "8.3.0-rc01" kotlin = "1.9.22" ksp = "1.9.22-1.0.16" composeBom = "2024.01.00"