mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 21fc391a9e |
@@ -1,3 +1,2 @@
|
|||||||
ko_fi: zaneschepke
|
ko_fi: zaneschepke
|
||||||
liberapay: zaneschepke
|
liberapay: zaneschepke
|
||||||
github: zaneschepke
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ A clear and concise description of what the bug is.
|
|||||||
- Device: [e.g. Pixel 4a]
|
- Device: [e.g. Pixel 4a]
|
||||||
- Android Version: [e.g. Android 13]
|
- Android Version: [e.g. Android 13]
|
||||||
- App Version [e.g. 3.3.3]
|
- App Version [e.g. 3.3.3]
|
||||||
- Backend: [e.g. Kernel, Userspace]
|
|
||||||
|
|
||||||
**To Reproduce**
|
**To Reproduce**
|
||||||
Steps to reproduce the behavior:
|
Steps to reproduce the behavior:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
name: on-issue
|
name: Issue Updates Workflow
|
||||||
|
|
||||||
on:
|
on:
|
||||||
issues:
|
issues:
|
||||||
@@ -7,8 +7,8 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
on-issue:
|
build:
|
||||||
name: On new issue
|
name: Build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Send Telegram Message
|
- name: Send Telegram Message
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
name: on-publish
|
name: Release Updates Workflow
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
@@ -7,8 +7,8 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
on-publish:
|
build:
|
||||||
name: On publish
|
name: Build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Send Telegram Message
|
- name: Send Telegram Message
|
||||||
@@ -2,7 +2,7 @@ name: release-android
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "4 3 * * *"
|
- cron: "4 3 * * *"
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
track:
|
track:
|
||||||
@@ -14,7 +14,7 @@ on:
|
|||||||
- alpha
|
- alpha
|
||||||
- beta
|
- beta
|
||||||
- production
|
- production
|
||||||
default: none
|
default: alpha
|
||||||
required: true
|
required: true
|
||||||
release_type:
|
release_type:
|
||||||
type: choice
|
type: choice
|
||||||
@@ -30,34 +30,13 @@ on:
|
|||||||
description: "Tag name for release"
|
description: "Tag name for release"
|
||||||
required: false
|
required: false
|
||||||
default: nightly
|
default: nightly
|
||||||
workflow_call:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check_commits:
|
|
||||||
name: Check for New Commits
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
outputs:
|
|
||||||
has_new_commits: ${{ steps.check.outputs.new_commits }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout Repository
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
with:
|
|
||||||
fetch-depth: 0 # This fetches all history so we can check commits
|
|
||||||
|
|
||||||
- name: Check for new commits
|
|
||||||
id: check
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
run: |
|
|
||||||
# This script checks for commits newer than 23 hours ago
|
|
||||||
NEW_COMMITS=$(git rev-list --count --after="$(date -Iseconds -d '23 hours ago')" ${{ github.sha }})
|
|
||||||
echo "new_commits=$NEW_COMMITS" >> $GITHUB_OUTPUT
|
|
||||||
build:
|
build:
|
||||||
needs: check_commits
|
|
||||||
if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
|
|
||||||
name: Build Signed APK
|
name: Build Signed APK
|
||||||
|
if: ${{ inputs.release_type != 'none' }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
env:
|
env:
|
||||||
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||||
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||||
@@ -131,24 +110,9 @@ jobs:
|
|||||||
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
|
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
|
||||||
echo "VERSION_CODE=$version_code" >> $GITHUB_ENV
|
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
|
# Save the APK after the Build job is complete to publish it as a Github release in the next job
|
||||||
- name: Upload APK
|
- name: Upload APK
|
||||||
uses: actions/upload-artifact@v4.4.3
|
uses: actions/upload-artifact@v4.3.5
|
||||||
with:
|
with:
|
||||||
name: wgtunnel
|
name: wgtunnel
|
||||||
path: ${{ env.APK_PATH }}
|
path: ${{ env.APK_PATH }}
|
||||||
@@ -185,7 +149,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo "RELEASE_NOTES=Nightly build for the latest development version of the app." >> $GITHUB_ENV
|
echo "RELEASE_NOTES=Nightly build for the latest development version of the app." >> $GITHUB_ENV
|
||||||
gh release delete nightly --yes || true
|
gh release delete nightly --yes || true
|
||||||
git push origin :nightly || true
|
|
||||||
|
|
||||||
- name: On prerelease release notes
|
- name: On prerelease release notes
|
||||||
if: ${{ inputs.release_type == 'prerelease' }}
|
if: ${{ inputs.release_type == 'prerelease' }}
|
||||||
|
|||||||
@@ -71,4 +71,3 @@ app/release/output.json
|
|||||||
.idea/codeStyles/
|
.idea/codeStyles/
|
||||||
# where we keep our signing secrets locally
|
# where we keep our signing secrets locally
|
||||||
app/signing.properties
|
app/signing.properties
|
||||||
/.kotlin/
|
|
||||||
|
|||||||
@@ -55,13 +55,13 @@ and on while on different networks. This app was created to offer a free solutio
|
|||||||
* Split tunneling by application with search
|
* Split tunneling by application with search
|
||||||
* WireGuard support for kernel and userspace modes
|
* WireGuard support for kernel and userspace modes
|
||||||
* Amnezia support for userspace mode for DPI/censorship protection
|
* Amnezia support for userspace mode for DPI/censorship protection
|
||||||
* Pre/Post Up/Down scripts support for all modes on a rooted device
|
|
||||||
* Always-On VPN support
|
* Always-On VPN support
|
||||||
* Export Amnezia and WireGuard tunnels to zip
|
* Export Amnezia and WireGuard tunnels to zip
|
||||||
* Quick tile support for tunnel toggling, auto-tunneling
|
* Quick tile support for tunnel toggling, auto-tunneling
|
||||||
* Static shortcuts support for tunnel toggling, auto-tunneling
|
* Static shortcuts support for tunnel toggling, auto-tunneling
|
||||||
* Intent automation support for all tunnels
|
* Intent automation support for all tunnels
|
||||||
* Automatic auto-tunneling service and/or tunnel restart after reboot or app update
|
* Automatic auto-tunneling service restart after reboot
|
||||||
|
* Automatic tunnel restart after reboot
|
||||||
* Battery preservation measures
|
* Battery preservation measures
|
||||||
* Restart tunnel on ping failure (beta)
|
* Restart tunnel on ping failure (beta)
|
||||||
|
|
||||||
|
|||||||
+16
-52
@@ -8,21 +8,6 @@ plugins {
|
|||||||
alias(libs.plugins.grgit)
|
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 {
|
android {
|
||||||
namespace = Constants.APP_ID
|
namespace = Constants.APP_ID
|
||||||
compileSdk = Constants.TARGET_SDK
|
compileSdk = Constants.TARGET_SDK
|
||||||
@@ -35,7 +20,7 @@ android {
|
|||||||
applicationId = Constants.APP_ID
|
applicationId = Constants.APP_ID
|
||||||
minSdk = Constants.MIN_SDK
|
minSdk = Constants.MIN_SDK
|
||||||
targetSdk = Constants.TARGET_SDK
|
targetSdk = Constants.TARGET_SDK
|
||||||
versionCode = Constants.VERSION_CODE + versionCodeIncrement
|
versionCode = determineVersionCode()
|
||||||
versionName = determineVersionName()
|
versionName = determineVersionName()
|
||||||
|
|
||||||
ksp { arg("room.schemaLocation", "$projectDir/schemas") }
|
ksp { arg("room.schemaLocation", "$projectDir/schemas") }
|
||||||
@@ -44,8 +29,6 @@ android {
|
|||||||
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
|
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
|
||||||
}
|
}
|
||||||
|
|
||||||
buildConfigField("String[]", "LANGUAGES", "new String[]{ ${languageList().joinToString(separator = ", ") { "\"$it\"" }} }")
|
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables { useSupportLibrary = true }
|
vectorDrawables { useSupportLibrary = true }
|
||||||
}
|
}
|
||||||
@@ -74,30 +57,15 @@ android {
|
|||||||
"proguard-rules.pro",
|
"proguard-rules.pro",
|
||||||
)
|
)
|
||||||
signingConfig = signingConfigs.getByName(Constants.RELEASE)
|
signingConfig = signingConfigs.getByName(Constants.RELEASE)
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider\"")
|
|
||||||
}
|
|
||||||
debug {
|
|
||||||
applicationIdSuffix = ".debug"
|
|
||||||
versionNameSuffix = "-debug"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Debug")
|
|
||||||
isDebuggable = true
|
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"")
|
|
||||||
}
|
}
|
||||||
|
debug { isDebuggable = true }
|
||||||
|
|
||||||
create(Constants.PRERELEASE) {
|
create(Constants.PRERELEASE) {
|
||||||
initWith(buildTypes.getByName(Constants.RELEASE))
|
initWith(buildTypes.getByName(Constants.RELEASE))
|
||||||
applicationIdSuffix = ".prerelease"
|
|
||||||
versionNameSuffix = "-pre"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Pre")
|
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create(Constants.NIGHTLY) {
|
create(Constants.NIGHTLY) {
|
||||||
initWith(buildTypes.getByName(Constants.RELEASE))
|
initWith(buildTypes.getByName(Constants.RELEASE))
|
||||||
applicationIdSuffix = ".nightly"
|
|
||||||
versionNameSuffix = "-nightly"
|
|
||||||
resValue("string", "app_name", "WG Tunnel - Nightly")
|
|
||||||
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
applicationVariants.all {
|
applicationVariants.all {
|
||||||
@@ -135,6 +103,8 @@ android {
|
|||||||
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
|
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val generalImplementation by configurations
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
implementation(project(":logcatter"))
|
implementation(project(":logcatter"))
|
||||||
@@ -151,7 +121,6 @@ dependencies {
|
|||||||
implementation(libs.androidx.compose.ui.tooling.preview)
|
implementation(libs.androidx.compose.ui.tooling.preview)
|
||||||
implementation(libs.androidx.material3)
|
implementation(libs.androidx.material3)
|
||||||
implementation(libs.androidx.appcompat)
|
implementation(libs.androidx.appcompat)
|
||||||
implementation(libs.material)
|
|
||||||
|
|
||||||
// test
|
// test
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
@@ -165,7 +134,6 @@ dependencies {
|
|||||||
debugImplementation(libs.androidx.compose.manifest)
|
debugImplementation(libs.androidx.compose.manifest)
|
||||||
|
|
||||||
// get tunnel lib from github packages or mavenLocal
|
// get tunnel lib from github packages or mavenLocal
|
||||||
// implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))
|
|
||||||
implementation(libs.tunnel)
|
implementation(libs.tunnel)
|
||||||
implementation(libs.amneziawg.android)
|
implementation(libs.amneziawg.android)
|
||||||
coreLibraryDesugaring(libs.desugar.jdk.libs)
|
coreLibraryDesugaring(libs.desugar.jdk.libs)
|
||||||
@@ -177,6 +145,8 @@ dependencies {
|
|||||||
implementation(libs.androidx.navigation.compose)
|
implementation(libs.androidx.navigation.compose)
|
||||||
implementation(libs.androidx.hilt.navigation.compose)
|
implementation(libs.androidx.hilt.navigation.compose)
|
||||||
|
|
||||||
|
implementation(libs.zaneschepke.multifab)
|
||||||
|
|
||||||
// hilt
|
// hilt
|
||||||
implementation(libs.hilt.android)
|
implementation(libs.hilt.android)
|
||||||
ksp(libs.hilt.android.compiler)
|
ksp(libs.hilt.android.compiler)
|
||||||
@@ -217,6 +187,16 @@ dependencies {
|
|||||||
implementation(libs.androidx.core.splashscreen)
|
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 {
|
fun determineVersionName(): String {
|
||||||
return with(getBuildTaskName().lowercase()) {
|
return with(getBuildTaskName().lowercase()) {
|
||||||
when {
|
when {
|
||||||
@@ -227,19 +207,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')"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,232 +0,0 @@
|
|||||||
{
|
|
||||||
"formatVersion": 1,
|
|
||||||
"database": {
|
|
||||||
"version": 11,
|
|
||||||
"identityHash": "4c9418386f72dfac5d28ab96c1e5ea0b",
|
|
||||||
"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_ping_enabled` INTEGER NOT NULL DEFAULT false, `is_amnezia_enabled` INTEGER NOT NULL DEFAULT false, `is_wildcards_enabled` INTEGER NOT NULL DEFAULT false, `is_wifi_by_shell_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": "isPingEnabled",
|
|
||||||
"columnName": "is_ping_enabled",
|
|
||||||
"affinity": "INTEGER",
|
|
||||||
"notNull": true,
|
|
||||||
"defaultValue": "false"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldPath": "isAmneziaEnabled",
|
|
||||||
"columnName": "is_amnezia_enabled",
|
|
||||||
"affinity": "INTEGER",
|
|
||||||
"notNull": true,
|
|
||||||
"defaultValue": "false"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldPath": "isWildcardsEnabled",
|
|
||||||
"columnName": "is_wildcards_enabled",
|
|
||||||
"affinity": "INTEGER",
|
|
||||||
"notNull": true,
|
|
||||||
"defaultValue": "false"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldPath": "isWifiNameByShellEnabled",
|
|
||||||
"columnName": "is_wifi_by_shell_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, '4c9418386f72dfac5d28ab96c1e5ea0b')"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,197 +0,0 @@
|
|||||||
{
|
|
||||||
"formatVersion": 1,
|
|
||||||
"database": {
|
|
||||||
"version": 9,
|
|
||||||
"identityHash": "e2c91dbf1885a9da592d3f54f1e08302",
|
|
||||||
"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)",
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"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, 'e2c91dbf1885a9da592d3f54f1e08302')"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,17 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
<uses-permission
|
||||||
|
android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="32" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="32"
|
||||||
|
tools:ignore="ScopedStorage" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.ACCESS_WIFI_STATE"
|
||||||
|
android:maxSdkVersion="30"
|
||||||
|
tools:ignore="LeanbackUsesWifi" />
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
@@ -12,8 +22,7 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||||
<!--foreground service exempt android 14-->
|
<!--foreground service exempt android 14-->
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
|
||||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
|
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||||
tools:ignore="ProtectedPermissions" />
|
|
||||||
|
|
||||||
<!--foreground service permissions-->
|
<!--foreground service permissions-->
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
@@ -22,12 +31,6 @@
|
|||||||
<!--start service on boot permission-->
|
<!--start service on boot permission-->
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
<!--android tv support-->
|
<!--android tv support-->
|
||||||
|
|
||||||
<permission
|
|
||||||
android:name="${applicationId}.permission.CONTROL_TUNNELS"
|
|
||||||
android:icon="@mipmap/ic_launcher"
|
|
||||||
android:protectionLevel="dangerous" />
|
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.software.leanback"
|
android:name="android.software.leanback"
|
||||||
android:required="false" />
|
android:required="false" />
|
||||||
@@ -40,12 +43,6 @@
|
|||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.hardware.screen.portrait"
|
android:name="android.hardware.screen.portrait"
|
||||||
android:required="false" />
|
android:required="false" />
|
||||||
<uses-feature
|
|
||||||
android:name="android.hardware.gamepad"
|
|
||||||
android:required="false"/>
|
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.wifi"
|
|
||||||
android:required="false"/>
|
|
||||||
|
|
||||||
<queries>
|
<queries>
|
||||||
<intent>
|
<intent>
|
||||||
@@ -63,12 +60,12 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.App.Start"
|
android:theme="@style/Theme.AppSplashScreen"
|
||||||
tools:targetApi="tiramisu">
|
tools:targetApi="tiramisu">
|
||||||
<activity
|
<activity
|
||||||
android:name=".ui.MainActivity"
|
android:name=".ui.SplashActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:theme="@style/Theme.WireguardAutoTunnel">
|
android:theme="@style/Theme.AppSplashScreen">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
@@ -80,6 +77,11 @@
|
|||||||
android:name="android.app.shortcuts"
|
android:name="android.app.shortcuts"
|
||||||
android:resource="@xml/shortcuts" />
|
android:resource="@xml/shortcuts" />
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name=".ui.MainActivity"
|
||||||
|
android:exported="true"
|
||||||
|
android:theme="@style/Theme.WireguardAutoTunnel">
|
||||||
|
</activity>
|
||||||
<activity
|
<activity
|
||||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
||||||
android:screenOrientation="portrait"
|
android:screenOrientation="portrait"
|
||||||
@@ -95,16 +97,12 @@
|
|||||||
android:launchMode="singleInstance"
|
android:launchMode="singleInstance"
|
||||||
android:theme="@android:style/Theme.NoDisplay" />
|
android:theme="@android:style/Theme.NoDisplay" />
|
||||||
|
|
||||||
<provider
|
<service
|
||||||
android:name="androidx.core.content.FileProvider"
|
android:name=".service.foreground.ForegroundService"
|
||||||
android:authorities="@string/provider"
|
android:enabled="true"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:grantUriPermissions="true">
|
android:foregroundServiceType="systemExempted"
|
||||||
<meta-data
|
tools:node="merge" />
|
||||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
|
||||||
android:resource="@xml/file_paths" />
|
|
||||||
</provider>
|
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".service.tile.TunnelControlTile"
|
android:name=".service.tile.TunnelControlTile"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
@@ -139,23 +137,23 @@
|
|||||||
<action android:name="android.service.quicksettings.action.QS_TILE" />
|
<action android:name="android.service.quicksettings.action.QS_TILE" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</service>
|
</service>
|
||||||
<service
|
|
||||||
android:name=".service.tunnel.AlwaysOnVpnService"
|
|
||||||
android:enabled="true"
|
|
||||||
android:exported="false"
|
|
||||||
android:foregroundServiceType="systemExempted"
|
|
||||||
android:permission="android.permission.BIND_VPN_SERVICE"
|
|
||||||
android:persistent="true"
|
|
||||||
tools:node="merge">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.net.VpnService" />
|
|
||||||
</intent-filter>
|
|
||||||
<meta-data
|
|
||||||
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
|
|
||||||
android:value="true" />
|
|
||||||
</service>
|
|
||||||
<service
|
<service
|
||||||
android:name=".service.foreground.AutoTunnelService"
|
android:name=".service.foreground.WireGuardTunnelService"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="false"
|
||||||
|
android:foregroundServiceType="systemExempted"
|
||||||
|
android:permission="android.permission.BIND_VPN_SERVICE"
|
||||||
|
android:persistent="true"
|
||||||
|
tools:node="merge">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.net.VpnService" />
|
||||||
|
</intent-filter>
|
||||||
|
<meta-data
|
||||||
|
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
|
||||||
|
android:value="true" />
|
||||||
|
</service>
|
||||||
|
<service
|
||||||
|
android:name=".service.foreground.WireGuardConnectivityWatcherService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:foregroundServiceType="systemExempted"
|
android:foregroundServiceType="systemExempted"
|
||||||
@@ -163,17 +161,6 @@
|
|||||||
android:stopWithTask="false"
|
android:stopWithTask="false"
|
||||||
tools:node="merge" />
|
tools:node="merge" />
|
||||||
|
|
||||||
<service
|
|
||||||
android:name=".service.foreground.TunnelBackgroundService"
|
|
||||||
android:exported="false"
|
|
||||||
android:persistent="true"
|
|
||||||
android:foregroundServiceType="systemExempted"
|
|
||||||
android:permission="android.permission.BIND_VPN_SERVICE">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.net.VpnService" />
|
|
||||||
</intent-filter>
|
|
||||||
</service>
|
|
||||||
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".receiver.BootReceiver"
|
android:name=".receiver.BootReceiver"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
@@ -187,20 +174,8 @@
|
|||||||
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
|
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".receiver.AppUpdateReceiver"
|
android:name=".receiver.NotificationActionReceiver"
|
||||||
android:exported="false">
|
android:exported="false" />
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
|
||||||
</intent-filter>
|
|
||||||
</receiver>
|
|
||||||
<receiver
|
|
||||||
android:name=".receiver.KernelReceiver"
|
|
||||||
android:exported="false"
|
|
||||||
android:permission="${applicationId}.permission.CONTROL_TUNNELS">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" />
|
|
||||||
</intent-filter>
|
|
||||||
</receiver>
|
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -1,41 +1,19 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.ComponentName
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.StrictMode
|
import android.os.StrictMode
|
||||||
import android.os.StrictMode.ThreadPolicy
|
import android.os.StrictMode.ThreadPolicy
|
||||||
import com.zaneschepke.logcatter.LogReader
|
import android.service.quicksettings.TileService
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
|
import com.zaneschepke.wireguardautotunnel.service.tile.AutoTunnelControlTile
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.service.tile.TunnelControlTile
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
import dagger.hilt.android.HiltAndroidApp
|
import dagger.hilt.android.HiltAndroidApp
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@HiltAndroidApp
|
@HiltAndroidApp
|
||||||
class WireGuardAutoTunnel : Application() {
|
class WireGuardAutoTunnel : Application() {
|
||||||
|
|
||||||
val localeStorage: LocaleStorage by lazy {
|
|
||||||
LocaleStorage(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var logReader: LogReader
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@IoDispatcher
|
|
||||||
lateinit var ioDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
instance = this
|
instance = this
|
||||||
@@ -52,19 +30,28 @@ class WireGuardAutoTunnel : Application() {
|
|||||||
} else {
|
} else {
|
||||||
Timber.plant(ReleaseTree())
|
Timber.plant(ReleaseTree())
|
||||||
}
|
}
|
||||||
if (!isRunningOnTv()) {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
|
||||||
logReader.start()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun attachBaseContext(base: Context) {
|
|
||||||
super.attachBaseContext(LocaleUtil.getLocalizedContext(base, LocaleStorage(base).getPreferredLocale()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
lateinit var instance: WireGuardAutoTunnel
|
lateinit var instance: WireGuardAutoTunnel
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
fun isRunningOnAndroidTv(): Boolean {
|
||||||
|
return instance.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun requestTunnelTileServiceStateUpdate() {
|
||||||
|
TileService.requestListeningState(
|
||||||
|
instance,
|
||||||
|
ComponentName(instance, TunnelControlTile::class.java),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun requestAutoTunnelTileServiceUpdate() {
|
||||||
|
TileService.requestListeningState(
|
||||||
|
instance,
|
||||||
|
ComponentName(instance, AutoTunnelControlTile::class.java),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
entities = [Settings::class, TunnelConfig::class],
|
entities = [Settings::class, TunnelConfig::class],
|
||||||
version = 11,
|
version = 8,
|
||||||
autoMigrations =
|
autoMigrations =
|
||||||
[
|
[
|
||||||
AutoMigration(from = 1, to = 2),
|
AutoMigration(from = 1, to = 2),
|
||||||
@@ -34,13 +34,6 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
spec = RemoveLegacySettingColumnsMigration::class,
|
spec = RemoveLegacySettingColumnsMigration::class,
|
||||||
),
|
),
|
||||||
AutoMigration(7, 8),
|
AutoMigration(7, 8),
|
||||||
AutoMigration(8, 9),
|
|
||||||
AutoMigration(9, 10),
|
|
||||||
AutoMigration(
|
|
||||||
from = 10,
|
|
||||||
to = 11,
|
|
||||||
spec = RemoveTunnelPauseMigration::class,
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
exportSchema = true,
|
exportSchema = true,
|
||||||
)
|
)
|
||||||
@@ -60,9 +53,3 @@ abstract class AppDatabase : RoomDatabase() {
|
|||||||
columnName = "is_battery_saver_enabled",
|
columnName = "is_battery_saver_enabled",
|
||||||
)
|
)
|
||||||
class RemoveLegacySettingColumnsMigration : AutoMigrationSpec
|
class RemoveLegacySettingColumnsMigration : AutoMigrationSpec
|
||||||
|
|
||||||
@DeleteColumn(
|
|
||||||
tableName = "Settings",
|
|
||||||
columnName = "is_auto_tunnel_paused",
|
|
||||||
)
|
|
||||||
class RemoveTunnelPauseMigration : AutoMigrationSpec
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import androidx.room.Insert
|
|||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
@@ -23,9 +23,6 @@ interface TunnelConfigDao {
|
|||||||
@Query("SELECT * FROM TunnelConfig WHERE name=:name")
|
@Query("SELECT * FROM TunnelConfig WHERE name=:name")
|
||||||
suspend fun getByName(name: String): TunnelConfig?
|
suspend fun getByName(name: String): TunnelConfig?
|
||||||
|
|
||||||
@Query("SELECT * FROM TunnelConfig WHERE is_Active=1")
|
|
||||||
suspend fun getActive(): TunnelConfigs
|
|
||||||
|
|
||||||
@Query("SELECT * FROM TunnelConfig")
|
@Query("SELECT * FROM TunnelConfig")
|
||||||
suspend fun getAll(): TunnelConfigs
|
suspend fun getAll(): TunnelConfigs
|
||||||
|
|
||||||
|
|||||||
+8
-18
@@ -4,6 +4,7 @@ import android.content.Context
|
|||||||
import androidx.datastore.preferences.core.Preferences
|
import androidx.datastore.preferences.core.Preferences
|
||||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||||
import androidx.datastore.preferences.core.edit
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.core.intPreferencesKey
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
@@ -21,12 +22,13 @@ class DataStoreManager(
|
|||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
val locationDisclosureShown = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
||||||
val batteryDisableShown = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
|
val BATTERY_OPTIMIZE_DISABLE_SHOWN = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
|
||||||
val currentSSID = stringPreferencesKey("CURRENT_SSID")
|
val TUNNEL_RUNNING_FROM_MANUAL_START =
|
||||||
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
booleanPreferencesKey("TUNNEL_RUNNING_FROM_MANUAL_START")
|
||||||
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
|
val ACTIVE_TUNNEL = intPreferencesKey("ACTIVE_TUNNEL")
|
||||||
val theme = stringPreferencesKey("THEME")
|
val CURRENT_SSID = stringPreferencesKey("CURRENT_SSID")
|
||||||
|
val IS_PIN_LOCK_ENABLED = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
||||||
}
|
}
|
||||||
|
|
||||||
// preferences
|
// preferences
|
||||||
@@ -58,18 +60,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] }
|
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] }
|
||||||
|
|
||||||
suspend fun <T> getFromStore(key: Preferences.Key<T>): T? {
|
suspend fun <T> getFromStore(key: Preferences.Key<T>): T? {
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.datastore
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
|
||||||
|
|
||||||
class LocaleStorage(context: Context) {
|
|
||||||
private var preferences: SharedPreferences = context.getSharedPreferences("sp", Context.MODE_PRIVATE)
|
|
||||||
|
|
||||||
fun getPreferredLocale(): String {
|
|
||||||
return preferences.getString("preferred_locale", LocaleUtil.OPTION_PHONE_LANGUAGE)!!
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreferredLocale(localeCode: String) {
|
|
||||||
preferences.edit().putString("preferred_locale", localeCode).apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,16 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.domain
|
package com.zaneschepke.wireguardautotunnel.data.domain
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
|
||||||
|
|
||||||
data class GeneralState(
|
data class GeneralState(
|
||||||
val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
||||||
val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
||||||
|
val isTunnelRunningFromManualStart: Boolean = TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT,
|
||||||
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
|
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
|
||||||
val isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED,
|
val activeTunnelId: Int? = null,
|
||||||
val theme: Theme = Theme.AUTOMATIC,
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
|
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
|
||||||
const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false
|
const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false
|
||||||
|
const val TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT = false
|
||||||
const val PIN_LOCK_ENABLED_DEFAULT = false
|
const val PIN_LOCK_ENABLED_DEFAULT = false
|
||||||
const val IS_TUNNEL_STATS_EXPANDED = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ data class Settings(
|
|||||||
defaultValue = "false",
|
defaultValue = "false",
|
||||||
)
|
)
|
||||||
val isMultiTunnelEnabled: Boolean = false,
|
val isMultiTunnelEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_auto_tunnel_paused",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
val isAutoTunnelPaused: Boolean = false,
|
||||||
@ColumnInfo(
|
@ColumnInfo(
|
||||||
name = "is_ping_enabled",
|
name = "is_ping_enabled",
|
||||||
defaultValue = "false",
|
defaultValue = "false",
|
||||||
@@ -50,14 +55,4 @@ data class Settings(
|
|||||||
defaultValue = "false",
|
defaultValue = "false",
|
||||||
)
|
)
|
||||||
val isAmneziaEnabled: Boolean = false,
|
val isAmneziaEnabled: Boolean = false,
|
||||||
@ColumnInfo(
|
|
||||||
name = "is_wildcards_enabled",
|
|
||||||
defaultValue = "false",
|
|
||||||
)
|
|
||||||
val isWildcardsEnabled: Boolean = false,
|
|
||||||
@ColumnInfo(
|
|
||||||
name = "is_wifi_by_shell_enabled",
|
|
||||||
defaultValue = "false",
|
|
||||||
)
|
|
||||||
val isWifiNameByShellEnabled: Boolean = false,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -32,39 +32,8 @@ data class TunnelConfig(
|
|||||||
defaultValue = "",
|
defaultValue = "",
|
||||||
)
|
)
|
||||||
val amQuick: String = AM_QUICK_DEFAULT,
|
val amQuick: String = AM_QUICK_DEFAULT,
|
||||||
@ColumnInfo(
|
|
||||||
name = "is_Active",
|
|
||||||
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 {
|
companion object {
|
||||||
|
|
||||||
fun configFromWgQuick(wgQuick: String): Config {
|
fun configFromWgQuick(wgQuick: String): Config {
|
||||||
val inputStream: InputStream = wgQuick.byteInputStream()
|
val inputStream: InputStream = wgQuick.byteInputStream()
|
||||||
return inputStream.bufferedReader(Charsets.UTF_8).use {
|
return inputStream.bufferedReader(Charsets.UTF_8).use {
|
||||||
|
|||||||
+2
@@ -7,6 +7,8 @@ interface AppDataRepository {
|
|||||||
|
|
||||||
suspend fun getStartTunnelConfig(): TunnelConfig?
|
suspend fun getStartTunnelConfig(): TunnelConfig?
|
||||||
|
|
||||||
|
suspend fun toggleWatcherServicePause()
|
||||||
|
|
||||||
val settings: SettingsRepository
|
val settings: SettingsRepository
|
||||||
val tunnels: TunnelConfigRepository
|
val tunnels: TunnelConfigRepository
|
||||||
val appState: AppStateRepository
|
val appState: AppStateRepository
|
||||||
|
|||||||
+18
-3
@@ -15,9 +15,24 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getStartTunnelConfig(): TunnelConfig? {
|
override suspend fun getStartTunnelConfig(): TunnelConfig? {
|
||||||
tunnels.getActive().let {
|
return if (appState.isTunnelRunningFromManualStart()) {
|
||||||
if (it.isNotEmpty()) return it.first()
|
appState.getActiveTunnelId()?.let {
|
||||||
return getPrimaryOrFirstTunnel()
|
tunnels.getById(it)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun toggleWatcherServicePause() {
|
||||||
|
val settings = settings.getSettings()
|
||||||
|
if (settings.isAutoTunnelEnabled) {
|
||||||
|
val pauseAutoTunnel = !settings.isAutoTunnelPaused
|
||||||
|
this.settings.save(
|
||||||
|
settings.copy(
|
||||||
|
isAutoTunnelPaused = pauseAutoTunnel,
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-9
@@ -1,7 +1,6 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface AppStateRepository {
|
interface AppStateRepository {
|
||||||
@@ -17,17 +16,17 @@ interface AppStateRepository {
|
|||||||
|
|
||||||
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
||||||
|
|
||||||
|
suspend fun isTunnelRunningFromManualStart(): Boolean
|
||||||
|
|
||||||
|
suspend fun setTunnelRunningFromManualStart(id: Int)
|
||||||
|
|
||||||
|
suspend fun setManualStop()
|
||||||
|
|
||||||
|
suspend fun getActiveTunnelId(): Int?
|
||||||
|
|
||||||
suspend fun getCurrentSsid(): String?
|
suspend fun getCurrentSsid(): String?
|
||||||
|
|
||||||
suspend fun setCurrentSsid(ssid: String)
|
suspend fun setCurrentSsid(ssid: String)
|
||||||
|
|
||||||
suspend fun isTunnelStatsExpanded(): Boolean
|
|
||||||
|
|
||||||
suspend fun setTunnelStatsExpanded(expanded: Boolean)
|
|
||||||
|
|
||||||
suspend fun setTheme(theme: Theme)
|
|
||||||
|
|
||||||
suspend fun getTheme(): Theme
|
|
||||||
|
|
||||||
val generalStateFlow: Flow<GeneralState>
|
val generalStateFlow: Flow<GeneralState>
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-41
@@ -2,71 +2,71 @@ package com.zaneschepke.wireguardautotunnel.data.repository
|
|||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
class DataStoreAppStateRepository(
|
class DataStoreAppStateRepository(private val dataStoreManager: DataStoreManager) :
|
||||||
private val dataStoreManager: DataStoreManager,
|
|
||||||
) :
|
|
||||||
AppStateRepository {
|
AppStateRepository {
|
||||||
override suspend fun isLocationDisclosureShown(): Boolean {
|
override suspend fun isLocationDisclosureShown(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown)
|
return dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN)
|
||||||
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
|
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setLocationDisclosureShown(shown: Boolean) {
|
override suspend fun setLocationDisclosureShown(shown: Boolean) {
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.locationDisclosureShown, shown)
|
dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, shown)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun isPinLockEnabled(): Boolean {
|
override suspend fun isPinLockEnabled(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.pinLockEnabled)
|
return dataStoreManager.getFromStore(DataStoreManager.IS_PIN_LOCK_ENABLED)
|
||||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
|
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setPinLockEnabled(enabled: Boolean) {
|
override suspend fun setPinLockEnabled(enabled: Boolean) {
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.pinLockEnabled, enabled)
|
dataStoreManager.saveToDataStore(DataStoreManager.IS_PIN_LOCK_ENABLED, enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun isBatteryOptimizationDisableShown(): Boolean {
|
override suspend fun isBatteryOptimizationDisableShown(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.batteryDisableShown)
|
return dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN)
|
||||||
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
|
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) {
|
override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) {
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown)
|
dataStoreManager.saveToDataStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun isTunnelRunningFromManualStart(): Boolean {
|
||||||
|
return dataStoreManager.getFromStore(DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START)
|
||||||
|
?: GeneralState.TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun setTunnelRunningFromManualStart(id: Int) {
|
||||||
|
setTunnelRunningFromManualStart(true)
|
||||||
|
setActiveTunnelId(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun setManualStop() {
|
||||||
|
setTunnelRunningFromManualStart(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun setTunnelRunningFromManualStart(running: Boolean) {
|
||||||
|
dataStoreManager.saveToDataStore(DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START, running)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getActiveTunnelId(): Int? {
|
||||||
|
return dataStoreManager.getFromStore(DataStoreManager.ACTIVE_TUNNEL)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun setActiveTunnelId(id: Int) {
|
||||||
|
dataStoreManager.saveToDataStore(DataStoreManager.ACTIVE_TUNNEL, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentSsid(): String? {
|
override suspend fun getCurrentSsid(): String? {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.currentSSID)
|
return dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setCurrentSsid(ssid: String) {
|
override suspend fun setCurrentSsid(ssid: String) {
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.currentSSID, ssid)
|
dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid)
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun isTunnelStatsExpanded(): Boolean {
|
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded)
|
|
||||||
?: GeneralState.IS_TUNNEL_STATS_EXPANDED
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setTunnelStatsExpanded(expanded: Boolean) {
|
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.tunnelStatsExpanded, expanded)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setTheme(theme: Theme) {
|
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.theme, theme.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getTheme(): Theme {
|
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.theme)?.let {
|
|
||||||
try {
|
|
||||||
Theme.valueOf(it)
|
|
||||||
} catch (_: IllegalArgumentException) {
|
|
||||||
Theme.AUTOMATIC
|
|
||||||
}
|
|
||||||
} ?: Theme.AUTOMATIC
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override val generalStateFlow: Flow<GeneralState> =
|
override val generalStateFlow: Flow<GeneralState> =
|
||||||
@@ -75,16 +75,17 @@ class DataStoreAppStateRepository(
|
|||||||
try {
|
try {
|
||||||
GeneralState(
|
GeneralState(
|
||||||
isLocationDisclosureShown =
|
isLocationDisclosureShown =
|
||||||
pref[DataStoreManager.locationDisclosureShown]
|
pref[DataStoreManager.LOCATION_DISCLOSURE_SHOWN]
|
||||||
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
||||||
isBatteryOptimizationDisableShown =
|
isBatteryOptimizationDisableShown =
|
||||||
pref[DataStoreManager.batteryDisableShown]
|
pref[DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN]
|
||||||
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
||||||
|
isTunnelRunningFromManualStart =
|
||||||
|
pref[DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START]
|
||||||
|
?: GeneralState.TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT,
|
||||||
isPinLockEnabled =
|
isPinLockEnabled =
|
||||||
pref[DataStoreManager.pinLockEnabled]
|
pref[DataStoreManager.IS_PIN_LOCK_ENABLED]
|
||||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
?: GeneralState.TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT,
|
||||||
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
|
|
||||||
theme = getTheme(),
|
|
||||||
)
|
)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
|
|||||||
+4
-11
@@ -2,16 +2,11 @@ package com.zaneschepke.wireguardautotunnel.data.repository
|
|||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.SettingsDao
|
import com.zaneschepke.wireguardautotunnel.data.SettingsDao
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
class RoomSettingsRepository(private val settingsDoa: SettingsDao, @IoDispatcher private val ioDispatcher: CoroutineDispatcher) : SettingsRepository {
|
class RoomSettingsRepository(private val settingsDoa: SettingsDao) : SettingsRepository {
|
||||||
override suspend fun save(settings: Settings) {
|
override suspend fun save(settings: Settings) {
|
||||||
withContext(ioDispatcher) {
|
settingsDoa.save(settings)
|
||||||
settingsDoa.save(settings)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSettingsFlow(): Flow<Settings> {
|
override fun getSettingsFlow(): Flow<Settings> {
|
||||||
@@ -19,12 +14,10 @@ class RoomSettingsRepository(private val settingsDoa: SettingsDao, @IoDispatcher
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getSettings(): Settings {
|
override suspend fun getSettings(): Settings {
|
||||||
return withContext(ioDispatcher) {
|
return settingsDoa.getAll().firstOrNull() ?: Settings()
|
||||||
settingsDoa.getAll().firstOrNull() ?: Settings()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getAll(): List<Settings> {
|
override suspend fun getAll(): List<Settings> {
|
||||||
return withContext(ioDispatcher) { settingsDoa.getAll() }
|
return settingsDoa.getAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-45
@@ -2,90 +2,70 @@ package com.zaneschepke.wireguardautotunnel.data.repository
|
|||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
class RoomTunnelConfigRepository(
|
class RoomTunnelConfigRepository(private val tunnelConfigDao: TunnelConfigDao) :
|
||||||
private val tunnelConfigDao: TunnelConfigDao,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
) :
|
|
||||||
TunnelConfigRepository {
|
TunnelConfigRepository {
|
||||||
override fun getTunnelConfigsFlow(): Flow<TunnelConfigs> {
|
override fun getTunnelConfigsFlow(): Flow<TunnelConfigs> {
|
||||||
return tunnelConfigDao.getAllFlow()
|
return tunnelConfigDao.getAllFlow()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getAll(): TunnelConfigs {
|
override suspend fun getAll(): TunnelConfigs {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.getAll() }
|
return tunnelConfigDao.getAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun save(tunnelConfig: TunnelConfig) {
|
override suspend fun save(tunnelConfig: TunnelConfig) {
|
||||||
withContext(ioDispatcher) {
|
tunnelConfigDao.save(tunnelConfig)
|
||||||
tunnelConfigDao.save(tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?) {
|
override suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?) {
|
||||||
withContext(ioDispatcher) {
|
tunnelConfigDao.resetPrimaryTunnel()
|
||||||
tunnelConfigDao.resetPrimaryTunnel()
|
tunnelConfig?.let {
|
||||||
tunnelConfig?.let {
|
save(
|
||||||
save(
|
it.copy(
|
||||||
it.copy(
|
isPrimaryTunnel = true,
|
||||||
isPrimaryTunnel = true,
|
),
|
||||||
),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?) {
|
override suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?) {
|
||||||
withContext(ioDispatcher) {
|
tunnelConfigDao.resetMobileDataTunnel()
|
||||||
tunnelConfigDao.resetMobileDataTunnel()
|
tunnelConfig?.let {
|
||||||
tunnelConfig?.let {
|
save(
|
||||||
save(
|
it.copy(
|
||||||
it.copy(
|
isMobileDataTunnel = true,
|
||||||
isMobileDataTunnel = true,
|
),
|
||||||
),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun delete(tunnelConfig: TunnelConfig) {
|
override suspend fun delete(tunnelConfig: TunnelConfig) {
|
||||||
withContext(ioDispatcher) {
|
tunnelConfigDao.delete(tunnelConfig)
|
||||||
tunnelConfigDao.delete(tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getById(id: Int): TunnelConfig? {
|
override suspend fun getById(id: Int): TunnelConfig? {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.getById(id.toLong()) }
|
return tunnelConfigDao.getById(id.toLong())
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun getActive(): TunnelConfigs {
|
|
||||||
return withContext(ioDispatcher) {
|
|
||||||
tunnelConfigDao.getActive()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun count(): Int {
|
override suspend fun count(): Int {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.count().toInt() }
|
return tunnelConfigDao.count().toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByTunnelName(name: String): TunnelConfig? {
|
override suspend fun findByTunnelName(name: String): TunnelConfig? {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.getByName(name) }
|
return tunnelConfigDao.getByName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByTunnelNetworksName(name: String): TunnelConfigs {
|
override suspend fun findByTunnelNetworksName(name: String): TunnelConfigs {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.findByTunnelNetworkName(name) }
|
return tunnelConfigDao.findByTunnelNetworkName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByMobileDataTunnel(): TunnelConfigs {
|
override suspend fun findByMobileDataTunnel(): TunnelConfigs {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.findByMobileDataTunnel() }
|
return tunnelConfigDao.findByMobileDataTunnel()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findPrimary(): TunnelConfigs {
|
override suspend fun findPrimary(): TunnelConfigs {
|
||||||
return withContext(ioDispatcher) { tunnelConfigDao.findByPrimary() }
|
return tunnelConfigDao.findByPrimary()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,7 +1,7 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface TunnelConfigRepository {
|
interface TunnelConfigRepository {
|
||||||
@@ -19,8 +19,6 @@ interface TunnelConfigRepository {
|
|||||||
|
|
||||||
suspend fun getById(id: Int): TunnelConfig?
|
suspend fun getById(id: Int): TunnelConfig?
|
||||||
|
|
||||||
suspend fun getActive(): TunnelConfigs
|
|
||||||
|
|
||||||
suspend fun count(): Int
|
suspend fun count(): Int
|
||||||
|
|
||||||
suspend fun findByTunnelName(name: String): TunnelConfig?
|
suspend fun findByTunnelName(name: String): TunnelConfig?
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.module
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.zaneschepke.logcatter.LogReader
|
import com.zaneschepke.logcatter.LocalLogCollector
|
||||||
import com.zaneschepke.logcatter.LogcatCollector
|
import com.zaneschepke.logcatter.LogcatUtil
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -24,7 +24,7 @@ class AppModule {
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideLogCollect(@ApplicationContext context: Context): LogReader {
|
fun provideLogCollect(@ApplicationContext context: Context): LocalLogCollector {
|
||||||
return LogcatCollector.init(context = context)
|
return LogcatUtil.init(context = context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,14 +54,14 @@ class RepositoryModule {
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): TunnelConfigRepository {
|
fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao): TunnelConfigRepository {
|
||||||
return RoomTunnelConfigRepository(tunnelConfigDao, ioDispatcher)
|
return RoomTunnelConfigRepository(tunnelConfigDao)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideSettingsRepository(settingsDao: SettingsDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): SettingsRepository {
|
fun provideSettingsRepository(settingsDao: SettingsDao): SettingsRepository {
|
||||||
return RoomSettingsRepository(settingsDao, ioDispatcher)
|
return RoomSettingsRepository(settingsDao)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|||||||
@@ -3,14 +3,12 @@ package com.zaneschepke.wireguardautotunnel.module
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.wireguard.android.backend.Backend
|
import com.wireguard.android.backend.Backend
|
||||||
import com.wireguard.android.backend.GoBackend
|
import com.wireguard.android.backend.GoBackend
|
||||||
import com.wireguard.android.backend.RootTunnelActionHandler
|
|
||||||
import com.wireguard.android.backend.WgQuickBackend
|
import com.wireguard.android.backend.WgQuickBackend
|
||||||
import com.wireguard.android.util.RootShell
|
import com.wireguard.android.util.RootShell
|
||||||
import com.wireguard.android.util.ToolsInstaller
|
import com.wireguard.android.util.ToolsInstaller
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
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.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
@@ -31,57 +29,49 @@ class TunnelModule {
|
|||||||
return RootShell(context)
|
return RootShell(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideRootShellAm(@ApplicationContext context: Context): org.amnezia.awg.util.RootShell {
|
|
||||||
return org.amnezia.awg.util.RootShell(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
@Userspace
|
@Userspace
|
||||||
fun provideUserspaceBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
|
fun provideUserspaceBackend(@ApplicationContext context: Context): Backend {
|
||||||
return GoBackend(context, RootTunnelActionHandler(rootShell))
|
return GoBackend(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
@Kernel
|
@Kernel
|
||||||
fun provideKernelBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
|
fun provideKernelBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
|
||||||
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell), RootTunnelActionHandler(rootShell))
|
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideAmneziaBackend(@ApplicationContext context: Context, rootShell: org.amnezia.awg.util.RootShell): org.amnezia.awg.backend.Backend {
|
fun provideAmneziaBackend(@ApplicationContext context: Context): org.amnezia.awg.backend.Backend {
|
||||||
return org.amnezia.awg.backend.GoBackend(context, org.amnezia.awg.backend.RootTunnelActionHandler(rootShell))
|
return org.amnezia.awg.backend.GoBackend(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideVpnService(
|
fun provideVpnService(
|
||||||
amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
||||||
|
@Userspace userspaceBackend: Provider<Backend>,
|
||||||
@Kernel kernelBackend: Provider<Backend>,
|
@Kernel kernelBackend: Provider<Backend>,
|
||||||
appDataRepository: AppDataRepository,
|
appDataRepository: AppDataRepository,
|
||||||
tunnelConfigRepository: TunnelConfigRepository,
|
|
||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
serviceManager: ServiceManager,
|
): VpnService {
|
||||||
): TunnelService {
|
|
||||||
return WireGuardTunnel(
|
return WireGuardTunnel(
|
||||||
amneziaBackend,
|
amneziaBackend,
|
||||||
tunnelConfigRepository,
|
userspaceBackend,
|
||||||
kernelBackend,
|
kernelBackend,
|
||||||
appDataRepository,
|
appDataRepository,
|
||||||
applicationScope,
|
applicationScope,
|
||||||
ioDispatcher,
|
ioDispatcher,
|
||||||
serviceManager,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
|
||||||
@Provides
|
@Provides
|
||||||
fun provideServiceManager(@ApplicationContext context: Context): ServiceManager {
|
@Singleton
|
||||||
return ServiceManager(context)
|
fun provideServiceManager(appDataRepository: AppDataRepository, @IoDispatcher ioDispatcher: CoroutineDispatcher): ServiceManager {
|
||||||
|
return ServiceManager(appDataRepository, ioDispatcher)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.receiver
|
|
||||||
|
|
||||||
import android.content.BroadcastReceiver
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
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 dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class AppUpdateReceiver : BroadcastReceiver() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var appDataRepository: AppDataRepository
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var serviceManager: ServiceManager
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
|
||||||
if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
|
|
||||||
applicationScope.launch {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (settings.isAutoTunnelEnabled) {
|
|
||||||
Timber.i("Restarting services after upgrade")
|
|
||||||
serviceManager.startAutoTunnel(true)
|
|
||||||
}
|
|
||||||
if (!settings.isAutoTunnelEnabled) {
|
|
||||||
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
|
||||||
if (tunnels.isNotEmpty()) tunnelService.get().startTunnel(tunnels.first(), true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,14 +6,11 @@ import android.content.Intent
|
|||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class BootReceiver : BroadcastReceiver() {
|
class BootReceiver : BroadcastReceiver() {
|
||||||
@@ -21,29 +18,35 @@ class BootReceiver : BroadcastReceiver() {
|
|||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
@Inject
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
lateinit var serviceManager: ServiceManager
|
if (Intent.ACTION_BOOT_COMPLETED != intent?.action) return
|
||||||
|
context?.run {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
applicationScope.launch {
|
||||||
if (Intent.ACTION_BOOT_COMPLETED != intent.action) return
|
val settings = appDataRepository.settings.getSettings()
|
||||||
applicationScope.launch {
|
if (settings.isRestoreOnBootEnabled) {
|
||||||
with(appDataRepository.settings.getSettings()) {
|
if (settings.isAutoTunnelEnabled) {
|
||||||
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")
|
|
||||||
tunnelService.get().startTunnel(activeTunnels.first(), true)
|
|
||||||
}
|
|
||||||
if (isAutoTunnelEnabled) {
|
|
||||||
Timber.i("Starting watcher service from boot")
|
Timber.i("Starting watcher service from boot")
|
||||||
serviceManager.startAutoTunnel(true)
|
serviceManager.startWatcherServiceForeground(context)
|
||||||
|
}
|
||||||
|
if (appDataRepository.appState.isTunnelRunningFromManualStart()) {
|
||||||
|
appDataRepository.appState.getActiveTunnelId()?.let {
|
||||||
|
Timber.i("Starting tunnel that was active before reboot")
|
||||||
|
serviceManager.startVpnServiceForeground(
|
||||||
|
context,
|
||||||
|
appDataRepository.tunnels.getById(it)?.id,
|
||||||
|
)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (settings.isAlwaysOnVpnEnabled) {
|
||||||
|
Timber.i("Starting vpn service from boot AOVPN")
|
||||||
|
serviceManager.startVpnServiceForeground(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.receiver
|
|
||||||
|
|
||||||
import android.content.BroadcastReceiver
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class KernelReceiver : BroadcastReceiver() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
|
||||||
val action = intent.action ?: return
|
|
||||||
applicationScope.launch {
|
|
||||||
if (action == REFRESH_TUNNELS_ACTION) {
|
|
||||||
tunnelService.get().runningTunnelNames().forEach { name ->
|
|
||||||
// TODO can optimize later
|
|
||||||
val tunnel = tunnelConfigRepository.findByTunnelName(name)
|
|
||||||
tunnel?.let {
|
|
||||||
tunnelConfigRepository.save(it.copy(isActive = true))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val REFRESH_TUNNELS_ACTION = "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.receiver
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class NotificationActionReceiver : BroadcastReceiver() {
|
||||||
|
@Inject
|
||||||
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@ApplicationScope
|
||||||
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
|
override fun onReceive(context: Context, intent: Intent?) {
|
||||||
|
applicationScope.launch {
|
||||||
|
try {
|
||||||
|
// TODO fix for manual start changes when enabled
|
||||||
|
serviceManager.stopVpnServiceForeground(context)
|
||||||
|
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
||||||
|
serviceManager.startVpnServiceForeground(context)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-556
@@ -1,556 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.NetworkCapabilities
|
|
||||||
import android.os.IBinder
|
|
||||||
import android.os.PowerManager
|
|
||||||
import androidx.core.app.ServiceCompat
|
|
||||||
import androidx.lifecycle.LifecycleService
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.wireguard.android.util.RootShell
|
|
||||||
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
|
|
||||||
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
|
||||||
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.getCurrentWifiName
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isDown
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
|
||||||
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
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.InetAddress
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class AutoTunnelService : LifecycleService() {
|
|
||||||
private val foregroundId = 122
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var rootShell: Provider<RootShell>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var wifiService: NetworkService<WifiService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var mobileDataService: NetworkService<MobileDataService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var ethernetService: NetworkService<EthernetService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var appDataRepository: AppDataRepository
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var notificationService: NotificationService
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@IoDispatcher
|
|
||||||
lateinit var ioDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var serviceManager: ServiceManager
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@MainImmediateDispatcher
|
|
||||||
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
private val autoTunnelStateFlow = MutableStateFlow(AutoTunnelState())
|
|
||||||
|
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
launchWatcherNotification()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
serviceManager.autoTunnelService.complete(this)
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start() {
|
|
||||||
kotlin.runCatching {
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
launchWatcherNotification()
|
|
||||||
initWakeLock()
|
|
||||||
}
|
|
||||||
startSettingsJob()
|
|
||||||
startVpnStateJob()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stop() {
|
|
||||||
wakeLock?.let {
|
|
||||||
if (it.isHeld) {
|
|
||||||
it.release()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stopSelf()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
cancelAndResetNetworkJobs()
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
serviceManager.autoTunnelService = CompletableDeferred()
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
|
|
||||||
val notification =
|
|
||||||
notificationService.createNotification(
|
|
||||||
channelId = getString(R.string.watcher_channel_id),
|
|
||||||
channelName = getString(R.string.watcher_channel_name),
|
|
||||||
title = getString(R.string.auto_tunnel_title),
|
|
||||||
description = description,
|
|
||||||
)
|
|
||||||
ServiceCompat.startForeground(
|
|
||||||
this,
|
|
||||||
foregroundId,
|
|
||||||
notification,
|
|
||||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
|
|
||||||
} finally {
|
|
||||||
release()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 {
|
|
||||||
it.copy(
|
|
||||||
isMobileDataConnected = true,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isMobileDataConnected = true,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Timber.i("Mobile data capabilities changed")
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isMobileDataConnected = false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Timber.i("Lost mobile data connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForPingFailure() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
Timber.i("Starting ping watcher")
|
|
||||||
runCatching {
|
|
||||||
do {
|
|
||||||
val vpnState = tunnelService.get().vpnState.value
|
|
||||||
if (vpnState.status == TunnelState.UP) {
|
|
||||||
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")
|
|
||||||
config.peers.map { peer ->
|
|
||||||
peer.isReachable()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delay(vpnState.tunnelConfig?.pingInterval ?: Constants.PING_INTERVAL)
|
|
||||||
} while (true)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
Timber.d("got new settings: ${it.settings}")
|
|
||||||
manageJobsBySettings(it.settings)
|
|
||||||
autoTunnelStateFlow.emit(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForVpnStateChanges() {
|
|
||||||
Timber.i("Starting vpn state watcher")
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
tunnelService.get().vpnState.distinctUntilChanged { old, new ->
|
|
||||||
old.tunnelConfig == new.tunnelConfig && old.status == new.status
|
|
||||||
}.collect { state ->
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(vpnState = state)
|
|
||||||
}
|
|
||||||
state.tunnelConfig?.let {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
|
||||||
}
|
|
||||||
if (!it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun manageJobsBySettings(settings: Settings) {
|
|
||||||
with(settings) {
|
|
||||||
if (isPingEnabled) {
|
|
||||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
|
||||||
} else {
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
}
|
|
||||||
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 fun updateWifi(connected: Boolean) {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isWifiConnected = 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.i("Ethernet capabilities changed")
|
|
||||||
updateEthernet(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
updateEthernet(false)
|
|
||||||
Timber.i("Lost Ethernet connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
updateWifi(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.i("Wifi capabilities changed")
|
|
||||||
updateWifi(true)
|
|
||||||
val ssid = getWifiSSID(status.networkCapabilities)
|
|
||||||
ssid?.let { name ->
|
|
||||||
if (name.contains(Constants.UNREADABLE_SSID)) {
|
|
||||||
Timber.w("SSID unreadable: missing permissions")
|
|
||||||
} else {
|
|
||||||
Timber.i("Detected valid SSID")
|
|
||||||
}
|
|
||||||
appDataRepository.appState.setCurrentSsid(name)
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
currentNetworkSSID = name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} ?: Timber.w("Failed to read ssid")
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
updateWifi(false)
|
|
||||||
Timber.i("Lost Wi-Fi connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getWifiSSID(networkCapabilities: NetworkCapabilities): String? {
|
|
||||||
return withContext(ioDispatcher) {
|
|
||||||
with(autoTunnelStateFlow.value.settings) {
|
|
||||||
if (isWifiNameByShellEnabled) return@withContext rootShell.get().getCurrentWifiName()
|
|
||||||
wifiService.getNetworkName(networkCapabilities)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getMobileDataTunnel(): TunnelConfig? {
|
|
||||||
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun handleNetworkEventChanges() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
Timber.i("Starting network event watcher")
|
|
||||||
autoTunnelStateFlow.collectLatest { watcherState ->
|
|
||||||
val autoTunnel = "Auto-tunnel watcher"
|
|
||||||
Timber.d("New watcher state!")
|
|
||||||
// delay for rapid network state changes and then collect latest
|
|
||||||
delay(Constants.WATCHER_COLLECTION_DELAY)
|
|
||||||
val activeTunnel = watcherState.vpnState.tunnelConfig
|
|
||||||
val defaultTunnel = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
when {
|
|
||||||
watcherState.isEthernetConditionMet() -> {
|
|
||||||
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
|
|
||||||
if (watcherState.vpnState.isDown()) {
|
|
||||||
defaultTunnel?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isMobileDataConditionMet() -> {
|
|
||||||
Timber.i("$autoTunnel - tunnel on mobile data condition met")
|
|
||||||
val mobileDataTunnel = getMobileDataTunnel()
|
|
||||||
val tunnel =
|
|
||||||
mobileDataTunnel ?: defaultTunnel
|
|
||||||
if (watcherState.vpnState.isDown() || activeTunnel?.isMobileDataTunnel == false) {
|
|
||||||
tunnel?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
|
|
||||||
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
|
|
||||||
if (!watcherState.vpnState.isDown()) {
|
|
||||||
activeTunnel?.let {
|
|
||||||
tunnelService.get().stopTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isUntrustedWifiConditionMet() -> {
|
|
||||||
Timber.i("Untrusted wifi condition met")
|
|
||||||
if (activeTunnel == null || watcherState.isCurrentSSIDActiveTunnelNetwork() == false ||
|
|
||||||
watcherState.vpnState.isDown()
|
|
||||||
) {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
|
|
||||||
)
|
|
||||||
watcherState.getTunnelWithMatchingTunnelNetwork()?.let {
|
|
||||||
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
|
|
||||||
if (watcherState.vpnState.isDown() || activeTunnel?.id != it.id) {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
} ?: suspend {
|
|
||||||
Timber.i("No tunnel associated with this SSID, using defaults")
|
|
||||||
val default = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
if (default?.name != tunnelService.get().name || watcherState.vpnState.isDown()) {
|
|
||||||
default?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTrustedWifiConditionMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!watcherState.vpnState.isDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnWifiConditionMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on wifi condition met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!watcherState.vpnState.isDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnNoConnectivityMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on no connectivity met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!watcherState.vpnState.isDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
Timber.i("$autoTunnel - no condition met")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+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
|
||||||
|
}
|
||||||
|
}
|
||||||
+92
-58
@@ -3,81 +3,115 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
|||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.util.SingletonHolder
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
import jakarta.inject.Inject
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.flow.update
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
class ServiceManager(
|
||||||
class ServiceManager
|
private val appDataRepository: AppDataRepository,
|
||||||
@Inject constructor(private val context: Context) {
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
) {
|
||||||
|
private fun <T : Service> actionOnService(action: Action, context: Context, cls: Class<T>, extras: Map<String, Int>? = null) {
|
||||||
|
val intent =
|
||||||
|
Intent(context, cls).also {
|
||||||
|
it.action = action.name
|
||||||
|
extras?.forEach { (k, v) -> it.putExtra(k, v) }
|
||||||
|
}
|
||||||
|
intent.component?.javaClass
|
||||||
|
try {
|
||||||
|
when (action) {
|
||||||
|
Action.START_FOREGROUND, Action.STOP_FOREGROUND ->
|
||||||
|
context.startForegroundService(
|
||||||
|
intent,
|
||||||
|
)
|
||||||
|
|
||||||
private val _autoTunnelActive = MutableStateFlow(false)
|
Action.START, Action.STOP -> context.startService(intent)
|
||||||
|
}
|
||||||
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e.message)
|
||||||
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
|
||||||
var backgroundService = CompletableDeferred<TunnelBackgroundService>()
|
|
||||||
|
|
||||||
companion object : SingletonHolder<ServiceManager, Context>(::ServiceManager)
|
|
||||||
|
|
||||||
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
|
||||||
val intent = Intent(context, cls)
|
|
||||||
if (background) {
|
|
||||||
context.startForegroundService(intent)
|
|
||||||
} else {
|
|
||||||
context.startService(intent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startAutoTunnel(background: Boolean) {
|
suspend fun startVpnService(context: Context, tunnelId: Int? = null, isManualStart: Boolean = false) {
|
||||||
if (autoTunnelService.isCompleted) return _autoTunnelActive.update { true }
|
if (isManualStart) onManualStart(tunnelId)
|
||||||
kotlin.runCatching {
|
actionOnService(
|
||||||
startService(AutoTunnelService::class.java, background)
|
Action.START,
|
||||||
autoTunnelService.await()
|
context,
|
||||||
autoTunnelService.getCompleted().start()
|
WireGuardTunnelService::class.java,
|
||||||
_autoTunnelActive.update { true }
|
tunnelId?.let { mapOf(Constants.TUNNEL_EXTRA_KEY to it) },
|
||||||
}.onFailure {
|
)
|
||||||
Timber.e(it)
|
}
|
||||||
|
|
||||||
|
suspend fun stopVpnServiceForeground(context: Context, isManualStop: Boolean = false) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
if (isManualStop) onManualStop()
|
||||||
|
Timber.i("Stopping vpn service")
|
||||||
|
actionOnService(
|
||||||
|
Action.STOP_FOREGROUND,
|
||||||
|
context,
|
||||||
|
WireGuardTunnelService::class.java,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startBackgroundService() {
|
suspend fun stopVpnService(context: Context, isManualStop: Boolean = false) {
|
||||||
if (backgroundService.isCompleted) return
|
withContext(ioDispatcher) {
|
||||||
kotlin.runCatching {
|
if (isManualStop) onManualStop()
|
||||||
startService(TunnelBackgroundService::class.java, true)
|
Timber.i("Stopping vpn service")
|
||||||
backgroundService.await()
|
actionOnService(
|
||||||
backgroundService.getCompleted().start()
|
Action.STOP,
|
||||||
}.onFailure {
|
context,
|
||||||
Timber.e(it)
|
WireGuardTunnelService::class.java,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopBackgroundService() {
|
private suspend fun onManualStop() {
|
||||||
if (!backgroundService.isCompleted) return
|
appDataRepository.appState.setManualStop()
|
||||||
runCatching {
|
}
|
||||||
backgroundService.getCompleted().stop()
|
|
||||||
}.onFailure {
|
private suspend fun onManualStart(tunnelId: Int?) {
|
||||||
Timber.e(it)
|
tunnelId?.let {
|
||||||
|
appDataRepository.appState.setTunnelRunningFromManualStart(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopAutoTunnel() {
|
suspend fun startVpnServiceForeground(context: Context, tunnelId: Int? = null, isManualStart: Boolean = false) {
|
||||||
if (!autoTunnelService.isCompleted) return
|
withContext(ioDispatcher) {
|
||||||
runCatching {
|
if (isManualStart) onManualStart(tunnelId)
|
||||||
autoTunnelService.getCompleted().stop()
|
actionOnService(
|
||||||
_autoTunnelActive.update { false }
|
Action.START_FOREGROUND,
|
||||||
}.onFailure {
|
context,
|
||||||
Timber.e(it)
|
WireGuardTunnelService::class.java,
|
||||||
|
tunnelId?.let { mapOf(Constants.TUNNEL_EXTRA_KEY to it) },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestTunnelTileUpdate() {
|
fun startWatcherServiceForeground(context: Context) {
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
actionOnService(
|
||||||
|
Action.START_FOREGROUND,
|
||||||
|
context,
|
||||||
|
WireGuardConnectivityWatcherService::class.java,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun startWatcherService(context: Context) {
|
||||||
|
actionOnService(
|
||||||
|
Action.START,
|
||||||
|
context,
|
||||||
|
WireGuardConnectivityWatcherService::class.java,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stopWatcherService(context: Context) {
|
||||||
|
actionOnService(
|
||||||
|
Action.STOP,
|
||||||
|
context,
|
||||||
|
WireGuardConnectivityWatcherService::class.java,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-69
@@ -1,69 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
import android.app.Notification
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.IBinder
|
|
||||||
import androidx.core.app.ServiceCompat
|
|
||||||
import androidx.lifecycle.LifecycleService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class TunnelBackgroundService : LifecycleService() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var notificationService: NotificationService
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var serviceManager: ServiceManager
|
|
||||||
|
|
||||||
private val foregroundId = 123
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
start()
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
serviceManager.backgroundService.complete(this)
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start() {
|
|
||||||
ServiceCompat.startForeground(
|
|
||||||
this,
|
|
||||||
foregroundId,
|
|
||||||
createNotification(),
|
|
||||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stop() {
|
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
||||||
stopSelf()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
serviceManager.backgroundService = CompletableDeferred()
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createNotification(): Notification {
|
|
||||||
return notificationService.createNotification(
|
|
||||||
getString(R.string.vpn_channel_id),
|
|
||||||
getString(R.string.vpn_channel_name),
|
|
||||||
getString(R.string.tunnel_start_text),
|
|
||||||
description = "",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+3
-37
@@ -1,19 +1,13 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
|
||||||
|
|
||||||
data class AutoTunnelState(
|
data class WatcherState(
|
||||||
val vpnState: VpnState = VpnState(),
|
|
||||||
val isWifiConnected: Boolean = false,
|
val isWifiConnected: Boolean = false,
|
||||||
val isEthernetConnected: Boolean = false,
|
val isEthernetConnected: Boolean = false,
|
||||||
val isMobileDataConnected: Boolean = false,
|
val isMobileDataConnected: Boolean = false,
|
||||||
val currentNetworkSSID: String = "",
|
val currentNetworkSSID: String = "",
|
||||||
val settings: Settings = Settings(),
|
val settings: Settings = Settings(),
|
||||||
val tunnels: TunnelConfigs = emptyList(),
|
|
||||||
) {
|
) {
|
||||||
fun isEthernetConditionMet(): Boolean {
|
fun isEthernetConditionMet(): Boolean {
|
||||||
return (
|
return (
|
||||||
@@ -44,7 +38,7 @@ data class AutoTunnelState(
|
|||||||
return (
|
return (
|
||||||
!isEthernetConnected &&
|
!isEthernetConnected &&
|
||||||
isWifiConnected &&
|
isWifiConnected &&
|
||||||
!isCurrentSSIDTrusted() &&
|
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
||||||
settings.isTunnelOnWifiEnabled
|
settings.isTunnelOnWifiEnabled
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -54,7 +48,7 @@ data class AutoTunnelState(
|
|||||||
!isEthernetConnected &&
|
!isEthernetConnected &&
|
||||||
(
|
(
|
||||||
isWifiConnected &&
|
isWifiConnected &&
|
||||||
isCurrentSSIDTrusted()
|
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -76,32 +70,4 @@ data class AutoTunnelState(
|
|||||||
!isMobileDataConnected
|
!isMobileDataConnected
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isCurrentSSIDTrusted(): Boolean {
|
|
||||||
return if (settings.isWildcardsEnabled) {
|
|
||||||
settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fun isCurrentSSIDActiveTunnelNetwork(): Boolean {
|
|
||||||
val currentTunnelNetworks = vpnState.tunnelConfig?.tunnelNetworks
|
|
||||||
return (
|
|
||||||
if (settings.isWildcardsEnabled) {
|
|
||||||
currentTunnelNetworks?.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
currentTunnelNetworks?.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
) == true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getTunnelWithMatchingTunnelNetwork(): TunnelConfig? {
|
|
||||||
return tunnels.firstOrNull {
|
|
||||||
if (settings.isWildcardsEnabled) {
|
|
||||||
it.tunnelNetworks.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
it.tunnelNetworks.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+474
@@ -0,0 +1,474 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.os.PowerManager
|
||||||
|
import androidx.core.app.ServiceCompat
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
|
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.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.net.InetAddress
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
|
private val foregroundId = 122
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var wifiService: NetworkService<WifiService>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var mobileDataService: NetworkService<MobileDataService>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var ethernetService: NetworkService<EthernetService>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var notificationService: NotificationService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var vpnService: VpnService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@IoDispatcher
|
||||||
|
lateinit var ioDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@MainImmediateDispatcher
|
||||||
|
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
private val networkEventsFlow = MutableStateFlow(WatcherState())
|
||||||
|
|
||||||
|
private var watcherJob: Job? = null
|
||||||
|
|
||||||
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
|
private val tag = this.javaClass.name
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
lifecycleScope.launch(mainImmediateDispatcher) {
|
||||||
|
try {
|
||||||
|
if (appDataRepository.settings.getSettings().isAutoTunnelPaused) {
|
||||||
|
launchWatcherPausedNotification()
|
||||||
|
} else {
|
||||||
|
launchWatcherNotification()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e("Failed to start watcher service, not enough permissions")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun startService(extras: Bundle?) {
|
||||||
|
super.startService(extras)
|
||||||
|
try {
|
||||||
|
// we need this lock so our service gets not affected by Doze Mode
|
||||||
|
lifecycleScope.launch { initWakeLock() }
|
||||||
|
cancelWatcherJob()
|
||||||
|
startWatcherJob()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e("Failed to launch watcher service, no permissions")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun stopService() {
|
||||||
|
super.stopService()
|
||||||
|
wakeLock?.let {
|
||||||
|
if (it.isHeld) {
|
||||||
|
it.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cancelWatcherJob()
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
|
||||||
|
val notification =
|
||||||
|
notificationService.createNotification(
|
||||||
|
channelId = getString(R.string.watcher_channel_id),
|
||||||
|
channelName = getString(R.string.watcher_channel_name),
|
||||||
|
title = getString(R.string.auto_tunnel_title),
|
||||||
|
description = description,
|
||||||
|
)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
foregroundId,
|
||||||
|
notification,
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchWatcherPausedNotification() {
|
||||||
|
launchWatcherNotification(getString(R.string.watcher_notification_text_paused))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initWakeLock() {
|
||||||
|
wakeLock =
|
||||||
|
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||||
|
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
|
||||||
|
try {
|
||||||
|
Timber.i("Initiating wakelock with 10 min timeout")
|
||||||
|
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
|
||||||
|
} finally {
|
||||||
|
release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelWatcherJob() {
|
||||||
|
try {
|
||||||
|
watcherJob?.cancel()
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
Timber.i("Watcher job cancelled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startWatcherJob() {
|
||||||
|
watcherJob =
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForMobileDataConnectivityChanges() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
mobileDataService.networkStatus.collect { status ->
|
||||||
|
when (status) {
|
||||||
|
is NetworkStatus.Available -> {
|
||||||
|
Timber.i("Gained Mobile data connection")
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isMobileDataConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isMobileDataConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Timber.i("Mobile data capabilities changed")
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isMobileDataConnected = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Timber.i("Lost mobile data connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForPingFailure() {
|
||||||
|
val context = this
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
try {
|
||||||
|
do {
|
||||||
|
if (vpnService.vpnState.value.status == TunnelState.UP) {
|
||||||
|
val tunnelConfig = vpnService.vpnState.value.tunnelConfig
|
||||||
|
tunnelConfig?.let {
|
||||||
|
val config = TunnelConfig.configFromWgQuick(it.wgQuick)
|
||||||
|
val results =
|
||||||
|
config.peers.map { peer ->
|
||||||
|
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
|
||||||
|
}
|
||||||
|
if (results.contains(false)) {
|
||||||
|
Timber.i("Restarting VPN for ping failure")
|
||||||
|
serviceManager.stopVpnServiceForeground(context)
|
||||||
|
delay(Constants.VPN_RESTART_DELAY)
|
||||||
|
serviceManager.startVpnServiceForeground(context, it.id)
|
||||||
|
delay(Constants.PING_COOLDOWN)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(Constants.PING_INTERVAL)
|
||||||
|
} while (true)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForSettingsChanges() {
|
||||||
|
appDataRepository.settings.getSettingsFlow().collect { settings ->
|
||||||
|
if (networkEventsFlow.value.settings.isAutoTunnelPaused
|
||||||
|
!= settings.isAutoTunnelPaused
|
||||||
|
) {
|
||||||
|
when (settings.isAutoTunnelPaused) {
|
||||||
|
true -> launchWatcherPausedNotification()
|
||||||
|
false -> launchWatcherNotification()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
settings = settings,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForEthernetConnectivityChanges() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
ethernetService.networkStatus.collect { status ->
|
||||||
|
when (status) {
|
||||||
|
is NetworkStatus.Available -> {
|
||||||
|
Timber.i("Gained Ethernet connection")
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isEthernetConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
Timber.i("Ethernet capabilities changed")
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isEthernetConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isEthernetConnected = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Timber.i("Lost Ethernet connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForWifiConnectivityChanges() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
wifiService.networkStatus.collect { status ->
|
||||||
|
when (status) {
|
||||||
|
is NetworkStatus.Available -> {
|
||||||
|
Timber.i("Gained Wi-Fi connection")
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isWifiConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
Timber.i("Wifi capabilities changed")
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isWifiConnected = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val ssid = wifiService.getNetworkName(status.networkCapabilities)
|
||||||
|
ssid?.let { name ->
|
||||||
|
if (name.contains(Constants.UNREADABLE_SSID)) {
|
||||||
|
Timber.w("SSID unreadable: missing permissions")
|
||||||
|
} else {
|
||||||
|
Timber.i("Detected valid SSID")
|
||||||
|
}
|
||||||
|
appDataRepository.appState.setCurrentSsid(name)
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
currentNetworkSSID = name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} ?: Timber.w("Failed to read ssid")
|
||||||
|
}
|
||||||
|
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.update {
|
||||||
|
it.copy(
|
||||||
|
isWifiConnected = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Timber.i("Lost Wi-Fi connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getMobileDataTunnel(): TunnelConfig? {
|
||||||
|
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getSsidTunnel(ssid: String): TunnelConfig? {
|
||||||
|
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isTunnelDown(): Boolean {
|
||||||
|
return vpnService.vpnState.value.status == TunnelState.DOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun manageVpn() {
|
||||||
|
val context = this
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
networkEventsFlow.collectLatest { watcherState ->
|
||||||
|
val autoTunnel = "Auto-tunnel watcher"
|
||||||
|
if (!watcherState.settings.isAutoTunnelPaused) {
|
||||||
|
// delay for rapid network state changes and then collect latest
|
||||||
|
delay(Constants.WATCHER_COLLECTION_DELAY)
|
||||||
|
val tunnelConfig = vpnService.vpnState.value.tunnelConfig
|
||||||
|
when {
|
||||||
|
watcherState.isEthernetConditionMet() -> {
|
||||||
|
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
|
||||||
|
if (isTunnelDown()) serviceManager.startVpnServiceForeground(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isMobileDataConditionMet() -> {
|
||||||
|
Timber.i("$autoTunnel - tunnel on mobile data condition met")
|
||||||
|
val mobileDataTunnel = getMobileDataTunnel()
|
||||||
|
val tunnel =
|
||||||
|
mobileDataTunnel ?: appDataRepository.getPrimaryOrFirstTunnel()
|
||||||
|
if (isTunnelDown() || tunnelConfig?.isMobileDataTunnel == false) {
|
||||||
|
serviceManager.startVpnServiceForeground(
|
||||||
|
context,
|
||||||
|
tunnel?.id,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
|
||||||
|
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
|
||||||
|
if (!isTunnelDown()) serviceManager.stopVpnServiceForeground(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isUntrustedWifiConditionMet() -> {
|
||||||
|
if (tunnelConfig?.tunnelNetworks?.contains(watcherState.currentNetworkSSID) == false ||
|
||||||
|
tunnelConfig == null
|
||||||
|
) {
|
||||||
|
Timber.i(
|
||||||
|
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
|
||||||
|
)
|
||||||
|
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
|
||||||
|
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
|
||||||
|
if (isTunnelDown() || tunnelConfig?.id != it.id) {
|
||||||
|
serviceManager.startVpnServiceForeground(
|
||||||
|
context,
|
||||||
|
it.id,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} ?: suspend {
|
||||||
|
Timber.i("No tunnel associated with this SSID, using defaults")
|
||||||
|
val default = appDataRepository.getPrimaryOrFirstTunnel()
|
||||||
|
if (default?.name != vpnService.name) {
|
||||||
|
default?.let {
|
||||||
|
serviceManager.startVpnServiceForeground(context, it.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isTrustedWifiConditionMet() -> {
|
||||||
|
Timber.i(
|
||||||
|
"$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off",
|
||||||
|
)
|
||||||
|
if (!isTunnelDown()) serviceManager.stopVpnServiceForeground(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isTunnelOffOnWifiConditionMet() -> {
|
||||||
|
Timber.i(
|
||||||
|
"$autoTunnel - tunnel off on wifi condition met, turning vpn off",
|
||||||
|
)
|
||||||
|
if (!isTunnelDown()) serviceManager.stopVpnServiceForeground(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
watcherState.isTunnelOffOnNoConnectivityMet() -> {
|
||||||
|
Timber.i(
|
||||||
|
"$autoTunnel - tunnel off on no connectivity met, turning vpn off",
|
||||||
|
)
|
||||||
|
if (!isTunnelDown()) serviceManager.stopVpnServiceForeground(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
Timber.i("$autoTunnel - no condition met")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+198
@@ -0,0 +1,198 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.core.app.ServiceCompat
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.handshakeStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.mapPeerStats
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class WireGuardTunnelService : ForegroundService() {
|
||||||
|
private val foregroundId = 123
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var vpnService: VpnService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var notificationService: NotificationService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@MainImmediateDispatcher
|
||||||
|
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@IoDispatcher
|
||||||
|
lateinit var ioDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
private var job: Job? = null
|
||||||
|
|
||||||
|
private var didShowConnected = false
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
lifecycleScope.launch(mainImmediateDispatcher) {
|
||||||
|
// TODO fix this to not launch if AOVPN
|
||||||
|
if (appDataRepository.tunnels.count() != 0) {
|
||||||
|
launchVpnNotification()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun startService(extras: Bundle?) {
|
||||||
|
super.startService(extras)
|
||||||
|
cancelJob()
|
||||||
|
job =
|
||||||
|
lifecycleScope.launch {
|
||||||
|
launch {
|
||||||
|
val tunnelId = extras?.getInt(Constants.TUNNEL_EXTRA_KEY)
|
||||||
|
if (vpnService.getState() == TunnelState.UP) {
|
||||||
|
vpnService.stopTunnel()
|
||||||
|
}
|
||||||
|
vpnService.startTunnel(
|
||||||
|
tunnelId?.let {
|
||||||
|
appDataRepository.tunnels.getById(it)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
launch {
|
||||||
|
handshakeNotifications()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO improve tunnel notifications
|
||||||
|
private suspend fun handshakeNotifications() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
var tunnelName: String? = null
|
||||||
|
vpnService.vpnState.collect { state ->
|
||||||
|
state.statistics
|
||||||
|
?.mapPeerStats()
|
||||||
|
?.map { it.value?.handshakeStatus() }
|
||||||
|
.let { statuses ->
|
||||||
|
when {
|
||||||
|
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> {
|
||||||
|
if (!didShowConnected) {
|
||||||
|
delay(Constants.VPN_CONNECTED_NOTIFICATION_DELAY)
|
||||||
|
tunnelName = state.tunnelConfig?.name
|
||||||
|
launchVpnNotification(
|
||||||
|
getString(R.string.tunnel_start_title),
|
||||||
|
"${getString(R.string.tunnel_start_text)} - $tunnelName",
|
||||||
|
)
|
||||||
|
didShowConnected = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
statuses?.any { it == HandshakeStatus.STALE } == true -> {}
|
||||||
|
statuses?.all { it == HandshakeStatus.NOT_STARTED } ==
|
||||||
|
true -> {
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (state.status == TunnelState.UP && state.tunnelConfig?.name != tunnelName) {
|
||||||
|
tunnelName = state.tunnelConfig?.name
|
||||||
|
launchVpnNotification(
|
||||||
|
getString(R.string.tunnel_start_title),
|
||||||
|
"${getString(R.string.tunnel_start_text)} - $tunnelName",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchAlwaysOnDisabledNotification() {
|
||||||
|
launchVpnNotification(
|
||||||
|
title = this.getString(R.string.vpn_connection_failed),
|
||||||
|
description = this.getString(R.string.always_on_disabled),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun stopService() {
|
||||||
|
super.stopService()
|
||||||
|
lifecycleScope.launch {
|
||||||
|
vpnService.stopTunnel()
|
||||||
|
didShowConnected = false
|
||||||
|
}
|
||||||
|
cancelJob()
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchVpnNotification(title: String = getString(R.string.vpn_starting), description: String = getString(R.string.attempt_connection)) {
|
||||||
|
val notification =
|
||||||
|
notificationService.createNotification(
|
||||||
|
channelId = getString(R.string.vpn_channel_id),
|
||||||
|
channelName = getString(R.string.vpn_channel_name),
|
||||||
|
title = title,
|
||||||
|
onGoing = false,
|
||||||
|
vibration = false,
|
||||||
|
showTimestamp = true,
|
||||||
|
description = description,
|
||||||
|
)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
foregroundId,
|
||||||
|
notification,
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchVpnConnectionFailedNotification(message: String) {
|
||||||
|
val notification =
|
||||||
|
notificationService.createNotification(
|
||||||
|
channelId = getString(R.string.vpn_channel_id),
|
||||||
|
channelName = getString(R.string.vpn_channel_name),
|
||||||
|
action =
|
||||||
|
PendingIntent.getBroadcast(
|
||||||
|
this,
|
||||||
|
0,
|
||||||
|
Intent(this, NotificationActionReceiver::class.java),
|
||||||
|
PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
),
|
||||||
|
actionText = getString(R.string.restart),
|
||||||
|
title = getString(R.string.vpn_connection_failed),
|
||||||
|
onGoing = false,
|
||||||
|
vibration = true,
|
||||||
|
showTimestamp = true,
|
||||||
|
description = message,
|
||||||
|
)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
foregroundId,
|
||||||
|
notification,
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelJob() {
|
||||||
|
try {
|
||||||
|
job?.cancel()
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
Timber.i("Tunnel job cancelled")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-1
@@ -5,6 +5,8 @@ import android.net.ConnectivityManager
|
|||||||
import android.net.Network
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
import android.net.NetworkRequest
|
import android.net.NetworkRequest
|
||||||
|
import android.net.wifi.SupplicantState
|
||||||
|
import android.net.wifi.WifiInfo
|
||||||
import android.net.wifi.WifiManager
|
import android.net.wifi.WifiManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
@@ -19,7 +21,7 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|||||||
private val connectivityManager =
|
private val connectivityManager =
|
||||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
|
||||||
val wifiManager =
|
private val wifiManager =
|
||||||
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||||
|
|
||||||
override val networkStatus =
|
override val networkStatus =
|
||||||
@@ -81,6 +83,30 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|||||||
|
|
||||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
||||||
|
var ssid: String? = getWifiNameFromCapabilities(networkCapabilities)
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
|
||||||
|
val info = wifiManager.connectionInfo
|
||||||
|
if (info.supplicantState === SupplicantState.COMPLETED) {
|
||||||
|
ssid = info.ssid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ssid?.trim('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private fun getWifiNameFromCapabilities(networkCapabilities: NetworkCapabilities): String? {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
val info: WifiInfo
|
||||||
|
if (networkCapabilities.transportInfo is WifiInfo) {
|
||||||
|
info = networkCapabilities.transportInfo as WifiInfo
|
||||||
|
return info.ssid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <Result> Flow<NetworkStatus>.map(
|
inline fun <Result> Flow<NetworkStatus>.map(
|
||||||
|
|||||||
+1
-6
@@ -10,9 +10,4 @@ class EthernetService
|
|||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
) :
|
) :
|
||||||
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET) {
|
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET)
|
||||||
|
|
||||||
override fun isNetworkSecure(): Boolean {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+1
-5
@@ -10,8 +10,4 @@ class MobileDataService
|
|||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
) :
|
) :
|
||||||
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR) {
|
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||||
override fun isNetworkSecure(): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
-16
@@ -1,16 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
|
||||||
|
|
||||||
import android.net.NetworkCapabilities
|
|
||||||
import android.net.wifi.WifiInfo
|
|
||||||
import android.os.Build
|
|
||||||
|
|
||||||
fun NetworkCapabilities.getWifiName(): String? {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
val info: WifiInfo
|
|
||||||
if (transportInfo is WifiInfo) {
|
|
||||||
info = transportInfo as WifiInfo
|
|
||||||
return info.ssid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
+1
-5
@@ -4,11 +4,7 @@ import android.net.NetworkCapabilities
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface NetworkService<T> {
|
interface NetworkService<T> {
|
||||||
fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
fun getNetworkName(networkCapabilities: NetworkCapabilities): String?
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isNetworkSecure(): Boolean
|
|
||||||
|
|
||||||
val networkStatus: Flow<NetworkStatus>
|
val networkStatus: Flow<NetworkStatus>
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-20
@@ -2,8 +2,6 @@ package com.zaneschepke.wireguardautotunnel.service.network
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
import android.net.wifi.SupplicantState
|
|
||||||
import android.os.Build
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -12,21 +10,4 @@ class WifiService
|
|||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
) :
|
) :
|
||||||
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI) {
|
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI)
|
||||||
|
|
||||||
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
|
||||||
var ssid = networkCapabilities.getWifiName()
|
|
||||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
|
|
||||||
val info = wifiManager.connectionInfo
|
|
||||||
if (info.supplicantState === SupplicantState.COMPLETED) {
|
|
||||||
ssid = info.ssid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ssid?.trim('"')
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isNetworkSecure(): Boolean {
|
|
||||||
// TODO
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+2
-2
@@ -9,7 +9,7 @@ import android.content.Intent
|
|||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.MainActivity
|
import com.zaneschepke.wireguardautotunnel.ui.SplashActivity
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ constructor(
|
|||||||
}
|
}
|
||||||
notificationManager.createNotificationChannel(channel)
|
notificationManager.createNotificationChannel(channel)
|
||||||
val pendingIntent: PendingIntent =
|
val pendingIntent: PendingIntent =
|
||||||
Intent(context, MainActivity::class.java).let { notificationIntent ->
|
Intent(context, SplashActivity::class.java).let { notificationIntent ->
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
context,
|
context,
|
||||||
0,
|
0,
|
||||||
|
|||||||
+36
-24
@@ -5,24 +5,19 @@ import androidx.activity.ComponentActivity
|
|||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.Action
|
import com.zaneschepke.wireguardautotunnel.service.foreground.Action
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.AutoTunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class ShortcutsActivity : ComponentActivity() {
|
class ShortcutsActivity : ComponentActivity() {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@@ -36,26 +31,45 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
val settings = appDataRepository.settings.getSettings()
|
val settings = appDataRepository.settings.getSettings()
|
||||||
if (settings.isShortcutsEnabled) {
|
if (settings.isShortcutsEnabled) {
|
||||||
when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) {
|
when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) {
|
||||||
LEGACY_TUNNEL_SERVICE_NAME, TunnelService::class.java.simpleName -> {
|
WireGuardTunnelService::class.java.simpleName -> {
|
||||||
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
|
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
|
||||||
Timber.d("Tunnel name extra: $tunnelName")
|
val tunnelConfig =
|
||||||
val tunnelConfig = tunnelName?.let {
|
tunnelName?.let {
|
||||||
appDataRepository.tunnels.getAll()
|
appDataRepository.tunnels.getAll().firstOrNull {
|
||||||
.firstOrNull { it.name == tunnelName }
|
it.name == tunnelName
|
||||||
} ?: appDataRepository.getStartTunnelConfig()
|
}
|
||||||
Timber.d("Shortcut action on name: ${tunnelConfig?.name}")
|
|
||||||
tunnelConfig?.let {
|
|
||||||
when (intent.action) {
|
|
||||||
Action.START.name -> tunnelService.get().startTunnel(it, true)
|
|
||||||
Action.STOP.name -> tunnelService.get().stopTunnel(it)
|
|
||||||
else -> Unit
|
|
||||||
}
|
}
|
||||||
|
when (intent.action) {
|
||||||
|
Action.START.name ->
|
||||||
|
serviceManager.startVpnServiceForeground(
|
||||||
|
this@ShortcutsActivity,
|
||||||
|
tunnelConfig?.id,
|
||||||
|
isManualStart = true,
|
||||||
|
)
|
||||||
|
|
||||||
|
Action.STOP.name ->
|
||||||
|
serviceManager.stopVpnServiceForeground(
|
||||||
|
this@ShortcutsActivity,
|
||||||
|
isManualStop = true,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
|
||||||
|
WireGuardConnectivityWatcherService::class.java.simpleName -> {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
Action.START.name -> serviceManager.startAutoTunnel(true)
|
Action.START.name ->
|
||||||
Action.STOP.name -> serviceManager.stopAutoTunnel()
|
appDataRepository.settings.save(
|
||||||
|
settings.copy(
|
||||||
|
isAutoTunnelPaused = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
Action.STOP.name ->
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
settings.copy(
|
||||||
|
isAutoTunnelPaused = true,
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,8 +79,6 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService"
|
|
||||||
const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService"
|
|
||||||
const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
|
const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
|
||||||
const val CLASS_NAME_EXTRA_KEY = "className"
|
const val CLASS_NAME_EXTRA_KEY = "className"
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-57
@@ -1,18 +1,18 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tile
|
package com.zaneschepke.wireguardautotunnel.service.tile
|
||||||
|
|
||||||
import android.content.Intent
|
import android.os.Build
|
||||||
import android.os.IBinder
|
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
import androidx.lifecycle.ServiceLifecycleDispatcher
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
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.foreground.ServiceManager
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -25,88 +25,80 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
private val dispatcher = ServiceLifecycleDispatcher(this)
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
private var manualStartConfig: TunnelConfig? = null
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStopListening() {
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
val settings = appDataRepository.settings.getSettings()
|
||||||
updateTileState()
|
when (settings.isAutoTunnelEnabled) {
|
||||||
|
true -> {
|
||||||
|
if (settings.isAutoTunnelPaused) {
|
||||||
|
setInactive()
|
||||||
|
setTileDescription(this@AutoTunnelControlTile.getString(R.string.paused))
|
||||||
|
} else {
|
||||||
|
setActive()
|
||||||
|
setTileDescription(this@AutoTunnelControlTile.getString(R.string.active))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false -> {
|
||||||
|
setTileDescription(this@AutoTunnelControlTile.getString(R.string.disabled))
|
||||||
|
setUnavailable()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTileState() {
|
override fun onTileAdded() {
|
||||||
serviceManager.autoTunnelActive.value.let {
|
super.onTileAdded()
|
||||||
if (it) setActive() else setInactive()
|
onStartListening()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
if (serviceManager.autoTunnelActive.value) {
|
try {
|
||||||
serviceManager.stopAutoTunnel()
|
appDataRepository.toggleWatcherServicePause()
|
||||||
setInactive()
|
onStartListening()
|
||||||
} else {
|
} catch (e: Exception) {
|
||||||
serviceManager.startAutoTunnel(true)
|
Timber.e(e.message)
|
||||||
setActive()
|
} finally {
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setActive() {
|
private fun setActive() {
|
||||||
kotlin.runCatching {
|
qsTile.state = Tile.STATE_ACTIVE
|
||||||
qsTile.state = Tile.STATE_ACTIVE
|
qsTile.updateTile()
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setInactive() {
|
private fun setInactive() {
|
||||||
kotlin.runCatching {
|
qsTile.state = Tile.STATE_INACTIVE
|
||||||
qsTile.state = Tile.STATE_INACTIVE
|
qsTile.updateTile()
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUnavailable() {
|
private fun setUnavailable() {
|
||||||
kotlin.runCatching {
|
manualStartConfig = null
|
||||||
qsTile.state = Tile.STATE_UNAVAILABLE
|
qsTile.state = Tile.STATE_UNAVAILABLE
|
||||||
qsTile.updateTile()
|
qsTile.updateTile()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
private fun setTileDescription(description: String) {
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
var ret: IBinder? = null
|
qsTile.subtitle = description
|
||||||
try {
|
|
||||||
ret = super.onBind(intent)
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
Timber.e("Failed to bind to TunnelControlTile")
|
|
||||||
}
|
}
|
||||||
return ret
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
qsTile.stateDescription = description
|
||||||
|
}
|
||||||
|
qsTile.updateTile()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
override val lifecycle: Lifecycle
|
||||||
get() = lifecycleRegistry
|
get() = dispatcher.lifecycle
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-81
@@ -1,24 +1,22 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tile
|
package com.zaneschepke.wireguardautotunnel.service.tile
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
import androidx.lifecycle.ServiceLifecycleDispatcher
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
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.service.tunnel.VpnService
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelControlTile : TileService(), LifecycleOwner {
|
class TunnelControlTile : TileService(), LifecycleOwner {
|
||||||
@@ -26,117 +24,98 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
lateinit var vpnService: VpnService
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
lateinit var serviceManager: ServiceManager
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
private val dispatcher = ServiceLifecycleDispatcher(this)
|
||||||
|
|
||||||
override fun onCreate() {
|
private var manualStartConfig: TunnelConfig? = null
|
||||||
super.onCreate()
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStopListening() {
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
Timber.d("On start listening called")
|
||||||
Timber.d("Updating tile!")
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
when (vpnService.getState()) {
|
||||||
updateTileState()
|
TunnelState.UP -> {
|
||||||
|
setActive()
|
||||||
|
setTileDescription(vpnService.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
TunnelState.DOWN -> {
|
||||||
|
setInactive()
|
||||||
|
val config =
|
||||||
|
appDataRepository.getStartTunnelConfig()?.also { config ->
|
||||||
|
manualStartConfig = config
|
||||||
|
} ?: appDataRepository.getPrimaryOrFirstTunnel()
|
||||||
|
config?.let {
|
||||||
|
setTileDescription(it.name)
|
||||||
|
} ?: setUnavailable()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> setInactive()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateTileState() {
|
override fun onTileAdded() {
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
super.onTileAdded()
|
||||||
lastActive?.let {
|
onStartListening()
|
||||||
updateTile(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
try {
|
||||||
lastActive?.let { tunnel ->
|
if (vpnService.getState() == TunnelState.UP) {
|
||||||
if (tunnel.isActive) {
|
serviceManager.stopVpnServiceForeground(
|
||||||
tunnelService.get().stopTunnel(tunnel)
|
this@TunnelControlTile,
|
||||||
|
isManualStop = true,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
tunnelService.get().startTunnel(tunnel, true)
|
serviceManager.startVpnServiceForeground(
|
||||||
|
this@TunnelControlTile,
|
||||||
|
manualStartConfig?.id,
|
||||||
|
isManualStart = true,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
updateTileState()
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e.message)
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setActive() {
|
private fun setActive() {
|
||||||
kotlin.runCatching {
|
qsTile.state = Tile.STATE_ACTIVE
|
||||||
qsTile.state = Tile.STATE_ACTIVE
|
qsTile.updateTile()
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setInactive() {
|
private fun setInactive() {
|
||||||
kotlin.runCatching {
|
qsTile.state = Tile.STATE_INACTIVE
|
||||||
qsTile.state = Tile.STATE_INACTIVE
|
qsTile.updateTile()
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUnavailable() {
|
private fun setUnavailable() {
|
||||||
kotlin.runCatching {
|
manualStartConfig = null
|
||||||
qsTile.state = Tile.STATE_UNAVAILABLE
|
qsTile.state = Tile.STATE_UNAVAILABLE
|
||||||
setTileDescription("")
|
qsTile.updateTile()
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setTileDescription(description: String) {
|
private fun setTileDescription(description: String) {
|
||||||
kotlin.runCatching {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
qsTile.subtitle = description
|
||||||
qsTile.subtitle = description
|
|
||||||
}
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
||||||
qsTile.stateDescription = description
|
|
||||||
}
|
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
}
|
||||||
}
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
qsTile.stateDescription = description
|
||||||
private fun updateTile(tunnelConfig: TunnelConfig?) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
tunnelConfig?.let {
|
|
||||||
setTileDescription(it.name)
|
|
||||||
if (it.isActive) return setActive()
|
|
||||||
setInactive()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
qsTile.updateTile()
|
||||||
|
|
||||||
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
|
||||||
var ret: IBinder? = null
|
|
||||||
try {
|
|
||||||
ret = super.onBind(intent)
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
Timber.e("Failed to bind to TunnelControlTile")
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
override val lifecycle: Lifecycle
|
||||||
get() = lifecycleRegistry
|
get() = dispatcher.lifecycle
|
||||||
}
|
}
|
||||||
|
|||||||
-46
@@ -1,46 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.IBinder
|
|
||||||
import androidx.lifecycle.LifecycleService
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class AlwaysOnVpnService : LifecycleService() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var appDataRepository: AppDataRepository
|
|
||||||
|
|
||||||
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 || intent.component == null || intent.component!!.packageName != packageName) {
|
|
||||||
Timber.i("Always-on VPN requested started")
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (settings.isAlwaysOnVpnEnabled) {
|
|
||||||
val tunnel = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
tunnel?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Timber.w("Always-on VPN is not enabled in app settings")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
|
||||||
|
|
||||||
import com.wireguard.android.backend.Tunnel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
|
|
||||||
interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
|
||||||
suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean = false): Result<TunnelState>
|
|
||||||
|
|
||||||
suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
|
||||||
|
|
||||||
suspend fun bounceTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
|
||||||
|
|
||||||
val vpnState: StateFlow<VpnState>
|
|
||||||
|
|
||||||
suspend fun runningTunnelNames(): Set<String>
|
|
||||||
|
|
||||||
suspend fun getState(): TunnelState
|
|
||||||
|
|
||||||
fun cancelStatsJob()
|
|
||||||
|
|
||||||
fun startStatsJob()
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
|
import com.wireguard.android.backend.Tunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
|
interface VpnService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
||||||
|
suspend fun startTunnel(tunnelConfig: TunnelConfig? = null): TunnelState
|
||||||
|
|
||||||
|
suspend fun stopTunnel()
|
||||||
|
|
||||||
|
val vpnState: StateFlow<VpnState>
|
||||||
|
|
||||||
|
fun getState(): TunnelState
|
||||||
|
}
|
||||||
+136
-160
@@ -1,26 +1,27 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
import com.wireguard.android.backend.Backend
|
import com.wireguard.android.backend.Backend
|
||||||
|
import com.wireguard.android.backend.BackendException
|
||||||
import com.wireguard.android.backend.Tunnel.State
|
import com.wireguard.android.backend.Tunnel.State
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
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.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.module.Userspace
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.amnezia.awg.backend.Tunnel
|
import org.amnezia.awg.backend.Tunnel
|
||||||
@@ -31,144 +32,107 @@ import javax.inject.Provider
|
|||||||
class WireGuardTunnel
|
class WireGuardTunnel
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
private val userspaceAmneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
||||||
tunnelConfigRepository: TunnelConfigRepository,
|
@Userspace private val userspaceBackend: Provider<Backend>,
|
||||||
@Kernel private val kernelBackend: Provider<Backend>,
|
@Kernel private val kernelBackend: Provider<Backend>,
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
@ApplicationScope private val applicationScope: CoroutineScope,
|
@ApplicationScope private val applicationScope: CoroutineScope,
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
private val serviceManager: ServiceManager,
|
) : VpnService {
|
||||||
) : TunnelService {
|
|
||||||
|
|
||||||
private val _vpnState = MutableStateFlow(VpnState())
|
private val _vpnState = MutableStateFlow(VpnState())
|
||||||
override val vpnState: StateFlow<VpnState> = _vpnState.combine(
|
override val vpnState: StateFlow<VpnState> = _vpnState.asStateFlow()
|
||||||
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 var statsJob: Job? = null
|
||||||
|
|
||||||
private suspend fun backend(): Any {
|
private var backendIsWgUserspace = true
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (settings.isKernelEnabled) return kernelBackend.get()
|
|
||||||
return amneziaBackend.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun runningTunnelNames(): Set<String> {
|
private var backendIsAmneziaUserspace = false
|
||||||
return when (val backend = backend()) {
|
|
||||||
is Backend -> backend.runningTunnelNames
|
init {
|
||||||
is org.amnezia.awg.backend.Backend -> backend.runningTunnelNames
|
applicationScope.launch(ioDispatcher) {
|
||||||
else -> emptySet()
|
appDataRepository.settings.getSettingsFlow().collect {
|
||||||
|
if (it.isKernelEnabled && (backendIsWgUserspace || backendIsAmneziaUserspace)) {
|
||||||
|
Timber.i("Setting kernel backend")
|
||||||
|
backendIsWgUserspace = false
|
||||||
|
backendIsAmneziaUserspace = false
|
||||||
|
} else if (!it.isKernelEnabled && !it.isAmneziaEnabled && !backendIsWgUserspace) {
|
||||||
|
Timber.i("Setting WireGuard userspace backend")
|
||||||
|
backendIsWgUserspace = true
|
||||||
|
backendIsAmneziaUserspace = false
|
||||||
|
} else if (it.isAmneziaEnabled && !backendIsAmneziaUserspace) {
|
||||||
|
Timber.i("Setting Amnezia userspace backend")
|
||||||
|
backendIsAmneziaUserspace = true
|
||||||
|
backendIsWgUserspace = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun setState(tunnelConfig: TunnelConfig, tunnelState: TunnelState): Result<TunnelState> {
|
private fun setState(tunnelConfig: TunnelConfig?, tunnelState: TunnelState): TunnelState {
|
||||||
return runCatching {
|
return if (backendIsAmneziaUserspace) {
|
||||||
when (val backend = backend()) {
|
Timber.i("Using Amnezia backend")
|
||||||
is Backend -> backend.setState(this, tunnelState.toWgState(), TunnelConfig.configFromWgQuick(tunnelConfig.wgQuick)).let { TunnelState.from(it) }
|
val config =
|
||||||
is org.amnezia.awg.backend.Backend -> {
|
tunnelConfig?.let {
|
||||||
val config = if (tunnelConfig.amQuick.isBlank()) {
|
if (it.amQuick != "") {
|
||||||
TunnelConfig.configFromAmQuick(
|
TunnelConfig.configFromAmQuick(it.amQuick)
|
||||||
tunnelConfig.wgQuick,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
TunnelConfig.configFromAmQuick(tunnelConfig.amQuick)
|
Timber.w(
|
||||||
}
|
"Using backwards compatible wg config, amnezia specific config not found.",
|
||||||
backend.setState(this, tunnelState.toAmState(), config).let {
|
)
|
||||||
TunnelState.from(it)
|
TunnelConfig.configFromAmQuick(it.wgQuick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> throw NotImplementedError()
|
val state =
|
||||||
}
|
userspaceAmneziaBackend.get().setState(this, tunnelState.toAmState(), config)
|
||||||
}.onFailure {
|
TunnelState.from(state)
|
||||||
Timber.e(it)
|
} else {
|
||||||
|
Timber.i("Using Wg backend")
|
||||||
|
val wgConfig = tunnelConfig?.let { TunnelConfig.configFromWgQuick(it.wgQuick) }
|
||||||
|
val state =
|
||||||
|
backend().setState(
|
||||||
|
this,
|
||||||
|
tunnelState.toWgState(),
|
||||||
|
wgConfig,
|
||||||
|
)
|
||||||
|
TunnelState.from(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> {
|
override suspend fun startTunnel(tunnelConfig: TunnelConfig?): TunnelState {
|
||||||
return withContext(ioDispatcher) {
|
return withContext(ioDispatcher) {
|
||||||
onBeforeStart(tunnelConfig, background)
|
try {
|
||||||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
// TODO we need better error handling here
|
||||||
emitTunnelState(it)
|
// need to bubble up these errors to the UI
|
||||||
}.onFailure {
|
val config = tunnelConfig ?: appDataRepository.getPrimaryOrFirstTunnel()
|
||||||
Timber.e(it)
|
if (config != null) {
|
||||||
onStartFailed()
|
emitTunnelConfig(config)
|
||||||
|
setState(config, TunnelState.UP)
|
||||||
|
} else {
|
||||||
|
throw Exception("No tunnels")
|
||||||
|
}
|
||||||
|
} catch (e: BackendException) {
|
||||||
|
Timber.e("Failed to start tunnel with error: ${e.message}")
|
||||||
|
TunnelState.from(State.DOWN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
private fun backend(): Backend {
|
||||||
return withContext(ioDispatcher) {
|
return when {
|
||||||
onBeforeStop(tunnelConfig)
|
backendIsWgUserspace -> {
|
||||||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
userspaceBackend.get()
|
||||||
emitTunnelState(it)
|
}
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
!backendIsWgUserspace && !backendIsAmneziaUserspace -> {
|
||||||
onStopFailed()
|
kernelBackend.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
userspaceBackend.get()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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, background: Boolean) {
|
|
||||||
if (_vpnState.value.status == TunnelState.UP &&
|
|
||||||
tunnelConfig != _vpnState.value.tunnelConfig
|
|
||||||
) {
|
|
||||||
vpnState.value.tunnelConfig?.let { stopTunnel(it) }
|
|
||||||
}
|
|
||||||
if (background) serviceManager.startBackgroundService()
|
|
||||||
resetBackendStatistics()
|
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
|
||||||
emitVpnStateConfig(tunnelConfig)
|
|
||||||
startStatsJob()
|
|
||||||
Timber.d("Updating start")
|
|
||||||
serviceManager.requestTunnelTileUpdate()
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) {
|
|
||||||
cancelStatsJob()
|
|
||||||
resetBackendStatistics()
|
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
|
||||||
serviceManager.stopBackgroundService()
|
|
||||||
Timber.d("UPdating stop")
|
|
||||||
serviceManager.requestTunnelTileUpdate()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitTunnelState(state: TunnelState) {
|
private fun emitTunnelState(state: TunnelState) {
|
||||||
_vpnState.tryEmit(
|
_vpnState.tryEmit(
|
||||||
_vpnState.value.copy(
|
_vpnState.value.copy(
|
||||||
@@ -185,73 +149,85 @@ constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitVpnStateConfig(tunnelConfig: TunnelConfig) {
|
private suspend fun emitTunnelConfig(tunnelConfig: TunnelConfig?) {
|
||||||
_vpnState.tryEmit(
|
_vpnState.emit(
|
||||||
_vpnState.value.copy(
|
_vpnState.value.copy(
|
||||||
tunnelConfig = tunnelConfig,
|
tunnelConfig = tunnelConfig,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resetBackendStatistics() {
|
private fun resetVpnState() {
|
||||||
_vpnState.tryEmit(
|
_vpnState.tryEmit(VpnState())
|
||||||
_vpnState.value.copy(
|
|
||||||
statistics = null,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getState(): TunnelState {
|
override suspend fun stopTunnel() {
|
||||||
return when (val backend = backend()) {
|
withContext(ioDispatcher) {
|
||||||
is Backend -> backend.getState(this).let { TunnelState.from(it) }
|
try {
|
||||||
is org.amnezia.awg.backend.Backend -> backend.getState(this).let { TunnelState.from(it) }
|
if (getState() == TunnelState.UP) {
|
||||||
else -> TunnelState.DOWN
|
val state = setState(null, TunnelState.DOWN)
|
||||||
|
resetVpnState()
|
||||||
|
emitTunnelState(state)
|
||||||
|
}
|
||||||
|
} catch (e: BackendException) {
|
||||||
|
Timber.e("Failed to stop wireguard tunnel with error: ${e.message}")
|
||||||
|
} catch (e: org.amnezia.awg.backend.BackendException) {
|
||||||
|
Timber.e("Failed to stop amnezia tunnel with error: ${e.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelStatsJob() {
|
override fun getState(): TunnelState {
|
||||||
statsJob?.cancel()
|
return if (backendIsAmneziaUserspace) {
|
||||||
}
|
TunnelState.from(
|
||||||
|
userspaceAmneziaBackend.get().getState(this),
|
||||||
override fun startStatsJob() {
|
)
|
||||||
statsJob = startTunnelStatisticsJob()
|
} else {
|
||||||
|
TunnelState.from(backend().getState(this))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getName(): String {
|
override fun getName(): String {
|
||||||
return _vpnState.value.tunnelConfig?.name ?: ""
|
return _vpnState.value.tunnelConfig?.name ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startTunnelStatisticsJob() = applicationScope.launch(ioDispatcher) {
|
override fun onStateChange(newState: Tunnel.State) {
|
||||||
val backend = backend()
|
handleStateChange(TunnelState.from(newState))
|
||||||
delay(STATS_START_DELAY)
|
}
|
||||||
while (true) {
|
|
||||||
when (backend) {
|
private fun handleStateChange(state: TunnelState) {
|
||||||
is Backend -> emitBackendStatistics(
|
emitTunnelState(state)
|
||||||
WireGuardStatistics(backend.getStatistics(this@WireGuardTunnel)),
|
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
|
||||||
)
|
if (state == TunnelState.UP) {
|
||||||
is org.amnezia.awg.backend.Backend -> {
|
statsJob = startTunnelStatisticsJob()
|
||||||
emitBackendStatistics(
|
}
|
||||||
AmneziaStatistics(
|
if (state == TunnelState.DOWN) {
|
||||||
backend.getStatistics(this@WireGuardTunnel),
|
try {
|
||||||
),
|
statsJob?.cancel()
|
||||||
)
|
} catch (e: CancellationException) {
|
||||||
}
|
Timber.i("Stats job cancelled")
|
||||||
}
|
}
|
||||||
delay(VPN_STATISTIC_CHECK_INTERVAL)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStateChange(newState: Tunnel.State) {
|
private fun startTunnelStatisticsJob() = applicationScope.launch(ioDispatcher) {
|
||||||
emitTunnelState(TunnelState.from(newState))
|
while (true) {
|
||||||
|
if (backendIsAmneziaUserspace) {
|
||||||
|
emitBackendStatistics(
|
||||||
|
AmneziaStatistics(
|
||||||
|
userspaceAmneziaBackend.get().getStatistics(this@WireGuardTunnel),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
emitBackendStatistics(
|
||||||
|
WireGuardStatistics(backend().getStatistics(this@WireGuardTunnel)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStateChange(state: State) {
|
override fun onStateChange(state: State) {
|
||||||
emitTunnelState(TunnelState.from(state))
|
handleStateChange(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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
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(
|
data class AppUiState(
|
||||||
val settings: Settings = Settings(),
|
val snackbarMessage: String = "",
|
||||||
val tunnels: List<TunnelConfig> = emptyList(),
|
val snackbarMessageConsumed: Boolean = true,
|
||||||
val vpnState: VpnState = VpnState(),
|
val vpnPermissionAccepted: Boolean = false,
|
||||||
val generalState: GeneralState = GeneralState(),
|
val notificationPermissionAccepted: Boolean = false,
|
||||||
val autoTunnelActive: Boolean = false,
|
val requestPermissions: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,110 +1,123 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import com.wireguard.android.backend.GoBackend
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
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.service.tunnel.TunnelState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.flow.combine
|
import timber.log.Timber
|
||||||
import kotlinx.coroutines.flow.onCompletion
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.flow.takeWhile
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.plus
|
|
||||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class AppViewModel
|
class AppViewModel
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor() : ViewModel() {
|
||||||
private val appDataRepository: AppDataRepository,
|
val vpnIntent: Intent? = GoBackend.VpnService.prepare(WireGuardAutoTunnel.instance)
|
||||||
private val tunnelService: Provider<TunnelService>,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
private val serviceManager: ServiceManager,
|
|
||||||
) : ViewModel() {
|
|
||||||
|
|
||||||
val uiState =
|
private val _appUiState =
|
||||||
combine(
|
MutableStateFlow(
|
||||||
appDataRepository.settings.getSettingsFlow(),
|
|
||||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
|
||||||
tunnelService.get().vpnState,
|
|
||||||
appDataRepository.appState.generalStateFlow,
|
|
||||||
serviceManager.autoTunnelActive,
|
|
||||||
) { settings, tunnels, tunnelState, generalState, autoTunnel ->
|
|
||||||
AppUiState(
|
AppUiState(
|
||||||
settings,
|
vpnPermissionAccepted = vpnIntent == null,
|
||||||
tunnels,
|
),
|
||||||
tunnelState,
|
|
||||||
generalState,
|
|
||||||
autoTunnel,
|
|
||||||
)
|
|
||||||
}.stateIn(
|
|
||||||
viewModelScope + ioDispatcher,
|
|
||||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
|
||||||
AppUiState(),
|
|
||||||
)
|
)
|
||||||
|
val appUiState = _appUiState.asStateFlow()
|
||||||
|
|
||||||
private val _isAppReady = MutableStateFlow<Boolean>(false)
|
fun isRequiredPermissionGranted(): Boolean {
|
||||||
val isAppReady = _isAppReady.asStateFlow()
|
val allAccepted =
|
||||||
|
(_appUiState.value.vpnPermissionAccepted && _appUiState.value.vpnPermissionAccepted)
|
||||||
|
if (!allAccepted) requestPermissions()
|
||||||
|
return allAccepted
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
private fun requestPermissions() {
|
||||||
viewModelScope.launch {
|
_appUiState.update {
|
||||||
initPin()
|
it.copy(
|
||||||
initAutoTunnel()
|
requestPermissions = true,
|
||||||
initTunnel()
|
)
|
||||||
appReadyCheck()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun appReadyCheck() {
|
fun permissionsRequested() {
|
||||||
val tunnelCount = appDataRepository.tunnels.count()
|
_appUiState.update {
|
||||||
uiState.takeWhile { it.tunnels.size != tunnelCount }.onCompletion {
|
it.copy(
|
||||||
_isAppReady.emit(true)
|
requestPermissions = false,
|
||||||
}.collect()
|
)
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun initTunnel() {
|
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun initPin() {
|
fun openWebPage(url: String, context: Context) {
|
||||||
val isPinEnabled = appDataRepository.appState.isPinLockEnabled()
|
try {
|
||||||
if (isPinEnabled) PinManager.initialize(WireGuardAutoTunnel.instance)
|
val webpage: Uri = Uri.parse(url)
|
||||||
|
val intent =
|
||||||
|
Intent(Intent.ACTION_VIEW, webpage).apply {
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
}
|
||||||
|
context.startActivity(intent)
|
||||||
|
} catch (e: ActivityNotFoundException) {
|
||||||
|
Timber.e(e)
|
||||||
|
showSnackbarMessage(context.getString(R.string.no_browser_detected))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun initAutoTunnel() {
|
fun onVpnPermissionAccepted() {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
_appUiState.update {
|
||||||
if (settings.isAutoTunnelEnabled) serviceManager.startAutoTunnel(false)
|
it.copy(
|
||||||
|
vpnPermissionAccepted = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
fun launchEmail(context: Context) {
|
||||||
PinManager.clearPin()
|
try {
|
||||||
appDataRepository.appState.setPinLockEnabled(false)
|
val intent =
|
||||||
|
Intent(Intent.ACTION_SENDTO).apply {
|
||||||
|
type = Constants.EMAIL_MIME_TYPE
|
||||||
|
putExtra(Intent.EXTRA_EMAIL, arrayOf(context.getString(R.string.my_email)))
|
||||||
|
putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.email_subject))
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
}
|
||||||
|
context.startActivity(
|
||||||
|
Intent.createChooser(intent, context.getString(R.string.email_chooser)).apply {
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} catch (e: ActivityNotFoundException) {
|
||||||
|
Timber.e(e)
|
||||||
|
showSnackbarMessage(context.getString(R.string.no_email_detected))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPinLockEnabled() = viewModelScope.launch {
|
fun showSnackbarMessage(message: String) {
|
||||||
appDataRepository.appState.setPinLockEnabled(true)
|
_appUiState.update {
|
||||||
|
it.copy(
|
||||||
|
snackbarMessage = message,
|
||||||
|
snackbarMessageConsumed = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLocationDisclosureShown() = viewModelScope.launch {
|
fun snackbarMessageConsumed() {
|
||||||
appDataRepository.appState.setLocationDisclosureShown(true)
|
_appUiState.update {
|
||||||
|
it.copy(
|
||||||
|
snackbarMessage = "",
|
||||||
|
snackbarMessageConsumed = true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setNotificationPermissionAccepted(accepted: Boolean) {
|
||||||
|
_appUiState.update {
|
||||||
|
it.copy(
|
||||||
|
notificationPermissionAccepted = accepted,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,249 +1,327 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
import android.content.Context
|
import android.Manifest
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.activity.SystemBarStyle
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.foundation.focusable
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.animation.fadeOut
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.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.MaterialTheme
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.SnackbarData
|
import androidx.compose.material3.SnackbarData
|
||||||
|
import androidx.compose.material3.SnackbarDuration
|
||||||
import androidx.compose.material3.SnackbarHost
|
import androidx.compose.material3.SnackbarHost
|
||||||
|
import androidx.compose.material3.SnackbarHostState
|
||||||
|
import androidx.compose.material3.SnackbarResult
|
||||||
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.surfaceColorAtElevation
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
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.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.navigation.NavType
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import androidx.navigation.toRoute
|
import androidx.navigation.navArgument
|
||||||
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||||
|
import com.google.accompanist.permissions.isGranted
|
||||||
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
|
import com.google.accompanist.permissions.shouldShowRationale
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalFocusRequester
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen
|
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.main.MainScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.options.OptionsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.options.OptionsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.pinlock.PinLockScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.pinlock.PinLockScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.scanner.ScannerScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.AppearanceScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.display.DisplayScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.language.LanguageScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.AutoTunnelScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.LocationDisclosureScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import timber.log.Timber
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private val localeStorage: LocaleStorage by lazy {
|
|
||||||
(application as WireGuardAutoTunnel).localeStorage
|
|
||||||
}
|
|
||||||
|
|
||||||
private lateinit var oldPrefLocaleCode: String
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appStateRepository: AppStateRepository
|
lateinit var appStateRepository: AppStateRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: TunnelService
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
private val viewModel by viewModels<AppViewModel>()
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@OptIn(
|
||||||
|
ExperimentalPermissionsApi::class,
|
||||||
|
)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
installSplashScreen().apply {
|
val isPinLockEnabled = intent.extras?.getBoolean(SplashActivity.IS_PIN_LOCK_ENABLED_KEY)
|
||||||
setKeepOnScreenCondition {
|
|
||||||
!viewModel.isAppReady.value
|
enableEdgeToEdge(navigationBarStyle = SystemBarStyle.dark(Color.Transparent.toArgb()))
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
|
||||||
|
val settings = settingsRepository.getSettings()
|
||||||
|
if (settings.isAutoTunnelEnabled) {
|
||||||
|
serviceManager.startWatcherService(application.applicationContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val appUiState by viewModel.uiState.collectAsStateWithLifecycle(lifecycle = this.lifecycle)
|
val appViewModel = hiltViewModel<AppViewModel>()
|
||||||
|
val appUiState by appViewModel.appUiState.collectAsStateWithLifecycle()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
val rootItemFocusRequester = remember { FocusRequester() }
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
|
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
LaunchedEffect(appUiState.tunnels) {
|
val notificationPermissionState =
|
||||||
Timber.d("Updating launched")
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
requestTunnelTileServiceStateUpdate()
|
rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS)
|
||||||
}
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
LaunchedEffect(appUiState.autoTunnelActive) {
|
val snackbarHostState = remember { SnackbarHostState() }
|
||||||
requestAutoTunnelTileServiceUpdate()
|
|
||||||
}
|
|
||||||
|
|
||||||
with(appUiState.settings) {
|
val vpnActivityResultState =
|
||||||
LaunchedEffect(isAutoTunnelEnabled) {
|
rememberLauncherForActivityResult(
|
||||||
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
onResult = {
|
||||||
|
val accepted = (it.resultCode == RESULT_OK)
|
||||||
|
if (accepted) {
|
||||||
|
appViewModel.onVpnPermissionAccepted()
|
||||||
|
} else {
|
||||||
|
showVpnPermissionDialog = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CompositionLocalProvider(LocalFocusRequester provides rootItemFocusRequester) {
|
LaunchedEffect(appUiState.requestPermissions) {
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
if (appUiState.requestPermissions) {
|
||||||
SnackbarControllerProvider { host ->
|
appViewModel.permissionsRequested()
|
||||||
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
if (notificationPermissionState != null && !notificationPermissionState.status.isGranted
|
||||||
Scaffold(
|
) {
|
||||||
contentWindowInsets = WindowInsets(0.dp),
|
notificationPermissionState.launchPermissionRequest()
|
||||||
snackbarHost = {
|
return@LaunchedEffect if (notificationPermissionState.status.shouldShowRationale || !notificationPermissionState.status.isGranted) {
|
||||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
showSnackBarMessage(
|
||||||
CustomSnackBar(
|
StringValue.StringResource(R.string.notification_permission_required),
|
||||||
snackbarData.visuals.message,
|
)
|
||||||
isRtl = false,
|
} else {
|
||||||
containerColor =
|
Unit
|
||||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
}
|
||||||
2.dp,
|
}
|
||||||
),
|
if (!appUiState.vpnPermissionAccepted) {
|
||||||
)
|
return@LaunchedEffect appViewModel.vpnIntent?.let {
|
||||||
}
|
vpnActivityResultState.launch(
|
||||||
},
|
it,
|
||||||
bottomBar = {
|
)
|
||||||
BottomNavBar(
|
} ?: Unit
|
||||||
navController,
|
}
|
||||||
listOf(
|
}
|
||||||
BottomNavItem(
|
}
|
||||||
name = stringResource(R.string.tunnels),
|
|
||||||
route = Route.Main,
|
WireguardAutoTunnelTheme {
|
||||||
icon = Icons.Rounded.Home,
|
LaunchedEffect(Unit) {
|
||||||
),
|
appViewModel.setNotificationPermissionAccepted(
|
||||||
BottomNavItem(
|
notificationPermissionState?.status?.isGranted ?: true,
|
||||||
name = stringResource(R.string.settings),
|
)
|
||||||
route = Route.Settings,
|
}
|
||||||
icon = Icons.Rounded.Settings,
|
|
||||||
),
|
LaunchedEffect(appUiState.snackbarMessageConsumed) {
|
||||||
BottomNavItem(
|
if (!appUiState.snackbarMessageConsumed) {
|
||||||
name = stringResource(R.string.support),
|
showSnackBarMessage(StringValue.DynamicString(appUiState.snackbarMessage))
|
||||||
route = Route.Support,
|
appViewModel.snackbarMessageConsumed()
|
||||||
icon = Icons.Rounded.QuestionMark,
|
}
|
||||||
),
|
}
|
||||||
),
|
|
||||||
)
|
val focusRequester = remember { FocusRequester() }
|
||||||
},
|
|
||||||
) {
|
if (showVpnPermissionDialog) {
|
||||||
Box(modifier = Modifier.fillMaxSize().padding(it)) {
|
InfoDialog(
|
||||||
NavHost(
|
onDismiss = { showVpnPermissionDialog = false },
|
||||||
navController,
|
onAttest = { showVpnPermissionDialog = false },
|
||||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
title = { Text(text = stringResource(R.string.vpn_denied_dialog_title)) },
|
||||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
body = {
|
||||||
startDestination = (if (appUiState.generalState.isPinLockEnabled == true) Route.Lock else Route.Main),
|
Column(verticalArrangement = Arrangement.spacedBy(15.dp)) {
|
||||||
) {
|
Text(text = stringResource(R.string.vpn_denied_dialog_message))
|
||||||
composable<Route.Main> {
|
Text(text = stringResource(R.string.vpn_denied_dialog_message2))
|
||||||
MainScreen(
|
|
||||||
uiState = appUiState,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.Settings> {
|
|
||||||
SettingsScreen(
|
|
||||||
appViewModel = viewModel,
|
|
||||||
uiState = appUiState,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.LocationDisclosure> {
|
|
||||||
LocationDisclosureScreen(viewModel, appUiState)
|
|
||||||
}
|
|
||||||
composable<Route.AutoTunnel> {
|
|
||||||
AutoTunnelScreen(
|
|
||||||
appUiState,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.Appearance> {
|
|
||||||
AppearanceScreen()
|
|
||||||
}
|
|
||||||
composable<Route.Language> {
|
|
||||||
LanguageScreen(localeStorage)
|
|
||||||
}
|
|
||||||
composable<Route.Display> {
|
|
||||||
DisplayScreen(appUiState)
|
|
||||||
}
|
|
||||||
composable<Route.Support> {
|
|
||||||
SupportScreen()
|
|
||||||
}
|
|
||||||
composable<Route.Logs> {
|
|
||||||
LogsScreen()
|
|
||||||
}
|
|
||||||
composable<Route.Config> {
|
|
||||||
val args = it.toRoute<Route.Config>()
|
|
||||||
ConfigScreen(
|
|
||||||
tunnelId = args.id,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.Option> {
|
|
||||||
val args = it.toRoute<Route.Option>()
|
|
||||||
OptionsScreen(
|
|
||||||
tunnelId = args.id,
|
|
||||||
appUiState = appUiState,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.Lock> {
|
|
||||||
PinLockScreen(
|
|
||||||
appViewModel = viewModel,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
composable<Route.Scanner> {
|
|
||||||
ScannerScreen()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
confirmText = { Text(text = stringResource(R.string.okay)) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Scaffold(
|
||||||
|
snackbarHost = {
|
||||||
|
SnackbarHost(snackbarHostState) { snackbarData: SnackbarData ->
|
||||||
|
CustomSnackBar(
|
||||||
|
snackbarData.visuals.message,
|
||||||
|
isRtl = false,
|
||||||
|
containerColor =
|
||||||
|
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||||
|
2.dp,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// TODO refactor
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.focusable()
|
||||||
|
.focusProperties {
|
||||||
|
when (navBackStackEntry?.destination?.route) {
|
||||||
|
Screen.Lock.route -> Unit
|
||||||
|
else -> up = focusRequester
|
||||||
|
}
|
||||||
|
},
|
||||||
|
bottomBar = {
|
||||||
|
BottomNavBar(
|
||||||
|
navController,
|
||||||
|
listOf(
|
||||||
|
Screen.Main.navItem,
|
||||||
|
Screen.Settings.navItem,
|
||||||
|
Screen.Support.navItem,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
) { padding ->
|
||||||
|
NavHost(
|
||||||
|
navController,
|
||||||
|
startDestination = (if (isPinLockEnabled == true) Screen.Lock.route else Screen.Main.route),
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(padding)
|
||||||
|
.fillMaxSize(),
|
||||||
|
) {
|
||||||
|
composable(
|
||||||
|
Screen.Main.route,
|
||||||
|
) {
|
||||||
|
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,
|
||||||
|
appViewModel = appViewModel,
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
if (!id.isNullOrBlank()) {
|
||||||
|
ConfigScreen(
|
||||||
|
navController = navController,
|
||||||
|
tunnelId = id,
|
||||||
|
appViewModel = appViewModel,
|
||||||
|
focusRequester = focusRequester,
|
||||||
|
configType = configType,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
composable("${Screen.Option.route}/{id}") {
|
||||||
|
val id = it.arguments?.getString("id")
|
||||||
|
if (!id.isNullOrBlank()) {
|
||||||
|
OptionsScreen(
|
||||||
|
navController = navController,
|
||||||
|
tunnelId = id,
|
||||||
|
appViewModel = appViewModel,
|
||||||
|
focusRequester = focusRequester,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
composable(Screen.Lock.route) {
|
||||||
|
PinLockScreen(
|
||||||
|
navController = navController,
|
||||||
|
appViewModel = appViewModel,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun attachBaseContext(newBase: Context) {
|
|
||||||
oldPrefLocaleCode = LocaleStorage(newBase).getPreferredLocale()
|
|
||||||
applyOverrideConfiguration(LocaleUtil.getLocalizedConfiguration(oldPrefLocaleCode))
|
|
||||||
super.attachBaseContext(newBase)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
val currentLocaleCode = LocaleStorage(this).getPreferredLocale()
|
|
||||||
if (oldPrefLocaleCode != currentLocaleCode) {
|
|
||||||
recreate() // locale is changed, restart the activity to update
|
|
||||||
oldPrefLocaleCode = currentLocaleCode
|
|
||||||
}
|
|
||||||
super.onResume()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
tunnelService.cancelStatsJob()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +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 AutoTunnel : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object LocationDisclosure : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Appearance : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Display : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Language : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Main : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class Option(
|
|
||||||
val id: Int,
|
|
||||||
) : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Lock : Route()
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data object Scanner : 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")
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.lifecycle.repeatOnLifecycle
|
||||||
|
import com.zaneschepke.logcatter.LocalLogCollector
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel.Companion.isRunningOnAndroidTv
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@SuppressLint("CustomSplashScreen")
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class SplashActivity : ComponentActivity() {
|
||||||
|
@Inject
|
||||||
|
lateinit var appStateRepository: AppStateRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var localLogCollector: LocalLogCollector
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@ApplicationScope
|
||||||
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
val splashScreen = installSplashScreen()
|
||||||
|
splashScreen.setKeepOnScreenCondition { true }
|
||||||
|
}
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
applicationScope.launch {
|
||||||
|
if (!isRunningOnAndroidTv()) localLogCollector.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
repeatOnLifecycle(Lifecycle.State.CREATED) {
|
||||||
|
val pinLockEnabled = appStateRepository.isPinLockEnabled()
|
||||||
|
if (pinLockEnabled) {
|
||||||
|
PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
val intent =
|
||||||
|
Intent(this@SplashActivity, MainActivity::class.java).apply {
|
||||||
|
putExtra(IS_PIN_LOCK_ENABLED_KEY, pinLockEnabled)
|
||||||
|
}
|
||||||
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val IS_PIN_LOCK_ENABLED_KEY = "is_pin_lock_enabled"
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-3
@@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Spacer
|
|||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -13,12 +12,12 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
|
||||||
@Composable
|
@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(
|
TextButton(
|
||||||
onClick = onClick,
|
onClick = onClick,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
) {
|
) {
|
||||||
Text(text, Modifier.weight(1f, false), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.primary)
|
Text(text, Modifier.weight(1f, false))
|
||||||
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = icon,
|
imageVector = icon,
|
||||||
|
|||||||
-65
@@ -1,65 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
|
||||||
|
|
||||||
import androidx.compose.animation.animateContentSize
|
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
|
||||||
import androidx.compose.foundation.combinedClickable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
|
||||||
@Composable
|
|
||||||
fun ExpandingRowListItem(
|
|
||||||
leading: @Composable () -> Unit,
|
|
||||||
text: String,
|
|
||||||
onHold: () -> Unit = {},
|
|
||||||
onClick: () -> Unit,
|
|
||||||
trailing: @Composable () -> Unit,
|
|
||||||
isExpanded: Boolean,
|
|
||||||
expanded: @Composable () -> Unit = {},
|
|
||||||
) {
|
|
||||||
Box(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.animateContentSize()
|
|
||||||
.clip(RoundedCornerShape(30.dp))
|
|
||||||
.combinedClickable(
|
|
||||||
onClick = { onClick() },
|
|
||||||
onLongClick = { onHold() },
|
|
||||||
),
|
|
||||||
) {
|
|
||||||
Column {
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 15.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(15.dp),
|
|
||||||
modifier = Modifier.fillMaxWidth(13 / 20f),
|
|
||||||
) {
|
|
||||||
leading()
|
|
||||||
Text(text, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.labelLarge)
|
|
||||||
}
|
|
||||||
trailing()
|
|
||||||
}
|
|
||||||
if (isExpanded) expanded()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
|
||||||
|
|
||||||
import androidx.compose.ui.geometry.Offset
|
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
|
||||||
|
|
||||||
class NestedScrollListener(val onUp: () -> Unit, val onDown: () -> Unit) : NestedScrollConnection {
|
|
||||||
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
|
||||||
if (available.y < -1) onDown()
|
|
||||||
if (available.y > 1) onUp()
|
|
||||||
return Offset.Zero
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.animation.animateContentSize
|
||||||
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
|
import androidx.compose.foundation.combinedClickable
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
|
import androidx.compose.ui.focus.focusRequester
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.unit.sp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.toThreeDecimalPlaceString
|
||||||
|
|
||||||
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
|
@Composable
|
||||||
|
fun RowListItem(
|
||||||
|
icon: @Composable () -> Unit,
|
||||||
|
text: String,
|
||||||
|
onHold: () -> Unit,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
rowButton: @Composable () -> Unit,
|
||||||
|
expanded: Boolean,
|
||||||
|
statistics: TunnelStatistics?,
|
||||||
|
focusRequester: FocusRequester,
|
||||||
|
) {
|
||||||
|
Box(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.focusRequester(focusRequester)
|
||||||
|
.animateContentSize()
|
||||||
|
.clip(RoundedCornerShape(30.dp))
|
||||||
|
.combinedClickable(
|
||||||
|
onClick = { onClick() },
|
||||||
|
onLongClick = { onHold() },
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
Column {
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(horizontal = 15.dp, vertical = 5.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth(13 / 20f),
|
||||||
|
) {
|
||||||
|
icon()
|
||||||
|
Text(text, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||||
|
}
|
||||||
|
rowButton()
|
||||||
|
}
|
||||||
|
if (expanded) {
|
||||||
|
statistics?.getPeers()?.forEach {
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(end = 10.dp, bottom = 10.dp, start = 10.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
|
) {
|
||||||
|
// TODO change these to string resources
|
||||||
|
val handshakeEpoch = statistics.peerStats(it)!!.latestHandshakeEpochMillis
|
||||||
|
val peerTx = statistics.peerStats(it)!!.txBytes
|
||||||
|
val peerRx = statistics.peerStats(it)!!.rxBytes
|
||||||
|
val peerId = it.toBase64().subSequence(0, 3).toString() + "***"
|
||||||
|
val handshakeSec =
|
||||||
|
NumberUtils.getSecondsBetweenTimestampAndNow(handshakeEpoch)
|
||||||
|
val handshake =
|
||||||
|
if (handshakeSec == null) "never" else "$handshakeSec secs ago"
|
||||||
|
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
||||||
|
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
||||||
|
val fontSize = 9.sp
|
||||||
|
Text("peer: $peerId", fontSize = fontSize)
|
||||||
|
Text("handshake: $handshake", fontSize = fontSize)
|
||||||
|
Text("tx: $peerTxMB MB", fontSize = fontSize)
|
||||||
|
Text("rx: $peerRxMB MB", fontSize = fontSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SelectedLabel() {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
horizontalArrangement = Arrangement.End,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.selected),
|
|
||||||
modifier =
|
|
||||||
Modifier.padding(
|
|
||||||
horizontal = 24.dp.scaledWidth(),
|
|
||||||
vertical = 16.dp.scaledHeight(),
|
|
||||||
),
|
|
||||||
color =
|
|
||||||
MaterialTheme.colorScheme.onSurfaceVariant,
|
|
||||||
style = MaterialTheme.typography.labelSmall,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-100
@@ -1,100 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
|
||||||
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.height
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.Card
|
|
||||||
import androidx.compose.material3.CardDefaults
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
import kotlin.let
|
|
||||||
|
|
||||||
@androidx.compose.runtime.Composable
|
|
||||||
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) {
|
|
||||||
val border: BorderStroke? =
|
|
||||||
if (selected) {
|
|
||||||
BorderStroke(
|
|
||||||
1.dp,
|
|
||||||
MaterialTheme.colorScheme.primary,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
Card(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.height(IntrinsicSize.Min),
|
|
||||||
shape = RoundedCornerShape(8.dp),
|
|
||||||
border = border,
|
|
||||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
|
||||||
) {
|
|
||||||
Box(
|
|
||||||
modifier = Modifier.clickable { onClick() }
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(horizontal = 8.dp.scaledWidth(), vertical = 10.dp.scaledHeight())
|
|
||||||
.padding(end = 16.dp.scaledWidth()).padding(start = 8.dp.scaledWidth())
|
|
||||||
.fillMaxSize(),
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(16.dp.scaledWidth()),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(
|
|
||||||
16.dp.scaledWidth(),
|
|
||||||
),
|
|
||||||
verticalAlignment = Alignment.Companion.CenterVertically,
|
|
||||||
modifier = Modifier.padding(vertical = if (description == null) 10.dp.scaledHeight() else 0.dp),
|
|
||||||
) {
|
|
||||||
leadingIcon?.let {
|
|
||||||
Icon(
|
|
||||||
leadingIcon,
|
|
||||||
leadingIcon.name,
|
|
||||||
Modifier.size(iconSize),
|
|
||||||
if (selected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Column {
|
|
||||||
Text(
|
|
||||||
title,
|
|
||||||
style = MaterialTheme.typography.titleMedium,
|
|
||||||
)
|
|
||||||
description?.let {
|
|
||||||
Text(
|
|
||||||
description,
|
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
||||||
style = MaterialTheme.typography.bodyMedium,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
|
||||||
|
|
||||||
import androidx.compose.material3.Switch
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.scale
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled: Boolean = true, modifier: Modifier = Modifier) {
|
|
||||||
Switch(
|
|
||||||
checked,
|
|
||||||
{ onClick(it) },
|
|
||||||
modifier.scale((52.dp.scaledHeight() / 52.dp)),
|
|
||||||
enabled = enabled,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
-63
@@ -1,63 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.height
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.Card
|
|
||||||
import androidx.compose.material3.CardDefaults
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.ripple
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SelectionItemButton(
|
|
||||||
leading: (@Composable () -> Unit)? = null,
|
|
||||||
buttonText: String,
|
|
||||||
trailing: (@Composable () -> Unit)? = null,
|
|
||||||
onClick: () -> Unit,
|
|
||||||
ripple: Boolean = true,
|
|
||||||
) {
|
|
||||||
Card(
|
|
||||||
modifier =
|
|
||||||
Modifier.clip(RoundedCornerShape(8.dp))
|
|
||||||
.clickable(
|
|
||||||
indication = if (ripple) ripple() else null,
|
|
||||||
interactionSource = remember { MutableInteractionSource() },
|
|
||||||
onClick = { onClick() },
|
|
||||||
)
|
|
||||||
.height(56.dp.scaledHeight()),
|
|
||||||
colors =
|
|
||||||
CardDefaults.cardColors(
|
|
||||||
containerColor = MaterialTheme.colorScheme.background,
|
|
||||||
),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Start,
|
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
) {
|
|
||||||
leading?.let {
|
|
||||||
it()
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
buttonText,
|
|
||||||
style = MaterialTheme.typography.bodyLarge,
|
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
|
||||||
)
|
|
||||||
trailing?.let {
|
|
||||||
it()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
|
||||||
|
|
||||||
data class SelectionItem(
|
|
||||||
val leadingIcon: ImageVector? = null,
|
|
||||||
val trailing: (@Composable () -> Unit)? = null,
|
|
||||||
val title: (@Composable () -> Unit),
|
|
||||||
val description: (@Composable () -> Unit)? = null,
|
|
||||||
val onClick: (() -> Unit)? = null,
|
|
||||||
val height: Int = 64,
|
|
||||||
)
|
|
||||||
-86
@@ -1,86 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.Card
|
|
||||||
import androidx.compose.material3.CardDefaults
|
|
||||||
import androidx.compose.material3.HorizontalDivider
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|
||||||
Card(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
shape = RoundedCornerShape(8.dp),
|
|
||||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
|
||||||
) {
|
|
||||||
items.mapIndexed { index, item ->
|
|
||||||
Box(
|
|
||||||
contentAlignment = Alignment.Center,
|
|
||||||
modifier = Modifier
|
|
||||||
.then(item.onClick?.let { Modifier.clickable { it() } } ?: Modifier)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(start = 16.dp.scaledWidth())
|
|
||||||
.weight(4f, false)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
item.leadingIcon?.let { icon ->
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(start = if (item.leadingIcon != null) 16.dp.scaledWidth() else 0.dp)
|
|
||||||
.padding(vertical = if (item.description == null) 16.dp.scaledHeight() else 6.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
item.title()
|
|
||||||
item.description?.let {
|
|
||||||
it()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
item.trailing?.let {
|
|
||||||
Box(
|
|
||||||
contentAlignment = Alignment.CenterEnd,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(end = 24.dp.scaledWidth(), start = 16.dp.scaledWidth())
|
|
||||||
.weight(1f),
|
|
||||||
) {
|
|
||||||
it()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+6
-13
@@ -1,6 +1,5 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||||
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
@@ -15,29 +14,23 @@ fun ConfigurationTextBox(
|
|||||||
value: String,
|
value: String,
|
||||||
hint: String,
|
hint: String,
|
||||||
onValueChange: (String) -> Unit,
|
onValueChange: (String) -> Unit,
|
||||||
keyboardActions: KeyboardActions = KeyboardActions(),
|
keyboardActions: KeyboardActions,
|
||||||
label: String,
|
label: String,
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
isError: Boolean = false,
|
|
||||||
keyboardOptions: KeyboardOptions = KeyboardOptions(
|
|
||||||
capitalization = KeyboardCapitalization.None,
|
|
||||||
imeAction = ImeAction.Done,
|
|
||||||
),
|
|
||||||
trailing: @Composable () -> Unit = {},
|
|
||||||
interactionSource: MutableInteractionSource? = null,
|
|
||||||
) {
|
) {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
isError = isError,
|
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
value = value,
|
value = value,
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
interactionSource = interactionSource,
|
|
||||||
onValueChange = { onValueChange(it) },
|
onValueChange = { onValueChange(it) },
|
||||||
label = { Text(label) },
|
label = { Text(label) },
|
||||||
maxLines = 1,
|
maxLines = 1,
|
||||||
placeholder = { Text(hint) },
|
placeholder = { Text(hint) },
|
||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions =
|
||||||
|
KeyboardOptions(
|
||||||
|
capitalization = KeyboardCapitalization.None,
|
||||||
|
imeAction = ImeAction.Done,
|
||||||
|
),
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
trailingIcon = trailing,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-11
@@ -3,33 +3,28 @@ package com.zaneschepke.wireguardautotunnel.ui.common.config
|
|||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.Switch
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfigurationToggle(
|
fun ConfigurationToggle(label: String, enabled: Boolean, checked: Boolean, padding: Dp, onCheckChanged: () -> Unit, modifier: Modifier = Modifier) {
|
||||||
label: String,
|
|
||||||
enabled: Boolean = true,
|
|
||||||
checked: Boolean,
|
|
||||||
onCheckChanged: (checked: Boolean) -> Unit,
|
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
) {
|
|
||||||
Row(
|
Row(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.padding(padding),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
label,
|
label,
|
||||||
textAlign = TextAlign.Start,
|
textAlign = TextAlign.Start,
|
||||||
style = MaterialTheme.typography.labelLarge,
|
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.weight(
|
.weight(
|
||||||
@@ -42,7 +37,7 @@ fun ConfigurationToggle(
|
|||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
checked = checked,
|
checked = checked,
|
||||||
onCheckedChange = { onCheckChanged(it) },
|
onCheckedChange = { onCheckChanged() },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-81
@@ -1,81 +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.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,
|
|
||||||
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(),
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
-60
@@ -1,60 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.functions
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import androidx.activity.compose.ManagedActivityResultLauncher
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun rememberFileImportLauncherForResult(onNoFileExplorer: () -> Unit, onData: (data: Uri) -> Unit): ManagedActivityResultLauncher<String, Uri?> {
|
|
||||||
return rememberLauncherForActivityResult(
|
|
||||||
object : ActivityResultContracts.GetContent() {
|
|
||||||
override fun createIntent(context: Context, input: String): Intent {
|
|
||||||
val intent = super.createIntent(context, input).apply {
|
|
||||||
type = if (context.isRunningOnTv()) {
|
|
||||||
Constants.ALLOWED_TV_FILE_TYPES
|
|
||||||
} else {
|
|
||||||
Constants.ALL_FILE_TYPES
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than
|
|
||||||
* what we can do, so detect this and throw an exception that we can catch later. */
|
|
||||||
val activitiesToResolveIntent =
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
context.packageManager.queryIntentActivities(
|
|
||||||
intent,
|
|
||||||
PackageManager.ResolveInfoFlags.of(
|
|
||||||
PackageManager.MATCH_DEFAULT_ONLY.toLong(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
context.packageManager.queryIntentActivities(
|
|
||||||
intent,
|
|
||||||
PackageManager.MATCH_DEFAULT_ONLY,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
activitiesToResolveIntent.all {
|
|
||||||
val name = it.activityInfo.packageName
|
|
||||||
name.startsWith(Constants.GOOGLE_TV_EXPLORER_STUB) ||
|
|
||||||
name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB)
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
onNoFileExplorer()
|
|
||||||
}
|
|
||||||
return intent
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) { data ->
|
|
||||||
if (data == null) return@rememberLauncherForActivityResult
|
|
||||||
onData(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.label
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun GroupLabel(title: String) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Start,
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
title,
|
|
||||||
style = MaterialTheme.typography.titleMedium,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.label
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.AnnotatedString
|
|
||||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun VersionLabel() {
|
|
||||||
val clipboardManager = LocalClipboardManager.current
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Start,
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
"${stringResource(R.string.version)}: ${BuildConfig.VERSION_NAME}",
|
|
||||||
style = MaterialTheme.typography.labelMedium,
|
|
||||||
color = MaterialTheme.colorScheme.outline,
|
|
||||||
modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(BuildConfig.VERSION_NAME))
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+20
-25
@@ -10,45 +10,40 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavItem>) {
|
fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavItem>) {
|
||||||
|
val backStackEntry = navController.currentBackStackEntryAsState()
|
||||||
|
|
||||||
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
|
|
||||||
showBottomBar = bottomNavItems.any {
|
// TODO find a better way to hide nav bar
|
||||||
navBackStackEntry?.isCurrentRoute(it.route::class) == true
|
showBottomBar =
|
||||||
}
|
when (navBackStackEntry?.destination?.route) {
|
||||||
|
Screen.Lock.route -> false
|
||||||
|
else -> true
|
||||||
|
}
|
||||||
|
|
||||||
|
NavigationBar(
|
||||||
|
containerColor = if (!showBottomBar) Color.Transparent else MaterialTheme.colorScheme.background,
|
||||||
|
) {
|
||||||
|
if (showBottomBar) {
|
||||||
|
bottomNavItems.forEach { item ->
|
||||||
|
val selected = item.route == backStackEntry.value?.destination?.route
|
||||||
|
|
||||||
if (showBottomBar) {
|
|
||||||
NavigationBar(
|
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
|
||||||
) {
|
|
||||||
bottomNavItems.forEachIndexed { index, item ->
|
|
||||||
val selected = navBackStackEntry.isCurrentRoute(item.route::class)
|
|
||||||
NavigationBarItem(
|
NavigationBarItem(
|
||||||
selected = selected,
|
selected = selected,
|
||||||
onClick = {
|
onClick = { navController.navigate(item.route) },
|
||||||
if (selected) return@NavigationBarItem
|
|
||||||
navController.navigate(item.route) {
|
|
||||||
// Pop up to the start destination of the graph to
|
|
||||||
// avoid building up a large stack of destinations
|
|
||||||
// on the back stack as users select items
|
|
||||||
popUpTo(navController.graph.findStartDestination().id) {
|
|
||||||
saveState = true
|
|
||||||
}
|
|
||||||
// Avoid multiple copies of the same destination when
|
|
||||||
// reselecting the same item
|
|
||||||
launchSingleTop = true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = item.name,
|
text = item.name,
|
||||||
style = MaterialTheme.typography.labelMedium,
|
fontWeight = FontWeight.SemiBold,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
icon = {
|
icon = {
|
||||||
|
|||||||
+1
-2
@@ -1,10 +1,9 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
|
|
||||||
data class BottomNavItem(
|
data class BottomNavItem(
|
||||||
val name: String,
|
val name: String,
|
||||||
val route: Route,
|
val route: String,
|
||||||
val icon: ImageVector,
|
val icon: ImageVector,
|
||||||
)
|
)
|
||||||
|
|||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import androidx.navigation.NavBackStackEntry
|
|
||||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
|
||||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
|
||||||
fun <T : Route> NavBackStackEntry?.isCurrentRoute(cls: KClass<T>): Boolean {
|
|
||||||
return this?.destination?.hierarchy?.any {
|
|
||||||
it.hasRoute(route = cls)
|
|
||||||
} == true
|
|
||||||
}
|
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import androidx.compose.runtime.compositionLocalOf
|
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
|
|
||||||
val LocalNavController = compositionLocalOf<NavHostController> {
|
|
||||||
error("NavController was not provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
val LocalFocusRequester = compositionLocalOf<FocusRequester> { error("FocusRequester is not provided") }
|
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
|
|
||||||
import androidx.compose.material3.CenterAlignedTopAppBar
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun TopNavBar(title: String, trailing: @Composable () -> Unit = {}, showBack: Boolean = true) {
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
CenterAlignedTopAppBar(
|
|
||||||
title = {
|
|
||||||
Text(title)
|
|
||||||
},
|
|
||||||
navigationIcon = {
|
|
||||||
if (showBack) {
|
|
||||||
IconButton(onClick = { navController.popBackStack() }) {
|
|
||||||
val icon = Icons.AutoMirrored.Outlined.ArrowBack
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actions = {
|
|
||||||
trailing()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
+5
-7
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.snackbar
|
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
@@ -19,21 +19,19 @@ import androidx.compose.runtime.CompositionLocalProvider
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||||
import androidx.compose.ui.unit.LayoutDirection
|
import androidx.compose.ui.unit.LayoutDirection
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CustomSnackBar(message: String, isRtl: Boolean = true, containerColor: Color = MaterialTheme.colorScheme.surface) {
|
fun CustomSnackBar(message: String, isRtl: Boolean = true, containerColor: Color = MaterialTheme.colorScheme.surface) {
|
||||||
val context = LocalContext.current
|
|
||||||
Snackbar(
|
Snackbar(
|
||||||
containerColor = containerColor,
|
containerColor = containerColor,
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(
|
.fillMaxWidth(
|
||||||
if (context.isRunningOnTv()) 1 / 3f else 2 / 3f,
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) 1 / 3f else 2 / 3f,
|
||||||
)
|
)
|
||||||
.padding(bottom = 100.dp),
|
.padding(bottom = 100.dp),
|
||||||
shape = RoundedCornerShape(16.dp),
|
shape = RoundedCornerShape(16.dp),
|
||||||
@@ -53,10 +51,10 @@ fun CustomSnackBar(message: String, isRtl: Boolean = true, containerColor: Color
|
|||||||
Icon(
|
Icon(
|
||||||
icon,
|
icon,
|
||||||
contentDescription = icon.name,
|
contentDescription = icon.name,
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
tint = Color.White,
|
||||||
modifier = Modifier.padding(end = 10.dp),
|
modifier = Modifier.padding(end = 10.dp),
|
||||||
)
|
)
|
||||||
Text(message, color = MaterialTheme.colorScheme.onSurface, modifier = Modifier.padding(end = 5.dp))
|
Text(message, color = Color.White, modifier = Modifier.padding(end = 5.dp))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.screen
|
||||||
|
|
||||||
|
import androidx.compose.foundation.focusable
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun LoadingScreen() {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.focusable()
|
||||||
|
.padding(),
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.padding(120.dp)) { CircularProgressIndicator() }
|
||||||
|
}
|
||||||
|
}
|
||||||
-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)
|
|
||||||
+4
-2
@@ -1,20 +1,22 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.text
|
package com.zaneschepke.wireguardautotunnel.ui.common.text
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.text.TextStyle
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.unit.Dp
|
import androidx.compose.ui.unit.Dp
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.unit.sp
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SectionTitle(title: String, padding: Dp) {
|
fun SectionTitle(title: String, padding: Dp) {
|
||||||
Text(
|
Text(
|
||||||
title,
|
title,
|
||||||
textAlign = TextAlign.Start,
|
textAlign = TextAlign.Start,
|
||||||
style = MaterialTheme.typography.titleMedium,
|
style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.ExtraBold),
|
||||||
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
|
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
-110
@@ -1,110 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.textbox
|
|
||||||
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.text.BasicTextField
|
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.OutlinedTextFieldDefaults
|
|
||||||
import androidx.compose.material3.TextFieldDefaults
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.graphics.SolidColor
|
|
||||||
import androidx.compose.ui.text.TextStyle
|
|
||||||
import androidx.compose.ui.text.input.VisualTransformation
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun CustomTextField(
|
|
||||||
value: String,
|
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
textStyle: TextStyle = MaterialTheme.typography.bodyLarge.copy(color = MaterialTheme.colorScheme.onSurface),
|
|
||||||
label: @Composable () -> Unit,
|
|
||||||
containerColor: Color,
|
|
||||||
onValueChange: (value: String) -> Unit = {},
|
|
||||||
singleLine: Boolean = false,
|
|
||||||
placeholder: @Composable (() -> Unit)? = null,
|
|
||||||
keyboardOptions: KeyboardOptions,
|
|
||||||
keyboardActions: KeyboardActions,
|
|
||||||
supportingText: @Composable (() -> Unit)? = null,
|
|
||||||
leading: @Composable (() -> Unit)? = null,
|
|
||||||
trailing: @Composable (() -> Unit)? = null,
|
|
||||||
isError: Boolean = false,
|
|
||||||
readOnly: Boolean = false,
|
|
||||||
enabled: Boolean = true,
|
|
||||||
) {
|
|
||||||
val interactionSource = remember { MutableInteractionSource() }
|
|
||||||
val space = " "
|
|
||||||
BasicTextField(
|
|
||||||
value = value,
|
|
||||||
textStyle = textStyle,
|
|
||||||
onValueChange = {
|
|
||||||
onValueChange(it)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
readOnly = readOnly,
|
|
||||||
cursorBrush = SolidColor(MaterialTheme.colorScheme.onSurface),
|
|
||||||
modifier = modifier,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
enabled = enabled,
|
|
||||||
singleLine = singleLine,
|
|
||||||
) {
|
|
||||||
OutlinedTextFieldDefaults.DecorationBox(
|
|
||||||
value = space + value,
|
|
||||||
innerTextField = {
|
|
||||||
if (value.isEmpty()) {
|
|
||||||
if (placeholder != null) {
|
|
||||||
placeholder()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
it.invoke()
|
|
||||||
},
|
|
||||||
contentPadding = OutlinedTextFieldDefaults.contentPadding(top = 0.dp, bottom = 0.dp),
|
|
||||||
leadingIcon = leading,
|
|
||||||
trailingIcon = trailing,
|
|
||||||
singleLine = singleLine,
|
|
||||||
supportingText = supportingText,
|
|
||||||
colors = TextFieldDefaults.colors().copy(
|
|
||||||
disabledLabelColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
disabledContainerColor = containerColor,
|
|
||||||
focusedLabelColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
focusedContainerColor = containerColor,
|
|
||||||
unfocusedContainerColor = containerColor,
|
|
||||||
focusedTextColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
cursorColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
),
|
|
||||||
enabled = enabled,
|
|
||||||
label = label,
|
|
||||||
visualTransformation = VisualTransformation.None,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
placeholder = placeholder,
|
|
||||||
container = {
|
|
||||||
OutlinedTextFieldDefaults.ContainerBox(
|
|
||||||
enabled,
|
|
||||||
isError = isError,
|
|
||||||
interactionSource,
|
|
||||||
colors = TextFieldDefaults.colors().copy(
|
|
||||||
errorContainerColor = containerColor,
|
|
||||||
disabledLabelColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
disabledContainerColor = containerColor,
|
|
||||||
focusedIndicatorColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
focusedLabelColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
focusedContainerColor = containerColor,
|
|
||||||
unfocusedContainerColor = containerColor,
|
|
||||||
focusedTextColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
cursorColor = MaterialTheme.colorScheme.onSurface,
|
|
||||||
),
|
|
||||||
shape = RoundedCornerShape(8.dp),
|
|
||||||
focusedBorderThickness = 0.5.dp,
|
|
||||||
unfocusedBorderThickness = 0.5.dp,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+313
-96
@@ -1,26 +1,36 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
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.clickable
|
||||||
import androidx.compose.foundation.focusGroup
|
import androidx.compose.foundation.focusGroup
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.layout.width
|
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.rememberScrollState
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.icons.Icons
|
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.ContentCopy
|
||||||
import androidx.compose.material.icons.rounded.Delete
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
import androidx.compose.material.icons.rounded.Refresh
|
import androidx.compose.material.icons.rounded.Refresh
|
||||||
import androidx.compose.material.icons.rounded.Save
|
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.FabPosition
|
||||||
import androidx.compose.material3.FloatingActionButton
|
import androidx.compose.material3.FloatingActionButton
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
@@ -29,11 +39,12 @@ import androidx.compose.material3.MaterialTheme
|
|||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.derivedStateOf
|
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
@@ -42,6 +53,8 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.compose.ui.focus.focusRequester
|
import androidx.compose.ui.focus.focusRequester
|
||||||
|
import androidx.compose.ui.focus.onFocusChanged
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.ClipboardManager
|
import androidx.compose.ui.platform.ClipboardManager
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
import androidx.compose.ui.platform.LocalClipboardManager
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
@@ -55,59 +68,60 @@ import androidx.compose.ui.text.input.VisualTransformation
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import com.google.accompanist.drawablepainter.DrawablePainter
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
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.ConfigurationTextBox
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
import com.zaneschepke.wireguardautotunnel.ui.common.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.common.text.SectionTitle
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.components.ApplicationSelectionDialog
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.getMessage
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
|
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||||
|
@OptIn(
|
||||||
|
ExperimentalMaterial3Api::class,
|
||||||
|
)
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfigScreen(tunnelId: Int) {
|
fun ConfigScreen(
|
||||||
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
viewModel: ConfigViewModel = hiltViewModel(),
|
||||||
factory.create(tunnelId)
|
focusRequester: FocusRequester,
|
||||||
}
|
navController: NavController,
|
||||||
|
appViewModel: AppViewModel,
|
||||||
|
tunnelId: String,
|
||||||
|
configType: ConfigType,
|
||||||
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
var showApplicationsDialog by remember { mutableStateOf(false) }
|
var showApplicationsDialog by remember { mutableStateOf(false) }
|
||||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||||
var isAuthenticated by remember { mutableStateOf(false) }
|
var isAuthenticated by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
var configType by remember { mutableStateOf<ConfigType?>(null) }
|
|
||||||
val derivedConfigType = remember {
|
|
||||||
derivedStateOf<ConfigType> {
|
|
||||||
configType ?: if (!uiState.hasAmneziaProperties()) ConfigType.WIREGUARD else ConfigType.AMNEZIA
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val saved by viewModel.saved.collectAsStateWithLifecycle(null)
|
|
||||||
|
|
||||||
LaunchedEffect(saved) {
|
LaunchedEffect(Unit) { viewModel.init(tunnelId) }
|
||||||
if (saved == true) {
|
|
||||||
navController.navigate(Route.Main)
|
LaunchedEffect(uiState.loading) {
|
||||||
|
if (!uiState.loading && WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||||
|
focusRequester.requestFocus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
if (uiState.loading) {
|
||||||
delay(2_000L)
|
LoadingScreen()
|
||||||
viewModel.cleanUpUninstalledApps()
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||||
|
|
||||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
||||||
|
|
||||||
val fillMaxHeight = .85f
|
val fillMaxHeight = .85f
|
||||||
@@ -142,13 +156,13 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
},
|
},
|
||||||
onError = {
|
onError = {
|
||||||
showAuthPrompt = false
|
showAuthPrompt = false
|
||||||
snackbar.showMessage(
|
appViewModel.showSnackbarMessage(
|
||||||
context.getString(R.string.error_authentication_failed),
|
context.getString(R.string.error_authentication_failed),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onFailure = {
|
onFailure = {
|
||||||
showAuthPrompt = false
|
showAuthPrompt = false
|
||||||
snackbar.showMessage(
|
appViewModel.showSnackbarMessage(
|
||||||
context.getString(R.string.error_authorization_failed),
|
context.getString(R.string.error_authorization_failed),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@@ -156,33 +170,193 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (showApplicationsDialog) {
|
if (showApplicationsDialog) {
|
||||||
ApplicationSelectionDialog(viewModel, uiState) {
|
val sortedPackages =
|
||||||
showApplicationsDialog = false
|
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(
|
Scaffold(
|
||||||
topBar = {
|
|
||||||
TopNavBar(stringResource(R.string.edit_tunnel))
|
|
||||||
},
|
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
floatingActionButtonPosition = FabPosition.End,
|
||||||
floatingActionButton = {
|
floatingActionButton = {
|
||||||
|
val secondaryColor = MaterialTheme.colorScheme.secondary
|
||||||
|
val hoverColor = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)
|
||||||
|
var fobColor by remember { mutableStateOf(secondaryColor) }
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onClick = {
|
modifier =
|
||||||
viewModel.onSaveAllChanges()
|
Modifier.onFocusChanged {
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
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),
|
shape = RoundedCornerShape(16.dp),
|
||||||
) {
|
) {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = Icons.Rounded.Save,
|
imageVector = Icons.Rounded.Save,
|
||||||
contentDescription = stringResource(id = R.string.save_changes),
|
contentDescription = stringResource(id = R.string.save_changes),
|
||||||
tint = MaterialTheme.colorScheme.background,
|
tint = Color.DarkGray,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
Column(Modifier.padding(it)) {
|
Column {
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
@@ -199,7 +373,7 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
color = MaterialTheme.colorScheme.surface,
|
color = MaterialTheme.colorScheme.surface,
|
||||||
modifier =
|
modifier =
|
||||||
(
|
(
|
||||||
if (context.isRunningOnTv()) {
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxHeight(fillMaxHeight)
|
.fillMaxHeight(fillMaxHeight)
|
||||||
.fillMaxWidth(fillMaxWidth)
|
.fillMaxWidth(fillMaxWidth)
|
||||||
@@ -207,7 +381,7 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
Modifier.fillMaxWidth(fillMaxWidth)
|
Modifier.fillMaxWidth(fillMaxWidth)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.padding(bottom = 10.dp.scaledHeight()).padding(top = 24.dp.scaledHeight()),
|
.padding(bottom = 10.dp),
|
||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
@@ -221,20 +395,16 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
stringResource(R.string.interface_),
|
stringResource(R.string.interface_),
|
||||||
padding = screenPadding,
|
padding = screenPadding,
|
||||||
)
|
)
|
||||||
ConfigurationToggle(
|
|
||||||
stringResource(id = R.string.show_amnezia_properties),
|
|
||||||
checked = derivedConfigType.value == ConfigType.AMNEZIA,
|
|
||||||
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.tunnelName,
|
value = uiState.tunnelName,
|
||||||
onValueChange = viewModel::onTunnelNameChange,
|
onValueChange = { value -> viewModel.onTunnelNameChange(value) },
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.name),
|
label = stringResource(R.string.name),
|
||||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
modifier =
|
modifier =
|
||||||
@@ -243,12 +413,12 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
.clickable { showAuthPrompt = true },
|
.clickable { showAuthPrompt = true },
|
||||||
value = uiState.interfaceProxy.privateKey,
|
value = uiState.interfaceProxy.privateKey,
|
||||||
visualTransformation =
|
visualTransformation =
|
||||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated) {
|
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated) {
|
||||||
VisualTransformation.None
|
VisualTransformation.None
|
||||||
} else {
|
} else {
|
||||||
PasswordVisualTransformation()
|
PasswordVisualTransformation()
|
||||||
},
|
},
|
||||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated,
|
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated,
|
||||||
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
||||||
trailingIcon = {
|
trailingIcon = {
|
||||||
IconButton(
|
IconButton(
|
||||||
@@ -258,7 +428,7 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
Icon(
|
Icon(
|
||||||
Icons.Rounded.Refresh,
|
Icons.Rounded.Refresh,
|
||||||
stringResource(R.string.rotate_keys),
|
stringResource(R.string.rotate_keys),
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
tint = Color.White,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -288,7 +458,7 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
Icon(
|
Icon(
|
||||||
Icons.Rounded.ContentCopy,
|
Icons.Rounded.ContentCopy,
|
||||||
stringResource(R.string.copy_public_key),
|
stringResource(R.string.copy_public_key),
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
tint = Color.White,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -298,29 +468,31 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions = keyboardOptions,
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
Row(modifier = Modifier.fillMaxWidth()) {
|
||||||
value = uiState.interfaceProxy.addresses,
|
ConfigurationTextBox(
|
||||||
onValueChange = viewModel::onAddressesChanged,
|
value = uiState.interfaceProxy.addresses,
|
||||||
keyboardActions = keyboardActions,
|
onValueChange = { value -> viewModel.onAddressesChanged(value) },
|
||||||
label = stringResource(R.string.addresses),
|
keyboardActions = keyboardActions,
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
label = stringResource(R.string.addresses),
|
||||||
modifier =
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
Modifier
|
modifier =
|
||||||
.fillMaxWidth()
|
Modifier
|
||||||
.padding(end = 5.dp),
|
.fillMaxWidth(3 / 5f)
|
||||||
)
|
.padding(end = 5.dp),
|
||||||
ConfigurationTextBox(
|
)
|
||||||
value = uiState.interfaceProxy.listenPort,
|
ConfigurationTextBox(
|
||||||
onValueChange = viewModel::onListenPortChanged,
|
value = uiState.interfaceProxy.listenPort,
|
||||||
keyboardActions = keyboardActions,
|
onValueChange = { value -> viewModel.onListenPortChanged(value) },
|
||||||
label = stringResource(R.string.listen_port),
|
keyboardActions = keyboardActions,
|
||||||
hint = stringResource(R.string.random),
|
label = stringResource(R.string.listen_port),
|
||||||
modifier = Modifier.fillMaxWidth(),
|
hint = stringResource(R.string.random),
|
||||||
)
|
modifier = Modifier.width(IntrinsicSize.Min),
|
||||||
|
)
|
||||||
|
}
|
||||||
Row(modifier = Modifier.fillMaxWidth()) {
|
Row(modifier = Modifier.fillMaxWidth()) {
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.dnsServers,
|
value = uiState.interfaceProxy.dnsServers,
|
||||||
onValueChange = viewModel::onDnsServersChanged,
|
onValueChange = { value -> viewModel.onDnsServersChanged(value) },
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.dns_servers),
|
label = stringResource(R.string.dns_servers),
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
@@ -331,27 +503,35 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.mtu,
|
value = uiState.interfaceProxy.mtu,
|
||||||
onValueChange = viewModel::onMtuChanged,
|
onValueChange = { value -> viewModel.onMtuChanged(value) },
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.mtu),
|
label = stringResource(R.string.mtu),
|
||||||
hint = stringResource(R.string.auto),
|
hint = stringResource(R.string.auto),
|
||||||
modifier = Modifier.width(IntrinsicSize.Min),
|
modifier = Modifier.width(IntrinsicSize.Min),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (derivedConfigType.value == ConfigType.AMNEZIA) {
|
if (configType == ConfigType.AMNEZIA) {
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.junkPacketCount,
|
value = uiState.interfaceProxy.junkPacketCount,
|
||||||
onValueChange = viewModel::onJunkPacketCountChanged,
|
onValueChange = {
|
||||||
|
value ->
|
||||||
|
viewModel.onJunkPacketCountChanged(value)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.junk_packet_count),
|
label = stringResource(R.string.junk_packet_count),
|
||||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.junkPacketMinSize,
|
value = uiState.interfaceProxy.junkPacketMinSize,
|
||||||
onValueChange = viewModel::onJunkPacketMinSizeChanged,
|
onValueChange = { value ->
|
||||||
|
viewModel.onJunkPacketMinSizeChanged(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.junk_packet_minimum_size),
|
label = stringResource(R.string.junk_packet_minimum_size),
|
||||||
hint =
|
hint =
|
||||||
@@ -360,11 +540,16 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.junkPacketMaxSize,
|
value = uiState.interfaceProxy.junkPacketMaxSize,
|
||||||
onValueChange = viewModel::onJunkPacketMaxSizeChanged,
|
onValueChange = { value ->
|
||||||
|
viewModel.onJunkPacketMaxSizeChanged(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.junk_packet_maximum_size),
|
label = stringResource(R.string.junk_packet_maximum_size),
|
||||||
hint =
|
hint =
|
||||||
@@ -373,21 +558,30 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.initPacketJunkSize,
|
value = uiState.interfaceProxy.initPacketJunkSize,
|
||||||
onValueChange = viewModel::onInitPacketJunkSizeChanged,
|
onValueChange = { value ->
|
||||||
|
viewModel.onInitPacketJunkSizeChanged(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.init_packet_junk_size),
|
label = stringResource(R.string.init_packet_junk_size),
|
||||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.responsePacketJunkSize,
|
value = uiState.interfaceProxy.responsePacketJunkSize,
|
||||||
onValueChange = viewModel::onResponsePacketJunkSize,
|
onValueChange = {
|
||||||
|
value ->
|
||||||
|
viewModel.onResponsePacketJunkSize(value)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.response_packet_junk_size),
|
label = stringResource(R.string.response_packet_junk_size),
|
||||||
hint =
|
hint =
|
||||||
@@ -396,11 +590,15 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.initPacketMagicHeader,
|
value = uiState.interfaceProxy.initPacketMagicHeader,
|
||||||
onValueChange = viewModel::onInitPacketMagicHeader,
|
onValueChange = {
|
||||||
|
value ->
|
||||||
|
viewModel.onInitPacketMagicHeader(value)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.init_packet_magic_header),
|
label = stringResource(R.string.init_packet_magic_header),
|
||||||
hint =
|
hint =
|
||||||
@@ -409,11 +607,16 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
||||||
onValueChange = viewModel::onResponsePacketMagicHeader,
|
onValueChange = { value ->
|
||||||
|
viewModel.onResponsePacketMagicHeader(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.response_packet_magic_header),
|
label = stringResource(R.string.response_packet_magic_header),
|
||||||
hint =
|
hint =
|
||||||
@@ -422,11 +625,16 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
||||||
onValueChange = viewModel::onUnderloadPacketMagicHeader,
|
onValueChange = { value ->
|
||||||
|
viewModel.onUnderloadPacketMagicHeader(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.underload_packet_magic_header),
|
label = stringResource(R.string.underload_packet_magic_header),
|
||||||
hint =
|
hint =
|
||||||
@@ -435,11 +643,16 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
||||||
onValueChange = viewModel::onTransportPacketMagicHeader,
|
onValueChange = { value ->
|
||||||
|
viewModel.onTransportPacketMagicHeader(
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
},
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
label = stringResource(R.string.transport_packet_magic_header),
|
label = stringResource(R.string.transport_packet_magic_header),
|
||||||
hint =
|
hint =
|
||||||
@@ -448,7 +661,8 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row(
|
Row(
|
||||||
@@ -473,7 +687,7 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
color = MaterialTheme.colorScheme.surface,
|
color = MaterialTheme.colorScheme.surface,
|
||||||
modifier =
|
modifier =
|
||||||
(
|
(
|
||||||
if (context.isRunningOnTv()) {
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxHeight(fillMaxHeight)
|
.fillMaxHeight(fillMaxHeight)
|
||||||
.fillMaxWidth(fillMaxWidth)
|
.fillMaxWidth(fillMaxWidth)
|
||||||
@@ -596,6 +810,9 @@ fun ConfigScreen(tunnelId: Int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Spacer(modifier = Modifier.weight(.17f))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-13
@@ -4,7 +4,7 @@ import com.wireguard.config.Config
|
|||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.InterfaceProxy
|
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.InterfaceProxy
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.Packages
|
import com.zaneschepke.wireguardautotunnel.util.Packages
|
||||||
|
|
||||||
data class ConfigUiState(
|
data class ConfigUiState(
|
||||||
val proxyPeers: List<PeerProxy> = arrayListOf(PeerProxy()),
|
val proxyPeers: List<PeerProxy> = arrayListOf(PeerProxy()),
|
||||||
@@ -15,12 +15,9 @@ data class ConfigUiState(
|
|||||||
val isAllApplicationsEnabled: Boolean = false,
|
val isAllApplicationsEnabled: Boolean = false,
|
||||||
val loading: Boolean = true,
|
val loading: Boolean = true,
|
||||||
val tunnel: TunnelConfig? = null,
|
val tunnel: TunnelConfig? = null,
|
||||||
var tunnelName: String = "",
|
val tunnelName: String = "",
|
||||||
val isAmneziaEnabled: Boolean = false,
|
val isAmneziaEnabled: Boolean = false,
|
||||||
) {
|
) {
|
||||||
fun hasAmneziaProperties(): Boolean {
|
|
||||||
return this.interfaceProxy.junkPacketCount != ""
|
|
||||||
}
|
|
||||||
companion object {
|
companion object {
|
||||||
fun from(config: Config): ConfigUiState {
|
fun from(config: Config): ConfigUiState {
|
||||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||||
@@ -48,6 +45,7 @@ data class ConfigUiState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun from(config: org.amnezia.awg.config.Config): ConfigUiState {
|
fun from(config: org.amnezia.awg.config.Config): ConfigUiState {
|
||||||
|
// TODO update with new values
|
||||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
||||||
var include = true
|
var include = true
|
||||||
@@ -71,13 +69,5 @@ data class ConfigUiState(
|
|||||||
isAllApplicationsEnabled,
|
isAllApplicationsEnabled,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun from(tunnel: TunnelConfig): ConfigUiState {
|
|
||||||
val config = tunnel.toAmConfig()
|
|
||||||
return from(config).copy(
|
|
||||||
tunnelName = tunnel.name,
|
|
||||||
tunnel = tunnel,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+196
-221
@@ -15,119 +15,100 @@ import com.zaneschepke.wireguardautotunnel.R
|
|||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
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.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.removeAt
|
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.update
|
import com.zaneschepke.wireguardautotunnel.util.removeAt
|
||||||
import dagger.assisted.Assisted
|
import com.zaneschepke.wireguardautotunnel.util.update
|
||||||
import dagger.assisted.AssistedFactory
|
|
||||||
import dagger.assisted.AssistedInject
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.onStart
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.plus
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel(assistedFactory = ConfigViewModel.ConfigViewModelFactory::class)
|
@HiltViewModel
|
||||||
class ConfigViewModel
|
class ConfigViewModel
|
||||||
@AssistedInject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
|
private val settingsRepository: SettingsRepository,
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
@Assisted val id: Int,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val packageManager = WireGuardAutoTunnel.instance.packageManager
|
private val packageManager = WireGuardAutoTunnel.instance.packageManager
|
||||||
|
|
||||||
private val _saved = MutableSharedFlow<Boolean>()
|
|
||||||
val saved = _saved.asSharedFlow()
|
|
||||||
|
|
||||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||||
val uiState = _uiState.onStart {
|
val uiState = _uiState.asStateFlow()
|
||||||
appDataRepository.tunnels.getById(id)?.let {
|
|
||||||
val packages = getQueriedPackages()
|
fun init(tunnelId: String) = viewModelScope.launch(ioDispatcher) {
|
||||||
_uiState.value = ConfigUiState.from(it).copy(
|
val packages = getQueriedPackages("")
|
||||||
packages = packages,
|
val state =
|
||||||
)
|
if (tunnelId != Constants.MANUAL_TUNNEL_CONFIG_ID) {
|
||||||
}
|
val tunnelConfig =
|
||||||
}.stateIn(
|
appDataRepository.tunnels.getAll()
|
||||||
viewModelScope + ioDispatcher,
|
.firstOrNull { it.id.toString() == tunnelId }
|
||||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
val isAmneziaEnabled = settingsRepository.getSettings().isAmneziaEnabled
|
||||||
ConfigUiState(),
|
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) {
|
fun onTunnelNameChange(name: String) {
|
||||||
_uiState.update {
|
_uiState.value = _uiState.value.copy(tunnelName = name)
|
||||||
it.copy(tunnelName = name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onIncludeChange(include: Boolean) {
|
fun onIncludeChange(include: Boolean) {
|
||||||
_uiState.update {
|
_uiState.value = _uiState.value.copy(include = include)
|
||||||
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,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onAddCheckedPackage(packageName: String) {
|
fun onAddCheckedPackage(packageName: String) {
|
||||||
_uiState.update {
|
_uiState.value =
|
||||||
it.copy(
|
_uiState.value.copy(
|
||||||
checkedPackageNames = it.checkedPackageNames + packageName,
|
checkedPackageNames = _uiState.value.checkedPackageNames + packageName,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
||||||
_uiState.update {
|
_uiState.value = _uiState.value.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
||||||
it.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onRemoveCheckedPackage(packageName: String) {
|
fun onRemoveCheckedPackage(packageName: String) {
|
||||||
_uiState.update {
|
_uiState.value =
|
||||||
it.copy(
|
_uiState.value.copy(
|
||||||
checkedPackageNames = it.checkedPackageNames - packageName,
|
checkedPackageNames = _uiState.value.checkedPackageNames - packageName,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getQueriedPackages(query: String = ""): List<PackageInfo> {
|
private fun getQueriedPackages(query: String): List<PackageInfo> {
|
||||||
return getAllInternetCapablePackages().filter {
|
return getAllInternetCapablePackages().filter {
|
||||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||||
}
|
}
|
||||||
@@ -156,13 +137,12 @@ constructor(
|
|||||||
return _uiState.value.isAllApplicationsEnabled
|
return _uiState.value.isAllApplicationsEnabled
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch { appDataRepository.tunnels.save(tunnelConfig) }
|
||||||
appDataRepository.tunnels.save(tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
||||||
if (tunnelConfig != null) {
|
if (tunnelConfig != null) {
|
||||||
saveConfig(tunnelConfig).join()
|
saveConfig(tunnelConfig).join()
|
||||||
|
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,113 +175,105 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun emptyCheckedPackagesList() {
|
private fun emptyCheckedPackagesList() {
|
||||||
_uiState.update {
|
_uiState.value = _uiState.value.copy(checkedPackageNames = emptyList())
|
||||||
it.copy(checkedPackageNames = emptyList())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildInterfaceListFromProxyInterface(): Interface {
|
private fun buildInterfaceListFromProxyInterface(): Interface {
|
||||||
val builder = Interface.Builder()
|
val builder = Interface.Builder()
|
||||||
with(_uiState.value.interfaceProxy) {
|
builder.parsePrivateKey(_uiState.value.interfaceProxy.privateKey.trim())
|
||||||
builder.parsePrivateKey(this.privateKey.trim())
|
builder.parseAddresses(_uiState.value.interfaceProxy.addresses.trim())
|
||||||
builder.parseAddresses(this.addresses.trim())
|
if (_uiState.value.interfaceProxy.dnsServers.isNotEmpty()) {
|
||||||
if (this.dnsServers.isNotEmpty()) {
|
builder.parseDnsServers(_uiState.value.interfaceProxy.dnsServers.trim())
|
||||||
builder.parseDnsServers(this.dnsServers.trim())
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.mtu.isNotEmpty()) {
|
||||||
if (this.mtu.isNotEmpty()) {
|
builder.parseMtu(_uiState.value.interfaceProxy.mtu.trim())
|
||||||
builder.parseMtu(this.mtu.trim())
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.listenPort.isNotEmpty()) {
|
||||||
if (this.listenPort.isNotEmpty()) {
|
builder.parseListenPort(_uiState.value.interfaceProxy.listenPort.trim())
|
||||||
builder.parseListenPort(this.listenPort.trim())
|
}
|
||||||
}
|
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
if (_uiState.value.include) {
|
||||||
if (_uiState.value.include) {
|
builder.includeApplications(
|
||||||
builder.includeApplications(
|
_uiState.value.checkedPackageNames,
|
||||||
_uiState.value.checkedPackageNames,
|
)
|
||||||
)
|
}
|
||||||
}
|
if (!_uiState.value.include) {
|
||||||
if (!_uiState.value.include) {
|
builder.excludeApplications(
|
||||||
builder.excludeApplications(
|
_uiState.value.checkedPackageNames,
|
||||||
_uiState.value.checkedPackageNames,
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build()
|
return builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildAmInterfaceListFromProxyInterface(): org.amnezia.awg.config.Interface {
|
private fun buildAmInterfaceListFromProxyInterface(): org.amnezia.awg.config.Interface {
|
||||||
val builder = org.amnezia.awg.config.Interface.Builder()
|
val builder = org.amnezia.awg.config.Interface.Builder()
|
||||||
with(_uiState.value.interfaceProxy) {
|
builder.parsePrivateKey(_uiState.value.interfaceProxy.privateKey.trim())
|
||||||
builder.parsePrivateKey(this.privateKey.trim())
|
builder.parseAddresses(_uiState.value.interfaceProxy.addresses.trim())
|
||||||
builder.parseAddresses(this.addresses.trim())
|
if (_uiState.value.interfaceProxy.dnsServers.isNotEmpty()) {
|
||||||
if (this.dnsServers.isNotEmpty()) {
|
builder.parseDnsServers(_uiState.value.interfaceProxy.dnsServers.trim())
|
||||||
builder.parseDnsServers(this.dnsServers.trim())
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.mtu.isNotEmpty()) {
|
||||||
if (this.mtu.isNotEmpty()) {
|
builder.parseMtu(_uiState.value.interfaceProxy.mtu.trim())
|
||||||
builder.parseMtu(this.mtu.trim())
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.listenPort.isNotEmpty()) {
|
||||||
if (this.listenPort.isNotEmpty()) {
|
builder.parseListenPort(_uiState.value.interfaceProxy.listenPort.trim())
|
||||||
builder.parseListenPort(this.listenPort.trim())
|
}
|
||||||
}
|
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
if (_uiState.value.include) {
|
||||||
if (_uiState.value.include) {
|
builder.includeApplications(
|
||||||
builder.includeApplications(
|
_uiState.value.checkedPackageNames,
|
||||||
_uiState.value.checkedPackageNames,
|
)
|
||||||
)
|
}
|
||||||
}
|
if (!_uiState.value.include) {
|
||||||
if (!_uiState.value.include) {
|
builder.excludeApplications(
|
||||||
builder.excludeApplications(
|
_uiState.value.checkedPackageNames,
|
||||||
_uiState.value.checkedPackageNames,
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.junkPacketCount.isNotEmpty()) {
|
||||||
if (this.junkPacketCount.isNotEmpty()) {
|
builder.setJunkPacketCount(
|
||||||
builder.setJunkPacketCount(
|
_uiState.value.interfaceProxy.junkPacketCount.trim().toInt(),
|
||||||
this.junkPacketCount.trim().toInt(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.junkPacketMinSize.isNotEmpty()) {
|
||||||
if (this.junkPacketMinSize.isNotEmpty()) {
|
builder.setJunkPacketMinSize(
|
||||||
builder.setJunkPacketMinSize(
|
_uiState.value.interfaceProxy.junkPacketMinSize.trim().toInt(),
|
||||||
this.junkPacketMinSize.trim().toInt(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.junkPacketMaxSize.isNotEmpty()) {
|
||||||
if (this.junkPacketMaxSize.isNotEmpty()) {
|
builder.setJunkPacketMaxSize(
|
||||||
builder.setJunkPacketMaxSize(
|
_uiState.value.interfaceProxy.junkPacketMaxSize.trim().toInt(),
|
||||||
this.junkPacketMaxSize.trim().toInt(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.initPacketJunkSize.isNotEmpty()) {
|
||||||
if (this.initPacketJunkSize.isNotEmpty()) {
|
builder.setInitPacketJunkSize(
|
||||||
builder.setInitPacketJunkSize(
|
_uiState.value.interfaceProxy.initPacketJunkSize.trim().toInt(),
|
||||||
this.initPacketJunkSize.trim().toInt(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.responsePacketJunkSize.isNotEmpty()) {
|
||||||
if (this.responsePacketJunkSize.isNotEmpty()) {
|
builder.setResponsePacketJunkSize(
|
||||||
builder.setResponsePacketJunkSize(
|
_uiState.value.interfaceProxy.responsePacketJunkSize.trim().toInt(),
|
||||||
this.responsePacketJunkSize.trim().toInt(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.initPacketMagicHeader.isNotEmpty()) {
|
||||||
if (this.initPacketMagicHeader.isNotEmpty()) {
|
builder.setInitPacketMagicHeader(
|
||||||
builder.setInitPacketMagicHeader(
|
_uiState.value.interfaceProxy.initPacketMagicHeader.trim().toLong(),
|
||||||
this.initPacketMagicHeader.trim().toLong(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.responsePacketMagicHeader.isNotEmpty()) {
|
||||||
if (this.responsePacketMagicHeader.isNotEmpty()) {
|
builder.setResponsePacketMagicHeader(
|
||||||
builder.setResponsePacketMagicHeader(
|
_uiState.value.interfaceProxy.responsePacketMagicHeader.trim().toLong(),
|
||||||
this.responsePacketMagicHeader.trim().toLong(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.transportPacketMagicHeader.isNotEmpty()) {
|
||||||
if (this.transportPacketMagicHeader.isNotEmpty()) {
|
builder.setTransportPacketMagicHeader(
|
||||||
builder.setTransportPacketMagicHeader(
|
_uiState.value.interfaceProxy.transportPacketMagicHeader.trim().toLong(),
|
||||||
this.transportPacketMagicHeader.trim().toLong(),
|
)
|
||||||
)
|
}
|
||||||
}
|
if (_uiState.value.interfaceProxy.underloadPacketMagicHeader.isNotEmpty()) {
|
||||||
if (this.underloadPacketMagicHeader.isNotEmpty()) {
|
builder.setUnderloadPacketMagicHeader(
|
||||||
builder.setUnderloadPacketMagicHeader(
|
_uiState.value.interfaceProxy.underloadPacketMagicHeader.trim().toLong(),
|
||||||
this.underloadPacketMagicHeader.trim().toLong(),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build()
|
return builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,33 +292,41 @@ constructor(
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onSaveAllChanges() = viewModelScope.launch {
|
fun onSaveAllChanges(configType: ConfigType): Result<Unit> {
|
||||||
kotlin.runCatching {
|
return try {
|
||||||
val wgQuick = buildConfig().toWgQuickString(true)
|
val wgQuick = buildConfig().toWgQuickString()
|
||||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
val amQuick =
|
||||||
val tunnelConfig = uiState.value.tunnel?.copy(
|
if (configType == ConfigType.AMNEZIA) {
|
||||||
name = _uiState.value.tunnelName,
|
buildAmConfig().toAwgQuickString()
|
||||||
amQuick = amQuick,
|
} else {
|
||||||
wgQuick = wgQuick,
|
TunnelConfig.AM_QUICK_DEFAULT
|
||||||
) ?: TunnelConfig(
|
}
|
||||||
name = _uiState.value.tunnelName,
|
val tunnelConfig =
|
||||||
wgQuick = wgQuick,
|
when (uiState.value.tunnel) {
|
||||||
amQuick = amQuick,
|
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)
|
updateTunnelConfig(tunnelConfig)
|
||||||
SnackbarController.showMessage(
|
Result.success(Unit)
|
||||||
StringValue.StringResource(R.string.config_changes_saved),
|
} catch (e: Exception) {
|
||||||
)
|
Timber.e(e)
|
||||||
_saved.emit(true)
|
val message = e.message?.substringAfter(":", missingDelimiterValue = "")
|
||||||
}.onFailure {
|
val stringValue =
|
||||||
Timber.e(it)
|
message?.let {
|
||||||
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
StringValue.DynamicString(message)
|
||||||
val stringValue = if (message.isNullOrBlank()) {
|
} ?: StringValue.StringResource(R.string.unknown_error)
|
||||||
StringValue.StringResource(R.string.unknown_error)
|
Result.failure(WgTunnelExceptions.ConfigParseError(stringValue))
|
||||||
} else {
|
|
||||||
StringValue.DynamicString(message)
|
|
||||||
}
|
|
||||||
SnackbarController.showMessage(stringValue)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,7 +409,7 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
privateKey = keyPair.privateKey.toBase64(),
|
privateKey = keyPair.privateKey.toBase64(),
|
||||||
publicKey = keyPair.publicKey.toBase64(),
|
publicKey = keyPair.publicKey.toBase64(),
|
||||||
),
|
),
|
||||||
@@ -440,7 +420,7 @@ constructor(
|
|||||||
fun onAddressesChanged(value: String) {
|
fun onAddressesChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(addresses = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(addresses = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -448,7 +428,7 @@ constructor(
|
|||||||
fun onListenPortChanged(value: String) {
|
fun onListenPortChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(listenPort = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(listenPort = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -456,21 +436,21 @@ constructor(
|
|||||||
fun onDnsServersChanged(value: String) {
|
fun onDnsServersChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(dnsServers = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(dnsServers = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMtuChanged(value: String) {
|
fun onMtuChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(interfaceProxy = it.interfaceProxy.copy(mtu = value))
|
it.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(mtu = value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onInterfacePublicKeyChange(value: String) {
|
private fun onInterfacePublicKeyChange(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(publicKey = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(publicKey = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -478,7 +458,7 @@ constructor(
|
|||||||
fun onPrivateKeyChange(value: String) {
|
fun onPrivateKeyChange(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(privateKey = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(privateKey = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (NumberUtils.isValidKey(value)) {
|
if (NumberUtils.isValidKey(value)) {
|
||||||
@@ -500,7 +480,7 @@ constructor(
|
|||||||
fun onJunkPacketCountChanged(value: String) {
|
fun onJunkPacketCountChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketCount = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketCount = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -508,7 +488,7 @@ constructor(
|
|||||||
fun onJunkPacketMinSizeChanged(value: String) {
|
fun onJunkPacketMinSizeChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMinSize = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketMinSize = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -516,7 +496,7 @@ constructor(
|
|||||||
fun onJunkPacketMaxSizeChanged(value: String) {
|
fun onJunkPacketMaxSizeChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMaxSize = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(junkPacketMaxSize = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -524,7 +504,7 @@ constructor(
|
|||||||
fun onInitPacketJunkSizeChanged(value: String) {
|
fun onInitPacketJunkSizeChanged(value: String) {
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy = it.interfaceProxy.copy(initPacketJunkSize = value),
|
interfaceProxy = _uiState.value.interfaceProxy.copy(initPacketJunkSize = value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -533,7 +513,7 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
responsePacketJunkSize = value,
|
responsePacketJunkSize = value,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -544,7 +524,7 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
initPacketMagicHeader = value,
|
initPacketMagicHeader = value,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -555,7 +535,7 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
responsePacketMagicHeader = value,
|
responsePacketMagicHeader = value,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -566,7 +546,7 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
transportPacketMagicHeader = value,
|
transportPacketMagicHeader = value,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -577,15 +557,10 @@ constructor(
|
|||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
interfaceProxy =
|
interfaceProxy =
|
||||||
it.interfaceProxy.copy(
|
_uiState.value.interfaceProxy.copy(
|
||||||
underloadPacketMagicHeader = value,
|
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-5
@@ -25,10 +25,7 @@ data class InterfaceProxy(
|
|||||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||||
addresses = i.addresses.joinToString(", ").trim(),
|
addresses = i.addresses.joinToString(", ").trim(),
|
||||||
dnsServers = listOf(
|
dnsServers = i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||||
i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
|
||||||
i.dnsSearchDomains.joinToString(", ").trim(),
|
|
||||||
).filter { it.length > 0 }.joinToString(", "),
|
|
||||||
listenPort =
|
listenPort =
|
||||||
if (i.listenPort.isPresent) {
|
if (i.listenPort.isPresent) {
|
||||||
i.listenPort.get().toString().trim()
|
i.listenPort.get().toString().trim()
|
||||||
@@ -44,7 +41,7 @@ data class InterfaceProxy(
|
|||||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||||
addresses = i.addresses.joinToString(", ").trim(),
|
addresses = i.addresses.joinToString(", ").trim(),
|
||||||
dnsServers = (i.dnsServers + i.dnsSearchDomains).joinToString(", ").replace("/", "").trim(),
|
dnsServers = i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||||
listenPort =
|
listenPort =
|
||||||
if (i.listenPort.isPresent) {
|
if (i.listenPort.isPresent) {
|
||||||
i.listenPort.get().toString().trim()
|
i.listenPort.get().toString().trim()
|
||||||
|
|||||||
+690
-162
@@ -1,120 +1,229 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.content.pm.PackageManager
|
||||||
import android.net.VpnService
|
import android.os.Build
|
||||||
import android.provider.Settings
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.ActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
|
import androidx.compose.animation.fadeIn
|
||||||
|
import androidx.compose.animation.fadeOut
|
||||||
|
import androidx.compose.animation.slideInVertically
|
||||||
|
import androidx.compose.animation.slideOutVertically
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.focusGroup
|
||||||
|
import androidx.compose.foundation.focusable
|
||||||
import androidx.compose.foundation.gestures.ScrollableDefaults
|
import androidx.compose.foundation.gestures.ScrollableDefaults
|
||||||
import androidx.compose.foundation.gestures.detectTapGestures
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
import androidx.compose.foundation.overscroll
|
import androidx.compose.foundation.overscroll
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.foundation.text.ClickableText
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.Add
|
import androidx.compose.material.icons.filled.Create
|
||||||
import androidx.compose.material.icons.outlined.Add
|
import androidx.compose.material.icons.filled.FileOpen
|
||||||
|
import androidx.compose.material.icons.filled.QrCode
|
||||||
|
import androidx.compose.material.icons.rounded.Bolt
|
||||||
|
import androidx.compose.material.icons.rounded.Circle
|
||||||
|
import androidx.compose.material.icons.rounded.CopyAll
|
||||||
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
|
import androidx.compose.material.icons.rounded.Info
|
||||||
|
import androidx.compose.material.icons.rounded.Settings
|
||||||
|
import androidx.compose.material.icons.rounded.Smartphone
|
||||||
|
import androidx.compose.material.icons.rounded.Star
|
||||||
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.FabPosition
|
import androidx.compose.material3.FabPosition
|
||||||
|
import androidx.compose.material3.HorizontalDivider
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.IconButton
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.ModalBottomSheet
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.material3.rememberModalBottomSheetState
|
||||||
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
|
import androidx.compose.ui.focus.focusRequester
|
||||||
|
import androidx.compose.ui.geometry.Offset
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
||||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||||
import androidx.compose.ui.res.stringResource
|
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 androidx.compose.ui.unit.dp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import androidx.navigation.NavController
|
||||||
|
import com.iamageo.multifablibrary.FabIcon
|
||||||
|
import com.iamageo.multifablibrary.FabOption
|
||||||
|
import com.iamageo.multifablibrary.MultiFabItem
|
||||||
|
import com.iamageo.multifablibrary.MultiFloatingActionButton
|
||||||
|
import com.journeyapps.barcodescanner.ScanContract
|
||||||
|
import com.journeyapps.barcodescanner.ScanOptions
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.NestedScrollListener
|
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.dialog.InfoDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.theme.corn
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.theme.mint
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.AutoTunnelRowItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.GettingStartedLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissFab
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelRowItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.VpnDeniedDialog
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isBatteryOptimizationsDisabled
|
import com.zaneschepke.wireguardautotunnel.util.getMessage
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.handshakeStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
import com.zaneschepke.wireguardautotunnel.util.mapPeerStats
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) {
|
fun MainScreen(
|
||||||
|
viewModel: MainViewModel = hiltViewModel(),
|
||||||
|
appViewModel: AppViewModel,
|
||||||
|
focusRequester: FocusRequester,
|
||||||
|
navController: NavController,
|
||||||
|
) {
|
||||||
|
val haptic = LocalHapticFeedback.current
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val navController = LocalNavController.current
|
val isVisible = rememberSaveable { mutableStateOf(true) }
|
||||||
val snackbar = SnackbarController.current
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
|
val sheetState = rememberModalBottomSheetState()
|
||||||
var showBottomSheet by remember { mutableStateOf(false) }
|
var showBottomSheet by remember { mutableStateOf(false) }
|
||||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
var configType by remember { mutableStateOf(ConfigType.WIREGUARD) }
|
||||||
var isFabVisible by rememberSaveable { mutableStateOf(true) }
|
|
||||||
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
|
||||||
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
|
||||||
val isRunningOnTv = remember { context.isRunningOnTv() }
|
|
||||||
|
|
||||||
val nestedScrollConnection = remember {
|
// Nested scroll for control FAB
|
||||||
NestedScrollListener({ isFabVisible = false }, { isFabVisible = true })
|
val nestedScrollConnection =
|
||||||
}
|
remember {
|
||||||
|
object : NestedScrollConnection {
|
||||||
|
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
||||||
|
// Hide FAB
|
||||||
|
if (available.y < -1) {
|
||||||
|
isVisible.value = false
|
||||||
|
}
|
||||||
|
// Show FAB
|
||||||
|
if (available.y > 1) {
|
||||||
|
isVisible.value = true
|
||||||
|
}
|
||||||
|
|
||||||
val vpnActivity =
|
return Offset.Zero
|
||||||
rememberLauncherForActivityResult(
|
}
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
}
|
||||||
onResult = {
|
|
||||||
if (it.resultCode != RESULT_OK) showVpnPermissionDialog = true
|
|
||||||
},
|
|
||||||
)
|
|
||||||
val batteryActivity =
|
|
||||||
rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
) { result: ActivityResult ->
|
|
||||||
viewModel.setBatteryOptimizeDisableShown()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
||||||
snackbar.showMessage(
|
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
||||||
context.getString(R.string.error_no_file_explorer),
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
)
|
|
||||||
}, onData = { data ->
|
|
||||||
viewModel.onTunnelFileSelected(data, context)
|
|
||||||
})
|
|
||||||
|
|
||||||
val requestPermissionLauncher = rememberLauncherForActivityResult(
|
LaunchedEffect(Unit) {
|
||||||
ActivityResultContracts.RequestPermission(),
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
) { isGranted ->
|
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||||
if (!isGranted) return@rememberLauncherForActivityResult snackbar.showMessage("Camera permission required")
|
focusRequester.requestFocus()
|
||||||
navController.navigate(Route.Scanner)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
val tunnelFileImportResultLauncher =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
object : ActivityResultContracts.GetContent() {
|
||||||
|
override fun createIntent(context: Context, input: String): Intent {
|
||||||
|
val intent = super.createIntent(context, input)
|
||||||
|
|
||||||
|
/* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than
|
||||||
|
* what we can do, so detect this and throw an exception that we can catch later. */
|
||||||
|
val activitiesToResolveIntent =
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
context.packageManager.queryIntentActivities(
|
||||||
|
intent,
|
||||||
|
PackageManager.ResolveInfoFlags.of(
|
||||||
|
PackageManager.MATCH_DEFAULT_ONLY.toLong(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
context.packageManager.queryIntentActivities(
|
||||||
|
intent,
|
||||||
|
PackageManager.MATCH_DEFAULT_ONLY,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
activitiesToResolveIntent.all {
|
||||||
|
val name = it.activityInfo.packageName
|
||||||
|
name.startsWith(Constants.GOOGLE_TV_EXPLORER_STUB) ||
|
||||||
|
name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
appViewModel.showSnackbarMessage(
|
||||||
|
context.getString(R.string.error_no_file_explorer),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return intent
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) { data ->
|
||||||
|
if (data == null) return@rememberLauncherForActivityResult
|
||||||
|
scope.launch {
|
||||||
|
viewModel.onTunnelFileSelected(data, configType, context).onFailure {
|
||||||
|
appViewModel.showSnackbarMessage(it.getMessage(context))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val scanLauncher =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
contract = ScanContract(),
|
||||||
|
onResult = {
|
||||||
|
if (it.contents != null) {
|
||||||
|
scope.launch {
|
||||||
|
viewModel.onTunnelQrResult(it.contents, configType).onFailure { error ->
|
||||||
|
appViewModel.showSnackbarMessage(error.getMessage(context))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
if (showDeleteTunnelAlertDialog) {
|
if (showDeleteTunnelAlertDialog) {
|
||||||
InfoDialog(
|
InfoDialog(
|
||||||
onDismiss = { showDeleteTunnelAlertDialog = false },
|
onDismiss = { showDeleteTunnelAlertDialog = false },
|
||||||
onAttest = {
|
onAttest = {
|
||||||
selectedTunnel?.let { viewModel::onDelete }
|
selectedTunnel?.let { viewModel.onDelete(it, context) }
|
||||||
showDeleteTunnelAlertDialog = false
|
showDeleteTunnelAlertDialog = false
|
||||||
selectedTunnel = null
|
selectedTunnel = null
|
||||||
},
|
},
|
||||||
@@ -124,99 +233,207 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestBatteryOptimizationsDisabled() {
|
|
||||||
val intent =
|
|
||||||
Intent().apply {
|
|
||||||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
|
||||||
data = Uri.parse("package:${context.packageName}")
|
|
||||||
}
|
|
||||||
batteryActivity.launch(intent)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAutoTunnelToggle() {
|
|
||||||
if (!uiState.generalState.isBatteryOptimizationDisableShown &&
|
|
||||||
!context.isBatteryOptimizationsDisabled() && !isRunningOnTv
|
|
||||||
) {
|
|
||||||
return requestBatteryOptimizationsDisabled()
|
|
||||||
}
|
|
||||||
val intent = if (!uiState.settings.isKernelEnabled) {
|
|
||||||
VpnService.prepare(context)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (intent != null) return vpnActivity.launch(intent)
|
|
||||||
viewModel.onToggleAutoTunnel()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
||||||
val intent = if (uiState.settings.isKernelEnabled) null else VpnService.prepare(context)
|
if (appViewModel.isRequiredPermissionGranted()) {
|
||||||
if (intent != null) return vpnActivity.launch(intent)
|
if (checked) {
|
||||||
if (!checked) viewModel.onTunnelStop(tunnel).also { return }
|
viewModel.onTunnelStart(tunnel, context)
|
||||||
viewModel.onTunnelStart(tunnel, uiState.settings.isKernelEnabled)
|
} else {
|
||||||
|
viewModel.onTunnelStop(
|
||||||
|
context,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.loading) {
|
||||||
|
return LoadingScreen()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun launchQrScanner() {
|
||||||
|
val scanOptions = ScanOptions()
|
||||||
|
scanOptions.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
||||||
|
scanOptions.setOrientationLocked(true)
|
||||||
|
scanOptions.setPrompt(
|
||||||
|
context.getString(R.string.scanning_qr),
|
||||||
|
)
|
||||||
|
scanOptions.setBeepEnabled(false)
|
||||||
|
scanLauncher.launch(scanOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier.pointerInput(Unit) {
|
Modifier.pointerInput(Unit) {
|
||||||
if (uiState.tunnels.isEmpty()) return@pointerInput
|
if (uiState.tunnels.isNotEmpty()) {
|
||||||
detectTapGestures(
|
detectTapGestures(
|
||||||
onTap = {
|
onTap = {
|
||||||
selectedTunnel = null
|
selectedTunnel = null
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
|
||||||
floatingActionButton = {
|
|
||||||
if (!isRunningOnTv) {
|
|
||||||
ScrollDismissFab({
|
|
||||||
val icon = Icons.Filled.Add
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
tint = MaterialTheme.colorScheme.onPrimary,
|
|
||||||
)
|
|
||||||
}, isVisible = isFabVisible, onClick = {
|
|
||||||
showBottomSheet = true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
topBar = {
|
|
||||||
if (isRunningOnTv) {
|
|
||||||
TopNavBar(
|
|
||||||
showBack = false,
|
|
||||||
title = stringResource(R.string.app_name),
|
|
||||||
trailing = {
|
|
||||||
IconButton(onClick = {
|
|
||||||
showBottomSheet = true
|
|
||||||
}) {
|
|
||||||
val icon = Icons.Outlined.Add
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) {
|
floatingActionButtonPosition = FabPosition.End,
|
||||||
TunnelImportSheet(
|
floatingActionButton = {
|
||||||
showBottomSheet,
|
AnimatedVisibility(
|
||||||
onDismiss = { showBottomSheet = false },
|
visible = isVisible.value,
|
||||||
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
||||||
onQrClick = { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) },
|
exit = slideOutVertically(targetOffsetY = { it * 2 }),
|
||||||
onManualImportClick = {
|
modifier =
|
||||||
navController.navigate(
|
Modifier
|
||||||
Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID),
|
.focusRequester(focusRequester)
|
||||||
|
.focusGroup(),
|
||||||
|
) {
|
||||||
|
val secondaryColor = MaterialTheme.colorScheme.secondary
|
||||||
|
val tvFobColor = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)
|
||||||
|
val fobColor =
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) tvFobColor else secondaryColor
|
||||||
|
val fobIconColor =
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) Color.White else MaterialTheme.colorScheme.background
|
||||||
|
MultiFloatingActionButton(
|
||||||
|
fabIcon =
|
||||||
|
FabIcon(
|
||||||
|
iconRes = R.drawable.add,
|
||||||
|
iconResAfterRotate = R.drawable.close,
|
||||||
|
iconRotate = 180f,
|
||||||
|
),
|
||||||
|
fabOption =
|
||||||
|
FabOption(
|
||||||
|
iconTint = fobIconColor,
|
||||||
|
backgroundTint = fobColor,
|
||||||
|
),
|
||||||
|
itemsMultiFab =
|
||||||
|
listOf(
|
||||||
|
MultiFabItem(
|
||||||
|
label = {
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.amnezia),
|
||||||
|
color = Color.White,
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
modifier = Modifier.padding(end = 10.dp),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.size(40.dp),
|
||||||
|
icon = R.drawable.add,
|
||||||
|
value = ConfigType.AMNEZIA.name,
|
||||||
|
miniFabOption =
|
||||||
|
FabOption(
|
||||||
|
backgroundTint = fobColor,
|
||||||
|
fobIconColor,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
MultiFabItem(
|
||||||
|
label = {
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.wireguard),
|
||||||
|
color = Color.White,
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
modifier = Modifier.padding(end = 10.dp),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
icon = R.drawable.add,
|
||||||
|
value = ConfigType.WIREGUARD.name,
|
||||||
|
miniFabOption =
|
||||||
|
FabOption(
|
||||||
|
backgroundTint = fobColor,
|
||||||
|
fobIconColor,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
onFabItemClicked = {
|
||||||
|
showBottomSheet = true
|
||||||
|
configType = ConfigType.valueOf(it.value)
|
||||||
|
},
|
||||||
|
shape = RoundedCornerShape(16.dp),
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
)
|
},
|
||||||
|
) {
|
||||||
|
if (showBottomSheet) {
|
||||||
|
ModalBottomSheet(
|
||||||
|
onDismissRequest = {
|
||||||
|
showBottomSheet = false
|
||||||
|
},
|
||||||
|
sheetState = sheetState,
|
||||||
|
) {
|
||||||
|
// Sheet content
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable {
|
||||||
|
showBottomSheet = false
|
||||||
|
tunnelFileImportResultLauncher.launch(Constants.ALLOWED_FILE_TYPES)
|
||||||
|
}
|
||||||
|
.padding(10.dp),
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.FileOpen,
|
||||||
|
contentDescription = stringResource(id = R.string.open_file),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.add_tunnels_text),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
HorizontalDivider()
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable {
|
||||||
|
scope.launch {
|
||||||
|
showBottomSheet = false
|
||||||
|
launchQrScanner()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(10.dp),
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.QrCode,
|
||||||
|
contentDescription = stringResource(id = R.string.qr_scan),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.add_from_qr),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HorizontalDivider()
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable {
|
||||||
|
showBottomSheet = false
|
||||||
|
navController.navigate(
|
||||||
|
"${Screen.Config.route}/${Constants.MANUAL_TUNNEL_CONFIG_ID}?configType=$configType",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.padding(10.dp),
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.Create,
|
||||||
|
contentDescription = stringResource(id = R.string.create_import),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.create_import),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxSize().padding(it)
|
.fillMaxSize()
|
||||||
.overscroll(ScrollableDefaults.overscrollEffect())
|
.overscroll(ScrollableDefaults.overscrollEffect())
|
||||||
.nestedScroll(nestedScrollConnection),
|
.nestedScroll(nestedScrollConnection),
|
||||||
state = rememberLazyListState(0, uiState.tunnels.count()),
|
state = rememberLazyListState(0, uiState.tunnels.count()),
|
||||||
@@ -224,37 +441,348 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
reverseLayout = false,
|
reverseLayout = false,
|
||||||
flingBehavior = ScrollableDefaults.flingBehavior(),
|
flingBehavior = ScrollableDefaults.flingBehavior(),
|
||||||
) {
|
) {
|
||||||
if (uiState.tunnels.isEmpty()) {
|
item {
|
||||||
item {
|
AnimatedVisibility(
|
||||||
GettingStartedLabel(onClick = { context.openWebUrl(it) })
|
uiState.tunnels.isEmpty(),
|
||||||
|
exit = fadeOut(),
|
||||||
|
enter = fadeIn(),
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(top = 100.dp)
|
||||||
|
.fillMaxSize(),
|
||||||
|
) {
|
||||||
|
val gettingStarted =
|
||||||
|
buildAnnotatedString {
|
||||||
|
append(stringResource(id = R.string.see_the))
|
||||||
|
append(" ")
|
||||||
|
pushStringAnnotation(
|
||||||
|
tag = "gettingStarted",
|
||||||
|
annotation = stringResource(id = R.string.getting_started_url),
|
||||||
|
)
|
||||||
|
withStyle(
|
||||||
|
style = SpanStyle(color = MaterialTheme.colorScheme.primary),
|
||||||
|
) {
|
||||||
|
append(stringResource(id = R.string.getting_started_guide))
|
||||||
|
}
|
||||||
|
pop()
|
||||||
|
append(" ")
|
||||||
|
append(stringResource(R.string.unsure_how))
|
||||||
|
append(".")
|
||||||
|
}
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.no_tunnels),
|
||||||
|
fontStyle = FontStyle.Italic,
|
||||||
|
)
|
||||||
|
ClickableText(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(vertical = 10.dp, horizontal = 24.dp),
|
||||||
|
text = gettingStarted,
|
||||||
|
style =
|
||||||
|
MaterialTheme.typography.bodyMedium.copy(
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
gettingStarted.getStringAnnotations(tag = "gettingStarted", it, it)
|
||||||
|
.firstOrNull()?.let { annotation ->
|
||||||
|
appViewModel.openWebPage(annotation.item, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
item {
|
item {
|
||||||
AutoTunnelRowItem(uiState, {
|
if (uiState.settings.isAutoTunnelEnabled) {
|
||||||
onAutoTunnelToggle()
|
val itemFocusRequester = remember { FocusRequester() }
|
||||||
})
|
val autoTunnelingLabel =
|
||||||
|
buildAnnotatedString {
|
||||||
|
append(stringResource(id = R.string.auto_tunneling))
|
||||||
|
append(": ")
|
||||||
|
if (uiState.settings.isAutoTunnelPaused) {
|
||||||
|
append(
|
||||||
|
stringResource(id = R.string.paused),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
append(
|
||||||
|
stringResource(id = R.string.active),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RowListItem(
|
||||||
|
icon = {
|
||||||
|
val icon = Icons.Rounded.Bolt
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(end = 8.5.dp)
|
||||||
|
.size(25.dp),
|
||||||
|
tint =
|
||||||
|
if (uiState.settings.isAutoTunnelPaused) {
|
||||||
|
Color.Gray
|
||||||
|
} else {
|
||||||
|
mint
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
|
text = autoTunnelingLabel.text,
|
||||||
|
rowButton = {
|
||||||
|
if (uiState.settings.isAutoTunnelPaused) {
|
||||||
|
TextButton(
|
||||||
|
modifier = Modifier.focusRequester(itemFocusRequester),
|
||||||
|
onClick = { viewModel.resumeAutoTunneling() },
|
||||||
|
) {
|
||||||
|
Text(stringResource(id = R.string.resume))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TextButton(
|
||||||
|
modifier = Modifier.focusRequester(itemFocusRequester),
|
||||||
|
onClick = { viewModel.pauseAutoTunneling() },
|
||||||
|
) {
|
||||||
|
Text(stringResource(id = R.string.pause))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
itemFocusRequester.requestFocus()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onHold = {},
|
||||||
|
expanded = false,
|
||||||
|
statistics = null,
|
||||||
|
focusRequester = focusRequester,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
items(
|
items(
|
||||||
uiState.tunnels,
|
uiState.tunnels,
|
||||||
key = { tunnel -> tunnel.id },
|
key = { tunnel -> tunnel.id },
|
||||||
) { tunnel ->
|
) { tunnel ->
|
||||||
val isActive = uiState.tunnels.any {
|
val leadingIconColor =
|
||||||
it.id == tunnel.id &&
|
(
|
||||||
it.isActive
|
if (
|
||||||
}
|
uiState.vpnState.tunnelConfig?.name == tunnel.name &&
|
||||||
val expanded = uiState.generalState.isTunnelStatsExpanded
|
uiState.vpnState.status == TunnelState.UP
|
||||||
TunnelRowItem(
|
) {
|
||||||
isActive,
|
uiState.vpnState.statistics
|
||||||
expanded,
|
?.mapPeerStats()
|
||||||
selectedTunnel?.id == tunnel.id,
|
?.map { it.value?.handshakeStatus() }
|
||||||
tunnel,
|
.let { statuses ->
|
||||||
vpnState = uiState.vpnState,
|
when {
|
||||||
{ selectedTunnel = tunnel },
|
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> mint
|
||||||
{ viewModel.onExpandedChanged(!expanded) },
|
statuses?.any { it == HandshakeStatus.STALE } == true -> corn
|
||||||
onDelete = { showDeleteTunnelAlertDialog = true },
|
statuses?.all { it == HandshakeStatus.NOT_STARTED } == true ->
|
||||||
onCopy = { viewModel.onCopyTunnel(tunnel) },
|
Color.Gray
|
||||||
onSwitchClick = { onTunnelToggle(it, tunnel) },
|
|
||||||
|
else -> {
|
||||||
|
Color.Gray
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Color.Gray
|
||||||
|
}
|
||||||
|
)
|
||||||
|
val itemFocusRequester = remember { FocusRequester() }
|
||||||
|
val expanded = remember { mutableStateOf(false) }
|
||||||
|
RowListItem(
|
||||||
|
icon = {
|
||||||
|
val circleIcon = Icons.Rounded.Circle
|
||||||
|
val icon =
|
||||||
|
if (tunnel.isPrimaryTunnel) {
|
||||||
|
Icons.Rounded.Star
|
||||||
|
} else if (tunnel.isMobileDataTunnel) {
|
||||||
|
Icons.Rounded.Smartphone
|
||||||
|
} else {
|
||||||
|
circleIcon
|
||||||
|
}
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
tint = leadingIconColor,
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(
|
||||||
|
end = if (icon == circleIcon) 12.5.dp else 10.dp,
|
||||||
|
start = if (icon == circleIcon) 2.5.dp else 0.dp,
|
||||||
|
)
|
||||||
|
.size(if (icon == circleIcon) 15.dp else 20.dp),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
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
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
if (
|
||||||
|
uiState.vpnState.status == TunnelState.UP &&
|
||||||
|
(uiState.vpnState.tunnelConfig?.name == tunnel.name)
|
||||||
|
) {
|
||||||
|
expanded.value = !expanded.value
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selectedTunnel = tunnel
|
||||||
|
itemFocusRequester.requestFocus()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
statistics = uiState.vpnState.statistics,
|
||||||
|
expanded = expanded.value,
|
||||||
|
focusRequester = focusRequester,
|
||||||
|
rowButton = {
|
||||||
|
if (
|
||||||
|
tunnel.id == selectedTunnel?.id &&
|
||||||
|
!WireGuardAutoTunnel.isRunningOnAndroidTv()
|
||||||
|
) {
|
||||||
|
Row {
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (
|
||||||
|
uiState.settings.isAutoTunnelEnabled &&
|
||||||
|
!uiState.settings.isAutoTunnelPaused
|
||||||
|
) {
|
||||||
|
appViewModel.showSnackbarMessage(
|
||||||
|
context.getString(R.string.turn_off_tunnel),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
navController.navigate(
|
||||||
|
"${Screen.Option.route}/${selectedTunnel?.id}",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Settings
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusable(),
|
||||||
|
onClick = { viewModel.onCopyTunnel(selectedTunnel) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.CopyAll
|
||||||
|
Icon(icon, icon.name)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusable(),
|
||||||
|
onClick = { showDeleteTunnelAlertDialog = true },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Delete
|
||||||
|
Icon(icon, icon.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val checked by remember {
|
||||||
|
derivedStateOf {
|
||||||
|
(
|
||||||
|
uiState.vpnState.status == TunnelState.UP &&
|
||||||
|
tunnel.name == uiState.vpnState.tunnelConfig?.name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!checked) expanded.value = false
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TunnelSwitch() = Switch(
|
||||||
|
modifier = Modifier.focusRequester(itemFocusRequester),
|
||||||
|
checked = checked,
|
||||||
|
onCheckedChange = { checked ->
|
||||||
|
if (!checked) expanded.value = false
|
||||||
|
onTunnelToggle(checked, tunnel)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Row {
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (uiState.settings.isAutoTunnelEnabled && !uiState.settings.isAutoTunnelPaused) {
|
||||||
|
appViewModel.showSnackbarMessage(
|
||||||
|
context.getString(R.string.turn_off_auto),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
selectedTunnel = tunnel
|
||||||
|
navController.navigate(
|
||||||
|
"${Screen.Option.route}/${selectedTunnel?.id}",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Settings
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusRequester(focusRequester),
|
||||||
|
onClick = {
|
||||||
|
if (
|
||||||
|
uiState.vpnState.status == TunnelState.UP &&
|
||||||
|
(uiState.vpnState.tunnelConfig?.name == tunnel.name)
|
||||||
|
) {
|
||||||
|
expanded.value = !expanded.value
|
||||||
|
} else {
|
||||||
|
appViewModel.showSnackbarMessage(
|
||||||
|
context.getString(R.string.turn_on_tunnel),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Info
|
||||||
|
Icon(icon, icon.name)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.onCopyTunnel(tunnel) },
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.CopyAll
|
||||||
|
Icon(icon, icon.name)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (
|
||||||
|
uiState.vpnState.status == TunnelState.UP &&
|
||||||
|
tunnel.name == uiState.vpnState.tunnelConfig?.name
|
||||||
|
) {
|
||||||
|
appViewModel.showSnackbarMessage(
|
||||||
|
context.getString(R.string.turn_off_tunnel),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
selectedTunnel = tunnel
|
||||||
|
showDeleteTunnelAlertDialog = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
val icon = Icons.Rounded.Delete
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
TunnelSwitch()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TunnelSwitch()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.TunnelConfigs
|
||||||
|
|
||||||
|
data class MainUiState(
|
||||||
|
val settings: Settings = Settings(),
|
||||||
|
val tunnels: TunnelConfigs = emptyList(),
|
||||||
|
val vpnState: VpnState = VpnState(),
|
||||||
|
val loading: Boolean = true,
|
||||||
|
)
|
||||||
+244
-119
@@ -6,51 +6,67 @@ import android.net.Uri
|
|||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.wireguard.config.Config
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
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.NumberUtils
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
import com.zaneschepke.wireguardautotunnel.util.WgTunnelExceptions
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
|
import com.zaneschepke.wireguardautotunnel.util.toWgQuickString
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
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.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.util.zip.ZipInputStream
|
import java.util.zip.ZipInputStream
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class MainViewModel
|
class MainViewModel
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
private val tunnelService: Provider<TunnelService>,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
|
val vpnService: VpnService,
|
||||||
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
val uiState =
|
||||||
|
combine(
|
||||||
|
appDataRepository.settings.getSettingsFlow(),
|
||||||
|
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||||
|
vpnService.vpnState,
|
||||||
|
) { settings, tunnels, vpnState ->
|
||||||
|
MainUiState(settings, tunnels, vpnState, false)
|
||||||
|
}
|
||||||
|
.stateIn(
|
||||||
|
viewModelScope,
|
||||||
|
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||||
|
MainUiState(),
|
||||||
|
)
|
||||||
|
|
||||||
fun onDelete(tunnel: TunnelConfig) {
|
private fun stopWatcherService(context: Context) {
|
||||||
|
serviceManager.stopWatcherService(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDelete(tunnel: TunnelConfig, context: Context) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
val settings = appDataRepository.settings.getSettings()
|
||||||
val isPrimary = tunnel.isPrimaryTunnel
|
val isPrimary = tunnel.isPrimaryTunnel
|
||||||
if (appDataRepository.tunnels.count() == 1 || isPrimary) {
|
if (appDataRepository.tunnels.count() == 1 || isPrimary) {
|
||||||
serviceManager.stopAutoTunnel()
|
stopWatcherService(context)
|
||||||
resetTunnelSetting(settings)
|
resetTunnelSetting(settings)
|
||||||
}
|
}
|
||||||
appDataRepository.tunnels.delete(tunnel)
|
appDataRepository.tunnels.delete(tunnel)
|
||||||
|
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,31 +79,46 @@ constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onExpandedChanged(expanded: Boolean) = viewModelScope.launch {
|
fun onTunnelStart(tunnelConfig: TunnelConfig, context: Context) = viewModelScope.launch {
|
||||||
appDataRepository.appState.setTunnelStatsExpanded(expanded)
|
Timber.d("On start called!")
|
||||||
|
serviceManager.startVpnService(
|
||||||
|
context,
|
||||||
|
tunnelConfig.id,
|
||||||
|
isManualStart = true,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelStart(tunnelConfig: TunnelConfig, background: Boolean) = viewModelScope.launch {
|
fun onTunnelStop(context: Context) = viewModelScope.launch {
|
||||||
Timber.i("Starting tunnel ${tunnelConfig.name}")
|
|
||||||
tunnelService.get().startTunnel(tunnelConfig, background)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelStop(tunnel: TunnelConfig) = viewModelScope.launch {
|
|
||||||
Timber.i("Stopping active tunnel")
|
Timber.i("Stopping active tunnel")
|
||||||
tunnelService.get().stopTunnel(tunnel)
|
serviceManager.stopVpnService(context, isManualStop = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
NumberUtils.generateRandomTunnelName()
|
NumberUtils.generateRandomTunnelName()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCodeTunnelName(config: String): String {
|
private fun generateQrCodeTunnelName(config: String, configType: ConfigType): String {
|
||||||
var defaultName = generateQrCodeDefaultName(config)
|
var defaultName = generateQrCodeDefaultName(config, configType)
|
||||||
val lines = config.lines().toMutableList()
|
val lines = config.lines().toMutableList()
|
||||||
val linesIterator = lines.iterator()
|
val linesIterator = lines.iterator()
|
||||||
while (linesIterator.hasNext()) {
|
while (linesIterator.hasNext()) {
|
||||||
@@ -100,107 +131,193 @@ constructor(
|
|||||||
return defaultName
|
return defaultName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigType.WIREGUARD ->
|
||||||
|
TunnelConfig(
|
||||||
|
name = tunnelName,
|
||||||
|
wgQuick = result,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
addTunnel(tunnelConfig)
|
||||||
|
Result.success(Unit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
Result.failure(WgTunnelExceptions.InvalidQrCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun makeTunnelNameUnique(name: String): String {
|
private suspend fun makeTunnelNameUnique(name: String): String {
|
||||||
return withContext(ioDispatcher) {
|
return withContext(ioDispatcher) {
|
||||||
val tunnels = appDataRepository.tunnels.getAll()
|
val tunnels = appDataRepository.tunnels.getAll()
|
||||||
var tunnelName = name
|
var tunnelName = name
|
||||||
var num = 1
|
var num = 1
|
||||||
while (tunnels.any { it.name == tunnelName }) {
|
while (tunnels.any { it.name == tunnelName }) {
|
||||||
tunnelName = if (!tunnelName.hasNumberInParentheses()) {
|
tunnelName = name + "($num)"
|
||||||
"$name($num)"
|
|
||||||
} else {
|
|
||||||
val pair = tunnelName.extractNameAndNumber()
|
|
||||||
"${pair?.first}($num)"
|
|
||||||
}
|
|
||||||
num++
|
num++
|
||||||
}
|
}
|
||||||
tunnelName
|
tunnelName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun saveTunnelConfigFromStream(stream: InputStream, fileName: String) {
|
private fun saveTunnelConfigFromStream(stream: InputStream, fileName: String, type: ConfigType) {
|
||||||
val amConfig = stream.use { org.amnezia.awg.config.Config.parse(it) }
|
var amQuick: String? = null
|
||||||
val tunnelName = makeTunnelNameUnique(getNameFromFileName(fileName))
|
val wgQuick =
|
||||||
saveTunnel(
|
stream.use {
|
||||||
TunnelConfig(
|
when (type) {
|
||||||
name = tunnelName,
|
ConfigType.AMNEZIA -> {
|
||||||
wgQuick = amConfig.toWgQuickString(),
|
val config = org.amnezia.awg.config.Config.parse(it)
|
||||||
amQuick = amConfig.toAwgQuickString(true),
|
amQuick = config.toAwgQuickString()
|
||||||
),
|
config.toWgQuickString()
|
||||||
)
|
}
|
||||||
|
|
||||||
|
ConfigType.WIREGUARD -> {
|
||||||
|
Config.parse(it).toWgQuickString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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? {
|
private fun getInputStreamFromUri(uri: Uri, context: Context): InputStream? {
|
||||||
return context.applicationContext.contentResolver.openInputStream(uri)
|
return context.applicationContext.contentResolver.openInputStream(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelFileSelected(uri: Uri, context: Context) = viewModelScope.launch(ioDispatcher) {
|
suspend fun onTunnelFileSelected(uri: Uri, configType: ConfigType, context: Context): Result<Unit> {
|
||||||
kotlin.runCatching {
|
return withContext(ioDispatcher) {
|
||||||
if (!isValidUriContentScheme(uri)) throw InvalidFileExtensionException
|
try {
|
||||||
val fileName = getFileName(context, uri)
|
if (isValidUriContentScheme(uri)) {
|
||||||
when (getFileExtensionFromFileName(fileName)) {
|
val fileName = getFileName(context, uri)
|
||||||
Constants.CONF_FILE_EXTENSION ->
|
return@withContext when (getFileExtensionFromFileName(fileName)) {
|
||||||
saveTunnelFromConfUri(fileName, uri, context)
|
Constants.CONF_FILE_EXTENSION ->
|
||||||
Constants.ZIP_FILE_EXTENSION ->
|
saveTunnelFromConfUri(fileName, uri, configType, context)
|
||||||
saveTunnelsFromZipUri(
|
|
||||||
uri,
|
Constants.ZIP_FILE_EXTENSION ->
|
||||||
context,
|
saveTunnelsFromZipUri(
|
||||||
)
|
uri,
|
||||||
else -> throw InvalidFileExtensionException
|
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()
|
||||||
|
config.toWgQuickString()
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigType.WIREGUARD -> {
|
||||||
|
Config.parse(zip).toWgQuickString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 {
|
} else {
|
||||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_file_format))
|
Result.failure(WgTunnelExceptions.FileReadFailed())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onToggleAutoTunnel() = viewModelScope.launch {
|
private fun addTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
val firstTunnel = appDataRepository.tunnels.count() == 0
|
||||||
val toggled = !settings.isAutoTunnelEnabled
|
saveTunnel(tunnelConfig)
|
||||||
if (toggled) {
|
if (firstTunnel) WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
|
||||||
serviceManager.startAutoTunnel(false)
|
}
|
||||||
} else {
|
|
||||||
serviceManager.stopAutoTunnel()
|
fun pauseAutoTunneling() = viewModelScope.launch {
|
||||||
}
|
|
||||||
appDataRepository.settings.save(
|
appDataRepository.settings.save(
|
||||||
settings.copy(
|
uiState.value.settings.copy(isAutoTunnelPaused = true),
|
||||||
isAutoTunnelEnabled = toggled,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
WireGuardAutoTunnel.requestAutoTunnelTileServiceUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
fun resumeAutoTunneling() = viewModelScope.launch {
|
||||||
ZipInputStream(getInputStreamFromUri(uri, context)).use { zip ->
|
appDataRepository.settings.save(
|
||||||
generateSequence { zip.nextEntry }
|
uiState.value.settings.copy(isAutoTunnelPaused = false),
|
||||||
.filterNot {
|
)
|
||||||
it.isDirectory ||
|
WireGuardAutoTunnel.requestAutoTunnelTileServiceUpdate()
|
||||||
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),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setBatteryOptimizeDisableShown() = viewModelScope.launch {
|
|
||||||
appDataRepository.appState.setBatteryOptimizationDisableShown(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri, context: Context) {
|
|
||||||
val stream = getInputStreamFromUri(uri, context) ?: throw FileReadException
|
|
||||||
saveTunnelConfigFromStream(stream, name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
private fun saveTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||||
@@ -208,23 +325,32 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getFileNameByCursor(context: Context, uri: Uri): String? {
|
private fun getFileNameByCursor(context: Context, uri: Uri): String? {
|
||||||
return context.contentResolver.query(uri, null, null, null, null)?.use {
|
context.contentResolver.query(uri, null, null, null, null)?.use {
|
||||||
getDisplayNameByCursor(it)
|
return getDisplayNameByCursor(it)
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDisplayNameColumnIndex(cursor: Cursor): Int? {
|
private fun getDisplayNameColumnIndex(cursor: Cursor): Int? {
|
||||||
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||||
if (columnIndex == -1) return null
|
return if (columnIndex != -1) {
|
||||||
return columnIndex
|
return columnIndex
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDisplayNameByCursor(cursor: Cursor): String? {
|
private fun getDisplayNameByCursor(cursor: Cursor): String? {
|
||||||
val move = cursor.moveToFirst()
|
return if (cursor.moveToFirst()) {
|
||||||
if (!move) return null
|
val index = getDisplayNameColumnIndex(cursor)
|
||||||
val index = getDisplayNameColumnIndex(cursor)
|
if (index != null) {
|
||||||
if (index == null) return index
|
cursor.getString(index)
|
||||||
return cursor.getString(index)
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isValidUriContentScheme(uri: Uri): Boolean {
|
private fun isValidUriContentScheme(uri: Uri): Boolean {
|
||||||
@@ -250,15 +376,14 @@ constructor(
|
|||||||
|
|
||||||
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
|
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
|
||||||
|
|
||||||
fun onCopyTunnel(tunnel: TunnelConfig) = viewModelScope.launch {
|
fun onCopyTunnel(tunnel: TunnelConfig?) = viewModelScope.launch {
|
||||||
saveTunnel(
|
tunnel?.let {
|
||||||
tunnel.copy(
|
saveTunnel(
|
||||||
id = 0,
|
TunnelConfig(
|
||||||
isPrimaryTunnel = false,
|
name = it.name.plus(NumberUtils.randomThree()),
|
||||||
isMobileDataTunnel = false,
|
wgQuick = it.wgQuick,
|
||||||
isActive = false,
|
),
|
||||||
name = makeTunnelNameUnique(tunnel.name),
|
)
|
||||||
),
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-61
@@ -1,61 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Bolt
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.draw.scale
|
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val itemFocusRequester = remember { FocusRequester() }
|
|
||||||
ExpandingRowListItem(
|
|
||||||
leading = {
|
|
||||||
val icon = Icons.Rounded.Bolt
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.size(16.dp.scaledHeight()).scale(1.5f),
|
|
||||||
tint =
|
|
||||||
if (!appUiState.autoTunnelActive) {
|
|
||||||
Color.Gray
|
|
||||||
} else {
|
|
||||||
SilverTree
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
text = stringResource(R.string.auto_tunneling),
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
appUiState.settings.isAutoTunnelEnabled,
|
|
||||||
onClick = {
|
|
||||||
onToggle()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
itemFocusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
isExpanded = false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
-71
@@ -1,71 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.text.ClickableText
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.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 androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun GettingStartedLabel(onClick: (url: String) -> Unit) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(top = 100.dp)
|
|
||||||
.fillMaxSize(),
|
|
||||||
) {
|
|
||||||
val gettingStarted =
|
|
||||||
buildAnnotatedString {
|
|
||||||
append(stringResource(id = R.string.see_the))
|
|
||||||
append(" ")
|
|
||||||
pushStringAnnotation(
|
|
||||||
tag = "gettingStarted",
|
|
||||||
annotation = stringResource(id = R.string.getting_started_url),
|
|
||||||
)
|
|
||||||
withStyle(
|
|
||||||
style = SpanStyle(color = MaterialTheme.colorScheme.primary),
|
|
||||||
) {
|
|
||||||
append(stringResource(id = R.string.getting_started_guide))
|
|
||||||
}
|
|
||||||
pop()
|
|
||||||
append(" ")
|
|
||||||
append(stringResource(R.string.unsure_how))
|
|
||||||
append(".")
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
text = stringResource(R.string.no_tunnels),
|
|
||||||
fontStyle = FontStyle.Italic,
|
|
||||||
)
|
|
||||||
ClickableText(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(vertical = 10.dp, horizontal = 24.dp),
|
|
||||||
text = gettingStarted,
|
|
||||||
style =
|
|
||||||
MaterialTheme.typography.bodyMedium.copy(
|
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
),
|
|
||||||
) {
|
|
||||||
gettingStarted.getStringAnnotations(tag = "gettingStarted", it, it)
|
|
||||||
.firstOrNull()?.let { annotation ->
|
|
||||||
onClick(annotation.item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
|
||||||
import androidx.compose.animation.slideInVertically
|
|
||||||
import androidx.compose.animation.slideOutVertically
|
|
||||||
import androidx.compose.foundation.focusGroup
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.FloatingActionButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ScrollDismissFab(icon: @Composable () -> Unit, isVisible: Boolean, onClick: () -> Unit) {
|
|
||||||
AnimatedVisibility(
|
|
||||||
visible = isVisible,
|
|
||||||
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
|
||||||
exit = slideOutVertically(targetOffsetY = { it * 2 }),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.focusGroup(),
|
|
||||||
) {
|
|
||||||
FloatingActionButton(
|
|
||||||
onClick = {
|
|
||||||
onClick()
|
|
||||||
},
|
|
||||||
shape = RoundedCornerShape(16.dp),
|
|
||||||
containerColor = MaterialTheme.colorScheme.primary,
|
|
||||||
) {
|
|
||||||
icon()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-104
@@ -1,104 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.filled.Create
|
|
||||||
import androidx.compose.material.icons.filled.FileOpen
|
|
||||||
import androidx.compose.material.icons.filled.QrCode
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.HorizontalDivider
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.ModalBottomSheet
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.rememberModalBottomSheetState
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
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.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun TunnelImportSheet(show: Boolean, onDismiss: () -> Unit, onFileClick: () -> Unit, onQrClick: () -> Unit, onManualImportClick: () -> Unit) {
|
|
||||||
val sheetState = rememberModalBottomSheetState()
|
|
||||||
|
|
||||||
val context = LocalContext.current
|
|
||||||
if (show) {
|
|
||||||
ModalBottomSheet(
|
|
||||||
onDismissRequest = {
|
|
||||||
onDismiss()
|
|
||||||
},
|
|
||||||
sheetState = sheetState,
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable {
|
|
||||||
onDismiss()
|
|
||||||
onFileClick()
|
|
||||||
}
|
|
||||||
.padding(10.dp),
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Filled.FileOpen,
|
|
||||||
contentDescription = stringResource(id = R.string.open_file),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.add_tunnels_text),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!context.isRunningOnTv()) {
|
|
||||||
HorizontalDivider()
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable {
|
|
||||||
onDismiss()
|
|
||||||
onQrClick()
|
|
||||||
}
|
|
||||||
.padding(10.dp),
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Filled.QrCode,
|
|
||||||
contentDescription = stringResource(id = R.string.qr_scan),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.add_from_qr),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HorizontalDivider()
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable {
|
|
||||||
onDismiss()
|
|
||||||
onManualImportClick()
|
|
||||||
}
|
|
||||||
.padding(10.dp),
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Filled.Create,
|
|
||||||
contentDescription = stringResource(id = R.string.create_import),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.create_import),
|
|
||||||
modifier = Modifier.padding(10.dp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-198
@@ -1,198 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
|
||||||
|
|
||||||
import androidx.compose.foundation.focusable
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Circle
|
|
||||||
import androidx.compose.material.icons.rounded.CopyAll
|
|
||||||
import androidx.compose.material.icons.rounded.Delete
|
|
||||||
import androidx.compose.material.icons.rounded.Info
|
|
||||||
import androidx.compose.material.icons.rounded.Settings
|
|
||||||
import androidx.compose.material.icons.rounded.Smartphone
|
|
||||||
import androidx.compose.material.icons.rounded.Star
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.focus.focusRequester
|
|
||||||
import androidx.compose.ui.graphics.Color
|
|
||||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asColor
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun TunnelRowItem(
|
|
||||||
isActive: Boolean,
|
|
||||||
expanded: Boolean,
|
|
||||||
isSelected: Boolean,
|
|
||||||
tunnel: TunnelConfig,
|
|
||||||
vpnState: VpnState,
|
|
||||||
onHold: () -> Unit,
|
|
||||||
onClick: () -> Unit,
|
|
||||||
onCopy: () -> Unit,
|
|
||||||
onDelete: () -> Unit,
|
|
||||||
onSwitchClick: (checked: Boolean) -> Unit,
|
|
||||||
) {
|
|
||||||
val leadingIconColor = if (!isActive) Color.Gray else vpnState.statistics.asColor()
|
|
||||||
val context = LocalContext.current
|
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
val haptic = LocalHapticFeedback.current
|
|
||||||
val itemFocusRequester = remember { FocusRequester() }
|
|
||||||
ExpandingRowListItem(
|
|
||||||
leading = {
|
|
||||||
val circleIcon = Icons.Rounded.Circle
|
|
||||||
val icon =
|
|
||||||
if (tunnel.isPrimaryTunnel) {
|
|
||||||
Icons.Rounded.Star
|
|
||||||
} else if (tunnel.isMobileDataTunnel) {
|
|
||||||
Icons.Rounded.Smartphone
|
|
||||||
} else {
|
|
||||||
circleIcon
|
|
||||||
}
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
tint = leadingIconColor,
|
|
||||||
modifier = Modifier.size(16.dp.scaledHeight()),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
text = tunnel.name,
|
|
||||||
onHold = {
|
|
||||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
|
||||||
onHold()
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
if (!context.isRunningOnTv()) {
|
|
||||||
if (isActive) {
|
|
||||||
onClick()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
onHold()
|
|
||||||
itemFocusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
isExpanded = expanded && isActive,
|
|
||||||
expanded = { if (isActive && expanded) TunnelStatisticsRow(vpnState.statistics, tunnel) },
|
|
||||||
trailing = {
|
|
||||||
if (
|
|
||||||
isSelected &&
|
|
||||||
!context.isRunningOnTv()
|
|
||||||
) {
|
|
||||||
Row {
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
navController.navigate(
|
|
||||||
Route.Option(tunnel.id),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Settings
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.focusable(),
|
|
||||||
onClick = { onCopy() },
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.CopyAll
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
IconButton(
|
|
||||||
enabled = !isActive,
|
|
||||||
modifier = Modifier.focusable(),
|
|
||||||
onClick = { onDelete() },
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
Row {
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
onHold()
|
|
||||||
navController.navigate(
|
|
||||||
Route.Option(tunnel.id),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Settings
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
if (isActive) {
|
|
||||||
onClick()
|
|
||||||
} else {
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.turn_on_tunnel),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Info
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
IconButton(
|
|
||||||
onClick = { onCopy() },
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.CopyAll
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
if (isActive) {
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.turn_off_tunnel),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
onHold()
|
|
||||||
onDelete()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ScaledSwitch(
|
|
||||||
modifier = Modifier.focusRequester(itemFocusRequester),
|
|
||||||
checked = isActive,
|
|
||||||
onClick = onSwitchClick,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ScaledSwitch(
|
|
||||||
modifier = Modifier.focusRequester(itemFocusRequester),
|
|
||||||
checked = isActive,
|
|
||||||
onClick = onSwitchClick,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user