mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e0e17787d | |||
| b4c5b51644 | |||
| 76191c46f3 | |||
| ecf5036f56 | |||
| 1c0d968cfb | |||
| bda1a2080a | |||
| 14a71e3118 | |||
| 57391290c5 | |||
| cd623c0c0c | |||
| 212c6cf088 | |||
| ca47127bff | |||
| e63733286c | |||
| 36c76565f7 | |||
| 47f8de8c57 | |||
| 5740012101 | |||
| 6f5bb24cfa | |||
| 5f791ffda1 | |||
| ec244eeda3 | |||
| ff2a2cc082 | |||
| a873546e9e | |||
| 757669ddbe | |||
| c71c4e5b29 | |||
| 7f0fea3766 | |||
| 53c19762ef | |||
| c98fa04f73 | |||
| aba0f7d4d3 | |||
| fa517b2124 | |||
| d7e2648393 | |||
| 53ff3bb1e5 | |||
| 97ede3d5b4 | |||
| dcd15f7bd8 | |||
| 6031d85edd | |||
| a71f8f86b1 | |||
| 007c9f4c5d |
@@ -4,7 +4,7 @@ WG Tunnel
|
|||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
An alternative Android client app for [WireGuard®](https://www.wireguard.com/)
|
An alternative Android client app for [WireGuard](https://www.wireguard.com/)
|
||||||
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
@@ -23,14 +23,14 @@ and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
|||||||
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
||||||
[](https://f-droid.org/packages/com.zaneschepke.wireguardautotunnel/)
|
[](https://f-droid.org/packages/com.zaneschepke.wireguardautotunnel/)
|
||||||
[](https://github.com/zaneschepke/fdroid)
|
[](https://github.com/zaneschepke/fdroid)
|
||||||
|
[](https://apps.obtainium.imranr.dev/redirect?r=obtainium://app/%7B%22id%22%3A%22com.zaneschepke.wireguardautotunnel%22%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fzaneschepke%2Fwgtunnel%22%2C%22author%22%3A%22zaneschepke%22%2C%22name%22%3A%22WG%20Tunnel%22%2C%22preferredApkIndex%22%3A0%2C%22additionalSettings%22%3A%22%7B%5C%22includePrereleases%5C%22%3Afalse%2C%5C%22fallbackToOlderReleases%5C%22%3Atrue%2C%5C%22filterReleaseTitlesByRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22filterReleaseNotesByRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22verifyLatestTag%5C%22%3Atrue%2C%5C%22sortMethodChoice%5C%22%3A%5C%22date%5C%22%2C%5C%22useLatestAssetDateAsReleaseDate%5C%22%3Afalse%2C%5C%22releaseTitleAsVersion%5C%22%3Afalse%2C%5C%22trackOnly%5C%22%3Afalse%2C%5C%22versionExtractionRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22matchGroupToUse%5C%22%3A%5C%22%5C%22%2C%5C%22versionDetection%5C%22%3Atrue%2C%5C%22releaseDateAsVersion%5C%22%3Afalse%2C%5C%22useVersionCodeAsOSVersion%5C%22%3Afalse%2C%5C%22apkFilterRegEx%5C%22%3A%5C%22%5C%22%2C%5C%22invertAPKFilter%5C%22%3Afalse%2C%5C%22autoApkFilterByArch%5C%22%3Atrue%2C%5C%22appName%5C%22%3A%5C%22WG%20Tunnel%5C%22%2C%5C%22appAuthor%5C%22%3A%5C%22Zane%20Schepke%5C%22%2C%5C%22shizukuPretendToBeGooglePlay%5C%22%3Afalse%2C%5C%22allowInsecure%5C%22%3Afalse%2C%5C%22exemptFromBackgroundUpdates%5C%22%3Afalse%2C%5C%22skipUpdateNotifications%5C%22%3Afalse%2C%5C%22about%5C%22%3A%5C%22%5C%22%2C%5C%22refreshBeforeDownload%5C%22%3Afalse%7D%22%2C%22overrideSource%22%3Anull%7D)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
[](https://discord.gg/rbRRNh6H7V)
|
[<img src="https://img.shields.io/badge/Telegram-26A5E4.svg?style=for-the-badge&logo=Telegram&logoColor=white">](https://t.me/wgtunnel)
|
||||||
[](https://t.me/wgtunnel)
|
[<img src="https://img.shields.io/badge/Matrix-000000.svg?style=for-the-badge&logo=Matrix&logoColor=white">](https://matrix.to/#/#wg-tunnel-space:matrix.org)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<details open="open">
|
<details open="open">
|
||||||
@@ -49,7 +49,7 @@ and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
|||||||
<div style="text-align: left;">
|
<div style="text-align: left;">
|
||||||
|
|
||||||
## About
|
## About
|
||||||
Inspired by the official [wireguard-android](https://github.com/WireGuard/wireguard-android) app, WG Tunnel was created to address features and support missing from the official app. This app combines support for both [WireGuard®](https://www.wireguard.com/)
|
Inspired by the official [wireguard-android](https://github.com/WireGuard/wireguard-android) app, WG Tunnel was created to address features and support missing from the official app. This app combines support for both [WireGuard](https://www.wireguard.com/)
|
||||||
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/), with its primary feature of auto-tunneling (on-demand tunneling).
|
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/), with its primary feature of auto-tunneling (on-demand tunneling).
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@@ -61,14 +61,14 @@ and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/), with its pr
|
|||||||
Thank you to the following:
|
Thank you to the following:
|
||||||
|
|
||||||
- All of the users that have helped contribute to the project with ideas, translations, feedback, bug reports, testing, and donations.
|
- All of the users that have helped contribute to the project with ideas, translations, feedback, bug reports, testing, and donations.
|
||||||
- [WireGuard®](https://www.wireguard.com/) - © Jason A. Donenfeld (https://github.com/WireGuard/wireguard-android)
|
- [WireGuard](https://www.wireguard.com/) - Jason A. Donenfeld (https://github.com/WireGuard/wireguard-android)
|
||||||
|
|
||||||
- [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) - Amnezia Team (https://github.com/amnezia-vpn/amneziawg-android)
|
- [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) - Amnezia Team (https://github.com/amnezia-vpn/amneziawg-android)
|
||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 10px;">
|
<div style="display: flex; flex-wrap: wrap; justify-content: left; gap: 10px;">
|
||||||
<img label="Main" src="fastlane/metadata/android/en-US/images/phoneScreenshots/main_screen.png" width="200" />
|
<img label="Main" src="fastlane/metadata/android/en-US/images/phoneScreenshots/main_screen.png" width="200" />
|
||||||
<img label="Settings" src="fastlane/metadata/android/en-US/images/phoneScreenshots/settings_screen.png" width="200" />
|
<img label="Settings" src="fastlane/metadata/android/en-US/images/phoneScreenshots/settings_screen.png" width="200" />
|
||||||
<img label="Auto" src="fastlane/metadata/android/en-US/images/phoneScreenshots/auto_screen.png" width="200" />
|
<img label="Auto" src="fastlane/metadata/android/en-US/images/phoneScreenshots/auto_screen.png" width="200" />
|
||||||
|
|||||||
@@ -86,7 +86,6 @@ android {
|
|||||||
}
|
}
|
||||||
debug {
|
debug {
|
||||||
applicationIdSuffix = ".debug"
|
applicationIdSuffix = ".debug"
|
||||||
versionNameSuffix = "-debug"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Debug")
|
resValue("string", "app_name", "WG Tunnel - Debug")
|
||||||
isDebuggable = true
|
isDebuggable = true
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"")
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"")
|
||||||
@@ -95,7 +94,6 @@ android {
|
|||||||
create(Constants.PRERELEASE) {
|
create(Constants.PRERELEASE) {
|
||||||
initWith(buildTypes.getByName(Constants.RELEASE))
|
initWith(buildTypes.getByName(Constants.RELEASE))
|
||||||
applicationIdSuffix = ".prerelease"
|
applicationIdSuffix = ".prerelease"
|
||||||
versionNameSuffix = "-pre"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Pre")
|
resValue("string", "app_name", "WG Tunnel - Pre")
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"")
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"")
|
||||||
}
|
}
|
||||||
@@ -103,7 +101,6 @@ android {
|
|||||||
create(Constants.NIGHTLY) {
|
create(Constants.NIGHTLY) {
|
||||||
initWith(buildTypes.getByName(Constants.RELEASE))
|
initWith(buildTypes.getByName(Constants.RELEASE))
|
||||||
applicationIdSuffix = ".nightly"
|
applicationIdSuffix = ".nightly"
|
||||||
versionNameSuffix = "-nightly"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Nightly")
|
resValue("string", "app_name", "WG Tunnel - Nightly")
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"")
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"")
|
||||||
}
|
}
|
||||||
@@ -160,6 +157,7 @@ dependencies {
|
|||||||
implementation(libs.androidx.material3)
|
implementation(libs.androidx.material3)
|
||||||
implementation(libs.androidx.appcompat)
|
implementation(libs.androidx.appcompat)
|
||||||
implementation(libs.material)
|
implementation(libs.material)
|
||||||
|
implementation(libs.androidx.storage)
|
||||||
|
|
||||||
// test
|
// test
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
@@ -204,13 +202,12 @@ dependencies {
|
|||||||
implementation(libs.androidx.lifecycle.runtime.ktx)
|
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||||
implementation(libs.androidx.lifecycle.process)
|
implementation(libs.androidx.lifecycle.process)
|
||||||
|
|
||||||
// icons
|
|
||||||
implementation(libs.material.icons.extended)
|
|
||||||
// serialization
|
// serialization
|
||||||
implementation(libs.kotlinx.serialization.json)
|
implementation(libs.kotlinx.serialization.json)
|
||||||
|
|
||||||
// barcode scanning
|
// ui
|
||||||
implementation(libs.zxing.android.embedded)
|
implementation(libs.zxing.android.embedded)
|
||||||
|
implementation(libs.material.icons.extended)
|
||||||
|
|
||||||
// bio
|
// bio
|
||||||
implementation(libs.androidx.biometric.ktx)
|
implementation(libs.androidx.biometric.ktx)
|
||||||
|
|||||||
@@ -39,4 +39,4 @@
|
|||||||
-dontwarn org.joda.time.Instant
|
-dontwarn org.joda.time.Instant
|
||||||
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
||||||
-dontwarn org.slf4j.impl.StaticMDCBinder
|
-dontwarn org.slf4j.impl.StaticMDCBinder
|
||||||
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
||||||
Vendored
+1
-1
@@ -58,4 +58,4 @@
|
|||||||
-dontwarn org.joda.time.Instant
|
-dontwarn org.joda.time.Instant
|
||||||
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
||||||
-dontwarn org.slf4j.impl.StaticMDCBinder
|
-dontwarn org.slf4j.impl.StaticMDCBinder
|
||||||
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
-dontwarn org.slf4j.impl.StaticMarkerBinder
|
||||||
@@ -63,7 +63,7 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:windowSoftInputMode="adjustResize"
|
android:windowSoftInputMode="adjustNothing"
|
||||||
android:theme="@style/Theme.WireguardAutoTunnel"
|
android:theme="@style/Theme.WireguardAutoTunnel"
|
||||||
android:configChanges="orientation|screenSize|keyboardHidden"
|
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,23 +1,35 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.provider.Settings
|
||||||
import androidx.activity.SystemBarStyle
|
import androidx.activity.SystemBarStyle
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.ActivityResult
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.animation.slideInVertically
|
||||||
import androidx.compose.animation.fadeOut
|
import androidx.compose.animation.slideOutVertically
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
import androidx.compose.foundation.layout.consumeWindowInsets
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.imePadding
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Bolt
|
||||||
import androidx.compose.material.icons.rounded.Home
|
import androidx.compose.material.icons.rounded.Home
|
||||||
import androidx.compose.material.icons.rounded.QuestionMark
|
import androidx.compose.material.icons.rounded.QuestionMark
|
||||||
import androidx.compose.material.icons.rounded.Settings
|
import androidx.compose.material.icons.rounded.Settings
|
||||||
@@ -25,49 +37,62 @@ import androidx.compose.material3.MaterialTheme
|
|||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.SnackbarData
|
import androidx.compose.material3.SnackbarData
|
||||||
import androidx.compose.material3.SnackbarHost
|
import androidx.compose.material3.SnackbarHost
|
||||||
|
import androidx.compose.material3.SnackbarHostState
|
||||||
import androidx.compose.material3.surfaceColorAtElevation
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import androidx.navigation.toRoute
|
import androidx.navigation.toRoute
|
||||||
import com.zaneschepke.wireguardautotunnel.core.shortcut.ShortcutManager
|
import com.zaneschepke.networkmonitor.NetworkMonitor
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.VpnDeniedDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.CustomBottomNavbar
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.DynamicTopAppBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.currentNavBackStackEntryAsNavBarState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.OptionsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.TunnelAutoTunnelScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.PinLockScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ScannerScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.scanner.ScannerScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.SplitTunnelScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.splittunnel.SplitTunnelScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.TunnelAutoTunnelScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.tunneloptions.TunnelOptionsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.AdvancedScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.pin.PinLockScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.AppearanceScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.DisplayScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.KillSwitchScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.LanguageScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.LocationDisclosureScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.AutoTunnelScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.AppearanceScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.LogsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.display.DisplayScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.language.LanguageScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AdvancedScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.LocationDisclosureScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.killswitch.KillSwitchScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.amnezia.awg.backend.GoBackend.VpnService
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
@@ -81,8 +106,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
lateinit var tunnelManager: TunnelManager
|
lateinit var tunnelManager: TunnelManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var shortcutManager: ShortcutManager
|
lateinit var networkMonitor: NetworkMonitor
|
||||||
|
|
||||||
|
private var lastLocationPermissionState: Boolean? = null
|
||||||
|
|
||||||
|
@SuppressLint("BatteryLife")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
enableEdgeToEdge(
|
enableEdgeToEdge(
|
||||||
statusBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
statusBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
|
||||||
@@ -97,163 +125,266 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
installSplashScreen().apply {
|
installSplashScreen().apply {
|
||||||
setKeepOnScreenCondition {
|
setKeepOnScreenCondition {
|
||||||
!viewModel.isAppReady.value
|
!viewModel.appViewState.value.isAppReady
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val appUiState by viewModel.uiState.collectAsStateWithLifecycle()
|
val appUiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val configurationChange by viewModel.configurationChange.collectAsStateWithLifecycle()
|
val appViewState by viewModel.appViewState.collectAsStateWithLifecycle()
|
||||||
val navController = rememberNavController()
|
|
||||||
|
|
||||||
LaunchedEffect(configurationChange) {
|
val navController = rememberNavController()
|
||||||
if (configurationChange) {
|
val backStackEntry by navController.currentBackStackEntryAsState()
|
||||||
Intent(this@MainActivity, MainActivity::class.java).also {
|
val navBarState by currentNavBackStackEntryAsNavBarState(navController, backStackEntry, viewModel, appUiState)
|
||||||
startActivity(it)
|
val snackbar = remember { SnackbarHostState() }
|
||||||
exitProcess(0)
|
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||||
|
var vpnPermissionDenied by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
val vpnActivity =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
onResult = {
|
||||||
|
if (it.resultCode != RESULT_OK) {
|
||||||
|
showVpnPermissionDialog = true
|
||||||
|
vpnPermissionDenied = true
|
||||||
|
} else {
|
||||||
|
vpnPermissionDenied = false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
LaunchedEffect(appUiState.tunnels) {
|
||||||
|
if (!appViewState.isAppReady) {
|
||||||
|
viewModel.handleEvent(AppEvent.AppReadyCheck(appUiState.tunnels))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val batteryActivity = rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
) { _: ActivityResult ->
|
||||||
|
viewModel.handleEvent(AppEvent.SetBatteryOptimizeDisableShown)
|
||||||
|
}
|
||||||
|
|
||||||
|
with(appViewState) {
|
||||||
|
LaunchedEffect(isConfigChanged) {
|
||||||
|
if (isConfigChanged) {
|
||||||
|
Intent(this@MainActivity, MainActivity::class.java).also {
|
||||||
|
startActivity(it)
|
||||||
|
exitProcess(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(errorMessage) {
|
||||||
|
errorMessage?.let {
|
||||||
|
snackbar.showSnackbar(it.asString(this@MainActivity))
|
||||||
|
viewModel.handleEvent(AppEvent.MessageShown)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(appUiState.activeTunnels) {
|
||||||
|
appUiState.activeTunnels
|
||||||
|
.mapNotNull { (tunnelConf, tunnelState) ->
|
||||||
|
(tunnelState.status as? TunnelStatus.Error)?.let { error ->
|
||||||
|
val message = error.error.toStringRes()
|
||||||
|
val context = this@MainActivity
|
||||||
|
snackbar.showSnackbar(context.getString(R.string.tunnel_error_template, context.getString(message)))
|
||||||
|
viewModel.handleEvent(AppEvent.ClearTunnelError(tunnelConf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(popBackStack) {
|
||||||
|
if (popBackStack) {
|
||||||
|
navController.popBackStack()
|
||||||
|
viewModel.handleEvent(AppEvent.PopBackStack(false))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(requestVpnPermission) {
|
||||||
|
if (requestVpnPermission) {
|
||||||
|
if (!vpnPermissionDenied) {
|
||||||
|
vpnActivity.launch(VpnService.prepare(this@MainActivity))
|
||||||
|
} else {
|
||||||
|
showVpnPermissionDialog = true
|
||||||
|
}
|
||||||
|
viewModel.handleEvent(AppEvent.VpnPermissionRequested)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(requestBatteryPermission) {
|
||||||
|
if (requestBatteryPermission) {
|
||||||
|
batteryActivity.launch(
|
||||||
|
Intent().apply {
|
||||||
|
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||||
|
data = Uri.parse("package:${this@MainActivity.packageName}")
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
viewModel.getEmitSplitTunnelApps(this@MainActivity)
|
|
||||||
}
|
|
||||||
|
|
||||||
with(appUiState.appSettings) {
|
|
||||||
LaunchedEffect(isShortcutsEnabled) {
|
|
||||||
if (!isShortcutsEnabled) return@LaunchedEffect shortcutManager.removeShortcuts()
|
|
||||||
shortcutManager.addShortcuts()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
CompositionLocalProvider(LocalNavController provides navController) {
|
||||||
SnackbarControllerProvider { host ->
|
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
||||||
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
||||||
Scaffold(
|
|
||||||
contentWindowInsets = WindowInsets(0),
|
Scaffold(
|
||||||
snackbarHost = {
|
modifier = Modifier.pointerInput(Unit) {
|
||||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
detectTapGestures {
|
||||||
CustomSnackBar(
|
viewModel.handleEvent(AppEvent.SetSelectedTunnel(null))
|
||||||
snackbarData.visuals.message,
|
}
|
||||||
isRtl = false,
|
},
|
||||||
containerColor =
|
snackbarHost = {
|
||||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
SnackbarHost(snackbar) { snackbarData: SnackbarData ->
|
||||||
2.dp,
|
CustomSnackBar(
|
||||||
),
|
snackbarData.visuals.message,
|
||||||
)
|
isRtl = false,
|
||||||
}
|
containerColor =
|
||||||
},
|
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||||
bottomBar = {
|
2.dp,
|
||||||
BottomNavBar(
|
),
|
||||||
navController,
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
topBar = {
|
||||||
|
DynamicTopAppBar(navBarState)
|
||||||
|
},
|
||||||
|
bottomBar = {
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = navBarState.showBottom,
|
||||||
|
enter = slideInVertically(initialOffsetY = { it }),
|
||||||
|
exit = slideOutVertically(targetOffsetY = { it }),
|
||||||
|
) {
|
||||||
|
CustomBottomNavbar(
|
||||||
listOf(
|
listOf(
|
||||||
BottomNavItem(
|
BottomNavItem(
|
||||||
name = stringResource(R.string.tunnels),
|
name = stringResource(R.string.tunnels),
|
||||||
route = Route.Main,
|
route = Route.Main,
|
||||||
icon = Icons.Rounded.Home,
|
icon = Icons.Rounded.Home,
|
||||||
|
onClick = { navController.goFromRoot(Route.Main) },
|
||||||
|
),
|
||||||
|
BottomNavItem(
|
||||||
|
name = stringResource(R.string.auto_tunnel),
|
||||||
|
route = Route.AutoTunnel,
|
||||||
|
icon = Icons.Rounded.Bolt,
|
||||||
|
onClick = {
|
||||||
|
val route = if (appUiState.generalState.isLocationDisclosureShown) Route.AutoTunnel else Route.LocationDisclosure
|
||||||
|
navController.goFromRoot(route)
|
||||||
|
},
|
||||||
|
active = appUiState.isAutoTunnelActive,
|
||||||
),
|
),
|
||||||
BottomNavItem(
|
BottomNavItem(
|
||||||
name = stringResource(R.string.settings),
|
name = stringResource(R.string.settings),
|
||||||
route = Route.Settings,
|
route = Route.Settings,
|
||||||
icon = Icons.Rounded.Settings,
|
icon = Icons.Rounded.Settings,
|
||||||
|
onClick = { navController.goFromRoot(Route.Settings) },
|
||||||
),
|
),
|
||||||
BottomNavItem(
|
BottomNavItem(
|
||||||
name = stringResource(R.string.support),
|
name = stringResource(R.string.support),
|
||||||
route = Route.Support,
|
route = Route.Support,
|
||||||
icon = Icons.Rounded.QuestionMark,
|
icon = Icons.Rounded.QuestionMark,
|
||||||
|
onClick = { navController.goFromRoot(Route.Support) },
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
navBarState = navBarState,
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
) { padding ->
|
},
|
||||||
Box(modifier = Modifier.Companion.fillMaxSize().padding(padding)) {
|
) { padding ->
|
||||||
NavHost(
|
Box(
|
||||||
navController,
|
modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.surface)
|
||||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
.padding(padding)
|
||||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
.consumeWindowInsets(padding)
|
||||||
startDestination = (if (appUiState.generalState.isPinLockEnabled) Route.Lock else Route.Main),
|
.imePadding(),
|
||||||
) {
|
) {
|
||||||
composable<Route.Main> {
|
NavHost(
|
||||||
MainScreen(
|
navController,
|
||||||
uiState = appUiState,
|
startDestination = (if (appUiState.generalState.isPinLockEnabled) Route.Lock else Route.Main),
|
||||||
)
|
) {
|
||||||
}
|
composable<Route.Main> {
|
||||||
composable<Route.Settings> {
|
MainScreen(appUiState, appViewState, viewModel)
|
||||||
SettingsScreen(
|
}
|
||||||
appViewModel = viewModel,
|
composable<Route.Settings> {
|
||||||
uiState = appUiState,
|
SettingsScreen(appUiState, appViewState, viewModel)
|
||||||
)
|
}
|
||||||
}
|
composable<Route.LocationDisclosure> {
|
||||||
composable<Route.LocationDisclosure> {
|
LocationDisclosureScreen(appUiState, viewModel)
|
||||||
LocationDisclosureScreen(viewModel, appUiState)
|
}
|
||||||
}
|
composable<Route.AutoTunnel> {
|
||||||
composable<Route.AutoTunnel> {
|
AutoTunnelScreen(appUiState, viewModel)
|
||||||
AutoTunnelScreen(
|
}
|
||||||
appUiState.appSettings,
|
composable<Route.Appearance> {
|
||||||
)
|
AppearanceScreen()
|
||||||
}
|
}
|
||||||
composable<Route.Appearance> {
|
composable<Route.Language> {
|
||||||
AppearanceScreen()
|
LanguageScreen(appUiState, viewModel)
|
||||||
}
|
}
|
||||||
composable<Route.Language> {
|
composable<Route.Display> {
|
||||||
LanguageScreen(appUiState, viewModel)
|
DisplayScreen(appUiState, viewModel)
|
||||||
}
|
}
|
||||||
composable<Route.Display> {
|
composable<Route.Support> {
|
||||||
DisplayScreen(appUiState)
|
SupportScreen()
|
||||||
}
|
}
|
||||||
composable<Route.Support> {
|
composable<Route.AutoTunnelAdvanced> {
|
||||||
SupportScreen(appUiState, viewModel)
|
AdvancedScreen(appUiState)
|
||||||
}
|
}
|
||||||
composable<Route.AutoTunnelAdvanced> {
|
composable<Route.Logs> {
|
||||||
AdvancedScreen(appUiState.appSettings, viewModel)
|
LogsScreen(appViewState, viewModel)
|
||||||
}
|
}
|
||||||
composable<Route.Logs> {
|
composable<Route.Config> { backStack ->
|
||||||
LogsScreen()
|
val args = backStack.toRoute<Route.Config>()
|
||||||
}
|
val config = appUiState.tunnels.firstOrNull { it.id == args.id }
|
||||||
composable<Route.Config> { backStack ->
|
ConfigScreen(config, viewModel)
|
||||||
val args = backStack.toRoute<Route.Config>()
|
}
|
||||||
val config =
|
composable<Route.TunnelOptions> { backStack ->
|
||||||
appUiState.tunnels.firstOrNull { it.id == args.id }
|
val args = backStack.toRoute<Route.TunnelOptions>()
|
||||||
ConfigScreen(config, viewModel)
|
appUiState.tunnels.firstOrNull { it.id == args.id }?.let { config ->
|
||||||
}
|
TunnelOptionsScreen(config, appUiState, viewModel)
|
||||||
composable<Route.TunnelOptions> { backStack ->
|
|
||||||
val args = backStack.toRoute<Route.TunnelOptions>()
|
|
||||||
appUiState.tunnels.firstOrNull { it.id == args.id }?.let { config ->
|
|
||||||
OptionsScreen(config, appUiState)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
composable<Route.Lock> {
|
|
||||||
PinLockScreen(viewModel)
|
|
||||||
}
|
|
||||||
composable<Route.Scanner> {
|
|
||||||
ScannerScreen()
|
|
||||||
}
|
|
||||||
composable<Route.KillSwitch> {
|
|
||||||
KillSwitchScreen(appUiState, viewModel)
|
|
||||||
}
|
|
||||||
composable<Route.SplitTunnel> { backStack ->
|
|
||||||
val args = backStack.toRoute<Route.SplitTunnel>()
|
|
||||||
appUiState.tunnels.firstOrNull { it.id == args.id }?.let {
|
|
||||||
SplitTunnelScreen(it, viewModel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
composable<Route.TunnelAutoTunnel> { backStack ->
|
|
||||||
val args = backStack.toRoute<Route.TunnelOptions>()
|
|
||||||
appUiState.tunnels.firstOrNull { it.id == args.id }?.let {
|
|
||||||
TunnelAutoTunnelScreen(it, appUiState.appSettings)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BackHandler {
|
composable<Route.Lock> {
|
||||||
if (navController.previousBackStackEntry == null || !navController.popBackStack()) {
|
PinLockScreen(viewModel)
|
||||||
this@MainActivity.finish()
|
}
|
||||||
|
composable<Route.Scanner> {
|
||||||
|
ScannerScreen(viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.KillSwitch> {
|
||||||
|
KillSwitchScreen(appUiState, viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.SplitTunnel> {
|
||||||
|
SplitTunnelScreen(viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.TunnelAutoTunnel> { backStack ->
|
||||||
|
val args = backStack.toRoute<Route.TunnelOptions>()
|
||||||
|
appUiState.tunnels.firstOrNull { it.id == args.id }?.let {
|
||||||
|
TunnelAutoTunnelScreen(it, appUiState.appSettings, viewModel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BackHandler {
|
||||||
|
if (navController.currentDestination?.route != Route.Main::class.qualifiedName) {
|
||||||
|
navController.popBackStack()
|
||||||
|
} else {
|
||||||
|
this@MainActivity.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
checkPermissionAndNotify()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkPermissionAndNotify() {
|
||||||
|
val hasLocation = ContextCompat.checkSelfPermission(
|
||||||
|
this,
|
||||||
|
Manifest.permission.ACCESS_FINE_LOCATION,
|
||||||
|
) == PackageManager.PERMISSION_GRANTED
|
||||||
|
if (lastLocationPermissionState != hasLocation) {
|
||||||
|
Timber.d("Location permission changed to: $hasLocation")
|
||||||
|
if (hasLocation) {
|
||||||
|
networkMonitor.sendLocationPermissionsGrantedBroadcast()
|
||||||
|
}
|
||||||
|
lastLocationPermissionState = hasLocation
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
|||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||||
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
import dagger.hilt.android.HiltAndroidApp
|
import dagger.hilt.android.HiltAndroidApp
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -87,7 +86,7 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
|||||||
tunnelManager.startTunnel(it)
|
tunnelManager.startTunnel(it)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Timber.Forest.w("Always-on VPN is not enabled in app settings")
|
Timber.w("Always-on VPN is not enabled in app settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,9 +94,6 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
|||||||
ServiceWorker.start(this)
|
ServiceWorker.start(this)
|
||||||
|
|
||||||
applicationScope.launch {
|
applicationScope.launch {
|
||||||
withContext(mainDispatcher) {
|
|
||||||
if (appDataRepository.appState.isLocalLogsEnabled() && !isRunningOnTv()) logReader.start()
|
|
||||||
}
|
|
||||||
if (!appDataRepository.settings.get().isKernelEnabled) {
|
if (!appDataRepository.settings.get().isKernelEnabled) {
|
||||||
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||||
}
|
}
|
||||||
@@ -106,6 +102,9 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
|||||||
LocaleUtil.changeLocale(it)
|
LocaleUtil.changeLocale(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
appDataRepository.appState.isLocalLogsEnabled().let { enabled ->
|
||||||
|
if (enabled) logReader.start()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,6 +134,19 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
|||||||
return foreground
|
return foreground
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var lastActiveTunnels: List<Int> = emptyList()
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun getLastActiveTunnels(): List<Int> {
|
||||||
|
return lastActiveTunnels
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun setLastActiveTunnels(newTunnels: List<Int>) {
|
||||||
|
lastActiveTunnels = newTunnels
|
||||||
|
}
|
||||||
|
|
||||||
lateinit var instance: WireGuardAutoTunnel
|
lateinit var instance: WireGuardAutoTunnel
|
||||||
private set
|
private set
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -3,10 +3,10 @@ package com.zaneschepke.wireguardautotunnel.core.broadcast
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|||||||
+6
-2
@@ -4,9 +4,9 @@ import android.content.BroadcastReceiver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
@@ -36,11 +36,15 @@ class NotificationActionReceiver : BroadcastReceiver() {
|
|||||||
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
|
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
|
||||||
NotificationAction.TUNNEL_OFF.name -> {
|
NotificationAction.TUNNEL_OFF.name -> {
|
||||||
val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0)
|
val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0)
|
||||||
if (tunnelId == 0) return@launch tunnelManager.stopTunnel()
|
if (tunnelId == STOP_ALL_TUNNELS_ID) return@launch tunnelManager.stopTunnel()
|
||||||
val tunnel = tunnelRepository.getById(tunnelId)
|
val tunnel = tunnelRepository.getById(tunnelId)
|
||||||
tunnelManager.stopTunnel(tunnel)
|
tunnelManager.stopTunnel(tunnel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val STOP_ALL_TUNNELS_ID = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -3,11 +3,11 @@ package com.zaneschepke.wireguardautotunnel.core.broadcast
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -44,14 +44,14 @@ class RestartReceiver : BroadcastReceiver() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Timber.d("RestartReceiver triggered with action: ${intent.action}")
|
Timber.d("RestartReceiver triggered with action: ${intent.action}")
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
|
serviceManager.updateAutoTunnelTile()
|
||||||
applicationScope.launch(ioDispatcher) {
|
applicationScope.launch(ioDispatcher) {
|
||||||
serviceManager.updateTunnelTile()
|
|
||||||
serviceManager.updateAutoTunnelTile()
|
|
||||||
val settings = appDataRepository.settings.get()
|
val settings = appDataRepository.settings.get()
|
||||||
if (settings.isRestoreOnBootEnabled) {
|
if (settings.isRestoreOnBootEnabled) {
|
||||||
if (settings.isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) {
|
if (settings.isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) {
|
||||||
Timber.d("Starting auto-tunnel on boot/update")
|
Timber.d("Starting auto-tunnel on boot/update")
|
||||||
serviceManager.startAutoTunnel(true)
|
serviceManager.startAutoTunnel()
|
||||||
} else {
|
} else {
|
||||||
Timber.d("Restoring previous tunnel state")
|
Timber.d("Restoring previous tunnel state")
|
||||||
tunnelManager.restorePreviousState()
|
tunnelManager.restorePreviousState()
|
||||||
|
|||||||
+1
-2
@@ -4,8 +4,8 @@ import android.app.Notification
|
|||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification.NotificationChannels
|
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification.NotificationChannels
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
|
||||||
interface NotificationManager {
|
interface NotificationManager {
|
||||||
@@ -39,7 +39,6 @@ interface NotificationManager {
|
|||||||
fun show(notificationId: Int, notification: Notification)
|
fun show(notificationId: Int, notification: Notification)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val KERNEL_SERVICE_NOTIFICATION_ID = 123
|
|
||||||
const val AUTO_TUNNEL_NOTIFICATION_ID = 122
|
const val AUTO_TUNNEL_NOTIFICATION_ID = 122
|
||||||
const val VPN_NOTIFICATION_ID = 100
|
const val VPN_NOTIFICATION_ID = 100
|
||||||
const val EXTRA_ID = "id"
|
const val EXTRA_ID = "id"
|
||||||
|
|||||||
+1
-1
@@ -12,9 +12,9 @@ import android.graphics.Color
|
|||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
|
import com.zaneschepke.wireguardautotunnel.MainActivity
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.core.broadcast.NotificationActionReceiver
|
import com.zaneschepke.wireguardautotunnel.core.broadcast.NotificationActionReceiver
|
||||||
import com.zaneschepke.wireguardautotunnel.MainActivity
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager.Companion.EXTRA_ID
|
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager.Companion.EXTRA_ID
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
|||||||
+54
-84
@@ -3,12 +3,12 @@ package com.zaneschepke.wireguardautotunnel.core.service
|
|||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.net.VpnService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.tile.AutoTunnelControlTile
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.tile.TunnelControlTile
|
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||||
@@ -20,24 +20,26 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
class ServiceManager @Inject constructor(
|
class ServiceManager @Inject constructor(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||||
|
@MainDispatcher private val mainDispatcher: CoroutineDispatcher,
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
private val autoTunnelMutex = Mutex()
|
||||||
|
|
||||||
private val _autoTunnelActive = MutableStateFlow(false)
|
private val _autoTunnelActive = MutableStateFlow(false)
|
||||||
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
||||||
|
|
||||||
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
||||||
var backgroundService = CompletableDeferred<TunnelForegroundService>()
|
var backgroundService = CompletableDeferred<TunnelForegroundService>()
|
||||||
var autoTunnelTile = CompletableDeferred<AutoTunnelControlTile>()
|
|
||||||
var tunnelControlTile = CompletableDeferred<TunnelControlTile>()
|
|
||||||
|
|
||||||
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
||||||
runCatching {
|
runCatching {
|
||||||
@@ -50,111 +52,79 @@ class ServiceManager @Inject constructor(
|
|||||||
}.onFailure { Timber.e(it) }
|
}.onFailure { Timber.e(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startAutoTunnel(background: Boolean) {
|
fun hasVpnPermission(): Boolean {
|
||||||
applicationScope.launch(ioDispatcher) {
|
return VpnService.prepare(context) == null
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun startAutoTunnel() {
|
||||||
|
autoTunnelMutex.withLock {
|
||||||
val settings = appDataRepository.settings.get()
|
val settings = appDataRepository.settings.get()
|
||||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
|
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
|
||||||
if (autoTunnelService.isCompleted) {
|
if (autoTunnelService.isCompleted) {
|
||||||
_autoTunnelActive.update { true }
|
_autoTunnelActive.update { true }
|
||||||
return@launch
|
return
|
||||||
}
|
}
|
||||||
runCatching {
|
runCatching {
|
||||||
autoTunnelService = CompletableDeferred()
|
autoTunnelService = CompletableDeferred()
|
||||||
startService(AutoTunnelService::class.java, background)
|
startService(AutoTunnelService::class.java, !WireGuardAutoTunnel.isForeground())
|
||||||
val service = withTimeoutOrNull(SERVICE_START_TIMEOUT) { autoTunnelService.await() }
|
|
||||||
?: throw IllegalStateException("AutoTunnelService start timed out")
|
|
||||||
service.start()
|
|
||||||
_autoTunnelActive.update { true }
|
_autoTunnelActive.update { true }
|
||||||
updateAutoTunnelTile()
|
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
_autoTunnelActive.update { false }
|
_autoTunnelActive.update { false }
|
||||||
}
|
}
|
||||||
|
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startBackgroundService(tunnelConf: TunnelConf) {
|
suspend fun stopAutoTunnel() {
|
||||||
applicationScope.launch(ioDispatcher) {
|
autoTunnelMutex.withLock {
|
||||||
if (backgroundService.isCompleted) return@launch
|
|
||||||
runCatching {
|
|
||||||
backgroundService = CompletableDeferred()
|
|
||||||
startService(TunnelForegroundService::class.java, true)
|
|
||||||
val service = withTimeoutOrNull(SERVICE_START_TIMEOUT) { backgroundService.await() }
|
|
||||||
?: throw IllegalStateException("Background service start timed out")
|
|
||||||
service.start(tunnelConf)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stopBackgroundService() {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
if (!backgroundService.isCompleted) return@launch
|
|
||||||
runCatching {
|
|
||||||
val service = backgroundService.await()
|
|
||||||
service.stop()
|
|
||||||
backgroundService = CompletableDeferred()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toggleAutoTunnel(background: Boolean) {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
if (_autoTunnelActive.value) stopAutoTunnel() else startAutoTunnel(background)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun updateAutoTunnelTile() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
runCatching {
|
|
||||||
val service = withTimeoutOrNull(SERVICE_START_TIMEOUT) { autoTunnelTile.await() }
|
|
||||||
?: run {
|
|
||||||
context.requestAutoTunnelTileServiceUpdate()
|
|
||||||
return@withContext
|
|
||||||
}
|
|
||||||
service.updateTileState()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun updateTunnelTile() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
runCatching {
|
|
||||||
val service = withTimeoutOrNull(SERVICE_START_TIMEOUT) { tunnelControlTile.await() }
|
|
||||||
?: run {
|
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
|
||||||
return@withContext
|
|
||||||
}
|
|
||||||
service.updateTileState()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stopAutoTunnel() {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
val settings = appDataRepository.settings.get()
|
val settings = appDataRepository.settings.get()
|
||||||
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
||||||
if (!autoTunnelService.isCompleted) return@launch
|
if (!autoTunnelService.isCompleted) return
|
||||||
runCatching {
|
runCatching {
|
||||||
val service = autoTunnelService.await()
|
val service = autoTunnelService.await()
|
||||||
service.stop()
|
service.stop()
|
||||||
_autoTunnelActive.update { false }
|
_autoTunnelActive.update { false }
|
||||||
autoTunnelService = CompletableDeferred()
|
autoTunnelService = CompletableDeferred()
|
||||||
updateAutoTunnelTile()
|
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
|
withContext(mainDispatcher) { updateAutoTunnelTile() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
fun startTunnelForegroundService() {
|
||||||
const val SERVICE_START_TIMEOUT = 5_000L
|
if (backgroundService.isCompleted) return
|
||||||
|
runCatching {
|
||||||
|
backgroundService = CompletableDeferred()
|
||||||
|
startService(TunnelForegroundService::class.java, !WireGuardAutoTunnel.isForeground())
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun stopTunnelForegroundService() {
|
||||||
|
if (!backgroundService.isCompleted) return
|
||||||
|
runCatching {
|
||||||
|
val service = backgroundService.await()
|
||||||
|
service.stop()
|
||||||
|
backgroundService = CompletableDeferred()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleAutoTunnel() {
|
||||||
|
applicationScope.launch(ioDispatcher) {
|
||||||
|
if (_autoTunnelActive.value) stopAutoTunnel() else startAutoTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateAutoTunnelTile() {
|
||||||
|
context.requestAutoTunnelTileServiceUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateTunnelTile() {
|
||||||
|
context.requestTunnelTileServiceStateUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+211
-9
@@ -5,14 +5,38 @@ import android.content.Intent
|
|||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import androidx.core.app.ServiceCompat
|
import androidx.core.app.ServiceCompat
|
||||||
import androidx.lifecycle.LifecycleService
|
import androidx.lifecycle.LifecycleService
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.networkmonitor.NetworkMonitor
|
||||||
|
import com.zaneschepke.networkmonitor.NetworkStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.NonCancellable
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
|
import kotlinx.coroutines.flow.flowOn
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.isActive
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
@@ -24,9 +48,32 @@ class TunnelForegroundService : LifecycleService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var networkMonitor: NetworkMonitor
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@IoDispatcher
|
||||||
|
lateinit var ioDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var tunnelRepo: TunnelRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var tunnelManager: TunnelManager
|
||||||
|
|
||||||
|
private val isNetworkConnected = MutableStateFlow(true)
|
||||||
|
|
||||||
|
private val tunnelJobs = ConcurrentHashMap<TunnelConf, Job>()
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
serviceManager.backgroundService.complete(this)
|
serviceManager.backgroundService.complete(this)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this@TunnelForegroundService,
|
||||||
|
NotificationManager.VPN_NOTIFICATION_ID,
|
||||||
|
onCreateNotification(),
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
@@ -35,30 +82,158 @@ class TunnelForegroundService : LifecycleService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
super.onStartCommand(intent, flags, startId)
|
||||||
serviceManager.backgroundService.complete(this)
|
serviceManager.backgroundService.complete(this)
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start(tunnelConf: TunnelConf) {
|
|
||||||
ServiceCompat.startForeground(
|
ServiceCompat.startForeground(
|
||||||
this@TunnelForegroundService,
|
this@TunnelForegroundService,
|
||||||
NotificationManager.KERNEL_SERVICE_NOTIFICATION_ID,
|
NotificationManager.VPN_NOTIFICATION_ID,
|
||||||
createNotification(tunnelConf),
|
onCreateNotification(),
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
|
start()
|
||||||
|
return START_STICKY
|
||||||
|
}
|
||||||
|
|
||||||
|
fun start() = lifecycleScope.launch {
|
||||||
|
tunnelManager.activeTunnels.distinctByKeys().collect { tuns ->
|
||||||
|
if (tuns.isEmpty() && tunnelJobs.isEmpty()) return@collect
|
||||||
|
if (tuns.isEmpty() && tunnelJobs.isNotEmpty()) {
|
||||||
|
return@collect tunnelJobs.forEach { (key, _) ->
|
||||||
|
Timber.d("Stopping all tunnel jobs")
|
||||||
|
tunnelJobs[key]?.cancel()
|
||||||
|
tunnelJobs.remove(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val (jobsToStop, jobsToStart) = findMissingKeys(tuns, tunnelJobs)
|
||||||
|
if (jobsToStop.isEmpty() && jobsToStart.isEmpty()) return@collect
|
||||||
|
jobsToStop.forEach { tun ->
|
||||||
|
Timber.d("Stopping tunnel jobs for ${tun.tunName}")
|
||||||
|
tunnelJobs[tun]?.cancel()
|
||||||
|
tunnelJobs.remove(tun)
|
||||||
|
}
|
||||||
|
jobsToStart.forEach { tun ->
|
||||||
|
Timber.d("Starting tunnel jobs for ${tun.tunName}")
|
||||||
|
tunnelJobs += (tun to startTunnelJobs(tun))
|
||||||
|
}
|
||||||
|
updateServiceNotification()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Would be cool to have this include kill switch
|
||||||
|
// TODO also we need to include errors
|
||||||
|
private fun updateServiceNotification() {
|
||||||
|
val notification = when (tunnelJobs.size) {
|
||||||
|
0 -> onCreateNotification()
|
||||||
|
1 -> createTunnelNotification(tunnelJobs.keys.first())
|
||||||
|
else -> createTunnelsNotification()
|
||||||
|
}
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this@TunnelForegroundService,
|
||||||
|
NotificationManager.VPN_NOTIFICATION_ID,
|
||||||
|
notification,
|
||||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use same scope so we can cancel all of these
|
||||||
|
private fun startTunnelJobs(tunnelConf: TunnelConf) = lifecycleScope.launch {
|
||||||
|
// monitor if we have internet connectivity
|
||||||
|
launch { startNetworkMonitorJob() }
|
||||||
|
// job to trigger stats emit on interval
|
||||||
|
launch { startTunnelStatsJob(tunnelConf) }
|
||||||
|
// monitor changes to the tunnel config
|
||||||
|
launch { startTunnelConfChangesJob(tunnelConf) }
|
||||||
|
// monitor tunnel ping
|
||||||
|
launch { startPingJob(tunnelConf) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findMissingKeys(map1: Map<TunnelConf, Any>, map2: Map<TunnelConf, Any>): Pair<Set<TunnelConf>, Set<TunnelConf>> {
|
||||||
|
val missingMap1 = map2.keys - map1.keys
|
||||||
|
val missingMap2 = map1.keys - map2.keys
|
||||||
|
return missingMap1 to missingMap2
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun startTunnelConfChangesJob(tunnelConf: TunnelConf) {
|
||||||
|
tunnelRepo.flow
|
||||||
|
.flowOn(ioDispatcher)
|
||||||
|
.map { storedTunnels ->
|
||||||
|
storedTunnels.firstOrNull { it.id == tunnelConf.id }
|
||||||
|
}
|
||||||
|
.filterNotNull()
|
||||||
|
// only emit when one of these 3 values change
|
||||||
|
.distinctUntilChanged { old, new ->
|
||||||
|
old == new
|
||||||
|
}
|
||||||
|
.collect { storedTunnel ->
|
||||||
|
if (tunnelConf != storedTunnel) {
|
||||||
|
Timber.d("Config changed for ${storedTunnel.tunName}, bouncing")
|
||||||
|
// let this complete, even after cancel
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
tunnelManager.bounceTunnel(storedTunnel, TunnelStatus.StopReason.CONFIG_CHANGED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun startNetworkMonitorJob() {
|
||||||
|
networkMonitor.networkStatusFlow
|
||||||
|
.flowOn(ioDispatcher)
|
||||||
|
.collectLatest { status ->
|
||||||
|
val isAvailable = status !is NetworkStatus.Disconnected
|
||||||
|
isNetworkConnected.value = isAvailable
|
||||||
|
Timber.d("Network available: $status")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun startTunnelStatsJob(tunnel: TunnelConf) = coroutineScope {
|
||||||
|
while (isActive) {
|
||||||
|
tunnelManager.updateTunnelStatistics(tunnel)
|
||||||
|
delay(STATS_DELAY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO fix cooldown
|
||||||
|
private suspend fun startPingJob(tunnel: TunnelConf) = coroutineScope {
|
||||||
|
delay(PING_START_DELAY)
|
||||||
|
while (isActive) {
|
||||||
|
val shouldBounce = shouldBounceTunnel(tunnel)
|
||||||
|
val delayMs = if (shouldBounce) {
|
||||||
|
// let this complete, even after cancel
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
tunnelManager.bounceTunnel(tunnel, TunnelStatus.StopReason.PING)
|
||||||
|
}
|
||||||
|
tunnel.pingCooldown ?: Constants.PING_COOLDOWN
|
||||||
|
} else {
|
||||||
|
tunnel.pingInterval ?: Constants.PING_INTERVAL
|
||||||
|
}
|
||||||
|
delay(delayMs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun shouldBounceTunnel(tunnel: TunnelConf): Boolean {
|
||||||
|
if (!isNetworkConnected.value) {
|
||||||
|
Timber.d("Network disconnected, skipping ping for ${tunnel.tunName}")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return runCatching {
|
||||||
|
!tunnel.isTunnelPingable(ioDispatcher)
|
||||||
|
}.onFailure { e ->
|
||||||
|
Timber.e(e, "Ping check failed for ${tunnel.tunName}")
|
||||||
|
}.getOrDefault(true)
|
||||||
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
serviceManager.backgroundService = CompletableDeferred()
|
serviceManager.backgroundService = CompletableDeferred()
|
||||||
|
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createNotification(tunnelConf: TunnelConf): Notification {
|
private fun createTunnelNotification(tunnelConf: TunnelConf): Notification {
|
||||||
return notificationManager.createNotification(
|
return notificationManager.createNotification(
|
||||||
WireGuardNotification.NotificationChannels.VPN,
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}",
|
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}",
|
||||||
@@ -67,4 +242,31 @@ class TunnelForegroundService : LifecycleService() {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createTunnelsNotification(): Notification {
|
||||||
|
return notificationManager.createNotification(
|
||||||
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
|
title = "${getString(R.string.tunnel_running)} - ${getString(R.string.multiple)}",
|
||||||
|
actions = listOf(
|
||||||
|
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onCreateNotification(): Notification {
|
||||||
|
return notificationManager.createNotification(
|
||||||
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
|
title = getString(R.string.tunnel_starting),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO add notification handling and optional log reading for restart on handshake failures
|
||||||
|
companion object {
|
||||||
|
const val STATS_DELAY = 1_000L
|
||||||
|
const val PING_START_DELAY = 30_000L
|
||||||
|
// ipv6 disabled or block on network
|
||||||
|
// const val userspaceStartFailed = "Failed to send handshake initiation: write udp [::]"
|
||||||
|
// const val ipv6Fails = "Failed to send data packets: write udp [::]"
|
||||||
|
// const val ipv4Fails = "Failed to send data packets: write udp 0.0.0.0:51820"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-20
@@ -14,8 +14,8 @@ import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotificati
|
|||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.di.MainImmediateDispatcher
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||||
@@ -30,6 +30,7 @@ import kotlinx.coroutines.CompletableDeferred
|
|||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
@@ -63,10 +64,6 @@ class AutoTunnelService : LifecycleService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
|
||||||
@MainImmediateDispatcher
|
|
||||||
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelManager: TunnelManager
|
lateinit var tunnelManager: TunnelManager
|
||||||
|
|
||||||
@@ -76,16 +73,12 @@ class AutoTunnelService : LifecycleService() {
|
|||||||
|
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
|
|
||||||
|
private var killSwitchJob: Job? = null
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
serviceManager.autoTunnelService.complete(this)
|
serviceManager.autoTunnelService.complete(this)
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
launchWatcherNotification()
|
||||||
runCatching {
|
|
||||||
launchWatcherNotification()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
@@ -94,20 +87,20 @@ class AutoTunnelService : LifecycleService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
super.onStartCommand(intent, flags, startId)
|
||||||
Timber.d("onStartCommand executed with startId: $startId")
|
Timber.d("onStartCommand executed with startId: $startId")
|
||||||
serviceManager.autoTunnelService.complete(this)
|
serviceManager.autoTunnelService.complete(this)
|
||||||
return super.onStartCommand(intent, flags, startId)
|
start()
|
||||||
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
launchWatcherNotification()
|
||||||
launchWatcherNotification()
|
initWakeLock()
|
||||||
initWakeLock()
|
|
||||||
}
|
|
||||||
startAutoTunnelJob()
|
startAutoTunnelJob()
|
||||||
startAutoTunnelStateJob()
|
startAutoTunnelStateJob()
|
||||||
startKillSwitchJob()
|
killSwitchJob = startKillSwitchJob()
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
@@ -120,9 +113,20 @@ class AutoTunnelService : LifecycleService() {
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
serviceManager.autoTunnelService = CompletableDeferred()
|
serviceManager.autoTunnelService = CompletableDeferred()
|
||||||
|
restoreVpnKillSwitch()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun restoreVpnKillSwitch() {
|
||||||
|
with(autoTunnelStateFlow.value) {
|
||||||
|
if (settings.isVpnKillSwitchEnabled && tunnelManager.getBackendState() != BackendState.KILL_SWITCH_ACTIVE) {
|
||||||
|
killSwitchJob?.cancel()
|
||||||
|
val allowedIps = if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList()
|
||||||
|
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun launchWatcherNotification(description: String = getString(R.string.monitoring_state_changes)) {
|
private fun launchWatcherNotification(description: String = getString(R.string.monitoring_state_changes)) {
|
||||||
val notification =
|
val notification =
|
||||||
notificationManager.createNotification(
|
notificationManager.createNotification(
|
||||||
@@ -178,8 +182,8 @@ class AutoTunnelService : LifecycleService() {
|
|||||||
combineSettings(),
|
combineSettings(),
|
||||||
appDataRepository.get().settings.flow
|
appDataRepository.get().settings.flow
|
||||||
.distinctUntilChanged { old, new -> old.isKernelEnabled == new.isKernelEnabled } // Only emit when isKernelEnabled changes
|
.distinctUntilChanged { old, new -> old.isKernelEnabled == new.isKernelEnabled } // Only emit when isKernelEnabled changes
|
||||||
.flatMapLatest { settings ->
|
.flatMapLatest {
|
||||||
networkMonitor.getNetworkStatusFlow(true, settings.isKernelEnabled)
|
networkMonitor.networkStatusFlow
|
||||||
.flowOn(ioDispatcher)
|
.flowOn(ioDispatcher)
|
||||||
.map { buildNetworkState(it) }
|
.map { buildNetworkState(it) }
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-21
@@ -4,62 +4,65 @@ import android.content.Intent
|
|||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import androidx.lifecycle.Lifecycle
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.lifecycle.LifecycleRegistry
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class AutoTunnelControlTile : TileService() {
|
class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
serviceManager.autoTunnelTile.complete(this)
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
serviceManager.autoTunnelTile = CompletableDeferred()
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
serviceManager.autoTunnelTile.complete(this)
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
||||||
applicationScope.launch {
|
Timber.d("Start listening called for auto tunnel tile")
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
lifecycleScope.launch {
|
||||||
updateTileState()
|
serviceManager.autoTunnelActive.collect {
|
||||||
|
if (it) return@collect setActive()
|
||||||
|
setInactive()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
lifecycleScope.launch {
|
||||||
|
appDataRepository.tunnels.flow.collect {
|
||||||
fun updateTileState() {
|
if (it.isEmpty()) {
|
||||||
serviceManager.autoTunnelActive.value.let {
|
setUnavailable()
|
||||||
if (it) setActive() else setInactive()
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
applicationScope.launch {
|
lifecycleScope.launch {
|
||||||
if (serviceManager.autoTunnelActive.value) {
|
if (serviceManager.autoTunnelActive.value) {
|
||||||
serviceManager.stopAutoTunnel()
|
serviceManager.stopAutoTunnel()
|
||||||
setInactive()
|
setInactive()
|
||||||
} else {
|
} else {
|
||||||
serviceManager.startAutoTunnel(true)
|
serviceManager.startAutoTunnel()
|
||||||
setActive()
|
setActive()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,4 +100,7 @@ class AutoTunnelControlTile : TileService() {
|
|||||||
qsTile.updateTile()
|
qsTile.updateTile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override val lifecycle: Lifecycle
|
||||||
|
get() = lifecycleRegistry
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-27
@@ -5,74 +5,128 @@ import android.os.Build
|
|||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.lifecycle.LifecycleRegistry
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelControlTile : TileService() {
|
class TunnelControlTile : TileService(), LifecycleOwner {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelManager: TunnelManager
|
lateinit var tunnelManager: TunnelManager
|
||||||
|
|
||||||
|
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
||||||
|
|
||||||
|
private var isCollecting = false
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
serviceManager.tunnelControlTile.complete(this)
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
serviceManager.tunnelControlTile = CompletableDeferred()
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
Timber.d("Start listening called")
|
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
||||||
serviceManager.tunnelControlTile.complete(this)
|
Timber.d("Start listening called for tunnel tile")
|
||||||
applicationScope.launch {
|
if (isCollecting) return
|
||||||
updateTileState()
|
isCollecting = true
|
||||||
|
lifecycleScope.launch {
|
||||||
|
tunnelManager.activeTunnels.collect {
|
||||||
|
updateTileState()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateTileState() = applicationScope.launch {
|
private suspend fun updateTileState() {
|
||||||
val tunnels = appDataRepository.tunnels.getAll()
|
try {
|
||||||
if (tunnels.isEmpty()) return@launch setUnavailable()
|
val tunnels = appDataRepository.tunnels.getAll()
|
||||||
with(tunnelManager.activeTunnels.value) {
|
if (tunnels.isEmpty()) {
|
||||||
if (isNotEmpty()) if (size == 1) {
|
setUnavailable()
|
||||||
tunnels.firstOrNull { it.id == keys.first() }?.let { return@launch updateTile(it.tunName, true) }
|
return
|
||||||
} else {
|
|
||||||
return@launch updateTile(getString(R.string.multiple), true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val activeTunnels = tunnelManager.activeTunnels.value
|
||||||
|
.filter { it.value.status.isUpOrStarting() }
|
||||||
|
|
||||||
|
when {
|
||||||
|
activeTunnels.isNotEmpty() -> {
|
||||||
|
val activeIds = activeTunnels.map { it.key.id }
|
||||||
|
// TODO improvements would be needed to make this work well with toggling multiple tunnels
|
||||||
|
// this would be better managed elsewhere
|
||||||
|
WireGuardAutoTunnel.setLastActiveTunnels(activeIds)
|
||||||
|
updateTileForActiveTunnels(activeTunnels)
|
||||||
|
}
|
||||||
|
else -> updateTileForLastActiveTunnels()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
setUnavailable()
|
||||||
}
|
}
|
||||||
appDataRepository.getStartTunnelConfig()?.let {
|
}
|
||||||
updateTile(it.tunName, false)
|
|
||||||
|
private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) {
|
||||||
|
val tileName = when (activeTunnels.size) {
|
||||||
|
1 -> activeTunnels.keys.first().tunName
|
||||||
|
else -> getString(R.string.multiple)
|
||||||
|
}
|
||||||
|
updateTile(tileName, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun updateTileForLastActiveTunnels() {
|
||||||
|
val lastActiveIds = WireGuardAutoTunnel.getLastActiveTunnels()
|
||||||
|
when {
|
||||||
|
lastActiveIds.isEmpty() -> {
|
||||||
|
appDataRepository.getStartTunnelConfig()?.let { config ->
|
||||||
|
updateTile(config.tunName, false)
|
||||||
|
} ?: setUnavailable()
|
||||||
|
}
|
||||||
|
lastActiveIds.size > 1 -> updateTile(getString(R.string.multiple), false)
|
||||||
|
else -> {
|
||||||
|
val tunnelId = lastActiveIds.first()
|
||||||
|
appDataRepository.tunnels.getById(tunnelId)?.let { tunnel ->
|
||||||
|
updateTile(tunnel.tunName, false)
|
||||||
|
} ?: setUnavailable()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
applicationScope.launch {
|
lifecycleScope.launch {
|
||||||
if (tunnelManager.activeTunnels.value.isNotEmpty()) return@launch tunnelManager.stopTunnel()
|
if (tunnelManager.activeTunnels.value.isNotEmpty()) return@launch tunnelManager.stopTunnel()
|
||||||
appDataRepository.getStartTunnelConfig()?.let {
|
val lastActive = WireGuardAutoTunnel.getLastActiveTunnels()
|
||||||
tunnelManager.startTunnel(it)
|
if (lastActive.isEmpty()) {
|
||||||
|
appDataRepository.getStartTunnelConfig()?.let {
|
||||||
|
tunnelManager.startTunnel(it)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lastActive.forEach { id ->
|
||||||
|
appDataRepository.tunnels.getById(id)?.let {
|
||||||
|
tunnelManager.startTunnel(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,4 +186,6 @@ class TunnelControlTile : TileService() {
|
|||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
override val lifecycle: Lifecycle
|
||||||
|
get() = lifecycleRegistry
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-10
@@ -2,11 +2,12 @@ package com.zaneschepke.wireguardautotunnel.core.shortcut
|
|||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -21,6 +22,9 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var tunnelManager: TunnelManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
@@ -39,17 +43,17 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
.firstOrNull { it.tunName == tunnelName }
|
.firstOrNull { it.tunName == tunnelName }
|
||||||
} ?: appDataRepository.getStartTunnelConfig()
|
} ?: appDataRepository.getStartTunnelConfig()
|
||||||
Timber.d("Shortcut action on name: ${tunnelConfig?.tunName}")
|
Timber.d("Shortcut action on name: ${tunnelConfig?.tunName}")
|
||||||
// tunnelConfig?.let {
|
tunnelConfig?.let {
|
||||||
// when (intent.action) {
|
when (intent.action) {
|
||||||
// Action.START.name -> tunnelService.get().startTunnel(it)
|
Action.START.name -> tunnelManager.startTunnel(it)
|
||||||
// Action.STOP.name -> tunnelService.get().stopTunnel()
|
Action.STOP.name -> tunnelManager.stopTunnel()
|
||||||
// else -> Unit
|
else -> Unit
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
Action.START.name -> serviceManager.startAutoTunnel(true)
|
Action.START.name -> serviceManager.startAutoTunnel()
|
||||||
Action.STOP.name -> serviceManager.stopAutoTunnel()
|
Action.STOP.name -> serviceManager.stopAutoTunnel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,256 +1,204 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||||
|
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
|
||||||
import com.zaneschepke.networkmonitor.NetworkStatus
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider.Companion.CHECK_INTERVAL
|
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendError
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendError
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.coroutineScope
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.flowOn
|
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.isActive
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
open class BaseTunnel(
|
abstract class BaseTunnel(
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||||
private val networkMonitor: NetworkMonitor,
|
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
private val notificationManager: NotificationManager,
|
|
||||||
) : TunnelProvider {
|
) : TunnelProvider {
|
||||||
|
|
||||||
internal val tunnels = MutableStateFlow<List<TunnelConf>>(emptyList())
|
private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap())
|
||||||
|
private val tunThreads = ConcurrentHashMap<Int, Thread>()
|
||||||
|
override val activeTunnels = activeTuns.asStateFlow()
|
||||||
|
|
||||||
private val _tunnelStates = MutableStateFlow<Map<Int, TunnelState>>(emptyMap())
|
private val tunMutex = Mutex()
|
||||||
|
private val tunStatusMutex = Mutex()
|
||||||
|
|
||||||
private val tunnelJobs = ConcurrentHashMap<Int, Job>()
|
private val isBouncing = AtomicBoolean(false)
|
||||||
|
|
||||||
private val isNetworkAvailable = AtomicBoolean(false)
|
abstract suspend fun startBackend(tunnel: TunnelConf)
|
||||||
|
|
||||||
init {
|
abstract fun stopBackend(tunnel: TunnelConf)
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
launch { startNetworkJob() }
|
|
||||||
launch { monitorTunnelConfigChanges() }
|
|
||||||
tunnels.collect { tuns ->
|
|
||||||
val previousTunIds = tunnelJobs.keys.toSet()
|
|
||||||
val currentTunIds = tuns.map { it.id }.toSet()
|
|
||||||
val newTuns = tuns.filter { it.id !in previousTunIds }
|
|
||||||
val removedTunIds = previousTunIds - currentTunIds
|
|
||||||
|
|
||||||
newTuns.forEach { tun ->
|
override suspend fun clearError(tunnelConf: TunnelConf) = updateTunnelStatus(tunnelConf, TunnelStatus.Down)
|
||||||
Timber.d("Starting tunnel jobs for tun ${tun.name} (ID: ${tun.id})")
|
|
||||||
tunnelJobs[tun.id] = startTunnelJobs(tun)
|
|
||||||
}
|
|
||||||
|
|
||||||
removedTunIds.forEach { tunId ->
|
override fun hasVpnPermission(): Boolean {
|
||||||
tunnelJobs[tunId]?.cancelWithMessage("Canceling tunnel jobs for tunnel ID: $tunId")
|
return serviceManager.hasVpnPermission()
|
||||||
tunnelJobs.remove(tunId)
|
}
|
||||||
_tunnelStates.update { it - tunId }
|
|
||||||
serviceManager.updateTunnelTile()
|
protected suspend fun updateTunnelStatus(tunnelConf: TunnelConf, state: TunnelStatus? = null, stats: TunnelStatistics? = null) {
|
||||||
|
tunStatusMutex.withLock {
|
||||||
|
activeTuns.update { current ->
|
||||||
|
val originalConf = current.getKeyById(tunnelConf.id) ?: tunnelConf
|
||||||
|
val existingState = current.getValueById(tunnelConf.id) ?: TunnelState()
|
||||||
|
val newState = state ?: existingState.status
|
||||||
|
if (newState == TunnelStatus.Down) {
|
||||||
|
Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN")
|
||||||
|
current - originalConf
|
||||||
|
} else if (existingState.status == newState && stats == null) {
|
||||||
|
Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newState")
|
||||||
|
current
|
||||||
|
} else {
|
||||||
|
val updated = existingState.copy(
|
||||||
|
status = newState,
|
||||||
|
statistics = stats ?: existingState.statistics,
|
||||||
|
)
|
||||||
|
current + (originalConf to updated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startTunnelJobs(tunnel: TunnelConf) = applicationScope.launch(ioDispatcher) {
|
private suspend fun stopActiveTunnels() {
|
||||||
launch { startTunnelStatisticsJob(tunnel) }
|
activeTunnels.value.forEach { (config, state) ->
|
||||||
if (tunnel.isPingEnabled) launch { startPingJob(tunnel) }
|
if (state.status.isUpOrStarting()) {
|
||||||
}
|
stopTunnel(config)
|
||||||
|
delay(300)
|
||||||
private fun updateTunnelState(tunnelId: Int, newState: TunnelStatus) {
|
}
|
||||||
Timber.d("Updating tunnel state for ID $tunnelId to $newState")
|
|
||||||
_tunnelStates.update { current ->
|
|
||||||
val currentState = current[tunnelId]
|
|
||||||
val updatedState = currentState?.copy(state = newState) ?: TunnelState(state = newState)
|
|
||||||
val newMap = current + (tunnelId to updatedState)
|
|
||||||
Timber.d("New tunnel states: $newMap")
|
|
||||||
newMap
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun beforeStartTunnel(tunnelConf: TunnelConf) {
|
private fun configureTunnelCallbacks(tunnelConf: TunnelConf) {
|
||||||
|
Timber.d("Configuring TunnelConf instance: ${tunnelConf.hashCode()}")
|
||||||
|
|
||||||
tunnelConf.setStateChangeCallback { state ->
|
tunnelConf.setStateChangeCallback { state ->
|
||||||
Timber.d("New tunnel state $state")
|
Timber.d("State change callback triggered for tunnel ${tunnelConf.id}: ${tunnelConf.tunName} with state $state at ${System.currentTimeMillis()}")
|
||||||
when (state) {
|
when (state) {
|
||||||
is Tunnel.State -> updateTunnelState(tunnelConf.id, state.asTunnelState())
|
is Tunnel.State -> applicationScope.launch { updateTunnelStatus(tunnelConf, state.asTunnelState()) }
|
||||||
is org.amnezia.awg.backend.Tunnel.State -> updateTunnelState(tunnelConf.id, state.asTunnelState())
|
is org.amnezia.awg.backend.Tunnel.State -> applicationScope.launch { updateTunnelStatus(tunnelConf, state.asTunnelState()) }
|
||||||
}
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
serviceManager.updateTunnelTile()
|
|
||||||
}
|
}
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startTunnel(tunnelConf: TunnelConf) {
|
override suspend fun updateTunnelStatistics(tunnel: TunnelConf) {
|
||||||
applicationScope.launch(ioDispatcher) {
|
val stats = getStatistics(tunnel)
|
||||||
serviceManager.startBackgroundService(tunnelConf)
|
updateTunnelStatus(tunnel, null, stats)
|
||||||
appDataRepository.tunnels.save(tunnelConf.copy(isActive = true))
|
|
||||||
addToActiveTunnels(tunnelConf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stopTunnel(tunnelConf: TunnelConf?) {
|
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||||
// Default empty implementation; subclasses override
|
if (activeTuns.exists(tunnelConf.id) || tunThreads.containsKey(tunnelConf.id)) return
|
||||||
}
|
// stop active tunnels if we are userspace
|
||||||
|
if (this@BaseTunnel is UserspaceTunnel) stopActiveTunnels()
|
||||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf) {
|
tunMutex.withLock {
|
||||||
stopTunnel(tunnelConf)
|
// use thread to interrupt java backend if stuck (like in dns resolution)
|
||||||
delay(1000)
|
tunThreads += tunnelConf.id to thread {
|
||||||
startTunnel(tunnelConf)
|
runBlocking {
|
||||||
}
|
try {
|
||||||
|
Timber.d("Starting tunnel ${tunnelConf.id}...")
|
||||||
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
startTunnelInner(tunnelConf)
|
||||||
// Default empty implementation
|
Timber.d("Started complete for tunnel ${tunnelConf.name}...")
|
||||||
}
|
} catch (e: BackendError) {
|
||||||
|
Timber.e(e, "Failed to start tunnel ${tunnelConf.name} userspace")
|
||||||
override suspend fun runningTunnelNames(): Set<String> {
|
updateTunnelStatus(tunnelConf, TunnelStatus.Error(e))
|
||||||
return emptySet()
|
} catch (e: InterruptedException) {
|
||||||
}
|
Timber.i("Tunnel start has been interrupted as ${tunnelConf.name} failed to start")
|
||||||
|
|
||||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics {
|
|
||||||
throw NotImplementedError("Get statistics not implemented in base class")
|
|
||||||
}
|
|
||||||
|
|
||||||
override val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
|
||||||
get() = _tunnelStates.asStateFlow()
|
|
||||||
|
|
||||||
internal suspend fun onTunnelStop(tunnelConf: TunnelConf) {
|
|
||||||
appDataRepository.tunnels.save(tunnelConf.copy(isActive = false))
|
|
||||||
removeFromActiveTunnels(tunnelConf)
|
|
||||||
if (tunnels.value.isEmpty()) serviceManager.stopBackgroundService()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun stopAllTunnels() {
|
|
||||||
tunnels.value.forEach {
|
|
||||||
stopTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun addToActiveTunnels(conf: TunnelConf) {
|
|
||||||
tunnels.update {
|
|
||||||
it.toMutableList().apply {
|
|
||||||
add(conf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun removeFromActiveTunnels(conf: TunnelConf) {
|
|
||||||
tunnels.update {
|
|
||||||
it.toMutableList().apply {
|
|
||||||
remove(conf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun startNetworkJob() = coroutineScope {
|
|
||||||
networkMonitor.getNetworkStatusFlow(includeWifiSsid = false, useRootShell = false)
|
|
||||||
.flowOn(ioDispatcher).collect {
|
|
||||||
isNetworkAvailable.set(it !is NetworkStatus.Disconnected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun startPingJob(tunnel: TunnelConf) = coroutineScope {
|
|
||||||
while (isActive) {
|
|
||||||
runCatching {
|
|
||||||
if (isNetworkAvailable.get() && tunnel.isActive) {
|
|
||||||
val pingSuccess = tunnel.isTunnelPingable(ioDispatcher)
|
|
||||||
handlePingResult(tunnel, pingSuccess)
|
|
||||||
}
|
|
||||||
delay(tunnel.pingInterval ?: Constants.PING_INTERVAL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun handlePingResult(tunnel: TunnelConf, pingSuccess: Boolean) {
|
|
||||||
if (!pingSuccess) {
|
|
||||||
if (isNetworkAvailable.get()) {
|
|
||||||
Timber.i("Ping result: target was not reachable, bouncing the tunnel")
|
|
||||||
bounceTunnel(tunnel)
|
|
||||||
delay(tunnel.pingCooldown ?: Constants.PING_COOLDOWN)
|
|
||||||
} else {
|
|
||||||
Timber.i("Ping result: target was not reachable, but no network available")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Timber.i("Ping result: all ping targets were reached successfully")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun handleBackendThrowable(backendError: BackendError) {
|
|
||||||
val message = when (backendError) {
|
|
||||||
BackendError.Config -> StringValue.StringResource(R.string.start_failed_config)
|
|
||||||
BackendError.DNS -> StringValue.StringResource(R.string.dns_error)
|
|
||||||
BackendError.Unauthorized -> StringValue.StringResource(R.string.unauthorized)
|
|
||||||
}
|
|
||||||
if (WireGuardAutoTunnel.isForeground()) {
|
|
||||||
SnackbarController.showMessage(message)
|
|
||||||
} else {
|
|
||||||
notificationManager.show(
|
|
||||||
NotificationManager.VPN_NOTIFICATION_ID,
|
|
||||||
notificationManager.createNotification(
|
|
||||||
WireGuardNotification.NotificationChannels.VPN,
|
|
||||||
title = StringValue.StringResource(R.string.tunne_start_failed_title),
|
|
||||||
description = message,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun monitorTunnelConfigChanges() = coroutineScope {
|
|
||||||
appDataRepository.tunnels.flow.collect { storageTuns ->
|
|
||||||
storageTuns.forEach { storageTun ->
|
|
||||||
val currentTun = tunnels.value.firstOrNull { it.id == storageTun.id }
|
|
||||||
if (currentTun != null) {
|
|
||||||
if (!currentTun.isQuickConfigMatching(storageTun)) {
|
|
||||||
Timber.d("Tunnel config changed for ID $storageTun, bouncing tunnel")
|
|
||||||
bounceTunnel(storageTun)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun startTunnelStatisticsJob(tunnel: TunnelConf) = coroutineScope {
|
private suspend fun startTunnelInner(tunnelConf: TunnelConf) {
|
||||||
while (this.isActive) {
|
configureTunnelCallbacks(tunnelConf)
|
||||||
runCatching {
|
Timber.d("Started backend for tunnel ${tunnelConf.id}...")
|
||||||
val stats = getStatistics(tunnel)
|
startBackend(tunnelConf)
|
||||||
_tunnelStates.update { currentStates ->
|
updateTunnelStatus(tunnelConf, TunnelStatus.Up)
|
||||||
val updatedState = currentStates[tunnel.id]?.copy(statistics = stats)
|
Timber.d("DONE for tun ${tunnelConf.id}...")
|
||||||
?: TunnelState(statistics = stats)
|
saveTunnelActiveState(tunnelConf, true)
|
||||||
currentStates + (tunnel.id to updatedState)
|
serviceManager.startTunnelForegroundService()
|
||||||
}
|
}
|
||||||
delay(CHECK_INTERVAL)
|
|
||||||
}.onFailure { exception ->
|
private suspend fun saveTunnelActiveState(tunnelConf: TunnelConf, active: Boolean) {
|
||||||
Timber.e(exception, "Failed to update tunnel statistics for ${tunnel.tunName}")
|
val tunnelCopy = tunnelConf.copyWithCallback(isActive = active)
|
||||||
|
appDataRepository.tunnels.save(tunnelCopy)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
|
||||||
|
if (tunnelConf == null) return stopActiveTunnels()
|
||||||
|
tunMutex.withLock {
|
||||||
|
try {
|
||||||
|
val stuckStarting = activeTuns.isStarting(tunnelConf.id)
|
||||||
|
handleTunnelThreadCleanup(tunnelConf)
|
||||||
|
if (stuckStarting) return Timber.d("Stuck in starting, so just shutting down tunnel thread")
|
||||||
|
updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason))
|
||||||
|
stopTunnelInner(tunnelConf)
|
||||||
|
} catch (e: BackendError) {
|
||||||
|
Timber.e(e, "Failed to stop tunnel ${tunnelConf.id}")
|
||||||
|
updateTunnelStatus(tunnelConf, TunnelStatus.Error(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun stopTunnelInner(tunnelConf: TunnelConf) {
|
||||||
|
val tunnel = activeTuns.findTunnel(tunnelConf.id) ?: return
|
||||||
|
stopBackend(tunnel)
|
||||||
|
saveTunnelActiveState(tunnelConf, false)
|
||||||
|
removeActiveTunnel(tunnel)
|
||||||
|
handleServiceChangesOnStop()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleServiceChangesOnStop() {
|
||||||
|
if (activeTuns.value.isEmpty() && !isBouncing.get()) return serviceManager.stopTunnelForegroundService()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleTunnelThreadCleanup(tunnel: TunnelConf) {
|
||||||
|
Timber.d("Cleaning up thread for ${tunnel.name}")
|
||||||
|
try {
|
||||||
|
tunThreads[tunnel.id]?.let {
|
||||||
|
if (it.state != Thread.State.TERMINATED) {
|
||||||
|
it.interrupt()
|
||||||
|
updateTunnelStatus(tunnel, TunnelStatus.Down)
|
||||||
|
} else {
|
||||||
|
Timber.d("Thread already terminated")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e, "Failed to stop tunnel thread for ${tunnel.name}")
|
||||||
|
}
|
||||||
|
Timber.d("Removing thread for ${tunnel.name}")
|
||||||
|
tunThreads -= tunnel.id
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeActiveTunnel(tunnelConf: TunnelConf) {
|
||||||
|
activeTuns.update { current ->
|
||||||
|
current.toMutableMap().apply { remove(tunnelConf) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||||
|
Timber.i("Bounce tunnel ${tunnelConf.name}")
|
||||||
|
isBouncing.set(true)
|
||||||
|
stopTunnel(tunnelConf, reason)
|
||||||
|
startTunnel(tunnelConf)
|
||||||
|
isBouncing.set(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun runningTunnelNames(): Set<String> = activeTuns.value.keys.map { it.tunName }.toSet()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
|
||||||
|
fun Map<TunnelConf, TunnelState>.allDown(): Boolean {
|
||||||
|
return this.all { it.value.status.isDown() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Map<TunnelConf, TunnelState>.hasActive(): Boolean {
|
||||||
|
return this.any { it.value.status.isUp() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Map<TunnelConf, TunnelState>.getValueById(id: Int): TunnelState? {
|
||||||
|
val key = this.keys.find { it.id == id }
|
||||||
|
return key?.let { this@getValueById[it] }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Map<TunnelConf, TunnelState>.getKeyById(id: Int): TunnelConf? {
|
||||||
|
return this.keys.find { it.id == id }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Map<TunnelConf, TunnelState>.isUp(tunnelConf: TunnelConf): Boolean {
|
||||||
|
return this.getValueById(tunnelConf.id)?.status?.isUp() ?: false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.exists(id: Int): Boolean {
|
||||||
|
return this.value.any { it.key.id == id }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isUp(id: Int): Boolean {
|
||||||
|
return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Up }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isStarting(id: Int): Boolean {
|
||||||
|
return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Starting }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.findTunnel(id: Int): TunnelConf? {
|
||||||
|
return this.value.keys.find { it.id == id }
|
||||||
|
}
|
||||||
|
|
||||||
|
private val URL_PATTERN = Regex(
|
||||||
|
"""^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}:[0-9]{1,5}$""",
|
||||||
|
)
|
||||||
|
|
||||||
|
fun String.isUrl(): Boolean {
|
||||||
|
return URL_PATTERN.matches(this)
|
||||||
|
}
|
||||||
@@ -3,20 +3,18 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
|||||||
import com.wireguard.android.backend.Backend
|
import com.wireguard.android.backend.Backend
|
||||||
import com.wireguard.android.backend.BackendException
|
import com.wireguard.android.backend.BackendException
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.WireGuardStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.WireGuardStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -25,54 +23,44 @@ class KernelTunnel @Inject constructor(
|
|||||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||||
serviceManager: ServiceManager,
|
serviceManager: ServiceManager,
|
||||||
appDataRepository: AppDataRepository,
|
appDataRepository: AppDataRepository,
|
||||||
notificationManager: NotificationManager,
|
|
||||||
private val backend: Backend,
|
private val backend: Backend,
|
||||||
networkMonitor: NetworkMonitor,
|
) : BaseTunnel(ioDispatcher, applicationScope, appDataRepository, serviceManager) {
|
||||||
) : BaseTunnel(ioDispatcher, applicationScope, networkMonitor, appDataRepository, serviceManager, notificationManager) {
|
|
||||||
|
|
||||||
override fun startTunnel(tunnelConf: TunnelConf) {
|
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||||
Timber.d("Starting tunnel ${tunnelConf.id} kernel")
|
return try {
|
||||||
applicationScope.launch(ioDispatcher) {
|
WireGuardStatistics(backend.getStatistics(tunnelConf))
|
||||||
if (tunnels.value.any { it.id == tunnelConf.id }) return@launch Timber.w("Tunnel already running")
|
} catch (e: Exception) {
|
||||||
runCatching {
|
Timber.e(e)
|
||||||
Timber.d("Setting backend state UP")
|
null
|
||||||
super.beforeStartTunnel(tunnelConf)
|
|
||||||
backend.setState(tunnelConf, Tunnel.State.UP, tunnelConf.toWgConfig())
|
|
||||||
Timber.d("Calling super.startTunnel")
|
|
||||||
super.startTunnel(tunnelConf)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it, "Failed to start tunnel ${tunnelConf.id} kernel")
|
|
||||||
onTunnelStop(tunnelConf)
|
|
||||||
if (it is BackendException) {
|
|
||||||
handleBackendThrowable(it.toBackendError())
|
|
||||||
} else {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics {
|
override suspend fun startBackend(tunnel: TunnelConf) {
|
||||||
return WireGuardStatistics(backend.getStatistics(tunnelConf))
|
try {
|
||||||
}
|
updateTunnelStatus(tunnel, TunnelStatus.Starting)
|
||||||
|
backend.setState(tunnel, Tunnel.State.UP, tunnel.toWgConfig())
|
||||||
override fun stopTunnel(tunnelConf: TunnelConf?) {
|
} catch (e: BackendException) {
|
||||||
applicationScope.launch(ioDispatcher) {
|
throw e.toBackendError()
|
||||||
runCatching {
|
|
||||||
tunnels.value.firstOrNull { it.id == tunnelConf?.id }?.let {
|
|
||||||
backend.setState(it, Tunnel.State.DOWN, it.toWgConfig())
|
|
||||||
onTunnelStop(it)
|
|
||||||
} ?: stopAllTunnels()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
override fun stopBackend(tunnel: TunnelConf) {
|
||||||
|
Timber.i("Stopping tunnel ${tunnel.id} kernel")
|
||||||
|
try {
|
||||||
|
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toWgConfig())
|
||||||
|
} catch (e: BackendException) {
|
||||||
|
throw e.toBackendError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||||
Timber.w("Not yet implemented for kernel")
|
Timber.w("Not yet implemented for kernel")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getBackendState(): BackendState {
|
||||||
|
return BackendState.INACTIVE
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun runningTunnelNames(): Set<String> {
|
override suspend fun runningTunnelNames(): Set<String> {
|
||||||
return backend.runningTunnelNames
|
return backend.runningTunnelNames
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.zaneschepke.wireguardautotunnel.di.Kernel
|
|||||||
import com.zaneschepke.wireguardautotunnel.di.Userspace
|
import com.zaneschepke.wireguardautotunnel.di.Userspace
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
@@ -56,27 +57,43 @@ class TunnelManager @Inject constructor(
|
|||||||
initialValue = emptyMap(),
|
initialValue = emptyMap(),
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun startTunnel(tunnelConf: TunnelConf) {
|
override fun hasVpnPermission(): Boolean {
|
||||||
|
return userspaceTunnel.hasVpnPermission()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun clearError(tunnelConf: TunnelConf) {
|
||||||
|
tunnelProviderFlow.value.clearError(tunnelConf)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateTunnelStatistics(tunnel: TunnelConf) {
|
||||||
|
tunnelProviderFlow.value.updateTunnelStatistics(tunnel)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startTunnel(tunnelConf: TunnelConf) {
|
||||||
tunnelProviderFlow.value.startTunnel(tunnelConf)
|
tunnelProviderFlow.value.startTunnel(tunnelConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stopTunnel(tunnelConf: TunnelConf?) {
|
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
|
||||||
tunnelProviderFlow.value.stopTunnel(tunnelConf)
|
tunnelProviderFlow.value.stopTunnel(tunnelConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun bounceTunnel(tunnelConf: TunnelConf) {
|
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||||
tunnelProviderFlow.value.bounceTunnel(tunnelConf)
|
tunnelProviderFlow.value.bounceTunnel(tunnelConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||||
tunnelProviderFlow.value.setBackendState(backendState, allowedIps)
|
tunnelProviderFlow.value.setBackendState(backendState, allowedIps)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getBackendState(): BackendState {
|
||||||
|
return tunnelProviderFlow.value.getBackendState()
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun runningTunnelNames(): Set<String> {
|
override suspend fun runningTunnelNames(): Set<String> {
|
||||||
return tunnelProviderFlow.value.runningTunnelNames()
|
return tunnelProviderFlow.value.runningTunnelNames()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics {
|
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||||
return tunnelProviderFlow.value.getStatistics(tunnelConf)
|
return tunnelProviderFlow.value.getStatistics(tunnelConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +101,7 @@ class TunnelManager @Inject constructor(
|
|||||||
val settings = appDataRepository.settings.get()
|
val settings = appDataRepository.settings.get()
|
||||||
if (settings.isRestoreOnBootEnabled) {
|
if (settings.isRestoreOnBootEnabled) {
|
||||||
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
|
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
|
||||||
val tunsToStart = previouslyActiveTuns.filterNot { tun -> activeTunnels.value.any { tun.id == it.key } }
|
val tunsToStart = previouslyActiveTuns.filterNot { tun -> activeTunnels.value.any { tun.id == it.key.id } }
|
||||||
if (settings.isKernelEnabled) {
|
if (settings.isKernelEnabled) {
|
||||||
return@launch tunsToStart.forEach {
|
return@launch tunsToStart.forEach {
|
||||||
startTunnel(it)
|
startTunnel(it)
|
||||||
|
|||||||
+11
-10
@@ -2,20 +2,21 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
|
|||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
interface TunnelProvider {
|
interface TunnelProvider {
|
||||||
companion object {
|
suspend fun startTunnel(tunnelConf: TunnelConf)
|
||||||
const val CHECK_INTERVAL = 1000L
|
suspend fun stopTunnel(tunnelConf: TunnelConf? = null, reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER)
|
||||||
}
|
suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER)
|
||||||
|
fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
|
||||||
fun startTunnel(tunnelConf: TunnelConf)
|
fun getBackendState(): BackendState
|
||||||
fun stopTunnel(tunnelConf: TunnelConf? = null)
|
|
||||||
suspend fun bounceTunnel(tunnelConf: TunnelConf)
|
|
||||||
suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
|
|
||||||
suspend fun runningTunnelNames(): Set<String>
|
suspend fun runningTunnelNames(): Set<String>
|
||||||
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics
|
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics?
|
||||||
val activeTunnels: StateFlow<Map<Int, TunnelState>>
|
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>>
|
||||||
|
fun hasVpnPermission(): Boolean
|
||||||
|
suspend fun clearError(tunnelConf: TunnelConf)
|
||||||
|
suspend fun updateTunnelStatistics(tunnel: TunnelConf)
|
||||||
}
|
}
|
||||||
|
|||||||
+69
-46
@@ -1,84 +1,107 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
package com.zaneschepke.wireguardautotunnel.core.tunnel
|
||||||
|
|
||||||
import com.wireguard.android.backend.BackendException
|
|
||||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.AmneziaStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.AmneziaStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.amnezia.awg.backend.Backend
|
import org.amnezia.awg.backend.Backend
|
||||||
|
import org.amnezia.awg.backend.BackendException
|
||||||
import org.amnezia.awg.backend.Tunnel
|
import org.amnezia.awg.backend.Tunnel
|
||||||
|
import org.amnezia.awg.config.Config
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import kotlin.jvm.optionals.getOrNull
|
||||||
|
|
||||||
class UserspaceTunnel @Inject constructor(
|
class UserspaceTunnel @Inject constructor(
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||||
serviceManager: ServiceManager,
|
val serviceManager: ServiceManager,
|
||||||
appDataRepository: AppDataRepository,
|
val appDataRepository: AppDataRepository,
|
||||||
notificationManager: NotificationManager,
|
|
||||||
private val backend: Backend,
|
private val backend: Backend,
|
||||||
networkMonitor: NetworkMonitor,
|
) : BaseTunnel(ioDispatcher, applicationScope, appDataRepository, serviceManager) {
|
||||||
) : BaseTunnel(ioDispatcher, applicationScope, networkMonitor, appDataRepository, serviceManager, notificationManager) {
|
|
||||||
|
|
||||||
override fun startTunnel(tunnelConf: TunnelConf) {
|
private var previousBackendState: Pair<BackendState, Boolean>? = null
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
Timber.d("Starting tunnel ${tunnelConf.id} userspace")
|
override suspend fun startBackend(tunnel: TunnelConf) {
|
||||||
if (tunnels.value.any { it.id == tunnelConf.id }) return@launch Timber.w("Tunnel already running")
|
try {
|
||||||
if (tunnels.value.isNotEmpty()) {
|
updateTunnelStatus(tunnel, TunnelStatus.Starting)
|
||||||
Timber.d("Stopping all tunnels")
|
val amConfig = tunnel.toAmConfig()
|
||||||
stopAllTunnels()
|
handleVpnKillSwitchWithDomainEndpoints(amConfig)
|
||||||
}
|
backend.setState(tunnel, Tunnel.State.UP, amConfig)
|
||||||
runCatching {
|
} catch (e: BackendException) {
|
||||||
Timber.d("Setting backend state UP")
|
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
|
||||||
super.beforeStartTunnel(tunnelConf)
|
throw e.toBackendError()
|
||||||
backend.setState(tunnelConf, Tunnel.State.UP, tunnelConf.toAmConfig())
|
|
||||||
Timber.d("Calling super.startTunnel")
|
|
||||||
super.startTunnel(tunnelConf)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it, "Failed to start tunnel ${tunnelConf.id} userspace")
|
|
||||||
onTunnelStop(tunnelConf)
|
|
||||||
if (it is BackendException) {
|
|
||||||
handleBackendThrowable(it.toBackendError())
|
|
||||||
} else {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stopTunnel(tunnelConf: TunnelConf?) {
|
override fun stopBackend(tunnel: TunnelConf) {
|
||||||
applicationScope.launch(ioDispatcher) {
|
Timber.i("Stopping tunnel ${tunnel.name} userspace")
|
||||||
runCatching {
|
try {
|
||||||
tunnels.value.firstOrNull { it.id == tunnelConf?.id }?.let {
|
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toAmConfig())
|
||||||
backend.setState(it, Tunnel.State.DOWN, it.toAmConfig())
|
} catch (e: BackendException) {
|
||||||
onTunnelStop(it)
|
Timber.e(e, "Failed to stop tunnel ${tunnel.id}")
|
||||||
} ?: stopAllTunnels()
|
throw e.toBackendError()
|
||||||
}.onFailure {
|
}
|
||||||
Timber.e(it)
|
handlePreviouslyEnabledVpnKillSwitch()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stop vpn kill switch if we need to resolve DNS for peer endpoints
|
||||||
|
private suspend fun handleVpnKillSwitchWithDomainEndpoints(config: Config) {
|
||||||
|
if (config.peers.any { it.endpoint.getOrNull()?.toString()?.isUrl() == true } &&
|
||||||
|
backend.backendState.asBackendState() == BackendState.KILL_SWITCH_ACTIVE
|
||||||
|
) {
|
||||||
|
val bypassLan = appDataRepository.settings.get().isLanOnKillSwitchEnabled
|
||||||
|
previousBackendState = Pair(BackendState.KILL_SWITCH_ACTIVE, bypassLan)
|
||||||
|
setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
// restore vpn kill switch if needed
|
||||||
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
private fun handlePreviouslyEnabledVpnKillSwitch() {
|
||||||
|
// let auto tunnel handle this if it is active
|
||||||
|
if (!serviceManager.autoTunnelActive.value) {
|
||||||
|
previousBackendState?.let { (state, lanEnabled) ->
|
||||||
|
Timber.d("Restoring kill switch configuration")
|
||||||
|
val lan = if (lanEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList()
|
||||||
|
backend.setBackendState(state.asAmBackendState(), lan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
previousBackendState = null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||||
|
Timber.d("Setting backend state: $backendState with allowedIps: $allowedIps")
|
||||||
|
try {
|
||||||
|
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
||||||
|
} catch (e: BackendException) {
|
||||||
|
throw e.toBackendError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getBackendState(): BackendState {
|
||||||
|
return backend.backendState.asBackendState()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun runningTunnelNames(): Set<String> {
|
override suspend fun runningTunnelNames(): Set<String> {
|
||||||
return backend.runningTunnelNames
|
return backend.runningTunnelNames
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics {
|
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
|
||||||
return AmneziaStatistics(backend.getStatistics(tunnelConf))
|
return try {
|
||||||
|
AmneziaStatistics(backend.getStatistics(tunnelConf))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}")
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class ServiceWorker @AssistedInject constructor(
|
|||||||
override suspend fun doWork(): Result = withContext(ioDispatcher) {
|
override suspend fun doWork(): Result = withContext(ioDispatcher) {
|
||||||
Timber.i("Service worker started")
|
Timber.i("Service worker started")
|
||||||
with(appDataRepository.settings.get()) {
|
with(appDataRepository.settings.get()) {
|
||||||
if (isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) return@with serviceManager.startAutoTunnel(true)
|
if (isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) return@with serviceManager.startAutoTunnel()
|
||||||
if (tunnelManager.activeTunnels.value.isEmpty()) tunnelManager.restorePreviousState()
|
if (tunnelManager.activeTunnels.value.isEmpty()) tunnelManager.restorePreviousState()
|
||||||
}
|
}
|
||||||
Result.success()
|
Result.success()
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data
|
package com.zaneschepke.wireguardautotunnel.data
|
||||||
|
|
||||||
import androidx.room.TypeConverter
|
import androidx.room.TypeConverter
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class DatabaseListConverters {
|
class DatabaseListConverters {
|
||||||
|
|||||||
+4
-4
@@ -1,10 +1,10 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class AppDataRoomRepository
|
class AppDataRoomRepository
|
||||||
|
|||||||
+1
-1
@@ -1,8 +1,8 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.DataStoreManager
|
import com.zaneschepke.wireguardautotunnel.data.DataStoreManager
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.model.GeneralState
|
import com.zaneschepke.wireguardautotunnel.data.model.GeneralState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
|
|||||||
+1
-1
@@ -1,10 +1,10 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao
|
import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
|
|||||||
+7
-1
@@ -1,10 +1,10 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
|
import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
@@ -30,6 +30,12 @@ class RoomTunnelRepository(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun saveAll(tunnelConfs: List<TunnelConf>) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
tunnelConfigDao.saveAll(tunnelConfs.map(TunnelConfig::from))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) {
|
override suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) {
|
||||||
withContext(ioDispatcher) {
|
withContext(ioDispatcher) {
|
||||||
tunnelConfigDao.resetPrimaryTunnel()
|
tunnelConfigDao.resetPrimaryTunnel()
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ import android.content.Context
|
|||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
|
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.DataStoreManager
|
||||||
import com.zaneschepke.wireguardautotunnel.data.DatabaseCallback
|
import com.zaneschepke.wireguardautotunnel.data.DatabaseCallback
|
||||||
import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao
|
import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao
|
||||||
import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao
|
||||||
import com.zaneschepke.wireguardautotunnel.data.DataStoreManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRoomRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRoomRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.DataStoreAppStateRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.DataStoreAppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.RoomSettingsRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.RoomSettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.RoomTunnelRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.RoomTunnelRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import com.wireguard.android.util.RootShell
|
|||||||
import com.wireguard.android.util.ToolsInstaller
|
import com.wireguard.android.util.ToolsInstaller
|
||||||
import com.zaneschepke.networkmonitor.AndroidNetworkMonitor
|
import com.zaneschepke.networkmonitor.AndroidNetworkMonitor
|
||||||
import com.zaneschepke.networkmonitor.NetworkMonitor
|
import com.zaneschepke.networkmonitor.NetworkMonitor
|
||||||
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.KernelTunnel
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.KernelTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
|
||||||
import com.zaneschepke.wireguardautotunnel.core.tunnel.UserspaceTunnel
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.UserspaceTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -20,6 +20,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
|
|||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.amnezia.awg.backend.Backend
|
import org.amnezia.awg.backend.Backend
|
||||||
import org.amnezia.awg.backend.GoBackend
|
import org.amnezia.awg.backend.GoBackend
|
||||||
import org.amnezia.awg.backend.RootTunnelActionHandler
|
import org.amnezia.awg.backend.RootTunnelActionHandler
|
||||||
@@ -65,11 +66,9 @@ class TunnelModule {
|
|||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
serviceManager: ServiceManager,
|
serviceManager: ServiceManager,
|
||||||
appDataRepository: AppDataRepository,
|
appDataRepository: AppDataRepository,
|
||||||
networkMonitor: NetworkMonitor,
|
|
||||||
notificationManager: NotificationManager,
|
|
||||||
backend: com.wireguard.android.backend.Backend,
|
backend: com.wireguard.android.backend.Backend,
|
||||||
): TunnelProvider {
|
): TunnelProvider {
|
||||||
return KernelTunnel(ioDispatcher, applicationScope, serviceManager, appDataRepository, notificationManager, backend, networkMonitor)
|
return KernelTunnel(ioDispatcher, applicationScope, serviceManager, appDataRepository, backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@@ -80,11 +79,9 @@ class TunnelModule {
|
|||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
serviceManager: ServiceManager,
|
serviceManager: ServiceManager,
|
||||||
appDataRepository: AppDataRepository,
|
appDataRepository: AppDataRepository,
|
||||||
notificationManager: NotificationManager,
|
|
||||||
networkMonitor: NetworkMonitor,
|
|
||||||
backend: Backend,
|
backend: Backend,
|
||||||
): TunnelProvider {
|
): TunnelProvider {
|
||||||
return UserspaceTunnel(ioDispatcher, applicationScope, serviceManager, appDataRepository, notificationManager, backend, networkMonitor)
|
return UserspaceTunnel(ioDispatcher, applicationScope, serviceManager, appDataRepository, backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@@ -101,8 +98,8 @@ class TunnelModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideNetworkMonitor(@ApplicationContext context: Context): NetworkMonitor {
|
fun provideNetworkMonitor(@ApplicationContext context: Context, settingsRepository: AppSettingRepository): NetworkMonitor {
|
||||||
return AndroidNetworkMonitor(context)
|
return AndroidNetworkMonitor(context) { runBlocking { settingsRepository.get().isWifiNameByShellEnabled } }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@@ -110,9 +107,10 @@ class TunnelModule {
|
|||||||
fun provideServiceManager(
|
fun provideServiceManager(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
|
@MainDispatcher mainCoroutineDispatcher: CoroutineDispatcher,
|
||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
appDataRepository: AppDataRepository,
|
appDataRepository: AppDataRepository,
|
||||||
): ServiceManager {
|
): ServiceManager {
|
||||||
return ServiceManager(context, ioDispatcher, applicationScope, appDataRepository)
|
return ServiceManager(context, ioDispatcher, applicationScope, mainCoroutineDispatcher, appDataRepository)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.domain.entity
|
package com.zaneschepke.wireguardautotunnel.domain.entity
|
||||||
|
|
||||||
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.defaultName
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.serialization.Transient
|
|
||||||
import org.amnezia.awg.backend.Tunnel
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
data class TunnelConf(
|
data class TunnelConf(
|
||||||
@@ -26,15 +29,68 @@ data class TunnelConf(
|
|||||||
val pingCooldown: Long? = null,
|
val pingCooldown: Long? = null,
|
||||||
val pingIp: String? = null,
|
val pingIp: String? = null,
|
||||||
val isEthernetTunnel: Boolean = false,
|
val isEthernetTunnel: Boolean = false,
|
||||||
val isIpv4Preferred: Boolean = false,
|
val isIpv4Preferred: Boolean = true,
|
||||||
@Transient
|
@Transient
|
||||||
private var stateChangeCallback: ((Any) -> Unit)? = null,
|
private var stateChangeCallback: ((Any) -> Unit)? = null,
|
||||||
) : Tunnel, com.wireguard.android.backend.Tunnel {
|
) : Tunnel, org.amnezia.awg.backend.Tunnel {
|
||||||
|
|
||||||
fun setStateChangeCallback(callback: (Any) -> Unit) {
|
fun setStateChangeCallback(callback: (Any) -> Unit) {
|
||||||
stateChangeCallback = callback
|
stateChangeCallback = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (other !is TunnelConf) return false
|
||||||
|
return id == other.id && tunName == other.tunName && wgQuick == other.wgQuick && amQuick == other.amQuick &&
|
||||||
|
isPrimaryTunnel == other.isPrimaryTunnel && isMobileDataTunnel == other.isMobileDataTunnel &&
|
||||||
|
isEthernetTunnel == other.isEthernetTunnel && isPingEnabled == other.isPingEnabled && pingIp == other.pingIp &&
|
||||||
|
pingCooldown == other.pingCooldown && pingInterval == other.pingInterval && tunnelNetworks == other.tunnelNetworks &&
|
||||||
|
isIpv4Preferred == other.isIpv4Preferred
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = id
|
||||||
|
result = 31 * result + tunName.hashCode()
|
||||||
|
result = 31 * result + wgQuick.hashCode()
|
||||||
|
result = 31 * result + amQuick.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun copyWithCallback(
|
||||||
|
id: Int = this.id,
|
||||||
|
tunName: String = this.tunName,
|
||||||
|
wgQuick: String = this.wgQuick,
|
||||||
|
tunnelNetworks: List<String> = this.tunnelNetworks,
|
||||||
|
isMobileDataTunnel: Boolean = this.isMobileDataTunnel,
|
||||||
|
isPrimaryTunnel: Boolean = this.isPrimaryTunnel,
|
||||||
|
amQuick: String = this.amQuick,
|
||||||
|
isActive: Boolean = this.isActive,
|
||||||
|
isPingEnabled: Boolean = this.isPingEnabled,
|
||||||
|
pingInterval: Long? = this.pingInterval,
|
||||||
|
pingCooldown: Long? = this.pingCooldown,
|
||||||
|
pingIp: String? = this.pingIp,
|
||||||
|
isEthernetTunnel: Boolean = this.isEthernetTunnel,
|
||||||
|
isIpv4Preferred: Boolean = this.isIpv4Preferred,
|
||||||
|
): TunnelConf {
|
||||||
|
return TunnelConf(
|
||||||
|
id, tunName, wgQuick, tunnelNetworks, isMobileDataTunnel, isPrimaryTunnel,
|
||||||
|
amQuick, isActive, isPingEnabled, pingInterval, pingCooldown, pingIp,
|
||||||
|
isEthernetTunnel, isIpv4Preferred,
|
||||||
|
).apply {
|
||||||
|
stateChangeCallback = this@TunnelConf.stateChangeCallback
|
||||||
|
// tunnelStatsCallback = this@TunnelConf.tunnelStatsCallback
|
||||||
|
// bounceTunnelCallback = this@TunnelConf.bounceTunnelCallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fun onUpdateStatistics() {
|
||||||
|
// tunnelStatsCallback?.invoke()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
|
||||||
|
// bounceTunnelCallback?.invoke(tunnelConf, reason)
|
||||||
|
// }
|
||||||
|
|
||||||
fun toAmConfig(): org.amnezia.awg.config.Config {
|
fun toAmConfig(): org.amnezia.awg.config.Config {
|
||||||
return configFromAmQuick(amQuick.ifBlank { wgQuick })
|
return configFromAmQuick(amQuick.ifBlank { wgQuick })
|
||||||
}
|
}
|
||||||
@@ -43,15 +99,11 @@ data class TunnelConf(
|
|||||||
return configFromWgQuick(wgQuick)
|
return configFromWgQuick(wgQuick)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getName(): String {
|
override fun getName(): String = tunName
|
||||||
return tunName
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isIpv4ResolutionPreferred(): Boolean {
|
override fun isIpv4ResolutionPreferred(): Boolean = isIpv4Preferred
|
||||||
return isIpv4Preferred
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStateChange(newState: com.wireguard.android.backend.Tunnel.State) {
|
override fun onStateChange(newState: org.amnezia.awg.backend.Tunnel.State) {
|
||||||
stateChangeCallback?.invoke(newState)
|
stateChangeCallback?.invoke(newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,16 +111,23 @@ data class TunnelConf(
|
|||||||
stateChangeCallback?.invoke(newState)
|
stateChangeCallback?.invoke(newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isQuickConfigMatching(updatedConf: TunnelConf): Boolean {
|
fun isTunnelConfigChanged(updatedConf: TunnelConf): Boolean {
|
||||||
return updatedConf.wgQuick == wgQuick ||
|
return updatedConf.wgQuick != wgQuick || updatedConf.amQuick != amQuick || updatedConf.name != name
|
||||||
updatedConf.amQuick == amQuick
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isPingConfigMatching(updatedConf: TunnelConf): Boolean {
|
fun generateUniqueName(tunnelNames: List<String>): String {
|
||||||
return updatedConf.isPingEnabled == isPingEnabled &&
|
var tunnelName = this.tunName
|
||||||
pingIp == updatedConf.pingIp &&
|
var num = 1
|
||||||
updatedConf.pingCooldown == pingCooldown &&
|
while (tunnelNames.any { it == tunnelName }) {
|
||||||
updatedConf.pingInterval == pingInterval
|
tunnelName = if (!tunnelName.hasNumberInParentheses()) {
|
||||||
|
"$name($num)"
|
||||||
|
} else {
|
||||||
|
val pair = tunnelName.extractNameAndNumber()
|
||||||
|
"${pair?.first}($num)"
|
||||||
|
}
|
||||||
|
num++
|
||||||
|
}
|
||||||
|
return tunnelName
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun isTunnelPingable(context: CoroutineContext): Boolean {
|
suspend fun isTunnelPingable(context: CoroutineContext): Boolean {
|
||||||
@@ -76,34 +135,37 @@ data class TunnelConf(
|
|||||||
val config = toWgConfig()
|
val config = toWgConfig()
|
||||||
if (pingIp != null) {
|
if (pingIp != null) {
|
||||||
return@withContext InetAddress.getByName(pingIp)
|
return@withContext InetAddress.getByName(pingIp)
|
||||||
.isReachable(Constants.PING_TIMEOUT.toInt())
|
.isReachable(Constants.PING_TIMEOUT.toInt()).also {
|
||||||
|
Timber.i("Ping reachable $pingIp: $it")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Timber.i("Pinging all peers")
|
|
||||||
config.peers.map { peer ->
|
config.peers.map { peer ->
|
||||||
peer.isReachable(isIpv4Preferred)
|
peer.isReachable()
|
||||||
}.all { true }
|
}.all { true }.also {
|
||||||
|
Timber.i("Ping of all peers reachable: $it")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun configFromWgQuick(wgQuick: String): Config {
|
fun configFromWgQuick(wgQuick: String): Config {
|
||||||
val inputStream: InputStream = wgQuick.byteInputStream()
|
val inputStream: InputStream = wgQuick.byteInputStream()
|
||||||
return inputStream.bufferedReader(Charsets.UTF_8).use {
|
return inputStream.bufferedReader(StandardCharsets.UTF_8).use {
|
||||||
Config.parse(it)
|
Config.parse(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun configFromAmQuick(amQuick: String): org.amnezia.awg.config.Config {
|
fun configFromAmQuick(amQuick: String): org.amnezia.awg.config.Config {
|
||||||
val inputStream: InputStream = amQuick.byteInputStream()
|
val inputStream: InputStream = amQuick.byteInputStream()
|
||||||
return inputStream.bufferedReader(Charsets.UTF_8).use {
|
return inputStream.bufferedReader(StandardCharsets.UTF_8).use {
|
||||||
org.amnezia.awg.config.Config.parse(it)
|
org.amnezia.awg.config.Config.parse(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tunnelConfigFromAmConfig(config: org.amnezia.awg.config.Config, name: String): TunnelConf {
|
fun tunnelConfigFromAmConfig(config: org.amnezia.awg.config.Config, name: String? = null): TunnelConf {
|
||||||
val amQuick = config.toAwgQuickString(true)
|
val amQuick = config.toAwgQuickString(true)
|
||||||
val wgQuick = config.toWgQuickString()
|
val wgQuick = config.toWgQuickString()
|
||||||
return TunnelConf(tunName = name, wgQuick = wgQuick, amQuick = amQuick)
|
return TunnelConf(tunName = name ?: config.defaultName(), wgQuick = wgQuick, amQuick = amQuick)
|
||||||
}
|
}
|
||||||
|
|
||||||
private const val IPV6_ALL_NETWORKS = "::/0"
|
private const val IPV6_ALL_NETWORKS = "::/0"
|
||||||
|
|||||||
@@ -1,7 +1,24 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.domain.enums
|
package com.zaneschepke.wireguardautotunnel.domain.enums
|
||||||
|
|
||||||
sealed class BackendError() {
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
|
sealed class BackendError : Exception() {
|
||||||
data object DNS : BackendError()
|
data object DNS : BackendError()
|
||||||
data object Unauthorized : BackendError()
|
data object Unauthorized : BackendError()
|
||||||
data object Config : BackendError()
|
data object Config : BackendError()
|
||||||
|
data object KernelModuleName : BackendError()
|
||||||
|
data object InvalidConfig : BackendError()
|
||||||
|
data object NotAuthorized : BackendError()
|
||||||
|
data object ServiceNotRunning : BackendError()
|
||||||
|
data object Unknown : BackendError()
|
||||||
|
|
||||||
|
fun toStringRes() = when (this) {
|
||||||
|
Config -> R.string.config_error
|
||||||
|
DNS -> R.string.dns_resolve_error
|
||||||
|
InvalidConfig -> R.string.invalid_config_error
|
||||||
|
KernelModuleName -> R.string.kernel_name_error
|
||||||
|
NotAuthorized, Unauthorized -> R.string.auth_error
|
||||||
|
ServiceNotRunning -> R.string.service_running_error
|
||||||
|
Unknown -> R.string.unknown_error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,31 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.domain.enums
|
package com.zaneschepke.wireguardautotunnel.domain.enums
|
||||||
|
|
||||||
enum class TunnelStatus {
|
sealed class TunnelStatus {
|
||||||
UP,
|
data class Error(val error: BackendError) : TunnelStatus()
|
||||||
DOWN,
|
data object Up : TunnelStatus()
|
||||||
;
|
data object Down : TunnelStatus()
|
||||||
|
data class Stopping(val reason: StopReason) : TunnelStatus()
|
||||||
|
data object Starting : TunnelStatus()
|
||||||
|
|
||||||
|
enum class StopReason {
|
||||||
|
USER,
|
||||||
|
PING,
|
||||||
|
CONFIG_CHANGED,
|
||||||
|
}
|
||||||
|
|
||||||
fun isDown(): Boolean {
|
fun isDown(): Boolean {
|
||||||
return this == DOWN
|
return this == Down
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isUp(): Boolean {
|
fun isUp(): Boolean {
|
||||||
return this == UP
|
return this == Up
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isUpOrStarting(): Boolean {
|
||||||
|
return this == Up || this == Starting
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isDownOrStopping(): Boolean {
|
||||||
|
return this == Down || this is Stopping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -11,6 +11,8 @@ interface TunnelRepository {
|
|||||||
|
|
||||||
suspend fun save(tunnelConf: TunnelConf)
|
suspend fun save(tunnelConf: TunnelConf)
|
||||||
|
|
||||||
|
suspend fun saveAll(tunnelConfList: List<TunnelConf>)
|
||||||
|
|
||||||
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
|
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
|
||||||
|
|
||||||
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
|
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
|
||||||
|
|||||||
+16
-13
@@ -1,13 +1,16 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.domain.state
|
package com.zaneschepke.wireguardautotunnel.domain.state
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.events.KillSwitchEvent
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.allDown
|
||||||
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.hasActive
|
||||||
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.isUp
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.events.KillSwitchEvent
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||||
|
|
||||||
data class AutoTunnelState(
|
data class AutoTunnelState(
|
||||||
val activeTunnels: Map<Int, TunnelState> = emptyMap(),
|
val activeTunnels: Map<TunnelConf, TunnelState> = emptyMap(),
|
||||||
val networkState: NetworkState = NetworkState(),
|
val networkState: NetworkState = NetworkState(),
|
||||||
val settings: AppSettings = AppSettings(),
|
val settings: AppSettings = AppSettings(),
|
||||||
val tunnels: List<TunnelConf> = emptyList(),
|
val tunnels: List<TunnelConf> = emptyList(),
|
||||||
@@ -20,12 +23,12 @@ data class AutoTunnelState(
|
|||||||
private fun isMobileTunnelDataChangeNeeded(): Boolean {
|
private fun isMobileTunnelDataChangeNeeded(): Boolean {
|
||||||
val preferredTunnel = preferredMobileDataTunnel()
|
val preferredTunnel = preferredMobileDataTunnel()
|
||||||
return preferredTunnel != null &&
|
return preferredTunnel != null &&
|
||||||
activeTunnels.isNotEmpty() && !activeTunnels.any { it.key == preferredTunnel.id }
|
activeTunnels.isNotEmpty() && !activeTunnels.isUp(preferredTunnel)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isEthernetTunnelChangeNeeded(): Boolean {
|
private fun isEthernetTunnelChangeNeeded(): Boolean {
|
||||||
val preferredTunnel = preferredEthernetTunnel()
|
val preferredTunnel = preferredEthernetTunnel()
|
||||||
return preferredTunnel != null && activeTunnels.isNotEmpty() && !activeTunnels.any { it.key == preferredTunnel.id }
|
return preferredTunnel != null && activeTunnels.isNotEmpty() && !activeTunnels.isUp(preferredTunnel)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun preferredMobileDataTunnel(): TunnelConf? {
|
private fun preferredMobileDataTunnel(): TunnelConf? {
|
||||||
@@ -45,11 +48,11 @@ data class AutoTunnelState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startOnEthernet(): Boolean {
|
private fun startOnEthernet(): Boolean {
|
||||||
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && activeTunnels.isEmpty()
|
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && activeTunnels.allDown()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopOnEthernet(): Boolean {
|
private fun stopOnEthernet(): Boolean {
|
||||||
return networkState.isEthernetConnected && !settings.isTunnelOnEthernetEnabled && activeTunnels.isNotEmpty()
|
return networkState.isEthernetConnected && !settings.isTunnelOnEthernetEnabled && activeTunnels.hasActive()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO test removed kill switch state check
|
// TODO test removed kill switch state check
|
||||||
@@ -67,11 +70,11 @@ data class AutoTunnelState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopOnMobileData(): Boolean {
|
private fun stopOnMobileData(): Boolean {
|
||||||
return isMobileDataActive() && !settings.isTunnelOnMobileDataEnabled && activeTunnels.isNotEmpty()
|
return isMobileDataActive() && !settings.isTunnelOnMobileDataEnabled && activeTunnels.hasActive()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startOnMobileData(): Boolean {
|
private fun startOnMobileData(): Boolean {
|
||||||
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && activeTunnels.isEmpty()
|
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && activeTunnels.allDown()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun changeOnMobileData(): Boolean {
|
private fun changeOnMobileData(): Boolean {
|
||||||
@@ -83,24 +86,24 @@ data class AutoTunnelState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopOnWifi(): Boolean {
|
private fun stopOnWifi(): Boolean {
|
||||||
return isWifiActive() && !settings.isTunnelOnWifiEnabled && activeTunnels.isNotEmpty()
|
return isWifiActive() && !settings.isTunnelOnWifiEnabled && activeTunnels.hasActive()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopOnTrustedWifi(): Boolean {
|
private fun stopOnTrustedWifi(): Boolean {
|
||||||
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.isNotEmpty() && isCurrentSSIDTrusted()
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.hasActive() && isCurrentSSIDTrusted()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startOnUntrustedWifi(): Boolean {
|
private fun startOnUntrustedWifi(): Boolean {
|
||||||
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.isEmpty() && !isCurrentSSIDTrusted()
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.allDown() && !isCurrentSSIDTrusted()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun changeOnUntrustedWifi(): Boolean {
|
private fun changeOnUntrustedWifi(): Boolean {
|
||||||
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.isNotEmpty() && !isCurrentSSIDTrusted() && !isWifiTunnelPreferred()
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.hasActive() && !isCurrentSSIDTrusted() && !isWifiTunnelPreferred()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isWifiTunnelPreferred(): Boolean {
|
private fun isWifiTunnelPreferred(): Boolean {
|
||||||
val preferred = preferredWifiTunnel()
|
val preferred = preferredWifiTunnel()
|
||||||
return activeTunnels.any { it.key == preferred?.id }
|
return preferred?.let { activeTunnels.isUp(it) } ?: true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun asAutoTunnelEvent(): AutoTunnelEvent {
|
fun asAutoTunnelEvent(): AutoTunnelEvent {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
|
|||||||
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
|
||||||
|
|
||||||
data class TunnelState(
|
data class TunnelState(
|
||||||
val state: TunnelStatus = TunnelStatus.DOWN,
|
val status: TunnelStatus = TunnelStatus.Down,
|
||||||
val backendState: BackendState = BackendState.INACTIVE,
|
val backendState: BackendState = BackendState.INACTIVE,
|
||||||
val statistics: TunnelStatistics? = null,
|
val statistics: TunnelStatistics? = null,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -52,7 +52,11 @@ sealed class Route {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data class SplitTunnel(
|
data class SplitTunnel(
|
||||||
val id: Int,
|
val id: Int,
|
||||||
) : Route()
|
) : Route() {
|
||||||
|
companion object {
|
||||||
|
const val KEY_ID = "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class TunnelAutoTunnel(
|
data class TunnelAutoTunnel(
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.ArrowDropDown
|
||||||
|
import androidx.compose.material3.DropdownMenu
|
||||||
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun <T> DropdownSelector(
|
||||||
|
currentValue: T,
|
||||||
|
options: List<T>,
|
||||||
|
onValueSelected: (T) -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
label: @Composable (() -> Unit)? = null,
|
||||||
|
isExpanded: Boolean = false,
|
||||||
|
onDismiss: () -> Unit = {},
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
if (label != null) label()
|
||||||
|
Text(
|
||||||
|
text = currentValue.toString(),
|
||||||
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
|
)
|
||||||
|
Icon(Icons.Default.ArrowDropDown, contentDescription = stringResource(R.string.dropdown))
|
||||||
|
}
|
||||||
|
DropdownMenu(
|
||||||
|
modifier = modifier.height(250.dp),
|
||||||
|
scrollState = rememberScrollState(),
|
||||||
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
|
expanded = isExpanded,
|
||||||
|
onDismissRequest = onDismiss,
|
||||||
|
) {
|
||||||
|
options.forEach { option ->
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text(text = option.toString()) },
|
||||||
|
onClick = {
|
||||||
|
onValueSelected(option)
|
||||||
|
onDismiss() // Close dropdown after selection
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
|
||||||
|
|
||||||
import androidx.compose.ui.geometry.Offset
|
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
|
||||||
|
|
||||||
class NestedScrollListener(val onUp: () -> Unit, val onDown: () -> Unit) : NestedScrollConnection {
|
|
||||||
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
|
||||||
if (available.y < -1) onDown()
|
|
||||||
if (available.y > 1) onUp()
|
|
||||||
return Offset.Zero
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
|
||||||
|
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Clear
|
|
||||||
import androidx.compose.material.icons.rounded.Search
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextField
|
|
||||||
import androidx.compose.material3.TextFieldDefaults
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.graphics.RectangleShape
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SearchBar(onQuery: (queryString: String) -> Unit) {
|
|
||||||
// Immediately update and keep track of query from text field changes.
|
|
||||||
var query: String by rememberSaveable { mutableStateOf("") }
|
|
||||||
var showClearIcon by rememberSaveable { mutableStateOf(false) }
|
|
||||||
|
|
||||||
if (query.isEmpty()) {
|
|
||||||
showClearIcon = false
|
|
||||||
} else if (query.isNotEmpty()) {
|
|
||||||
showClearIcon = true
|
|
||||||
}
|
|
||||||
|
|
||||||
TextField(
|
|
||||||
value = query,
|
|
||||||
onValueChange = { onQueryChanged ->
|
|
||||||
// If user makes changes to text, immediately updated it.
|
|
||||||
query = onQueryChanged
|
|
||||||
onQuery(onQueryChanged)
|
|
||||||
},
|
|
||||||
leadingIcon = {
|
|
||||||
val icon = Icons.Rounded.Search
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
tint = MaterialTheme.colorScheme.onBackground,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailingIcon = {
|
|
||||||
if (showClearIcon) {
|
|
||||||
IconButton(onClick = { query = "" }) {
|
|
||||||
val icon = Icons.Rounded.Clear
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
tint = MaterialTheme.colorScheme.onBackground,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
maxLines = 1,
|
|
||||||
colors =
|
|
||||||
TextFieldDefaults.colors(
|
|
||||||
focusedContainerColor = Color.Transparent,
|
|
||||||
unfocusedContainerColor = Color.Transparent,
|
|
||||||
disabledContainerColor = Color.Transparent,
|
|
||||||
),
|
|
||||||
placeholder = { Text(text = stringResource(R.string.hint_search_packages)) },
|
|
||||||
textStyle = MaterialTheme.typography.bodySmall,
|
|
||||||
singleLine = true,
|
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.background(color = MaterialTheme.colorScheme.background, shape = RectangleShape),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.animation
|
||||||
|
|
||||||
|
import androidx.compose.animation.core.LinearEasing
|
||||||
|
import androidx.compose.animation.core.animateFloat
|
||||||
|
import androidx.compose.animation.core.infiniteRepeatable
|
||||||
|
import androidx.compose.animation.core.rememberInfiniteTransition
|
||||||
|
import androidx.compose.animation.core.tween
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.geometry.Offset
|
||||||
|
import androidx.compose.ui.graphics.Brush
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ShimmerEffect(modifier: Modifier = Modifier): Brush {
|
||||||
|
val shimmerColors = listOf(
|
||||||
|
Color.LightGray.copy(alpha = 0.9f),
|
||||||
|
Color.LightGray.copy(alpha = 0.3f),
|
||||||
|
Color.LightGray.copy(alpha = 0.9f),
|
||||||
|
)
|
||||||
|
|
||||||
|
val transition = rememberInfiniteTransition()
|
||||||
|
val translateAnim by transition.animateFloat(
|
||||||
|
initialValue = 0f,
|
||||||
|
targetValue = 1000f,
|
||||||
|
animationSpec = infiniteRepeatable(
|
||||||
|
animation = tween(durationMillis = 1200, easing = LinearEasing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return Brush.linearGradient(
|
||||||
|
colors = shimmerColors,
|
||||||
|
start = Offset(0f, 0f),
|
||||||
|
end = Offset(translateAnim, translateAnim),
|
||||||
|
)
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.components
|
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
||||||
|
|
||||||
import androidx.compose.foundation.focusable
|
import androidx.compose.foundation.focusable
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
+5
-7
@@ -23,8 +23,6 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@androidx.compose.runtime.Composable
|
@androidx.compose.runtime.Composable
|
||||||
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) {
|
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) {
|
||||||
@@ -53,22 +51,22 @@ fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, lea
|
|||||||
Column(
|
Column(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.padding(horizontal = 8.dp.scaledWidth(), vertical = 10.dp.scaledHeight())
|
.padding(horizontal = 8.dp, vertical = 10.dp)
|
||||||
.padding(end = 16.dp.scaledWidth()).padding(start = 8.dp.scaledWidth())
|
.padding(end = 16.dp).padding(start = 8.dp)
|
||||||
.fillMaxSize(),
|
.fillMaxSize(),
|
||||||
verticalArrangement = Arrangement.Center,
|
verticalArrangement = Arrangement.Center,
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
verticalAlignment = Alignment.Companion.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.spacedBy(16.dp.scaledWidth()),
|
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
horizontalArrangement = Arrangement.spacedBy(
|
horizontalArrangement = Arrangement.spacedBy(
|
||||||
16.dp.scaledWidth(),
|
16.dp,
|
||||||
),
|
),
|
||||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
verticalAlignment = Alignment.Companion.CenterVertically,
|
||||||
modifier = Modifier.padding(vertical = if (description == null) 10.dp.scaledHeight() else 0.dp),
|
modifier = Modifier.padding(vertical = if (description == null) 10.dp else 0.dp),
|
||||||
) {
|
) {
|
||||||
leadingIcon?.let {
|
leadingIcon?.let {
|
||||||
Icon(
|
Icon(
|
||||||
|
|||||||
+1
-2
@@ -7,14 +7,13 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.scale
|
import androidx.compose.ui.draw.scale
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled: Boolean = true, modifier: Modifier = Modifier) {
|
fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled: Boolean = true, modifier: Modifier = Modifier) {
|
||||||
Switch(
|
Switch(
|
||||||
checked,
|
checked,
|
||||||
{ onClick(it) },
|
{ onClick(it) },
|
||||||
modifier.scale((52.dp.scaledHeight() / 52.dp)),
|
modifier.scale((52.dp / 52.dp)),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
colors = SwitchDefaults.colors().copy(
|
colors = SwitchDefaults.colors().copy(
|
||||||
checkedThumbColor = MaterialTheme.colorScheme.background,
|
checkedThumbColor = MaterialTheme.colorScheme.background,
|
||||||
|
|||||||
+6
-6
@@ -19,10 +19,9 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SelectionItemButton(
|
fun SelectionItemButton(
|
||||||
@@ -31,20 +30,21 @@ fun SelectionItemButton(
|
|||||||
trailing: (@Composable () -> Unit)? = null,
|
trailing: (@Composable () -> Unit)? = null,
|
||||||
onClick: () -> Unit,
|
onClick: () -> Unit,
|
||||||
ripple: Boolean = true,
|
ripple: Boolean = true,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
Card(
|
Card(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
modifier
|
||||||
.clip(RoundedCornerShape(8.dp))
|
.clip(RoundedCornerShape(8.dp))
|
||||||
.clickable(
|
.clickable(
|
||||||
indication = if (ripple) ripple() else null,
|
indication = if (ripple) ripple() else null,
|
||||||
interactionSource = remember { MutableInteractionSource() },
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
onClick = { onClick() },
|
onClick = { onClick() },
|
||||||
)
|
)
|
||||||
.height(56.dp.scaledHeight()),
|
.height(56.dp),
|
||||||
colors =
|
colors =
|
||||||
CardDefaults.cardColors(
|
CardDefaults.cardColors(
|
||||||
containerColor = MaterialTheme.colorScheme.background,
|
containerColor = Color.Transparent,
|
||||||
),
|
),
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
@@ -52,7 +52,7 @@ fun SelectionItemButton(
|
|||||||
horizontalArrangement = Arrangement.Start,
|
horizontalArrangement = Arrangement.Start,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(end = 10.dp.scaledWidth()),
|
.padding(end = 10.dp),
|
||||||
) {
|
) {
|
||||||
leading?.let {
|
leading?.let {
|
||||||
it()
|
it()
|
||||||
|
|||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
||||||
|
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SelectionItemLabel(
|
||||||
|
textResId: Int,
|
||||||
|
style: androidx.compose.ui.text.TextStyle = MaterialTheme.typography.bodyMedium,
|
||||||
|
isDescription: Boolean = false,
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = stringResource(textResId),
|
||||||
|
style = style.copy(
|
||||||
|
color = if (isDescription) MaterialTheme.colorScheme.outline else MaterialTheme.colorScheme.onSurface,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
+5
-7
@@ -19,8 +19,6 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
||||||
@@ -38,12 +36,12 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp.scaledHeight()),
|
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(start = 16.dp.scaledWidth())
|
.padding(start = 16.dp)
|
||||||
.weight(4f, false)
|
.weight(4f, false)
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
) {
|
) {
|
||||||
@@ -60,8 +58,8 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|||||||
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(start = if (item.leadingIcon != null) 16.dp.scaledWidth() else 0.dp)
|
.padding(start = if (item.leadingIcon != null) 16.dp else 0.dp)
|
||||||
.padding(vertical = if (item.description == null) 16.dp.scaledHeight() else 6.dp.scaledHeight()),
|
.padding(vertical = if (item.description == null) 16.dp else 6.dp),
|
||||||
) {
|
) {
|
||||||
item.title()
|
item.title()
|
||||||
item.description?.let {
|
item.description?.let {
|
||||||
@@ -73,7 +71,7 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|||||||
Box(
|
Box(
|
||||||
contentAlignment = Alignment.CenterEnd,
|
contentAlignment = Alignment.CenterEnd,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(end = 24.dp.scaledWidth(), start = 16.dp.scaledWidth())
|
.padding(end = 24.dp, start = 16.dp)
|
||||||
.weight(1f),
|
.weight(1f),
|
||||||
) {
|
) {
|
||||||
it()
|
it()
|
||||||
|
|||||||
+2
-2
@@ -35,9 +35,9 @@ fun ConfigurationTextBox(
|
|||||||
singleLine = true,
|
singleLine = true,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
onValueChange = { onValueChange(it) },
|
onValueChange = { onValueChange(it) },
|
||||||
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.labelMedium) },
|
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.bodyMedium) },
|
||||||
maxLines = 1,
|
maxLines = 1,
|
||||||
placeholder = { Text(hint, color = MaterialTheme.colorScheme.outline, style = MaterialTheme.typography.labelLarge) },
|
placeholder = { Text(hint, color = MaterialTheme.colorScheme.outline, style = MaterialTheme.typography.bodyMedium) },
|
||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions = keyboardOptions,
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
trailingIcon = trailing,
|
trailingIcon = trailing,
|
||||||
|
|||||||
+1
-2
@@ -24,7 +24,6 @@ import androidx.compose.ui.text.input.ImeAction
|
|||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SubmitConfigurationTextBox(
|
fun SubmitConfigurationTextBox(
|
||||||
@@ -59,7 +58,7 @@ fun SubmitConfigurationTextBox(
|
|||||||
.padding(
|
.padding(
|
||||||
top = 5.dp,
|
top = 5.dp,
|
||||||
bottom = 10.dp,
|
bottom = 10.dp,
|
||||||
).fillMaxWidth().padding(end = 16.dp.scaledWidth()),
|
).fillMaxWidth().padding(end = 16.dp),
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions = keyboardOptions,
|
||||||
keyboardActions = KeyboardActions(
|
keyboardActions = KeyboardActions(
|
||||||
|
|||||||
+31
-18
@@ -1,10 +1,14 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.dialog
|
package com.zaneschepke.wireguardautotunnel.ui.common.dialog
|
||||||
|
|
||||||
import androidx.compose.material3.AlertDialog
|
import androidx.compose.material3.AlertDialog
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.window.DialogProperties
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -15,23 +19,32 @@ fun InfoDialog(
|
|||||||
body: @Composable () -> Unit,
|
body: @Composable () -> Unit,
|
||||||
confirmText: @Composable () -> Unit,
|
confirmText: @Composable () -> Unit,
|
||||||
) {
|
) {
|
||||||
AlertDialog(
|
MaterialTheme(
|
||||||
onDismissRequest = { onDismiss() },
|
colorScheme = MaterialTheme.colorScheme.copy(),
|
||||||
confirmButton = {
|
) {
|
||||||
TextButton(
|
Surface(
|
||||||
onClick = {
|
color = MaterialTheme.colorScheme.surface,
|
||||||
onAttest()
|
tonalElevation = 0.dp,
|
||||||
|
) {
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = { onDismiss() },
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(onClick = { onAttest() }) {
|
||||||
|
confirmText()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
) {
|
dismissButton = {
|
||||||
confirmText()
|
TextButton(onClick = { onDismiss() }) {
|
||||||
}
|
Text(text = stringResource(R.string.cancel))
|
||||||
},
|
}
|
||||||
dismissButton = {
|
},
|
||||||
TextButton(onClick = { onDismiss() }) {
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
Text(text = stringResource(R.string.cancel))
|
title = { title() },
|
||||||
}
|
text = { body() },
|
||||||
},
|
properties = DialogProperties(
|
||||||
title = { title() },
|
usePlatformDefaultWidth = true,
|
||||||
text = { body() },
|
),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn
|
package com.zaneschepke.wireguardautotunnel.ui.common.dialog
|
||||||
|
|
||||||
import androidx.compose.foundation.text.ClickableText
|
import androidx.compose.foundation.text.ClickableText
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
@@ -10,7 +10,6 @@ import androidx.compose.ui.text.SpanStyle
|
|||||||
import androidx.compose.ui.text.buildAnnotatedString
|
import androidx.compose.ui.text.buildAnnotatedString
|
||||||
import androidx.compose.ui.text.withStyle
|
import androidx.compose.ui.text.withStyle
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
|
import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@@ -6,6 +6,7 @@ import androidx.compose.material3.MaterialTheme
|
|||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun GroupLabel(title: String) {
|
fun GroupLabel(title: String) {
|
||||||
@@ -16,6 +17,7 @@ fun GroupLabel(title: String) {
|
|||||||
Text(
|
Text(
|
||||||
title,
|
title,
|
||||||
style = MaterialTheme.typography.titleMedium,
|
style = MaterialTheme.typography.titleMedium,
|
||||||
|
fontWeight = FontWeight.Bold,
|
||||||
color = MaterialTheme.colorScheme.onBackground,
|
color = MaterialTheme.colorScheme.onBackground,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
package com.zaneschepke.wireguardautotunnel.ui.common.label
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
+93
@@ -0,0 +1,93 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import androidx.compose.animation.animateColorAsState
|
||||||
|
import androidx.compose.animation.core.Spring
|
||||||
|
import androidx.compose.animation.core.spring
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.offset
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material3.Badge
|
||||||
|
import androidx.compose.material3.BadgedBox
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun BottomBarTabs(tabs: List<BottomNavItem>, selectedTabIndex: Int, isChildRoute: Boolean, onTabSelected: (BottomNavItem) -> Unit) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(64.dp)
|
||||||
|
.padding(horizontal = 8.dp)
|
||||||
|
.padding(top = 12.dp),
|
||||||
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
tabs.forEachIndexed { index, tab ->
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.fillMaxHeight()
|
||||||
|
.background(Color.Transparent)
|
||||||
|
.pointerInput(Unit) {
|
||||||
|
detectTapGestures {
|
||||||
|
if (index == selectedTabIndex && !isChildRoute) return@detectTapGestures
|
||||||
|
tab.onClick.invoke()
|
||||||
|
onTabSelected(tab)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
) {
|
||||||
|
val animatedColor by animateColorAsState(
|
||||||
|
targetValue = MaterialTheme.colorScheme.primary,
|
||||||
|
animationSpec = spring(stiffness = Spring.StiffnessLow),
|
||||||
|
label = "animatedColor",
|
||||||
|
)
|
||||||
|
val color = if (selectedTabIndex == index) animatedColor else MaterialTheme.colorScheme.onSurface
|
||||||
|
|
||||||
|
if (tab.active) {
|
||||||
|
BadgedBox(
|
||||||
|
badge = {
|
||||||
|
Badge(
|
||||||
|
modifier = Modifier
|
||||||
|
.offset(x = 8.dp, y = ((-8).dp))
|
||||||
|
.size(6.dp),
|
||||||
|
containerColor = SilverTree,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = tab.icon,
|
||||||
|
contentDescription = tab.name,
|
||||||
|
tint = color,
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Icon(
|
||||||
|
imageVector = tab.icon,
|
||||||
|
contentDescription = tab.name,
|
||||||
|
tint = color,
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-73
@@ -1,73 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.NavigationBar
|
|
||||||
import androidx.compose.material3.NavigationBarItem
|
|
||||||
import androidx.compose.material3.NavigationBarItemDefaults
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.navigation.NavController
|
|
||||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavItem>) {
|
|
||||||
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
|
||||||
|
|
||||||
showBottomBar = bottomNavItems.any {
|
|
||||||
navBackStackEntry?.isCurrentRoute(it.route::class) == true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showBottomBar) {
|
|
||||||
NavigationBar(
|
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
|
||||||
) {
|
|
||||||
bottomNavItems.forEachIndexed { index, item ->
|
|
||||||
val selected = navBackStackEntry.isCurrentRoute(item.route::class)
|
|
||||||
NavigationBarItem(
|
|
||||||
selected = selected,
|
|
||||||
onClick = {
|
|
||||||
if (selected) return@NavigationBarItem
|
|
||||||
navController.navigate(item.route) {
|
|
||||||
// Pop up to the start destination of the graph to
|
|
||||||
// avoid building up a large stack of destinations
|
|
||||||
// on the back stack as users select items
|
|
||||||
popUpTo(navController.graph.findStartDestination().id) {
|
|
||||||
saveState = true
|
|
||||||
}
|
|
||||||
// Avoid multiple copies of the same destination when
|
|
||||||
// reselecting the same item
|
|
||||||
launchSingleTop = true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
text = item.name,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
icon = {
|
|
||||||
Icon(
|
|
||||||
imageVector = item.icon,
|
|
||||||
contentDescription = "${item.name} Icon",
|
|
||||||
)
|
|
||||||
},
|
|
||||||
colors = NavigationBarItemDefaults.colors().copy(
|
|
||||||
selectedIndicatorColor = Color.Transparent,
|
|
||||||
selectedIconColor = MaterialTheme.colorScheme.primary,
|
|
||||||
selectedTextColor = MaterialTheme.colorScheme.primary,
|
|
||||||
unselectedTextColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
|
||||||
unselectedIconColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
@@ -7,4 +7,6 @@ data class BottomNavItem(
|
|||||||
val name: String,
|
val name: String,
|
||||||
val route: Route,
|
val route: Route,
|
||||||
val icon: ImageVector,
|
val icon: ImageVector,
|
||||||
|
val onClick: () -> Unit,
|
||||||
|
val active: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import androidx.compose.animation.animateColorAsState
|
||||||
|
import androidx.compose.animation.core.Spring
|
||||||
|
import androidx.compose.animation.core.animateFloatAsState
|
||||||
|
import androidx.compose.animation.core.spring
|
||||||
|
import androidx.compose.foundation.Canvas
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.WindowInsets
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.systemBars
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableIntStateOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.geometry.CornerRadius
|
||||||
|
import androidx.compose.ui.geometry.RoundRect
|
||||||
|
import androidx.compose.ui.geometry.toRect
|
||||||
|
import androidx.compose.ui.graphics.Brush
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.Path
|
||||||
|
import androidx.compose.ui.graphics.PathEffect
|
||||||
|
import androidx.compose.ui.graphics.PathMeasure
|
||||||
|
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||||
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun CustomBottomNavbar(tabs: List<BottomNavItem>, navBarState: NavBarState) {
|
||||||
|
var selectedTabIndex by remember { mutableIntStateOf(0) }
|
||||||
|
var isChildRoute by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
LaunchedEffect(tabs) {
|
||||||
|
}
|
||||||
|
when (navBarState.route) {
|
||||||
|
Route.Main -> {
|
||||||
|
selectedTabIndex = 0
|
||||||
|
isChildRoute = false
|
||||||
|
}
|
||||||
|
Route.AutoTunnel -> {
|
||||||
|
selectedTabIndex = 1
|
||||||
|
isChildRoute = false
|
||||||
|
}
|
||||||
|
Route.Settings -> {
|
||||||
|
selectedTabIndex = 2
|
||||||
|
isChildRoute = false
|
||||||
|
}
|
||||||
|
Route.Support -> {
|
||||||
|
selectedTabIndex = 3
|
||||||
|
isChildRoute = false
|
||||||
|
}
|
||||||
|
else -> isChildRoute = true
|
||||||
|
}
|
||||||
|
|
||||||
|
val systemBars = WindowInsets.systemBars
|
||||||
|
val bottomPadding = with(LocalDensity.current) {
|
||||||
|
systemBars.getBottom(this).toDp()
|
||||||
|
}
|
||||||
|
val navHeight = 64.dp + bottomPadding
|
||||||
|
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(navHeight)
|
||||||
|
.background(Color.Transparent),
|
||||||
|
) {
|
||||||
|
BottomBarTabs(
|
||||||
|
tabs = tabs,
|
||||||
|
selectedTabIndex = selectedTabIndex,
|
||||||
|
isChildRoute = isChildRoute,
|
||||||
|
onTabSelected = {
|
||||||
|
selectedTabIndex = tabs.indexOf(it)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
val animatedSelectedTabIndex by animateFloatAsState(
|
||||||
|
targetValue = selectedTabIndex.toFloat(),
|
||||||
|
label = "animatedSelectedTabIndex",
|
||||||
|
animationSpec = spring(
|
||||||
|
stiffness = Spring.StiffnessLow,
|
||||||
|
dampingRatio = Spring.DampingRatioLowBouncy,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
val animatedColor by animateColorAsState(
|
||||||
|
targetValue = MaterialTheme.colorScheme.primary,
|
||||||
|
label = "animatedColor",
|
||||||
|
animationSpec = spring(
|
||||||
|
stiffness = Spring.StiffnessLow,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
Canvas(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(navHeight),
|
||||||
|
) {
|
||||||
|
val path = Path().apply {
|
||||||
|
addRoundRect(RoundRect(size.toRect(), CornerRadius(size.height)))
|
||||||
|
}
|
||||||
|
val length = PathMeasure().apply { setPath(path, false) }.length
|
||||||
|
|
||||||
|
val tabWidth = size.width / tabs.size
|
||||||
|
drawPath(
|
||||||
|
path,
|
||||||
|
brush = Brush.horizontalGradient(
|
||||||
|
colors = listOf(
|
||||||
|
animatedColor.copy(alpha = 0f),
|
||||||
|
animatedColor.copy(alpha = 1f),
|
||||||
|
animatedColor.copy(alpha = 1f),
|
||||||
|
animatedColor.copy(alpha = 0f),
|
||||||
|
),
|
||||||
|
startX = tabWidth * animatedSelectedTabIndex,
|
||||||
|
endX = tabWidth * (animatedSelectedTabIndex + 1),
|
||||||
|
),
|
||||||
|
style = Stroke(
|
||||||
|
width = 4f,
|
||||||
|
pathEffect = PathEffect.dashPathEffect(
|
||||||
|
intervals = floatArrayOf(length / 2, length),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
|
import androidx.compose.animation.fadeIn
|
||||||
|
import androidx.compose.animation.fadeOut
|
||||||
|
import androidx.compose.animation.slideInVertically
|
||||||
|
import androidx.compose.animation.slideOutVertically
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
import androidx.compose.material3.TopAppBar
|
||||||
|
import androidx.compose.material3.TopAppBarDefaults
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
fun DynamicTopAppBar(navBarState: NavBarState, modifier: Modifier = Modifier) {
|
||||||
|
TopAppBar(
|
||||||
|
modifier = modifier,
|
||||||
|
colors = TopAppBarDefaults.topAppBarColors().copy(Color.Transparent),
|
||||||
|
title = {
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = navBarState.showTop,
|
||||||
|
enter = slideInVertically() + fadeIn(),
|
||||||
|
exit = slideOutVertically() + fadeOut(),
|
||||||
|
) {
|
||||||
|
Box(modifier = Modifier.padding(start = 10.dp)) {
|
||||||
|
navBarState.topTitle?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
actions = {
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = navBarState.showTop,
|
||||||
|
enter = slideInVertically() + fadeIn(),
|
||||||
|
exit = slideOutVertically() + fadeOut(),
|
||||||
|
) {
|
||||||
|
Box(modifier = Modifier.padding(end = 10.dp)) {
|
||||||
|
navBarState.topTrailing?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
+217
@@ -0,0 +1,217 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Add
|
||||||
|
import androidx.compose.material.icons.rounded.Edit
|
||||||
|
import androidx.compose.material.icons.rounded.Menu
|
||||||
|
import androidx.compose.material.icons.rounded.PlayArrow
|
||||||
|
import androidx.compose.material.icons.rounded.Save
|
||||||
|
import androidx.compose.material.icons.rounded.Stop
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.State
|
||||||
|
import androidx.compose.runtime.produceState
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.navigation.NavBackStackEntry
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import androidx.navigation.toRoute
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.Brick
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
data class NavBarState(
|
||||||
|
val showTop: Boolean = true,
|
||||||
|
val showBottom: Boolean = true,
|
||||||
|
val topTitle: @Composable (() -> Unit)? = null,
|
||||||
|
val topTrailing: @Composable (() -> Unit)? = null,
|
||||||
|
val route: Route? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun currentNavBackStackEntryAsNavBarState(
|
||||||
|
navController: NavController,
|
||||||
|
backStackEntry: NavBackStackEntry?,
|
||||||
|
viewModel: AppViewModel,
|
||||||
|
uiState: AppUiState,
|
||||||
|
): State<NavBarState> {
|
||||||
|
return produceState(initialValue = NavBarState(), key1 = backStackEntry, key2 = uiState) {
|
||||||
|
value = when {
|
||||||
|
backStackEntry.isCurrentRoute(Route.Main::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.tunnels)) },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleBottomSheet) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Add
|
||||||
|
Icon(icon, stringResource(R.string.add_tunnel), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = Route.Main,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.AutoTunnel::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.auto_tunnel)) },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnel) },
|
||||||
|
) {
|
||||||
|
val (icon, description, color) = if (uiState.appSettings.isAutoTunnelEnabled) {
|
||||||
|
Triple(Icons.Rounded.Stop, R.string.stop_auto, Brick)
|
||||||
|
} else {
|
||||||
|
Triple(Icons.Rounded.PlayArrow, R.string.start_auto, SilverTree)
|
||||||
|
}
|
||||||
|
Icon(icon, stringResource(description), tint = color, modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = Route.AutoTunnel,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.AutoTunnelAdvanced::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.advanced_settings)) },
|
||||||
|
route = Route.AutoTunnelAdvanced,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Settings::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.settings)) },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleBottomSheet) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Menu
|
||||||
|
Icon(icon, stringResource(R.string.quick_actions), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = Route.Settings,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.KillSwitch::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.kill_switch)) },
|
||||||
|
route = Route.KillSwitch,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Appearance::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.appearance)) },
|
||||||
|
route = Route.Appearance,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Language::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.language)) },
|
||||||
|
route = Route.Language,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Display::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.display_theme)) },
|
||||||
|
route = Route.Display,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Logs::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = false,
|
||||||
|
{ Text(stringResource(R.string.logs)) },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleBottomSheet) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Menu
|
||||||
|
Icon(icon, stringResource(R.string.quick_actions), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = Route.Logs,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.TunnelOptions::class) -> {
|
||||||
|
val args = backStackEntry?.toRoute<Route.TunnelOptions>()
|
||||||
|
val tunnel = uiState.tunnels.find { it.id == args?.id }
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ tunnel?.name?.let { Text(it) } },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { tunnel?.id?.let { navController.navigate(Route.Config(id = it)) } },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Edit
|
||||||
|
Icon(icon, stringResource(R.string.edit_tunnel), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = args?.let { Route.TunnelOptions(it.id) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.SplitTunnel::class) -> {
|
||||||
|
val args = backStackEntry?.toRoute<Route.SplitTunnel>()
|
||||||
|
val name = uiState.tunnels.find { it.id == args?.id }?.name
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ name?.let { Text(it) } },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.InvokeScreenAction) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Save
|
||||||
|
Icon(icon, stringResource(R.string.save), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = args?.let { Route.SplitTunnel(it.id) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Config::class) -> {
|
||||||
|
val args = backStackEntry?.toRoute<Route.Config>()
|
||||||
|
val name = uiState.tunnels.find { it.id == args?.id }?.name
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ name?.let { Text(it) } },
|
||||||
|
{
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.InvokeScreenAction) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Save
|
||||||
|
Icon(icon, stringResource(R.string.save), modifier = Modifier.size(iconSize))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
route = args?.let { Route.Config(it.id) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.TunnelAutoTunnel::class) -> {
|
||||||
|
val args = backStackEntry?.toRoute<Route.TunnelAutoTunnel>()
|
||||||
|
val name = uiState.tunnels.find { it.id == args?.id }?.name
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ name?.let { Text(it) } },
|
||||||
|
route = args?.let { Route.TunnelAutoTunnel(it.id) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
backStackEntry.isCurrentRoute(Route.Support::class) -> {
|
||||||
|
NavBarState(
|
||||||
|
showTop = true, showBottom = true,
|
||||||
|
{ Text(stringResource(R.string.support)) },
|
||||||
|
route = Route.Support,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else -> NavBarState(showTop = false, showBottom = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
|
|
||||||
import androidx.compose.material3.CenterAlignedTopAppBar
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun TopNavBar(title: String, trailing: @Composable () -> Unit = {}, showBack: Boolean = true) {
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
CenterAlignedTopAppBar(
|
|
||||||
title = {
|
|
||||||
Text(title)
|
|
||||||
},
|
|
||||||
navigationIcon = {
|
|
||||||
if (showBack) {
|
|
||||||
IconButton(onClick = {
|
|
||||||
navController.popBackStack()
|
|
||||||
}) {
|
|
||||||
val icon = Icons.AutoMirrored.Outlined.ArrowBack
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actions = {
|
|
||||||
trailing()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
-38
@@ -1,38 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn
|
|
||||||
|
|
||||||
import android.net.VpnService
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
inline fun <T> withVpnPermission(crossinline onSuccess: (t: T) -> Unit): (t: T) -> Unit {
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val vpnActivity =
|
|
||||||
rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
onResult = {
|
|
||||||
if (it.resultCode != RESULT_OK) showVpnPermissionDialog = true
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
|
||||||
|
|
||||||
return {
|
|
||||||
val intent = VpnService.prepare(context)
|
|
||||||
if (intent != null) {
|
|
||||||
vpnActivity.launch(intent)
|
|
||||||
} else {
|
|
||||||
onSuccess(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.permission
|
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import android.provider.Settings
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
||||||
import androidx.activity.result.ActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isBatteryOptimizationsDisabled
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
inline fun withIgnoreBatteryOpt(ignore: Boolean, crossinline callback: () -> Unit): () -> Unit {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val batteryActivity =
|
|
||||||
rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
) { result: ActivityResult ->
|
|
||||||
// we only ask once
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
if (ignore || context.isBatteryOptimizationsDisabled()) {
|
|
||||||
callback()
|
|
||||||
} else {
|
|
||||||
batteryActivity.launch(
|
|
||||||
Intent().apply {
|
|
||||||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
|
||||||
data = Uri.parse("package:${context.packageName}")
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+9
-21
@@ -9,6 +9,7 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) {
|
fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) {
|
||||||
@@ -18,33 +19,20 @@ fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (
|
|||||||
val isBiometricAvailable =
|
val isBiometricAvailable =
|
||||||
remember {
|
remember {
|
||||||
when (bio) {
|
when (bio) {
|
||||||
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
|
|
||||||
onError("Biometrics not available")
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
|
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
|
||||||
onError("Biometrics not created")
|
onError(context.getString(R.string.bio_not_created))
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
|
|
||||||
onError("Biometric hardware not found")
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
|
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
|
||||||
onError("Biometric security update required")
|
onError(context.getString(R.string.bio_update_required))
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
|
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED, BiometricManager.BIOMETRIC_STATUS_UNKNOWN,
|
||||||
onError("Biometrics not supported")
|
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE, BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE,
|
||||||
false
|
-> {
|
||||||
}
|
onError(context.getString(R.string.bio_not_supported))
|
||||||
|
|
||||||
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
|
|
||||||
onError("Biometrics status unknown")
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +46,8 @@ fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (
|
|||||||
val promptInfo =
|
val promptInfo =
|
||||||
BiometricPrompt.PromptInfo.Builder()
|
BiometricPrompt.PromptInfo.Builder()
|
||||||
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
||||||
.setTitle("Biometric Authentication")
|
.setTitle(context.getString(R.string.bio_auth_title))
|
||||||
.setSubtitle("Log in using your biometric credential")
|
.setSubtitle(context.getString(R.string.bio_subtitle))
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val biometricPrompt =
|
val biometricPrompt =
|
||||||
|
|||||||
-108
@@ -1,108 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.snackbar
|
|
||||||
|
|
||||||
import androidx.compose.material3.SnackbarDuration
|
|
||||||
import androidx.compose.material3.SnackbarHostState
|
|
||||||
import androidx.compose.material3.SnackbarResult
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
|
||||||
import androidx.compose.runtime.DisposableEffect
|
|
||||||
import androidx.compose.runtime.Immutable
|
|
||||||
import androidx.compose.runtime.ReadOnlyComposable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
|
||||||
import androidx.compose.runtime.staticCompositionLocalOf
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlin.coroutines.EmptyCoroutineContext
|
|
||||||
|
|
||||||
private val LocalSnackbarController = staticCompositionLocalOf {
|
|
||||||
SnackbarController(
|
|
||||||
host = SnackbarHostState(),
|
|
||||||
scope = CoroutineScope(EmptyCoroutineContext),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
private val channel = Channel<SnackbarChannelMessage>(capacity = 1)
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SnackbarControllerProvider(content: @Composable (snackbarHost: SnackbarHostState) -> Unit) {
|
|
||||||
val snackHostState = remember { SnackbarHostState() }
|
|
||||||
val scope = rememberCoroutineScope()
|
|
||||||
val snackController = remember(scope) { SnackbarController(snackHostState, scope) }
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
DisposableEffect(snackController, scope) {
|
|
||||||
val job = scope.launch {
|
|
||||||
for (payload in channel) {
|
|
||||||
snackController.showMessage(
|
|
||||||
message = payload.message.asString(context),
|
|
||||||
duration = payload.duration,
|
|
||||||
action = payload.action,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onDispose {
|
|
||||||
job.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CompositionLocalProvider(LocalSnackbarController provides snackController) {
|
|
||||||
content(
|
|
||||||
snackHostState,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Immutable
|
|
||||||
class SnackbarController(
|
|
||||||
private val host: SnackbarHostState,
|
|
||||||
private val scope: CoroutineScope,
|
|
||||||
) {
|
|
||||||
companion object {
|
|
||||||
val current
|
|
||||||
@Composable
|
|
||||||
@ReadOnlyComposable
|
|
||||||
get() = LocalSnackbarController.current
|
|
||||||
|
|
||||||
fun showMessage(message: StringValue, action: SnackbarAction? = null, duration: SnackbarDuration = SnackbarDuration.Short) {
|
|
||||||
channel.trySend(
|
|
||||||
SnackbarChannelMessage(
|
|
||||||
message = message,
|
|
||||||
duration = duration,
|
|
||||||
action = action,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showMessage(message: String, action: SnackbarAction? = null, duration: SnackbarDuration = SnackbarDuration.Short) {
|
|
||||||
scope.launch {
|
|
||||||
/**
|
|
||||||
* note: uncomment this line if you want snackbar to be displayed immediately,
|
|
||||||
* rather than being enqueued and waiting [duration] * current_queue_size
|
|
||||||
*/
|
|
||||||
host.currentSnackbarData?.dismiss()
|
|
||||||
val result =
|
|
||||||
host.showSnackbar(
|
|
||||||
message = message,
|
|
||||||
actionLabel = action?.title,
|
|
||||||
duration = duration,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (result == SnackbarResult.ActionPerformed) {
|
|
||||||
action?.onActionPress?.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class SnackbarChannelMessage(
|
|
||||||
val message: StringValue,
|
|
||||||
val action: SnackbarAction?,
|
|
||||||
val duration: SnackbarDuration = SnackbarDuration.Short,
|
|
||||||
)
|
|
||||||
|
|
||||||
data class SnackbarAction(val title: String, val onActionPress: () -> Unit)
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.text
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
|
||||||
import androidx.compose.ui.unit.Dp
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SectionTitle(title: String, padding: Dp) {
|
|
||||||
Text(
|
|
||||||
title,
|
|
||||||
textAlign = TextAlign.Start,
|
|
||||||
style = MaterialTheme.typography.titleMedium,
|
|
||||||
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||||
|
import com.google.accompanist.permissions.isGranted
|
||||||
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.AdvancedSettingsItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.NetworkTunnelingItems
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.WifiTunnelingItems
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.BackgroundLocationDialog
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LocationServicesDialog
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isLocationServicesEnabled
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
|
||||||
|
@OptIn(ExperimentalPermissionsApi::class)
|
||||||
|
@Composable
|
||||||
|
fun AutoTunnelScreen(uiState: AppUiState, viewModel: AppViewModel) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
|
var currentText by remember { mutableStateOf("") }
|
||||||
|
var isBackgroundLocationGranted by remember { mutableStateOf(true) }
|
||||||
|
var showLocationServicesAlertDialog by remember { mutableStateOf(false) }
|
||||||
|
var showLocationDialog by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
fun checkFineLocationGranted() {
|
||||||
|
isBackgroundLocationGranted = fineLocationState.status.isGranted
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isWifiNameReadable(): Boolean {
|
||||||
|
return when {
|
||||||
|
!isBackgroundLocationGranted || !fineLocationState.status.isGranted -> {
|
||||||
|
showLocationDialog = true
|
||||||
|
false
|
||||||
|
}
|
||||||
|
!context.isLocationServicesEnabled() -> {
|
||||||
|
showLocationServicesAlertDialog = true
|
||||||
|
false
|
||||||
|
}
|
||||||
|
else -> true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) checkFineLocationGranted()
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
if (context.isRunningOnTv() && Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
|
||||||
|
checkFineLocationGranted()
|
||||||
|
} else {
|
||||||
|
val backgroundLocationState = rememberPermissionState(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
|
||||||
|
isBackgroundLocationGranted = backgroundLocationState.status.isGranted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(uiState.appSettings.trustedNetworkSSIDs) {
|
||||||
|
currentText = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
LocationServicesDialog(
|
||||||
|
showLocationServicesAlertDialog,
|
||||||
|
onDismiss = { showLocationServicesAlertDialog = false },
|
||||||
|
onAttest = { showLocationServicesAlertDialog = false },
|
||||||
|
)
|
||||||
|
|
||||||
|
BackgroundLocationDialog(
|
||||||
|
showLocationDialog,
|
||||||
|
onDismiss = { showLocationDialog = false },
|
||||||
|
onAttest = { showLocationDialog = false },
|
||||||
|
)
|
||||||
|
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(vertical = 24.dp)
|
||||||
|
.padding(horizontal = 24.dp),
|
||||||
|
) {
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
items = WifiTunnelingItems(uiState, viewModel, currentText, { currentText = it }, { isWifiNameReadable() }),
|
||||||
|
)
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
items = NetworkTunnelingItems(uiState, viewModel),
|
||||||
|
)
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
items = listOf(AdvancedSettingsItem()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.components.DebounceDelaySelector
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AdvancedScreen(appUiState: AppUiState) {
|
||||||
|
val appViewModel: AppViewModel = hiltViewModel()
|
||||||
|
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(top = 24.dp)
|
||||||
|
.padding(horizontal = 24.dp),
|
||||||
|
) {
|
||||||
|
DebounceDelaySelector(
|
||||||
|
currentDelay = appUiState.appSettings.debounceDelaySeconds,
|
||||||
|
onEvent = appViewModel::handleEvent,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.components
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.PauseCircle
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.DropdownSelector
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun DebounceDelaySelector(currentDelay: Int, onEvent: (AppEvent) -> Unit) {
|
||||||
|
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
listOf(
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.PauseCircle,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.debounce_delay),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(
|
||||||
|
color = MaterialTheme.colorScheme.onSurface,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { isDropDownExpanded = true },
|
||||||
|
trailing = {
|
||||||
|
DropdownSelector(
|
||||||
|
currentValue = currentDelay,
|
||||||
|
options = (0..10).toList(),
|
||||||
|
onValueSelected = { num -> onEvent(AppEvent.SetDebounceDelay(num)) },
|
||||||
|
isExpanded = isDropDownExpanded,
|
||||||
|
onDismiss = { isDropDownExpanded = false },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.Settings
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AdvancedSettingsItem(): SelectionItem {
|
||||||
|
val navController = LocalNavController.current
|
||||||
|
return SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.Settings,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.advanced_settings),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ForwardButton { navController.navigate(Route.AutoTunnelAdvanced) }
|
||||||
|
},
|
||||||
|
onClick = { navController.navigate(Route.AutoTunnelAdvanced) },
|
||||||
|
)
|
||||||
|
}
|
||||||
+98
@@ -0,0 +1,98 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.AirplanemodeActive
|
||||||
|
import androidx.compose.material.icons.outlined.SettingsEthernet
|
||||||
|
import androidx.compose.material.icons.outlined.SignalCellular4Bar
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<SelectionItem> {
|
||||||
|
return listOf(
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.SignalCellular4Bar,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.tunnel_mobile_data),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled,
|
||||||
|
checked = uiState.appSettings.isTunnelOnMobileDataEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnCellular) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
val cellularActive = remember(uiState.networkStatus) { uiState.networkStatus?.cellularConnected ?: false }
|
||||||
|
Text(
|
||||||
|
text = if (cellularActive) stringResource(R.string.active) else stringResource(R.string.inactive),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.outline),
|
||||||
|
maxLines = 1,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnCellular) },
|
||||||
|
),
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.SettingsEthernet,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.tunnel_on_ethernet),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled,
|
||||||
|
checked = uiState.appSettings.isTunnelOnEthernetEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnEthernet) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
val ethernetActive = remember(uiState.networkStatus) { uiState.networkStatus?.ethernetConnected ?: false }
|
||||||
|
Text(
|
||||||
|
text = if (ethernetActive) stringResource(R.string.active) else stringResource(R.string.inactive),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.outline),
|
||||||
|
maxLines = 1,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnEthernet) },
|
||||||
|
),
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.AirplanemodeActive,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.stop_on_no_internet),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.stop_on_internet_loss),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
checked = uiState.appSettings.isStopOnNoInternetEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleStopTunnelOnNoInternet) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleStopTunnelOnNoInternet) },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
+4
-6
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
|
||||||
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
@@ -26,11 +26,9 @@ import androidx.compose.ui.text.input.ImeAction
|
|||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ClickableIconButton
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@OptIn(ExperimentalLayoutApi::class)
|
@OptIn(ExperimentalLayoutApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
@@ -43,7 +41,7 @@ fun TrustedNetworkTextBox(
|
|||||||
supporting: @Composable () -> Unit,
|
supporting: @Composable () -> Unit,
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
Column(verticalArrangement = Arrangement.spacedBy(10.dp.scaledHeight())) {
|
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||||
FlowRow(
|
FlowRow(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier.fillMaxWidth(),
|
Modifier.fillMaxWidth(),
|
||||||
@@ -83,7 +81,7 @@ fun TrustedNetworkTextBox(
|
|||||||
.padding(
|
.padding(
|
||||||
top = 5.dp,
|
top = 5.dp,
|
||||||
bottom = 10.dp,
|
bottom = 10.dp,
|
||||||
).fillMaxWidth().padding(end = 16.dp.scaledWidth()),
|
).fillMaxWidth().padding(end = 16.dp),
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
keyboardOptions =
|
keyboardOptions =
|
||||||
KeyboardOptions(
|
KeyboardOptions(
|
||||||
+196
@@ -0,0 +1,196 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.Code
|
||||||
|
import androidx.compose.material.icons.outlined.Filter1
|
||||||
|
import androidx.compose.material.icons.outlined.Security
|
||||||
|
import androidx.compose.material.icons.outlined.VpnKeyOff
|
||||||
|
import androidx.compose.material.icons.outlined.Wifi
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.derivedStateOf
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.networkmonitor.NetworkStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LearnMoreLinkLabel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun WifiTunnelingItems(
|
||||||
|
uiState: AppUiState,
|
||||||
|
viewModel: AppViewModel,
|
||||||
|
currentText: String,
|
||||||
|
onTextChange: (String) -> Unit,
|
||||||
|
isWifiNameReadable: () -> Boolean,
|
||||||
|
): List<SelectionItem> {
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
val baseItems = listOf(
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.Wifi,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.tunnel_on_wifi),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled,
|
||||||
|
checked = uiState.appSettings.isTunnelOnWifiEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnWifi) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
val wifiName by remember(uiState.networkStatus) {
|
||||||
|
derivedStateOf {
|
||||||
|
(uiState.networkStatus as? NetworkStatus.Connected)
|
||||||
|
?.takeIf { it.wifiConnected }
|
||||||
|
?.wifiSsid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Text(
|
||||||
|
text = wifiName?.let { stringResource(R.string.wifi_name_template, it) } ?: stringResource(R.string.inactive),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.outline),
|
||||||
|
maxLines = 1,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnWifi) },
|
||||||
|
),
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.Code,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.wifi_name_via_shell),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.use_root_shell_for_wifi),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
checked = uiState.appSettings.isWifiNameByShellEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleRootShellWifi) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleRootShellWifi) },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return if (uiState.appSettings.isTunnelOnWifiEnabled) {
|
||||||
|
baseItems + listOf(
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.Filter1,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.use_wildcards),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
LearnMoreLinkLabel({ context.openWebUrl(it) }, stringResource(R.string.docs_wildcards))
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
checked = uiState.appSettings.isWildcardsEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelWildcards) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelWildcards) },
|
||||||
|
),
|
||||||
|
SelectionItem(
|
||||||
|
title = {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.weight(4f, false).fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
val icon = Icons.Outlined.Security
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
)
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(start = 16.dp)
|
||||||
|
.padding(vertical = 6.dp),
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.trusted_wifi_names),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
TrustedNetworkTextBox(
|
||||||
|
uiState.appSettings.trustedNetworkSSIDs,
|
||||||
|
onDelete = { viewModel.handleEvent(AppEvent.DeleteTrustedSSID(it)) },
|
||||||
|
currentText = currentText,
|
||||||
|
onSave = { ssid ->
|
||||||
|
if (uiState.appSettings.isWifiNameByShellEnabled || isWifiNameReadable()) {
|
||||||
|
viewModel.handleEvent(AppEvent.SaveTrustedSSID(ssid))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onValueChange = onTextChange,
|
||||||
|
supporting = { if (uiState.appSettings.isWildcardsEnabled) WildcardsLabel() },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.VpnKeyOff,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.kill_switch_off),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
enabled = uiState.appSettings.isVpnKillSwitchEnabled,
|
||||||
|
checked = uiState.appSettings.isDisableKillSwitchOnTrustedEnabled,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleStopKillSwitchOnTrusted) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleStopKillSwitchOnTrusted) },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
baseItems
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components
|
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
|
||||||
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
-808
@@ -1,808 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
|
||||||
|
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.focusGroup
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.imePadding
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.layout.width
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.outlined.Save
|
|
||||||
import androidx.compose.material.icons.rounded.ContentCopy
|
|
||||||
import androidx.compose.material.icons.rounded.Delete
|
|
||||||
import androidx.compose.material.icons.rounded.MoreVert
|
|
||||||
import androidx.compose.material.icons.rounded.Refresh
|
|
||||||
import androidx.compose.material3.DropdownMenu
|
|
||||||
import androidx.compose.material3.DropdownMenuItem
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.runtime.toMutableStateList
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.shadow
|
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.focus.focusRequester
|
|
||||||
import androidx.compose.ui.platform.ClipboardManager
|
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.AnnotatedString
|
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
|
||||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
||||||
import androidx.compose.ui.text.input.VisualTransformation
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.enums.InterfaceActions
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.enums.PeerActions
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
import org.amnezia.awg.crypto.KeyPair
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ConfigScreen(tunnelConf: TunnelConf?, appViewModel: AppViewModel) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
var isInterfaceDropDownExpanded by remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
val popBackStack by appViewModel.popBackStack.collectAsStateWithLifecycle(false)
|
|
||||||
|
|
||||||
val configPair = Pair(tunnelConf?.tunName ?: "", tunnelConf?.toAmConfig())
|
|
||||||
|
|
||||||
var tunnelName by remember {
|
|
||||||
mutableStateOf(configPair.first)
|
|
||||||
}
|
|
||||||
|
|
||||||
var interfaceState by remember {
|
|
||||||
mutableStateOf(configPair.second?.let { InterfaceProxy.from(it.`interface`) } ?: InterfaceProxy())
|
|
||||||
}
|
|
||||||
|
|
||||||
var showAmneziaValues by remember {
|
|
||||||
mutableStateOf(configPair.second?.`interface`?.junkPacketCount?.isPresent == true)
|
|
||||||
}
|
|
||||||
|
|
||||||
var showScripts by remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
val peersState = remember {
|
|
||||||
(configPair.second?.peers?.map { PeerProxy.from(it) } ?: listOf(PeerProxy())).toMutableStateList()
|
|
||||||
}
|
|
||||||
|
|
||||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
|
||||||
var isAuthenticated by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
|
||||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
|
||||||
|
|
||||||
if (showAuthPrompt) {
|
|
||||||
AuthorizationPrompt(
|
|
||||||
onSuccess = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
isAuthenticated = true
|
|
||||||
},
|
|
||||||
onError = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.error_authentication_failed),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onFailure = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.error_authorization_failed),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(popBackStack) {
|
|
||||||
if (popBackStack) navController.popBackStack()
|
|
||||||
}
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(stringResource(R.string.edit_tunnel), trailing = {
|
|
||||||
IconButton(onClick = {
|
|
||||||
tunnelConf?.let {
|
|
||||||
appViewModel.updateExistingTunnelConfig(
|
|
||||||
it,
|
|
||||||
tunnelName,
|
|
||||||
peersState,
|
|
||||||
interfaceState,
|
|
||||||
)
|
|
||||||
} ?: appViewModel.saveNewTunnel(tunnelName, peersState, interfaceState)
|
|
||||||
}) {
|
|
||||||
val icon = Icons.Outlined.Save
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
) { padding ->
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(padding)
|
|
||||||
.imePadding()
|
|
||||||
.verticalScroll(rememberScrollState())
|
|
||||||
.padding(top = 24.dp.scaledHeight())
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth()),
|
|
||||||
) {
|
|
||||||
Surface(
|
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
color = MaterialTheme.colorScheme.surface,
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(16.dp.scaledWidth())
|
|
||||||
.focusGroup(),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
GroupLabel(
|
|
||||||
stringResource(R.string.interface_),
|
|
||||||
)
|
|
||||||
Column {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
onClick = {
|
|
||||||
isInterfaceDropDownExpanded = true
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.MoreVert
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
DropdownMenu(
|
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
|
||||||
expanded = isInterfaceDropDownExpanded,
|
|
||||||
modifier = Modifier.shadow(12.dp).background(MaterialTheme.colorScheme.surface),
|
|
||||||
onDismissRequest = {
|
|
||||||
isInterfaceDropDownExpanded = false
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val isAmneziaCompatibilitySet = interfaceState.isAmneziaCompatibilityModeSet()
|
|
||||||
InterfaceActions.entries.forEach { action ->
|
|
||||||
DropdownMenuItem(
|
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
text = when (action) {
|
|
||||||
InterfaceActions.TOGGLE_SHOW_SCRIPTS -> if (showScripts) {
|
|
||||||
stringResource(R.string.hide_scripts)
|
|
||||||
} else {
|
|
||||||
stringResource(R.string.show_scripts)
|
|
||||||
}
|
|
||||||
InterfaceActions.TOGGLE_AMNEZIA_VALUES -> if (showAmneziaValues) {
|
|
||||||
stringResource(R.string.hide_amnezia_properties)
|
|
||||||
} else {
|
|
||||||
stringResource(R.string.show_amnezia_properties)
|
|
||||||
}
|
|
||||||
|
|
||||||
InterfaceActions.SET_AMNEZIA_COMPATIBILITY -> if (isAmneziaCompatibilitySet) {
|
|
||||||
stringResource(R.string.remove_amnezia_compatibility)
|
|
||||||
} else {
|
|
||||||
stringResource(R.string.enable_amnezia_compatibility)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
isInterfaceDropDownExpanded = false
|
|
||||||
when (action) {
|
|
||||||
InterfaceActions.TOGGLE_AMNEZIA_VALUES -> showAmneziaValues = !showAmneziaValues
|
|
||||||
InterfaceActions.TOGGLE_SHOW_SCRIPTS -> showScripts = !showScripts
|
|
||||||
InterfaceActions.SET_AMNEZIA_COMPATIBILITY -> if (isAmneziaCompatibilitySet) {
|
|
||||||
showAmneziaValues = false
|
|
||||||
interfaceState = interfaceState.resetAmneziaProperties()
|
|
||||||
} else {
|
|
||||||
showAmneziaValues = true
|
|
||||||
interfaceState = interfaceState.toAmneziaCompatibilityConfig()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = tunnelName,
|
|
||||||
onValueChange = { tunnelName = it },
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.name),
|
|
||||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
val privateKeyEnabled = (tunnelConf == null) || isAuthenticated
|
|
||||||
OutlinedTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable { showAuthPrompt = true },
|
|
||||||
value = interfaceState.privateKey,
|
|
||||||
visualTransformation =
|
|
||||||
if (privateKeyEnabled) {
|
|
||||||
VisualTransformation.None
|
|
||||||
} else {
|
|
||||||
PasswordVisualTransformation()
|
|
||||||
},
|
|
||||||
enabled = privateKeyEnabled,
|
|
||||||
onValueChange = { interfaceState = interfaceState.copy(privateKey = it) },
|
|
||||||
trailingIcon = {
|
|
||||||
IconButton(
|
|
||||||
enabled = privateKeyEnabled,
|
|
||||||
modifier = Modifier.focusRequester(FocusRequester.Default).size(iconSize),
|
|
||||||
onClick = {
|
|
||||||
val keypair = KeyPair()
|
|
||||||
interfaceState = interfaceState.copy(
|
|
||||||
privateKey = keypair.privateKey.toBase64(),
|
|
||||||
publicKey = keypair.publicKey.toBase64(),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.Refresh,
|
|
||||||
stringResource(R.string.rotate_keys),
|
|
||||||
tint = if (privateKeyEnabled) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.outline,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.private_key),
|
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.base64_key),
|
|
||||||
style = MaterialTheme.typography.labelLarge,
|
|
||||||
color = MaterialTheme.colorScheme.outline,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.focusRequester(FocusRequester.Default),
|
|
||||||
value = interfaceState.publicKey,
|
|
||||||
enabled = false,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(publicKey = it)
|
|
||||||
},
|
|
||||||
trailingIcon = {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.focusRequester(FocusRequester.Default).size(iconSize),
|
|
||||||
onClick = {
|
|
||||||
clipboardManager.setText(
|
|
||||||
AnnotatedString(interfaceState.publicKey),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.ContentCopy,
|
|
||||||
stringResource(R.string.copy_public_key),
|
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.public_key),
|
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.base64_key),
|
|
||||||
style = MaterialTheme.typography.labelLarge,
|
|
||||||
color = MaterialTheme.colorScheme.outline,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.addresses,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(addresses = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.addresses),
|
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(end = 5.dp),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.listenPort,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(listenPort = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.listen_port),
|
|
||||||
hint = stringResource(R.string.random),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
Row(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
|
||||||
) {
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.dnsServers,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(dnsServers = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.dns_servers),
|
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(3 / 5f)
|
|
||||||
.padding(end = 5.dp),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.mtu,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(mtu = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.mtu),
|
|
||||||
hint = stringResource(R.string.auto),
|
|
||||||
modifier = Modifier.width(IntrinsicSize.Min),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (showScripts) {
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.preUp,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(preUp = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.pre_up),
|
|
||||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.postUp,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(postUp = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.post_up),
|
|
||||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.preDown,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(preDown = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.pre_down),
|
|
||||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.postDown,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(postDown = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.post_down),
|
|
||||||
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (showAmneziaValues) {
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.junkPacketCount,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(junkPacketCount = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_count),
|
|
||||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.junkPacketMinSize,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(junkPacketMinSize = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_minimum_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.junk_packet_minimum_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.junkPacketMaxSize,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(junkPacketMaxSize = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_maximum_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.junk_packet_maximum_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.initPacketJunkSize,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(initPacketJunkSize = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.init_packet_junk_size),
|
|
||||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.responsePacketJunkSize,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(responsePacketJunkSize = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.response_packet_junk_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.response_packet_junk_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.initPacketMagicHeader,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(initPacketMagicHeader = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.init_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.init_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.responsePacketMagicHeader,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(responsePacketMagicHeader = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.response_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.response_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.underloadPacketMagicHeader,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(underloadPacketMagicHeader = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.underload_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.underload_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = interfaceState.transportPacketMagicHeader,
|
|
||||||
onValueChange = {
|
|
||||||
interfaceState = interfaceState.copy(transportPacketMagicHeader = it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.transport_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.transport_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
peersState.forEachIndexed { index, peer ->
|
|
||||||
var isPeerDropDownExpanded by remember {
|
|
||||||
mutableStateOf(false)
|
|
||||||
}
|
|
||||||
val isLanExcluded = peer.isLanExcluded()
|
|
||||||
Surface(
|
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
color = MaterialTheme.colorScheme.surface,
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.Top),
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(16.dp.scaledWidth())
|
|
||||||
.focusGroup(),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
GroupLabel(
|
|
||||||
stringResource(R.string.peer),
|
|
||||||
)
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.End),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
) {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
onClick = {
|
|
||||||
// TODO make a dialog to confirm this
|
|
||||||
peersState.removeAt(index)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
Column {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
onClick = {
|
|
||||||
isPeerDropDownExpanded = true
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.MoreVert
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
DropdownMenu(
|
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
|
||||||
expanded = isPeerDropDownExpanded,
|
|
||||||
modifier = Modifier.shadow(12.dp).background(MaterialTheme.colorScheme.surface),
|
|
||||||
onDismissRequest = {
|
|
||||||
isPeerDropDownExpanded = false
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
PeerActions.entries.forEach { action ->
|
|
||||||
DropdownMenuItem(
|
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
text = when (action) {
|
|
||||||
PeerActions.EXCLUDE_LAN -> if (isLanExcluded) {
|
|
||||||
stringResource(R.string.include_lan)
|
|
||||||
} else {
|
|
||||||
stringResource(R.string.exclude_lan)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
isPeerDropDownExpanded = false
|
|
||||||
when (action) {
|
|
||||||
PeerActions.EXCLUDE_LAN -> if (isLanExcluded) {
|
|
||||||
peersState[index] = peer.includeLan()
|
|
||||||
} else {
|
|
||||||
peersState[index] = peer.excludeLan()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = peer.publicKey,
|
|
||||||
onValueChange = { value ->
|
|
||||||
peersState[index] = peersState[index].copy(publicKey = value)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.public_key),
|
|
||||||
hint = stringResource(R.string.base64_key),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
val presharedKeyEnabled = (tunnelConf == null) || isAuthenticated ||
|
|
||||||
with(configPair.second?.peers[index]?.preSharedKey) { this?.isEmpty == true || this?.isPresent == false }
|
|
||||||
OutlinedTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable { showAuthPrompt = true },
|
|
||||||
value = peer.preSharedKey,
|
|
||||||
visualTransformation =
|
|
||||||
if (presharedKeyEnabled) {
|
|
||||||
VisualTransformation.None
|
|
||||||
} else {
|
|
||||||
PasswordVisualTransformation()
|
|
||||||
},
|
|
||||||
enabled = presharedKeyEnabled,
|
|
||||||
onValueChange = { value -> peersState[index] = peersState[index].copy(preSharedKey = value) },
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.preshared_key),
|
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.optional),
|
|
||||||
style = MaterialTheme.typography.labelLarge,
|
|
||||||
color = MaterialTheme.colorScheme.outline,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = peer.persistentKeepalive,
|
|
||||||
enabled = true,
|
|
||||||
onValueChange = { value ->
|
|
||||||
peersState[index] = peersState[index].copy(persistentKeepalive = value)
|
|
||||||
},
|
|
||||||
trailingIcon = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.seconds),
|
|
||||||
modifier = Modifier.padding(end = 10.dp),
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(stringResource(R.string.persistent_keepalive), color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.labelMedium)
|
|
||||||
},
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(stringResource(R.string.optional_no_recommend), style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.outline)
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = peer.endpoint,
|
|
||||||
onValueChange = { value ->
|
|
||||||
peersState[index] = peersState[index].copy(endpoint = value)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.endpoint),
|
|
||||||
hint = stringResource(R.string.endpoint).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = peer.allowedIps,
|
|
||||||
enabled = true,
|
|
||||||
onValueChange = { value ->
|
|
||||||
peersState[index] = peersState[index].copy(allowedIps = value)
|
|
||||||
},
|
|
||||||
label = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.allowed_ips),
|
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(stringResource(R.string.comma_separated_list), style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.outline)
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.SpaceEvenly,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(bottom = 140.dp),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Center,
|
|
||||||
) {
|
|
||||||
TextButton(onClick = {
|
|
||||||
peersState.add(PeerProxy())
|
|
||||||
}) {
|
|
||||||
Text(stringResource(R.string.add_peer))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+60
-195
@@ -2,129 +2,63 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
|||||||
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
|
||||||
import androidx.compose.foundation.gestures.ScrollableDefaults
|
|
||||||
import androidx.compose.foundation.gestures.detectTapGestures
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
|
||||||
import androidx.compose.foundation.overscroll
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.filled.Add
|
|
||||||
import androidx.compose.material.icons.outlined.Add
|
|
||||||
import androidx.compose.material3.FabPosition
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
import androidx.compose.ui.platform.LocalClipboardManager
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.NestedScrollListener
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn.withVpnPermission
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.permission.withIgnoreBatteryOpt
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.AutoTunnelRowItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.GettingStartedLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissFab
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelRowItem
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelList
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.UrlImportDialog
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppViewState
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.MainViewModel
|
|
||||||
import java.text.Collator
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) {
|
fun MainScreen(appUiState: AppUiState, appViewState: AppViewState, viewModel: AppViewModel) {
|
||||||
val context = LocalContext.current
|
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
val clipboard = LocalClipboardManager.current
|
val clipboard = LocalClipboardManager.current
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
|
|
||||||
var showBottomSheet by remember { mutableStateOf(false) }
|
|
||||||
var isFabVisible by rememberSaveable { mutableStateOf(true) }
|
|
||||||
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
||||||
var selectedTunnel by remember { mutableStateOf<TunnelConf?>(null) }
|
var showUrlImportDialog by remember { mutableStateOf(false) }
|
||||||
val isRunningOnTv = remember { context.isRunningOnTv() }
|
|
||||||
|
|
||||||
val activeTunnels by viewModel.tunnelManager.activeTunnels.collectAsStateWithLifecycle(emptyMap())
|
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(
|
||||||
|
onNoFileExplorer = { viewModel.handleEvent(AppEvent.ShowMessage(StringValue.StringResource(R.string.error_no_file_explorer))) },
|
||||||
|
onData = { data -> viewModel.handleEvent(AppEvent.ImportTunnelFromFile(data)) },
|
||||||
|
)
|
||||||
|
|
||||||
val collator = Collator.getInstance(Locale.getDefault())
|
val requestPermissionLauncher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
||||||
|
if (!isGranted) {
|
||||||
val sortedTunnels = remember(uiState.tunnels) {
|
viewModel.handleEvent(
|
||||||
uiState.tunnels.sortedWith(compareBy(collator) { it.tunName })
|
AppEvent.ShowMessage(StringValue.StringResource(R.string.camera_permission_required)),
|
||||||
}
|
)
|
||||||
|
return@rememberLauncherForActivityResult
|
||||||
val startAutoTunnel = withVpnPermission<Unit> { viewModel.onToggleAutoTunnel() }
|
|
||||||
val startTunnel = withVpnPermission<TunnelConf> {
|
|
||||||
viewModel.onTunnelStart(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
val autoTunnelToggleBattery = withIgnoreBatteryOpt(uiState.generalState.isBatteryOptimizationDisableShown) {
|
|
||||||
if (!uiState.generalState.isBatteryOptimizationDisableShown) viewModel.setBatteryOptimizeDisableShown()
|
|
||||||
if (uiState.appSettings.isKernelEnabled) {
|
|
||||||
viewModel.onToggleAutoTunnel()
|
|
||||||
} else {
|
|
||||||
startAutoTunnel.invoke(Unit)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val nestedScrollConnection = remember {
|
|
||||||
NestedScrollListener({ isFabVisible = false }, { isFabVisible = true })
|
|
||||||
}
|
|
||||||
|
|
||||||
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.error_no_file_explorer),
|
|
||||||
)
|
|
||||||
}, onData = { data ->
|
|
||||||
viewModel.onTunnelFileSelected(data, context)
|
|
||||||
})
|
|
||||||
|
|
||||||
val requestPermissionLauncher = rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.RequestPermission(),
|
|
||||||
) { isGranted ->
|
|
||||||
if (!isGranted) return@rememberLauncherForActivityResult snackbar.showMessage("Camera permission required")
|
|
||||||
navController.navigate(Route.Scanner)
|
navController.navigate(Route.Scanner)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showDeleteTunnelAlertDialog) {
|
if (showDeleteTunnelAlertDialog && appViewState.selectedTunnel != null) {
|
||||||
InfoDialog(
|
InfoDialog(
|
||||||
onDismiss = { showDeleteTunnelAlertDialog = false },
|
onDismiss = { showDeleteTunnelAlertDialog = false },
|
||||||
onAttest = {
|
onAttest = {
|
||||||
selectedTunnel?.let { viewModel.onDelete(it) }
|
appViewState.selectedTunnel.let { viewModel.handleEvent(AppEvent.DeleteTunnel(it)) }
|
||||||
showDeleteTunnelAlertDialog = false
|
showDeleteTunnelAlertDialog = false
|
||||||
selectedTunnel = null
|
viewModel.handleEvent(AppEvent.SetSelectedTunnel(null))
|
||||||
},
|
},
|
||||||
title = { Text(text = stringResource(R.string.delete_tunnel)) },
|
title = { Text(text = stringResource(R.string.delete_tunnel)) },
|
||||||
body = { Text(text = stringResource(R.string.delete_tunnel_message)) },
|
body = { Text(text = stringResource(R.string.delete_tunnel_message)) },
|
||||||
@@ -132,115 +66,46 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConf) {
|
TunnelImportSheet(
|
||||||
if (!checked) return viewModel.onTunnelStop(tunnel).let { }
|
appViewState.showBottomSheet,
|
||||||
if (uiState.appSettings.isKernelEnabled) viewModel.onTunnelStart(tunnel) else startTunnel(tunnel)
|
onDismiss = { viewModel.handleEvent(AppEvent.ToggleBottomSheet) },
|
||||||
}
|
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
||||||
|
onQrClick = { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) },
|
||||||
|
onClipboardClick = { clipboard.getText()?.text?.let { viewModel.handleEvent(AppEvent.ImportTunnelFromClipboard(it)) } },
|
||||||
|
onManualImportClick = { navController.navigate(Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID)) },
|
||||||
|
onUrlClick = { showUrlImportDialog = true },
|
||||||
|
)
|
||||||
|
|
||||||
Scaffold(
|
if (showUrlImportDialog) {
|
||||||
modifier =
|
UrlImportDialog(
|
||||||
Modifier.pointerInput(Unit) {
|
onDismiss = { showUrlImportDialog = false },
|
||||||
if (uiState.tunnels.isEmpty()) return@pointerInput
|
onConfirm = { url ->
|
||||||
detectTapGestures(
|
viewModel.handleEvent(AppEvent.ImportTunnelFromUrl(url))
|
||||||
onTap = {
|
showUrlImportDialog = false
|
||||||
selectedTunnel = null
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
|
||||||
floatingActionButton = {
|
|
||||||
if (!isRunningOnTv) {
|
|
||||||
ScrollDismissFab({
|
|
||||||
val icon = Icons.Filled.Add
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
tint = MaterialTheme.colorScheme.onPrimary,
|
|
||||||
)
|
|
||||||
}, isVisible = isFabVisible, onClick = {
|
|
||||||
showBottomSheet = true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
topBar = {
|
|
||||||
if (isRunningOnTv) {
|
|
||||||
TopNavBar(
|
|
||||||
showBack = false,
|
|
||||||
title = stringResource(R.string.app_name),
|
|
||||||
trailing = {
|
|
||||||
IconButton(onClick = {
|
|
||||||
showBottomSheet = true
|
|
||||||
}) {
|
|
||||||
val icon = Icons.Outlined.Add
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) { padding ->
|
|
||||||
TunnelImportSheet(
|
|
||||||
showBottomSheet,
|
|
||||||
onDismiss = { showBottomSheet = false },
|
|
||||||
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
|
||||||
onQrClick = { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) },
|
|
||||||
onClipboardClick = {
|
|
||||||
clipboard.getText()?.text?.let {
|
|
||||||
viewModel.onClipboardImport(it)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onManualImportClick = {
|
|
||||||
navController.navigate(
|
|
||||||
Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID),
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
LazyColumn(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize().padding(padding).padding(top = 24.dp.scaledHeight())
|
|
||||||
.overscroll(ScrollableDefaults.overscrollEffect())
|
|
||||||
.nestedScroll(nestedScrollConnection),
|
|
||||||
state = rememberLazyListState(0, uiState.tunnels.count()),
|
|
||||||
userScrollEnabled = true,
|
|
||||||
reverseLayout = false,
|
|
||||||
flingBehavior = ScrollableDefaults.flingBehavior(),
|
|
||||||
) {
|
|
||||||
if (uiState.tunnels.isEmpty()) {
|
|
||||||
item {
|
|
||||||
GettingStartedLabel(onClick = { context.openWebUrl(it) })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
item {
|
|
||||||
AutoTunnelRowItem(uiState) {
|
|
||||||
autoTunnelToggleBattery.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
items(
|
|
||||||
sortedTunnels,
|
|
||||||
key = { tunnel -> tunnel.id },
|
|
||||||
) { tunnel ->
|
|
||||||
val expanded = uiState.generalState.isTunnelStatsExpanded
|
|
||||||
val tunnelState = activeTunnels.getOrDefault(tunnel.id, TunnelState())
|
|
||||||
TunnelRowItem(
|
|
||||||
tunnelState.state.isUp(),
|
|
||||||
expanded,
|
|
||||||
selectedTunnel?.id == tunnel.id,
|
|
||||||
tunnel,
|
|
||||||
tunnelState = tunnelState,
|
|
||||||
{ selectedTunnel = tunnel },
|
|
||||||
{ viewModel.onExpandedChanged(!expanded) },
|
|
||||||
onDelete = { showDeleteTunnelAlertDialog = true },
|
|
||||||
onCopy = { viewModel.onCopyTunnel(tunnel) },
|
|
||||||
onSwitchClick = { onTunnelToggle(it, tunnel) },
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TunnelList(
|
||||||
|
appUiState = appUiState,
|
||||||
|
activeTunnels = appUiState.activeTunnels,
|
||||||
|
selectedTunnel = appViewState.selectedTunnel,
|
||||||
|
onSetSelectedTunnel = { viewModel.handleEvent(AppEvent.SetSelectedTunnel(it)) },
|
||||||
|
onDeleteTunnel = {
|
||||||
|
viewModel.handleEvent(AppEvent.SetSelectedTunnel(it))
|
||||||
|
showDeleteTunnelAlertDialog = true
|
||||||
|
},
|
||||||
|
onToggleTunnel = { tunnel, checked ->
|
||||||
|
if (checked) viewModel.handleEvent(AppEvent.StartTunnel(tunnel)) else viewModel.handleEvent(AppEvent.StopTunnel(tunnel))
|
||||||
|
},
|
||||||
|
onExpandStats = { viewModel.handleEvent(AppEvent.ToggleTunnelStatsExpanded) },
|
||||||
|
onCopyTunnel = {
|
||||||
|
viewModel.handleEvent(AppEvent.CopyTunnel(it))
|
||||||
|
viewModel.handleEvent(AppEvent.SetSelectedTunnel(null))
|
||||||
|
},
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(top = 12.dp, bottom = 24.dp).padding(horizontal = 12.dp),
|
||||||
|
viewModel = viewModel,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
-269
@@ -1,269 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
|
||||||
|
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.height
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.outlined.Check
|
|
||||||
import androidx.compose.material.icons.outlined.Save
|
|
||||||
import androidx.compose.material.icons.outlined.Search
|
|
||||||
import androidx.compose.material3.Checkbox
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.MultiChoiceSegmentedButtonRow
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.SegmentedButton
|
|
||||||
import androidx.compose.material3.SegmentedButtonDefaults
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.derivedStateOf
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.runtime.toMutableStateList
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.SelectionItemButton
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.enums.SplitOptions
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
import java.text.Collator
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SplitTunnelScreen(tunnelConf: TunnelConf, viewModel: AppViewModel) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
val inputHeight = 45.dp
|
|
||||||
|
|
||||||
val collator = Collator.getInstance(Locale.getDefault())
|
|
||||||
|
|
||||||
val popBackStack by viewModel.popBackStack.collectAsStateWithLifecycle(false)
|
|
||||||
|
|
||||||
LaunchedEffect(popBackStack) {
|
|
||||||
if (popBackStack) navController.popBackStack()
|
|
||||||
}
|
|
||||||
|
|
||||||
val splitTunnelApps by viewModel.splitTunnelApps.collectAsStateWithLifecycle()
|
|
||||||
|
|
||||||
var proxyInterface by remember { mutableStateOf(InterfaceProxy()) }
|
|
||||||
|
|
||||||
var selectedSplitOption by remember { mutableStateOf(SplitOptions.ALL) }
|
|
||||||
|
|
||||||
val selectedPackages = remember { mutableStateListOf<String>() }
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
proxyInterface = InterfaceProxy.from(tunnelConf.toAmConfig().`interface`)
|
|
||||||
val pair = when {
|
|
||||||
proxyInterface.excludedApplications.isNotEmpty() -> Pair(SplitOptions.EXCLUDE, proxyInterface.excludedApplications)
|
|
||||||
proxyInterface.includedApplications.isNotEmpty() -> Pair(SplitOptions.INCLUDE, proxyInterface.includedApplications)
|
|
||||||
else -> Pair(SplitOptions.ALL, mutableSetOf())
|
|
||||||
}
|
|
||||||
selectedSplitOption = pair.first
|
|
||||||
selectedPackages.addAll(pair.second)
|
|
||||||
}
|
|
||||||
|
|
||||||
var query: String by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
val sortedPackages by remember {
|
|
||||||
derivedStateOf {
|
|
||||||
splitTunnelApps.sortedWith(compareBy(collator) { it.name }).filter { it.name.lowercase().contains(query.lowercase()) }.toMutableStateList()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
// clean up any split tunnel packages for apps that were uninstalled
|
|
||||||
viewModel.cleanUpUninstalledApps(tunnelConf, splitTunnelApps.map { it.`package` })
|
|
||||||
}
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(stringResource(R.string.tunneling_apps), trailing = {
|
|
||||||
IconButton(onClick = {
|
|
||||||
proxyInterface.apply {
|
|
||||||
includedApplications.clear()
|
|
||||||
excludedApplications.clear()
|
|
||||||
}
|
|
||||||
when (selectedSplitOption) {
|
|
||||||
SplitOptions.INCLUDE -> proxyInterface.includedApplications.apply {
|
|
||||||
addAll(selectedPackages)
|
|
||||||
}
|
|
||||||
SplitOptions.EXCLUDE -> proxyInterface.excludedApplications.apply {
|
|
||||||
addAll(selectedPackages)
|
|
||||||
}
|
|
||||||
SplitOptions.ALL -> Unit
|
|
||||||
}
|
|
||||||
viewModel.updateExistingTunnelConfig(tunnelConf, `interface` = proxyInterface)
|
|
||||||
}) {
|
|
||||||
val icon = Icons.Outlined.Save
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
) { padding ->
|
|
||||||
|
|
||||||
Column(
|
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.CenterVertically),
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(padding)
|
|
||||||
.padding(top = 24.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
MultiChoiceSegmentedButtonRow(
|
|
||||||
modifier = Modifier.background(color = MaterialTheme.colorScheme.background).fillMaxWidth()
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth()).height(inputHeight),
|
|
||||||
) {
|
|
||||||
SplitOptions.entries.forEachIndexed { index, entry ->
|
|
||||||
val active = selectedSplitOption == entry
|
|
||||||
SegmentedButton(
|
|
||||||
shape = SegmentedButtonDefaults.itemShape(index = index, count = SplitOptions.entries.size, baseShape = RoundedCornerShape(8.dp)),
|
|
||||||
icon = {
|
|
||||||
SegmentedButtonDefaults.Icon(active = active, activeContent = {
|
|
||||||
val icon = Icons.Outlined.Check
|
|
||||||
Icon(imageVector = icon, icon.name, tint = MaterialTheme.colorScheme.primary, modifier = Modifier.size(SegmentedButtonDefaults.IconSize))
|
|
||||||
}) {
|
|
||||||
Icon(
|
|
||||||
imageVector = entry.icon(),
|
|
||||||
contentDescription = entry.icon().name,
|
|
||||||
modifier = Modifier.size(SegmentedButtonDefaults.IconSize),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
colors = SegmentedButtonDefaults.colors().copy(
|
|
||||||
activeContainerColor = MaterialTheme.colorScheme.surface,
|
|
||||||
inactiveContainerColor = MaterialTheme.colorScheme.background,
|
|
||||||
),
|
|
||||||
onCheckedChange = {
|
|
||||||
selectedSplitOption = entry
|
|
||||||
},
|
|
||||||
checked = active,
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
entry.text().asString(context)
|
|
||||||
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() },
|
|
||||||
color = MaterialTheme.colorScheme.onBackground,
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (selectedSplitOption != SplitOptions.ALL) {
|
|
||||||
Column(
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
CustomTextField(
|
|
||||||
textStyle = MaterialTheme.typography.labelMedium.copy(
|
|
||||||
color = MaterialTheme.colorScheme.onBackground,
|
|
||||||
),
|
|
||||||
value = query,
|
|
||||||
onValueChange = { input ->
|
|
||||||
query = input
|
|
||||||
},
|
|
||||||
interactionSource = remember { MutableInteractionSource() },
|
|
||||||
label = {},
|
|
||||||
leading = {
|
|
||||||
val icon = Icons.Outlined.Search
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
},
|
|
||||||
containerColor = MaterialTheme.colorScheme.background,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth().height(inputHeight).padding(horizontal = 24.dp.scaledWidth()),
|
|
||||||
singleLine = true,
|
|
||||||
keyboardOptions =
|
|
||||||
KeyboardOptions(
|
|
||||||
capitalization = KeyboardCapitalization.None,
|
|
||||||
imeAction = ImeAction.Done,
|
|
||||||
),
|
|
||||||
keyboardActions = KeyboardActions(),
|
|
||||||
)
|
|
||||||
LazyColumn(
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
contentPadding = PaddingValues(top = 10.dp),
|
|
||||||
) {
|
|
||||||
items(sortedPackages, key = { it.`package` }) { app ->
|
|
||||||
val checked = selectedPackages.contains(app.`package`)
|
|
||||||
val onClick = {
|
|
||||||
if (checked) selectedPackages.remove(app.`package`) else selectedPackages.add(app.`package`)
|
|
||||||
}
|
|
||||||
SelectionItemButton(
|
|
||||||
{
|
|
||||||
Image(
|
|
||||||
rememberDrawablePainter(app.icon),
|
|
||||||
app.name,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth())
|
|
||||||
.size(
|
|
||||||
iconSize,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
buttonText = app.name,
|
|
||||||
onClick = {
|
|
||||||
onClick()
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
horizontalArrangement = Arrangement.End,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
) {
|
|
||||||
Checkbox(
|
|
||||||
checked = checked,
|
|
||||||
onCheckedChange = {
|
|
||||||
onClick()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-175
@@ -1,175 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.outlined.PhoneAndroid
|
|
||||||
import androidx.compose.material.icons.outlined.Security
|
|
||||||
import androidx.compose.material.icons.outlined.SettingsEthernet
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.TrustedNetworkTextBox
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.WildcardsLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelAutoTunnelViewModel
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun TunnelAutoTunnelScreen(tunnelConf: TunnelConf, appSettings: AppSettings, tunnelAutoTunnelViewModel: TunnelAutoTunnelViewModel = hiltViewModel()) {
|
|
||||||
var currentText by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
LaunchedEffect(tunnelConf.tunnelNetworks) {
|
|
||||||
currentText = ""
|
|
||||||
}
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(tunnelConf.tunName)
|
|
||||||
},
|
|
||||||
) { padding ->
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(padding)
|
|
||||||
.verticalScroll(rememberScrollState())
|
|
||||||
.padding(top = 24.dp.scaledHeight())
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth()),
|
|
||||||
) {
|
|
||||||
SurfaceSelectionGroupButton(
|
|
||||||
buildList {
|
|
||||||
addAll(
|
|
||||||
listOf(
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.PhoneAndroid,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.mobile_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.mobile_data_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
tunnelConf.isMobileDataTunnel,
|
|
||||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsMobileDataTunnel(tunnelConf) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsMobileDataTunnel(tunnelConf) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.SettingsEthernet,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.ethernet_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.set_ethernet_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
tunnelConf.isEthernetTunnel,
|
|
||||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsEthernetTunnel(tunnelConf) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { tunnelAutoTunnelViewModel.onToggleIsEthernetTunnel(tunnelConf) },
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
add(
|
|
||||||
SelectionItem(
|
|
||||||
title = {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(vertical = 4.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier
|
|
||||||
.weight(4f, false)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
val icon = Icons.Outlined.Security
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
)
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(start = 16.dp.scaledWidth())
|
|
||||||
.padding(vertical = 6.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.use_tunnel_on_wifi_name),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
TrustedNetworkTextBox(
|
|
||||||
tunnelConf.tunnelNetworks,
|
|
||||||
onDelete = { tunnelAutoTunnelViewModel.onDeleteRunSSID(it, tunnelConf) },
|
|
||||||
currentText = currentText,
|
|
||||||
onSave = { tunnelAutoTunnelViewModel.onSaveRunSSID(it, tunnelConf) },
|
|
||||||
onValueChange = { currentText = it },
|
|
||||||
supporting = {
|
|
||||||
if (appSettings.isWildcardsEnabled) {
|
|
||||||
WildcardsLabel()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-255
@@ -1,255 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.imePadding
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.automirrored.outlined.CallSplit
|
|
||||||
import androidx.compose.material.icons.outlined.Bolt
|
|
||||||
import androidx.compose.material.icons.outlined.Dns
|
|
||||||
import androidx.compose.material.icons.outlined.Edit
|
|
||||||
import androidx.compose.material.icons.outlined.NetworkPing
|
|
||||||
import androidx.compose.material.icons.outlined.Star
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
|
||||||
import androidx.compose.ui.text.input.KeyboardType
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.SubmitConfigurationTextBox
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isValidIpv4orIpv6Address
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.TunnelOptionsViewModel
|
|
||||||
import kotlin.text.isBlank
|
|
||||||
import kotlin.text.isNullOrBlank
|
|
||||||
import kotlin.text.toLong
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun OptionsScreen(tunnelConf: TunnelConf, appUiState: AppUiState, viewModel: TunnelOptionsViewModel = hiltViewModel()) {
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
var currentText by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
LaunchedEffect(tunnelConf.tunnelNetworks) {
|
|
||||||
currentText = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
val onPingToggle = {
|
|
||||||
viewModel.saveTunnel(tunnelConf.copy(isPingEnabled = !tunnelConf.isPingEnabled))
|
|
||||||
}
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(tunnelConf.tunName)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(it)
|
|
||||||
.imePadding()
|
|
||||||
.verticalScroll(rememberScrollState())
|
|
||||||
.padding(top = 24.dp.scaledHeight())
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth()),
|
|
||||||
) {
|
|
||||||
SurfaceSelectionGroupButton(
|
|
||||||
listOf(
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.Star,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.primary_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.set_primary_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
tunnelConf.isPrimaryTunnel,
|
|
||||||
onClick = { viewModel.onTogglePrimaryTunnel(tunnelConf) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { viewModel.onTogglePrimaryTunnel(tunnelConf) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.Bolt,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.auto_tunneling),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.tunnel_specific_settings),
|
|
||||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
navController.navigate(Route.TunnelAutoTunnel(id = tunnelConf.id))
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ForwardButton { navController.navigate(Route.TunnelAutoTunnel(id = tunnelConf.id)) }
|
|
||||||
},
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.Edit,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.edit_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
navController.navigate(Route.Config(id = tunnelConf.id))
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ForwardButton { navController.navigate(Route.Config(id = tunnelConf.id)) }
|
|
||||||
},
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.Dns,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.server_ipv4),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.prefer_ipv4),
|
|
||||||
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
tunnelConf.isIpv4Preferred,
|
|
||||||
onClick = { viewModel.onToggleIpv4(tunnelConf) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { viewModel.onToggleIpv4(tunnelConf) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.AutoMirrored.Outlined.CallSplit,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.splt_tunneling),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
navController.navigate(Route.SplitTunnel(id = tunnelConf.id))
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ForwardButton { navController.navigate(Route.SplitTunnel(id = tunnelConf.id)) }
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
SurfaceSelectionGroupButton(
|
|
||||||
buildList {
|
|
||||||
add(
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.NetworkPing,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.restart_on_ping),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
checked = tunnelConf.isPingEnabled,
|
|
||||||
enabled = !appUiState.activeTunnels.containsKey(tunnelConf.id),
|
|
||||||
onClick = { onPingToggle() },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { onPingToggle() },
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if (tunnelConf.isPingEnabled) {
|
|
||||||
add(
|
|
||||||
SelectionItem(
|
|
||||||
title = {},
|
|
||||||
description = {
|
|
||||||
SubmitConfigurationTextBox(
|
|
||||||
tunnelConf.pingIp,
|
|
||||||
stringResource(R.string.set_custom_ping_ip),
|
|
||||||
stringResource(R.string.default_ping_ip),
|
|
||||||
isErrorValue = { !it.isNullOrBlank() && !it.isValidIpv4orIpv6Address() },
|
|
||||||
onSubmit = {
|
|
||||||
viewModel.saveTunnel(
|
|
||||||
tunnelConf.copy(pingIp = it.ifBlank { null }),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
fun isSecondsError(seconds: String?): Boolean {
|
|
||||||
return seconds?.let { value -> if (value.isBlank()) false else value.toLong() >= Long.MAX_VALUE / 1000 } == true
|
|
||||||
}
|
|
||||||
SubmitConfigurationTextBox(
|
|
||||||
tunnelConf.pingInterval?.let { (it / 1000).toString() },
|
|
||||||
stringResource(R.string.set_custom_ping_internal),
|
|
||||||
"(${stringResource(R.string.optional_default)} ${Constants.PING_INTERVAL / 1000})",
|
|
||||||
keyboardOptions = KeyboardOptions(
|
|
||||||
keyboardType = KeyboardType.Number,
|
|
||||||
imeAction = ImeAction.Done,
|
|
||||||
),
|
|
||||||
isErrorValue = ::isSecondsError,
|
|
||||||
onSubmit = {
|
|
||||||
viewModel.onPingIntervalChange(tunnelConf, it)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
SubmitConfigurationTextBox(
|
|
||||||
tunnelConf.pingCooldown?.let { (it / 1000).toString() },
|
|
||||||
stringResource(R.string.set_custom_ping_cooldown),
|
|
||||||
"(${stringResource(R.string.optional_default)} ${Constants.PING_COOLDOWN / 1000})",
|
|
||||||
keyboardOptions = KeyboardOptions(
|
|
||||||
keyboardType = KeyboardType.Number,
|
|
||||||
),
|
|
||||||
isErrorValue = ::isSecondsError,
|
|
||||||
onSubmit = { viewModel.onPingCoolDownChange(tunnelConf, it) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.EthernetTunnelItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.MobileDataTunnelItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.WifiTunnelItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TunnelAutoTunnelScreen(tunnelConf: TunnelConf, appSettings: AppSettings, viewModel: AppViewModel) {
|
||||||
|
var currentText by remember { mutableStateOf("") }
|
||||||
|
|
||||||
|
LaunchedEffect(tunnelConf.tunnelNetworks) {
|
||||||
|
currentText = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(top = 24.dp)
|
||||||
|
.padding(horizontal = 24.dp),
|
||||||
|
) {
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
items = buildList {
|
||||||
|
add(MobileDataTunnelItem(tunnelConf, viewModel))
|
||||||
|
add(EthernetTunnelItem(tunnelConf, viewModel))
|
||||||
|
add(WifiTunnelItem(tunnelConf, appSettings, viewModel, currentText) { currentText = it })
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.SettingsEthernet
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun EthernetTunnelItem(tunnelConf: TunnelConf, viewModel: AppViewModel): SelectionItem {
|
||||||
|
return SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.SettingsEthernet,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.ethernet_tunnel),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.set_ethernet_tunnel),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
checked = tunnelConf.isEthernetTunnel,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleEthernetTunnel(tunnelConf)) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleEthernetTunnel(tunnelConf)) },
|
||||||
|
)
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.PhoneAndroid
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun MobileDataTunnelItem(tunnelConf: TunnelConf, viewModel: AppViewModel): SelectionItem {
|
||||||
|
return SelectionItem(
|
||||||
|
leadingIcon = Icons.Outlined.PhoneAndroid,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.mobile_tunnel),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.mobile_data_tunnel),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
checked = tunnelConf.isMobileDataTunnel,
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleMobileDataTunnel(tunnelConf)) },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { viewModel.handleEvent(AppEvent.ToggleMobileDataTunnel(tunnelConf)) },
|
||||||
|
)
|
||||||
|
}
|
||||||
+76
@@ -0,0 +1,76 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.Security
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.TrustedNetworkTextBox
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.WildcardsLabel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun WifiTunnelItem(
|
||||||
|
tunnelConf: TunnelConf,
|
||||||
|
appSettings: AppSettings,
|
||||||
|
viewModel: AppViewModel,
|
||||||
|
currentText: String,
|
||||||
|
onTextChange: (String) -> Unit,
|
||||||
|
): SelectionItem {
|
||||||
|
return SelectionItem(
|
||||||
|
title = {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.Start,
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(vertical = 8.dp),
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Outlined.Security,
|
||||||
|
contentDescription = stringResource(R.string.use_tunnel_on_wifi_name),
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.use_tunnel_on_wifi_name),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(start = 16.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
description = {
|
||||||
|
TrustedNetworkTextBox(
|
||||||
|
trustedNetworks = tunnelConf.tunnelNetworks,
|
||||||
|
onDelete = { viewModel.handleEvent(AppEvent.DeleteTunnelRunSSID(it, tunnelConf)) },
|
||||||
|
currentText = currentText,
|
||||||
|
onSave = {
|
||||||
|
viewModel.handleEvent(AppEvent.AddTunnelRunSSID(it, tunnelConf))
|
||||||
|
onTextChange("") // Reset the text field after saving
|
||||||
|
},
|
||||||
|
onValueChange = onTextChange,
|
||||||
|
supporting = {
|
||||||
|
if (appSettings.isWildcardsEnabled) {
|
||||||
|
WildcardsLabel()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
-57
@@ -1,57 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Bolt
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.scale
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
ExpandingRowListItem(
|
|
||||||
leading = {
|
|
||||||
val icon = Icons.Rounded.Bolt
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.size(16.dp).scale(1.5f),
|
|
||||||
tint =
|
|
||||||
if (!appUiState.autoTunnelActive) {
|
|
||||||
Color.Gray
|
|
||||||
} else {
|
|
||||||
SilverTree
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
text = stringResource(R.string.auto_tunneling),
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
appUiState.appSettings.isAutoTunnelEnabled,
|
|
||||||
onClick = {
|
|
||||||
onToggle()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
onToggle()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
isExpanded = false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
+25
@@ -8,10 +8,12 @@ import androidx.compose.material.icons.Icons
|
|||||||
import androidx.compose.material.icons.filled.ContentPasteGo
|
import androidx.compose.material.icons.filled.ContentPasteGo
|
||||||
import androidx.compose.material.icons.filled.Create
|
import androidx.compose.material.icons.filled.Create
|
||||||
import androidx.compose.material.icons.filled.FileOpen
|
import androidx.compose.material.icons.filled.FileOpen
|
||||||
|
import androidx.compose.material.icons.filled.Link
|
||||||
import androidx.compose.material.icons.filled.QrCode
|
import androidx.compose.material.icons.filled.QrCode
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.HorizontalDivider
|
import androidx.compose.material3.HorizontalDivider
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.ModalBottomSheet
|
import androidx.compose.material3.ModalBottomSheet
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.rememberModalBottomSheetState
|
import androidx.compose.material3.rememberModalBottomSheetState
|
||||||
@@ -33,12 +35,14 @@ fun TunnelImportSheet(
|
|||||||
onQrClick: () -> Unit,
|
onQrClick: () -> Unit,
|
||||||
onManualImportClick: () -> Unit,
|
onManualImportClick: () -> Unit,
|
||||||
onClipboardClick: () -> Unit,
|
onClipboardClick: () -> Unit,
|
||||||
|
onUrlClick: () -> Unit,
|
||||||
) {
|
) {
|
||||||
val sheetState = rememberModalBottomSheetState()
|
val sheetState = rememberModalBottomSheetState()
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
if (show) {
|
if (show) {
|
||||||
ModalBottomSheet(
|
ModalBottomSheet(
|
||||||
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
onDismissRequest = {
|
onDismissRequest = {
|
||||||
onDismiss()
|
onDismiss()
|
||||||
},
|
},
|
||||||
@@ -110,6 +114,27 @@ fun TunnelImportSheet(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
HorizontalDivider()
|
HorizontalDivider()
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable {
|
||||||
|
onDismiss()
|
||||||
|
onUrlClick()
|
||||||
|
}
|
||||||
|
.padding(10.dp),
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.Link,
|
||||||
|
contentDescription = stringResource(id = R.string.add_from_url),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.add_from_url),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
HorizontalDivider()
|
||||||
Row(
|
Row(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
|
|||||||
+80
@@ -0,0 +1,80 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
|
import androidx.compose.foundation.gestures.ScrollableDefaults
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
|
import androidx.compose.foundation.overscroll
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.core.tunnel.getValueById
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import java.text.Collator
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
|
@Composable
|
||||||
|
fun TunnelList(
|
||||||
|
appUiState: AppUiState,
|
||||||
|
activeTunnels: Map<TunnelConf, TunnelState>,
|
||||||
|
selectedTunnel: TunnelConf?,
|
||||||
|
onSetSelectedTunnel: (TunnelConf?) -> Unit,
|
||||||
|
onDeleteTunnel: (TunnelConf) -> Unit,
|
||||||
|
onToggleTunnel: (TunnelConf, Boolean) -> Unit,
|
||||||
|
onExpandStats: () -> Unit,
|
||||||
|
onCopyTunnel: (TunnelConf) -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
viewModel: AppViewModel,
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val collator = Collator.getInstance(Locale.getDefault())
|
||||||
|
val sortedTunnels = remember(appUiState.tunnels) {
|
||||||
|
appUiState.tunnels.sortedWith(compareBy(collator) { it.tunName })
|
||||||
|
}
|
||||||
|
|
||||||
|
LazyColumn(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
||||||
|
modifier = modifier
|
||||||
|
.pointerInput(Unit) {
|
||||||
|
if (appUiState.tunnels.isEmpty()) return@pointerInput
|
||||||
|
}.overscroll(ScrollableDefaults.overscrollEffect()),
|
||||||
|
state = rememberLazyListState(0, appUiState.tunnels.count()),
|
||||||
|
userScrollEnabled = true,
|
||||||
|
reverseLayout = false,
|
||||||
|
flingBehavior = ScrollableDefaults.flingBehavior(),
|
||||||
|
) {
|
||||||
|
if (appUiState.tunnels.isEmpty()) {
|
||||||
|
item {
|
||||||
|
GettingStartedLabel(onClick = { context.openWebUrl(it) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
items(sortedTunnels, key = { it.id }) { tunnel ->
|
||||||
|
val tunnelState = activeTunnels.getValueById(tunnel.id) ?: TunnelState()
|
||||||
|
TunnelRowItem(
|
||||||
|
isActive = tunnelState.status.isUpOrStarting(),
|
||||||
|
expanded = appUiState.generalState.isTunnelStatsExpanded,
|
||||||
|
isSelected = selectedTunnel?.id == tunnel.id,
|
||||||
|
tunnel = tunnel,
|
||||||
|
tunnelState = tunnelState,
|
||||||
|
onSetSelectedTunnel = { onSetSelectedTunnel(it) },
|
||||||
|
onClick = onExpandStats,
|
||||||
|
onCopy = { onCopyTunnel(tunnel) },
|
||||||
|
onDelete = { onDeleteTunnel(tunnel) },
|
||||||
|
onSwitchClick = { checked -> onToggleTunnel(tunnel, checked) },
|
||||||
|
viewModel = viewModel,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+127
-102
@@ -1,7 +1,8 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
||||||
|
|
||||||
import androidx.compose.foundation.focusable
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.rounded.Circle
|
import androidx.compose.material.icons.rounded.Circle
|
||||||
@@ -23,6 +24,7 @@ import androidx.compose.ui.graphics.Color
|
|||||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
@@ -31,9 +33,11 @@ import com.zaneschepke.wireguardautotunnel.ui.Route
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asColor
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asColor
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TunnelRowItem(
|
fun TunnelRowItem(
|
||||||
@@ -42,153 +46,174 @@ fun TunnelRowItem(
|
|||||||
isSelected: Boolean,
|
isSelected: Boolean,
|
||||||
tunnel: TunnelConf,
|
tunnel: TunnelConf,
|
||||||
tunnelState: TunnelState,
|
tunnelState: TunnelState,
|
||||||
onHold: () -> Unit,
|
onSetSelectedTunnel: (TunnelConf?) -> Unit,
|
||||||
onClick: () -> Unit,
|
onClick: () -> Unit,
|
||||||
onCopy: () -> Unit,
|
onCopy: () -> Unit,
|
||||||
onDelete: () -> Unit,
|
onDelete: () -> Unit,
|
||||||
onSwitchClick: (checked: Boolean) -> Unit,
|
onSwitchClick: (Boolean) -> Unit,
|
||||||
|
viewModel: AppViewModel,
|
||||||
) {
|
) {
|
||||||
val leadingIconColor = if (!isActive) Color.Gray else tunnelState.statistics.asColor()
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
val haptic = LocalHapticFeedback.current
|
val haptic = LocalHapticFeedback.current
|
||||||
val itemFocusRequester = remember { FocusRequester() }
|
val itemFocusRequester = remember { FocusRequester() }
|
||||||
|
val isTv = context.isRunningOnTv()
|
||||||
|
|
||||||
|
val leadingIconColor = if (!isActive) Color.Gray else tunnelState.statistics.asColor()
|
||||||
|
|
||||||
|
val (leadingIcon, size) = when {
|
||||||
|
tunnel.isPrimaryTunnel -> Pair(Icons.Rounded.Star, 16.dp)
|
||||||
|
tunnel.isMobileDataTunnel -> Pair(Icons.Rounded.Smartphone, 16.dp)
|
||||||
|
tunnel.isEthernetTunnel -> Pair(Icons.Rounded.SettingsEthernet, 16.dp)
|
||||||
|
else -> Pair(Icons.Rounded.Circle, 14.dp)
|
||||||
|
}
|
||||||
|
|
||||||
ExpandingRowListItem(
|
ExpandingRowListItem(
|
||||||
leading = {
|
leading = {
|
||||||
val icon = when {
|
|
||||||
tunnel.isPrimaryTunnel -> Icons.Rounded.Star
|
|
||||||
tunnel.isMobileDataTunnel -> Icons.Rounded.Smartphone
|
|
||||||
tunnel.isEthernetTunnel -> Icons.Rounded.SettingsEthernet
|
|
||||||
else -> Icons.Rounded.Circle
|
|
||||||
}
|
|
||||||
Icon(
|
Icon(
|
||||||
icon,
|
leadingIcon,
|
||||||
icon.name,
|
stringResource(R.string.status),
|
||||||
tint = leadingIconColor,
|
tint = leadingIconColor,
|
||||||
modifier = Modifier.size(16.dp),
|
modifier = Modifier.size(size),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
text = tunnel.tunName,
|
text = tunnel.tunName,
|
||||||
onHold = {
|
onHold = {
|
||||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||||
onHold()
|
onSetSelectedTunnel(tunnel)
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!context.isRunningOnTv()) {
|
if (!isTv) {
|
||||||
if (isActive) {
|
if (isActive) onClick()
|
||||||
onClick()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
onHold()
|
onSetSelectedTunnel(tunnel)
|
||||||
itemFocusRequester.requestFocus()
|
itemFocusRequester.requestFocus()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isExpanded = expanded && isActive,
|
isExpanded = expanded && isActive,
|
||||||
expanded = { if (isActive && expanded) TunnelStatisticsRow(tunnelState.statistics, tunnel) },
|
expanded = { if (isActive && expanded) TunnelStatisticsRow(tunnelState.statistics, tunnel) },
|
||||||
trailing = {
|
trailing = {
|
||||||
if (
|
if (isSelected && !isTv) {
|
||||||
isSelected &&
|
Row(
|
||||||
!context.isRunningOnTv()
|
modifier = Modifier.fillMaxWidth(),
|
||||||
) {
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
Row {
|
) {
|
||||||
IconButton(
|
IconButton(
|
||||||
|
modifier = Modifier.weight(1f),
|
||||||
onClick = {
|
onClick = {
|
||||||
navController.navigate(
|
onSetSelectedTunnel(null)
|
||||||
Route.TunnelOptions(tunnel.id),
|
navController.navigate(Route.TunnelOptions(tunnel.id))
|
||||||
)
|
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
val icon = Icons.Rounded.Settings
|
|
||||||
Icon(
|
Icon(
|
||||||
icon,
|
Icons.Rounded.Settings,
|
||||||
icon.name,
|
stringResource(id = R.string.settings),
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
IconButton(
|
IconButton(
|
||||||
modifier = Modifier.focusable(),
|
modifier = Modifier.weight(1f),
|
||||||
onClick = { onCopy() },
|
onClick = {
|
||||||
|
onCopy()
|
||||||
|
onSetSelectedTunnel(null)
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
val icon = Icons.Rounded.CopyAll
|
Icon(
|
||||||
Icon(icon, icon.name)
|
Icons.Rounded.CopyAll,
|
||||||
|
stringResource(R.string.copy),
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
IconButton(
|
IconButton(
|
||||||
|
modifier = Modifier.weight(1f),
|
||||||
enabled = !isActive,
|
enabled = !isActive,
|
||||||
modifier = Modifier.focusable(),
|
|
||||||
onClick = { onDelete() },
|
onClick = { onDelete() },
|
||||||
) {
|
) {
|
||||||
val icon = Icons.Rounded.Delete
|
Icon(
|
||||||
Icon(icon, icon.name)
|
Icons.Rounded.Delete,
|
||||||
}
|
stringResource(R.string.delete_tunnel),
|
||||||
}
|
modifier = Modifier.size(24.dp),
|
||||||
} else {
|
)
|
||||||
if (context.isRunningOnTv()) {
|
}
|
||||||
Row {
|
}
|
||||||
IconButton(
|
} else if (isTv) {
|
||||||
onClick = {
|
Row(
|
||||||
onHold()
|
modifier = Modifier.fillMaxWidth(),
|
||||||
navController.navigate(
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
Route.TunnelOptions(tunnel.id),
|
) {
|
||||||
)
|
IconButton(
|
||||||
},
|
modifier = Modifier.weight(1f),
|
||||||
) {
|
onClick = {
|
||||||
val icon = Icons.Rounded.Settings
|
navController.navigate(Route.TunnelOptions(tunnel.id))
|
||||||
Icon(
|
onSetSelectedTunnel(null)
|
||||||
icon,
|
},
|
||||||
icon.name,
|
) {
|
||||||
)
|
Icon(
|
||||||
}
|
Icons.Rounded.Settings,
|
||||||
IconButton(
|
stringResource(id = R.string.settings),
|
||||||
onClick = {
|
modifier = Modifier.size(24.dp),
|
||||||
if (isActive) {
|
)
|
||||||
onClick()
|
}
|
||||||
} else {
|
IconButton(
|
||||||
snackbar.showMessage(
|
modifier = Modifier.weight(1f),
|
||||||
context.getString(R.string.turn_on_tunnel),
|
onClick = {
|
||||||
)
|
if (isActive) {
|
||||||
}
|
onClick()
|
||||||
},
|
} else {
|
||||||
) {
|
viewModel.handleEvent(
|
||||||
val icon = Icons.Rounded.Info
|
AppEvent.ShowMessage(StringValue.StringResource(R.string.turn_on_tunnel)),
|
||||||
Icon(icon, icon.name)
|
)
|
||||||
}
|
}
|
||||||
IconButton(
|
},
|
||||||
onClick = { onCopy() },
|
) {
|
||||||
) {
|
Icon(
|
||||||
val icon = Icons.Rounded.CopyAll
|
Icons.Rounded.Info,
|
||||||
Icon(icon, icon.name)
|
stringResource(R.string.info),
|
||||||
}
|
modifier = Modifier.size(24.dp),
|
||||||
IconButton(
|
)
|
||||||
onClick = {
|
}
|
||||||
if (isActive) {
|
IconButton(
|
||||||
snackbar.showMessage(
|
modifier = Modifier.weight(1f),
|
||||||
context.getString(R.string.turn_off_tunnel),
|
onClick = onCopy,
|
||||||
)
|
) {
|
||||||
} else {
|
Icon(
|
||||||
onHold()
|
Icons.Rounded.CopyAll,
|
||||||
onDelete()
|
stringResource(R.string.copy),
|
||||||
}
|
modifier = Modifier.size(24.dp),
|
||||||
},
|
)
|
||||||
) {
|
}
|
||||||
val icon = Icons.Rounded.Delete
|
IconButton(
|
||||||
Icon(
|
modifier = Modifier.weight(1f),
|
||||||
icon,
|
onClick = {
|
||||||
icon.name,
|
if (isActive) {
|
||||||
)
|
viewModel.handleEvent(
|
||||||
}
|
AppEvent.ShowMessage(StringValue.StringResource(R.string.turn_off_tunnel)),
|
||||||
ScaledSwitch(
|
)
|
||||||
modifier = Modifier.focusRequester(itemFocusRequester),
|
} else {
|
||||||
checked = isActive,
|
onDelete()
|
||||||
onClick = onSwitchClick,
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Delete,
|
||||||
|
stringResource(R.string.delete_tunnel),
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ScaledSwitch(
|
ScaledSwitch(
|
||||||
modifier = Modifier.focusRequester(itemFocusRequester),
|
modifier = Modifier
|
||||||
|
.focusRequester(itemFocusRequester)
|
||||||
|
.weight(1f),
|
||||||
checked = isActive,
|
checked = isActive,
|
||||||
onClick = onSwitchClick,
|
onClick = onSwitchClick,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ScaledSwitch(
|
||||||
|
modifier = Modifier.focusRequester(itemFocusRequester),
|
||||||
|
checked = isActive,
|
||||||
|
onClick = onSwitchClick,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun UrlImportDialog(onDismiss: () -> Unit, onConfirm: (String) -> Unit) {
|
||||||
|
var url by remember { mutableStateOf("") }
|
||||||
|
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = onDismiss,
|
||||||
|
title = { Text(stringResource(R.string.add_from_url)) },
|
||||||
|
text = {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(vertical = 8.dp),
|
||||||
|
) {
|
||||||
|
OutlinedTextField(
|
||||||
|
value = url,
|
||||||
|
onValueChange = { url = it },
|
||||||
|
label = { Text(stringResource(R.string.enter_config_url)) },
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(
|
||||||
|
onClick = { onConfirm(url) },
|
||||||
|
enabled = url.isNotBlank(),
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.okay))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(onClick = onDismiss) {
|
||||||
|
Text(stringResource(R.string.cancel))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
+108
@@ -0,0 +1,108 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config
|
||||||
|
|
||||||
|
import android.view.WindowManager
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.DisposableEffect
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.zaneschepke.wireguardautotunnel.MainActivity
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.AddPeerButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.InterfaceSection
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.PeersSection
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ConfigScreen(tunnelConf: TunnelConf?, appViewModel: AppViewModel, viewModel: ConfigViewModel = hiltViewModel()) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
|
||||||
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
|
val activity = context as? MainActivity
|
||||||
|
|
||||||
|
// Secure screen due to sensitive information
|
||||||
|
DisposableEffect(Unit) {
|
||||||
|
activity?.window?.setFlags(
|
||||||
|
WindowManager.LayoutParams.FLAG_SECURE,
|
||||||
|
WindowManager.LayoutParams.FLAG_SECURE,
|
||||||
|
)
|
||||||
|
onDispose {
|
||||||
|
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
// set callback for navbar to invoke save
|
||||||
|
appViewModel.handleEvent(
|
||||||
|
AppEvent.SetScreenAction {
|
||||||
|
keyboardController?.hide()
|
||||||
|
viewModel.save(tunnelConf)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(tunnelConf) {
|
||||||
|
viewModel.initFromTunnel(tunnelConf)
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(uiState.success) {
|
||||||
|
if (uiState.success == true) {
|
||||||
|
appViewModel.handleEvent(AppEvent.ShowMessage(StringValue.StringResource(R.string.config_changes_saved)))
|
||||||
|
appViewModel.handleEvent(AppEvent.PopBackStack(true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(uiState.message) {
|
||||||
|
uiState.message?.let { message ->
|
||||||
|
appViewModel.handleEvent(AppEvent.ShowMessage(message))
|
||||||
|
viewModel.setMessage(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.showAuthPrompt) {
|
||||||
|
AuthorizationPrompt(
|
||||||
|
onSuccess = {
|
||||||
|
viewModel.toggleShowAuthPrompt()
|
||||||
|
viewModel.onAuthenticated()
|
||||||
|
},
|
||||||
|
onError = {
|
||||||
|
viewModel.toggleShowAuthPrompt()
|
||||||
|
appViewModel.handleEvent(AppEvent.ShowMessage(StringValue.StringResource(R.string.error_authentication_failed)))
|
||||||
|
},
|
||||||
|
onFailure = {
|
||||||
|
viewModel.toggleShowAuthPrompt()
|
||||||
|
appViewModel.handleEvent(AppEvent.ShowMessage(StringValue.StringResource(R.string.error_authorization_failed)))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(horizontal = 24.dp),
|
||||||
|
) {
|
||||||
|
InterfaceSection(uiState, viewModel)
|
||||||
|
PeersSection(uiState, viewModel)
|
||||||
|
AddPeerButton(viewModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
+173
@@ -0,0 +1,173 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
|
||||||
|
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class ConfigViewModel @Inject constructor(
|
||||||
|
private val tunnelRepository: TunnelRepository,
|
||||||
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
|
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||||
|
val uiState: StateFlow<ConfigUiState> = _uiState.asStateFlow()
|
||||||
|
|
||||||
|
fun initFromTunnel(tunnelConf: TunnelConf?) {
|
||||||
|
if (tunnelConf == null) return
|
||||||
|
_uiState.update {
|
||||||
|
val proxy = ConfigProxy.from(tunnelConf.toAmConfig())
|
||||||
|
it.copy(
|
||||||
|
tunnelName = tunnelConf.name,
|
||||||
|
configProxy = proxy,
|
||||||
|
showScripts = proxy.hasScripts(),
|
||||||
|
showAmneziaValues = proxy.`interface`.junkPacketCount.isNotBlank(),
|
||||||
|
isAuthenticated = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateTunnelName(name: String) {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(tunnelName = name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateInterface(newInterface: InterfaceProxy) {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(
|
||||||
|
configProxy = it.configProxy.copy(
|
||||||
|
`interface` = newInterface,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleAmneziaValues() {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(showAmneziaValues = !it.showAmneziaValues)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleScripts() {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(showScripts = !it.showScripts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleAmneziaCompatibility() {
|
||||||
|
val (show, `interface`) = with(_uiState.value.configProxy) {
|
||||||
|
if (`interface`.isAmneziaCompatibilityModeSet()) {
|
||||||
|
Pair(false, `interface`.resetAmneziaProperties())
|
||||||
|
} else {
|
||||||
|
Pair(true, `interface`.toAmneziaCompatibilityConfig())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(
|
||||||
|
showAmneziaValues = show,
|
||||||
|
configProxy = it.configProxy.copy(
|
||||||
|
`interface` = `interface`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addPeer() {
|
||||||
|
_uiState.update { currentState ->
|
||||||
|
currentState.copy(
|
||||||
|
configProxy = currentState.configProxy.copy(
|
||||||
|
peers = currentState.configProxy.peers + PeerProxy(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removePeer(index: Int) {
|
||||||
|
_uiState.update { currentState ->
|
||||||
|
currentState.copy(
|
||||||
|
configProxy = currentState.configProxy.copy(
|
||||||
|
peers = currentState.configProxy.peers.toMutableList().apply { removeAt(index) },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatePeer(index: Int, peer: PeerProxy) {
|
||||||
|
_uiState.update { currentState ->
|
||||||
|
currentState.copy(
|
||||||
|
configProxy = currentState.configProxy.copy(
|
||||||
|
peers = currentState.configProxy.peers.toMutableList().apply { set(index, peer) },
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleLanExclusion(index: Int) {
|
||||||
|
val peer = _uiState.value.configProxy.peers[index]
|
||||||
|
val updated = if (peer.isLanExcluded()) peer.includeLan() else peer.excludeLan()
|
||||||
|
updatePeer(index, updated)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setMessage(message: StringValue?) {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(message = message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO improve error messaging
|
||||||
|
fun save(tunnelConf: TunnelConf?) = viewModelScope.launch(ioDispatcher) {
|
||||||
|
val message = try {
|
||||||
|
val saveConfig = buildTunnelConfFromState(tunnelConf)
|
||||||
|
tunnelRepository.save(saveConfig)
|
||||||
|
_uiState.update { it.copy(success = true) }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
setMessage(
|
||||||
|
e.message?.let { message ->
|
||||||
|
(StringValue.DynamicString(message))
|
||||||
|
} ?: StringValue.StringResource(R.string.unknown_error),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildTunnelConfFromState(tunnelConf: TunnelConf?): TunnelConf {
|
||||||
|
val (wg, am) = _uiState.value.configProxy.buildConfigs()
|
||||||
|
val name = _uiState.value.tunnelName
|
||||||
|
return tunnelConf?.copyWithCallback(
|
||||||
|
tunName = name,
|
||||||
|
amQuick = am.toAwgQuickString(true),
|
||||||
|
wgQuick = wg.toWgQuickString(true),
|
||||||
|
) ?: TunnelConf(
|
||||||
|
tunName = name, amQuick = am.toAwgQuickString(true),
|
||||||
|
wgQuick = wg.toWgQuickString(true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onAuthenticated() {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(isAuthenticated = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleShowAuthPrompt() {
|
||||||
|
_uiState.update {
|
||||||
|
it.copy(showAuthPrompt = !it.showAuthPrompt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewModel
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AddPeerButton(viewModel: ConfigViewModel) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.Center,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth().padding(bottom = 24.dp),
|
||||||
|
) {
|
||||||
|
TextButton(onClick = { viewModel.addPeer() }) {
|
||||||
|
Text(stringResource(R.string.add_peer))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+72
@@ -0,0 +1,72 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.MoreVert
|
||||||
|
import androidx.compose.material3.DropdownMenu
|
||||||
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.shadow
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun InterfaceDropdown(
|
||||||
|
expanded: Boolean,
|
||||||
|
onExpandedChange: (Boolean) -> Unit,
|
||||||
|
showScripts: Boolean,
|
||||||
|
showAmneziaValues: Boolean,
|
||||||
|
isAmneziaCompatibilitySet: Boolean,
|
||||||
|
onToggleScripts: () -> Unit,
|
||||||
|
onToggleAmneziaValues: () -> Unit,
|
||||||
|
onToggleAmneziaCompatibility: () -> Unit,
|
||||||
|
) {
|
||||||
|
Column {
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
onClick = { onExpandedChange(true) },
|
||||||
|
) {
|
||||||
|
Icon(Icons.Rounded.MoreVert, contentDescription = "More")
|
||||||
|
}
|
||||||
|
DropdownMenu(
|
||||||
|
expanded = expanded,
|
||||||
|
onDismissRequest = { onExpandedChange(false) },
|
||||||
|
modifier = Modifier.shadow(12.dp).background(MaterialTheme.colorScheme.surface),
|
||||||
|
) {
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text(if (showScripts) stringResource(R.string.hide_scripts) else stringResource(R.string.show_scripts)) },
|
||||||
|
onClick = {
|
||||||
|
onToggleScripts()
|
||||||
|
onExpandedChange(false)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text(if (showAmneziaValues) stringResource(R.string.hide_amnezia_properties) else stringResource(R.string.show_amnezia_properties)) },
|
||||||
|
onClick = {
|
||||||
|
onToggleAmneziaValues()
|
||||||
|
onExpandedChange(false)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = {
|
||||||
|
Text(
|
||||||
|
if (isAmneziaCompatibilitySet) stringResource(R.string.remove_amnezia_compatibility) else stringResource(R.string.enable_amnezia_compatibility),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
onToggleAmneziaCompatibility()
|
||||||
|
onExpandedChange(false)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+220
@@ -0,0 +1,220 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.ContentCopy
|
||||||
|
import androidx.compose.material.icons.rounded.Refresh
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalClipboardManager
|
||||||
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.AnnotatedString
|
||||||
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||||
|
import androidx.compose.ui.text.input.VisualTransformation
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun InterfaceFields(
|
||||||
|
interfaceState: InterfaceProxy,
|
||||||
|
showAuthPrompt: () -> Unit,
|
||||||
|
isAuthenticated: Boolean,
|
||||||
|
showScripts: Boolean,
|
||||||
|
showAmneziaValues: Boolean,
|
||||||
|
onInterfaceChange: (InterfaceProxy) -> Unit,
|
||||||
|
) {
|
||||||
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
val clipboardManager = LocalClipboardManager.current
|
||||||
|
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||||
|
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||||
|
|
||||||
|
OutlinedTextField(
|
||||||
|
value = interfaceState.privateKey,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(privateKey = it)) },
|
||||||
|
label = { Text(stringResource(R.string.private_key), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
modifier = Modifier.fillMaxWidth().clickable {
|
||||||
|
if (!isAuthenticated) showAuthPrompt()
|
||||||
|
},
|
||||||
|
visualTransformation = if (isAuthenticated) VisualTransformation.None else PasswordVisualTransformation(),
|
||||||
|
trailingIcon = {
|
||||||
|
IconButton(enabled = isAuthenticated, onClick = {
|
||||||
|
val keypair = com.wireguard.crypto.KeyPair()
|
||||||
|
onInterfaceChange(
|
||||||
|
interfaceState.copy(
|
||||||
|
privateKey = keypair.privateKey.toBase64(),
|
||||||
|
publicKey = keypair.publicKey.toBase64(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Refresh,
|
||||||
|
stringResource(R.string.rotate_keys),
|
||||||
|
tint = if (isAuthenticated) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.outline,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enabled = isAuthenticated,
|
||||||
|
singleLine = true,
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
)
|
||||||
|
OutlinedTextField(
|
||||||
|
value = interfaceState.publicKey,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(publicKey = it)) },
|
||||||
|
label = { Text(stringResource(R.string.public_key), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
enabled = false,
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
singleLine = true,
|
||||||
|
trailingIcon = {
|
||||||
|
IconButton(onClick = {
|
||||||
|
clipboardManager.setText(AnnotatedString(interfaceState.publicKey))
|
||||||
|
}) {
|
||||||
|
Icon(Icons.Rounded.ContentCopy, stringResource(R.string.copy_public_key))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.addresses,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(addresses = it)) },
|
||||||
|
label = stringResource(R.string.addresses),
|
||||||
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.listenPort,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(listenPort = it)) },
|
||||||
|
label = stringResource(R.string.listen_port),
|
||||||
|
hint = stringResource(R.string.random),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||||
|
) {
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.dnsServers,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(dnsServers = it)) },
|
||||||
|
label = stringResource(R.string.dns_servers),
|
||||||
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
|
modifier = Modifier.weight(3f),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.mtu,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(mtu = it)) },
|
||||||
|
label = stringResource(R.string.mtu),
|
||||||
|
hint = stringResource(R.string.auto),
|
||||||
|
modifier = Modifier.weight(2f),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (showScripts) {
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.preUp,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(preUp = it)) },
|
||||||
|
label = stringResource(R.string.pre_up),
|
||||||
|
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.postUp,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(postUp = it)) },
|
||||||
|
label = stringResource(R.string.post_up),
|
||||||
|
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.preDown,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(preDown = it)) },
|
||||||
|
label = stringResource(R.string.pre_down),
|
||||||
|
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.postDown,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(postDown = it)) },
|
||||||
|
label = stringResource(R.string.post_down),
|
||||||
|
hint = stringResource(R.string.comma_separated_list).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (showAmneziaValues) {
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.junkPacketCount,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(junkPacketCount = it)) },
|
||||||
|
label = stringResource(R.string.junk_packet_count),
|
||||||
|
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.junkPacketMinSize,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(junkPacketMinSize = it)) },
|
||||||
|
label = stringResource(R.string.junk_packet_minimum_size),
|
||||||
|
hint = stringResource(R.string.junk_packet_minimum_size).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.junkPacketMaxSize,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(junkPacketMaxSize = it)) },
|
||||||
|
label = stringResource(R.string.junk_packet_maximum_size),
|
||||||
|
hint = stringResource(R.string.junk_packet_maximum_size).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.initPacketJunkSize,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(initPacketJunkSize = it)) },
|
||||||
|
label = stringResource(R.string.init_packet_junk_size),
|
||||||
|
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.responsePacketJunkSize,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(responsePacketJunkSize = it)) },
|
||||||
|
label = stringResource(R.string.response_packet_junk_size),
|
||||||
|
hint = stringResource(R.string.response_packet_junk_size).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.initPacketMagicHeader,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(initPacketMagicHeader = it)) },
|
||||||
|
label = stringResource(R.string.init_packet_magic_header),
|
||||||
|
hint = stringResource(R.string.init_packet_magic_header).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.responsePacketMagicHeader,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(responsePacketMagicHeader = it)) },
|
||||||
|
label = stringResource(R.string.response_packet_magic_header),
|
||||||
|
hint = stringResource(R.string.response_packet_magic_header).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.underloadPacketMagicHeader,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(underloadPacketMagicHeader = it)) },
|
||||||
|
label = stringResource(R.string.underload_packet_magic_header),
|
||||||
|
hint = stringResource(R.string.underload_packet_magic_header).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = interfaceState.transportPacketMagicHeader,
|
||||||
|
onValueChange = { onInterfaceChange(interfaceState.copy(transportPacketMagicHeader = it)) },
|
||||||
|
label = stringResource(R.string.transport_packet_magic_header),
|
||||||
|
hint = stringResource(R.string.transport_packet_magic_header).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.focusGroup
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun InterfaceSection(uiState: ConfigUiState, viewModel: ConfigViewModel) {
|
||||||
|
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
Surface(
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||||
|
modifier = Modifier.padding(16.dp).focusGroup(),
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
GroupLabel(stringResource(R.string.interface_))
|
||||||
|
InterfaceDropdown(
|
||||||
|
expanded = isDropDownExpanded,
|
||||||
|
onExpandedChange = { isDropDownExpanded = it },
|
||||||
|
showScripts = uiState.showScripts,
|
||||||
|
showAmneziaValues = uiState.showAmneziaValues,
|
||||||
|
isAmneziaCompatibilitySet = uiState.configProxy.`interface`.isAmneziaCompatibilityModeSet(),
|
||||||
|
onToggleScripts = viewModel::toggleScripts,
|
||||||
|
onToggleAmneziaValues = viewModel::toggleAmneziaValues,
|
||||||
|
onToggleAmneziaCompatibility = viewModel::toggleAmneziaCompatibility,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.tunnelName,
|
||||||
|
onValueChange = viewModel::updateTunnelName,
|
||||||
|
label = stringResource(R.string.name),
|
||||||
|
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
InterfaceFields(
|
||||||
|
interfaceState = uiState.configProxy.`interface`,
|
||||||
|
showAuthPrompt = { viewModel.toggleShowAuthPrompt() },
|
||||||
|
showScripts = uiState.showScripts,
|
||||||
|
showAmneziaValues = uiState.showAmneziaValues,
|
||||||
|
onInterfaceChange = viewModel::updateInterface,
|
||||||
|
isAuthenticated = uiState.isAuthenticated,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||||
|
import androidx.compose.ui.text.input.VisualTransformation
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun PeerFields(peer: PeerProxy, onPeerChange: (PeerProxy) -> Unit, showAuthPrompt: () -> Unit, isAuthenticated: Boolean) {
|
||||||
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||||
|
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||||
|
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = peer.publicKey,
|
||||||
|
onValueChange = { onPeerChange(peer.copy(publicKey = it)) },
|
||||||
|
label = stringResource(R.string.public_key),
|
||||||
|
hint = stringResource(R.string.base64_key),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
OutlinedTextField(
|
||||||
|
visualTransformation = if (isAuthenticated) VisualTransformation.None else PasswordVisualTransformation(),
|
||||||
|
value = peer.preSharedKey,
|
||||||
|
enabled = isAuthenticated,
|
||||||
|
onValueChange = { onPeerChange(peer.copy(preSharedKey = it)) },
|
||||||
|
label = { Text(stringResource(R.string.preshared_key), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
placeholder = { Text(stringResource(R.string.optional), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
modifier = Modifier.fillMaxWidth().clickable { if (!isAuthenticated) showAuthPrompt() },
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
singleLine = true,
|
||||||
|
)
|
||||||
|
OutlinedTextField(
|
||||||
|
value = peer.persistentKeepalive,
|
||||||
|
onValueChange = { onPeerChange(peer.copy(persistentKeepalive = it)) },
|
||||||
|
label = { Text(stringResource(R.string.persistent_keepalive), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
placeholder = { Text(stringResource(R.string.optional), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
trailingIcon = { Text(stringResource(R.string.seconds), style = MaterialTheme.typography.bodyMedium, modifier = Modifier.padding(end = 10.dp)) },
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = peer.endpoint,
|
||||||
|
onValueChange = { onPeerChange(peer.copy(endpoint = it)) },
|
||||||
|
label = stringResource(R.string.endpoint),
|
||||||
|
hint = stringResource(R.string.endpoint).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
)
|
||||||
|
OutlinedTextField(
|
||||||
|
value = peer.allowedIps,
|
||||||
|
onValueChange = { onPeerChange(peer.copy(allowedIps = it)) },
|
||||||
|
label = { Text(stringResource(R.string.allowed_ips), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
placeholder = { Text(stringResource(R.string.comma_separated_list), style = MaterialTheme.typography.bodyMedium) },
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
)
|
||||||
|
}
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.focusGroup
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
|
import androidx.compose.material.icons.rounded.MoreVert
|
||||||
|
import androidx.compose.material3.DropdownMenu
|
||||||
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.shadow
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun PeersSection(uiState: ConfigUiState, viewModel: ConfigViewModel) {
|
||||||
|
uiState.configProxy.peers.forEachIndexed { index, peer ->
|
||||||
|
var isDropDownExpanded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
Surface(
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||||
|
modifier = Modifier.padding(16.dp).focusGroup(),
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
GroupLabel(stringResource(R.string.peer))
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
onClick = { viewModel.removePeer(index) },
|
||||||
|
) {
|
||||||
|
Icon(Icons.Rounded.Delete, contentDescription = stringResource(R.string.delete))
|
||||||
|
}
|
||||||
|
Column {
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.size(iconSize),
|
||||||
|
onClick = { isDropDownExpanded = true },
|
||||||
|
) {
|
||||||
|
Icon(Icons.Rounded.MoreVert, contentDescription = stringResource(R.string.quick_actions))
|
||||||
|
}
|
||||||
|
DropdownMenu(
|
||||||
|
expanded = isDropDownExpanded,
|
||||||
|
onDismissRequest = { isDropDownExpanded = false },
|
||||||
|
modifier = Modifier.shadow(12.dp).background(MaterialTheme.colorScheme.surface),
|
||||||
|
) {
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = {
|
||||||
|
Text(if (peer.isLanExcluded()) stringResource(R.string.include_lan) else stringResource(R.string.exclude_lan))
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
viewModel.toggleLanExclusion(index)
|
||||||
|
isDropDownExpanded = false
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PeerFields(
|
||||||
|
peer = peer,
|
||||||
|
onPeerChange = { viewModel.updatePeer(index, it) },
|
||||||
|
showAuthPrompt = { viewModel.toggleShowAuthPrompt() },
|
||||||
|
isAuthenticated = uiState.isAuthenticated,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
|
||||||
|
data class ConfigUiState(
|
||||||
|
val tunnelName: String = "",
|
||||||
|
val configProxy: ConfigProxy = ConfigProxy(`interface` = InterfaceProxy(), peers = listOf(PeerProxy())),
|
||||||
|
val showAmneziaValues: Boolean = false,
|
||||||
|
val showScripts: Boolean = false,
|
||||||
|
val isAuthenticated: Boolean = true,
|
||||||
|
val showAuthPrompt: Boolean = false,
|
||||||
|
val message: StringValue? = null,
|
||||||
|
val success: Boolean? = null,
|
||||||
|
)
|
||||||
+5
-15
@@ -1,28 +1,18 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.scanner
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.DisposableEffect
|
import androidx.compose.runtime.DisposableEffect
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.viewinterop.AndroidView
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
import com.journeyapps.barcodescanner.CompoundBarcodeView
|
import com.journeyapps.barcodescanner.CompoundBarcodeView
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
|
||||||
import com.zaneschepke.wireguardautotunnel.viewmodel.ScannerViewModel
|
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScannerScreen(viewModel: ScannerViewModel = hiltViewModel()) {
|
fun ScannerScreen(viewModel: AppViewModel) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
val success = viewModel.success.collectAsStateWithLifecycle(null)
|
|
||||||
|
|
||||||
LaunchedEffect(success.value) {
|
|
||||||
if (success.value != null) navController.popBackStack()
|
|
||||||
}
|
|
||||||
|
|
||||||
val barcodeView = remember {
|
val barcodeView = remember {
|
||||||
CompoundBarcodeView(context).apply {
|
CompoundBarcodeView(context).apply {
|
||||||
@@ -30,7 +20,7 @@ fun ScannerScreen(viewModel: ScannerViewModel = hiltViewModel()) {
|
|||||||
this.setStatusText("")
|
this.setStatusText("")
|
||||||
this.decodeSingle { result ->
|
this.decodeSingle { result ->
|
||||||
result.text?.let { barCodeOrQr ->
|
result.text?.let { barCodeOrQr ->
|
||||||
viewModel.onTunnelQrResult(barCodeOrQr)
|
viewModel.handleEvent(AppEvent.ImportTunnelFromQrCode(barCodeOrQr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user