update gradle, vpn permission

This commit is contained in:
Zane Schepke 2024-02-09 22:34:10 -05:00
parent 0197198f7b
commit 500b85f687
3 changed files with 24 additions and 38 deletions

View File

@ -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,

View File

@ -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<TunnelConfig?>(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()
}

View File

@ -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"