mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f3c4cb2b3 |
@@ -1,4 +1,4 @@
|
||||
name: on-issue
|
||||
name: Issue Updates Workflow
|
||||
|
||||
on:
|
||||
issues:
|
||||
@@ -7,8 +7,8 @@ on:
|
||||
|
||||
jobs:
|
||||
|
||||
on-issue:
|
||||
name: On new issue
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Send Telegram Message
|
||||
@@ -1,4 +1,4 @@
|
||||
name: on-publish
|
||||
name: Release Updates Workflow
|
||||
|
||||
on:
|
||||
release:
|
||||
@@ -7,8 +7,8 @@ on:
|
||||
|
||||
jobs:
|
||||
|
||||
on-publish:
|
||||
name: On publish
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Send Telegram Message
|
||||
@@ -2,7 +2,7 @@ name: release-android
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "4 3 * * *"
|
||||
- cron: "4 3 * * *"
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
track:
|
||||
@@ -14,7 +14,7 @@ on:
|
||||
- alpha
|
||||
- beta
|
||||
- production
|
||||
default: none
|
||||
default: alpha
|
||||
required: true
|
||||
release_type:
|
||||
type: choice
|
||||
@@ -30,30 +30,13 @@ on:
|
||||
description: "Tag name for release"
|
||||
required: false
|
||||
default: nightly
|
||||
workflow_call:
|
||||
|
||||
jobs:
|
||||
check_date:
|
||||
runs-on: ubuntu-latest
|
||||
name: Check latest commit
|
||||
outputs:
|
||||
should_run: ${{ steps.should_run.outputs.should_run }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: print latest_commit
|
||||
run: echo ${{ github.sha }}
|
||||
- id: should_run
|
||||
continue-on-error: true
|
||||
name: check latest commit is less than a day
|
||||
if: ${{ github.event_name == 'schedule' }}
|
||||
run: test -z $(git rev-list --after="23 hours" ${{ github.sha }}) && echo "::set-output name=should_run::false"
|
||||
build:
|
||||
needs: check_date
|
||||
if: |
|
||||
github.event_name != 'schedule' ||
|
||||
(needs.check_date.outputs.should_run == 'true' && github.event_name == 'schedule')
|
||||
name: Build Signed APK
|
||||
if: ${{ inputs.release_type != 'none' }}
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
env:
|
||||
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||
@@ -127,24 +110,9 @@ jobs:
|
||||
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
|
||||
echo "VERSION_CODE=$version_code" >> $GITHUB_ENV
|
||||
|
||||
- name: Commit and push versionCode changes
|
||||
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' || inputs.release_type == 'prerelease' }}
|
||||
run: |
|
||||
git config --global user.name 'GitHub Actions'
|
||||
git config --global user.email 'actions@github.com'
|
||||
git add versionCode.txt
|
||||
git commit -m "Automated build update"
|
||||
|
||||
- name: Push changes
|
||||
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' || inputs.release_type == 'prerelease' }}
|
||||
uses: ad-m/github-push-action@master
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
branch: ${{ github.ref }}
|
||||
|
||||
# Save the APK after the Build job is complete to publish it as a Github release in the next job
|
||||
- name: Upload APK
|
||||
uses: actions/upload-artifact@v4.4.0
|
||||
uses: actions/upload-artifact@v4.3.6
|
||||
with:
|
||||
name: wgtunnel
|
||||
path: ${{ env.APK_PATH }}
|
||||
|
||||
+13
-32
@@ -8,21 +8,6 @@ plugins {
|
||||
alias(libs.plugins.grgit)
|
||||
}
|
||||
|
||||
val versionFile = file("$rootDir/versionCode.txt")
|
||||
|
||||
val versionCodeIncrement = with(getBuildTaskName().lowercase()) {
|
||||
when {
|
||||
this.contains(Constants.NIGHTLY) || this.contains(Constants.PRERELEASE) -> {
|
||||
if (versionFile.exists()) {
|
||||
versionFile.readText().toInt() + 1
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = Constants.APP_ID
|
||||
compileSdk = Constants.TARGET_SDK
|
||||
@@ -35,7 +20,7 @@ android {
|
||||
applicationId = Constants.APP_ID
|
||||
minSdk = Constants.MIN_SDK
|
||||
targetSdk = Constants.TARGET_SDK
|
||||
versionCode = Constants.VERSION_CODE + versionCodeIncrement
|
||||
versionCode = determineVersionCode()
|
||||
versionName = determineVersionName()
|
||||
|
||||
ksp { arg("room.schemaLocation", "$projectDir/schemas") }
|
||||
@@ -172,6 +157,8 @@ dependencies {
|
||||
implementation(libs.androidx.navigation.compose)
|
||||
implementation(libs.androidx.hilt.navigation.compose)
|
||||
|
||||
implementation(libs.zaneschepke.multifab)
|
||||
|
||||
// hilt
|
||||
implementation(libs.hilt.android)
|
||||
ksp(libs.hilt.android.compiler)
|
||||
@@ -212,6 +199,16 @@ dependencies {
|
||||
implementation(libs.androidx.core.splashscreen)
|
||||
}
|
||||
|
||||
fun determineVersionCode(): Int {
|
||||
return with(getBuildTaskName().lowercase()) {
|
||||
when {
|
||||
contains(Constants.NIGHTLY) -> Constants.VERSION_CODE + Constants.NIGHTLY_CODE
|
||||
contains(Constants.PRERELEASE) -> Constants.VERSION_CODE + Constants.PRERELEASE_CODE
|
||||
else -> Constants.VERSION_CODE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun determineVersionName(): String {
|
||||
return with(getBuildTaskName().lowercase()) {
|
||||
when {
|
||||
@@ -222,19 +219,3 @@ fun determineVersionName(): String {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val incrementVersionCode by tasks.registering {
|
||||
doLast {
|
||||
val versionFile = file("$rootDir/versionCode.txt")
|
||||
if (versionFile.exists()) {
|
||||
versionFile.writeText(versionCodeIncrement.toString())
|
||||
println("Incremented versionCode to $versionCodeIncrement")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.whenTaskAdded {
|
||||
if (name.startsWith("assemble") && !name.lowercase().contains("debug")) {
|
||||
dependsOn(incrementVersionCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,225 +0,0 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 10,
|
||||
"identityHash": "c8621055524f90b4d1972f6171f59e80",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "Settings",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `is_tunnel_enabled` INTEGER NOT NULL, `is_tunnel_on_mobile_data_enabled` INTEGER NOT NULL, `trusted_network_ssids` TEXT NOT NULL, `is_always_on_vpn_enabled` INTEGER NOT NULL, `is_tunnel_on_ethernet_enabled` INTEGER NOT NULL, `is_shortcuts_enabled` INTEGER NOT NULL DEFAULT false, `is_tunnel_on_wifi_enabled` INTEGER NOT NULL DEFAULT false, `is_kernel_enabled` INTEGER NOT NULL DEFAULT false, `is_restore_on_boot_enabled` INTEGER NOT NULL DEFAULT false, `is_multi_tunnel_enabled` INTEGER NOT NULL DEFAULT false, `is_auto_tunnel_paused` INTEGER NOT NULL DEFAULT false, `is_ping_enabled` INTEGER NOT NULL DEFAULT false, `is_amnezia_enabled` INTEGER NOT NULL DEFAULT false)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isAutoTunnelEnabled",
|
||||
"columnName": "is_tunnel_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isTunnelOnMobileDataEnabled",
|
||||
"columnName": "is_tunnel_on_mobile_data_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "trustedNetworkSSIDs",
|
||||
"columnName": "trusted_network_ssids",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isAlwaysOnVpnEnabled",
|
||||
"columnName": "is_always_on_vpn_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isTunnelOnEthernetEnabled",
|
||||
"columnName": "is_tunnel_on_ethernet_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "isShortcutsEnabled",
|
||||
"columnName": "is_shortcuts_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isTunnelOnWifiEnabled",
|
||||
"columnName": "is_tunnel_on_wifi_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isKernelEnabled",
|
||||
"columnName": "is_kernel_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isRestoreOnBootEnabled",
|
||||
"columnName": "is_restore_on_boot_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isMultiTunnelEnabled",
|
||||
"columnName": "is_multi_tunnel_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isAutoTunnelPaused",
|
||||
"columnName": "is_auto_tunnel_paused",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isPingEnabled",
|
||||
"columnName": "is_ping_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isAmneziaEnabled",
|
||||
"columnName": "is_amnezia_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [],
|
||||
"foreignKeys": []
|
||||
},
|
||||
{
|
||||
"tableName": "TunnelConfig",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `wg_quick` TEXT NOT NULL, `tunnel_networks` TEXT NOT NULL DEFAULT '', `is_mobile_data_tunnel` INTEGER NOT NULL DEFAULT false, `is_primary_tunnel` INTEGER NOT NULL DEFAULT false, `am_quick` TEXT NOT NULL DEFAULT '', `is_Active` INTEGER NOT NULL DEFAULT false, `is_ping_enabled` INTEGER NOT NULL DEFAULT false, `ping_interval` INTEGER DEFAULT null, `ping_cooldown` INTEGER DEFAULT null, `ping_ip` TEXT DEFAULT null)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "wgQuick",
|
||||
"columnName": "wg_quick",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "tunnelNetworks",
|
||||
"columnName": "tunnel_networks",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true,
|
||||
"defaultValue": "''"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isMobileDataTunnel",
|
||||
"columnName": "is_mobile_data_tunnel",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isPrimaryTunnel",
|
||||
"columnName": "is_primary_tunnel",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "amQuick",
|
||||
"columnName": "am_quick",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true,
|
||||
"defaultValue": "''"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isActive",
|
||||
"columnName": "is_Active",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "isPingEnabled",
|
||||
"columnName": "is_ping_enabled",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true,
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"fieldPath": "pingInterval",
|
||||
"columnName": "ping_interval",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false,
|
||||
"defaultValue": "null"
|
||||
},
|
||||
{
|
||||
"fieldPath": "pingCooldown",
|
||||
"columnName": "ping_cooldown",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false,
|
||||
"defaultValue": "null"
|
||||
},
|
||||
{
|
||||
"fieldPath": "pingIp",
|
||||
"columnName": "ping_ip",
|
||||
"affinity": "TEXT",
|
||||
"notNull": false,
|
||||
"defaultValue": "null"
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": true,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
},
|
||||
"indices": [
|
||||
{
|
||||
"name": "index_TunnelConfig_name",
|
||||
"unique": true,
|
||||
"columnNames": [
|
||||
"name"
|
||||
],
|
||||
"orders": [],
|
||||
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_TunnelConfig_name` ON `${TABLE_NAME}` (`name`)"
|
||||
}
|
||||
],
|
||||
"foreignKeys": []
|
||||
}
|
||||
],
|
||||
"views": [],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c8621055524f90b4d1972f6171f59e80')"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,12 @@
|
||||
android:launchMode="singleInstance"
|
||||
android:theme="@android:style/Theme.NoDisplay" />
|
||||
|
||||
<service
|
||||
android:name=".service.foreground.ForegroundService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="systemExempted"
|
||||
tools:node="merge" />
|
||||
<service
|
||||
android:name=".service.tile.TunnelControlTile"
|
||||
android:exported="true"
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
|
||||
@Database(
|
||||
entities = [Settings::class, TunnelConfig::class],
|
||||
version = 10,
|
||||
version = 9,
|
||||
autoMigrations =
|
||||
[
|
||||
AutoMigration(from = 1, to = 2),
|
||||
@@ -35,7 +35,6 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
),
|
||||
AutoMigration(7, 8),
|
||||
AutoMigration(8, 9),
|
||||
AutoMigration(9, 10),
|
||||
],
|
||||
exportSchema = true,
|
||||
)
|
||||
|
||||
+2
-12
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
@@ -23,6 +24,7 @@ class DataStoreManager(
|
||||
companion object {
|
||||
val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
||||
val BATTERY_OPTIMIZE_DISABLE_SHOWN = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
|
||||
val LAST_ACTIVE_TUNNEL = intPreferencesKey("LAST_ACTIVE_TUNNEL")
|
||||
val CURRENT_SSID = stringPreferencesKey("CURRENT_SSID")
|
||||
val IS_PIN_LOCK_ENABLED = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
||||
}
|
||||
@@ -56,18 +58,6 @@ class DataStoreManager(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T> removeFromDataStore(key: Preferences.Key<T>) {
|
||||
withContext(ioDispatcher) {
|
||||
try {
|
||||
context.dataStore.edit { it.remove(key) }
|
||||
} catch (e: IOException) {
|
||||
Timber.e(e)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] }
|
||||
|
||||
suspend fun <T> getFromStore(key: Preferences.Key<T>): T? {
|
||||
|
||||
@@ -4,6 +4,7 @@ data class GeneralState(
|
||||
val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
||||
val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
||||
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
|
||||
val lastActiveTunnelId: Int? = null,
|
||||
) {
|
||||
companion object {
|
||||
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
|
||||
|
||||
@@ -37,33 +37,11 @@ data class TunnelConfig(
|
||||
defaultValue = "false",
|
||||
)
|
||||
val isActive: Boolean = false,
|
||||
@ColumnInfo(
|
||||
name = "is_ping_enabled",
|
||||
defaultValue = "false",
|
||||
)
|
||||
val isPingEnabled: Boolean = false,
|
||||
@ColumnInfo(
|
||||
name = "ping_interval",
|
||||
defaultValue = "null",
|
||||
)
|
||||
val pingInterval: Long? = null,
|
||||
@ColumnInfo(
|
||||
name = "ping_cooldown",
|
||||
defaultValue = "null",
|
||||
)
|
||||
val pingCooldown: Long? = null,
|
||||
@ColumnInfo(
|
||||
name = "ping_ip",
|
||||
defaultValue = "null",
|
||||
)
|
||||
var pingIp: String? = null,
|
||||
) {
|
||||
|
||||
fun toAmConfig(): org.amnezia.awg.config.Config {
|
||||
return configFromAmQuick(if (amQuick != "") amQuick else wgQuick)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun findDefault(tunnels: List<TunnelConfig>): TunnelConfig? {
|
||||
return tunnels.find { it.isPrimaryTunnel } ?: tunnels.firstOrNull()
|
||||
}
|
||||
|
||||
fun configFromWgQuick(wgQuick: String): Config {
|
||||
val inputStream: InputStream = wgQuick.byteInputStream()
|
||||
|
||||
+3
-4
@@ -15,9 +15,8 @@ constructor(
|
||||
}
|
||||
|
||||
override suspend fun getStartTunnelConfig(): TunnelConfig? {
|
||||
tunnels.getActive().let {
|
||||
if (it.isNotEmpty()) return it.first()
|
||||
return getPrimaryOrFirstTunnel()
|
||||
}
|
||||
return appState.getLastActiveTunnelId()?.let {
|
||||
tunnels.getById(it)
|
||||
} ?: getPrimaryOrFirstTunnel()
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -16,6 +16,10 @@ interface AppStateRepository {
|
||||
|
||||
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
||||
|
||||
suspend fun getLastActiveTunnelId(): Int?
|
||||
|
||||
suspend fun setLastActiveTunnelId(id: Int)
|
||||
|
||||
suspend fun getCurrentSsid(): String?
|
||||
|
||||
suspend fun setCurrentSsid(ssid: String)
|
||||
|
||||
+30
-11
@@ -2,47 +2,65 @@ package com.zaneschepke.wireguardautotunnel.data.repository
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
|
||||
class DataStoreAppStateRepository(
|
||||
private val dataStoreManager: DataStoreManager,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) :
|
||||
AppStateRepository {
|
||||
override suspend fun isLocationDisclosureShown(): Boolean {
|
||||
return dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN)
|
||||
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
|
||||
return withContext(ioDispatcher) {
|
||||
dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN)
|
||||
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setLocationDisclosureShown(shown: Boolean) {
|
||||
dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, shown)
|
||||
withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, shown) }
|
||||
}
|
||||
|
||||
override suspend fun isPinLockEnabled(): Boolean {
|
||||
return dataStoreManager.getFromStore(DataStoreManager.IS_PIN_LOCK_ENABLED)
|
||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
|
||||
return withContext(ioDispatcher) {
|
||||
dataStoreManager.getFromStore(DataStoreManager.IS_PIN_LOCK_ENABLED)
|
||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setPinLockEnabled(enabled: Boolean) {
|
||||
dataStoreManager.saveToDataStore(DataStoreManager.IS_PIN_LOCK_ENABLED, enabled)
|
||||
withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.IS_PIN_LOCK_ENABLED, enabled) }
|
||||
}
|
||||
|
||||
override suspend fun isBatteryOptimizationDisableShown(): Boolean {
|
||||
return dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN)
|
||||
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
|
||||
return withContext(ioDispatcher) {
|
||||
dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN)
|
||||
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) {
|
||||
dataStoreManager.saveToDataStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown)
|
||||
withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown) }
|
||||
}
|
||||
|
||||
override suspend fun getLastActiveTunnelId(): Int? {
|
||||
return withContext(ioDispatcher) { dataStoreManager.getFromStore(DataStoreManager.LAST_ACTIVE_TUNNEL) }
|
||||
}
|
||||
|
||||
override suspend fun setLastActiveTunnelId(id: Int) {
|
||||
return withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.LAST_ACTIVE_TUNNEL, id) }
|
||||
}
|
||||
|
||||
override suspend fun getCurrentSsid(): String? {
|
||||
return dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID)
|
||||
return withContext(ioDispatcher) { dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID) }
|
||||
}
|
||||
|
||||
override suspend fun setCurrentSsid(ssid: String) {
|
||||
dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid)
|
||||
withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid) }
|
||||
}
|
||||
|
||||
override val generalStateFlow: Flow<GeneralState> =
|
||||
@@ -59,6 +77,7 @@ class DataStoreAppStateRepository(
|
||||
isPinLockEnabled =
|
||||
pref[DataStoreManager.IS_PIN_LOCK_ENABLED]
|
||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
||||
lastActiveTunnelId = pref[DataStoreManager.LAST_ACTIVE_TUNNEL],
|
||||
)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Timber.e(e)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.module
|
||||
|
||||
import android.content.Context
|
||||
import androidx.navigation.NavHostController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.NavigationService
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.components.ActivityRetainedComponent
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.android.scopes.ActivityRetainedScoped
|
||||
|
||||
@Module
|
||||
@InstallIn(ActivityRetainedComponent::class)
|
||||
object NavigationModule {
|
||||
|
||||
@Provides
|
||||
@ActivityRetainedScoped
|
||||
fun provideNestedNavController(@ApplicationContext context: Context): NavHostController {
|
||||
return NavigationService(context).navController
|
||||
}
|
||||
}
|
||||
@@ -72,8 +72,8 @@ class RepositoryModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager): AppStateRepository {
|
||||
return DataStoreAppStateRepository(dataStoreManager)
|
||||
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager, @IoDispatcher ioDispatcher: CoroutineDispatcher): AppStateRepository {
|
||||
return DataStoreAppStateRepository(dataStoreManager, ioDispatcher)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.wireguard.android.backend.WgQuickBackend
|
||||
import com.wireguard.android.util.RootShell
|
||||
import com.wireguard.android.util.ToolsInstaller
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
||||
import dagger.Module
|
||||
@@ -60,19 +60,25 @@ class TunnelModule {
|
||||
@Singleton
|
||||
fun provideVpnService(
|
||||
amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
||||
@Userspace userspaceBackend: Provider<Backend>,
|
||||
@Kernel kernelBackend: Provider<Backend>,
|
||||
appDataRepository: AppDataRepository,
|
||||
tunnelConfigRepository: TunnelConfigRepository,
|
||||
@ApplicationScope applicationScope: CoroutineScope,
|
||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||
): TunnelService {
|
||||
return WireGuardTunnel(
|
||||
amneziaBackend,
|
||||
tunnelConfigRepository,
|
||||
userspaceBackend,
|
||||
kernelBackend,
|
||||
appDataRepository,
|
||||
applicationScope,
|
||||
ioDispatcher,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideServiceManager(): ServiceManager {
|
||||
return ServiceManager()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
||||
@Inject
|
||||
lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject
|
||||
lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject
|
||||
lateinit var tunnelService: TunnelService
|
||||
|
||||
@@ -33,7 +36,7 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isAutoTunnelEnabled) {
|
||||
Timber.i("Restarting services after upgrade")
|
||||
ServiceManager.startWatcherServiceForeground(context)
|
||||
serviceManager.startWatcherServiceForeground(context)
|
||||
}
|
||||
if (!settings.isAutoTunnelEnabled || settings.isAutoTunnelPaused) {
|
||||
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
||||
|
||||
+5
-2
@@ -27,6 +27,9 @@ class BackgroundActionReceiver : BroadcastReceiver() {
|
||||
@Inject
|
||||
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
||||
|
||||
@Inject
|
||||
lateinit var serviceManager: ServiceManager
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val id = intent.getIntExtra(TUNNEL_ID_EXTRA_KEY, 0)
|
||||
if (id == 0) return
|
||||
@@ -36,7 +39,7 @@ class BackgroundActionReceiver : BroadcastReceiver() {
|
||||
applicationScope.launch {
|
||||
val tunnel = tunnelConfigRepository.getById(id)
|
||||
tunnel?.let {
|
||||
ServiceManager.startTunnelBackgroundService(context)
|
||||
serviceManager.startTunnelBackgroundService(context)
|
||||
tunnelService.get().startTunnel(it)
|
||||
}
|
||||
}
|
||||
@@ -45,7 +48,7 @@ class BackgroundActionReceiver : BroadcastReceiver() {
|
||||
applicationScope.launch {
|
||||
val tunnel = tunnelConfigRepository.getById(id)
|
||||
tunnel?.let {
|
||||
ServiceManager.stopTunnelBackgroundService(context)
|
||||
serviceManager.stopTunnelBackgroundService(context)
|
||||
tunnelService.get().stopTunnel(it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -24,6 +23,9 @@ class BootReceiver : BroadcastReceiver() {
|
||||
@Inject
|
||||
lateinit var tunnelService: Provider<TunnelService>
|
||||
|
||||
@Inject
|
||||
lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject
|
||||
@ApplicationScope
|
||||
lateinit var applicationScope: CoroutineScope
|
||||
@@ -31,20 +33,16 @@ class BootReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (Intent.ACTION_BOOT_COMPLETED != intent.action) return
|
||||
applicationScope.launch {
|
||||
with(appDataRepository.settings.getSettings()) {
|
||||
if (isRestoreOnBootEnabled) {
|
||||
val activeTunnels = appDataRepository.tunnels.getActive()
|
||||
val tunState = tunnelService.get().vpnState.value.status
|
||||
if (activeTunnels.isNotEmpty() && tunState != TunnelState.UP) {
|
||||
Timber.i("Starting previously active tunnel")
|
||||
context.startTunnelBackground(activeTunnels.first().id)
|
||||
}
|
||||
if (isAutoTunnelEnabled) {
|
||||
Timber.i("Starting watcher service from boot")
|
||||
ServiceManager.startWatcherServiceForeground(context)
|
||||
}
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isRestoreOnBootEnabled) {
|
||||
appDataRepository.getStartTunnelConfig()?.let {
|
||||
context.startTunnelBackground(it.id)
|
||||
}
|
||||
}
|
||||
if (settings.isAutoTunnelEnabled) {
|
||||
Timber.i("Starting watcher service from boot")
|
||||
serviceManager.startWatcherServiceForeground(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+111
-221
@@ -1,14 +1,11 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
@@ -22,18 +19,11 @@ import com.zaneschepke.wireguardautotunnel.service.notification.NotificationServ
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -43,7 +33,7 @@ import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
|
||||
@AndroidEntryPoint
|
||||
class AutoTunnelService : LifecycleService() {
|
||||
class AutoTunnelService : ForegroundService() {
|
||||
private val foregroundId = 122
|
||||
|
||||
@Inject
|
||||
@@ -72,17 +62,11 @@ class AutoTunnelService : LifecycleService() {
|
||||
@MainImmediateDispatcher
|
||||
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
||||
|
||||
private val autoTunnelStateFlow = MutableStateFlow(AutoTunnelState())
|
||||
private val networkEventsFlow = MutableStateFlow(AutoTunnelState())
|
||||
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
private val tag = this.javaClass.name
|
||||
|
||||
private var wifiJob: Job? = null
|
||||
private var mobileDataJob: Job? = null
|
||||
private var ethernetJob: Job? = null
|
||||
private var pingJob: Job? = null
|
||||
private var networkEventJob: Job? = null
|
||||
|
||||
@get:Synchronized @set:Synchronized
|
||||
private var running: Boolean = false
|
||||
|
||||
override fun onCreate() {
|
||||
@@ -96,26 +80,6 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
super.onBind(intent)
|
||||
// We don't provide binding, so return null
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Timber.d("onStartCommand executed with startId: $startId")
|
||||
if (intent != null) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
Action.START.name,
|
||||
Action.START_FOREGROUND.name,
|
||||
-> startService()
|
||||
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
|
||||
}
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
private suspend fun launchNotification() {
|
||||
if (appDataRepository.settings.getSettings().isAutoTunnelPaused) {
|
||||
launchWatcherPausedNotification()
|
||||
@@ -124,34 +88,27 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun startService() {
|
||||
override fun startService(extras: Bundle?) {
|
||||
super.startService(extras)
|
||||
if (running) return
|
||||
running = true
|
||||
kotlin.runCatching {
|
||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
||||
launchNotification()
|
||||
initWakeLock()
|
||||
}
|
||||
startSettingsJob()
|
||||
startVpnStateJob()
|
||||
startWatcherJob()
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopService() {
|
||||
override fun stopService() {
|
||||
super.stopService()
|
||||
wakeLock?.let {
|
||||
if (it.isHeld) {
|
||||
it.release()
|
||||
}
|
||||
}
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
cancelAndResetNetworkJobs()
|
||||
cancelAndResetPingJob()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
|
||||
@@ -177,7 +134,6 @@ class AutoTunnelService : LifecycleService() {
|
||||
private fun initWakeLock() {
|
||||
wakeLock =
|
||||
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||
val tag = this.javaClass.name
|
||||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
|
||||
try {
|
||||
Timber.i("Initiating wakelock with 10 min timeout")
|
||||
@@ -189,42 +145,48 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun startSettingsJob() = lifecycleScope.launch {
|
||||
watchForSettingsChanges()
|
||||
}
|
||||
|
||||
private fun startVpnStateJob() = lifecycleScope.launch {
|
||||
watchForVpnStateChanges()
|
||||
}
|
||||
|
||||
private fun startWifiJob() = lifecycleScope.launch {
|
||||
watchForWifiConnectivityChanges()
|
||||
}
|
||||
|
||||
private fun startMobileDataJob() = lifecycleScope.launch {
|
||||
watchForMobileDataConnectivityChanges()
|
||||
}
|
||||
|
||||
private fun startEthernetJob() = lifecycleScope.launch {
|
||||
watchForEthernetConnectivityChanges()
|
||||
}
|
||||
|
||||
private fun startPingJob() = lifecycleScope.launch {
|
||||
watchForPingFailure()
|
||||
}
|
||||
|
||||
private fun startNetworkEventJob() = lifecycleScope.launch {
|
||||
handleNetworkEventChanges()
|
||||
private fun startWatcherJob() = lifecycleScope.launch {
|
||||
val setting = appDataRepository.settings.getSettings()
|
||||
launch {
|
||||
Timber.i("Starting wifi watcher")
|
||||
watchForWifiConnectivityChanges()
|
||||
}
|
||||
if (setting.isTunnelOnMobileDataEnabled) {
|
||||
launch {
|
||||
Timber.i("Starting mobile data watcher")
|
||||
watchForMobileDataConnectivityChanges()
|
||||
}
|
||||
}
|
||||
if (setting.isTunnelOnEthernetEnabled) {
|
||||
launch {
|
||||
Timber.i("Starting ethernet data watcher")
|
||||
watchForEthernetConnectivityChanges()
|
||||
}
|
||||
}
|
||||
launch {
|
||||
Timber.i("Starting settings watcher")
|
||||
watchForSettingsChanges()
|
||||
}
|
||||
if (setting.isPingEnabled) {
|
||||
launch {
|
||||
Timber.i("Starting ping watcher")
|
||||
watchForPingFailure()
|
||||
}
|
||||
}
|
||||
launch {
|
||||
Timber.i("Starting management watcher")
|
||||
manageVpn()
|
||||
}
|
||||
running = true
|
||||
}
|
||||
|
||||
private suspend fun watchForMobileDataConnectivityChanges() {
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Starting mobile data watcher")
|
||||
mobileDataService.networkStatus.collect { status ->
|
||||
when (status) {
|
||||
is NetworkStatus.Available -> {
|
||||
Timber.i("Gained Mobile data connection")
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isMobileDataConnected = true,
|
||||
)
|
||||
@@ -232,7 +194,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
|
||||
is NetworkStatus.CapabilitiesChanged -> {
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isMobileDataConnected = true,
|
||||
)
|
||||
@@ -241,7 +203,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
|
||||
is NetworkStatus.Unavailable -> {
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isMobileDataConnected = false,
|
||||
)
|
||||
@@ -255,165 +217,92 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
private suspend fun watchForPingFailure() {
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Starting ping watcher")
|
||||
runCatching {
|
||||
try {
|
||||
do {
|
||||
val vpnState = tunnelService.get().vpnState.value
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (vpnState.status == TunnelState.UP && !settings.isAutoTunnelPaused) {
|
||||
if (vpnState.tunnelConfig != null) {
|
||||
val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick)
|
||||
val results = if (vpnState.tunnelConfig.pingIp != null) {
|
||||
Timber.d("Pinging custom ip : ${vpnState.tunnelConfig.pingIp}")
|
||||
listOf(InetAddress.getByName(vpnState.tunnelConfig.pingIp).isReachable(Constants.PING_TIMEOUT.toInt()))
|
||||
} else {
|
||||
Timber.d("Pinging all peers")
|
||||
if (tunnelService.get().vpnState.value.status == TunnelState.UP) {
|
||||
val tunnelConfig = tunnelService.get().vpnState.value.tunnelConfig
|
||||
tunnelConfig?.let {
|
||||
val config = TunnelConfig.configFromWgQuick(it.wgQuick)
|
||||
val results =
|
||||
config.peers.map { peer ->
|
||||
peer.isReachable()
|
||||
val host =
|
||||
if (peer.endpoint.isPresent &&
|
||||
peer.endpoint.get().resolved.isPresent
|
||||
) {
|
||||
peer.endpoint.get().resolved.get().host
|
||||
} else {
|
||||
Constants.DEFAULT_PING_IP
|
||||
}
|
||||
Timber.i("Checking reachability of: $host")
|
||||
val reachable =
|
||||
InetAddress.getByName(host)
|
||||
.isReachable(Constants.PING_TIMEOUT.toInt())
|
||||
Timber.i("Result: reachable - $reachable")
|
||||
reachable
|
||||
}
|
||||
}
|
||||
Timber.i("Ping results reachable: $results")
|
||||
if (results.contains(false)) {
|
||||
Timber.i("Restarting VPN for ping failure")
|
||||
val cooldown = vpnState.tunnelConfig.pingCooldown
|
||||
tunnelService.get().bounceTunnel(vpnState.tunnelConfig)
|
||||
delay(cooldown ?: Constants.PING_COOLDOWN)
|
||||
continue
|
||||
tunnelService.get().stopTunnel(it)
|
||||
delay(Constants.VPN_RESTART_DELAY)
|
||||
tunnelService.get().startTunnel(it)
|
||||
delay(Constants.PING_COOLDOWN)
|
||||
}
|
||||
}
|
||||
}
|
||||
delay(vpnState.tunnelConfig?.pingInterval ?: Constants.PING_INTERVAL)
|
||||
delay(Constants.PING_INTERVAL)
|
||||
} while (true)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onAutoTunnelPause(paused: Boolean) {
|
||||
if (autoTunnelStateFlow.value.settings.isAutoTunnelPaused
|
||||
!= paused
|
||||
) {
|
||||
when (paused) {
|
||||
true -> launchWatcherPausedNotification()
|
||||
false -> launchWatcherNotification()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun watchForSettingsChanges() {
|
||||
Timber.i("Starting settings watcher")
|
||||
withContext(ioDispatcher) {
|
||||
appDataRepository.settings.getSettingsFlow().combine(
|
||||
// ignore isActive changes to allow manual tunnel overrides
|
||||
appDataRepository.tunnels.getTunnelConfigsFlow().distinctUntilChanged { old, new ->
|
||||
old.map { it.isActive } != new.map { it.isActive }
|
||||
},
|
||||
) { settings, tunnels ->
|
||||
autoTunnelStateFlow.value.copy(
|
||||
settings = settings,
|
||||
tunnels = tunnels,
|
||||
)
|
||||
}.collect {
|
||||
onAutoTunnelPause(it.settings.isAutoTunnelPaused)
|
||||
manageJobsBySettings(it.settings)
|
||||
autoTunnelStateFlow.emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun watchForVpnStateChanges() {
|
||||
Timber.i("Starting vpn state watcher")
|
||||
withContext(ioDispatcher) {
|
||||
tunnelService.get().vpnState.collect { state ->
|
||||
state.tunnelConfig?.let {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (it.isPingEnabled && !settings.isPingEnabled) {
|
||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
||||
}
|
||||
if (!it.isPingEnabled && !settings.isPingEnabled) {
|
||||
cancelAndResetPingJob()
|
||||
}
|
||||
appDataRepository.settings.getSettingsFlow().collect { settings ->
|
||||
if (networkEventsFlow.value.settings.isAutoTunnelPaused
|
||||
!= settings.isAutoTunnelPaused
|
||||
) {
|
||||
when (settings.isAutoTunnelPaused) {
|
||||
true -> launchWatcherPausedNotification()
|
||||
false -> launchWatcherNotification()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun manageJobsBySettings(settings: Settings) {
|
||||
with(settings) {
|
||||
if (isPingEnabled) {
|
||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
||||
} else {
|
||||
cancelAndResetPingJob()
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
settings = settings,
|
||||
)
|
||||
}
|
||||
if (isTunnelOnWifiEnabled || isTunnelOnEthernetEnabled || isTunnelOnMobileDataEnabled) {
|
||||
startNetworkJobs()
|
||||
} else {
|
||||
cancelAndResetNetworkJobs()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startNetworkJobs() {
|
||||
wifiJob.onNotRunning {
|
||||
Timber.i("Wifi job starting")
|
||||
wifiJob = startWifiJob()
|
||||
}
|
||||
ethernetJob.onNotRunning {
|
||||
ethernetJob = startEthernetJob()
|
||||
Timber.i("Ethernet job starting")
|
||||
}
|
||||
mobileDataJob.onNotRunning {
|
||||
mobileDataJob = startMobileDataJob()
|
||||
Timber.i("Mobile data job starting")
|
||||
}
|
||||
networkEventJob.onNotRunning {
|
||||
Timber.i("Network event job starting")
|
||||
networkEventJob = startNetworkEventJob()
|
||||
}
|
||||
}
|
||||
|
||||
private fun cancelAndResetPingJob() {
|
||||
pingJob?.cancelWithMessage("Ping job canceled")
|
||||
pingJob = null
|
||||
}
|
||||
|
||||
private fun cancelAndResetNetworkJobs() {
|
||||
networkEventJob?.cancelWithMessage("Network event job canceled")
|
||||
wifiJob?.cancelWithMessage("Wifi job canceled")
|
||||
ethernetJob?.cancelWithMessage("Ethernet job canceled")
|
||||
mobileDataJob?.cancelWithMessage("Mobile data job canceled")
|
||||
networkEventJob = null
|
||||
wifiJob = null
|
||||
ethernetJob = null
|
||||
mobileDataJob = null
|
||||
}
|
||||
|
||||
private fun updateEthernet(connected: Boolean) {
|
||||
autoTunnelStateFlow.update {
|
||||
it.copy(
|
||||
isEthernetConnected = connected,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun watchForEthernetConnectivityChanges() {
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Starting ethernet data watcher")
|
||||
ethernetService.networkStatus.collect { status ->
|
||||
when (status) {
|
||||
is NetworkStatus.Available -> {
|
||||
Timber.i("Gained Ethernet connection")
|
||||
updateEthernet(true)
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isEthernetConnected = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is NetworkStatus.CapabilitiesChanged -> {
|
||||
Timber.i("Ethernet capabilities changed")
|
||||
updateEthernet(true)
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isEthernetConnected = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is NetworkStatus.Unavailable -> {
|
||||
updateEthernet(false)
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isEthernetConnected = false,
|
||||
)
|
||||
}
|
||||
Timber.i("Lost Ethernet connection")
|
||||
}
|
||||
}
|
||||
@@ -423,12 +312,11 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
private suspend fun watchForWifiConnectivityChanges() {
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Starting wifi watcher")
|
||||
wifiService.networkStatus.collect { status ->
|
||||
when (status) {
|
||||
is NetworkStatus.Available -> {
|
||||
Timber.i("Gained Wi-Fi connection")
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isWifiConnected = true,
|
||||
)
|
||||
@@ -437,7 +325,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
|
||||
is NetworkStatus.CapabilitiesChanged -> {
|
||||
Timber.i("Wifi capabilities changed")
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isWifiConnected = true,
|
||||
)
|
||||
@@ -450,7 +338,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
Timber.i("Detected valid SSID")
|
||||
}
|
||||
appDataRepository.appState.setCurrentSsid(name)
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
currentNetworkSSID = name,
|
||||
)
|
||||
@@ -459,7 +347,7 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
|
||||
is NetworkStatus.Unavailable -> {
|
||||
autoTunnelStateFlow.update {
|
||||
networkEventsFlow.update {
|
||||
it.copy(
|
||||
isWifiConnected = false,
|
||||
)
|
||||
@@ -475,14 +363,17 @@ class AutoTunnelService : LifecycleService() {
|
||||
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
|
||||
}
|
||||
|
||||
private suspend fun getSsidTunnel(ssid: String): TunnelConfig? {
|
||||
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
|
||||
}
|
||||
|
||||
private fun isTunnelDown(): Boolean {
|
||||
return tunnelService.get().vpnState.value.status == TunnelState.DOWN
|
||||
}
|
||||
|
||||
private suspend fun handleNetworkEventChanges() {
|
||||
private suspend fun manageVpn() {
|
||||
withContext(ioDispatcher) {
|
||||
Timber.i("Starting network event watcher")
|
||||
autoTunnelStateFlow.collectLatest { watcherState ->
|
||||
networkEventsFlow.collectLatest { watcherState ->
|
||||
val autoTunnel = "Auto-tunnel watcher"
|
||||
if (!watcherState.settings.isAutoTunnelPaused) {
|
||||
// delay for rapid network state changes and then collect latest
|
||||
@@ -521,14 +412,13 @@ class AutoTunnelService : LifecycleService() {
|
||||
}
|
||||
|
||||
watcherState.isUntrustedWifiConditionMet() -> {
|
||||
Timber.i("Untrusted wifi condition met")
|
||||
if (activeTunnel?.tunnelNetworks?.isMatchingToWildcardList(watcherState.currentNetworkSSID) == false ||
|
||||
activeTunnel == null || isTunnelDown()
|
||||
if (activeTunnel?.tunnelNetworks?.contains(watcherState.currentNetworkSSID) == false ||
|
||||
activeTunnel == null
|
||||
) {
|
||||
Timber.i(
|
||||
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
|
||||
)
|
||||
watcherState.tunnels.firstOrNull { it.tunnelNetworks.isMatchingToWildcardList(watcherState.currentNetworkSSID) }?.let {
|
||||
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
|
||||
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
|
||||
if (isTunnelDown() || activeTunnel?.id != it.id) {
|
||||
tunnelService.get().startTunnel(it)
|
||||
|
||||
+2
-5
@@ -1,8 +1,6 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
||||
|
||||
data class AutoTunnelState(
|
||||
val isWifiConnected: Boolean = false,
|
||||
@@ -10,7 +8,6 @@ data class AutoTunnelState(
|
||||
val isMobileDataConnected: Boolean = false,
|
||||
val currentNetworkSSID: String = "",
|
||||
val settings: Settings = Settings(),
|
||||
val tunnels: TunnelConfigs = emptyList(),
|
||||
) {
|
||||
fun isEthernetConditionMet(): Boolean {
|
||||
return (
|
||||
@@ -41,7 +38,7 @@ data class AutoTunnelState(
|
||||
return (
|
||||
!isEthernetConnected &&
|
||||
isWifiConnected &&
|
||||
!settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID) &&
|
||||
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
||||
settings.isTunnelOnWifiEnabled
|
||||
)
|
||||
}
|
||||
@@ -51,7 +48,7 @@ data class AutoTunnelState(
|
||||
!isEthernetConnected &&
|
||||
(
|
||||
isWifiConnected &&
|
||||
settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID)
|
||||
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import timber.log.Timber
|
||||
|
||||
open class ForegroundService : LifecycleService() {
|
||||
private var isServiceStarted = false
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
super.onBind(intent)
|
||||
// We don't provide binding, so return null
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
Timber.d("onStartCommand executed with startId: $startId")
|
||||
if (intent != null) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
Action.START.name,
|
||||
Action.START_FOREGROUND.name,
|
||||
-> startService(intent.extras)
|
||||
|
||||
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
|
||||
Constants.ALWAYS_ON_VPN_ACTION -> {
|
||||
Timber.i("Always-on VPN starting service")
|
||||
startService(intent.extras)
|
||||
}
|
||||
|
||||
else -> Timber.d("This should never happen. No action in the received intent")
|
||||
}
|
||||
} else {
|
||||
Timber.d(
|
||||
"with a null intent. It has been probably restarted by the system.",
|
||||
)
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
protected open fun startService(extras: Bundle?) {
|
||||
if (isServiceStarted) return
|
||||
Timber.d("Starting ${this.javaClass.simpleName}")
|
||||
isServiceStarted = true
|
||||
}
|
||||
|
||||
protected open fun stopService() {
|
||||
Timber.d("Stopping ${this.javaClass.simpleName}")
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
isServiceStarted = false
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -3,12 +3,10 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import timber.log.Timber
|
||||
|
||||
object ServiceManager {
|
||||
class ServiceManager {
|
||||
private fun <T : Service> actionOnService(action: Action, context: Context, cls: Class<T>, extras: Map<String, Int>? = null) {
|
||||
if (VpnService.prepare(context) != null) return
|
||||
val intent =
|
||||
Intent(context, cls).also {
|
||||
it.action = action.name
|
||||
|
||||
+6
-26
@@ -1,16 +1,14 @@
|
||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||
|
||||
import android.app.Notification
|
||||
import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.lifecycle.LifecycleService
|
||||
import android.os.Bundle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TunnelBackgroundService : LifecycleService() {
|
||||
class TunnelBackgroundService : ForegroundService() {
|
||||
|
||||
@Inject
|
||||
lateinit var notificationService: NotificationService
|
||||
@@ -22,32 +20,14 @@ class TunnelBackgroundService : LifecycleService() {
|
||||
startForeground(foregroundId, createNotification())
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
super.onBind(intent)
|
||||
// We don't provide binding, so return null
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (intent != null) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
Action.START.name,
|
||||
Action.START_FOREGROUND.name,
|
||||
-> startService()
|
||||
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
|
||||
}
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
private fun startService() {
|
||||
override fun startService(extras: Bundle?) {
|
||||
super.startService(extras)
|
||||
startForeground(foregroundId, createNotification())
|
||||
}
|
||||
|
||||
private fun stopService() {
|
||||
override fun stopService() {
|
||||
super.stopService()
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
private fun createNotification(): Notification {
|
||||
|
||||
+4
@@ -12,6 +12,7 @@ import androidx.lifecycle.lifecycleScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -23,6 +24,9 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
||||
@Inject
|
||||
lateinit var appDataRepository: AppDataRepository
|
||||
|
||||
@Inject
|
||||
lateinit var serviceManager: ServiceManager
|
||||
|
||||
@Inject
|
||||
@ApplicationScope
|
||||
lateinit var applicationScope: CoroutineScope
|
||||
|
||||
@@ -9,8 +9,6 @@ interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
||||
|
||||
suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
||||
|
||||
suspend fun bounceTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
||||
|
||||
val vpnState: StateFlow<VpnState>
|
||||
|
||||
suspend fun runningTunnelNames(): Set<String>
|
||||
|
||||
+43
-86
@@ -2,24 +2,25 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||
|
||||
import com.wireguard.android.backend.Backend
|
||||
import com.wireguard.android.backend.Tunnel.State
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
||||
import com.zaneschepke.wireguardautotunnel.module.Userspace
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.amnezia.awg.backend.Tunnel
|
||||
@@ -31,30 +32,14 @@ class WireGuardTunnel
|
||||
@Inject
|
||||
constructor(
|
||||
private val amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
||||
tunnelConfigRepository: TunnelConfigRepository,
|
||||
@Userspace private val userspaceBackend: Provider<Backend>,
|
||||
@Kernel private val kernelBackend: Provider<Backend>,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : TunnelService {
|
||||
|
||||
private val _vpnState = MutableStateFlow(VpnState())
|
||||
override val vpnState: StateFlow<VpnState> = _vpnState.combine(
|
||||
tunnelConfigRepository.getTunnelConfigsFlow(),
|
||||
) {
|
||||
vpnState, tunnels ->
|
||||
vpnState.copy(
|
||||
tunnelConfig = tunnels.firstOrNull { it.id == vpnState.tunnelConfig?.id },
|
||||
)
|
||||
}.stateIn(applicationScope, SharingStarted.Lazily, VpnState())
|
||||
|
||||
private var statsJob: Job? = null
|
||||
|
||||
private suspend fun backend(): Any {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isKernelEnabled) return kernelBackend.get()
|
||||
return amneziaBackend.get()
|
||||
}
|
||||
override val vpnState: StateFlow<VpnState> = _vpnState.asStateFlow()
|
||||
|
||||
override suspend fun runningTunnelNames(): Set<String> {
|
||||
return when (val backend = backend()) {
|
||||
@@ -64,6 +49,8 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private var statsJob: Job? = null
|
||||
|
||||
private suspend fun setState(tunnelConfig: TunnelConfig, tunnelState: TunnelState): Result<TunnelState> {
|
||||
return runCatching {
|
||||
when (val backend = backend()) {
|
||||
@@ -87,76 +74,44 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun backend(): Any {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isKernelEnabled) return kernelBackend.get()
|
||||
if (settings.isAmneziaEnabled) return amneziaBackend.get()
|
||||
return userspaceBackend.get()
|
||||
}
|
||||
|
||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
||||
return withContext(ioDispatcher) {
|
||||
onBeforeStart(tunnelConfig)
|
||||
if (_vpnState.value.status == TunnelState.UP) vpnState.value.tunnelConfig?.let { stopTunnel(it) }
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
appDataRepository.appState.setLastActiveTunnelId(tunnelConfig.id)
|
||||
emitTunnelConfig(tunnelConfig)
|
||||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
||||
emitTunnelState(it)
|
||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
onStartFailed()
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
||||
return withContext(ioDispatcher) {
|
||||
onBeforeStop(tunnelConfig)
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
||||
emitTunnelState(it)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
onStopFailed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// use this when we just want to bounce tunnel and not change tunnelConfig active state
|
||||
override suspend fun bounceTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
||||
toggleTunnel(tunnelConfig)
|
||||
delay(VPN_RESTART_DELAY)
|
||||
return toggleTunnel(tunnelConfig)
|
||||
}
|
||||
|
||||
private suspend fun toggleTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
||||
return withContext(ioDispatcher) {
|
||||
setState(tunnelConfig, TunnelState.TOGGLE).onSuccess {
|
||||
emitTunnelState(it)
|
||||
resetBackendStatistics()
|
||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun onStopFailed() {
|
||||
_vpnState.value.tunnelConfig?.let {
|
||||
appDataRepository.tunnels.save(it.copy(isActive = true))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun onStartFailed() {
|
||||
_vpnState.value.tunnelConfig?.let {
|
||||
appDataRepository.tunnels.save(it.copy(isActive = false))
|
||||
}
|
||||
cancelStatsJob()
|
||||
resetBackendStatistics()
|
||||
}
|
||||
|
||||
private suspend fun onBeforeStart(tunnelConfig: TunnelConfig) {
|
||||
if (_vpnState.value.status == TunnelState.UP) vpnState.value.tunnelConfig?.let { stopTunnel(it) }
|
||||
resetBackendStatistics()
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||
emitVpnStateConfig(tunnelConfig)
|
||||
startStatsJob()
|
||||
}
|
||||
|
||||
private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) {
|
||||
cancelStatsJob()
|
||||
resetBackendStatistics()
|
||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||
}
|
||||
|
||||
private fun emitTunnelState(state: TunnelState) {
|
||||
_vpnState.tryEmit(
|
||||
_vpnState.value.copy(
|
||||
@@ -173,7 +128,7 @@ constructor(
|
||||
)
|
||||
}
|
||||
|
||||
private fun emitVpnStateConfig(tunnelConfig: TunnelConfig) {
|
||||
private fun emitTunnelConfig(tunnelConfig: TunnelConfig?) {
|
||||
_vpnState.tryEmit(
|
||||
_vpnState.value.copy(
|
||||
tunnelConfig = tunnelConfig,
|
||||
@@ -209,9 +164,21 @@ constructor(
|
||||
return _vpnState.value.tunnelConfig?.name ?: ""
|
||||
}
|
||||
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
handleStateChange(TunnelState.from(newState))
|
||||
}
|
||||
|
||||
private fun handleStateChange(state: TunnelState) {
|
||||
emitTunnelState(state)
|
||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
||||
when (state) {
|
||||
TunnelState.UP -> startStatsJob()
|
||||
else -> cancelStatsJob()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTunnelStatisticsJob() = applicationScope.launch(ioDispatcher) {
|
||||
val backend = backend()
|
||||
delay(STATS_START_DELAY)
|
||||
while (true) {
|
||||
when (backend) {
|
||||
is Backend -> emitBackendStatistics(
|
||||
@@ -225,21 +192,11 @@ constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
delay(VPN_STATISTIC_CHECK_INTERVAL)
|
||||
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
emitTunnelState(TunnelState.from(newState))
|
||||
}
|
||||
|
||||
override fun onStateChange(state: State) {
|
||||
emitTunnelState(TunnelState.from(state))
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val STATS_START_DELAY = 5_000L
|
||||
const val VPN_STATISTIC_CHECK_INTERVAL = 1_000L
|
||||
const val VPN_RESTART_DELAY = 1_000L
|
||||
handleStateChange(TunnelState.from(state))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||
|
||||
data class AppUiState(
|
||||
val settings: Settings = Settings(),
|
||||
val tunnels: List<TunnelConfig> = emptyList(),
|
||||
val vpnState: VpnState = VpnState(),
|
||||
val generalState: GeneralState = GeneralState(),
|
||||
val snackbarMessage: String = "",
|
||||
val snackbarMessageConsumed: Boolean = true,
|
||||
val notificationPermissionAccepted: Boolean = false,
|
||||
val requestPermissions: Boolean = false,
|
||||
)
|
||||
|
||||
@@ -2,20 +2,12 @@ package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.navigation.NavHostController
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.plus
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -24,42 +16,33 @@ class AppViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
tunnelService: TunnelService,
|
||||
val navHostController: NavHostController,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _appUiState = MutableStateFlow(AppUiState())
|
||||
private val _appUiState =
|
||||
MutableStateFlow(
|
||||
AppUiState(),
|
||||
)
|
||||
val appUiState = _appUiState.asStateFlow()
|
||||
|
||||
val uiState =
|
||||
combine(
|
||||
appDataRepository.settings.getSettingsFlow(),
|
||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||
tunnelService.vpnState,
|
||||
appDataRepository.appState.generalStateFlow,
|
||||
) { settings, tunnels, tunnelState, generalState ->
|
||||
AppUiState(
|
||||
settings,
|
||||
tunnels,
|
||||
tunnelState,
|
||||
generalState,
|
||||
fun showSnackbarMessage(message: String) {
|
||||
_appUiState.update {
|
||||
it.copy(
|
||||
snackbarMessage = message,
|
||||
snackbarMessageConsumed = false,
|
||||
)
|
||||
}
|
||||
.stateIn(
|
||||
viewModelScope + ioDispatcher,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
_appUiState.value,
|
||||
)
|
||||
|
||||
fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch(ioDispatcher) {
|
||||
_appUiState.emit(
|
||||
_appUiState.value.copy(
|
||||
tunnels = tunnels,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
||||
fun snackbarMessageConsumed() {
|
||||
_appUiState.update {
|
||||
it.copy(
|
||||
snackbarMessage = "",
|
||||
snackbarMessageConsumed = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onPinLockDisabled() = viewModelScope.launch {
|
||||
PinManager.clearPin()
|
||||
appDataRepository.appState.setPinLockEnabled(false)
|
||||
}
|
||||
|
||||
@@ -11,14 +11,13 @@ import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Home
|
||||
import androidx.compose.material.icons.rounded.QuestionMark
|
||||
import androidx.compose.material.icons.rounded.Settings
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarData
|
||||
import androidx.compose.material3.SnackbarDuration
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.SnackbarResult
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.surfaceColorAtElevation
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
@@ -29,25 +28,22 @@ import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusProperties
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.toRoute
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.isCurrentRoute
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.options.OptionsScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.pinlock.PinLockScreen
|
||||
@@ -56,8 +52,10 @@ 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.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -73,133 +71,159 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
val isPinLockEnabled = intent.extras?.getBoolean(SplashActivity.IS_PIN_LOCK_ENABLED_KEY)
|
||||
|
||||
enableEdgeToEdge(
|
||||
navigationBarStyle = SystemBarStyle.auto(
|
||||
lightScrim = Color.Transparent.toArgb(),
|
||||
darkScrim = Color.Transparent.toArgb(),
|
||||
),
|
||||
)
|
||||
enableEdgeToEdge(navigationBarStyle = SystemBarStyle.dark(Color.Transparent.toArgb()))
|
||||
|
||||
setContent {
|
||||
val appViewModel = hiltViewModel<AppViewModel>()
|
||||
val appUiState by appViewModel.uiState.collectAsStateWithLifecycle(lifecycle = this.lifecycle)
|
||||
val navController = appViewModel.navHostController
|
||||
val appUiState by appViewModel.appUiState.collectAsStateWithLifecycle()
|
||||
val navController = rememberNavController()
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
|
||||
LaunchedEffect(appUiState.vpnState.status) {
|
||||
val context = this@MainActivity
|
||||
when (appUiState.vpnState.status) {
|
||||
TunnelState.DOWN -> ServiceManager.stopTunnelBackgroundService(context)
|
||||
else -> Unit
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
|
||||
fun showSnackBarMessage(message: StringValue) {
|
||||
lifecycleScope.launch(Dispatchers.Main) {
|
||||
val result =
|
||||
snackbarHostState.showSnackbar(
|
||||
message = message.asString(this@MainActivity),
|
||||
duration = SnackbarDuration.Short,
|
||||
)
|
||||
when (result) {
|
||||
SnackbarResult.ActionPerformed,
|
||||
SnackbarResult.Dismissed,
|
||||
-> {
|
||||
snackbarHostState.currentSnackbarData?.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
context.requestTunnelTileServiceStateUpdate()
|
||||
}
|
||||
|
||||
SnackbarControllerProvider { host ->
|
||||
WireguardAutoTunnelTheme {
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
Scaffold(
|
||||
snackbarHost = {
|
||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
||||
CustomSnackBar(
|
||||
snackbarData.visuals.message,
|
||||
isRtl = false,
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||
2.dp,
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
containerColor = MaterialTheme.colorScheme.background,
|
||||
modifier =
|
||||
Modifier
|
||||
.focusable()
|
||||
.focusProperties {
|
||||
if (navBackStackEntry?.isCurrentRoute(Route.Lock) == true) {
|
||||
Unit
|
||||
} else {
|
||||
up = focusRequester
|
||||
}
|
||||
},
|
||||
bottomBar = {
|
||||
BottomNavBar(
|
||||
navController,
|
||||
listOf(
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.tunnels),
|
||||
route = Route.Main,
|
||||
icon = Icons.Rounded.Home,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.settings),
|
||||
route = Route.Settings,
|
||||
icon = Icons.Rounded.Settings,
|
||||
),
|
||||
BottomNavItem(
|
||||
name = stringResource(R.string.support),
|
||||
route = Route.Support,
|
||||
icon = Icons.Rounded.QuestionMark,
|
||||
),
|
||||
WireguardAutoTunnelTheme {
|
||||
LaunchedEffect(appUiState.snackbarMessageConsumed) {
|
||||
if (!appUiState.snackbarMessageConsumed) {
|
||||
showSnackBarMessage(StringValue.DynamicString(appUiState.snackbarMessage))
|
||||
appViewModel.snackbarMessageConsumed()
|
||||
}
|
||||
}
|
||||
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
|
||||
Scaffold(
|
||||
snackbarHost = {
|
||||
SnackbarHost(snackbarHostState) { snackbarData: SnackbarData ->
|
||||
CustomSnackBar(
|
||||
snackbarData.visuals.message,
|
||||
isRtl = false,
|
||||
containerColor =
|
||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||
2.dp,
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
containerColor = MaterialTheme.colorScheme.background,
|
||||
modifier =
|
||||
Modifier
|
||||
.focusable()
|
||||
.focusProperties {
|
||||
when (navBackStackEntry?.destination?.route) {
|
||||
Screen.Lock.route -> Unit
|
||||
else -> up = focusRequester
|
||||
}
|
||||
},
|
||||
) { padding ->
|
||||
Surface(modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||
NavHost(
|
||||
navController,
|
||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||
startDestination = (if (isPinLockEnabled == true) Route.Lock else Route.Main),
|
||||
bottomBar = {
|
||||
BottomNavBar(
|
||||
navController,
|
||||
listOf(
|
||||
Screen.Main.navItem,
|
||||
Screen.Settings.navItem,
|
||||
Screen.Support.navItem,
|
||||
),
|
||||
)
|
||||
},
|
||||
) { padding ->
|
||||
Surface(modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||
NavHost(
|
||||
navController,
|
||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||
startDestination = (if (isPinLockEnabled == true) Screen.Lock.route else Screen.Main.route),
|
||||
) {
|
||||
composable(
|
||||
Screen.Main.route,
|
||||
) {
|
||||
composable<Route.Main> {
|
||||
MainScreen(
|
||||
focusRequester = focusRequester,
|
||||
uiState = appUiState,
|
||||
navController = navController,
|
||||
MainScreen(
|
||||
focusRequester = focusRequester,
|
||||
appViewModel = appViewModel,
|
||||
navController = navController,
|
||||
)
|
||||
}
|
||||
composable(
|
||||
Screen.Settings.route,
|
||||
) {
|
||||
SettingsScreen(
|
||||
appViewModel = appViewModel,
|
||||
navController = navController,
|
||||
focusRequester = focusRequester,
|
||||
)
|
||||
}
|
||||
composable(
|
||||
Screen.Support.route,
|
||||
) {
|
||||
SupportScreen(
|
||||
focusRequester = focusRequester,
|
||||
navController = navController,
|
||||
)
|
||||
}
|
||||
composable(Screen.Support.Logs.route) {
|
||||
LogsScreen()
|
||||
}
|
||||
composable(
|
||||
"${Screen.Config.route}/{id}?configType={configType}",
|
||||
arguments =
|
||||
listOf(
|
||||
navArgument("id") {
|
||||
type = NavType.StringType
|
||||
defaultValue = "0"
|
||||
},
|
||||
navArgument("configType") {
|
||||
type = NavType.StringType
|
||||
defaultValue = ConfigType.WIREGUARD.name
|
||||
},
|
||||
),
|
||||
) {
|
||||
val id = it.arguments?.getString("id")
|
||||
val configType =
|
||||
ConfigType.valueOf(
|
||||
it.arguments?.getString("configType") ?: ConfigType.WIREGUARD.name,
|
||||
)
|
||||
}
|
||||
composable<Route.Settings> {
|
||||
SettingsScreen(
|
||||
appViewModel = appViewModel,
|
||||
uiState = appUiState,
|
||||
navController = navController,
|
||||
focusRequester = focusRequester,
|
||||
)
|
||||
}
|
||||
composable<Route.Support> {
|
||||
SupportScreen(
|
||||
focusRequester = focusRequester,
|
||||
navController = navController,
|
||||
appUiState = appUiState,
|
||||
)
|
||||
}
|
||||
composable<Route.Logs> {
|
||||
LogsScreen()
|
||||
}
|
||||
composable<Route.Config> {
|
||||
val args = it.toRoute<Route.Config>()
|
||||
if (!id.isNullOrBlank()) {
|
||||
ConfigScreen(
|
||||
navController = navController,
|
||||
tunnelId = id,
|
||||
appViewModel = appViewModel,
|
||||
focusRequester = focusRequester,
|
||||
tunnelId = args.id,
|
||||
configType = configType,
|
||||
)
|
||||
}
|
||||
composable<Route.Option> {
|
||||
val args = it.toRoute<Route.Option>()
|
||||
}
|
||||
composable("${Screen.Option.route}/{id}") {
|
||||
val id = it.arguments?.getString("id")
|
||||
if (!id.isNullOrBlank()) {
|
||||
OptionsScreen(
|
||||
navController = navController,
|
||||
tunnelId = args.id,
|
||||
focusRequester = focusRequester,
|
||||
appUiState = appUiState,
|
||||
)
|
||||
}
|
||||
composable<Route.Lock> {
|
||||
PinLockScreen(
|
||||
navController = navController,
|
||||
tunnelId = id,
|
||||
appViewModel = appViewModel,
|
||||
focusRequester = focusRequester,
|
||||
)
|
||||
}
|
||||
}
|
||||
composable(Screen.Lock.route) {
|
||||
PinLockScreen(
|
||||
navController = navController,
|
||||
appViewModel = appViewModel,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
sealed class Route {
|
||||
@Serializable
|
||||
data object Support : Route()
|
||||
|
||||
@Serializable
|
||||
data object Settings : Route()
|
||||
|
||||
@Serializable
|
||||
data object Main : Route()
|
||||
|
||||
@Serializable
|
||||
data class Option(
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@Serializable
|
||||
data object Lock : Route()
|
||||
|
||||
@Serializable
|
||||
data class Config(
|
||||
val id: Int,
|
||||
) : Route()
|
||||
|
||||
@Serializable
|
||||
data object Logs : Route()
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Home
|
||||
import androidx.compose.material.icons.rounded.QuestionMark
|
||||
import androidx.compose.material.icons.rounded.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
||||
|
||||
sealed class Screen(val route: String) {
|
||||
data object Main : Screen("main") {
|
||||
val navItem =
|
||||
BottomNavItem(
|
||||
name = WireGuardAutoTunnel.instance.getString(R.string.tunnels),
|
||||
route = route,
|
||||
icon = Icons.Rounded.Home,
|
||||
)
|
||||
}
|
||||
|
||||
data object Settings : Screen("settings") {
|
||||
val navItem =
|
||||
BottomNavItem(
|
||||
name = WireGuardAutoTunnel.instance.getString(R.string.settings),
|
||||
route = route,
|
||||
icon = Icons.Rounded.Settings,
|
||||
)
|
||||
}
|
||||
|
||||
data object Support : Screen("support") {
|
||||
val navItem =
|
||||
BottomNavItem(
|
||||
name = WireGuardAutoTunnel.instance.getString(R.string.support),
|
||||
route = route,
|
||||
icon = Icons.Rounded.QuestionMark,
|
||||
)
|
||||
|
||||
data object Logs : Screen("support/logs")
|
||||
}
|
||||
|
||||
data object Config : Screen("config")
|
||||
|
||||
data object Lock : Screen("lock")
|
||||
|
||||
data object Option : Screen("option")
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
@@ -17,8 +16,8 @@ import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
import javax.inject.Inject
|
||||
@@ -36,7 +35,8 @@ class SplashActivity : ComponentActivity() {
|
||||
@Inject
|
||||
lateinit var tunnelService: Provider<TunnelService>
|
||||
|
||||
private val appViewModel: AppViewModel by viewModels()
|
||||
@Inject
|
||||
lateinit var serviceManager: ServiceManager
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
@@ -47,28 +47,16 @@ class SplashActivity : ComponentActivity() {
|
||||
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.CREATED) {
|
||||
val pinLockEnabled = async {
|
||||
appStateRepository.isPinLockEnabled().also {
|
||||
if (it) PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||
}
|
||||
}.await()
|
||||
async {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isAutoTunnelEnabled) ServiceManager.startWatcherService(application.applicationContext)
|
||||
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
|
||||
val activeTunnels = appDataRepository.tunnels.getActive()
|
||||
if (activeTunnels.isNotEmpty() &&
|
||||
tunnelService.get().getState() == TunnelState.DOWN
|
||||
) {
|
||||
tunnelService.get().startTunnel(activeTunnels.first())
|
||||
}
|
||||
}.await()
|
||||
|
||||
async {
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
appViewModel.setTunnels(tunnels)
|
||||
}.await()
|
||||
|
||||
val pinLockEnabled = appStateRepository.isPinLockEnabled()
|
||||
if (pinLockEnabled) {
|
||||
PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||
}
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
if (settings.isAutoTunnelEnabled) serviceManager.startWatcherService(application.applicationContext)
|
||||
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
|
||||
val tunnels = appDataRepository.tunnels.getActive()
|
||||
if (tunnels.isNotEmpty() && tunnelService.get().getState() == TunnelState.DOWN) tunnelService.get().startTunnel(tunnels.first())
|
||||
requestTunnelTileServiceStateUpdate()
|
||||
requestAutoTunnelTileServiceUpdate()
|
||||
|
||||
val intent =
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
|
||||
@Composable
|
||||
fun ClickableIconButton(onClick: () -> Unit, onIconClick: () -> Unit, text: String, icon: ImageVector, enabled: Boolean = true) {
|
||||
fun ClickableIconButton(onClick: () -> Unit, onIconClick: () -> Unit, text: String, icon: ImageVector, enabled: Boolean) {
|
||||
TextButton(
|
||||
onClick = onClick,
|
||||
enabled = enabled,
|
||||
|
||||
+6
-13
@@ -1,6 +1,5 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
@@ -15,29 +14,23 @@ fun ConfigurationTextBox(
|
||||
value: String,
|
||||
hint: String,
|
||||
onValueChange: (String) -> Unit,
|
||||
keyboardActions: KeyboardActions = KeyboardActions(),
|
||||
keyboardActions: KeyboardActions,
|
||||
label: String,
|
||||
modifier: Modifier,
|
||||
isError: Boolean = false,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
trailing: @Composable () -> Unit = {},
|
||||
interactionSource: MutableInteractionSource? = null,
|
||||
) {
|
||||
OutlinedTextField(
|
||||
isError = isError,
|
||||
modifier = modifier,
|
||||
value = value,
|
||||
singleLine = true,
|
||||
interactionSource = interactionSource,
|
||||
onValueChange = { onValueChange(it) },
|
||||
label = { Text(label) },
|
||||
maxLines = 1,
|
||||
placeholder = { Text(hint) },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardOptions =
|
||||
KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
keyboardActions = keyboardActions,
|
||||
trailingIcon = trailing,
|
||||
)
|
||||
}
|
||||
|
||||
+2
-9
@@ -13,14 +13,7 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.Dp
|
||||
|
||||
@Composable
|
||||
fun ConfigurationToggle(
|
||||
label: String,
|
||||
enabled: Boolean = true,
|
||||
checked: Boolean,
|
||||
padding: Dp,
|
||||
onCheckChanged: (checked: Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
fun ConfigurationToggle(label: String, enabled: Boolean, checked: Boolean, padding: Dp, onCheckChanged: () -> Unit, modifier: Modifier = Modifier) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
@@ -44,7 +37,7 @@ fun ConfigurationToggle(
|
||||
modifier = modifier,
|
||||
enabled = enabled,
|
||||
checked = checked,
|
||||
onCheckedChange = { onCheckChanged(it) },
|
||||
onCheckedChange = { onCheckChanged() },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.interaction.collectIsFocusedAsState
|
||||
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.outlined.Save
|
||||
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.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.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
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.KeyboardCapitalization
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun SubmitConfigurationTextBox(
|
||||
value: String?,
|
||||
label: String,
|
||||
hint: String,
|
||||
focusRequester: FocusRequester,
|
||||
isErrorValue: (value: String?) -> Boolean,
|
||||
onSubmit: (value: String) -> Unit,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
) {
|
||||
val focusManager = LocalFocusManager.current
|
||||
val interactionSource = remember { MutableInteractionSource() }
|
||||
val isFocused by interactionSource.collectIsFocusedAsState()
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
|
||||
var stateValue by remember { mutableStateOf(value ?: "") }
|
||||
|
||||
OutlinedTextField(
|
||||
isError = isErrorValue(stateValue),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.focusRequester(focusRequester),
|
||||
value = stateValue,
|
||||
singleLine = true,
|
||||
interactionSource = interactionSource,
|
||||
onValueChange = { stateValue = it },
|
||||
label = { Text(label) },
|
||||
maxLines = 1,
|
||||
placeholder = { Text(hint) },
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = KeyboardActions(
|
||||
onDone = {
|
||||
onSubmit(stateValue)
|
||||
keyboardController?.hide()
|
||||
},
|
||||
),
|
||||
trailingIcon = {
|
||||
if (!isErrorValue(stateValue) && isFocused) {
|
||||
IconButton(onClick = {
|
||||
onSubmit(stateValue)
|
||||
keyboardController?.hide()
|
||||
focusManager.clearFocus()
|
||||
}) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Save,
|
||||
contentDescription = stringResource(R.string.save_changes),
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
+4
-9
@@ -12,8 +12,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
|
||||
@@ -22,22 +20,19 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavIte
|
||||
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
|
||||
showBottomBar = bottomNavItems.firstOrNull {
|
||||
navBackStackEntry?.destination?.hierarchy?.any { dest ->
|
||||
bottomNavItems.map { dest.hasRoute(route = it.route::class) }.contains(true)
|
||||
} == true
|
||||
} != null
|
||||
showBottomBar = bottomNavItems.firstOrNull { navBackStackEntry?.destination?.route?.contains(it.route) == true } != null
|
||||
|
||||
if (showBottomBar) {
|
||||
NavigationBar(
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
) {
|
||||
bottomNavItems.forEach { item ->
|
||||
val selected = navBackStackEntry.isCurrentRoute(item.route)
|
||||
val selected = navBackStackEntry?.destination?.route?.contains(item.route) == true
|
||||
|
||||
NavigationBarItem(
|
||||
selected = selected,
|
||||
onClick = {
|
||||
if (selected) return@NavigationBarItem
|
||||
if (navBackStackEntry?.destination?.route == item.route) return@NavigationBarItem
|
||||
navController.navigate(item.route) {
|
||||
// Pop up to the start destination of the graph to
|
||||
// avoid building up a large stack of destinations
|
||||
|
||||
+1
-2
@@ -1,10 +1,9 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
|
||||
data class BottomNavItem(
|
||||
val name: String,
|
||||
val route: Route,
|
||||
val route: String,
|
||||
val icon: ImageVector,
|
||||
)
|
||||
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||
|
||||
import androidx.navigation.NavBackStackEntry
|
||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
|
||||
fun NavBackStackEntry?.isCurrentRoute(route: Route): Boolean {
|
||||
return this?.destination?.hierarchy?.any {
|
||||
it.hasRoute(route = route::class)
|
||||
} == true
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||
|
||||
import android.content.Context
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.ComposeNavigator
|
||||
import androidx.navigation.compose.DialogNavigator
|
||||
|
||||
class NavigationService constructor(
|
||||
context: Context,
|
||||
) {
|
||||
val navController = NavHostController(context).apply {
|
||||
navigatorProvider.addNavigator(ComposeNavigator())
|
||||
navigatorProvider.addNavigator(DialogNavigator())
|
||||
}
|
||||
}
|
||||
-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)
|
||||
+278
-61
@@ -1,27 +1,36 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.compose.foundation.Image
|
||||
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.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
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.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
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.rounded.Android
|
||||
import androidx.compose.material.icons.rounded.ContentCopy
|
||||
import androidx.compose.material.icons.rounded.Delete
|
||||
import androidx.compose.material.icons.rounded.Refresh
|
||||
import androidx.compose.material.icons.rounded.Save
|
||||
import androidx.compose.material3.BasicAlertDialog
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FabPosition
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -30,8 +39,10 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.surfaceColorAtElevation
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -42,6 +53,7 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.platform.ClipboardManager
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -55,37 +67,47 @@ import androidx.compose.ui.text.input.VisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavController
|
||||
import com.google.accompanist.drawablepainter.DrawablePainter
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.components.ApplicationSelectionDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||
@OptIn(
|
||||
ExperimentalMaterial3Api::class,
|
||||
)
|
||||
@Composable
|
||||
fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
||||
factory.create(tunnelId)
|
||||
}
|
||||
|
||||
fun ConfigScreen(
|
||||
viewModel: ConfigViewModel = hiltViewModel(),
|
||||
focusRequester: FocusRequester,
|
||||
navController: NavController,
|
||||
appViewModel: AppViewModel,
|
||||
tunnelId: String,
|
||||
configType: ConfigType,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val snackbar = SnackbarController.current
|
||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
var showApplicationsDialog by remember { mutableStateOf(false) }
|
||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||
var isAuthenticated by remember { mutableStateOf(false) }
|
||||
var configType by remember { mutableStateOf(ConfigType.WIREGUARD) }
|
||||
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
LaunchedEffect(Unit) { viewModel.init(tunnelId) }
|
||||
|
||||
LaunchedEffect(uiState.loading) {
|
||||
if (!uiState.loading && context.isRunningOnTv()) {
|
||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||
kotlin.runCatching {
|
||||
@@ -97,12 +119,13 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
delay(2_000L)
|
||||
viewModel.cleanUpUninstalledApps()
|
||||
if (uiState.loading) {
|
||||
LoadingScreen()
|
||||
return
|
||||
}
|
||||
|
||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||
|
||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||
|
||||
val fillMaxHeight = .85f
|
||||
@@ -137,13 +160,13 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
},
|
||||
onError = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.error_authentication_failed),
|
||||
)
|
||||
},
|
||||
onFailure = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.error_authorization_failed),
|
||||
)
|
||||
},
|
||||
@@ -151,19 +174,182 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
}
|
||||
|
||||
if (showApplicationsDialog) {
|
||||
ApplicationSelectionDialog(viewModel, uiState) {
|
||||
showApplicationsDialog = false
|
||||
val sortedPackages =
|
||||
remember(uiState.packages) {
|
||||
uiState.packages.sortedBy { viewModel.getPackageLabel(it) }
|
||||
}
|
||||
BasicAlertDialog(onDismissRequest = { showApplicationsDialog = false }) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight(if (uiState.isAllApplicationsEnabled) 1 / 5f else 4 / 5f),
|
||||
) {
|
||||
Column(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.tunnel_all))
|
||||
Switch(
|
||||
checked = uiState.isAllApplicationsEnabled,
|
||||
onCheckedChange = { viewModel.onAllApplicationsChange(it) },
|
||||
)
|
||||
}
|
||||
if (!uiState.isAllApplicationsEnabled) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.include))
|
||||
Checkbox(
|
||||
checked = uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.exclude))
|
||||
Checkbox(
|
||||
checked = !uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
SearchBar(viewModel::emitQueriedPackages)
|
||||
}
|
||||
Spacer(Modifier.padding(5.dp))
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.fillMaxHeight(4 / 5f),
|
||||
) {
|
||||
items(sortedPackages, key = { it.packageName }) { pack ->
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(5.dp),
|
||||
) {
|
||||
Row(modifier = Modifier.fillMaxWidth(fillMaxWidth)) {
|
||||
val drawable =
|
||||
pack.applicationInfo?.loadIcon(context.packageManager)
|
||||
if (drawable != null) {
|
||||
Image(
|
||||
painter = DrawablePainter(drawable),
|
||||
stringResource(id = R.string.icon),
|
||||
modifier = Modifier.size(50.dp, 50.dp),
|
||||
)
|
||||
} else {
|
||||
val icon = Icons.Rounded.Android
|
||||
Icon(
|
||||
icon,
|
||||
icon.name,
|
||||
modifier = Modifier.size(50.dp, 50.dp),
|
||||
)
|
||||
}
|
||||
Text(
|
||||
viewModel.getPackageLabel(pack),
|
||||
modifier = Modifier.padding(5.dp),
|
||||
)
|
||||
}
|
||||
Checkbox(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
checked =
|
||||
(
|
||||
uiState.checkedPackageNames.contains(
|
||||
pack.packageName,
|
||||
)
|
||||
),
|
||||
onCheckedChange = {
|
||||
if (it) {
|
||||
viewModel.onAddCheckedPackage(pack.packageName)
|
||||
} else {
|
||||
viewModel.onRemoveCheckedPackage(pack.packageName)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = { showApplicationsDialog = false }) {
|
||||
Text(stringResource(R.string.done))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
floatingActionButtonPosition = FabPosition.End,
|
||||
floatingActionButton = {
|
||||
val secondaryColor = MaterialTheme.colorScheme.secondary
|
||||
val hoverColor = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)
|
||||
var fobColor by remember { mutableStateOf(secondaryColor) }
|
||||
FloatingActionButton(
|
||||
onClick = {
|
||||
viewModel.onSaveAllChanges()
|
||||
modifier =
|
||||
Modifier.onFocusChanged {
|
||||
if (context.isRunningOnTv()) {
|
||||
fobColor = if (it.isFocused) hoverColor else secondaryColor
|
||||
}
|
||||
},
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
onClick = {
|
||||
viewModel.onSaveAllChanges(configType).onSuccess {
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.config_changes_saved),
|
||||
)
|
||||
navController.navigate(Screen.Main.route)
|
||||
}.onFailure {
|
||||
appViewModel.showSnackbarMessage(it.getMessage(context))
|
||||
}
|
||||
},
|
||||
containerColor = fobColor,
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
) {
|
||||
Icon(
|
||||
@@ -213,16 +399,9 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
stringResource(R.string.interface_),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.show_amnezia_properties),
|
||||
checked = configType == ConfigType.AMNEZIA,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
||||
modifier = Modifier.focusRequester(focusRequester),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.tunnelName,
|
||||
onValueChange = viewModel::onTunnelNameChange,
|
||||
onValueChange = { value -> viewModel.onTunnelNameChange(value) },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.name),
|
||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||
@@ -238,12 +417,12 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
.clickable { showAuthPrompt = true },
|
||||
value = uiState.interfaceProxy.privateKey,
|
||||
visualTransformation =
|
||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated) {
|
||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated) {
|
||||
VisualTransformation.None
|
||||
} else {
|
||||
PasswordVisualTransformation()
|
||||
},
|
||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated,
|
||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated,
|
||||
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
@@ -293,29 +472,31 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.addresses,
|
||||
onValueChange = viewModel::onAddressesChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.addresses),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.listenPort,
|
||||
onValueChange = viewModel::onListenPortChanged,
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.listen_port),
|
||||
hint = stringResource(R.string.random),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.addresses,
|
||||
onValueChange = { value -> viewModel.onAddressesChanged(value) },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.addresses),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(3 / 5f)
|
||||
.padding(end = 5.dp),
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.listenPort,
|
||||
onValueChange = { value -> viewModel.onListenPortChanged(value) },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.listen_port),
|
||||
hint = stringResource(R.string.random),
|
||||
modifier = Modifier.width(IntrinsicSize.Min),
|
||||
)
|
||||
}
|
||||
Row(modifier = Modifier.fillMaxWidth()) {
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.dnsServers,
|
||||
onValueChange = viewModel::onDnsServersChanged,
|
||||
onValueChange = { value -> viewModel.onDnsServersChanged(value) },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.dns_servers),
|
||||
hint = stringResource(R.string.comma_separated_list),
|
||||
@@ -326,7 +507,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.mtu,
|
||||
onValueChange = viewModel::onMtuChanged,
|
||||
onValueChange = { value -> viewModel.onMtuChanged(value) },
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.mtu),
|
||||
hint = stringResource(R.string.auto),
|
||||
@@ -336,7 +517,10 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
if (configType == ConfigType.AMNEZIA) {
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketCount,
|
||||
onValueChange = viewModel::onJunkPacketCountChanged,
|
||||
onValueChange = {
|
||||
value ->
|
||||
viewModel.onJunkPacketCountChanged(value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_count),
|
||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
||||
@@ -347,7 +531,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketMinSize,
|
||||
onValueChange = viewModel::onJunkPacketMinSizeChanged,
|
||||
onValueChange = { value ->
|
||||
viewModel.onJunkPacketMinSizeChanged(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_minimum_size),
|
||||
hint =
|
||||
@@ -361,7 +549,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.junkPacketMaxSize,
|
||||
onValueChange = viewModel::onJunkPacketMaxSizeChanged,
|
||||
onValueChange = { value ->
|
||||
viewModel.onJunkPacketMaxSizeChanged(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.junk_packet_maximum_size),
|
||||
hint =
|
||||
@@ -375,7 +567,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.initPacketJunkSize,
|
||||
onValueChange = viewModel::onInitPacketJunkSizeChanged,
|
||||
onValueChange = { value ->
|
||||
viewModel.onInitPacketJunkSizeChanged(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_junk_size),
|
||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
||||
@@ -386,7 +582,10 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.responsePacketJunkSize,
|
||||
onValueChange = viewModel::onResponsePacketJunkSize,
|
||||
onValueChange = {
|
||||
value ->
|
||||
viewModel.onResponsePacketJunkSize(value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_junk_size),
|
||||
hint =
|
||||
@@ -400,7 +599,10 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.initPacketMagicHeader,
|
||||
onValueChange = viewModel::onInitPacketMagicHeader,
|
||||
onValueChange = {
|
||||
value ->
|
||||
viewModel.onInitPacketMagicHeader(value)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.init_packet_magic_header),
|
||||
hint =
|
||||
@@ -414,7 +616,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
||||
onValueChange = viewModel::onResponsePacketMagicHeader,
|
||||
onValueChange = { value ->
|
||||
viewModel.onResponsePacketMagicHeader(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.response_packet_magic_header),
|
||||
hint =
|
||||
@@ -428,7 +634,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
||||
onValueChange = viewModel::onUnderloadPacketMagicHeader,
|
||||
onValueChange = { value ->
|
||||
viewModel.onUnderloadPacketMagicHeader(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.underload_packet_magic_header),
|
||||
hint =
|
||||
@@ -442,7 +652,11 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
)
|
||||
ConfigurationTextBox(
|
||||
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
||||
onValueChange = viewModel::onTransportPacketMagicHeader,
|
||||
onValueChange = { value ->
|
||||
viewModel.onTransportPacketMagicHeader(
|
||||
value,
|
||||
)
|
||||
},
|
||||
keyboardActions = keyboardActions,
|
||||
label = stringResource(R.string.transport_packet_magic_header),
|
||||
hint =
|
||||
@@ -600,6 +814,9 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (context.isRunningOnTv()) {
|
||||
Spacer(modifier = Modifier.weight(.17f))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-9
@@ -15,7 +15,7 @@ data class ConfigUiState(
|
||||
val isAllApplicationsEnabled: Boolean = false,
|
||||
val loading: Boolean = true,
|
||||
val tunnel: TunnelConfig? = null,
|
||||
var tunnelName: String = "",
|
||||
val tunnelName: String = "",
|
||||
val isAmneziaEnabled: Boolean = false,
|
||||
) {
|
||||
companion object {
|
||||
@@ -45,6 +45,7 @@ data class ConfigUiState(
|
||||
}
|
||||
|
||||
fun from(config: org.amnezia.awg.config.Config): ConfigUiState {
|
||||
// TODO update with new values
|
||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
||||
var include = true
|
||||
@@ -68,13 +69,5 @@ data class ConfigUiState(
|
||||
isAllApplicationsEnabled,
|
||||
)
|
||||
}
|
||||
|
||||
fun from(tunnel: TunnelConfig): ConfigUiState {
|
||||
val config = tunnel.toAmConfig()
|
||||
return from(config).copy(
|
||||
tunnelName = tunnel.name,
|
||||
tunnel = tunnel,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+192
-216
@@ -6,7 +6,6 @@ import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.navigation.NavHostController
|
||||
import com.wireguard.config.Config
|
||||
import com.wireguard.config.Interface
|
||||
import com.wireguard.config.Peer
|
||||
@@ -16,116 +15,100 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.removeAt
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.update
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.plus
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel(assistedFactory = ConfigViewModel.ConfigViewModelFactory::class)
|
||||
@HiltViewModel
|
||||
class ConfigViewModel
|
||||
@AssistedInject
|
||||
@Inject
|
||||
constructor(
|
||||
private val settingsRepository: SettingsRepository,
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val navController: NavHostController,
|
||||
@Assisted val id: Int,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
private val packageManager = WireGuardAutoTunnel.instance.packageManager
|
||||
|
||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||
val uiState = _uiState.onStart {
|
||||
appDataRepository.tunnels.getById(id)?.let {
|
||||
val packages = getQueriedPackages()
|
||||
_uiState.value = ConfigUiState.from(it).copy(
|
||||
packages = packages,
|
||||
)
|
||||
}
|
||||
}.stateIn(
|
||||
viewModelScope + ioDispatcher,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
ConfigUiState(),
|
||||
)
|
||||
val uiState = _uiState.asStateFlow()
|
||||
|
||||
fun init(tunnelId: String) = viewModelScope.launch(ioDispatcher) {
|
||||
val packages = getQueriedPackages("")
|
||||
val state =
|
||||
if (tunnelId != Constants.MANUAL_TUNNEL_CONFIG_ID) {
|
||||
val tunnelConfig =
|
||||
appDataRepository.tunnels.getAll()
|
||||
.firstOrNull { it.id.toString() == tunnelId }
|
||||
val isAmneziaEnabled = settingsRepository.getSettings().isAmneziaEnabled
|
||||
if (tunnelConfig != null) {
|
||||
(
|
||||
if (isAmneziaEnabled) {
|
||||
val amConfig =
|
||||
if (tunnelConfig.amQuick == "") tunnelConfig.wgQuick else tunnelConfig.amQuick
|
||||
ConfigUiState.from(TunnelConfig.configFromAmQuick(amConfig))
|
||||
} else {
|
||||
ConfigUiState.from(
|
||||
TunnelConfig.configFromWgQuick(tunnelConfig.wgQuick),
|
||||
)
|
||||
}
|
||||
).copy(
|
||||
packages = packages,
|
||||
loading = false,
|
||||
tunnel = tunnelConfig,
|
||||
tunnelName = tunnelConfig.name,
|
||||
isAmneziaEnabled = isAmneziaEnabled,
|
||||
)
|
||||
} else {
|
||||
ConfigUiState(loading = false, packages = packages)
|
||||
}
|
||||
} else {
|
||||
ConfigUiState(loading = false, packages = packages)
|
||||
}
|
||||
_uiState.value = state
|
||||
}
|
||||
|
||||
fun onTunnelNameChange(name: String) {
|
||||
_uiState.update {
|
||||
it.copy(tunnelName = name)
|
||||
}
|
||||
_uiState.value = _uiState.value.copy(tunnelName = name)
|
||||
}
|
||||
|
||||
fun onIncludeChange(include: Boolean) {
|
||||
_uiState.update {
|
||||
it.copy(include = include)
|
||||
}
|
||||
}
|
||||
|
||||
fun cleanUpUninstalledApps() = viewModelScope.launch(ioDispatcher) {
|
||||
uiState.value.tunnel?.let {
|
||||
val config = it.toAmConfig()
|
||||
val packages = getQueriedPackages()
|
||||
val packageSet = packages.map { pack -> pack.packageName }.toSet()
|
||||
val includedApps = config.`interface`.includedApplications.toMutableList()
|
||||
val excludedApps = config.`interface`.excludedApplications.toMutableList()
|
||||
if (includedApps.isEmpty() && excludedApps.isEmpty()) return@launch
|
||||
if (includedApps.retainAll(packageSet) || excludedApps.retainAll(packageSet)) {
|
||||
Timber.i("Removing split tunnel package name that no longer exists on the device")
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
checkedPackageNames = if (_uiState.value.include) includedApps else excludedApps,
|
||||
)
|
||||
}
|
||||
val wgQuick = buildConfig().toWgQuickString(true)
|
||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
||||
saveConfig(
|
||||
it.copy(
|
||||
amQuick = amQuick,
|
||||
wgQuick = wgQuick,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
_uiState.value = _uiState.value.copy(include = include)
|
||||
}
|
||||
|
||||
fun onAddCheckedPackage(packageName: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
checkedPackageNames = it.checkedPackageNames + packageName,
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
checkedPackageNames = _uiState.value.checkedPackageNames + packageName,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
||||
_uiState.update {
|
||||
it.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
||||
}
|
||||
_uiState.value = _uiState.value.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
||||
}
|
||||
|
||||
fun onRemoveCheckedPackage(packageName: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
checkedPackageNames = it.checkedPackageNames - packageName,
|
||||
_uiState.value =
|
||||
_uiState.value.copy(
|
||||
checkedPackageNames = _uiState.value.checkedPackageNames - packageName,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getQueriedPackages(query: String = ""): List<PackageInfo> {
|
||||
private fun getQueriedPackages(query: String): List<PackageInfo> {
|
||||
return getAllInternetCapablePackages().filter {
|
||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||
}
|
||||
@@ -154,9 +137,7 @@ constructor(
|
||||
return _uiState.value.isAllApplicationsEnabled
|
||||
}
|
||||
|
||||
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(tunnelConfig)
|
||||
}
|
||||
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch { appDataRepository.tunnels.save(tunnelConfig) }
|
||||
|
||||
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
||||
if (tunnelConfig != null) {
|
||||
@@ -193,113 +174,105 @@ constructor(
|
||||
}
|
||||
|
||||
private fun emptyCheckedPackagesList() {
|
||||
_uiState.update {
|
||||
it.copy(checkedPackageNames = emptyList())
|
||||
}
|
||||
_uiState.value = _uiState.value.copy(checkedPackageNames = emptyList())
|
||||
}
|
||||
|
||||
private fun buildInterfaceListFromProxyInterface(): Interface {
|
||||
val builder = Interface.Builder()
|
||||
with(_uiState.value.interfaceProxy) {
|
||||
builder.parsePrivateKey(this.privateKey.trim())
|
||||
builder.parseAddresses(this.addresses.trim())
|
||||
if (this.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(this.dnsServers.trim())
|
||||
}
|
||||
if (this.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(this.mtu.trim())
|
||||
}
|
||||
if (this.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(this.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
builder.parsePrivateKey(_uiState.value.interfaceProxy.privateKey.trim())
|
||||
builder.parseAddresses(_uiState.value.interfaceProxy.addresses.trim())
|
||||
if (_uiState.value.interfaceProxy.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(_uiState.value.interfaceProxy.dnsServers.trim())
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(_uiState.value.interfaceProxy.mtu.trim())
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(_uiState.value.interfaceProxy.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
private fun buildAmInterfaceListFromProxyInterface(): org.amnezia.awg.config.Interface {
|
||||
val builder = org.amnezia.awg.config.Interface.Builder()
|
||||
with(_uiState.value.interfaceProxy) {
|
||||
builder.parsePrivateKey(this.privateKey.trim())
|
||||
builder.parseAddresses(this.addresses.trim())
|
||||
if (this.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(this.dnsServers.trim())
|
||||
}
|
||||
if (this.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(this.mtu.trim())
|
||||
}
|
||||
if (this.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(this.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (this.junkPacketCount.isNotEmpty()) {
|
||||
builder.setJunkPacketCount(
|
||||
this.junkPacketCount.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.junkPacketMinSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMinSize(
|
||||
this.junkPacketMinSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.junkPacketMaxSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMaxSize(
|
||||
this.junkPacketMaxSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.initPacketJunkSize.isNotEmpty()) {
|
||||
builder.setInitPacketJunkSize(
|
||||
this.initPacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.responsePacketJunkSize.isNotEmpty()) {
|
||||
builder.setResponsePacketJunkSize(
|
||||
this.responsePacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (this.initPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setInitPacketMagicHeader(
|
||||
this.initPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.responsePacketMagicHeader.isNotEmpty()) {
|
||||
builder.setResponsePacketMagicHeader(
|
||||
this.responsePacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.transportPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setTransportPacketMagicHeader(
|
||||
this.transportPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (this.underloadPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setUnderloadPacketMagicHeader(
|
||||
this.underloadPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
builder.parsePrivateKey(_uiState.value.interfaceProxy.privateKey.trim())
|
||||
builder.parseAddresses(_uiState.value.interfaceProxy.addresses.trim())
|
||||
if (_uiState.value.interfaceProxy.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(_uiState.value.interfaceProxy.dnsServers.trim())
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.mtu.isNotEmpty()) {
|
||||
builder.parseMtu(_uiState.value.interfaceProxy.mtu.trim())
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.listenPort.isNotEmpty()) {
|
||||
builder.parseListenPort(_uiState.value.interfaceProxy.listenPort.trim())
|
||||
}
|
||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||
if (_uiState.value.include) {
|
||||
builder.includeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (!_uiState.value.include) {
|
||||
builder.excludeApplications(
|
||||
_uiState.value.checkedPackageNames,
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.junkPacketCount.isNotEmpty()) {
|
||||
builder.setJunkPacketCount(
|
||||
_uiState.value.interfaceProxy.junkPacketCount.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.junkPacketMinSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMinSize(
|
||||
_uiState.value.interfaceProxy.junkPacketMinSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.junkPacketMaxSize.isNotEmpty()) {
|
||||
builder.setJunkPacketMaxSize(
|
||||
_uiState.value.interfaceProxy.junkPacketMaxSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.initPacketJunkSize.isNotEmpty()) {
|
||||
builder.setInitPacketJunkSize(
|
||||
_uiState.value.interfaceProxy.initPacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.responsePacketJunkSize.isNotEmpty()) {
|
||||
builder.setResponsePacketJunkSize(
|
||||
_uiState.value.interfaceProxy.responsePacketJunkSize.trim().toInt(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.initPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setInitPacketMagicHeader(
|
||||
_uiState.value.interfaceProxy.initPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.responsePacketMagicHeader.isNotEmpty()) {
|
||||
builder.setResponsePacketMagicHeader(
|
||||
_uiState.value.interfaceProxy.responsePacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.transportPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setTransportPacketMagicHeader(
|
||||
_uiState.value.interfaceProxy.transportPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
if (_uiState.value.interfaceProxy.underloadPacketMagicHeader.isNotEmpty()) {
|
||||
builder.setUnderloadPacketMagicHeader(
|
||||
_uiState.value.interfaceProxy.underloadPacketMagicHeader.trim().toLong(),
|
||||
)
|
||||
}
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
@@ -318,33 +291,41 @@ constructor(
|
||||
.build()
|
||||
}
|
||||
|
||||
fun onSaveAllChanges() = viewModelScope.launch {
|
||||
kotlin.runCatching {
|
||||
fun onSaveAllChanges(configType: ConfigType): Result<Unit> {
|
||||
return try {
|
||||
val wgQuick = buildConfig().toWgQuickString(true)
|
||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
||||
val tunnelConfig = uiState.value.tunnel?.copy(
|
||||
name = _uiState.value.tunnelName,
|
||||
amQuick = amQuick,
|
||||
wgQuick = wgQuick,
|
||||
) ?: TunnelConfig(
|
||||
name = _uiState.value.tunnelName,
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick,
|
||||
)
|
||||
val amQuick =
|
||||
if (configType == ConfigType.AMNEZIA) {
|
||||
buildAmConfig().toAwgQuickString(true)
|
||||
} else {
|
||||
TunnelConfig.AM_QUICK_DEFAULT
|
||||
}
|
||||
val tunnelConfig =
|
||||
when (uiState.value.tunnel) {
|
||||
null ->
|
||||
TunnelConfig(
|
||||
name = _uiState.value.tunnelName,
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick,
|
||||
)
|
||||
|
||||
else ->
|
||||
uiState.value.tunnel!!.copy(
|
||||
name = _uiState.value.tunnelName,
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick,
|
||||
)
|
||||
}
|
||||
updateTunnelConfig(tunnelConfig)
|
||||
SnackbarController.showMessage(
|
||||
StringValue.StringResource(R.string.config_changes_saved),
|
||||
)
|
||||
navController.navigate(Route.Main)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
||||
val stringValue = if (message.isNullOrBlank()) {
|
||||
StringValue.StringResource(R.string.unknown_error)
|
||||
} else {
|
||||
StringValue.DynamicString(message)
|
||||
}
|
||||
SnackbarController.showMessage(stringValue)
|
||||
Result.success(Unit)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
val message = e.message?.substringAfter(":", missingDelimiterValue = "")
|
||||
val stringValue =
|
||||
message?.let {
|
||||
StringValue.DynamicString(message)
|
||||
} ?: StringValue.StringResource(R.string.unknown_error)
|
||||
Result.failure(WgTunnelExceptions.ConfigParseError(stringValue))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,7 +408,7 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
privateKey = keyPair.privateKey.toBase64(),
|
||||
publicKey = keyPair.publicKey.toBase64(),
|
||||
),
|
||||
@@ -438,7 +419,7 @@ constructor(
|
||||
fun onAddressesChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(addresses = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(addresses = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -446,7 +427,7 @@ constructor(
|
||||
fun onListenPortChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(listenPort = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(listenPort = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -454,21 +435,21 @@ constructor(
|
||||
fun onDnsServersChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(dnsServers = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(dnsServers = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onMtuChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(interfaceProxy = it.interfaceProxy.copy(mtu = value))
|
||||
it.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(mtu = value))
|
||||
}
|
||||
}
|
||||
|
||||
private fun onInterfacePublicKeyChange(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(publicKey = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(publicKey = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -476,7 +457,7 @@ constructor(
|
||||
fun onPrivateKeyChange(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(privateKey = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(privateKey = value),
|
||||
)
|
||||
}
|
||||
if (NumberUtils.isValidKey(value)) {
|
||||
@@ -498,7 +479,7 @@ constructor(
|
||||
fun onJunkPacketCountChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketCount = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketCount = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -506,7 +487,7 @@ constructor(
|
||||
fun onJunkPacketMinSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMinSize = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketMinSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -514,7 +495,7 @@ constructor(
|
||||
fun onJunkPacketMaxSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMaxSize = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketMaxSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -522,7 +503,7 @@ constructor(
|
||||
fun onInitPacketJunkSizeChanged(value: String) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy = it.interfaceProxy.copy(initPacketJunkSize = value),
|
||||
interfaceProxy = _uiState.value.interfaceProxy.copy(initPacketJunkSize = value),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -531,7 +512,7 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
responsePacketJunkSize = value,
|
||||
),
|
||||
)
|
||||
@@ -542,7 +523,7 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
initPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
@@ -553,7 +534,7 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
responsePacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
@@ -564,7 +545,7 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
transportPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
@@ -575,15 +556,10 @@ constructor(
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
interfaceProxy =
|
||||
it.interfaceProxy.copy(
|
||||
_uiState.value.interfaceProxy.copy(
|
||||
underloadPacketMagicHeader = value,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@AssistedFactory
|
||||
interface ConfigViewModelFactory {
|
||||
fun create(id: Int): ConfigViewModel
|
||||
}
|
||||
}
|
||||
|
||||
-199
@@ -1,199 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.components
|
||||
|
||||
import android.content.pm.PackageInfo
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
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.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Android
|
||||
import androidx.compose.material3.BasicAlertDialog
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
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.unit.dp
|
||||
import com.google.accompanist.drawablepainter.DrawablePainter
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ApplicationSelectionDialog(viewModel: ConfigViewModel, uiState: ConfigUiState, onDismiss: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val licenseComparator = compareBy<PackageInfo> { viewModel.getPackageLabel(it) }
|
||||
|
||||
val sortedPackages = remember(uiState.packages, licenseComparator) {
|
||||
uiState.packages.sortedWith(licenseComparator)
|
||||
}
|
||||
BasicAlertDialog(
|
||||
onDismissRequest = { onDismiss() },
|
||||
) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.fillMaxHeight(if (uiState.isAllApplicationsEnabled) 1 / 5f else 4 / 5f),
|
||||
) {
|
||||
Column(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(),
|
||||
) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.tunnel_all))
|
||||
Switch(
|
||||
checked = uiState.isAllApplicationsEnabled,
|
||||
onCheckedChange = viewModel::onAllApplicationsChange,
|
||||
)
|
||||
}
|
||||
if (!uiState.isAllApplicationsEnabled) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.include))
|
||||
Checkbox(
|
||||
checked = uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Text(stringResource(id = R.string.exclude))
|
||||
Checkbox(
|
||||
checked = !uiState.include,
|
||||
onCheckedChange = {
|
||||
viewModel.onIncludeChange(!uiState.include)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
SearchBar(viewModel::emitQueriedPackages)
|
||||
}
|
||||
Spacer(Modifier.padding(5.dp))
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.fillMaxHeight(19 / 22f),
|
||||
) {
|
||||
items(sortedPackages, key = { it.packageName }) { pack ->
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(5.dp).padding(end = 25.dp),
|
||||
) {
|
||||
Row(modifier = Modifier.fillMaxWidth().padding(start = 5.dp)) {
|
||||
val drawable =
|
||||
pack.applicationInfo?.loadIcon(context.packageManager)
|
||||
val iconSize = 35.dp
|
||||
if (drawable != null) {
|
||||
Image(
|
||||
painter = DrawablePainter(drawable),
|
||||
stringResource(id = R.string.icon),
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
} else {
|
||||
val icon = Icons.Rounded.Android
|
||||
Icon(
|
||||
icon,
|
||||
icon.name,
|
||||
modifier = Modifier.size(iconSize),
|
||||
)
|
||||
}
|
||||
Text(
|
||||
viewModel.getPackageLabel(pack),
|
||||
modifier = Modifier.padding(5.dp),
|
||||
)
|
||||
}
|
||||
Checkbox(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
checked =
|
||||
(
|
||||
uiState.checkedPackageNames.contains(
|
||||
pack.packageName,
|
||||
)
|
||||
),
|
||||
onCheckedChange = {
|
||||
if (it) {
|
||||
viewModel.onAddCheckedPackage(pack.packageName)
|
||||
} else {
|
||||
viewModel.onRemoveCheckedPackage(pack.packageName)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(onClick = { onDismiss() }) {
|
||||
Text(stringResource(R.string.done))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-4
@@ -25,10 +25,7 @@ data class InterfaceProxy(
|
||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||
addresses = i.addresses.joinToString(", ").trim(),
|
||||
dnsServers = listOf(
|
||||
i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||
i.dnsSearchDomains.joinToString(", ").trim(),
|
||||
).filter { it.length > 0 }.joinToString(", "),
|
||||
dnsServers = i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||
listenPort =
|
||||
if (i.listenPort.isPresent) {
|
||||
i.listenPort.get().toString().trim()
|
||||
|
||||
+70
-38
@@ -18,7 +18,6 @@ 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.rounded.Bolt
|
||||
import androidx.compose.material.icons.rounded.Circle
|
||||
import androidx.compose.material.icons.rounded.CopyAll
|
||||
@@ -30,7 +29,6 @@ import androidx.compose.material.icons.rounded.Star
|
||||
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.Switch
|
||||
import androidx.compose.material3.Text
|
||||
@@ -40,6 +38,7 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
@@ -59,6 +58,7 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavController
|
||||
import com.journeyapps.barcodescanner.ScanContract
|
||||
import com.journeyapps.barcodescanner.ScanOptions
|
||||
@@ -67,40 +67,49 @@ import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.RowListItem
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||
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.ScrollDismissMultiFab
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.VpnDeniedDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.corn
|
||||
import com.zaneschepke.wireguardautotunnel.ui.theme.mint
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.handshakeStatus
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.mapPeerStats
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState, focusRequester: FocusRequester, navController: NavController) {
|
||||
fun MainScreen(
|
||||
viewModel: MainViewModel = hiltViewModel(),
|
||||
appViewModel: AppViewModel,
|
||||
focusRequester: FocusRequester,
|
||||
navController: NavController,
|
||||
) {
|
||||
val haptic = LocalHapticFeedback.current
|
||||
val context = LocalContext.current
|
||||
val snackbar = SnackbarController.current
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
var showBottomSheet by remember { mutableStateOf(false) }
|
||||
var configType by remember { mutableStateOf(ConfigType.WIREGUARD) }
|
||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||
val isVisible = rememberSaveable { mutableStateOf(true) }
|
||||
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
||||
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
|
||||
val nestedScrollConnection =
|
||||
remember {
|
||||
@@ -145,11 +154,15 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
}
|
||||
|
||||
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.error_no_file_explorer),
|
||||
)
|
||||
}, onData = { data ->
|
||||
viewModel.onTunnelFileSelected(data, context)
|
||||
scope.launch {
|
||||
viewModel.onTunnelFileSelected(data, configType, context).onFailure {
|
||||
appViewModel.showSnackbarMessage(it.getMessage(context))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
val scanLauncher =
|
||||
@@ -157,7 +170,11 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
contract = ScanContract(),
|
||||
onResult = {
|
||||
if (it.contents != null) {
|
||||
viewModel.onTunnelQrResult(it.contents)
|
||||
scope.launch {
|
||||
viewModel.onTunnelQrResult(it.contents, configType).onFailure { error ->
|
||||
appViewModel.showSnackbarMessage(error.getMessage(context))
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -192,6 +209,10 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
}
|
||||
}
|
||||
|
||||
if (uiState.loading) {
|
||||
return LoadingScreen()
|
||||
}
|
||||
|
||||
fun launchQrScanner() {
|
||||
val scanOptions = ScanOptions()
|
||||
scanOptions.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
||||
@@ -216,15 +237,9 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
},
|
||||
floatingActionButtonPosition = FabPosition.End,
|
||||
floatingActionButton = {
|
||||
ScrollDismissFab(icon = {
|
||||
val icon = Icons.Filled.Add
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = icon.name,
|
||||
tint = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
}, focusRequester, isVisible = isVisible.value, onClick = {
|
||||
ScrollDismissMultiFab(R.drawable.add, focusRequester, isVisible = isVisible.value, onFabItemClicked = {
|
||||
showBottomSheet = true
|
||||
configType = ConfigType.valueOf(it.value)
|
||||
})
|
||||
},
|
||||
) {
|
||||
@@ -235,7 +250,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
onQrClick = { launchQrScanner() },
|
||||
onManualImportClick = {
|
||||
navController.navigate(
|
||||
Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID),
|
||||
"${Screen.Config.route}/${Constants.MANUAL_TUNNEL_CONFIG_ID}?configType=$configType",
|
||||
)
|
||||
},
|
||||
)
|
||||
@@ -326,22 +341,20 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
uiState.tunnels,
|
||||
key = { tunnel -> tunnel.id },
|
||||
) { tunnel ->
|
||||
val isActive = uiState.tunnels.any {
|
||||
it.id == tunnel.id &&
|
||||
it.isActive
|
||||
}
|
||||
val isActive = uiState.tunnels.any { it.id == tunnel.id && it.isActive }
|
||||
val leadingIconColor =
|
||||
(
|
||||
if (
|
||||
isActive && uiState.vpnState.statistics != null
|
||||
isActive
|
||||
) {
|
||||
uiState.vpnState.statistics.mapPeerStats()
|
||||
.map { it.value?.handshakeStatus() }
|
||||
uiState.vpnState.statistics
|
||||
?.mapPeerStats()
|
||||
?.map { it.value?.handshakeStatus() }
|
||||
.let { statuses ->
|
||||
when {
|
||||
statuses.all { it == HandshakeStatus.HEALTHY } -> mint
|
||||
statuses.any { it == HandshakeStatus.STALE } -> corn
|
||||
statuses.all { it == HandshakeStatus.NOT_STARTED } ->
|
||||
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> mint
|
||||
statuses?.any { it == HandshakeStatus.STALE } == true -> corn
|
||||
statuses?.all { it == HandshakeStatus.NOT_STARTED } == true ->
|
||||
Color.Gray
|
||||
|
||||
else -> {
|
||||
@@ -381,6 +394,15 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
},
|
||||
text = tunnel.name,
|
||||
onHold = {
|
||||
if (
|
||||
(uiState.vpnState.status == TunnelState.UP) &&
|
||||
(tunnel.name == uiState.vpnState.tunnelConfig?.name)
|
||||
) {
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.turn_off_tunnel),
|
||||
)
|
||||
return@RowListItem
|
||||
}
|
||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
selectedTunnel = tunnel
|
||||
},
|
||||
@@ -407,9 +429,16 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
Row {
|
||||
IconButton(
|
||||
onClick = {
|
||||
selectedTunnel?.let {
|
||||
if (
|
||||
uiState.settings.isAutoTunnelEnabled &&
|
||||
!uiState.settings.isAutoTunnelPaused
|
||||
) {
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.turn_off_tunnel),
|
||||
)
|
||||
} else {
|
||||
navController.navigate(
|
||||
Route.Option(it.id),
|
||||
"${Screen.Option.route}/${selectedTunnel?.id}",
|
||||
)
|
||||
}
|
||||
},
|
||||
@@ -428,7 +457,6 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
Icon(icon, icon.name)
|
||||
}
|
||||
IconButton(
|
||||
enabled = !isActive,
|
||||
modifier = Modifier.focusable(),
|
||||
onClick = { showDeleteTunnelAlertDialog = true },
|
||||
) {
|
||||
@@ -453,10 +481,14 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
Row {
|
||||
IconButton(
|
||||
onClick = {
|
||||
selectedTunnel = tunnel
|
||||
selectedTunnel?.let {
|
||||
if (uiState.settings.isAutoTunnelEnabled && !uiState.settings.isAutoTunnelPaused) {
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.turn_off_auto),
|
||||
)
|
||||
} else {
|
||||
selectedTunnel = tunnel
|
||||
navController.navigate(
|
||||
Route.Option(it.id),
|
||||
"${Screen.Option.route}/${selectedTunnel?.id}",
|
||||
)
|
||||
}
|
||||
},
|
||||
@@ -476,7 +508,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
) {
|
||||
expanded.value = !expanded.value
|
||||
} else {
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.turn_on_tunnel),
|
||||
)
|
||||
}
|
||||
@@ -497,7 +529,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
||||
uiState.vpnState.status == TunnelState.UP &&
|
||||
tunnel.name == uiState.vpnState.tunnelConfig?.name
|
||||
) {
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.turn_off_tunnel),
|
||||
)
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||
|
||||
data class MainUiState(
|
||||
val settings: Settings = Settings(),
|
||||
val tunnels: TunnelConfigs = emptyList(),
|
||||
val vpnState: VpnState = VpnState(),
|
||||
val loading: Boolean = true,
|
||||
)
|
||||
+201
-86
@@ -6,22 +6,22 @@ import android.net.Uri
|
||||
import android.provider.OpenableColumns
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.wireguard.config.Config
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.FileReadException
|
||||
import com.zaneschepke.wireguardautotunnel.util.InvalidFileExtensionException
|
||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
@@ -34,12 +34,26 @@ class MainViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val serviceManager: ServiceManager,
|
||||
val tunnelService: TunnelService,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
val uiState =
|
||||
combine(
|
||||
appDataRepository.settings.getSettingsFlow(),
|
||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||
tunnelService.vpnState,
|
||||
) { settings, tunnels, vpnState ->
|
||||
MainUiState(settings, tunnels, vpnState, false)
|
||||
}
|
||||
.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
MainUiState(),
|
||||
)
|
||||
|
||||
private fun stopWatcherService(context: Context) {
|
||||
ServiceManager.stopWatcherService(context)
|
||||
serviceManager.stopWatcherService(context)
|
||||
}
|
||||
|
||||
fun onDelete(tunnel: TunnelConfig, context: Context) {
|
||||
@@ -73,17 +87,32 @@ constructor(
|
||||
tunnelService.stopTunnel(tunnel)
|
||||
}
|
||||
|
||||
private fun generateQrCodeDefaultName(config: String): String {
|
||||
private fun validateConfigString(config: String, configType: ConfigType) {
|
||||
when (configType) {
|
||||
ConfigType.AMNEZIA -> TunnelConfig.configFromAmQuick(config)
|
||||
ConfigType.WIREGUARD -> TunnelConfig.configFromWgQuick(config)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateQrCodeDefaultName(config: String, configType: ConfigType): String {
|
||||
return try {
|
||||
TunnelConfig.configFromAmQuick(config).peers[0].endpoint.get().host
|
||||
when (configType) {
|
||||
ConfigType.AMNEZIA -> {
|
||||
TunnelConfig.configFromAmQuick(config).peers[0].endpoint.get().host
|
||||
}
|
||||
|
||||
ConfigType.WIREGUARD -> {
|
||||
TunnelConfig.configFromWgQuick(config).peers[0].endpoint.get().host
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
NumberUtils.generateRandomTunnelName()
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateQrCodeTunnelName(config: String): String {
|
||||
var defaultName = generateQrCodeDefaultName(config)
|
||||
private fun generateQrCodeTunnelName(config: String, configType: ConfigType): String {
|
||||
var defaultName = generateQrCodeDefaultName(config, configType)
|
||||
val lines = config.lines().toMutableList()
|
||||
val linesIterator = lines.iterator()
|
||||
while (linesIterator.hasNext()) {
|
||||
@@ -96,18 +125,37 @@ constructor(
|
||||
return defaultName
|
||||
}
|
||||
|
||||
fun onTunnelQrResult(result: String) = viewModelScope.launch(ioDispatcher) {
|
||||
kotlin.runCatching {
|
||||
val amConfig = TunnelConfig.configFromAmQuick(result)
|
||||
val amQuick = amConfig.toAwgQuickString(true)
|
||||
val wgQuick = amConfig.toWgQuickString()
|
||||
suspend fun onTunnelQrResult(result: String, configType: ConfigType): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
try {
|
||||
validateConfigString(result, configType)
|
||||
val tunnelName =
|
||||
makeTunnelNameUnique(generateQrCodeTunnelName(result, configType))
|
||||
val tunnelConfig =
|
||||
when (configType) {
|
||||
ConfigType.AMNEZIA -> {
|
||||
TunnelConfig(
|
||||
name = tunnelName,
|
||||
amQuick = result,
|
||||
wgQuick =
|
||||
TunnelConfig.configFromAmQuick(
|
||||
result,
|
||||
).toWgQuickString(),
|
||||
)
|
||||
}
|
||||
|
||||
val tunnelName = makeTunnelNameUnique(generateQrCodeTunnelName(result))
|
||||
val tunnelConfig = TunnelConfig(name = tunnelName, wgQuick = wgQuick, amQuick = amQuick)
|
||||
saveTunnel(tunnelConfig)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_invalid_code))
|
||||
ConfigType.WIREGUARD ->
|
||||
TunnelConfig(
|
||||
name = tunnelName,
|
||||
wgQuick = result,
|
||||
)
|
||||
}
|
||||
addTunnel(tunnelConfig)
|
||||
Result.success(Unit)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
Result.failure(WgTunnelExceptions.InvalidQrCode())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,83 +172,141 @@ constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelConfigFromStream(stream: InputStream, fileName: String) {
|
||||
val amConfig = stream.use { org.amnezia.awg.config.Config.parse(it) }
|
||||
val tunnelName = makeTunnelNameUnique(getNameFromFileName(fileName))
|
||||
saveTunnel(
|
||||
TunnelConfig(
|
||||
name = tunnelName,
|
||||
wgQuick = amConfig.toWgQuickString(),
|
||||
amQuick = amConfig.toAwgQuickString(true),
|
||||
),
|
||||
)
|
||||
private fun saveTunnelConfigFromStream(stream: InputStream, fileName: String, type: ConfigType) {
|
||||
var amQuick: String? = null
|
||||
val wgQuick =
|
||||
stream.use {
|
||||
when (type) {
|
||||
ConfigType.AMNEZIA -> {
|
||||
val config = org.amnezia.awg.config.Config.parse(it)
|
||||
amQuick = config.toAwgQuickString(true)
|
||||
config.toWgQuickString()
|
||||
}
|
||||
|
||||
ConfigType.WIREGUARD -> {
|
||||
Config.parse(it).toWgQuickString(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
viewModelScope.launch {
|
||||
val tunnelName = makeTunnelNameUnique(getNameFromFileName(fileName))
|
||||
addTunnel(
|
||||
TunnelConfig(
|
||||
name = tunnelName,
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick ?: TunnelConfig.AM_QUICK_DEFAULT,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getInputStreamFromUri(uri: Uri, context: Context): InputStream? {
|
||||
return context.applicationContext.contentResolver.openInputStream(uri)
|
||||
}
|
||||
|
||||
fun onTunnelFileSelected(uri: Uri, context: Context) = viewModelScope.launch(ioDispatcher) {
|
||||
kotlin.runCatching {
|
||||
if (!isValidUriContentScheme(uri)) throw InvalidFileExtensionException
|
||||
val fileName = getFileName(context, uri)
|
||||
when (getFileExtensionFromFileName(fileName)) {
|
||||
Constants.CONF_FILE_EXTENSION ->
|
||||
saveTunnelFromConfUri(fileName, uri, context)
|
||||
Constants.ZIP_FILE_EXTENSION ->
|
||||
saveTunnelsFromZipUri(
|
||||
uri,
|
||||
context,
|
||||
)
|
||||
else -> throw InvalidFileExtensionException
|
||||
suspend fun onTunnelFileSelected(uri: Uri, configType: ConfigType, context: Context): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
try {
|
||||
if (isValidUriContentScheme(uri)) {
|
||||
val fileName = getFileName(context, uri)
|
||||
return@withContext when (getFileExtensionFromFileName(fileName)) {
|
||||
Constants.CONF_FILE_EXTENSION ->
|
||||
saveTunnelFromConfUri(fileName, uri, configType, context)
|
||||
|
||||
Constants.ZIP_FILE_EXTENSION ->
|
||||
saveTunnelsFromZipUri(
|
||||
uri,
|
||||
configType,
|
||||
context,
|
||||
)
|
||||
|
||||
else -> Result.failure(WgTunnelExceptions.InvalidFileExtension())
|
||||
}
|
||||
} else {
|
||||
Result.failure(WgTunnelExceptions.InvalidFileExtension())
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
Result.failure(WgTunnelExceptions.FileReadFailed())
|
||||
}
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
if (it is InvalidFileExtensionException) {
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_file_extension))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelsFromZipUri(uri: Uri, configType: ConfigType, context: Context): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
ZipInputStream(getInputStreamFromUri(uri, context)).use { zip ->
|
||||
generateSequence { zip.nextEntry }
|
||||
.filterNot {
|
||||
it.isDirectory ||
|
||||
getFileExtensionFromFileName(it.name) != Constants.CONF_FILE_EXTENSION
|
||||
}
|
||||
.forEach {
|
||||
val name = getNameFromFileName(it.name)
|
||||
withContext(viewModelScope.coroutineContext) {
|
||||
try {
|
||||
var amQuick: String? = null
|
||||
val wgQuick =
|
||||
when (configType) {
|
||||
ConfigType.AMNEZIA -> {
|
||||
val config =
|
||||
org.amnezia.awg.config.Config.parse(
|
||||
zip,
|
||||
)
|
||||
amQuick = config.toAwgQuickString(true)
|
||||
config.toWgQuickString()
|
||||
}
|
||||
|
||||
ConfigType.WIREGUARD -> {
|
||||
Config.parse(zip).toWgQuickString(true)
|
||||
}
|
||||
}
|
||||
addTunnel(
|
||||
TunnelConfig(
|
||||
name = makeTunnelNameUnique(name),
|
||||
wgQuick = wgQuick,
|
||||
amQuick = amQuick ?: TunnelConfig.AM_QUICK_DEFAULT,
|
||||
),
|
||||
)
|
||||
Result.success(Unit)
|
||||
} catch (e: Exception) {
|
||||
Result.failure(WgTunnelExceptions.FileReadFailed())
|
||||
}
|
||||
}
|
||||
}
|
||||
Result.success(Unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri, configType: ConfigType, context: Context): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
val stream = getInputStreamFromUri(uri, context)
|
||||
return@withContext if (stream != null) {
|
||||
try {
|
||||
saveTunnelConfigFromStream(stream, name, configType)
|
||||
} catch (e: Exception) {
|
||||
return@withContext Result.failure(WgTunnelExceptions.ConfigParseError())
|
||||
}
|
||||
Result.success(Unit)
|
||||
} else {
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_file_format))
|
||||
Result.failure(WgTunnelExceptions.FileReadFailed())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
||||
ZipInputStream(getInputStreamFromUri(uri, context)).use { zip ->
|
||||
generateSequence { zip.nextEntry }
|
||||
.filterNot {
|
||||
it.isDirectory ||
|
||||
getFileExtensionFromFileName(it.name) != Constants.CONF_FILE_EXTENSION
|
||||
}
|
||||
.forEach { entry ->
|
||||
val name = getNameFromFileName(entry.name)
|
||||
val amConf = org.amnezia.awg.config.Config.parse(zip.bufferedReader())
|
||||
saveTunnel(
|
||||
TunnelConfig(
|
||||
name = makeTunnelNameUnique(name),
|
||||
wgQuick = amConf.toWgQuickString(),
|
||||
amQuick = amConf.toAwgQuickString(true),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri, context: Context) {
|
||||
val stream = getInputStreamFromUri(uri, context) ?: throw FileReadException
|
||||
saveTunnelConfigFromStream(stream, name)
|
||||
private fun addTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
saveTunnel(tunnelConfig)
|
||||
}
|
||||
|
||||
fun pauseAutoTunneling() = viewModelScope.launch {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
appDataRepository.settings.save(
|
||||
settings.copy(isAutoTunnelPaused = true),
|
||||
uiState.value.settings.copy(isAutoTunnelPaused = true),
|
||||
)
|
||||
}
|
||||
|
||||
fun resumeAutoTunneling() = viewModelScope.launch {
|
||||
val settings = appDataRepository.settings.getSettings()
|
||||
appDataRepository.settings.save(
|
||||
settings.copy(isAutoTunnelPaused = false),
|
||||
uiState.value.settings.copy(isAutoTunnelPaused = false),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -209,23 +315,32 @@ constructor(
|
||||
}
|
||||
|
||||
private fun getFileNameByCursor(context: Context, uri: Uri): String? {
|
||||
return context.contentResolver.query(uri, null, null, null, null)?.use {
|
||||
getDisplayNameByCursor(it)
|
||||
context.contentResolver.query(uri, null, null, null, null)?.use {
|
||||
return getDisplayNameByCursor(it)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getDisplayNameColumnIndex(cursor: Cursor): Int? {
|
||||
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||
if (columnIndex == -1) return null
|
||||
return columnIndex
|
||||
return if (columnIndex != -1) {
|
||||
return columnIndex
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDisplayNameByCursor(cursor: Cursor): String? {
|
||||
val move = cursor.moveToFirst()
|
||||
if (!move) return null
|
||||
val index = getDisplayNameColumnIndex(cursor)
|
||||
if (index == null) return index
|
||||
return cursor.getString(index)
|
||||
return if (cursor.moveToFirst()) {
|
||||
val index = getDisplayNameColumnIndex(cursor)
|
||||
if (index != null) {
|
||||
cursor.getString(index)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValidUriContentScheme(uri: Uri): Boolean {
|
||||
|
||||
+80
-9
@@ -1,20 +1,41 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
||||
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.slideInVertically
|
||||
import androidx.compose.animation.slideOutVertically
|
||||
import androidx.compose.foundation.focusGroup
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.iamageo.multifablibrary.FabIcon
|
||||
import com.iamageo.multifablibrary.FabOption
|
||||
import com.iamageo.multifablibrary.MultiFabItem
|
||||
import com.iamageo.multifablibrary.MultiFloatingActionButton
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
|
||||
@Composable
|
||||
fun ScrollDismissFab(icon: @Composable () -> Unit, focusRequester: FocusRequester, isVisible: Boolean, onClick: () -> Unit) {
|
||||
fun ScrollDismissMultiFab(
|
||||
@DrawableRes res: Int,
|
||||
focusRequester: FocusRequester,
|
||||
isVisible: Boolean,
|
||||
onFabItemClicked: (fabItem: MultiFabItem) -> Unit,
|
||||
) {
|
||||
// Nested scroll for control FAB
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
AnimatedVisibility(
|
||||
visible = isVisible,
|
||||
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
||||
@@ -24,14 +45,64 @@ fun ScrollDismissFab(icon: @Composable () -> Unit, focusRequester: FocusRequeste
|
||||
.focusRequester(focusRequester)
|
||||
.focusGroup(),
|
||||
) {
|
||||
FloatingActionButton(
|
||||
onClick = {
|
||||
onClick()
|
||||
val fobColor = MaterialTheme.colorScheme.secondary
|
||||
val fobIconColor = MaterialTheme.colorScheme.background
|
||||
MultiFloatingActionButton(
|
||||
fabIcon =
|
||||
FabIcon(
|
||||
iconRes = res,
|
||||
iconResAfterRotate = R.drawable.close,
|
||||
iconRotate = 180f,
|
||||
),
|
||||
fabOption =
|
||||
FabOption(
|
||||
iconTint = fobIconColor,
|
||||
backgroundTint = fobColor,
|
||||
),
|
||||
itemsMultiFab =
|
||||
listOf(
|
||||
MultiFabItem(
|
||||
label = {
|
||||
Text(
|
||||
stringResource(id = R.string.amnezia),
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.padding(end = 10.dp),
|
||||
)
|
||||
},
|
||||
modifier =
|
||||
Modifier
|
||||
.size(40.dp),
|
||||
icon = res,
|
||||
value = ConfigType.AMNEZIA.name,
|
||||
miniFabOption =
|
||||
FabOption(
|
||||
backgroundTint = fobColor,
|
||||
fobIconColor,
|
||||
),
|
||||
),
|
||||
MultiFabItem(
|
||||
label = {
|
||||
Text(
|
||||
stringResource(id = R.string.wireguard),
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.padding(end = 10.dp),
|
||||
)
|
||||
},
|
||||
icon = res,
|
||||
value = ConfigType.WIREGUARD.name,
|
||||
miniFabOption =
|
||||
FabOption(
|
||||
backgroundTint = fobColor,
|
||||
fobIconColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
onFabItemClicked = {
|
||||
onFabItemClicked(it)
|
||||
},
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
) {
|
||||
icon()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+45
-88
@@ -19,7 +19,6 @@ import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material.icons.outlined.Add
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
@@ -33,6 +32,7 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -44,24 +44,23 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.SubmitConfigurationTextBox
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissFab
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.WildcardSupportingLabel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissMultiFab
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isValidIpv4orIpv6Address
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@@ -69,15 +68,16 @@ import kotlinx.coroutines.delay
|
||||
fun OptionsScreen(
|
||||
optionsViewModel: OptionsViewModel = hiltViewModel(),
|
||||
navController: NavController,
|
||||
appViewModel: AppViewModel,
|
||||
focusRequester: FocusRequester,
|
||||
appUiState: AppUiState,
|
||||
tunnelId: Int,
|
||||
tunnelId: String,
|
||||
) {
|
||||
val scrollState = rememberScrollState()
|
||||
val uiState by optionsViewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
val config = appUiState.tunnels.first { it.id == tunnelId }
|
||||
|
||||
val interactionSource = remember { MutableInteractionSource() }
|
||||
val scope = rememberCoroutineScope()
|
||||
val focusManager = LocalFocusManager.current
|
||||
val screenPadding = 5.dp
|
||||
val fillMaxWidth = .85f
|
||||
@@ -85,6 +85,7 @@ fun OptionsScreen(
|
||||
var currentText by remember { mutableStateOf("") }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
optionsViewModel.init(tunnelId)
|
||||
if (context.isRunningOnTv()) {
|
||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||
kotlin.runCatching {
|
||||
@@ -98,23 +99,22 @@ fun OptionsScreen(
|
||||
|
||||
fun saveTrustedSSID() {
|
||||
if (currentText.isNotEmpty()) {
|
||||
optionsViewModel.onSaveRunSSID(currentText, config)
|
||||
currentText = ""
|
||||
scope.launch {
|
||||
optionsViewModel.onSaveRunSSID(currentText).onSuccess {
|
||||
currentText = ""
|
||||
}.onFailure {
|
||||
appViewModel.showSnackbarMessage(it.getMessage(context))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
floatingActionButton = {
|
||||
ScrollDismissFab(icon = {
|
||||
val icon = Icons.Filled.Edit
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = icon.name,
|
||||
tint = MaterialTheme.colorScheme.onPrimary,
|
||||
)
|
||||
}, focusRequester, isVisible = true, onClick = {
|
||||
ScrollDismissMultiFab(R.drawable.edit, focusRequester, isVisible = true, onFabItemClicked = {
|
||||
val configType = ConfigType.valueOf(it.value)
|
||||
navController.navigate(
|
||||
Route.Config(config.id),
|
||||
"${Screen.Config.route}/$tunnelId?configType=${configType.name}",
|
||||
)
|
||||
})
|
||||
},
|
||||
@@ -165,12 +165,12 @@ fun OptionsScreen(
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.set_primary_tunnel),
|
||||
enabled = true,
|
||||
checked = config.isPrimaryTunnel,
|
||||
checked = uiState.isDefaultTunnel,
|
||||
modifier =
|
||||
Modifier
|
||||
.focusRequester(focusRequester),
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { optionsViewModel.onTogglePrimaryTunnel(config) },
|
||||
onCheckChanged = { optionsViewModel.onTogglePrimaryTunnel() },
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -206,9 +206,9 @@ fun OptionsScreen(
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.mobile_data_tunnel),
|
||||
enabled = true,
|
||||
checked = config.isMobileDataTunnel,
|
||||
checked = uiState.tunnel?.isMobileDataTunnel == true,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { optionsViewModel.onToggleIsMobileDataTunnel(config) },
|
||||
onCheckChanged = { optionsViewModel.onToggleIsMobileDataTunnel() },
|
||||
)
|
||||
Column {
|
||||
FlowRow(
|
||||
@@ -218,24 +218,24 @@ fun OptionsScreen(
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
) {
|
||||
config.tunnelNetworks.forEach { ssid ->
|
||||
uiState.tunnel?.tunnelNetworks?.forEach { ssid ->
|
||||
ClickableIconButton(
|
||||
onClick = {
|
||||
if (context.isRunningOnTv()) {
|
||||
focusRequester.requestFocus()
|
||||
optionsViewModel.onDeleteRunSSID(ssid, config)
|
||||
optionsViewModel.onDeleteRunSSID(ssid)
|
||||
}
|
||||
},
|
||||
onIconClick = {
|
||||
if (context.isRunningOnTv()) focusRequester.requestFocus()
|
||||
optionsViewModel.onDeleteRunSSID(ssid, config)
|
||||
optionsViewModel.onDeleteRunSSID(ssid)
|
||||
},
|
||||
text = ssid,
|
||||
icon = Icons.Filled.Close,
|
||||
enabled = true,
|
||||
)
|
||||
}
|
||||
if (config.tunnelNetworks.isEmpty()) {
|
||||
if (uiState.tunnel == null || uiState.tunnel?.tunnelNetworks?.isEmpty() == true) {
|
||||
Text(
|
||||
stringResource(R.string.no_wifi_names_configured),
|
||||
fontStyle = FontStyle.Italic,
|
||||
@@ -248,7 +248,6 @@ fun OptionsScreen(
|
||||
value = currentText,
|
||||
onValueChange = { currentText = it },
|
||||
label = { Text(stringResource(id = R.string.use_tunnel_on_wifi_name)) },
|
||||
supportingText = { WildcardSupportingLabel { context.openWebUrl(it) } },
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(
|
||||
@@ -268,68 +267,26 @@ fun OptionsScreen(
|
||||
IconButton(onClick = { saveTrustedSSID() }) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Add,
|
||||
contentDescription = stringResource(R.string.save_changes),
|
||||
contentDescription =
|
||||
if (currentText == "") {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_empty_description,
|
||||
)
|
||||
} else {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_value_description,
|
||||
)
|
||||
},
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.restart_on_ping),
|
||||
enabled = !appUiState.settings.isPingEnabled,
|
||||
checked = config.isPingEnabled || appUiState.settings.isPingEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { optionsViewModel.onToggleRestartOnPing(config) },
|
||||
)
|
||||
if (config.isPingEnabled || appUiState.settings.isPingEnabled) {
|
||||
SubmitConfigurationTextBox(
|
||||
config.pingIp,
|
||||
stringResource(R.string.set_custom_ping_ip),
|
||||
stringResource(R.string.default_ping_ip),
|
||||
focusRequester,
|
||||
isErrorValue = { !it.isNullOrBlank() && !it.isValidIpv4orIpv6Address() },
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
config.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 } ?: false
|
||||
}
|
||||
SubmitConfigurationTextBox(
|
||||
config.pingInterval?.let { (it / 1000).toString() },
|
||||
stringResource(R.string.set_custom_ping_internal),
|
||||
"(${stringResource(R.string.optional_default)} ${Constants.PING_INTERVAL / 1000})",
|
||||
focusRequester,
|
||||
keyboardOptions = KeyboardOptions(
|
||||
keyboardType = KeyboardType.Number,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
isErrorValue = ::isSecondsError,
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
config.copy(pingInterval = if (it.isBlank()) null else it.toLong() * 1000),
|
||||
)
|
||||
},
|
||||
)
|
||||
SubmitConfigurationTextBox(
|
||||
config.pingCooldown?.let { (it / 1000).toString() },
|
||||
stringResource(R.string.set_custom_ping_cooldown),
|
||||
"(${stringResource(R.string.optional_default)} ${Constants.PING_COOLDOWN / 1000})",
|
||||
focusRequester,
|
||||
keyboardOptions = KeyboardOptions(
|
||||
keyboardType = KeyboardType.Number,
|
||||
),
|
||||
isErrorValue = ::isSecondsError,
|
||||
onSubmit = {
|
||||
optionsViewModel.saveTunnelChanges(
|
||||
config.copy(pingCooldown = if (it.isBlank()) null else it.toLong() * 1000),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.options
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
|
||||
data class OptionsUiState(
|
||||
val id: String? = null,
|
||||
val tunnel: TunnelConfig? = null,
|
||||
val isDefaultTunnel: Boolean = false,
|
||||
)
|
||||
+74
-45
@@ -1,14 +1,20 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.options
|
||||
|
||||
import androidx.compose.ui.util.fastFirstOrNull
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -17,63 +23,86 @@ class OptionsViewModel
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
) : ViewModel() {
|
||||
private val _optionState = MutableStateFlow(OptionsUiState())
|
||||
|
||||
fun onDeleteRunSSID(ssid: String, tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(
|
||||
tunnelConfig =
|
||||
tunnelConfig.copy(
|
||||
tunnelNetworks = (tunnelConfig.tunnelNetworks - ssid).toMutableList(),
|
||||
),
|
||||
val uiState =
|
||||
combine(
|
||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||
_optionState,
|
||||
) { tunnels, optionState ->
|
||||
if (optionState.id != null) {
|
||||
val tunnelConfig = tunnels.fastFirstOrNull { it.id.toString() == optionState.id }
|
||||
val isPrimaryTunnel = tunnelConfig?.isPrimaryTunnel == true
|
||||
OptionsUiState(optionState.id, tunnelConfig, isPrimaryTunnel)
|
||||
} else {
|
||||
OptionsUiState()
|
||||
}
|
||||
}.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
OptionsUiState(),
|
||||
)
|
||||
|
||||
fun init(tunnelId: String) {
|
||||
_optionState.update {
|
||||
it.copy(
|
||||
id = tunnelId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun saveTunnelChanges(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(tunnelConfig)
|
||||
fun onDeleteRunSSID(ssid: String) = viewModelScope.launch {
|
||||
uiState.value.tunnel?.let {
|
||||
appDataRepository.tunnels.save(
|
||||
tunnelConfig =
|
||||
it.copy(
|
||||
tunnelNetworks = (uiState.value.tunnel!!.tunnelNetworks - ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun onSaveRunSSID(ssid: String, tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
private fun saveTunnel(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
||||
tunnelConfig?.let {
|
||||
appDataRepository.tunnels.save(it)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun onSaveRunSSID(ssid: String): Result<Unit> {
|
||||
val trimmed = ssid.trim()
|
||||
val tunnelsWithName = appDataRepository.tunnels.findByTunnelNetworksName(trimmed)
|
||||
|
||||
if (!tunnelConfig.tunnelNetworks.contains(trimmed) &&
|
||||
val tunnelsWithName =
|
||||
withContext(viewModelScope.coroutineContext) {
|
||||
appDataRepository.tunnels.findByTunnelNetworksName(trimmed)
|
||||
}
|
||||
return if (uiState.value.tunnel?.tunnelNetworks?.contains(trimmed) != true &&
|
||||
tunnelsWithName.isEmpty()
|
||||
) {
|
||||
saveTunnelChanges(
|
||||
tunnelConfig.copy(
|
||||
tunnelNetworks = (tunnelConfig.tunnelNetworks + ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
uiState.value.tunnel?.tunnelNetworks?.add(trimmed)
|
||||
saveTunnel(uiState.value.tunnel)
|
||||
Result.success(Unit)
|
||||
} else {
|
||||
SnackbarController.showMessage(
|
||||
StringValue.StringResource(
|
||||
R.string.error_ssid_exists,
|
||||
),
|
||||
)
|
||||
Result.failure(WgTunnelExceptions.SsidConflict())
|
||||
}
|
||||
}
|
||||
|
||||
fun onToggleIsMobileDataTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
if (tunnelConfig.isMobileDataTunnel) {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(null)
|
||||
} else {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(tunnelConfig)
|
||||
fun onToggleIsMobileDataTunnel() = viewModelScope.launch {
|
||||
uiState.value.tunnel?.let {
|
||||
if (it.isMobileDataTunnel) {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(null)
|
||||
} else {
|
||||
appDataRepository.tunnels.updateMobileDataTunnel(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onTogglePrimaryTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.updatePrimaryTunnel(
|
||||
when (tunnelConfig.isPrimaryTunnel) {
|
||||
true -> null
|
||||
false -> tunnelConfig
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleRestartOnPing(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||
appDataRepository.tunnels.save(
|
||||
tunnelConfig.copy(
|
||||
isPingEnabled = !tunnelConfig.isPingEnabled,
|
||||
),
|
||||
)
|
||||
fun onTogglePrimaryTunnel() = viewModelScope.launch {
|
||||
if (uiState.value.tunnel != null) {
|
||||
appDataRepository.tunnels.updatePrimaryTunnel(
|
||||
when (uiState.value.isDefaultTunnel) {
|
||||
true -> null
|
||||
false -> uiState.value.tunnel
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-9
@@ -8,8 +8,7 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import xyz.teamgravity.pin_lock_compose.PinLock
|
||||
@@ -17,11 +16,9 @@ import xyz.teamgravity.pin_lock_compose.PinLock
|
||||
@Composable
|
||||
fun PinLockScreen(navController: NavController, appViewModel: AppViewModel) {
|
||||
val context = LocalContext.current
|
||||
val snackbar = SnackbarController.current
|
||||
PinLock(
|
||||
title = { pinExists ->
|
||||
Text(
|
||||
color = MaterialTheme.colorScheme.onSecondary,
|
||||
text =
|
||||
if (pinExists) {
|
||||
stringResource(id = R.string.enter_pin)
|
||||
@@ -32,27 +29,27 @@ fun PinLockScreen(navController: NavController, appViewModel: AppViewModel) {
|
||||
},
|
||||
)
|
||||
},
|
||||
color = MaterialTheme.colorScheme.secondary,
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
onPinCorrect = {
|
||||
// pin is correct, navigate or hide pin lock
|
||||
if (context.isRunningOnTv()) {
|
||||
navController.navigate(Route.Main)
|
||||
navController.navigate(Screen.Main.route)
|
||||
} else {
|
||||
val isPopped = navController.popBackStack()
|
||||
if (!isPopped) {
|
||||
navController.navigate(Route.Main)
|
||||
navController.navigate(Screen.Main.route)
|
||||
}
|
||||
}
|
||||
},
|
||||
onPinIncorrect = {
|
||||
// pin is incorrect, show error
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
StringValue.StringResource(R.string.incorrect_pin).asString(context),
|
||||
)
|
||||
},
|
||||
onPinCreated = {
|
||||
// pin created for the first time, navigate or hide pin lock
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
StringValue.StringResource(R.string.pin_created).asString(context),
|
||||
)
|
||||
appViewModel.onPinLockEnabled()
|
||||
|
||||
+393
-332
@@ -5,7 +5,6 @@ import android.app.Activity
|
||||
import android.content.Context.POWER_SERVICE
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
@@ -46,6 +45,7 @@ import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -67,23 +67,21 @@ import com.google.accompanist.permissions.rememberPermissionState
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.VpnDeniedDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.BackgroundLocationDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.BackgroundLocationDisclosure
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LocationServicesDialog
|
||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.WildcardSupportingLabel
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getMessage
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.showToast
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
|
||||
@OptIn(
|
||||
@@ -94,17 +92,16 @@ import xyz.teamgravity.pin_lock_compose.PinManager
|
||||
fun SettingsScreen(
|
||||
viewModel: SettingsViewModel = hiltViewModel(),
|
||||
appViewModel: AppViewModel,
|
||||
uiState: AppUiState,
|
||||
navController: NavController,
|
||||
focusRequester: FocusRequester,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val focusManager = LocalFocusManager.current
|
||||
val snackbar = SnackbarController.current
|
||||
val scope = rememberCoroutineScope()
|
||||
val scrollState = rememberScrollState()
|
||||
val interactionSource = remember { MutableInteractionSource() }
|
||||
val isRunningOnTv = context.isRunningOnTv()
|
||||
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val kernelSupport by viewModel.kernelSupport.collectAsStateWithLifecycle()
|
||||
|
||||
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
@@ -112,7 +109,7 @@ fun SettingsScreen(
|
||||
var isBackgroundLocationGranted by remember { mutableStateOf(true) }
|
||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||
var showLocationServicesAlertDialog by remember { mutableStateOf(false) }
|
||||
val didExportFiles by remember { mutableStateOf(false) }
|
||||
var didExportFiles by remember { mutableStateOf(false) }
|
||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||
var showLocationDialog by remember { mutableStateOf(false) }
|
||||
|
||||
@@ -123,9 +120,12 @@ fun SettingsScreen(
|
||||
viewModel.checkKernelSupport()
|
||||
}
|
||||
|
||||
LaunchedEffect(uiState.settings.trustedNetworkSSIDs) {
|
||||
currentText = ""
|
||||
}
|
||||
val notificationPermissionState =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val startForResult =
|
||||
rememberLauncherForActivityResult(
|
||||
@@ -159,20 +159,22 @@ fun SettingsScreen(
|
||||
fun requestBatteryOptimizationsDisabled() {
|
||||
val intent =
|
||||
Intent().apply {
|
||||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||
data = Uri.parse("package:${context.packageName}")
|
||||
this.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||
data = Uri.fromParts("package", context.packageName, null)
|
||||
}
|
||||
startForResult.launch(intent)
|
||||
}
|
||||
|
||||
fun handleAutoTunnelToggle() {
|
||||
if (!uiState.generalState.isBatteryOptimizationDisableShown &&
|
||||
!isBatteryOptimizationsDisabled() && !context.isRunningOnTv()
|
||||
) {
|
||||
return requestBatteryOptimizationsDisabled()
|
||||
if (!uiState.isBatteryOptimizeDisableShown || !isBatteryOptimizationsDisabled()) return requestBatteryOptimizationsDisabled()
|
||||
if (notificationPermissionState != null && !notificationPermissionState.status.isGranted) {
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.notification_permission_required),
|
||||
)
|
||||
return notificationPermissionState.launchPermissionRequest()
|
||||
}
|
||||
val intent = if (!uiState.settings.isKernelEnabled) {
|
||||
VpnService.prepare(context)
|
||||
com.wireguard.android.backend.GoBackend.VpnService.prepare(context)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
@@ -182,7 +184,11 @@ fun SettingsScreen(
|
||||
|
||||
fun saveTrustedSSID() {
|
||||
if (currentText.isNotEmpty()) {
|
||||
viewModel.onSaveTrustedSSID(currentText)
|
||||
viewModel.onSaveTrustedSSID(currentText).onSuccess {
|
||||
currentText = ""
|
||||
}.onFailure {
|
||||
appViewModel.showSnackbarMessage(it.getMessage(context))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,9 +202,13 @@ fun SettingsScreen(
|
||||
}
|
||||
}
|
||||
|
||||
fun onRootDenied() = appViewModel.showSnackbarMessage(context.getString(R.string.error_root_denied))
|
||||
|
||||
fun onRootAccepted() = appViewModel.showSnackbarMessage(context.getString(R.string.root_accepted))
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
if (
|
||||
isRunningOnTv &&
|
||||
context.isRunningOnTv() &&
|
||||
Build.VERSION.SDK_INT == Build.VERSION_CODES.Q
|
||||
) {
|
||||
checkFineLocationGranted()
|
||||
@@ -218,7 +228,7 @@ fun SettingsScreen(
|
||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
|
||||
checkFineLocationGranted()
|
||||
}
|
||||
if (!uiState.generalState.isLocationDisclosureShown) {
|
||||
if (!uiState.isLocationDisclosureShown) {
|
||||
BackgroundLocationDisclosure(
|
||||
onDismiss = { viewModel.setLocationDisclosureShown() },
|
||||
onAttest = {
|
||||
@@ -249,359 +259,410 @@ fun SettingsScreen(
|
||||
AuthorizationPrompt(
|
||||
onSuccess = {
|
||||
showAuthPrompt = false
|
||||
viewModel.exportAllConfigs()
|
||||
scope.launch {
|
||||
viewModel.exportAllConfigs().onSuccess {
|
||||
appViewModel.showSnackbarMessage(context.getString(R.string.exported_configs_message))
|
||||
}.onFailure {
|
||||
appViewModel.showSnackbarMessage(context.getString(R.string.export_configs_failed))
|
||||
}
|
||||
}
|
||||
},
|
||||
onError = { _ ->
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.error_authentication_failed),
|
||||
)
|
||||
},
|
||||
onFailure = {
|
||||
showAuthPrompt = false
|
||||
snackbar.showMessage(
|
||||
appViewModel.showSnackbarMessage(
|
||||
context.getString(R.string.error_authorization_failed),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(scrollState)
|
||||
.clickable(
|
||||
indication = null,
|
||||
interactionSource = interactionSource,
|
||||
) {
|
||||
focusManager.clearFocus()
|
||||
},
|
||||
) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
if (uiState.isLocationDisclosureShown) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier =
|
||||
(
|
||||
if (isRunningOnTv) {
|
||||
Modifier
|
||||
.height(IntrinsicSize.Min)
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(top = 10.dp)
|
||||
} else {
|
||||
Modifier
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(top = 20.dp)
|
||||
}
|
||||
)
|
||||
.padding(bottom = 10.dp),
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(scrollState)
|
||||
.clickable(
|
||||
indication = null,
|
||||
interactionSource = interactionSource,
|
||||
) {
|
||||
focusManager.clearFocus()
|
||||
},
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.auto_tunneling),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.tunnel_on_wifi),
|
||||
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||
checked = uiState.settings.isTunnelOnWifiEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { checked ->
|
||||
if (!checked) viewModel.onToggleTunnelOnWifi()
|
||||
if (checked) {
|
||||
when (false) {
|
||||
isBackgroundLocationGranted -> showLocationDialog = true
|
||||
fineLocationState.status.isGranted -> showLocationDialog = true
|
||||
viewModel.isLocationEnabled(context) ->
|
||||
showLocationServicesAlertDialog = true
|
||||
else -> {
|
||||
viewModel.onToggleTunnelOnWifi()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier =
|
||||
if (uiState.settings.isAutoTunnelEnabled) {
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
(
|
||||
if (context.isRunningOnTv()) {
|
||||
Modifier
|
||||
.height(IntrinsicSize.Min)
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(top = 10.dp)
|
||||
} else {
|
||||
Modifier
|
||||
.focusRequester(focusRequester)
|
||||
},
|
||||
)
|
||||
if (uiState.settings.isTunnelOnWifiEnabled) {
|
||||
Column {
|
||||
FlowRow(
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(screenPadding)
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
) {
|
||||
uiState.settings.trustedNetworkSSIDs.forEach { ssid ->
|
||||
ClickableIconButton(
|
||||
onClick = {
|
||||
if (isRunningOnTv) {
|
||||
focusRequester.requestFocus()
|
||||
viewModel.onDeleteTrustedSSID(ssid)
|
||||
}
|
||||
},
|
||||
onIconClick = {
|
||||
if (isRunningOnTv) focusRequester.requestFocus()
|
||||
viewModel.onDeleteTrustedSSID(ssid)
|
||||
},
|
||||
text = ssid,
|
||||
icon = Icons.Filled.Close,
|
||||
)
|
||||
}
|
||||
if (uiState.settings.trustedNetworkSSIDs.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.none),
|
||||
fontStyle = FontStyle.Italic,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
}
|
||||
OutlinedTextField(
|
||||
value = currentText,
|
||||
onValueChange = { currentText = it },
|
||||
label = { Text(stringResource(R.string.add_trusted_ssid)) },
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(
|
||||
start = screenPadding,
|
||||
top = 5.dp,
|
||||
bottom = 10.dp,
|
||||
),
|
||||
supportingText = { WildcardSupportingLabel { context.openWebUrl(it) } },
|
||||
maxLines = 1,
|
||||
keyboardOptions =
|
||||
KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
keyboardActions = KeyboardActions(onDone = { saveTrustedSSID() }),
|
||||
trailingIcon = {
|
||||
if (currentText != "") {
|
||||
IconButton(onClick = { saveTrustedSSID() }) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Add,
|
||||
contentDescription =
|
||||
if (currentText == "") {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_empty_description,
|
||||
)
|
||||
} else {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_value_description,
|
||||
)
|
||||
},
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(top = 20.dp)
|
||||
}
|
||||
}
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.tunnel_mobile_data),
|
||||
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||
checked = uiState.settings.isTunnelOnMobileDataEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleTunnelOnMobileData() },
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.tunnel_on_ethernet),
|
||||
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||
checked = uiState.settings.isTunnelOnEthernetEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleTunnelOnEthernet() },
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.restart_on_ping),
|
||||
checked = uiState.settings.isPingEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleRestartOnPing() },
|
||||
)
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
(
|
||||
if (!uiState.settings.isAutoTunnelEnabled) {
|
||||
)
|
||||
.padding(bottom = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.auto_tunneling),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(id = R.string.tunnel_on_wifi),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
checked = uiState.settings.isTunnelOnWifiEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleTunnelOnWifi() },
|
||||
modifier =
|
||||
if (uiState.settings.isAutoTunnelEnabled) {
|
||||
Modifier
|
||||
} else {
|
||||
Modifier.focusRequester(
|
||||
focusRequester,
|
||||
Modifier
|
||||
.focusRequester(focusRequester)
|
||||
},
|
||||
)
|
||||
if (uiState.settings.isTunnelOnWifiEnabled) {
|
||||
Column {
|
||||
FlowRow(
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(screenPadding)
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(5.dp),
|
||||
) {
|
||||
uiState.settings.trustedNetworkSSIDs.forEach { ssid ->
|
||||
ClickableIconButton(
|
||||
onClick = {
|
||||
if (context.isRunningOnTv()) {
|
||||
focusRequester.requestFocus()
|
||||
viewModel.onDeleteTrustedSSID(ssid)
|
||||
}
|
||||
},
|
||||
onIconClick = {
|
||||
if (context.isRunningOnTv()) focusRequester.requestFocus()
|
||||
viewModel.onDeleteTrustedSSID(ssid)
|
||||
},
|
||||
text = ssid,
|
||||
icon = Icons.Filled.Close,
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
)
|
||||
}
|
||||
if (uiState.settings.trustedNetworkSSIDs.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.none),
|
||||
fontStyle = FontStyle.Italic,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
}
|
||||
}
|
||||
OutlinedTextField(
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
value = currentText,
|
||||
onValueChange = { currentText = it },
|
||||
label = { Text(stringResource(R.string.add_trusted_ssid)) },
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(
|
||||
start = screenPadding,
|
||||
top = 5.dp,
|
||||
bottom = 10.dp,
|
||||
),
|
||||
maxLines = 1,
|
||||
keyboardOptions =
|
||||
KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.None,
|
||||
imeAction = ImeAction.Done,
|
||||
),
|
||||
keyboardActions = KeyboardActions(onDone = { saveTrustedSSID() }),
|
||||
trailingIcon = {
|
||||
if (currentText != "") {
|
||||
IconButton(onClick = { saveTrustedSSID() }) {
|
||||
Icon(
|
||||
imageVector = Icons.Outlined.Add,
|
||||
contentDescription =
|
||||
if (currentText == "") {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_empty_description,
|
||||
)
|
||||
} else {
|
||||
stringResource(
|
||||
id =
|
||||
R.string
|
||||
.trusted_ssid_value_description,
|
||||
)
|
||||
},
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
)
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(
|
||||
onClick = {
|
||||
if (uiState.tunnels.isEmpty()) return@TextButton context.showToast(R.string.tunnel_required)
|
||||
handleAutoTunnelToggle()
|
||||
},
|
||||
) {
|
||||
val autoTunnelButtonText =
|
||||
if (uiState.settings.isAutoTunnelEnabled) {
|
||||
stringResource(R.string.disable_auto_tunnel)
|
||||
} else {
|
||||
stringResource(id = R.string.enable_auto_tunnel)
|
||||
}
|
||||
Text(autoTunnelButtonText)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(vertical = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.backend),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.use_kernel),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled ||
|
||||
(uiState.vpnState.status == TunnelState.UP) ||
|
||||
!kernelSupport
|
||||
),
|
||||
checked = uiState.settings.isKernelEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
viewModel.onToggleKernelMode()
|
||||
},
|
||||
)
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(
|
||||
onClick = {
|
||||
viewModel.onRequestRoot()
|
||||
},
|
||||
) {
|
||||
Text(stringResource(R.string.request_root))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(vertical = 10.dp)
|
||||
.padding(bottom = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.other),
|
||||
padding = screenPadding,
|
||||
)
|
||||
if (!isRunningOnTv) {
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.always_on_vpn_support),
|
||||
enabled = !(
|
||||
uiState.settings.isTunnelOnWifiEnabled ||
|
||||
uiState.settings.isTunnelOnWifiEnabled ||
|
||||
uiState.settings.isTunnelOnMobileDataEnabled
|
||||
stringResource(R.string.tunnel_mobile_data),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||
checked = uiState.settings.isTunnelOnMobileDataEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleAlwaysOnVPN() },
|
||||
onCheckChanged = { viewModel.onToggleTunnelOnMobileData() },
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.enabled_app_shortcuts),
|
||||
enabled = true,
|
||||
checked = uiState.settings.isShortcutsEnabled,
|
||||
stringResource(id = R.string.tunnel_on_ethernet),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
checked = uiState.settings.isTunnelOnEthernetEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleShortcutsEnabled() },
|
||||
onCheckChanged = { viewModel.onToggleTunnelOnEthernet() },
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.restart_on_ping),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled
|
||||
),
|
||||
checked = uiState.settings.isPingEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleRestartOnPing() },
|
||||
)
|
||||
}
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.restart_at_boot),
|
||||
enabled = true,
|
||||
checked = uiState.settings.isRestoreOnBootEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
viewModel.onToggleRestartAtBoot()
|
||||
},
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.enable_app_lock),
|
||||
enabled = true,
|
||||
checked = uiState.generalState.isPinLockEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
if (uiState.generalState.isPinLockEnabled) {
|
||||
appViewModel.onPinLockDisabled()
|
||||
} else {
|
||||
// TODO may want to show a dialog before proceeding in the future
|
||||
PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||
navController.navigate(Route.Lock)
|
||||
}
|
||||
},
|
||||
)
|
||||
if (!isRunningOnTv) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
(
|
||||
if (!uiState.settings.isAutoTunnelEnabled) {
|
||||
Modifier
|
||||
} else {
|
||||
Modifier.focusRequester(
|
||||
focusRequester,
|
||||
)
|
||||
}
|
||||
)
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(
|
||||
enabled = !didExportFiles,
|
||||
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||
onClick = {
|
||||
if (uiState.tunnels.isEmpty()) return@TextButton context.showToast(R.string.tunnel_required)
|
||||
showAuthPrompt = true
|
||||
if (
|
||||
uiState.settings.isTunnelOnWifiEnabled &&
|
||||
!uiState.settings.isAutoTunnelEnabled
|
||||
) {
|
||||
when (false) {
|
||||
isBackgroundLocationGranted -> showLocationDialog = true
|
||||
fineLocationState.status.isGranted -> showLocationDialog = true
|
||||
viewModel.isLocationEnabled(context) ->
|
||||
showLocationServicesAlertDialog = true
|
||||
|
||||
else -> {
|
||||
handleAutoTunnelToggle()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
handleAutoTunnelToggle()
|
||||
}
|
||||
},
|
||||
) {
|
||||
Text(stringResource(R.string.export_configs))
|
||||
val autoTunnelButtonText =
|
||||
if (uiState.settings.isAutoTunnelEnabled) {
|
||||
stringResource(R.string.disable_auto_tunnel)
|
||||
} else {
|
||||
stringResource(id = R.string.enable_auto_tunnel)
|
||||
}
|
||||
Text(autoTunnelButtonText)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(vertical = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.backend),
|
||||
padding = screenPadding,
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.use_amnezia),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled ||
|
||||
(uiState.vpnState.status == TunnelState.UP) || uiState.settings.isKernelEnabled
|
||||
),
|
||||
checked = uiState.settings.isAmneziaEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
viewModel.onToggleAmnezia()
|
||||
},
|
||||
)
|
||||
if (kernelSupport) {
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.use_kernel),
|
||||
enabled =
|
||||
!(
|
||||
uiState.settings.isAutoTunnelEnabled ||
|
||||
uiState.settings.isAlwaysOnVpnEnabled ||
|
||||
(uiState.vpnState.status == TunnelState.UP)
|
||||
),
|
||||
checked = uiState.settings.isKernelEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
scope.launch {
|
||||
viewModel.onToggleKernelMode({ onRootAccepted() }, { onRootDenied() })
|
||||
}
|
||||
},
|
||||
)
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(
|
||||
onClick = {
|
||||
viewModel.requestRoot({ onRootAccepted() }, { onRootDenied() })
|
||||
},
|
||||
) {
|
||||
Text(stringResource(R.string.request_root))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 2.dp,
|
||||
shadowElevation = 2.dp,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth(fillMaxWidth)
|
||||
.padding(vertical = 10.dp)
|
||||
.padding(bottom = 10.dp),
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.Start,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
modifier = Modifier.padding(15.dp),
|
||||
) {
|
||||
SectionTitle(
|
||||
title = stringResource(id = R.string.other),
|
||||
padding = screenPadding,
|
||||
)
|
||||
if (!context.isRunningOnTv()) {
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.always_on_vpn_support),
|
||||
enabled = !uiState.settings.isAutoTunnelEnabled,
|
||||
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleAlwaysOnVPN() },
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.enabled_app_shortcuts),
|
||||
enabled = true,
|
||||
checked = uiState.settings.isShortcutsEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = { viewModel.onToggleShortcutsEnabled() },
|
||||
)
|
||||
}
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.restart_at_boot),
|
||||
enabled = true,
|
||||
checked = uiState.settings.isRestoreOnBootEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
viewModel.onToggleRestartAtBoot()
|
||||
},
|
||||
)
|
||||
ConfigurationToggle(
|
||||
stringResource(R.string.enable_app_lock),
|
||||
enabled = true,
|
||||
checked = uiState.isPinLockEnabled,
|
||||
padding = screenPadding,
|
||||
onCheckChanged = {
|
||||
if (uiState.isPinLockEnabled) {
|
||||
appViewModel.onPinLockDisabled()
|
||||
} else {
|
||||
// TODO may want to show a dialog before proceeding in the future
|
||||
PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||
navController.navigate(Screen.Lock.route)
|
||||
}
|
||||
},
|
||||
)
|
||||
if (!context.isRunningOnTv()) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = 5.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
TextButton(
|
||||
enabled = !didExportFiles,
|
||||
onClick = {
|
||||
if (uiState.tunnels.isEmpty()) return@TextButton context.showToast(R.string.tunnel_required)
|
||||
showAuthPrompt = true
|
||||
},
|
||||
) {
|
||||
Text(stringResource(R.string.export_configs))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||
|
||||
data class SettingsUiState(
|
||||
val settings: Settings = Settings(),
|
||||
val tunnels: List<TunnelConfig> = emptyList(),
|
||||
val vpnState: VpnState = VpnState(),
|
||||
val isLocationDisclosureShown: Boolean = true,
|
||||
val isBatteryOptimizeDisableShown: Boolean = false,
|
||||
val isPinLockEnabled: Boolean = false,
|
||||
)
|
||||
+146
-143
@@ -7,23 +7,25 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.wireguard.android.backend.WgQuickBackend
|
||||
import com.wireguard.android.util.RootShell
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Provider
|
||||
@@ -33,29 +35,45 @@ class SettingsViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val appDataRepository: AppDataRepository,
|
||||
private val serviceManager: ServiceManager,
|
||||
private val rootShell: Provider<RootShell>,
|
||||
private val fileUtils: FileUtils,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
tunnelService: TunnelService,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _kernelSupport = MutableStateFlow(false)
|
||||
val kernelSupport = _kernelSupport.asStateFlow()
|
||||
private val settings = appDataRepository.settings.getSettingsFlow()
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, Settings())
|
||||
|
||||
fun onSaveTrustedSSID(ssid: String) = viewModelScope.launch {
|
||||
val uiState =
|
||||
combine(
|
||||
appDataRepository.settings.getSettingsFlow(),
|
||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||
tunnelService.vpnState,
|
||||
appDataRepository.appState.generalStateFlow,
|
||||
) { settings, tunnels, tunnelState, generalState ->
|
||||
SettingsUiState(
|
||||
settings,
|
||||
tunnels,
|
||||
tunnelState,
|
||||
generalState.isLocationDisclosureShown,
|
||||
generalState.isBatteryOptimizationDisableShown,
|
||||
generalState.isPinLockEnabled,
|
||||
)
|
||||
}
|
||||
.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
SettingsUiState(),
|
||||
)
|
||||
|
||||
fun onSaveTrustedSSID(ssid: String): Result<Unit> {
|
||||
val trimmed = ssid.trim()
|
||||
with(settings.value) {
|
||||
if (!trustedNetworkSSIDs.contains(trimmed)) {
|
||||
this.trustedNetworkSSIDs.add(ssid)
|
||||
appDataRepository.settings.save(this)
|
||||
} else {
|
||||
SnackbarController.showMessage(
|
||||
StringValue.StringResource(
|
||||
R.string.error_ssid_exists,
|
||||
),
|
||||
)
|
||||
}
|
||||
return if (!uiState.value.settings.trustedNetworkSSIDs.contains(trimmed)) {
|
||||
uiState.value.settings.trustedNetworkSSIDs.add(trimmed)
|
||||
saveSettings(uiState.value.settings)
|
||||
Result.success(Unit)
|
||||
} else {
|
||||
Result.failure(WgTunnelExceptions.SsidConflict())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,70 +85,61 @@ constructor(
|
||||
appDataRepository.appState.setBatteryOptimizationDisableShown(true)
|
||||
}
|
||||
|
||||
fun onToggleTunnelOnMobileData() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isTunnelOnMobileDataEnabled = !this.isTunnelOnMobileDataEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
fun onToggleTunnelOnMobileData() {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isTunnelOnMobileDataEnabled = !uiState.value.settings.isTunnelOnMobileDataEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onDeleteTrustedSSID(ssid: String) = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
trustedNetworkSSIDs = (this.trustedNetworkSSIDs - ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
fun onDeleteTrustedSSID(ssid: String) {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
trustedNetworkSSIDs =
|
||||
(uiState.value.settings.trustedNetworkSSIDs - ssid).toMutableList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun exportTunnels(files: List<File>) = viewModelScope.launch {
|
||||
fileUtils.saveFilesToZip(files).onSuccess {
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.exported_configs_message))
|
||||
}.onFailure {
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.export_configs_failed))
|
||||
}
|
||||
suspend fun onExportTunnels(files: List<File>): Result<Unit> {
|
||||
return fileUtils.saveFilesToZip(files)
|
||||
}
|
||||
|
||||
fun onToggleAutoTunnel(context: Context) = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
var isAutoTunnelPaused = this.isAutoTunnelPaused
|
||||
if (isAutoTunnelEnabled) {
|
||||
ServiceManager.stopWatcherService(context)
|
||||
} else {
|
||||
ServiceManager.startWatcherService(context)
|
||||
isAutoTunnelPaused = false
|
||||
}
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isAutoTunnelEnabled = !isAutoTunnelEnabled,
|
||||
isAutoTunnelPaused = isAutoTunnelPaused,
|
||||
),
|
||||
)
|
||||
val isAutoTunnelEnabled = uiState.value.settings.isAutoTunnelEnabled
|
||||
var isAutoTunnelPaused = uiState.value.settings.isAutoTunnelPaused
|
||||
|
||||
if (isAutoTunnelEnabled) {
|
||||
serviceManager.stopWatcherService(context)
|
||||
} else {
|
||||
serviceManager.startWatcherService(context)
|
||||
isAutoTunnelPaused = false
|
||||
}
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isAutoTunnelEnabled = !isAutoTunnelEnabled,
|
||||
isAutoTunnelPaused = isAutoTunnelPaused,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleAlwaysOnVPN() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isAlwaysOnVpnEnabled = !isAlwaysOnVpnEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isAlwaysOnVpnEnabled = !uiState.value.settings.isAlwaysOnVpnEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleTunnelOnEthernet() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isTunnelOnEthernetEnabled = !isTunnelOnEthernetEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
|
||||
|
||||
fun onToggleTunnelOnEthernet() {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isTunnelOnEthernetEnabled = !uiState.value.settings.isTunnelOnEthernetEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun isLocationEnabled(context: Context): Boolean {
|
||||
@@ -141,74 +150,73 @@ constructor(
|
||||
return LocationManagerCompat.isLocationEnabled(locationManager)
|
||||
}
|
||||
|
||||
fun onToggleShortcutsEnabled() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
this.copy(
|
||||
isShortcutsEnabled = !isShortcutsEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
fun onToggleShortcutsEnabled() {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isShortcutsEnabled = !uiState.value.settings.isShortcutsEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun saveKernelMode(enabled: Boolean) = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
this.copy(
|
||||
isKernelEnabled = enabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
private fun saveKernelMode(enabled: Boolean) {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isKernelEnabled = enabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleTunnelOnWifi() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isTunnelOnWifiEnabled = !isTunnelOnWifiEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
fun onToggleTunnelOnWifi() {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isTunnelOnWifiEnabled = !uiState.value.settings.isTunnelOnWifiEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleAmnezia() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
if (isKernelEnabled) {
|
||||
saveKernelMode(false)
|
||||
}
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isAmneziaEnabled = !isAmneziaEnabled,
|
||||
),
|
||||
)
|
||||
if (uiState.value.settings.isKernelEnabled) {
|
||||
saveKernelMode(false)
|
||||
}
|
||||
saveAmneziaMode(!uiState.value.settings.isAmneziaEnabled)
|
||||
}
|
||||
|
||||
fun onToggleKernelMode() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
if (!isKernelEnabled) {
|
||||
requestRoot().onSuccess {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
private fun saveAmneziaMode(on: Boolean) {
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isAmneziaEnabled = on,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun onToggleKernelMode(onSuccess: () -> Unit, onFailure: () -> Unit) = viewModelScope.launch {
|
||||
if (!uiState.value.settings.isKernelEnabled) {
|
||||
requestRoot(
|
||||
{
|
||||
onSuccess()
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isKernelEnabled = true,
|
||||
isAmneziaEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
saveKernelMode(enabled = false)
|
||||
}
|
||||
},
|
||||
{
|
||||
onFailure()
|
||||
saveKernelMode(enabled = false)
|
||||
},
|
||||
)
|
||||
} else {
|
||||
saveKernelMode(enabled = false)
|
||||
}
|
||||
}
|
||||
|
||||
fun onToggleRestartOnPing() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isPingEnabled = !isPingEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isPingEnabled = !uiState.value.settings.isPingEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun checkKernelSupport() = viewModelScope.launch {
|
||||
@@ -222,36 +230,31 @@ constructor(
|
||||
}
|
||||
|
||||
fun onToggleRestartAtBoot() = viewModelScope.launch {
|
||||
with(settings.value) {
|
||||
appDataRepository.settings.save(
|
||||
copy(
|
||||
isRestoreOnBootEnabled = !isRestoreOnBootEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
saveSettings(
|
||||
uiState.value.settings.copy(
|
||||
isRestoreOnBootEnabled = !uiState.value.settings.isRestoreOnBootEnabled,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun requestRoot(): Result<Unit> {
|
||||
return withContext(ioDispatcher) {
|
||||
kotlin.runCatching {
|
||||
rootShell.get().start()
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.root_accepted))
|
||||
}.onFailure {
|
||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_root_denied))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onRequestRoot() = viewModelScope.launch {
|
||||
requestRoot()
|
||||
}
|
||||
|
||||
fun exportAllConfigs() = viewModelScope.launch {
|
||||
fun requestRoot(onSuccess: () -> Unit, onFailure: () -> Unit) = viewModelScope.launch(ioDispatcher) {
|
||||
kotlin.runCatching {
|
||||
rootShell.get().start()
|
||||
Timber.i("Root shell accepted!")
|
||||
onSuccess()
|
||||
}.onFailure {
|
||||
onFailure()
|
||||
}.onSuccess {
|
||||
onSuccess()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun exportAllConfigs(): Result<Unit> {
|
||||
return kotlin.runCatching {
|
||||
val tunnels = appDataRepository.tunnels.getAll()
|
||||
val wgFiles = fileUtils.createWgFiles(tunnels)
|
||||
val amFiles = fileUtils.createAmFiles(tunnels)
|
||||
exportTunnels(wgFiles + amFiles)
|
||||
onExportTunnels(wgFiles + amFiles)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.components
|
||||
|
||||
import androidx.compose.foundation.text.ClickableText
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.SpanStyle
|
||||
import androidx.compose.ui.text.buildAnnotatedString
|
||||
import androidx.compose.ui.text.font.FontStyle
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.withStyle
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
@Composable
|
||||
fun WildcardSupportingLabel(onClick: (url: String) -> Unit) {
|
||||
// TODO update link when docs are fully updated
|
||||
val gettingStarted =
|
||||
buildAnnotatedString {
|
||||
pushStringAnnotation(
|
||||
tag = "details",
|
||||
annotation = stringResource(id = R.string.docs_wildcards),
|
||||
)
|
||||
withStyle(
|
||||
style = SpanStyle(color = MaterialTheme.colorScheme.primary),
|
||||
) {
|
||||
append(stringResource(id = R.string.wildcard_supported))
|
||||
}
|
||||
pop()
|
||||
}
|
||||
ClickableText(
|
||||
text = gettingStarted,
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Start,
|
||||
fontStyle = FontStyle.Italic,
|
||||
),
|
||||
) {
|
||||
gettingStarted.getStringAnnotations(tag = "details", it, it)
|
||||
.firstOrNull()?.let { annotation ->
|
||||
onClick(annotation.item)
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-5
@@ -27,6 +27,7 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
@@ -42,20 +43,23 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextDecoration
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavController
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchSupportEmail
|
||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||
|
||||
@Composable
|
||||
fun SupportScreen(navController: NavController, focusRequester: FocusRequester, appUiState: AppUiState) {
|
||||
fun SupportScreen(viewModel: SupportViewModel = hiltViewModel(), navController: NavController, focusRequester: FocusRequester) {
|
||||
val context = LocalContext.current
|
||||
val fillMaxWidth = .85f
|
||||
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Top,
|
||||
@@ -244,7 +248,7 @@ fun SupportScreen(navController: NavController, focusRequester: FocusRequester,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
)
|
||||
TextButton(
|
||||
onClick = { navController.navigate(Route.Logs) },
|
||||
onClick = { navController.navigate(Screen.Support.Logs.route) },
|
||||
modifier = Modifier.padding(vertical = 5.dp),
|
||||
) {
|
||||
Row(
|
||||
@@ -297,7 +301,7 @@ fun SupportScreen(navController: NavController, focusRequester: FocusRequester,
|
||||
buildAnnotatedString {
|
||||
append(stringResource(R.string.mode))
|
||||
append(": ")
|
||||
when (appUiState.settings.isKernelEnabled) {
|
||||
when (uiState.settings.isKernelEnabled) {
|
||||
true -> append(stringResource(id = R.string.kernel))
|
||||
false -> append(stringResource(id = R.string.userspace))
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
||||
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||
|
||||
data class SupportUiState(val settings: Settings = Settings())
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class SupportViewModel
|
||||
@Inject
|
||||
constructor(settingsRepository: SettingsRepository) :
|
||||
ViewModel() {
|
||||
val uiState =
|
||||
settingsRepository
|
||||
.getSettingsFlow()
|
||||
.map { SupportUiState(it) }
|
||||
.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||
SupportUiState(),
|
||||
)
|
||||
}
|
||||
@@ -4,10 +4,10 @@ object Constants {
|
||||
const val BASE_LOG_FILE_NAME = "wg_tunnel_logs"
|
||||
const val LOG_BUFFER_SIZE = 3_000L
|
||||
|
||||
const val MANUAL_TUNNEL_CONFIG_ID = 0
|
||||
const val MANUAL_TUNNEL_CONFIG_ID = "0"
|
||||
const val BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT = 10 * 60 * 1_000L // 10 minutes
|
||||
const val VPN_STATISTIC_CHECK_INTERVAL = 1_000L
|
||||
const val WATCHER_COLLECTION_DELAY = 3_000L
|
||||
|
||||
const val CONF_FILE_EXTENSION = ".conf"
|
||||
const val ZIP_FILE_EXTENSION = ".zip"
|
||||
const val URI_CONTENT_SCHEME = "content"
|
||||
@@ -16,6 +16,7 @@ object Constants {
|
||||
const val ZIP_FILE_MIME_TYPE = "application/zip"
|
||||
const val GOOGLE_TV_EXPLORER_STUB = "com.google.android.tv.frameworkpackagestubs"
|
||||
const val ANDROID_TV_EXPLORER_STUB = "com.android.tv.frameworkpackagestubs"
|
||||
const val ALWAYS_ON_VPN_ACTION = "android.net.VpnService"
|
||||
const val VPN_SETTINGS_PACKAGE = "android.net.vpn.SETTINGS"
|
||||
const val EMAIL_MIME_TYPE = "plain/text"
|
||||
const val SYSTEM_EXEMPT_SERVICE_TYPE_ID = 1024
|
||||
@@ -27,11 +28,12 @@ object Constants {
|
||||
|
||||
const val DEFAULT_PING_IP = "1.1.1.1"
|
||||
const val PING_TIMEOUT = 5_000L
|
||||
const val VPN_RESTART_DELAY = 1_000L
|
||||
const val PING_INTERVAL = 60_000L
|
||||
const val PING_COOLDOWN = PING_INTERVAL * 60 // one hour
|
||||
|
||||
const val UNREADABLE_SSID = "<unknown ssid>"
|
||||
|
||||
val amProperties = listOf("Jc", "Jmin", "Jmax", "S1", "S2", "H1", "H2", "H3", "H4")
|
||||
val amneziaProperties = listOf("Jc", "Jmin", "Jmax", "S1", "S2", "H1", "H2", "H3", "H4")
|
||||
const val QR_CODE_NAME_PROPERTY = "# Name ="
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util
|
||||
|
||||
object InvalidFileExtensionException : Exception() {
|
||||
private fun readResolve(): Any = InvalidFileExtensionException
|
||||
}
|
||||
|
||||
object FileReadException : Exception() {
|
||||
private fun readResolve(): Any = FileReadException
|
||||
}
|
||||
|
||||
object ConfigExportException : Exception() {
|
||||
private fun readResolve(): Any = ConfigExportException
|
||||
}
|
||||
@@ -118,7 +118,7 @@ class FileUtils(
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
Result.failure(ConfigExportException)
|
||||
Result.failure(WgTunnelExceptions.ConfigExportFailed())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util
|
||||
|
||||
import android.content.Context
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
|
||||
sealed class WgTunnelExceptions : Exception() {
|
||||
abstract fun getMessage(context: Context): String
|
||||
|
||||
data class General(private val userMessage: StringValue) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class SsidConflict(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_ssid_exists,
|
||||
),
|
||||
) :
|
||||
WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class ConfigExportFailed(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.export_configs_failed,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class ConfigParseError(private val appendMessage: StringValue = StringValue.Empty) :
|
||||
WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return StringValue.StringResource(R.string.config_parse_error).asString(context) + (
|
||||
if (appendMessage != StringValue.Empty) ": ${appendMessage.asString(context)}" else ""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
data class RootDenied(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_root_denied,
|
||||
),
|
||||
) :
|
||||
WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class InvalidQrCode(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_invalid_code,
|
||||
),
|
||||
) :
|
||||
WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class InvalidFileExtension(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_file_extension,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class FileReadFailed(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_file_format,
|
||||
),
|
||||
) :
|
||||
WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class AuthenticationFailed(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_authentication_failed,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class AuthorizationFailed(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_authorization_failed,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class BackgroundLocationRequired(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.background_location_required,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class LocationServicesRequired(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.location_services_required,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class PreciseLocationRequired(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.precise_location_required,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
data class FileExplorerRequired(
|
||||
private val userMessage: StringValue =
|
||||
StringValue.StringResource(
|
||||
R.string.error_no_file_explorer,
|
||||
),
|
||||
) : WgTunnelExceptions() {
|
||||
override fun getMessage(context: Context): String {
|
||||
return userMessage.asString(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
-14
@@ -3,7 +3,6 @@ package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
@@ -77,16 +76,3 @@ fun <T> CoroutineScope.asChannel(flow: Flow<T>): ReceiveChannel<T> = produce {
|
||||
channel.send(value)
|
||||
}
|
||||
}
|
||||
|
||||
fun Job?.onNotRunning(callback: () -> Unit) {
|
||||
if (this == null || this.isCompleted || this.isCompleted) {
|
||||
callback.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
fun Job.cancelWithMessage(message: String) {
|
||||
kotlin.runCatching {
|
||||
this.cancel()
|
||||
Timber.i(message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageInfo
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||
import java.math.BigDecimal
|
||||
import java.text.DecimalFormat
|
||||
|
||||
@@ -17,3 +21,10 @@ fun <T> List<T>.removeAt(index: Int): List<T> = toMutableList().apply { this.rem
|
||||
typealias TunnelConfigs = List<TunnelConfig>
|
||||
|
||||
typealias Packages = List<PackageInfo>
|
||||
|
||||
fun Throwable.getMessage(context: Context): String {
|
||||
return when (this) {
|
||||
is WgTunnelExceptions -> this.getMessage(context)
|
||||
else -> this.message ?: StringValue.StringResource(R.string.unknown_error).asString(context)
|
||||
}
|
||||
}
|
||||
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
|
||||
import timber.log.Timber
|
||||
import java.util.regex.Pattern
|
||||
|
||||
fun String.isValidIpv4orIpv6Address(): Boolean {
|
||||
val ipv4Pattern = Pattern.compile(
|
||||
"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\$",
|
||||
)
|
||||
val ipv6Pattern = Pattern.compile(
|
||||
"^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}\$",
|
||||
)
|
||||
return ipv4Pattern.matcher(this).matches() || ipv6Pattern.matcher(this).matches()
|
||||
}
|
||||
|
||||
fun List<String>.isMatchingToWildcardList(value: String): Boolean {
|
||||
val excludeValues = this.filter { it.startsWith("!") }.map { it.removePrefix("!").toRegexWithWildcards() }
|
||||
Timber.d("Excluded values: $excludeValues")
|
||||
val includedValues = this.filter { !it.startsWith("!") }.map { it.toRegexWithWildcards() }
|
||||
Timber.d("Included values: $includedValues")
|
||||
val matches = includedValues.filter { it.matches(value) }
|
||||
val excludedMatches = excludeValues.filter { it.matches(value) }
|
||||
Timber.d("Excluded matches: $excludedMatches")
|
||||
Timber.d("Matches: $matches")
|
||||
return matches.isNotEmpty() && excludedMatches.isEmpty()
|
||||
}
|
||||
|
||||
fun String.toRegexWithWildcards(): Regex {
|
||||
return this.replace("*", ".*").replace("?", ".").toRegex()
|
||||
}
|
||||
+1
-21
@@ -1,13 +1,10 @@
|
||||
package com.zaneschepke.wireguardautotunnel.util.extensions
|
||||
|
||||
import com.wireguard.config.Peer
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||
import org.amnezia.awg.config.Config
|
||||
import timber.log.Timber
|
||||
import java.net.InetAddress
|
||||
|
||||
fun TunnelStatistics.mapPeerStats(): Map<org.amnezia.awg.crypto.Key, TunnelStatistics.PeerStats?> {
|
||||
return this.getPeers().associateWith { key -> (this.peerStats(key)) }
|
||||
@@ -31,30 +28,13 @@ fun TunnelStatistics.PeerStats.handshakeStatus(): HandshakeStatus {
|
||||
}
|
||||
}
|
||||
|
||||
fun Peer.isReachable(): Boolean {
|
||||
val host =
|
||||
if (this.endpoint.isPresent &&
|
||||
this.endpoint.get().resolved.isPresent
|
||||
) {
|
||||
this.endpoint.get().resolved.get().host
|
||||
} else {
|
||||
Constants.DEFAULT_PING_IP
|
||||
}
|
||||
Timber.i("Checking reachability of peer: $host")
|
||||
val reachable =
|
||||
InetAddress.getByName(host)
|
||||
.isReachable(Constants.PING_TIMEOUT.toInt())
|
||||
Timber.i("Result: reachable - $reachable")
|
||||
return reachable
|
||||
}
|
||||
|
||||
fun Config.toWgQuickString(): String {
|
||||
val amQuick = toAwgQuickString(true)
|
||||
val lines = amQuick.lines().toMutableList()
|
||||
val linesIterator = lines.iterator()
|
||||
while (linesIterator.hasNext()) {
|
||||
val next = linesIterator.next()
|
||||
Constants.amProperties.forEach {
|
||||
Constants.amneziaProperties.forEach {
|
||||
if (next.startsWith(it, ignoreCase = true)) {
|
||||
linesIterator.remove()
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@
|
||||
<string name="watcher_channel_name">Watcher Notification Channel</string>
|
||||
<string name="github_url" translatable="false">https://github.com/zaneschepke/wgtunnel/issues</string>
|
||||
<string name="docs_url" translatable="false">https://zaneschepke.com/wgtunnel-docs/overview.html</string>
|
||||
<string name="docs_features" translatable="false">https://zaneschepke.com/wgtunnel-docs/features.html</string>
|
||||
<string name="privacy_policy_url" translatable="false">https://zaneschepke.com/wgtunnel-docs/privacypolicy.html</string>
|
||||
<string name="docs_wildcards" translatable="false" >https://zaneschepke.com/wgtunnel-docs/features.html#wildcard-wi-fi-name-support</string>
|
||||
<string name="error_file_extension">File is not a .conf or .zip</string>
|
||||
<string name="error_file_extension">File is not a .conf or .zip</string>
|
||||
<string name="turn_off_tunnel">Action requires tunnel off</string>
|
||||
<string name="no_tunnels">No tunnels added yet!</string>
|
||||
<string name="discord_url" translatable="false">https://discord.gg/rbRRNh6H7V</string>
|
||||
@@ -188,12 +186,4 @@
|
||||
<string name="app_settings">app settings</string>
|
||||
<string name="background_location_message2">to make sure these permissions are enabled.</string>
|
||||
<string name="root_accepted">Root shell accepted</string>
|
||||
<string name="set_custom_ping_ip">Set custom ping ip</string>
|
||||
<string name="default_ping_ip">(optional, defaults to peers)</string>
|
||||
<string name="set_custom_ping_internal">Ping interval (sec)</string>
|
||||
<string name="optional_default">"optional, default: "</string>
|
||||
<string name="set_custom_ping_cooldown">Ping restart cooldown (sec)</string>
|
||||
<string name="wildcard_supported">Learn about supported wildcards.</string>
|
||||
<string name="details">details</string>
|
||||
<string name="show_amnezia_properties">Show Amnezia properties</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
object Constants {
|
||||
const val VERSION_NAME = "3.5.2"
|
||||
const val VERSION_NAME = "3.5.1"
|
||||
const val JVM_TARGET = "17"
|
||||
const val VERSION_CODE = 35200
|
||||
const val VERSION_CODE = 35102
|
||||
const val TARGET_SDK = 34
|
||||
const val MIN_SDK = 26
|
||||
const val APP_ID = "com.zaneschepke.wireguardautotunnel"
|
||||
@@ -15,5 +15,9 @@ object Constants {
|
||||
const val RELEASE = "release"
|
||||
const val NIGHTLY = "nightly"
|
||||
const val PRERELEASE = "prerelease"
|
||||
const val DEBUG = "debug"
|
||||
const val TYPE = "type"
|
||||
|
||||
const val NIGHTLY_CODE = 42
|
||||
const val PRERELEASE_CODE = 53
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
What's new:
|
||||
- Added wildcard support for wifi names
|
||||
- Fix slowness on mobile data
|
||||
- Various bug fixes and improvements
|
||||
- UI optimizations
|
||||
+14
-12
@@ -1,6 +1,6 @@
|
||||
[versions]
|
||||
accompanist = "0.36.0"
|
||||
activityCompose = "1.9.2"
|
||||
accompanist = "0.34.0"
|
||||
activityCompose = "1.9.1"
|
||||
amneziawgAndroid = "1.2.2"
|
||||
androidx-junit = "1.2.1"
|
||||
appcompat = "1.7.0"
|
||||
@@ -8,24 +8,25 @@ biometricKtx = "1.2.0-alpha05"
|
||||
coreGoogleShortcuts = "1.1.0"
|
||||
coreKtx = "1.13.1"
|
||||
datastorePreferences = "1.1.1"
|
||||
desugar_jdk_libs = "2.1.2"
|
||||
desugar_jdk_libs = "2.0.4"
|
||||
espressoCore = "3.6.1"
|
||||
hiltAndroid = "2.52"
|
||||
hiltNavigationCompose = "1.2.0"
|
||||
junit = "4.13.2"
|
||||
kotlinx-serialization-json = "1.7.3"
|
||||
lifecycle-runtime-compose = "2.8.6"
|
||||
material3 = "1.3.0"
|
||||
navigationCompose = "2.8.1"
|
||||
kotlinx-serialization-json = "1.7.1"
|
||||
lifecycle-runtime-compose = "2.8.4"
|
||||
material3 = "1.2.1"
|
||||
multifabVersion = "1.1.1"
|
||||
navigationCompose = "2.7.7"
|
||||
pinLockCompose = "1.0.3"
|
||||
roomVersion = "2.6.1"
|
||||
timber = "5.0.1"
|
||||
tunnel = "1.2.1"
|
||||
androidGradlePlugin = "8.6.1"
|
||||
tunnel = "1.2.3"
|
||||
androidGradlePlugin = "8.7.0-alpha08"
|
||||
kotlin = "2.0.20"
|
||||
ksp = "2.0.20-1.0.25"
|
||||
composeBom = "2024.09.02"
|
||||
compose = "1.7.2"
|
||||
ksp = "2.0.20-1.0.24"
|
||||
composeBom = "2024.08.00"
|
||||
compose = "1.6.8"
|
||||
zxingAndroidEmbedded = "4.3.0"
|
||||
coreSplashscreen = "1.0.1"
|
||||
gradlePlugins-grgit = "5.2.2"
|
||||
@@ -89,6 +90,7 @@ pin-lock-compose = { module = "com.zaneschepke:pin_lock_compose", version.ref =
|
||||
timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
|
||||
tunnel = { module = "com.zaneschepke:wireguard-android", version.ref = "tunnel" }
|
||||
|
||||
zaneschepke-multifab = { module = "com.zaneschepke:multifab", version.ref = "multifabVersion" }
|
||||
zxing-android-embedded = { module = "com.journeyapps:zxing-android-embedded", version.ref = "zxingAndroidEmbedded" }
|
||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
#Wed Oct 11 22:39:21 EDT 2023
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
|
||||
distributionSha256Sum=d725d707bfabd4dfdc958c624003b3c80accc03f7037b5122c4b1d0ef15cecab
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
|
||||
distributionSha256Sum=544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
9
|
||||
Reference in New Issue
Block a user