mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
80 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4180d39925 | |||
| 6762d4733e | |||
| add18d5cef | |||
| c5b42b55c3 | |||
| 670d9d680c | |||
| bbfc0e2fab | |||
| 708b4c7646 | |||
| efba604c31 | |||
| 1441488053 | |||
| bb3b05d224 | |||
| cda747deee | |||
| c3a2e05eb2 | |||
| a992009c71 | |||
| 57676bf4bb | |||
| 921e33cb70 | |||
| 70649383e0 | |||
| 64a7680b81 | |||
| a81d3a8843 | |||
| bad2f55121 | |||
| fecc55fe9c | |||
| 4d2e9629f9 | |||
| 347c79741f | |||
| 9bb30069fe | |||
| 9a2d77c8bf | |||
| f79f922838 | |||
| a9d5994070 | |||
| f967b38af1 | |||
| a060f00490 | |||
| b15cdbce7c | |||
| 63d46c1817 | |||
| f9ef308f8a | |||
| b2938c3ac8 | |||
| 17b575b7f2 | |||
| 777a948244 | |||
| 72bf0a1979 | |||
| c7a45845d6 | |||
| c2725bf066 | |||
| 2993f1db22 | |||
| e9ae48f96c | |||
| 5cc2ae0d01 | |||
| 7fdec509e9 | |||
| cab2945930 | |||
| 6a90cd02b9 | |||
| 7d810c7c3d | |||
| dad34b9e24 | |||
| fb33b8996f | |||
| ec3a5dcd65 | |||
| 7b443add3a | |||
| d70ef658e2 | |||
| c01b045022 | |||
| d3ea75869a | |||
| 0784c96011 | |||
| 553279ea76 | |||
| 89f6dec357 | |||
| ab7499a616 | |||
| 105c753c66 | |||
| d9f0de2dd4 | |||
| 82280091ad | |||
| b97b7cf989 | |||
| f83e40f6cc | |||
| 1fab9dfdf2 | |||
| a670931b06 | |||
| 2c0c88baf2 | |||
| 05e7cb7c04 | |||
| bc811f74ef | |||
| 09b669f54b | |||
| f08d73cbb7 | |||
| 7ca23d3ef5 | |||
| 7fdd95ea51 | |||
| e11f0f794a | |||
| fea31437cd | |||
| 1306fdc8b2 | |||
| d2e5d6d3bc | |||
| 960af02beb | |||
| 3f1ff22488 | |||
| d6b032845b | |||
| ffad6b331f | |||
| 1fb953e2fe | |||
| 893a03d3b1 | |||
| 30b577a03d |
@@ -1,2 +1,3 @@
|
|||||||
ko_fi: zaneschepke
|
ko_fi: zaneschepke
|
||||||
liberapay: zaneschepke
|
liberapay: zaneschepke
|
||||||
|
github: zaneschepke
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ 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:
|
||||||
|
|||||||
@@ -33,25 +33,29 @@ on:
|
|||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check_date:
|
check_commits:
|
||||||
|
name: Check for New Commits
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Check latest commit
|
|
||||||
outputs:
|
outputs:
|
||||||
should_run: ${{ steps.should_run.outputs.should_run }}
|
has_new_commits: ${{ steps.check.outputs.new_commits }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- name: Checkout Repository
|
||||||
- name: print latest_commit
|
uses: actions/checkout@v3
|
||||||
run: echo ${{ github.sha }}
|
with:
|
||||||
- id: should_run
|
fetch-depth: 0 # This fetches all history so we can check commits
|
||||||
continue-on-error: true
|
|
||||||
name: check latest commit is less than a day
|
- name: Check for new commits
|
||||||
if: ${{ github.event_name == 'schedule' }}
|
id: check
|
||||||
run: test -z $(git rev-list --after="23 hours" ${{ github.sha }}) && echo "::set-output name=should_run::false"
|
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_date
|
needs: check_commits
|
||||||
if: |
|
if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
|
||||||
github.event_name != 'schedule' ||
|
|
||||||
(needs.check_date.outputs.should_run == 'true' && github.event_name == 'schedule')
|
|
||||||
name: Build Signed APK
|
name: Build Signed APK
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
@@ -90,6 +94,32 @@ jobs:
|
|||||||
fileDir: ${{ env.KEY_STORE_LOCATION }}
|
fileDir: ${{ env.KEY_STORE_LOCATION }}
|
||||||
encodedString: ${{ secrets.KEYSTORE }}
|
encodedString: ${{ secrets.KEYSTORE }}
|
||||||
|
|
||||||
|
# update latest tag
|
||||||
|
- name: Set latest tag
|
||||||
|
uses: rickstaa/action-create-tag@v1
|
||||||
|
id: tag_creation
|
||||||
|
with:
|
||||||
|
tag: "latest" # or any tag name you wish to use
|
||||||
|
message: "Automated tag for HEAD commit"
|
||||||
|
force_push_tag: true
|
||||||
|
tag_exists_error: false
|
||||||
|
|
||||||
|
- name: Get latest release
|
||||||
|
id: latest_release
|
||||||
|
uses: kaliber5/action-get-release@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
latest: true
|
||||||
|
|
||||||
|
- name: Generate Changelog
|
||||||
|
id: changelog
|
||||||
|
uses: requarks/changelog-action@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
toTag: ${{ github.event_name == 'schedule' && 'nightly' || steps.latest_release.outputs.tag_name }}
|
||||||
|
fromTag: "latest"
|
||||||
|
writeToFile: false # we won't write to file, just output
|
||||||
|
|
||||||
# create keystore path for gradle to read
|
# create keystore path for gradle to read
|
||||||
- name: Create keystore path env var
|
- name: Create keystore path env var
|
||||||
run: |
|
run: |
|
||||||
@@ -144,7 +174,7 @@ jobs:
|
|||||||
|
|
||||||
# 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.0
|
uses: actions/upload-artifact@v4.4.3
|
||||||
with:
|
with:
|
||||||
name: wgtunnel
|
name: wgtunnel
|
||||||
path: ${{ env.APK_PATH }}
|
path: ${{ env.APK_PATH }}
|
||||||
@@ -205,6 +235,9 @@ jobs:
|
|||||||
|
|
||||||
SHA256 fingerprint:
|
SHA256 fingerprint:
|
||||||
```${{ steps.checksum.outputs.checksum }}```
|
```${{ steps.checksum.outputs.checksum }}```
|
||||||
|
|
||||||
|
### Changelog
|
||||||
|
${{ steps.changelog.outputs.changes }}
|
||||||
tag_name: ${{ env.TAG_NAME }}
|
tag_name: ${{ env.TAG_NAME }}
|
||||||
name: ${{ env.TAG_NAME }}
|
name: ${{ env.TAG_NAME }}
|
||||||
draft: false
|
draft: false
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ WG Tunnel
|
|||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
[](https://discord.gg/rbRRNh6H7V)
|
[](https://discord.gg/rbRRNh6H7V)
|
||||||
[](https://twitter.com/i/communities/1780655267685736818)
|
|
||||||
[](https://t.me/wgtunnel)
|
[](https://t.me/wgtunnel)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ 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 }
|
||||||
}
|
}
|
||||||
@@ -72,12 +74,14 @@ 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 {
|
debug {
|
||||||
applicationIdSuffix = ".debug"
|
applicationIdSuffix = ".debug"
|
||||||
versionNameSuffix = "-debug"
|
versionNameSuffix = "-debug"
|
||||||
resValue("string", "app_name", "WG Tunnel - Debug")
|
resValue("string", "app_name", "WG Tunnel - Debug")
|
||||||
isDebuggable = true
|
isDebuggable = true
|
||||||
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
create(Constants.PRERELEASE) {
|
create(Constants.PRERELEASE) {
|
||||||
@@ -85,6 +89,7 @@ android {
|
|||||||
applicationIdSuffix = ".prerelease"
|
applicationIdSuffix = ".prerelease"
|
||||||
versionNameSuffix = "-pre"
|
versionNameSuffix = "-pre"
|
||||||
resValue("string", "app_name", "WG Tunnel - Pre")
|
resValue("string", "app_name", "WG Tunnel - Pre")
|
||||||
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
create(Constants.NIGHTLY) {
|
create(Constants.NIGHTLY) {
|
||||||
@@ -92,6 +97,7 @@ android {
|
|||||||
applicationIdSuffix = ".nightly"
|
applicationIdSuffix = ".nightly"
|
||||||
versionNameSuffix = "-nightly"
|
versionNameSuffix = "-nightly"
|
||||||
resValue("string", "app_name", "WG Tunnel - Nightly")
|
resValue("string", "app_name", "WG Tunnel - Nightly")
|
||||||
|
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
applicationVariants.all {
|
applicationVariants.all {
|
||||||
@@ -129,8 +135,6 @@ 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"))
|
||||||
@@ -147,6 +151,7 @@ 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)
|
||||||
|
|||||||
@@ -0,0 +1,232 @@
|
|||||||
|
{
|
||||||
|
"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')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,246 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 12,
|
||||||
|
"identityHash": "acf79ac5defacda5be6c3f976e777de3",
|
||||||
|
"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, `is_stop_on_no_internet_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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isStopOnNoInternetEnabled",
|
||||||
|
"columnName": "is_stop_on_no_internet_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, `is_ethernet_tunnel` 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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isEthernetTunnel",
|
||||||
|
"columnName": "is_ethernet_tunnel",
|
||||||
|
"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, 'acf79ac5defacda5be6c3f976e777de3')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,267 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 13,
|
||||||
|
"identityHash": "ff209157b98a641c424f5086818ec585",
|
||||||
|
"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, `is_stop_on_no_internet_enabled` INTEGER NOT NULL DEFAULT false, `is_vpn_kill_switch_enabled` INTEGER NOT NULL DEFAULT false, `is_kernel_kill_switch_enabled` INTEGER NOT NULL DEFAULT false, `is_lan_on_kill_switch_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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isStopOnNoInternetEnabled",
|
||||||
|
"columnName": "is_stop_on_no_internet_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true,
|
||||||
|
"defaultValue": "false"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isVpnKillSwitchEnabled",
|
||||||
|
"columnName": "is_vpn_kill_switch_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true,
|
||||||
|
"defaultValue": "false"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isKernelKillSwitchEnabled",
|
||||||
|
"columnName": "is_kernel_kill_switch_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true,
|
||||||
|
"defaultValue": "false"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isLanOnKillSwitchEnabled",
|
||||||
|
"columnName": "is_lan_on_kill_switch_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, `is_ethernet_tunnel` 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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isEthernetTunnel",
|
||||||
|
"columnName": "is_ethernet_tunnel",
|
||||||
|
"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, 'ff209157b98a641c424f5086818ec585')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,17 +3,7 @@
|
|||||||
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
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
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" />
|
||||||
@@ -22,7 +12,8 @@
|
|||||||
<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" />
|
||||||
@@ -49,6 +40,12 @@
|
|||||||
<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>
|
||||||
@@ -66,12 +63,14 @@
|
|||||||
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.AppSplashScreen"
|
android:theme="@style/Theme.App.Start"
|
||||||
tools:targetApi="tiramisu">
|
tools:targetApi="tiramisu">
|
||||||
<activity
|
<activity
|
||||||
android:name=".ui.SplashActivity"
|
android:name=".ui.MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:theme="@style/Theme.AppSplashScreen">
|
android:theme="@style/Theme.WireguardAutoTunnel"
|
||||||
|
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||||
|
>
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
@@ -83,11 +82,6 @@
|
|||||||
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"
|
||||||
@@ -103,6 +97,16 @@
|
|||||||
android:launchMode="singleInstance"
|
android:launchMode="singleInstance"
|
||||||
android:theme="@android:style/Theme.NoDisplay" />
|
android:theme="@android:style/Theme.NoDisplay" />
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
android:authorities="@string/provider"
|
||||||
|
android:exported="false"
|
||||||
|
android:grantUriPermissions="true">
|
||||||
|
<meta-data
|
||||||
|
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"
|
||||||
@@ -153,7 +157,7 @@
|
|||||||
android:value="true" />
|
android:value="true" />
|
||||||
</service>
|
</service>
|
||||||
<service
|
<service
|
||||||
android:name=".service.foreground.AutoTunnelService"
|
android:name=".service.foreground.autotunnel.AutoTunnelService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:foregroundServiceType="systemExempted"
|
android:foregroundServiceType="systemExempted"
|
||||||
@@ -164,6 +168,7 @@
|
|||||||
<service
|
<service
|
||||||
android:name=".service.foreground.TunnelBackgroundService"
|
android:name=".service.foreground.TunnelBackgroundService"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
|
android:persistent="true"
|
||||||
android:foregroundServiceType="systemExempted"
|
android:foregroundServiceType="systemExempted"
|
||||||
android:permission="android.permission.BIND_VPN_SERVICE">
|
android:permission="android.permission.BIND_VPN_SERVICE">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
@@ -184,10 +189,6 @@
|
|||||||
<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
|
|
||||||
android:name=".receiver.BackgroundActionReceiver"
|
|
||||||
android:enabled="true"
|
|
||||||
android:exported="false"/>
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".receiver.AppUpdateReceiver"
|
android:name=".receiver.AppUpdateReceiver"
|
||||||
android:exported="false">
|
android:exported="false">
|
||||||
@@ -203,5 +204,10 @@
|
|||||||
<action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" />
|
<action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
|
<receiver
|
||||||
|
android:name=".receiver.NotificationActionReceiver"
|
||||||
|
android:exported="false"
|
||||||
|
android:permission="${applicationId}.permission.CONTROL_TUNNELS">
|
||||||
|
</receiver>
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -3,15 +3,22 @@ package com.zaneschepke.wireguardautotunnel
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.os.StrictMode
|
import android.os.StrictMode
|
||||||
import android.os.StrictMode.ThreadPolicy
|
import android.os.StrictMode.ThreadPolicy
|
||||||
import com.zaneschepke.logcatter.LocalLogCollector
|
import com.zaneschepke.logcatter.LogReader
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
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.MainDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.BackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
|
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 com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import dagger.hilt.android.HiltAndroidApp
|
import dagger.hilt.android.HiltAndroidApp
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -23,12 +30,25 @@ class WireGuardAutoTunnel : Application() {
|
|||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var localLogCollector: LocalLogCollector
|
lateinit var logReader: LogReader
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var appStateRepository: AppStateRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var tunnelService: TunnelService
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@IoDispatcher
|
@IoDispatcher
|
||||||
lateinit var ioDispatcher: CoroutineDispatcher
|
lateinit var ioDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@MainDispatcher
|
||||||
|
lateinit var mainDispatcher: CoroutineDispatcher
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
instance = this
|
instance = this
|
||||||
@@ -45,13 +65,28 @@ class WireGuardAutoTunnel : Application() {
|
|||||||
} else {
|
} else {
|
||||||
Timber.plant(ReleaseTree())
|
Timber.plant(ReleaseTree())
|
||||||
}
|
}
|
||||||
if (!isRunningOnTv()) {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
applicationScope.launch {
|
||||||
localLogCollector.start()
|
withContext(mainDispatcher) {
|
||||||
|
if (appStateRepository.isLocalLogsEnabled() && !isRunningOnTv()) logReader.initialize()
|
||||||
|
}
|
||||||
|
if (!settingsRepository.getSettings().isKernelEnabled) {
|
||||||
|
tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
appStateRepository.getLocale()?.let {
|
||||||
|
LocaleUtil.changeLocale(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onTerminate() {
|
||||||
|
applicationScope.launch {
|
||||||
|
tunnelService.setBackendState(BackendState.INACTIVE, emptyList())
|
||||||
|
}
|
||||||
|
super.onTerminate()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
lateinit var instance: WireGuardAutoTunnel
|
lateinit var instance: WireGuardAutoTunnel
|
||||||
private set
|
private set
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
|
|
||||||
@Database(
|
@Database(
|
||||||
entities = [Settings::class, TunnelConfig::class],
|
entities = [Settings::class, TunnelConfig::class],
|
||||||
version = 10,
|
version = 13,
|
||||||
autoMigrations =
|
autoMigrations =
|
||||||
[
|
[
|
||||||
AutoMigration(from = 1, to = 2),
|
AutoMigration(from = 1, to = 2),
|
||||||
@@ -36,6 +36,19 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
AutoMigration(7, 8),
|
AutoMigration(7, 8),
|
||||||
AutoMigration(8, 9),
|
AutoMigration(8, 9),
|
||||||
AutoMigration(9, 10),
|
AutoMigration(9, 10),
|
||||||
|
AutoMigration(
|
||||||
|
from = 10,
|
||||||
|
to = 11,
|
||||||
|
spec = RemoveTunnelPauseMigration::class,
|
||||||
|
),
|
||||||
|
AutoMigration(
|
||||||
|
from = 11,
|
||||||
|
to = 12,
|
||||||
|
),
|
||||||
|
AutoMigration(
|
||||||
|
from = 12,
|
||||||
|
to = 13,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
exportSchema = true,
|
exportSchema = true,
|
||||||
)
|
)
|
||||||
@@ -55,3 +68,9 @@ 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
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ object Queries {
|
|||||||
VALUES
|
VALUES
|
||||||
('false',
|
('false',
|
||||||
'false',
|
'false',
|
||||||
'sampleSSID1,sampleSSID2',
|
'',
|
||||||
'false',
|
'false',
|
||||||
'false',
|
'false',
|
||||||
'false',
|
'false',
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ interface TunnelConfigDao {
|
|||||||
@Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1")
|
@Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1")
|
||||||
suspend fun resetMobileDataTunnel()
|
suspend fun resetMobileDataTunnel()
|
||||||
|
|
||||||
|
@Query("UPDATE TunnelConfig SET is_ethernet_tunnel = 0 WHERE is_ethernet_tunnel =1")
|
||||||
|
suspend fun resetEthernetTunnel()
|
||||||
|
|
||||||
@Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1")
|
@Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1")
|
||||||
suspend fun findByPrimary(): TunnelConfigs
|
suspend fun findByPrimary(): TunnelConfigs
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -21,10 +21,14 @@ class DataStoreManager(
|
|||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
val locationDisclosureShown = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
||||||
val BATTERY_OPTIMIZE_DISABLE_SHOWN = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
|
val batteryDisableShown = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
|
||||||
val CURRENT_SSID = stringPreferencesKey("CURRENT_SSID")
|
val currentSSID = stringPreferencesKey("CURRENT_SSID")
|
||||||
val IS_PIN_LOCK_ENABLED = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
||||||
|
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
|
||||||
|
val isLocalLogsEnabled = booleanPreferencesKey("LOCAL_LOGS_ENABLED")
|
||||||
|
val locale = stringPreferencesKey("LOCALE")
|
||||||
|
val theme = stringPreferencesKey("THEME")
|
||||||
}
|
}
|
||||||
|
|
||||||
// preferences
|
// preferences
|
||||||
|
|||||||
@@ -1,13 +1,21 @@
|
|||||||
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 isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
|
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
|
||||||
|
val isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED,
|
||||||
|
val isLocalLogsEnabled: Boolean = IS_LOGS_ENABLED_DEFAULT,
|
||||||
|
val locale: String? = 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 PIN_LOCK_ENABLED_DEFAULT = false
|
const val PIN_LOCK_ENABLED_DEFAULT = false
|
||||||
|
const val IS_TUNNEL_STATS_EXPANDED = false
|
||||||
|
const val IS_LOGS_ENABLED_DEFAULT = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,11 +40,6 @@ 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",
|
||||||
@@ -55,4 +50,34 @@ 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,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_stop_on_no_internet_enabled",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
val isStopOnNoInternetEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_vpn_kill_switch_enabled",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
val isVpnKillSwitchEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_kernel_kill_switch_enabled",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
val isKernelKillSwitchEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_lan_on_kill_switch_enabled",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
val isLanOnKillSwitchEnabled: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import androidx.room.Entity
|
|||||||
import androidx.room.Index
|
import androidx.room.Index
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
@Entity(indices = [Index(value = ["name"], unique = true)])
|
@Entity(indices = [Index(value = ["name"], unique = true)])
|
||||||
@@ -57,6 +58,11 @@ data class TunnelConfig(
|
|||||||
defaultValue = "null",
|
defaultValue = "null",
|
||||||
)
|
)
|
||||||
var pingIp: String? = null,
|
var pingIp: String? = null,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_ethernet_tunnel",
|
||||||
|
defaultValue = "false",
|
||||||
|
)
|
||||||
|
var isEthernetTunnel: Boolean = false,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun toAmConfig(): org.amnezia.awg.config.Config {
|
fun toAmConfig(): org.amnezia.awg.config.Config {
|
||||||
@@ -79,6 +85,21 @@ data class TunnelConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun tunnelConfigFromAmConfig(config: org.amnezia.awg.config.Config, name: String): TunnelConfig {
|
||||||
|
val amQuick = config.toAwgQuickString(true)
|
||||||
|
val wgQuick = config.toWgQuickString()
|
||||||
|
return TunnelConfig(name = name, wgQuick = wgQuick, amQuick = amQuick)
|
||||||
|
}
|
||||||
|
|
||||||
const val AM_QUICK_DEFAULT = ""
|
const val AM_QUICK_DEFAULT = ""
|
||||||
|
|
||||||
|
val IPV4_PUBLIC_NETWORKS = setOf(
|
||||||
|
"0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3",
|
||||||
|
"64.0.0.0/2", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/6", "172.0.0.0/12",
|
||||||
|
"172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7",
|
||||||
|
"176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16",
|
||||||
|
"192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10",
|
||||||
|
"193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-2
@@ -1,6 +1,7 @@
|
|||||||
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 {
|
||||||
@@ -16,9 +17,21 @@ interface AppStateRepository {
|
|||||||
|
|
||||||
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
||||||
|
|
||||||
suspend fun getCurrentSsid(): String?
|
suspend fun isTunnelStatsExpanded(): Boolean
|
||||||
|
|
||||||
suspend fun setCurrentSsid(ssid: String)
|
suspend fun setTunnelStatsExpanded(expanded: Boolean)
|
||||||
|
|
||||||
|
suspend fun setTheme(theme: Theme)
|
||||||
|
|
||||||
|
suspend fun getTheme(): Theme
|
||||||
|
|
||||||
|
suspend fun isLocalLogsEnabled(): Boolean
|
||||||
|
|
||||||
|
suspend fun setLocalLogsEnabled(enabled: Boolean)
|
||||||
|
|
||||||
|
suspend fun setLocale(localeTag: String)
|
||||||
|
|
||||||
|
suspend fun getLocale(): String?
|
||||||
|
|
||||||
val generalStateFlow: Flow<GeneralState>
|
val generalStateFlow: Flow<GeneralState>
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-13
@@ -2,6 +2,7 @@ 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
|
||||||
@@ -11,38 +12,69 @@ class DataStoreAppStateRepository(
|
|||||||
) :
|
) :
|
||||||
AppStateRepository {
|
AppStateRepository {
|
||||||
override suspend fun isLocationDisclosureShown(): Boolean {
|
override suspend fun isLocationDisclosureShown(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN)
|
return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown)
|
||||||
?: 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.LOCATION_DISCLOSURE_SHOWN, shown)
|
dataStoreManager.saveToDataStore(DataStoreManager.locationDisclosureShown, shown)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun isPinLockEnabled(): Boolean {
|
override suspend fun isPinLockEnabled(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.IS_PIN_LOCK_ENABLED)
|
return dataStoreManager.getFromStore(DataStoreManager.pinLockEnabled)
|
||||||
?: 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.IS_PIN_LOCK_ENABLED, enabled)
|
dataStoreManager.saveToDataStore(DataStoreManager.pinLockEnabled, enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun isBatteryOptimizationDisableShown(): Boolean {
|
override suspend fun isBatteryOptimizationDisableShown(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN)
|
return dataStoreManager.getFromStore(DataStoreManager.batteryDisableShown)
|
||||||
?: 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.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown)
|
dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentSsid(): String? {
|
override suspend fun isTunnelStatsExpanded(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID)
|
return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded)
|
||||||
|
?: GeneralState.IS_TUNNEL_STATS_EXPANDED
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun setCurrentSsid(ssid: String) {
|
override suspend fun setTunnelStatsExpanded(expanded: Boolean) {
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid)
|
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 suspend fun isLocalLogsEnabled(): Boolean {
|
||||||
|
return dataStoreManager.getFromStore(DataStoreManager.isLocalLogsEnabled) ?: GeneralState.IS_LOGS_ENABLED_DEFAULT
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun setLocalLogsEnabled(enabled: Boolean) {
|
||||||
|
dataStoreManager.saveToDataStore(DataStoreManager.isLocalLogsEnabled, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun setLocale(localeTag: String) {
|
||||||
|
dataStoreManager.saveToDataStore(DataStoreManager.locale, localeTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getLocale(): String? {
|
||||||
|
return dataStoreManager.getFromStore(DataStoreManager.locale)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val generalStateFlow: Flow<GeneralState> =
|
override val generalStateFlow: Flow<GeneralState> =
|
||||||
@@ -51,14 +83,18 @@ class DataStoreAppStateRepository(
|
|||||||
try {
|
try {
|
||||||
GeneralState(
|
GeneralState(
|
||||||
isLocationDisclosureShown =
|
isLocationDisclosureShown =
|
||||||
pref[DataStoreManager.LOCATION_DISCLOSURE_SHOWN]
|
pref[DataStoreManager.locationDisclosureShown]
|
||||||
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
|
||||||
isBatteryOptimizationDisableShown =
|
isBatteryOptimizationDisableShown =
|
||||||
pref[DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN]
|
pref[DataStoreManager.batteryDisableShown]
|
||||||
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
|
||||||
isPinLockEnabled =
|
isPinLockEnabled =
|
||||||
pref[DataStoreManager.IS_PIN_LOCK_ENABLED]
|
pref[DataStoreManager.pinLockEnabled]
|
||||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
||||||
|
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
|
||||||
|
isLocalLogsEnabled = pref[DataStoreManager.isLocalLogsEnabled] ?: GeneralState.IS_LOGS_ENABLED_DEFAULT,
|
||||||
|
locale = pref[DataStoreManager.locale],
|
||||||
|
theme = getTheme(),
|
||||||
)
|
)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
|
|||||||
+13
-6
@@ -1,11 +1,9 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.repository
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
|
||||||
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.module.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -26,8 +24,6 @@ class RoomTunnelConfigRepository(
|
|||||||
override suspend fun save(tunnelConfig: TunnelConfig) {
|
override suspend fun save(tunnelConfig: TunnelConfig) {
|
||||||
withContext(ioDispatcher) {
|
withContext(ioDispatcher) {
|
||||||
tunnelConfigDao.save(tunnelConfig)
|
tunnelConfigDao.save(tunnelConfig)
|
||||||
}.also {
|
|
||||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,11 +53,22 @@ class RoomTunnelConfigRepository(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun updateEthernetTunnel(tunnelConfig: TunnelConfig?) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
tunnelConfigDao.resetEthernetTunnel()
|
||||||
|
tunnelConfig?.let {
|
||||||
|
save(
|
||||||
|
it.copy(
|
||||||
|
isEthernetTunnel = true,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun delete(tunnelConfig: TunnelConfig) {
|
override suspend fun delete(tunnelConfig: TunnelConfig) {
|
||||||
withContext(ioDispatcher) {
|
withContext(ioDispatcher) {
|
||||||
tunnelConfigDao.delete(tunnelConfig)
|
tunnelConfigDao.delete(tunnelConfig)
|
||||||
}.also {
|
|
||||||
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
@@ -15,6 +15,8 @@ interface TunnelConfigRepository {
|
|||||||
|
|
||||||
suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?)
|
suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?)
|
||||||
|
|
||||||
|
suspend fun updateEthernetTunnel(tunnelConfig: TunnelConfig?)
|
||||||
|
|
||||||
suspend fun delete(tunnelConfig: TunnelConfig)
|
suspend fun delete(tunnelConfig: TunnelConfig)
|
||||||
|
|
||||||
suspend fun getById(id: Int): TunnelConfig?
|
suspend fun getById(id: Int): TunnelConfig?
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.module
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.zaneschepke.logcatter.LocalLogCollector
|
import com.zaneschepke.logcatter.LogReader
|
||||||
import com.zaneschepke.logcatter.LogcatUtil
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.logcatter.LogcatReader
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -24,7 +26,13 @@ class AppModule {
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideLogCollect(@ApplicationContext context: Context): LocalLogCollector {
|
fun provideLogCollect(@ApplicationContext context: Context): LogReader {
|
||||||
return LogcatUtil.init(context = context)
|
return LogcatReader.init(storageDir = context.filesDir.absolutePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideNotificationService(@ApplicationContext context: Context): NotificationService {
|
||||||
|
return WireGuardNotification(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,3 +9,11 @@ annotation class Kernel
|
|||||||
@Qualifier
|
@Qualifier
|
||||||
@Retention(AnnotationRetention.BINARY)
|
@Retention(AnnotationRetention.BINARY)
|
||||||
annotation class Userspace
|
annotation class Userspace
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class TunnelShell
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class AppShell
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.module
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.NavigationService
|
|
||||||
import dagger.Module
|
|
||||||
import dagger.Provides
|
|
||||||
import dagger.hilt.InstallIn
|
|
||||||
import dagger.hilt.android.components.ActivityRetainedComponent
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import dagger.hilt.android.scopes.ActivityRetainedScoped
|
|
||||||
|
|
||||||
@Module
|
|
||||||
@InstallIn(ActivityRetainedComponent::class)
|
|
||||||
object NavigationModule {
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@ActivityRetainedScoped
|
|
||||||
fun provideNestedNavController(@ApplicationContext context: Context): NavHostController {
|
|
||||||
return NavigationService(context).navController
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,8 +4,6 @@ import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
|||||||
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
|
||||||
import dagger.Binds
|
import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -15,10 +13,6 @@ import dagger.hilt.android.scopes.ServiceScoped
|
|||||||
@Module
|
@Module
|
||||||
@InstallIn(ServiceComponent::class)
|
@InstallIn(ServiceComponent::class)
|
||||||
abstract class ServiceModule {
|
abstract class ServiceModule {
|
||||||
@Binds
|
|
||||||
@ServiceScoped
|
|
||||||
abstract fun provideNotificationService(wireGuardNotification: WireGuardNotification): NotificationService
|
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@ServiceScoped
|
||||||
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
|
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ 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.data.repository.TunnelConfigRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
@@ -24,9 +26,18 @@ import javax.inject.Singleton
|
|||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
class TunnelModule {
|
class TunnelModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideRootShell(@ApplicationContext context: Context): RootShell {
|
@TunnelShell
|
||||||
|
fun provideTunnelRootShell(@ApplicationContext context: Context): RootShell {
|
||||||
|
return RootShell(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
@AppShell
|
||||||
|
fun provideAppRootShell(@ApplicationContext context: Context): RootShell {
|
||||||
return RootShell(context)
|
return RootShell(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,14 +50,14 @@ class TunnelModule {
|
|||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
@Userspace
|
@Userspace
|
||||||
fun provideUserspaceBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
|
fun provideUserspaceBackend(@ApplicationContext context: Context, @TunnelShell rootShell: RootShell): Backend {
|
||||||
return GoBackend(context, RootTunnelActionHandler(rootShell))
|
return GoBackend(context, RootTunnelActionHandler(rootShell))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
@Kernel
|
@Kernel
|
||||||
fun provideKernelBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
|
fun provideKernelBackend(@ApplicationContext context: Context, @TunnelShell rootShell: RootShell): Backend {
|
||||||
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell), RootTunnelActionHandler(rootShell))
|
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell), RootTunnelActionHandler(rootShell))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +76,8 @@ class TunnelModule {
|
|||||||
tunnelConfigRepository: TunnelConfigRepository,
|
tunnelConfigRepository: TunnelConfigRepository,
|
||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
|
serviceManager: ServiceManager,
|
||||||
|
notificationService: NotificationService,
|
||||||
): TunnelService {
|
): TunnelService {
|
||||||
return WireGuardTunnel(
|
return WireGuardTunnel(
|
||||||
amneziaBackend,
|
amneziaBackend,
|
||||||
@@ -73,6 +86,18 @@ class TunnelModule {
|
|||||||
appDataRepository,
|
appDataRepository,
|
||||||
applicationScope,
|
applicationScope,
|
||||||
ioDispatcher,
|
ioDispatcher,
|
||||||
|
serviceManager,
|
||||||
|
notificationService,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideServiceManager(
|
||||||
|
@ApplicationContext context: Context,
|
||||||
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
|
appDataRepository: AppDataRepository,
|
||||||
|
): ServiceManager {
|
||||||
|
return ServiceManager(context, ioDispatcher, appDataRepository)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ 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.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
|
||||||
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 AppUpdateReceiver : BroadcastReceiver() {
|
class AppUpdateReceiver : BroadcastReceiver() {
|
||||||
@@ -25,7 +25,10 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
|||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: TunnelService
|
lateinit var tunnelService: Provider<TunnelService>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
|
if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
|
||||||
@@ -33,11 +36,11 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
|||||||
val settings = appDataRepository.settings.getSettings()
|
val settings = appDataRepository.settings.getSettings()
|
||||||
if (settings.isAutoTunnelEnabled) {
|
if (settings.isAutoTunnelEnabled) {
|
||||||
Timber.i("Restarting services after upgrade")
|
Timber.i("Restarting services after upgrade")
|
||||||
ServiceManager.startWatcherServiceForeground(context)
|
serviceManager.startAutoTunnel(true)
|
||||||
}
|
}
|
||||||
if (!settings.isAutoTunnelEnabled || settings.isAutoTunnelPaused) {
|
if (!settings.isAutoTunnelEnabled) {
|
||||||
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
||||||
if (tunnels.isNotEmpty()) context.startTunnelBackground(tunnels.first().id)
|
if (tunnels.isNotEmpty()) tunnelService.get().startTunnel(tunnels.first(), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-61
@@ -1,61 +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.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 BackgroundActionReceiver : BroadcastReceiver() {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@ApplicationScope
|
|
||||||
lateinit var applicationScope: CoroutineScope
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
|
||||||
val id = intent.getIntExtra(TUNNEL_ID_EXTRA_KEY, 0)
|
|
||||||
if (id == 0) return
|
|
||||||
when (intent.action) {
|
|
||||||
ACTION_CONNECT -> {
|
|
||||||
Timber.d("Connect actions")
|
|
||||||
applicationScope.launch {
|
|
||||||
val tunnel = tunnelConfigRepository.getById(id)
|
|
||||||
tunnel?.let {
|
|
||||||
ServiceManager.startTunnelBackgroundService(context)
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ACTION_DISCONNECT -> {
|
|
||||||
applicationScope.launch {
|
|
||||||
val tunnel = tunnelConfigRepository.getById(id)
|
|
||||||
tunnel?.let {
|
|
||||||
ServiceManager.stopTunnelBackgroundService(context)
|
|
||||||
tunnelService.get().stopTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val ACTION_CONNECT = "ACTION_CONNECT"
|
|
||||||
const val ACTION_DISCONNECT = "ACTION_DISCONNECT"
|
|
||||||
const val TUNNEL_ID_EXTRA_KEY = "tunnelId"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,6 @@ 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.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
|
||||||
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
|
||||||
@@ -28,6 +27,9 @@ class BootReceiver : BroadcastReceiver() {
|
|||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (Intent.ACTION_BOOT_COMPLETED != intent.action) return
|
if (Intent.ACTION_BOOT_COMPLETED != intent.action) return
|
||||||
applicationScope.launch {
|
applicationScope.launch {
|
||||||
@@ -37,11 +39,11 @@ class BootReceiver : BroadcastReceiver() {
|
|||||||
val tunState = tunnelService.get().vpnState.value.status
|
val tunState = tunnelService.get().vpnState.value.status
|
||||||
if (activeTunnels.isNotEmpty() && tunState != TunnelState.UP) {
|
if (activeTunnels.isNotEmpty() && tunState != TunnelState.UP) {
|
||||||
Timber.i("Starting previously active tunnel")
|
Timber.i("Starting previously active tunnel")
|
||||||
context.startTunnelBackground(activeTunnels.first().id)
|
tunnelService.get().startTunnel(activeTunnels.first(), true)
|
||||||
}
|
}
|
||||||
if (isAutoTunnelEnabled) {
|
if (isAutoTunnelEnabled) {
|
||||||
Timber.i("Starting watcher service from boot")
|
Timber.i("Starting watcher service from boot")
|
||||||
ServiceManager.startWatcherServiceForeground(context)
|
serviceManager.startAutoTunnel(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
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.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
|
||||||
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
|
||||||
@@ -26,6 +26,9 @@ class KernelReceiver : BroadcastReceiver() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
val action = intent.action ?: return
|
val action = intent.action ?: return
|
||||||
applicationScope.launch {
|
applicationScope.launch {
|
||||||
@@ -37,7 +40,7 @@ class KernelReceiver : BroadcastReceiver() {
|
|||||||
tunnelConfigRepository.save(it.copy(isActive = true))
|
tunnelConfigRepository.save(it.copy(isActive = true))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
serviceManager.updateTunnelTile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.receiver
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class NotificationActionReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var tunnelService: TunnelService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@ApplicationScope
|
||||||
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
applicationScope.launch {
|
||||||
|
when (intent.action) {
|
||||||
|
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
|
||||||
|
NotificationAction.TUNNEL_OFF.name -> tunnelService.stopTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
enum class Action {
|
|
||||||
START,
|
|
||||||
START_FOREGROUND,
|
|
||||||
STOP,
|
|
||||||
STOP_FOREGROUND,
|
|
||||||
}
|
|
||||||
-577
@@ -1,577 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.IBinder
|
|
||||||
import android.os.PowerManager
|
|
||||||
import androidx.core.app.ServiceCompat
|
|
||||||
import androidx.lifecycle.LifecycleService
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
|
||||||
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.isMatchingToWildcardList
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
|
||||||
import kotlinx.coroutines.flow.combine
|
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
||||||
import kotlinx.coroutines.flow.update
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
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 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
|
|
||||||
@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
|
|
||||||
|
|
||||||
@get:Synchronized @set:Synchronized
|
|
||||||
private var running: Boolean = false
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
launchNotification()
|
|
||||||
}.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")
|
|
||||||
if (intent != null) {
|
|
||||||
val action = intent.action
|
|
||||||
when (action) {
|
|
||||||
Action.START.name,
|
|
||||||
Action.START_FOREGROUND.name,
|
|
||||||
-> startService()
|
|
||||||
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun launchNotification() {
|
|
||||||
if (appDataRepository.settings.getSettings().isAutoTunnelPaused) {
|
|
||||||
launchWatcherPausedNotification()
|
|
||||||
} else {
|
|
||||||
launchWatcherNotification()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun startService() {
|
|
||||||
if (running) return
|
|
||||||
running = true
|
|
||||||
kotlin.runCatching {
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
launchNotification()
|
|
||||||
initWakeLock()
|
|
||||||
}
|
|
||||||
startSettingsJob()
|
|
||||||
startVpnStateJob()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun stopService() {
|
|
||||||
wakeLock?.let {
|
|
||||||
if (it.isHeld) {
|
|
||||||
it.release()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stopSelf()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
cancelAndResetNetworkJobs()
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
|
|
||||||
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 {
|
|
||||||
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
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (vpnState.status == TunnelState.UP && !settings.isAutoTunnelPaused) {
|
|
||||||
if (vpnState.tunnelConfig != null) {
|
|
||||||
val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick)
|
|
||||||
val results = if (vpnState.tunnelConfig.pingIp != null) {
|
|
||||||
Timber.d("Pinging custom ip : ${vpnState.tunnelConfig.pingIp}")
|
|
||||||
listOf(InetAddress.getByName(vpnState.tunnelConfig.pingIp).isReachable(Constants.PING_TIMEOUT.toInt()))
|
|
||||||
} else {
|
|
||||||
Timber.d("Pinging all peers")
|
|
||||||
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 fun onAutoTunnelPause(paused: Boolean) {
|
|
||||||
if (autoTunnelStateFlow.value.settings.isAutoTunnelPaused
|
|
||||||
!= paused
|
|
||||||
) {
|
|
||||||
when (paused) {
|
|
||||||
true -> launchWatcherPausedNotification()
|
|
||||||
false -> launchWatcherNotification()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForSettingsChanges() {
|
|
||||||
Timber.i("Starting settings watcher")
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
appDataRepository.settings.getSettingsFlow().combine(
|
|
||||||
// ignore isActive changes to allow manual tunnel overrides
|
|
||||||
appDataRepository.tunnels.getTunnelConfigsFlow().distinctUntilChanged { old, new ->
|
|
||||||
old.map { it.isActive } != new.map { it.isActive }
|
|
||||||
},
|
|
||||||
) { settings, tunnels ->
|
|
||||||
autoTunnelStateFlow.value.copy(
|
|
||||||
settings = settings,
|
|
||||||
tunnels = tunnels,
|
|
||||||
)
|
|
||||||
}.collect {
|
|
||||||
onAutoTunnelPause(it.settings.isAutoTunnelPaused)
|
|
||||||
manageJobsBySettings(it.settings)
|
|
||||||
autoTunnelStateFlow.emit(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForVpnStateChanges() {
|
|
||||||
Timber.i("Starting vpn state watcher")
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
tunnelService.get().vpnState.collect { state ->
|
|
||||||
state.tunnelConfig?.let {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
|
||||||
}
|
|
||||||
if (!it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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")
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isWifiConnected = true,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.i("Wifi capabilities changed")
|
|
||||||
autoTunnelStateFlow.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)
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
currentNetworkSSID = name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} ?: Timber.w("Failed to read ssid")
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isWifiConnected = false,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Timber.i("Lost Wi-Fi connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getMobileDataTunnel(): TunnelConfig? {
|
|
||||||
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isTunnelDown(): Boolean {
|
|
||||||
return tunnelService.get().vpnState.value.status == TunnelState.DOWN
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun handleNetworkEventChanges() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
Timber.i("Starting network event watcher")
|
|
||||||
autoTunnelStateFlow.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 activeTunnel = tunnelService.get().vpnState.value.tunnelConfig
|
|
||||||
val defaultTunnel = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
when {
|
|
||||||
watcherState.isEthernetConditionMet() -> {
|
|
||||||
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
|
|
||||||
if (isTunnelDown()) {
|
|
||||||
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 (isTunnelDown() || activeTunnel?.isMobileDataTunnel == false) {
|
|
||||||
tunnel?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
|
|
||||||
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
|
|
||||||
if (!isTunnelDown()) {
|
|
||||||
activeTunnel?.let {
|
|
||||||
tunnelService.get().stopTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isUntrustedWifiConditionMet() -> {
|
|
||||||
Timber.i("Untrusted wifi condition met")
|
|
||||||
if (activeTunnel?.tunnelNetworks?.isMatchingToWildcardList(watcherState.currentNetworkSSID) == false ||
|
|
||||||
activeTunnel == null || isTunnelDown()
|
|
||||||
) {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
|
|
||||||
)
|
|
||||||
watcherState.tunnels.firstOrNull { it.tunnelNetworks.isMatchingToWildcardList(watcherState.currentNetworkSSID) }?.let {
|
|
||||||
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
|
|
||||||
if (isTunnelDown() || activeTunnel?.id != it.id) {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
} ?: suspend {
|
|
||||||
Timber.i("No tunnel associated with this SSID, using defaults")
|
|
||||||
val default = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
if (default?.name != tunnelService.get().name || isTunnelDown()) {
|
|
||||||
default?.let {
|
|
||||||
tunnelService.get().startTunnel(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTrustedWifiConditionMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnWifiConditionMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on wifi condition met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
watcherState.isTunnelOffOnNoConnectivityMet() -> {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel off on no connectivity met, turning vpn off",
|
|
||||||
)
|
|
||||||
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
Timber.i("$autoTunnel - no condition met")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-76
@@ -1,76 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
|
||||||
|
|
||||||
data class AutoTunnelState(
|
|
||||||
val isWifiConnected: Boolean = false,
|
|
||||||
val isEthernetConnected: Boolean = false,
|
|
||||||
val isMobileDataConnected: Boolean = false,
|
|
||||||
val currentNetworkSSID: String = "",
|
|
||||||
val settings: Settings = Settings(),
|
|
||||||
val tunnels: TunnelConfigs = emptyList(),
|
|
||||||
) {
|
|
||||||
fun isEthernetConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
isEthernetConnected &&
|
|
||||||
settings.isTunnelOnEthernetEnabled
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isMobileDataConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
settings.isTunnelOnMobileDataEnabled &&
|
|
||||||
!isWifiConnected &&
|
|
||||||
isMobileDataConnected
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTunnelOffOnMobileDataConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
!settings.isTunnelOnMobileDataEnabled &&
|
|
||||||
isMobileDataConnected &&
|
|
||||||
!isWifiConnected
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isUntrustedWifiConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
isWifiConnected &&
|
|
||||||
!settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID) &&
|
|
||||||
settings.isTunnelOnWifiEnabled
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTrustedWifiConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
(
|
|
||||||
isWifiConnected &&
|
|
||||||
settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTunnelOffOnWifiConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
(
|
|
||||||
isWifiConnected &&
|
|
||||||
!settings.isTunnelOnWifiEnabled
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTunnelOffOnNoConnectivityMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
!isWifiConnected &&
|
|
||||||
!isMobileDataConnected
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+96
-49
@@ -3,69 +3,116 @@ 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 android.net.VpnService
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.AutoTunnelService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tile.AutoTunnelControlTile
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tile.TunnelControlTile
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||||
|
import jakarta.inject.Inject
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
object ServiceManager {
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
private fun <T : Service> actionOnService(action: Action, context: Context, cls: Class<T>, extras: Map<String, Int>? = null) {
|
class ServiceManager
|
||||||
if (VpnService.prepare(context) != null) return
|
@Inject constructor(private val context: Context, private val ioDispatcher: CoroutineDispatcher, private val appDataRepository: AppDataRepository) {
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
Action.START, Action.STOP -> context.startService(intent)
|
private val _autoTunnelActive = MutableStateFlow(false)
|
||||||
|
|
||||||
|
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
||||||
|
|
||||||
|
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
||||||
|
var backgroundService = CompletableDeferred<TunnelBackgroundService>()
|
||||||
|
var autoTunnelTile = CompletableDeferred<AutoTunnelControlTile>()
|
||||||
|
var tunnelControlTile = CompletableDeferred<TunnelControlTile>()
|
||||||
|
|
||||||
|
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
||||||
|
runCatching {
|
||||||
|
val intent = Intent(context, cls)
|
||||||
|
if (background) {
|
||||||
|
context.startForegroundService(intent)
|
||||||
|
} else {
|
||||||
|
context.startService(intent)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
}.onFailure { Timber.e(it) }
|
||||||
Timber.e(e.message)
|
}
|
||||||
|
|
||||||
|
suspend fun startAutoTunnel(background: Boolean) {
|
||||||
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
|
||||||
|
if (autoTunnelService.isCompleted) return _autoTunnelActive.update { true }
|
||||||
|
kotlin.runCatching {
|
||||||
|
startService(AutoTunnelService::class.java, background)
|
||||||
|
autoTunnelService.await()
|
||||||
|
autoTunnelService.getCompleted().start()
|
||||||
|
_autoTunnelActive.update { true }
|
||||||
|
updateAutoTunnelTile()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startWatcherServiceForeground(context: Context) {
|
suspend fun startBackgroundService() {
|
||||||
actionOnService(
|
if (backgroundService.isCompleted) return
|
||||||
Action.START_FOREGROUND,
|
kotlin.runCatching {
|
||||||
context,
|
startService(TunnelBackgroundService::class.java, true)
|
||||||
AutoTunnelService::class.java,
|
backgroundService.await()
|
||||||
)
|
backgroundService.getCompleted().start()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startWatcherService(context: Context) {
|
fun stopBackgroundService() {
|
||||||
actionOnService(
|
if (!backgroundService.isCompleted) return
|
||||||
Action.START,
|
runCatching {
|
||||||
context,
|
backgroundService.getCompleted().stop()
|
||||||
AutoTunnelService::class.java,
|
}.onFailure {
|
||||||
)
|
Timber.e(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopWatcherService(context: Context) {
|
suspend fun toggleAutoTunnel(background: Boolean) {
|
||||||
actionOnService(
|
withContext(ioDispatcher) {
|
||||||
Action.STOP,
|
if (_autoTunnelActive.value) return@withContext stopAutoTunnel()
|
||||||
context,
|
startAutoTunnel(background)
|
||||||
AutoTunnelService::class.java,
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startTunnelBackgroundService(context: Context) {
|
fun updateAutoTunnelTile() {
|
||||||
actionOnService(
|
if (autoTunnelTile.isCompleted) {
|
||||||
Action.START_FOREGROUND,
|
autoTunnelTile.getCompleted().updateTileState()
|
||||||
context,
|
} else {
|
||||||
TunnelBackgroundService::class.java,
|
context.requestAutoTunnelTileServiceUpdate()
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopTunnelBackgroundService(context: Context) {
|
fun updateTunnelTile() {
|
||||||
actionOnService(
|
if (tunnelControlTile.isCompleted) {
|
||||||
Action.STOP,
|
tunnelControlTile.getCompleted().updateTileState()
|
||||||
context,
|
} else {
|
||||||
TunnelBackgroundService::class.java,
|
context.requestTunnelTileServiceStateUpdate()
|
||||||
)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun stopAutoTunnel() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
|
||||||
|
if (!autoTunnelService.isCompleted) return@withContext
|
||||||
|
runCatching {
|
||||||
|
autoTunnelService.getCompleted().stop()
|
||||||
|
_autoTunnelActive.update { false }
|
||||||
|
updateAutoTunnelTile()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-17
@@ -3,10 +3,14 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
|||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
|
import androidx.core.app.ServiceCompat
|
||||||
import androidx.lifecycle.LifecycleService
|
import androidx.lifecycle.LifecycleService
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
@@ -15,11 +19,12 @@ class TunnelBackgroundService : LifecycleService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var notificationService: NotificationService
|
lateinit var notificationService: NotificationService
|
||||||
|
|
||||||
private val foregroundId = 123
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
startForeground(foregroundId, createNotification())
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
@@ -29,32 +34,33 @@ class TunnelBackgroundService : LifecycleService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (intent != null) {
|
serviceManager.backgroundService.complete(this)
|
||||||
val action = intent.action
|
|
||||||
when (action) {
|
|
||||||
Action.START.name,
|
|
||||||
Action.START_FOREGROUND.name,
|
|
||||||
-> startService()
|
|
||||||
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startService() {
|
fun start() {
|
||||||
startForeground(foregroundId, createNotification())
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
NotificationService.KERNEL_SERVICE_NOTIFICATION_ID,
|
||||||
|
createNotification(),
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopService() {
|
fun stop() {
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
serviceManager.backgroundService = CompletableDeferred()
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
private fun createNotification(): Notification {
|
private fun createNotification(): Notification {
|
||||||
return notificationService.createNotification(
|
return notificationService.createNotification(
|
||||||
getString(R.string.vpn_channel_id),
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
getString(R.string.vpn_channel_name),
|
getString(R.string.tunnel_running),
|
||||||
getString(R.string.tunnel_start_text),
|
|
||||||
description = "",
|
description = "",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+309
@@ -0,0 +1,309 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel
|
||||||
|
|
||||||
|
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.AppShell
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.AutoTunnelEvent
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.AutoTunnelState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.NetworkState
|
||||||
|
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.NotificationAction
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
|
||||||
|
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.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
import kotlinx.coroutines.flow.filterNot
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Provider
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class AutoTunnelService : LifecycleService() {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@AppShell
|
||||||
|
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: Provider<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 val pingTunnelRestartActive = AtomicBoolean(false)
|
||||||
|
|
||||||
|
private var pingJob: Job? = null
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
serviceManager.autoTunnelService.complete(this)
|
||||||
|
lifecycleScope.launch(mainImmediateDispatcher) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
launchWatcherNotification()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
|
super.onBind(intent)
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
startAutoTunnelJob()
|
||||||
|
startAutoTunnelStateJob()
|
||||||
|
startPingStateJob()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stop() {
|
||||||
|
wakeLock?.let { if (it.isHeld) it.release() }
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
cancelAndResetPingJob()
|
||||||
|
serviceManager.autoTunnelService = CompletableDeferred()
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchWatcherNotification(description: String = getString(R.string.monitoring_state_changes)) {
|
||||||
|
val notification =
|
||||||
|
notificationService.createNotification(
|
||||||
|
WireGuardNotification.NotificationChannels.AUTO_TUNNEL,
|
||||||
|
title = getString(R.string.auto_tunnel_title),
|
||||||
|
description = description,
|
||||||
|
actions = listOf(
|
||||||
|
notificationService.createNotificationAction(NotificationAction.AUTO_TUNNEL_OFF),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
NotificationService.AUTO_TUNNEL_NOTIFICATION_ID,
|
||||||
|
notification,
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initWakeLock() {
|
||||||
|
wakeLock = (getSystemService(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 startPingJob() = lifecycleScope.launch {
|
||||||
|
watchForPingFailure()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startPingStateJob() = lifecycleScope.launch {
|
||||||
|
autoTunnelStateFlow.collect {
|
||||||
|
if (it.isPingEnabled()) {
|
||||||
|
pingJob.onNotRunning { pingJob = startPingJob() }
|
||||||
|
} else {
|
||||||
|
if (!pingTunnelRestartActive.get()) cancelAndResetPingJob()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForPingFailure() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
Timber.i("Starting ping watcher")
|
||||||
|
runCatching {
|
||||||
|
do {
|
||||||
|
val vpnState = autoTunnelStateFlow.value.vpnState
|
||||||
|
if (vpnState.status.isUp() && !autoTunnelStateFlow.value.isNoConnectivity()) {
|
||||||
|
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
|
||||||
|
pingTunnelRestartActive.set(true)
|
||||||
|
tunnelService.get().bounceTunnel()
|
||||||
|
pingTunnelRestartActive.set(false)
|
||||||
|
delay(cooldown ?: Constants.PING_COOLDOWN)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(vpnState.tunnelConfig?.pingInterval ?: Constants.PING_INTERVAL)
|
||||||
|
} while (true)
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startAutoTunnelStateJob() = lifecycleScope.launch(ioDispatcher) {
|
||||||
|
combine(
|
||||||
|
combineSettings(),
|
||||||
|
combineNetworkEventsJob(),
|
||||||
|
) { double, networkState ->
|
||||||
|
AutoTunnelState(tunnelService.get().vpnState.value, networkState, double.first, double.second)
|
||||||
|
}.collect { state ->
|
||||||
|
autoTunnelStateFlow.update {
|
||||||
|
it.copy(state.vpnState, state.networkState, state.settings, state.tunnels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelAndResetPingJob() {
|
||||||
|
pingJob?.cancelWithMessage("Ping job canceled")
|
||||||
|
pingJob = null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun combineNetworkEventsJob(): Flow<NetworkState> {
|
||||||
|
return combine(
|
||||||
|
wifiService.networkStatus,
|
||||||
|
mobileDataService.networkStatus,
|
||||||
|
ethernetService.networkStatus,
|
||||||
|
) { wifi, mobileData, ethernet ->
|
||||||
|
NetworkState(
|
||||||
|
wifi.isConnected,
|
||||||
|
mobileData.isConnected,
|
||||||
|
ethernet.isConnected,
|
||||||
|
when (wifi) {
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> getWifiSSID(wifi.networkCapabilities)
|
||||||
|
is NetworkStatus.Available -> autoTunnelStateFlow.value.networkState.wifiName
|
||||||
|
is NetworkStatus.Unavailable -> null
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun combineSettings(): Flow<Pair<Settings, TunnelConfigs>> {
|
||||||
|
return combine(
|
||||||
|
appDataRepository.get().settings.getSettingsFlow(),
|
||||||
|
appDataRepository.get().tunnels.getTunnelConfigsFlow().distinctUntilChanged { old, new ->
|
||||||
|
old.map { it.isActive } != new.map { it.isActive }
|
||||||
|
},
|
||||||
|
) { settings, tunnels ->
|
||||||
|
Pair(settings, tunnels)
|
||||||
|
}.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getWifiSSID(networkCapabilities: NetworkCapabilities): String? {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
with(autoTunnelStateFlow.value.settings) {
|
||||||
|
if (isWifiNameByShellEnabled) return@withContext rootShell.get().getCurrentWifiName()
|
||||||
|
wifiService.getNetworkName(networkCapabilities)
|
||||||
|
}.also {
|
||||||
|
if (it?.contains(Constants.UNREADABLE_SSID) == true) {
|
||||||
|
Timber.w("SSID unreadable: missing permissions")
|
||||||
|
} else {
|
||||||
|
Timber.i("Detected valid SSID")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startAutoTunnelJob() = lifecycleScope.launch(ioDispatcher) {
|
||||||
|
Timber.i("Starting auto-tunnel network event watcher")
|
||||||
|
autoTunnelStateFlow.collect { watcherState ->
|
||||||
|
Timber.d("New auto tunnel state emitted")
|
||||||
|
when (val event = watcherState.asAutoTunnelEvent()) {
|
||||||
|
is AutoTunnelEvent.Start -> tunnelService.get().startTunnel(
|
||||||
|
event.tunnelConfig
|
||||||
|
?: appDataRepository.get().getPrimaryOrFirstTunnel(),
|
||||||
|
)
|
||||||
|
is AutoTunnelEvent.Stop -> tunnelService.get().stopTunnel()
|
||||||
|
AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: no condition met")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
|
|
||||||
|
sealed class AutoTunnelEvent {
|
||||||
|
data class Start(val tunnelConfig: TunnelConfig? = null) : AutoTunnelEvent()
|
||||||
|
data object Stop : AutoTunnelEvent()
|
||||||
|
data object DoNothing : AutoTunnelEvent()
|
||||||
|
}
|
||||||
+147
@@ -0,0 +1,147 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model
|
||||||
|
|
||||||
|
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
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
data class AutoTunnelState(
|
||||||
|
val vpnState: VpnState = VpnState(),
|
||||||
|
val networkState: NetworkState = NetworkState(),
|
||||||
|
val settings: Settings = Settings(),
|
||||||
|
val tunnels: TunnelConfigs = emptyList(),
|
||||||
|
) {
|
||||||
|
|
||||||
|
private fun isMobileDataActive(): Boolean {
|
||||||
|
return !networkState.isEthernetConnected && !networkState.isWifiConnected && networkState.isMobileDataConnected
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isMobileTunnelDataChangeNeeded(): Boolean {
|
||||||
|
val preferredTunnel = preferredMobileDataTunnel()
|
||||||
|
return preferredTunnel != null &&
|
||||||
|
vpnState.status.isUp() && preferredTunnel.id != vpnState.tunnelConfig?.id
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isEthernetTunnelChangeNeeded(): Boolean {
|
||||||
|
val preferredTunnel = preferredEthernetTunnel()
|
||||||
|
return preferredTunnel != null && vpnState.status.isUp() && preferredTunnel.id != vpnState.tunnelConfig?.id
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun preferredMobileDataTunnel(): TunnelConfig? {
|
||||||
|
return tunnels.firstOrNull { it.isMobileDataTunnel } ?: tunnels.firstOrNull { it.isPrimaryTunnel }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun preferredEthernetTunnel(): TunnelConfig? {
|
||||||
|
return tunnels.firstOrNull { it.isEthernetTunnel } ?: tunnels.firstOrNull { it.isPrimaryTunnel }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun preferredWifiTunnel(): TunnelConfig? {
|
||||||
|
return getTunnelWithMatchingTunnelNetwork() ?: tunnels.firstOrNull { it.isPrimaryTunnel }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isWifiActive(): Boolean {
|
||||||
|
return !networkState.isEthernetConnected && networkState.isWifiConnected
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startOnEthernet(): Boolean {
|
||||||
|
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && vpnState.status.isDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopOnEthernet(): Boolean {
|
||||||
|
return networkState.isEthernetConnected && !settings.isTunnelOnEthernetEnabled && vpnState.status.isUp()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isNoConnectivity(): Boolean {
|
||||||
|
return !networkState.isEthernetConnected && !networkState.isWifiConnected && !networkState.isMobileDataConnected
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopOnMobileData(): Boolean {
|
||||||
|
return isMobileDataActive() && !settings.isTunnelOnMobileDataEnabled && vpnState.status.isUp()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startOnMobileData(): Boolean {
|
||||||
|
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && vpnState.status.isDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun changeOnMobileData(): Boolean {
|
||||||
|
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && isMobileTunnelDataChangeNeeded()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun changeOnEthernet(): Boolean {
|
||||||
|
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && isEthernetTunnelChangeNeeded()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopOnWifi(): Boolean {
|
||||||
|
return isWifiActive() && !settings.isTunnelOnWifiEnabled && vpnState.status.isUp()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopOnTrustedWifi(): Boolean {
|
||||||
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && vpnState.status.isUp() && isCurrentSSIDTrusted()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startOnUntrustedWifi(): Boolean {
|
||||||
|
Timber.d("Is tunnel on wifi enabled ${settings.isTunnelOnWifiEnabled}")
|
||||||
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && vpnState.status.isDown() && !isCurrentSSIDTrusted()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun changeOnUntrustedWifi(): Boolean {
|
||||||
|
return isWifiActive() && settings.isTunnelOnWifiEnabled && vpnState.status.isUp() && !isCurrentSSIDTrusted() && !isWifiTunnelPreferred()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isWifiTunnelPreferred(): Boolean {
|
||||||
|
val preferred = preferredWifiTunnel()
|
||||||
|
val vpnTunnel = vpnState.tunnelConfig
|
||||||
|
return if (preferred != null && vpnTunnel != null) {
|
||||||
|
preferred.id == vpnTunnel.id
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun asAutoTunnelEvent(): AutoTunnelEvent {
|
||||||
|
return when {
|
||||||
|
// ethernet scenarios
|
||||||
|
stopOnEthernet() -> AutoTunnelEvent.Stop
|
||||||
|
startOnEthernet() || changeOnEthernet() -> AutoTunnelEvent.Start(preferredEthernetTunnel())
|
||||||
|
// mobile data scenarios
|
||||||
|
stopOnMobileData() -> AutoTunnelEvent.Stop
|
||||||
|
startOnMobileData() || changeOnMobileData() -> AutoTunnelEvent.Start(preferredMobileDataTunnel())
|
||||||
|
// wifi scenarios
|
||||||
|
stopOnWifi() -> AutoTunnelEvent.Stop
|
||||||
|
stopOnTrustedWifi() -> AutoTunnelEvent.Stop
|
||||||
|
startOnUntrustedWifi() || changeOnUntrustedWifi() -> AutoTunnelEvent.Start(preferredWifiTunnel())
|
||||||
|
// no connectivity
|
||||||
|
isNoConnectivity() && settings.isStopOnNoInternetEnabled -> AutoTunnelEvent.Stop
|
||||||
|
else -> AutoTunnelEvent.DoNothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isCurrentSSIDTrusted(): Boolean {
|
||||||
|
return networkState.wifiName?.let {
|
||||||
|
hasTrustedWifiName(it)
|
||||||
|
} == true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hasTrustedWifiName(wifiName: String, wifiNames: List<String> = settings.trustedNetworkSSIDs): Boolean {
|
||||||
|
return if (settings.isWildcardsEnabled) {
|
||||||
|
wifiNames.isMatchingToWildcardList(wifiName)
|
||||||
|
} else {
|
||||||
|
wifiNames.contains(wifiName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTunnelWithMatchingTunnelNetwork(): TunnelConfig? {
|
||||||
|
return networkState.wifiName?.let { wifiName ->
|
||||||
|
tunnels.firstOrNull {
|
||||||
|
hasTrustedWifiName(wifiName, it.tunnelNetworks)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isPingEnabled(): Boolean {
|
||||||
|
return settings.isPingEnabled ||
|
||||||
|
(vpnState.status.isUp() && vpnState.tunnelConfig != null && tunnels.first { it.id == vpnState.tunnelConfig.id }.isPingEnabled)
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model
|
||||||
|
|
||||||
|
data class NetworkState(
|
||||||
|
val isWifiConnected: Boolean = false,
|
||||||
|
val isMobileDataConnected: Boolean = false,
|
||||||
|
val isEthernetConnected: Boolean = false,
|
||||||
|
val wifiName: String? = null,
|
||||||
|
)
|
||||||
+21
-32
@@ -5,14 +5,15 @@ 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
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.callbackFlow
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
||||||
val context: Context,
|
val context: Context,
|
||||||
@@ -21,11 +22,20 @@ 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
|
||||||
|
|
||||||
private val wifiManager =
|
val wifiManager =
|
||||||
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||||
|
|
||||||
|
fun checkHasCapability(networkCapability: Int): Boolean {
|
||||||
|
val network = connectivityManager.activeNetwork
|
||||||
|
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
|
||||||
|
return networkCapabilities?.hasTransport(networkCapability) == true
|
||||||
|
}
|
||||||
|
|
||||||
override val networkStatus =
|
override val networkStatus =
|
||||||
callbackFlow {
|
callbackFlow {
|
||||||
|
if (!checkHasCapability(networkCapability)) {
|
||||||
|
trySend(NetworkStatus.Unavailable())
|
||||||
|
}
|
||||||
val networkStatusCallback =
|
val networkStatusCallback =
|
||||||
when (Build.VERSION.SDK_INT) {
|
when (Build.VERSION.SDK_INT) {
|
||||||
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
||||||
@@ -38,7 +48,7 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
override fun onLost(network: Network) {
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
trySend(NetworkStatus.Unavailable())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
@@ -59,7 +69,7 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
override fun onLost(network: Network) {
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
trySend(NetworkStatus.Unavailable())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
@@ -82,41 +92,20 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|||||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||||
|
|
||||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||||
}
|
}.catch {
|
||||||
|
Timber.e(it)
|
||||||
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
// conflate for backpressure
|
||||||
var ssid: String? = getWifiNameFromCapabilities(networkCapabilities)
|
}.conflate()
|
||||||
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(
|
||||||
crossinline onUnavailable: suspend (network: Network) -> Result,
|
crossinline onUnavailable: suspend () -> Result,
|
||||||
crossinline onAvailable: suspend (network: Network) -> Result,
|
crossinline onAvailable: suspend (network: Network) -> Result,
|
||||||
crossinline onCapabilitiesChanged:
|
crossinline onCapabilitiesChanged:
|
||||||
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
||||||
): Flow<Result> = map { status ->
|
): Flow<Result> = map { status ->
|
||||||
when (status) {
|
when (status) {
|
||||||
is NetworkStatus.Unavailable -> onUnavailable(status.network)
|
is NetworkStatus.Unavailable -> onUnavailable()
|
||||||
is NetworkStatus.Available -> onAvailable(status.network)
|
is NetworkStatus.Available -> onAvailable(status.network)
|
||||||
is NetworkStatus.CapabilitiesChanged ->
|
is NetworkStatus.CapabilitiesChanged ->
|
||||||
onCapabilitiesChanged(
|
onCapabilitiesChanged(
|
||||||
|
|||||||
+6
-1
@@ -10,4 +10,9 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -10,4 +10,8 @@ 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
@@ -0,0 +1,16 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
+5
-1
@@ -4,7 +4,11 @@ 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>
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -4,10 +4,11 @@ import android.net.Network
|
|||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
|
|
||||||
sealed class NetworkStatus {
|
sealed class NetworkStatus {
|
||||||
class Available(val network: Network) : NetworkStatus()
|
abstract val isConnected: Boolean
|
||||||
|
class Available(val network: Network, override val isConnected: Boolean = true) : NetworkStatus()
|
||||||
|
|
||||||
class Unavailable(val network: Network) : NetworkStatus()
|
class Unavailable(override val isConnected: Boolean = false) : NetworkStatus()
|
||||||
|
|
||||||
class CapabilitiesChanged(val network: Network, val networkCapabilities: NetworkCapabilities) :
|
class CapabilitiesChanged(val network: Network, val networkCapabilities: NetworkCapabilities, override val isConnected: Boolean = true) :
|
||||||
NetworkStatus()
|
NetworkStatus()
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-1
@@ -2,6 +2,8 @@ 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
|
||||||
|
|
||||||
@@ -10,4 +12,21 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.notification
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
|
enum class NotificationAction {
|
||||||
|
TUNNEL_OFF,
|
||||||
|
AUTO_TUNNEL_OFF,
|
||||||
|
;
|
||||||
|
|
||||||
|
fun title(context: Context): String {
|
||||||
|
return when (this) {
|
||||||
|
TUNNEL_OFF -> context.getString(R.string.stop)
|
||||||
|
AUTO_TUNNEL_OFF -> context.getString(R.string.stop)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
-8
@@ -2,21 +2,32 @@ package com.zaneschepke.wireguardautotunnel.service.notification
|
|||||||
|
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.app.PendingIntent
|
import android.content.Context
|
||||||
|
import androidx.core.app.NotificationCompat
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification.NotificationChannels
|
||||||
|
|
||||||
interface NotificationService {
|
interface NotificationService {
|
||||||
|
val context: Context
|
||||||
fun createNotification(
|
fun createNotification(
|
||||||
channelId: String,
|
channel: NotificationChannels,
|
||||||
channelName: String,
|
|
||||||
title: String = "",
|
title: String = "",
|
||||||
action: PendingIntent? = null,
|
actions: Collection<NotificationCompat.Action> = emptyList(),
|
||||||
actionText: String? = null,
|
description: String = "",
|
||||||
description: String,
|
|
||||||
showTimestamp: Boolean = false,
|
showTimestamp: Boolean = false,
|
||||||
importance: Int = NotificationManager.IMPORTANCE_HIGH,
|
importance: Int = NotificationManager.IMPORTANCE_HIGH,
|
||||||
vibration: Boolean = false,
|
|
||||||
onGoing: Boolean = true,
|
onGoing: Boolean = true,
|
||||||
lights: Boolean = true,
|
|
||||||
onlyAlertOnce: Boolean = true,
|
onlyAlertOnce: Boolean = true,
|
||||||
): Notification
|
): Notification
|
||||||
|
|
||||||
|
fun createNotificationAction(action: NotificationAction): NotificationCompat.Action
|
||||||
|
|
||||||
|
fun remove(notificationId: Int)
|
||||||
|
|
||||||
|
fun show(notificationId: Int, notification: Notification)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val KERNEL_SERVICE_NOTIFICATION_ID = 123
|
||||||
|
const val AUTO_TUNNEL_NOTIFICATION_ID = 122
|
||||||
|
const val VPN_NOTIFICATION_ID = 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+97
-68
@@ -1,105 +1,134 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.notification
|
package com.zaneschepke.wireguardautotunnel.service.notification
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationChannel
|
import android.app.NotificationChannel
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.SplashActivity
|
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class WireGuardNotification
|
class WireGuardNotification
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext private val context: Context,
|
@ApplicationContext override val context: Context,
|
||||||
) :
|
) : NotificationService {
|
||||||
NotificationService {
|
|
||||||
private val notificationManager =
|
|
||||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
|
|
||||||
private val watcherBuilder: NotificationCompat.Builder =
|
enum class NotificationChannels {
|
||||||
NotificationCompat.Builder(
|
VPN,
|
||||||
context,
|
AUTO_TUNNEL,
|
||||||
context.getString(R.string.watcher_channel_id),
|
}
|
||||||
)
|
|
||||||
private val tunnelBuilder: NotificationCompat.Builder =
|
private val notificationManager = NotificationManagerCompat.from(context)
|
||||||
NotificationCompat.Builder(
|
|
||||||
context,
|
|
||||||
context.getString(R.string.vpn_channel_id),
|
|
||||||
)
|
|
||||||
|
|
||||||
override fun createNotification(
|
override fun createNotification(
|
||||||
channelId: String,
|
channel: NotificationChannels,
|
||||||
channelName: String,
|
|
||||||
title: String,
|
title: String,
|
||||||
action: PendingIntent?,
|
actions: Collection<NotificationCompat.Action>,
|
||||||
actionText: String?,
|
|
||||||
description: String,
|
description: String,
|
||||||
showTimestamp: Boolean,
|
showTimestamp: Boolean,
|
||||||
importance: Int,
|
importance: Int,
|
||||||
vibration: Boolean,
|
|
||||||
onGoing: Boolean,
|
onGoing: Boolean,
|
||||||
lights: Boolean,
|
|
||||||
onlyAlertOnce: Boolean,
|
onlyAlertOnce: Boolean,
|
||||||
): Notification {
|
): Notification {
|
||||||
val channel =
|
notificationManager.createNotificationChannel(channel.asChannel())
|
||||||
NotificationChannel(
|
return channel.asBuilder().apply {
|
||||||
channelId,
|
actions.forEach {
|
||||||
channelName,
|
addAction(it)
|
||||||
importance,
|
}
|
||||||
)
|
setContentTitle(title)
|
||||||
.let {
|
setContentText(description)
|
||||||
it.description = title
|
setOnlyAlertOnce(onlyAlertOnce)
|
||||||
it.enableLights(lights)
|
setOngoing(onGoing)
|
||||||
it.lightColor = Color.RED
|
setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
it.enableVibration(vibration)
|
setShowWhen(showTimestamp)
|
||||||
it.vibrationPattern = longArrayOf(100, 200, 300)
|
setSmallIcon(R.drawable.ic_launcher)
|
||||||
it
|
}.build()
|
||||||
}
|
}
|
||||||
notificationManager.createNotificationChannel(channel)
|
|
||||||
val pendingIntent: PendingIntent =
|
override fun createNotificationAction(notificationAction: NotificationAction): NotificationCompat.Action {
|
||||||
Intent(context, SplashActivity::class.java).let { notificationIntent ->
|
val pendingIntent = PendingIntent.getBroadcast(
|
||||||
PendingIntent.getActivity(
|
context,
|
||||||
|
0,
|
||||||
|
Intent(context, NotificationActionReceiver::class.java).apply {
|
||||||
|
action = notificationAction.name
|
||||||
|
},
|
||||||
|
PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
)
|
||||||
|
return NotificationCompat.Action.Builder(
|
||||||
|
R.drawable.ic_launcher,
|
||||||
|
notificationAction.title(context).uppercase(),
|
||||||
|
pendingIntent,
|
||||||
|
).build()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remove(notificationId: Int) {
|
||||||
|
notificationManager.cancel(notificationId)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun show(notificationId: Int, notification: Notification) {
|
||||||
|
with(notificationManager) {
|
||||||
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
notify(notificationId, notification)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
|
||||||
|
return when (this) {
|
||||||
|
NotificationChannels.VPN -> {
|
||||||
|
NotificationCompat.Builder(
|
||||||
context,
|
context,
|
||||||
0,
|
context.getString(R.string.auto_tunnel_channel_id),
|
||||||
notificationIntent,
|
|
||||||
PendingIntent.FLAG_IMMUTABLE,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
NotificationChannels.AUTO_TUNNEL -> {
|
||||||
|
NotificationCompat.Builder(
|
||||||
|
context,
|
||||||
|
context.getString(R.string.vpn_channel_id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val builder =
|
fun NotificationChannels.asChannel(): NotificationChannel {
|
||||||
when (channelId) {
|
return when (this) {
|
||||||
context.getString(R.string.watcher_channel_id) -> watcherBuilder
|
NotificationChannels.VPN -> {
|
||||||
context.getString(R.string.vpn_channel_id) -> tunnelBuilder
|
NotificationChannel(
|
||||||
else -> {
|
context.getString(R.string.vpn_channel_id),
|
||||||
NotificationCompat.Builder(
|
context.getString(R.string.vpn_channel_name),
|
||||||
context,
|
NotificationManager.IMPORTANCE_HIGH,
|
||||||
channelId,
|
).apply {
|
||||||
)
|
description = context.getString(R.string.vpn_channel_description)
|
||||||
|
enableLights(true)
|
||||||
|
lightColor = Color.WHITE
|
||||||
|
enableVibration(false)
|
||||||
|
vibrationPattern = longArrayOf(100, 200, 300)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
NotificationChannels.AUTO_TUNNEL -> {
|
||||||
return builder.let {
|
NotificationChannel(
|
||||||
if (action != null && actionText != null) {
|
context.getString(R.string.auto_tunnel_channel_id),
|
||||||
it.addAction(
|
context.getString(R.string.auto_tunnel_channel_name),
|
||||||
NotificationCompat.Action.Builder(0, actionText, action).build(),
|
NotificationManager.IMPORTANCE_HIGH,
|
||||||
)
|
).apply {
|
||||||
it.setAutoCancel(true)
|
description = context.getString(R.string.auto_tunnel_channel_description)
|
||||||
|
enableLights(true)
|
||||||
|
lightColor = Color.WHITE
|
||||||
|
enableVibration(false)
|
||||||
|
vibrationPattern = longArrayOf(100, 200, 300)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
it.setContentTitle(title)
|
|
||||||
.setContentText(description)
|
|
||||||
.setOnlyAlertOnce(onlyAlertOnce)
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.setOngoing(onGoing)
|
|
||||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
||||||
.setShowWhen(showTimestamp)
|
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-18
@@ -4,11 +4,9 @@ import android.os.Bundle
|
|||||||
import androidx.activity.ComponentActivity
|
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.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.AutoTunnelService
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.AutoTunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.stopTunnelBackground
|
|
||||||
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
|
||||||
@@ -24,6 +22,9 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
lateinit var tunnelService: Provider<TunnelService>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
@@ -44,26 +45,16 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
Timber.d("Shortcut action on name: ${tunnelConfig?.name}")
|
Timber.d("Shortcut action on name: ${tunnelConfig?.name}")
|
||||||
tunnelConfig?.let {
|
tunnelConfig?.let {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
Action.START.name -> this@ShortcutsActivity.startTunnelBackground(it.id)
|
Action.START.name -> tunnelService.get().startTunnel(it, true)
|
||||||
Action.STOP.name -> this@ShortcutsActivity.stopTunnelBackground(it.id)
|
Action.STOP.name -> tunnelService.get().stopTunnel()
|
||||||
else -> Unit
|
else -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
Action.START.name ->
|
Action.START.name -> serviceManager.startAutoTunnel(true)
|
||||||
appDataRepository.settings.save(
|
Action.STOP.name -> serviceManager.stopAutoTunnel()
|
||||||
settings.copy(
|
|
||||||
isAutoTunnelPaused = false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
Action.STOP.name ->
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
settings.copy(
|
|
||||||
isAutoTunnelPaused = true,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,6 +63,11 @@ class ShortcutsActivity : ComponentActivity() {
|
|||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class Action {
|
||||||
|
START,
|
||||||
|
STOP,
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService"
|
const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService"
|
||||||
const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService"
|
const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService"
|
||||||
|
|||||||
+26
-85
@@ -1,107 +1,63 @@
|
|||||||
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.LifecycleOwner
|
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
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 dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
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
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
class AutoTunnelControlTile : TileService() {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
|
||||||
|
|
||||||
/* 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 (e: Throwable) {
|
|
||||||
Timber.e("Failed to bind to AutoTunnelTile")
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
serviceManager.autoTunnelTile.complete(this)
|
||||||
|
|
||||||
applicationScope.launch {
|
|
||||||
appDataRepository.settings.getSettingsFlow().collect {
|
|
||||||
kotlin.runCatching {
|
|
||||||
when (it.isAutoTunnelEnabled) {
|
|
||||||
true -> {
|
|
||||||
if (it.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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStopListening() {
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
serviceManager.autoTunnelTile = CompletableDeferred()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
serviceManager.autoTunnelTile.complete(this)
|
||||||
|
applicationScope.launch {
|
||||||
|
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
||||||
|
updateTileState()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateTileState() {
|
||||||
|
serviceManager.autoTunnelActive.value.let {
|
||||||
|
if (it) setActive() else setInactive()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
kotlin.runCatching {
|
if (serviceManager.autoTunnelActive.value) {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
serviceManager.stopAutoTunnel()
|
||||||
if (settings.isAutoTunnelPaused) {
|
setInactive()
|
||||||
return@launch appDataRepository.settings.save(
|
} else {
|
||||||
settings.copy(
|
serviceManager.startAutoTunnel(true)
|
||||||
isAutoTunnelPaused = false,
|
setActive()
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
settings.copy(
|
|
||||||
isAutoTunnelPaused = true,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,19 +83,4 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
qsTile.updateTile()
|
qsTile.updateTile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setTileDescription(description: String) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
qsTile.subtitle = description
|
|
||||||
}
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
||||||
qsTile.stateDescription = description
|
|
||||||
}
|
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
|
||||||
get() = lifecycleRegistry
|
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-27
@@ -3,25 +3,20 @@ package com.zaneschepke.wireguardautotunnel.service.tile
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.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.module.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.stopTunnelBackground
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
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
|
import javax.inject.Provider
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelControlTile : TileService(), LifecycleOwner {
|
class TunnelControlTile : TileService() {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@@ -32,33 +27,29 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
@Inject
|
||||||
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
Timber.d("onCreate for tile service")
|
serviceManager.tunnelControlTile.complete(this)
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStopListening() {
|
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
serviceManager.tunnelControlTile = CompletableDeferred()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
serviceManager.tunnelControlTile.complete(this)
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
||||||
updateTileState()
|
updateTileState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateTileState() {
|
fun updateTileState() = applicationScope.launch {
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
val lastActive = appDataRepository.getStartTunnelConfig()
|
||||||
lastActive?.let {
|
lastActive?.let {
|
||||||
updateTile(it)
|
updateTile(it)
|
||||||
@@ -68,13 +59,15 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
Timber.d("Click")
|
applicationScope.launch {
|
||||||
lifecycleScope.launch {
|
|
||||||
val context = this@TunnelControlTile
|
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
val lastActive = appDataRepository.getStartTunnelConfig()
|
||||||
lastActive?.let { tunnel ->
|
lastActive?.let { tunnel ->
|
||||||
if (tunnel.isActive) return@launch context.stopTunnelBackground(tunnel.id)
|
if (tunnel.isActive) {
|
||||||
context.startTunnelBackground(tunnel.id)
|
tunnelService.get().stopTunnel()
|
||||||
|
} else {
|
||||||
|
tunnelService.get().startTunnel(tunnel, true)
|
||||||
|
}
|
||||||
|
updateTileState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,7 +116,4 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
|
||||||
get() = lifecycleRegistry
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
|
enum class BackendState {
|
||||||
|
KILL_SWITCH_ACTIVE,
|
||||||
|
SERVICE_ACTIVE,
|
||||||
|
INACTIVE,
|
||||||
|
}
|
||||||
+11
-5
@@ -5,11 +5,16 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
||||||
suspend fun startTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
|
||||||
|
|
||||||
suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean = false)
|
||||||
|
|
||||||
suspend fun bounceTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
suspend fun stopTunnel()
|
||||||
|
|
||||||
|
suspend fun bounceTunnel()
|
||||||
|
|
||||||
|
suspend fun getBackendState(): BackendState
|
||||||
|
|
||||||
|
suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
|
||||||
|
|
||||||
val vpnState: StateFlow<VpnState>
|
val vpnState: StateFlow<VpnState>
|
||||||
|
|
||||||
@@ -17,6 +22,7 @@ interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
|
|||||||
|
|
||||||
suspend fun getState(): TunnelState
|
suspend fun getState(): TunnelState
|
||||||
|
|
||||||
fun cancelStatsJob()
|
fun cancelActiveTunnelJobs()
|
||||||
fun startStatsJob()
|
|
||||||
|
fun startActiveTunnelJobs()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,14 @@ enum class TunnelState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isDown(): Boolean {
|
||||||
|
return this == DOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isUp(): Boolean {
|
||||||
|
return this == UP
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun from(state: Tunnel.State): TunnelState {
|
fun from(state: Tunnel.State): TunnelState {
|
||||||
return when (state) {
|
return when (state) {
|
||||||
|
|||||||
+195
-90
@@ -8,21 +8,30 @@ import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepositor
|
|||||||
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.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
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.extensions.asAmBackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
|
||||||
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.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.amnezia.awg.backend.Tunnel
|
import org.amnezia.awg.backend.Tunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService.Companion.VPN_NOTIFICATION_ID
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
import javax.inject.Provider
|
||||||
@@ -31,28 +40,38 @@ class WireGuardTunnel
|
|||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
private val amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
|
||||||
tunnelConfigRepository: TunnelConfigRepository,
|
private val tunnelConfigRepository: TunnelConfigRepository,
|
||||||
@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,
|
||||||
|
private val notificationService: NotificationService,
|
||||||
) : TunnelService {
|
) : 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 var tunnelChangesJob: Job? = null
|
||||||
|
|
||||||
|
@get:Synchronized @set:Synchronized
|
||||||
|
private var isKernelBackend: Boolean? = null
|
||||||
|
|
||||||
|
private val tunnelControlMutex = Mutex()
|
||||||
|
|
||||||
|
init {
|
||||||
|
applicationScope.launch(ioDispatcher) {
|
||||||
|
appDataRepository.settings.getSettingsFlow().collect {
|
||||||
|
isKernelBackend = it.isKernelEnabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun backend(): Any {
|
private suspend fun backend(): Any {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
val isKernelEnabled = isKernelBackend
|
||||||
if (settings.isKernelEnabled) return kernelBackend.get()
|
?: appDataRepository.settings.getSettings().isKernelEnabled
|
||||||
|
if (isKernelEnabled) return kernelBackend.get()
|
||||||
return amneziaBackend.get()
|
return amneziaBackend.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,106 +106,169 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
private fun isTunnelAlreadyRunning(tunnelConfig: TunnelConfig): Boolean {
|
||||||
return withContext(ioDispatcher) {
|
val isRunning = tunnelConfig == _vpnState.value.tunnelConfig && _vpnState.value.status.isUp()
|
||||||
onBeforeStart(tunnelConfig)
|
if (isRunning) Timber.w("Tunnel already running")
|
||||||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
return isRunning
|
||||||
emitTunnelState(it)
|
}
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
override suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean) {
|
||||||
onStartFailed()
|
if (tunnelConfig == null) return
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
if (isTunnelAlreadyRunning(tunnelConfig)) return@withContext
|
||||||
|
withServiceActive {
|
||||||
|
onBeforeStart(background)
|
||||||
|
tunnelControlMutex.withLock {
|
||||||
|
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
||||||
|
startActiveTunnelJobs()
|
||||||
|
if (it.isUp()) appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||||
|
with(notificationService) {
|
||||||
|
val notification = createNotification(
|
||||||
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
|
title = "${context.getString(R.string.tunnel_running)} - ${tunnelConfig.name}",
|
||||||
|
actions = listOf(
|
||||||
|
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
show(VPN_NOTIFICATION_ID, notification)
|
||||||
|
}
|
||||||
|
updateTunnelState(it, tunnelConfig)
|
||||||
|
}
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
override suspend fun stopTunnel() {
|
||||||
return withContext(ioDispatcher) {
|
withContext(ioDispatcher) {
|
||||||
onBeforeStop(tunnelConfig)
|
if (_vpnState.value.status.isDown()) return@withContext
|
||||||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
with(_vpnState.value) {
|
||||||
emitTunnelState(it)
|
if (tunnelConfig == null) return@withContext
|
||||||
}.onFailure {
|
tunnelControlMutex.withLock {
|
||||||
Timber.e(it)
|
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
||||||
onStopFailed()
|
updateTunnelState(it, null)
|
||||||
|
onStop(tunnelConfig)
|
||||||
|
notificationService.remove(VPN_NOTIFICATION_ID)
|
||||||
|
stopBackgroundService()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use this when we just want to bounce tunnel and not change tunnelConfig active state
|
private suspend fun toggleTunnel(tunnelConfig: TunnelConfig) {
|
||||||
override suspend fun bounceTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
withContext(ioDispatcher) {
|
||||||
toggleTunnel(tunnelConfig)
|
tunnelControlMutex.withLock {
|
||||||
delay(VPN_RESTART_DELAY)
|
setState(tunnelConfig, TunnelState.TOGGLE)
|
||||||
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() {
|
// utility to keep vpnService alive during rapid changes to prevent bad states
|
||||||
|
private suspend fun withServiceActive(callback: suspend () -> Unit) {
|
||||||
|
when (val backend = backend()) {
|
||||||
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
|
val backendState = backend.backendState
|
||||||
|
if (backendState == org.amnezia.awg.backend.Backend.BackendState.INACTIVE) {
|
||||||
|
backend.setBackendState(org.amnezia.awg.backend.Backend.BackendState.SERVICE_ACTIVE, emptyList())
|
||||||
|
}
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
is Backend -> callback()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun bounceTunnel() {
|
||||||
_vpnState.value.tunnelConfig?.let {
|
_vpnState.value.tunnelConfig?.let {
|
||||||
appDataRepository.tunnels.save(it.copy(isActive = true))
|
withServiceActive {
|
||||||
|
toggleTunnel(it)
|
||||||
|
toggleTunnel(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onStartFailed() {
|
override suspend fun getBackendState(): BackendState {
|
||||||
_vpnState.value.tunnelConfig?.let {
|
return when (val backend = backend()) {
|
||||||
appDataRepository.tunnels.save(it.copy(isActive = false))
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
|
backend.backendState.asBackendState()
|
||||||
|
}
|
||||||
|
is Backend -> BackendState.SERVICE_ACTIVE
|
||||||
|
else -> BackendState.INACTIVE
|
||||||
}
|
}
|
||||||
cancelStatsJob()
|
|
||||||
resetBackendStatistics()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onBeforeStart(tunnelConfig: TunnelConfig) {
|
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||||
if (_vpnState.value.status == TunnelState.UP) vpnState.value.tunnelConfig?.let { stopTunnel(it) }
|
kotlin.runCatching {
|
||||||
resetBackendStatistics()
|
when (val backend = backend()) {
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
emitVpnStateConfig(tunnelConfig)
|
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
||||||
startStatsJob()
|
}
|
||||||
|
is Backend -> {
|
||||||
|
// TODO not yet implemented
|
||||||
|
Timber.d("Kernel backend state not yet implemented")
|
||||||
|
}
|
||||||
|
else -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) {
|
private suspend fun shutDownActiveTunnel() {
|
||||||
cancelStatsJob()
|
with(_vpnState.value) {
|
||||||
|
if (status.isUp()) {
|
||||||
|
stopTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun startBackgroundService() {
|
||||||
|
serviceManager.startBackgroundService()
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopBackgroundService() {
|
||||||
|
serviceManager.stopBackgroundService()
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun onBeforeStart(background: Boolean) {
|
||||||
|
shutDownActiveTunnel()
|
||||||
resetBackendStatistics()
|
resetBackendStatistics()
|
||||||
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
if (background || settings.isKernelEnabled) startBackgroundService()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun onStop(tunnelConfig: TunnelConfig) {
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||||
|
cancelActiveTunnelJobs()
|
||||||
|
resetBackendStatistics()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitTunnelState(state: TunnelState) {
|
private fun updateTunnelState(state: TunnelState, tunnelConfig: TunnelConfig?) {
|
||||||
_vpnState.tryEmit(
|
_vpnState.update {
|
||||||
_vpnState.value.copy(
|
it.copy(status = state, tunnelConfig = tunnelConfig)
|
||||||
status = state,
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitBackendStatistics(statistics: TunnelStatistics) {
|
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) {
|
||||||
_vpnState.tryEmit(
|
_vpnState.update {
|
||||||
_vpnState.value.copy(
|
it.copy(tunnelConfig = tunnelConfig)
|
||||||
statistics = statistics,
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitVpnStateConfig(tunnelConfig: TunnelConfig) {
|
private fun updateBackendStatistics(statistics: TunnelStatistics) {
|
||||||
_vpnState.tryEmit(
|
_vpnState.update {
|
||||||
_vpnState.value.copy(
|
it.copy(statistics = statistics)
|
||||||
tunnelConfig = tunnelConfig,
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resetBackendStatistics() {
|
private fun resetBackendStatistics() {
|
||||||
_vpnState.tryEmit(
|
_vpnState.update {
|
||||||
_vpnState.value.copy(
|
it.copy(statistics = null)
|
||||||
statistics = null,
|
}
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getState(): TunnelState {
|
override suspend fun getState(): TunnelState {
|
||||||
@@ -197,12 +279,14 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelStatsJob() {
|
override fun cancelActiveTunnelJobs() {
|
||||||
statsJob?.cancel()
|
statsJob?.cancel()
|
||||||
|
tunnelChangesJob?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startStatsJob() {
|
override fun startActiveTunnelJobs() {
|
||||||
statsJob = startTunnelStatisticsJob()
|
statsJob = startTunnelStatisticsJob()
|
||||||
|
tunnelChangesJob = startTunnelConfigChangesJob()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getName(): String {
|
override fun getName(): String {
|
||||||
@@ -214,11 +298,11 @@ constructor(
|
|||||||
delay(STATS_START_DELAY)
|
delay(STATS_START_DELAY)
|
||||||
while (true) {
|
while (true) {
|
||||||
when (backend) {
|
when (backend) {
|
||||||
is Backend -> emitBackendStatistics(
|
is Backend -> updateBackendStatistics(
|
||||||
WireGuardStatistics(backend.getStatistics(this@WireGuardTunnel)),
|
WireGuardStatistics(backend.getStatistics(this@WireGuardTunnel)),
|
||||||
)
|
)
|
||||||
is org.amnezia.awg.backend.Backend -> {
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
emitBackendStatistics(
|
updateBackendStatistics(
|
||||||
AmneziaStatistics(
|
AmneziaStatistics(
|
||||||
backend.getStatistics(this@WireGuardTunnel),
|
backend.getStatistics(this@WireGuardTunnel),
|
||||||
),
|
),
|
||||||
@@ -229,17 +313,38 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun startTunnelConfigChangesJob() = applicationScope.launch(ioDispatcher) {
|
||||||
|
tunnelConfigRepository.getTunnelConfigsFlow().collect {
|
||||||
|
with(_vpnState.value) {
|
||||||
|
if (status.isDown() || tunnelConfig == null) return@collect
|
||||||
|
val vpnConfigFromStorage = it.first { it.id == tunnelConfig.id }
|
||||||
|
val isRestartNeeded = vpnConfigFromStorage.wgQuick != tunnelConfig.wgQuick ||
|
||||||
|
vpnConfigFromStorage.amQuick != tunnelConfig.amQuick
|
||||||
|
updateTunnelConfig(vpnConfigFromStorage)
|
||||||
|
if (isRestartNeeded) {
|
||||||
|
Timber.d("Bouncing tunnel on config change")
|
||||||
|
bounceTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStateChange(newState: Tunnel.State) {
|
override fun onStateChange(newState: Tunnel.State) {
|
||||||
emitTunnelState(TunnelState.from(newState))
|
_vpnState.update {
|
||||||
|
it.copy(status = TunnelState.from(newState))
|
||||||
|
}
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStateChange(state: State) {
|
override fun onStateChange(state: State) {
|
||||||
emitTunnelState(TunnelState.from(state))
|
_vpnState.update {
|
||||||
|
it.copy(status = TunnelState.from(state))
|
||||||
|
}
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val STATS_START_DELAY = 5_000L
|
const val STATS_START_DELAY = 1_000L
|
||||||
const val VPN_STATISTIC_CHECK_INTERVAL = 1_000L
|
const val VPN_STATISTIC_CHECK_INTERVAL = 1_000L
|
||||||
const val VPN_RESTART_DELAY = 1_000L
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ data class AppUiState(
|
|||||||
val tunnels: List<TunnelConfig> = emptyList(),
|
val tunnels: List<TunnelConfig> = emptyList(),
|
||||||
val vpnState: VpnState = VpnState(),
|
val vpnState: VpnState = VpnState(),
|
||||||
val generalState: GeneralState = GeneralState(),
|
val generalState: GeneralState = GeneralState(),
|
||||||
|
val autoTunnelActive: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,61 +2,118 @@ package com.zaneschepke.wireguardautotunnel.ui
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.navigation.NavHostController
|
import com.wireguard.android.backend.WgQuickBackend
|
||||||
|
import com.wireguard.android.util.RootShell
|
||||||
|
import com.zaneschepke.logcatter.LogReader
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.AppShell
|
||||||
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.BackendState
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
|
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.extensions.TunnelConfigs
|
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.onCompletion
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.flow.takeWhile
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.plus
|
import kotlinx.coroutines.plus
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
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(
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
tunnelService: TunnelService,
|
private val tunnelService: Provider<TunnelService>,
|
||||||
val navHostController: NavHostController,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
@AppShell private val rootShell: Provider<RootShell>,
|
||||||
|
private val serviceManager: ServiceManager,
|
||||||
|
private val logReader: LogReader,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _appUiState = MutableStateFlow(AppUiState())
|
|
||||||
|
|
||||||
val uiState =
|
val uiState =
|
||||||
combine(
|
combine(
|
||||||
appDataRepository.settings.getSettingsFlow(),
|
appDataRepository.settings.getSettingsFlow(),
|
||||||
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
appDataRepository.tunnels.getTunnelConfigsFlow(),
|
||||||
tunnelService.vpnState,
|
tunnelService.get().vpnState,
|
||||||
appDataRepository.appState.generalStateFlow,
|
appDataRepository.appState.generalStateFlow,
|
||||||
) { settings, tunnels, tunnelState, generalState ->
|
serviceManager.autoTunnelActive,
|
||||||
|
) { settings, tunnels, tunnelState, generalState, autoTunnel ->
|
||||||
AppUiState(
|
AppUiState(
|
||||||
settings,
|
settings,
|
||||||
tunnels,
|
tunnels,
|
||||||
tunnelState,
|
tunnelState,
|
||||||
generalState,
|
generalState,
|
||||||
|
autoTunnel,
|
||||||
)
|
)
|
||||||
}
|
}.stateIn(
|
||||||
.stateIn(
|
viewModelScope + ioDispatcher,
|
||||||
viewModelScope + ioDispatcher,
|
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
AppUiState(),
|
||||||
_appUiState.value,
|
|
||||||
)
|
|
||||||
|
|
||||||
fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch(ioDispatcher) {
|
|
||||||
_appUiState.emit(
|
|
||||||
_appUiState.value.copy(
|
|
||||||
tunnels = tunnels,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val _isAppReady = MutableStateFlow(false)
|
||||||
|
val isAppReady = _isAppReady.asStateFlow()
|
||||||
|
|
||||||
|
private val _configurationChange = MutableStateFlow(false)
|
||||||
|
val configurationChange = _configurationChange.asStateFlow()
|
||||||
|
|
||||||
|
init {
|
||||||
|
viewModelScope.launch {
|
||||||
|
initPin()
|
||||||
|
initServices()
|
||||||
|
initTunnel()
|
||||||
|
appReadyCheck()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun appReadyCheck() {
|
||||||
|
val tunnelCount = appDataRepository.tunnels.count()
|
||||||
|
uiState.takeWhile { it.tunnels.size != tunnelCount }.onCompletion {
|
||||||
|
_isAppReady.emit(true)
|
||||||
|
}.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun initTunnel() {
|
||||||
|
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startActiveTunnelJobs()
|
||||||
|
val activeTunnels = appDataRepository.tunnels.getActive()
|
||||||
|
if (activeTunnels.isNotEmpty() &&
|
||||||
|
tunnelService.get().getState() == TunnelState.DOWN
|
||||||
|
) {
|
||||||
|
tunnelService.get().startTunnel(activeTunnels.first())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun initPin() {
|
||||||
|
val isPinEnabled = appDataRepository.appState.isPinLockEnabled()
|
||||||
|
if (isPinEnabled) PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun initServices() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
handleVpnKillSwitchChange(settings.isVpnKillSwitchEnabled)
|
||||||
|
if (settings.isAutoTunnelEnabled) serviceManager.startAutoTunnel(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
||||||
@@ -67,4 +124,146 @@ constructor(
|
|||||||
fun onPinLockEnabled() = viewModelScope.launch {
|
fun onPinLockEnabled() = viewModelScope.launch {
|
||||||
appDataRepository.appState.setPinLockEnabled(true)
|
appDataRepository.appState.setPinLockEnabled(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setLocationDisclosureShown() = viewModelScope.launch {
|
||||||
|
appDataRepository.appState.setLocationDisclosureShown(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleLocalLogging() = viewModelScope.launch(ioDispatcher) {
|
||||||
|
with(uiState.value.generalState) {
|
||||||
|
val toggledOn = !isLocalLogsEnabled
|
||||||
|
appDataRepository.appState.setLocalLogsEnabled(toggledOn)
|
||||||
|
if (!toggledOn) onLoggerStop()
|
||||||
|
_configurationChange.update {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun onLoggerStop() {
|
||||||
|
logReader.deleteAndClearLogs()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleAlwaysOnVPN() = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isAlwaysOnVpnEnabled = !isAlwaysOnVpnEnabled,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onLocaleChange(localeTag: String) = viewModelScope.launch {
|
||||||
|
appDataRepository.appState.setLocale(localeTag)
|
||||||
|
LocaleUtil.changeLocale(localeTag)
|
||||||
|
_configurationChange.update {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleRestartAtBoot() = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isRestoreOnBootEnabled = !isRestoreOnBootEnabled,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleVpnKillSwitch(enabled: Boolean) = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isVpnKillSwitchEnabled = enabled,
|
||||||
|
isLanOnKillSwitchEnabled = if (enabled) isLanOnKillSwitchEnabled else false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
handleVpnKillSwitchChange(enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleVpnKillSwitchChange(enabled: Boolean) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
if (enabled) {
|
||||||
|
Timber.d("Starting kill switch")
|
||||||
|
val allowedIps = if (appDataRepository.settings.getSettings().isLanOnKillSwitchEnabled) {
|
||||||
|
TunnelConfig.IPV4_PUBLIC_NETWORKS
|
||||||
|
} else {
|
||||||
|
emptySet()
|
||||||
|
}
|
||||||
|
tunnelService.get().setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||||
|
} else {
|
||||||
|
Timber.d("Sending shutdown of kill switch")
|
||||||
|
tunnelService.get().setBackendState(BackendState.SERVICE_ACTIVE, emptySet())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleLanOnKillSwitch(enabled: Boolean) = viewModelScope.launch(ioDispatcher) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isLanOnKillSwitchEnabled = enabled,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
val allowedIps = if (enabled) TunnelConfig.IPV4_PUBLIC_NETWORKS else emptySet()
|
||||||
|
Timber.d("Setting allowedIps $allowedIps")
|
||||||
|
tunnelService.get().setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleShortcutsEnabled() = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isShortcutsEnabled = !isShortcutsEnabled,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveKernelMode(enabled: Boolean) = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
this.copy(
|
||||||
|
isKernelEnabled = enabled,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleKernelMode() = viewModelScope.launch {
|
||||||
|
with(uiState.value.settings) {
|
||||||
|
if (!isKernelEnabled) {
|
||||||
|
requestRoot().onSuccess {
|
||||||
|
if (!isKernelSupported()) return@onSuccess SnackbarController.showMessage(StringValue.StringResource(R.string.kernel_not_supported))
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isKernelEnabled = true,
|
||||||
|
isAmneziaEnabled = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
saveKernelMode(enabled = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun isKernelSupported(): Boolean {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
WgQuickBackend.hasKernelSupport()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun requestRoot(): Result<Unit> {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
rootShell.get().start()
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.root_accepted))
|
||||||
|
}.onFailure {
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.error_root_denied))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.SystemBarStyle
|
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.core.tween
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.animation.fadeIn
|
||||||
import androidx.compose.animation.fadeOut
|
import androidx.compose.animation.fadeOut
|
||||||
import androidx.compose.foundation.focusable
|
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.Icons
|
||||||
@@ -19,49 +20,51 @@ import androidx.compose.material3.MaterialTheme
|
|||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.SnackbarData
|
import androidx.compose.material3.SnackbarData
|
||||||
import androidx.compose.material3.SnackbarHost
|
import androidx.compose.material3.SnackbarHost
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
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.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
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.hilt.navigation.compose.hiltViewModel
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.rememberNavController
|
||||||
import androidx.navigation.toRoute
|
import androidx.navigation.toRoute
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
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.navigation.BottomNavItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.isCurrentRoute
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
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.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.settings.killswitch.KillSwitchScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.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.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appStateRepository: AppStateRepository
|
lateinit var appStateRepository: AppStateRepository
|
||||||
|
|
||||||
@@ -71,133 +74,143 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
val isPinLockEnabled = intent.extras?.getBoolean(SplashActivity.IS_PIN_LOCK_ENABLED_KEY)
|
val viewModel by viewModels<AppViewModel>()
|
||||||
|
|
||||||
enableEdgeToEdge(
|
installSplashScreen().apply {
|
||||||
navigationBarStyle = SystemBarStyle.auto(
|
setKeepOnScreenCondition {
|
||||||
lightScrim = Color.Transparent.toArgb(),
|
!viewModel.isAppReady.value
|
||||||
darkScrim = Color.Transparent.toArgb(),
|
}
|
||||||
),
|
}
|
||||||
)
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val appViewModel = hiltViewModel<AppViewModel>()
|
val appUiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val appUiState by appViewModel.uiState.collectAsStateWithLifecycle(lifecycle = this.lifecycle)
|
val configurationChange by viewModel.configurationChange.collectAsStateWithLifecycle()
|
||||||
val navController = appViewModel.navHostController
|
val navController = rememberNavController()
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
|
||||||
|
|
||||||
LaunchedEffect(appUiState.vpnState.status) {
|
LaunchedEffect(configurationChange) {
|
||||||
val context = this@MainActivity
|
if (configurationChange) {
|
||||||
when (appUiState.vpnState.status) {
|
Intent(this@MainActivity, MainActivity::class.java).also {
|
||||||
TunnelState.DOWN -> ServiceManager.stopTunnelBackgroundService(context)
|
startActivity(it)
|
||||||
else -> Unit
|
exitProcess(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SnackbarControllerProvider { host ->
|
LaunchedEffect(appUiState.autoTunnelActive) {
|
||||||
WireguardAutoTunnelTheme {
|
requestAutoTunnelTileServiceUpdate()
|
||||||
val focusRequester = remember { FocusRequester() }
|
}
|
||||||
Scaffold(
|
|
||||||
snackbarHost = {
|
with(appUiState.settings) {
|
||||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
LaunchedEffect(isAutoTunnelEnabled) {
|
||||||
CustomSnackBar(
|
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
||||||
snackbarData.visuals.message,
|
}
|
||||||
isRtl = false,
|
}
|
||||||
containerColor =
|
|
||||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
CompositionLocalProvider(LocalNavController provides navController) {
|
||||||
2.dp,
|
SnackbarControllerProvider { host ->
|
||||||
),
|
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
||||||
)
|
Scaffold(
|
||||||
}
|
contentWindowInsets = WindowInsets(0.dp),
|
||||||
},
|
snackbarHost = {
|
||||||
containerColor = MaterialTheme.colorScheme.background,
|
SnackbarHost(host) { snackbarData: SnackbarData ->
|
||||||
modifier =
|
CustomSnackBar(
|
||||||
Modifier
|
snackbarData.visuals.message,
|
||||||
.focusable()
|
isRtl = false,
|
||||||
.focusProperties {
|
containerColor =
|
||||||
if (navBackStackEntry?.isCurrentRoute(Route.Lock) == true) {
|
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||||
Unit
|
2.dp,
|
||||||
} else {
|
),
|
||||||
up = focusRequester
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
bottomBar = {
|
bottomBar = {
|
||||||
BottomNavBar(
|
BottomNavBar(
|
||||||
navController,
|
navController,
|
||||||
listOf(
|
listOf(
|
||||||
BottomNavItem(
|
BottomNavItem(
|
||||||
name = stringResource(R.string.tunnels),
|
name = stringResource(R.string.tunnels),
|
||||||
route = Route.Main,
|
route = Route.Main,
|
||||||
icon = Icons.Rounded.Home,
|
icon = Icons.Rounded.Home,
|
||||||
|
),
|
||||||
|
BottomNavItem(
|
||||||
|
name = stringResource(R.string.settings),
|
||||||
|
route = Route.Settings,
|
||||||
|
icon = Icons.Rounded.Settings,
|
||||||
|
),
|
||||||
|
BottomNavItem(
|
||||||
|
name = stringResource(R.string.support),
|
||||||
|
route = Route.Support,
|
||||||
|
icon = Icons.Rounded.QuestionMark,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
BottomNavItem(
|
)
|
||||||
name = stringResource(R.string.settings),
|
},
|
||||||
route = Route.Settings,
|
) { padding ->
|
||||||
icon = Icons.Rounded.Settings,
|
Box(modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||||
),
|
NavHost(
|
||||||
BottomNavItem(
|
navController,
|
||||||
name = stringResource(R.string.support),
|
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||||
route = Route.Support,
|
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||||
icon = Icons.Rounded.QuestionMark,
|
startDestination = (if (appUiState.generalState.isPinLockEnabled) Route.Lock else Route.Main),
|
||||||
),
|
) {
|
||||||
),
|
composable<Route.Main> {
|
||||||
)
|
MainScreen(
|
||||||
},
|
uiState = appUiState,
|
||||||
) { padding ->
|
)
|
||||||
Surface(modifier = Modifier.fillMaxSize().padding(padding)) {
|
}
|
||||||
NavHost(
|
composable<Route.Settings> {
|
||||||
navController,
|
SettingsScreen(
|
||||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
appViewModel = viewModel,
|
||||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
uiState = appUiState,
|
||||||
startDestination = (if (isPinLockEnabled == true) Route.Lock else Route.Main),
|
)
|
||||||
) {
|
}
|
||||||
composable<Route.Main> {
|
composable<Route.LocationDisclosure> {
|
||||||
MainScreen(
|
LocationDisclosureScreen(viewModel, appUiState)
|
||||||
focusRequester = focusRequester,
|
}
|
||||||
uiState = appUiState,
|
composable<Route.AutoTunnel> {
|
||||||
navController = navController,
|
AutoTunnelScreen(
|
||||||
)
|
appUiState,
|
||||||
}
|
)
|
||||||
composable<Route.Settings> {
|
}
|
||||||
SettingsScreen(
|
composable<Route.Appearance> {
|
||||||
appViewModel = appViewModel,
|
AppearanceScreen()
|
||||||
uiState = appUiState,
|
}
|
||||||
navController = navController,
|
composable<Route.Language> {
|
||||||
focusRequester = focusRequester,
|
LanguageScreen(appUiState, viewModel)
|
||||||
)
|
}
|
||||||
}
|
composable<Route.Display> {
|
||||||
composable<Route.Support> {
|
DisplayScreen(appUiState)
|
||||||
SupportScreen(
|
}
|
||||||
focusRequester = focusRequester,
|
composable<Route.Support> {
|
||||||
navController = navController,
|
SupportScreen(appUiState, viewModel)
|
||||||
appUiState = appUiState,
|
}
|
||||||
)
|
composable<Route.Logs> {
|
||||||
}
|
LogsScreen()
|
||||||
composable<Route.Logs> {
|
}
|
||||||
LogsScreen()
|
composable<Route.Config> {
|
||||||
}
|
val args = it.toRoute<Route.Config>()
|
||||||
composable<Route.Config> {
|
ConfigScreen(
|
||||||
val args = it.toRoute<Route.Config>()
|
tunnelId = args.id,
|
||||||
ConfigScreen(
|
)
|
||||||
focusRequester = focusRequester,
|
}
|
||||||
tunnelId = args.id,
|
composable<Route.Option> {
|
||||||
)
|
val args = it.toRoute<Route.Option>()
|
||||||
}
|
OptionsScreen(
|
||||||
composable<Route.Option> {
|
tunnelId = args.id,
|
||||||
val args = it.toRoute<Route.Option>()
|
appUiState = appUiState,
|
||||||
OptionsScreen(
|
)
|
||||||
navController = navController,
|
}
|
||||||
tunnelId = args.id,
|
composable<Route.Lock> {
|
||||||
focusRequester = focusRequester,
|
PinLockScreen(
|
||||||
appUiState = appUiState,
|
appViewModel = viewModel,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
composable<Route.Lock> {
|
composable<Route.Scanner> {
|
||||||
PinLockScreen(
|
ScannerScreen()
|
||||||
navController = navController,
|
}
|
||||||
appViewModel = appViewModel,
|
composable<Route.KillSwitch> {
|
||||||
)
|
KillSwitchScreen(appUiState, viewModel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,9 +219,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
tunnelService.cancelStatsJob()
|
// save battery by not polling stats while app is closed
|
||||||
|
tunnelService.cancelActiveTunnelJobs()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,24 @@ sealed class Route {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data object Settings : Route()
|
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 KillSwitch : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data object Language : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data object Main : Route()
|
data object Main : Route()
|
||||||
|
|
||||||
@@ -20,6 +38,9 @@ sealed class Route {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data object Lock : Route()
|
data object Lock : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data object Scanner : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Config(
|
data class Config(
|
||||||
val id: Int,
|
val id: Int,
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
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.activity.viewModels
|
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import androidx.lifecycle.repeatOnLifecycle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@SuppressLint("CustomSplashScreen")
|
|
||||||
@AndroidEntryPoint
|
|
||||||
class SplashActivity : ComponentActivity() {
|
|
||||||
@Inject
|
|
||||||
lateinit var appStateRepository: AppStateRepository
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var appDataRepository: AppDataRepository
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
private val appViewModel: AppViewModel by viewModels()
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
||||||
val splashScreen = installSplashScreen()
|
|
||||||
splashScreen.setKeepOnScreenCondition { true }
|
|
||||||
}
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
|
|
||||||
lifecycleScope.launch {
|
|
||||||
repeatOnLifecycle(Lifecycle.State.CREATED) {
|
|
||||||
val pinLockEnabled = async {
|
|
||||||
appStateRepository.isPinLockEnabled().also {
|
|
||||||
if (it) PinManager.initialize(WireGuardAutoTunnel.instance)
|
|
||||||
}
|
|
||||||
}.await()
|
|
||||||
async {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (settings.isAutoTunnelEnabled) ServiceManager.startWatcherService(application.applicationContext)
|
|
||||||
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
|
|
||||||
val activeTunnels = appDataRepository.tunnels.getActive()
|
|
||||||
if (activeTunnels.isNotEmpty() &&
|
|
||||||
tunnelService.get().getState() == TunnelState.DOWN
|
|
||||||
) {
|
|
||||||
tunnelService.get().startTunnel(activeTunnels.first())
|
|
||||||
}
|
|
||||||
}.await()
|
|
||||||
|
|
||||||
async {
|
|
||||||
val tunnels = appDataRepository.tunnels.getAll()
|
|
||||||
appViewModel.setTunnels(tunnels)
|
|
||||||
}.await()
|
|
||||||
|
|
||||||
requestAutoTunnelTileServiceUpdate()
|
|
||||||
|
|
||||||
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
-1
@@ -5,6 +5,7 @@ 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
|
||||||
@@ -17,7 +18,7 @@ fun ClickableIconButton(onClick: () -> Unit, onIconClick: () -> Unit, text: Stri
|
|||||||
onClick = onClick,
|
onClick = onClick,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
) {
|
) {
|
||||||
Text(text, Modifier.weight(1f, false))
|
Text(text, Modifier.weight(1f, false), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.primary)
|
||||||
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = icon,
|
imageVector = icon,
|
||||||
|
|||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
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
@@ -0,0 +1,13 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.ui.geometry.Offset
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||||
|
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
||||||
|
|
||||||
|
class NestedScrollListener(val onUp: () -> Unit, val onDown: () -> Unit) : NestedScrollConnection {
|
||||||
|
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
||||||
|
if (available.y < -1) onDown()
|
||||||
|
if (available.y > 1) onUp()
|
||||||
|
return Offset.Zero
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,99 +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.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.extensions.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+99
@@ -0,0 +1,99 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
@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
@@ -0,0 +1,18 @@
|
|||||||
|
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
@@ -0,0 +1,63 @@
|
|||||||
|
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
@@ -0,0 +1,13 @@
|
|||||||
|
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
@@ -0,0 +1,86 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-5
@@ -3,35 +3,33 @@ 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.foundation.layout.padding
|
import androidx.compose.material3.MaterialTheme
|
||||||
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,
|
label: String,
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
checked: Boolean,
|
checked: Boolean,
|
||||||
padding: Dp,
|
|
||||||
onCheckChanged: (checked: Boolean) -> Unit,
|
onCheckChanged: (checked: Boolean) -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
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(
|
||||||
|
|||||||
+20
-17
@@ -3,6 +3,7 @@ package com.zaneschepke.wireguardautotunnel.ui.common.config
|
|||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
import androidx.compose.foundation.interaction.collectIsFocusedAsState
|
import androidx.compose.foundation.interaction.collectIsFocusedAsState
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
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.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
@@ -10,7 +11,6 @@ import androidx.compose.material.icons.outlined.Save
|
|||||||
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.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
@@ -18,21 +18,19 @@ import androidx.compose.runtime.mutableStateOf
|
|||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.setValue
|
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.platform.LocalFocusManager
|
import androidx.compose.ui.platform.LocalFocusManager
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
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.ImeAction
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SubmitConfigurationTextBox(
|
fun SubmitConfigurationTextBox(
|
||||||
value: String?,
|
value: String?,
|
||||||
label: String,
|
label: String,
|
||||||
hint: String,
|
hint: String,
|
||||||
focusRequester: FocusRequester,
|
|
||||||
isErrorValue: (value: String?) -> Boolean,
|
isErrorValue: (value: String?) -> Boolean,
|
||||||
onSubmit: (value: String) -> Unit,
|
onSubmit: (value: String) -> Unit,
|
||||||
keyboardOptions: KeyboardOptions = KeyboardOptions(
|
keyboardOptions: KeyboardOptions = KeyboardOptions(
|
||||||
@@ -47,18 +45,22 @@ fun SubmitConfigurationTextBox(
|
|||||||
|
|
||||||
var stateValue by remember { mutableStateOf(value ?: "") }
|
var stateValue by remember { mutableStateOf(value ?: "") }
|
||||||
|
|
||||||
OutlinedTextField(
|
CustomTextField(
|
||||||
isError = isErrorValue(stateValue),
|
isError = isErrorValue(stateValue),
|
||||||
modifier = Modifier
|
textStyle = MaterialTheme.typography.bodySmall,
|
||||||
.fillMaxWidth()
|
|
||||||
.focusRequester(focusRequester),
|
|
||||||
value = stateValue,
|
value = stateValue,
|
||||||
singleLine = true,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
onValueChange = { stateValue = it },
|
onValueChange = { stateValue = it },
|
||||||
|
interactionSource = interactionSource,
|
||||||
label = { Text(label) },
|
label = { Text(label) },
|
||||||
maxLines = 1,
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
placeholder = { Text(hint) },
|
placeholder = { Text(hint, style = MaterialTheme.typography.bodySmall) },
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.padding(
|
||||||
|
top = 5.dp,
|
||||||
|
bottom = 10.dp,
|
||||||
|
).fillMaxWidth().padding(end = 16.dp.scaledWidth()),
|
||||||
|
singleLine = true,
|
||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions = keyboardOptions,
|
||||||
keyboardActions = KeyboardActions(
|
keyboardActions = KeyboardActions(
|
||||||
onDone = {
|
onDone = {
|
||||||
@@ -66,16 +68,17 @@ fun SubmitConfigurationTextBox(
|
|||||||
keyboardController?.hide()
|
keyboardController?.hide()
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
trailingIcon = {
|
trailing = {
|
||||||
if (!isErrorValue(stateValue) && isFocused) {
|
if (!isErrorValue(stateValue) && isFocused) {
|
||||||
IconButton(onClick = {
|
IconButton(onClick = {
|
||||||
onSubmit(stateValue)
|
onSubmit(stateValue)
|
||||||
keyboardController?.hide()
|
keyboardController?.hide()
|
||||||
focusManager.clearFocus()
|
focusManager.clearFocus()
|
||||||
}) {
|
}) {
|
||||||
|
val icon = Icons.Outlined.Save
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = Icons.Outlined.Save,
|
imageVector = icon,
|
||||||
contentDescription = stringResource(R.string.save_changes),
|
contentDescription = icon.name,
|
||||||
tint = MaterialTheme.colorScheme.primary,
|
tint = MaterialTheme.colorScheme.primary,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -10,13 +10,20 @@ import androidx.activity.compose.rememberLauncherForActivityResult
|
|||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun rememberFileImportLauncherForResult(onNoFileExplorer: () -> Unit, onData: (data: Uri) -> Unit): ManagedActivityResultLauncher<String, Uri?> {
|
fun rememberFileImportLauncherForResult(onNoFileExplorer: () -> Unit, onData: (data: Uri) -> Unit): ManagedActivityResultLauncher<String, Uri?> {
|
||||||
return rememberLauncherForActivityResult(
|
return rememberLauncherForActivityResult(
|
||||||
object : ActivityResultContracts.GetContent() {
|
object : ActivityResultContracts.GetContent() {
|
||||||
override fun createIntent(context: Context, input: String): Intent {
|
override fun createIntent(context: Context, input: String): Intent {
|
||||||
val intent = super.createIntent(context, input)
|
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
|
/* 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. */
|
* what we can do, so detect this and throw an exception that we can catch later. */
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
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))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-11
@@ -10,10 +10,7 @@ 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.text.font.FontWeight
|
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
|
||||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
|
||||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
|
|
||||||
@@ -22,18 +19,16 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavIte
|
|||||||
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
var showBottomBar by rememberSaveable { mutableStateOf(true) }
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
|
|
||||||
showBottomBar = bottomNavItems.firstOrNull {
|
showBottomBar = bottomNavItems.any {
|
||||||
navBackStackEntry?.destination?.hierarchy?.any { dest ->
|
navBackStackEntry?.isCurrentRoute(it.route::class) == true
|
||||||
bottomNavItems.map { dest.hasRoute(route = it.route::class) }.contains(true)
|
}
|
||||||
} == true
|
|
||||||
} != null
|
|
||||||
|
|
||||||
if (showBottomBar) {
|
if (showBottomBar) {
|
||||||
NavigationBar(
|
NavigationBar(
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
) {
|
) {
|
||||||
bottomNavItems.forEach { item ->
|
bottomNavItems.forEachIndexed { index, item ->
|
||||||
val selected = navBackStackEntry.isCurrentRoute(item.route)
|
val selected = navBackStackEntry.isCurrentRoute(item.route::class)
|
||||||
NavigationBarItem(
|
NavigationBarItem(
|
||||||
selected = selected,
|
selected = selected,
|
||||||
onClick = {
|
onClick = {
|
||||||
@@ -53,7 +48,7 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavIte
|
|||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = item.name,
|
text = item.name,
|
||||||
fontWeight = FontWeight.SemiBold,
|
style = MaterialTheme.typography.labelMedium,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
icon = {
|
icon = {
|
||||||
|
|||||||
+5
-2
@@ -1,12 +1,15 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import androidx.navigation.NavBackStackEntry
|
import androidx.navigation.NavBackStackEntry
|
||||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
import androidx.navigation.NavDestination.Companion.hasRoute
|
||||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
import androidx.navigation.NavDestination.Companion.hierarchy
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
fun NavBackStackEntry?.isCurrentRoute(route: Route): Boolean {
|
@SuppressLint("RestrictedApi")
|
||||||
|
fun <T : Route> NavBackStackEntry?.isCurrentRoute(cls: KClass<T>): Boolean {
|
||||||
return this?.destination?.hierarchy?.any {
|
return this?.destination?.hierarchy?.any {
|
||||||
it.hasRoute(route = route::class)
|
it.hasRoute(route = cls)
|
||||||
} == true
|
} == true
|
||||||
}
|
}
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
|
import androidx.navigation.NavHostController
|
||||||
|
|
||||||
|
val LocalNavController = compositionLocalOf<NavHostController> {
|
||||||
|
error("NavController was not provided")
|
||||||
|
}
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import androidx.navigation.compose.ComposeNavigator
|
|
||||||
import androidx.navigation.compose.DialogNavigator
|
|
||||||
|
|
||||||
class NavigationService constructor(
|
|
||||||
context: Context,
|
|
||||||
) {
|
|
||||||
val navController = NavHostController(context).apply {
|
|
||||||
navigatorProvider.addNavigator(ComposeNavigator())
|
|
||||||
navigatorProvider.addNavigator(DialogNavigator())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
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()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
package com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn
|
||||||
|
|
||||||
import androidx.compose.foundation.text.ClickableText
|
import androidx.compose.foundation.text.ClickableText
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn
|
||||||
|
|
||||||
|
import android.net.VpnService
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
inline fun <T> withVpnPermission(crossinline onSuccess: (t: T) -> Unit): (t: T) -> Unit {
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
val vpnActivity =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
onResult = {
|
||||||
|
if (it.resultCode != RESULT_OK) showVpnPermissionDialog = true
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
||||||
|
|
||||||
|
return {
|
||||||
|
val intent = VpnService.prepare(context)
|
||||||
|
if (intent != null) {
|
||||||
|
vpnActivity.launch(intent)
|
||||||
|
} else {
|
||||||
|
onSuccess(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.permission
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.Settings
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
|
import androidx.activity.result.ActivityResult
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isBatteryOptimizationsDisabled
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
inline fun withIgnoreBatteryOpt(ignore: Boolean, crossinline callback: () -> Unit): () -> Unit {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val batteryActivity =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
|
) { result: ActivityResult ->
|
||||||
|
// we only ask once
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
if (ignore || context.isBatteryOptimizationsDisabled()) {
|
||||||
|
callback()
|
||||||
|
} else {
|
||||||
|
batteryActivity.launch(
|
||||||
|
Intent().apply {
|
||||||
|
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||||
|
data = Uri.parse("package:${context.packageName}")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-27
@@ -1,27 +0,0 @@
|
|||||||
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() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
|
package com.zaneschepke.wireguardautotunnel.ui.common.snackbar
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
+2
-4
@@ -1,22 +1,20 @@
|
|||||||
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 = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.ExtraBold),
|
style = MaterialTheme.typography.titleMedium,
|
||||||
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
|
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
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.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,
|
||||||
|
interactionSource: 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,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
-42
@@ -1,6 +1,5 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
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
|
||||||
@@ -34,6 +33,7 @@ import androidx.compose.material3.Text
|
|||||||
import androidx.compose.material3.TextButton
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.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
|
||||||
@@ -56,8 +56,11 @@ 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 com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
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.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.snackbar.SnackbarController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||||
@@ -65,11 +68,11 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.config.components.Applicat
|
|||||||
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.extensions.isRunningOnTv
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
fun ConfigScreen(tunnelId: Int) {
|
||||||
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
||||||
factory.create(tunnelId)
|
factory.create(tunnelId)
|
||||||
}
|
}
|
||||||
@@ -78,22 +81,24 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
val snackbar = SnackbarController.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) }
|
||||||
var configType by remember { mutableStateOf(ConfigType.WIREGUARD) }
|
|
||||||
|
|
||||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
var configType by remember { mutableStateOf<ConfigType?>(null) }
|
||||||
|
val derivedConfigType = remember {
|
||||||
|
derivedStateOf {
|
||||||
|
configType ?: if (!uiState.isAmneziaEnabled) ConfigType.WIREGUARD else ConfigType.AMNEZIA
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val saved by viewModel.saved.collectAsStateWithLifecycle(null)
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(saved) {
|
||||||
if (!uiState.loading && context.isRunningOnTv()) {
|
if (saved == true) {
|
||||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
navController.navigate(Route.Main)
|
||||||
kotlin.runCatching {
|
|
||||||
focusRequester.requestFocus()
|
|
||||||
}.onFailure {
|
|
||||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
|
||||||
focusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,6 +162,9 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
|
topBar = {
|
||||||
|
TopNavBar(stringResource(R.string.edit_tunnel))
|
||||||
|
},
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
floatingActionButtonPosition = FabPosition.End,
|
||||||
floatingActionButton = {
|
floatingActionButton = {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
@@ -173,8 +181,8 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) {
|
) { padding ->
|
||||||
Column {
|
Column(Modifier.padding(padding)) {
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
@@ -199,7 +207,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
Modifier.fillMaxWidth(fillMaxWidth)
|
Modifier.fillMaxWidth(fillMaxWidth)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.padding(bottom = 10.dp),
|
.padding(bottom = 10.dp.scaledHeight()).padding(top = 24.dp.scaledHeight()),
|
||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
@@ -215,10 +223,8 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
)
|
)
|
||||||
ConfigurationToggle(
|
ConfigurationToggle(
|
||||||
stringResource(id = R.string.show_amnezia_properties),
|
stringResource(id = R.string.show_amnezia_properties),
|
||||||
checked = configType == ConfigType.AMNEZIA,
|
checked = derivedConfigType.value == ConfigType.AMNEZIA,
|
||||||
padding = screenPadding,
|
|
||||||
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
||||||
modifier = Modifier.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.tunnelName,
|
value = uiState.tunnelName,
|
||||||
@@ -228,8 +234,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
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 =
|
||||||
@@ -238,12 +243,12 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
.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(
|
||||||
@@ -333,7 +338,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
modifier = Modifier.width(IntrinsicSize.Min),
|
modifier = Modifier.width(IntrinsicSize.Min),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (configType == ConfigType.AMNEZIA) {
|
if (derivedConfigType.value == ConfigType.AMNEZIA) {
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.junkPacketCount,
|
value = uiState.interfaceProxy.junkPacketCount,
|
||||||
onValueChange = viewModel::onJunkPacketCountChanged,
|
onValueChange = viewModel::onJunkPacketCountChanged,
|
||||||
@@ -342,8 +347,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
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,
|
||||||
@@ -356,8 +360,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.junkPacketMaxSize,
|
value = uiState.interfaceProxy.junkPacketMaxSize,
|
||||||
@@ -370,8 +373,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.initPacketJunkSize,
|
value = uiState.interfaceProxy.initPacketJunkSize,
|
||||||
@@ -381,8 +383,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
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,
|
||||||
@@ -395,8 +396,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.initPacketMagicHeader,
|
value = uiState.interfaceProxy.initPacketMagicHeader,
|
||||||
@@ -409,8 +409,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
||||||
@@ -423,8 +422,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
||||||
@@ -437,8 +435,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
ConfigurationTextBox(
|
ConfigurationTextBox(
|
||||||
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
||||||
@@ -451,8 +448,7 @@ fun ConfigScreen(tunnelId: Int, focusRequester: FocusRequester) {
|
|||||||
).lowercase(),
|
).lowercase(),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth(),
|
||||||
.focusRequester(focusRequester),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row(
|
Row(
|
||||||
|
|||||||
+1
@@ -74,6 +74,7 @@ data class ConfigUiState(
|
|||||||
return from(config).copy(
|
return from(config).copy(
|
||||||
tunnelName = tunnel.name,
|
tunnelName = tunnel.name,
|
||||||
tunnel = tunnel,
|
tunnel = tunnel,
|
||||||
|
isAmneziaEnabled = config.`interface`.junkPacketCount.isPresent,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -6,7 +6,6 @@ import android.content.pm.PackageManager
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.navigation.NavHostController
|
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import com.wireguard.config.Interface
|
import com.wireguard.config.Interface
|
||||||
import com.wireguard.config.Peer
|
import com.wireguard.config.Peer
|
||||||
@@ -17,7 +16,6 @@ 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.module.IoDispatcher
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
import com.zaneschepke.wireguardautotunnel.ui.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.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
@@ -30,8 +28,10 @@ import dagger.assisted.AssistedFactory
|
|||||||
import dagger.assisted.AssistedInject
|
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.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
import kotlinx.coroutines.flow.onStart
|
import kotlinx.coroutines.flow.onStart
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
@@ -44,12 +44,14 @@ class ConfigViewModel
|
|||||||
@AssistedInject
|
@AssistedInject
|
||||||
constructor(
|
constructor(
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
private val navController: NavHostController,
|
|
||||||
@Assisted val id: Int,
|
@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.onStart {
|
||||||
appDataRepository.tunnels.getById(id)?.let {
|
appDataRepository.tunnels.getById(id)?.let {
|
||||||
@@ -335,7 +337,7 @@ constructor(
|
|||||||
SnackbarController.showMessage(
|
SnackbarController.showMessage(
|
||||||
StringValue.StringResource(R.string.config_changes_saved),
|
StringValue.StringResource(R.string.config_changes_saved),
|
||||||
)
|
)
|
||||||
navController.navigate(Route.Main)
|
_saved.emit(true)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
||||||
|
|||||||
+1
-1
@@ -44,7 +44,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.joinToString(", ").replace("/", "").trim(),
|
dnsServers = (i.dnsServers + i.dnsSearchDomains).joinToString(", ").replace("/", "").trim(),
|
||||||
listenPort =
|
listenPort =
|
||||||
if (i.listenPort.isPresent) {
|
if (i.listenPort.isPresent) {
|
||||||
i.listenPort.get().toString().trim()
|
i.listenPort.get().toString().trim()
|
||||||
|
|||||||
+114
-395
@@ -1,42 +1,27 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
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.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.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.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.Add
|
import androidx.compose.material.icons.filled.Add
|
||||||
import androidx.compose.material.icons.rounded.Bolt
|
import androidx.compose.material.icons.outlined.Add
|
||||||
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.FabPosition
|
import androidx.compose.material3.FabPosition
|
||||||
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.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.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
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
|
||||||
@@ -44,104 +29,73 @@ 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.LocalClipboardManager
|
||||||
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.buildAnnotatedString
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.core.os.ConfigurationCompat
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.navigation.NavController
|
|
||||||
import com.journeyapps.barcodescanner.ScanContract
|
|
||||||
import com.journeyapps.barcodescanner.ScanOptions
|
|
||||||
import com.wireguard.android.backend.GoBackend
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.RowListItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.NestedScrollListener
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.permission.vpn.withVpnPermission
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.permission.withIgnoreBatteryOpt
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
import com.zaneschepke.wireguardautotunnel.ui.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.GettingStartedLabel
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissFab
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ScrollDismissFab
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.VpnDeniedDialog
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelRowItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.corn
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.mint
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.handshakeStatus
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.mapPeerStats
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import kotlinx.coroutines.delay
|
import java.text.Collator
|
||||||
import timber.log.Timber
|
|
||||||
|
|
||||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState, focusRequester: FocusRequester, navController: NavController) {
|
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) {
|
||||||
val haptic = LocalHapticFeedback.current
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val navController = LocalNavController.current
|
||||||
|
val clipboard = LocalClipboardManager.current
|
||||||
val snackbar = SnackbarController.current
|
val snackbar = SnackbarController.current
|
||||||
|
|
||||||
var showBottomSheet by remember { mutableStateOf(false) }
|
var showBottomSheet by remember { mutableStateOf(false) }
|
||||||
var showVpnPermissionDialog by remember { mutableStateOf(false) }
|
var isFabVisible by rememberSaveable { mutableStateOf(true) }
|
||||||
val isVisible = rememberSaveable { mutableStateOf(true) }
|
|
||||||
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
var showDeleteTunnelAlertDialog by remember { mutableStateOf(false) }
|
||||||
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
||||||
|
val isRunningOnTv = remember { context.isRunningOnTv() }
|
||||||
|
|
||||||
val nestedScrollConnection =
|
val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
|
||||||
remember {
|
val collator = Collator.getInstance(currentLocale)
|
||||||
object : NestedScrollConnection {
|
|
||||||
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
val sortedTunnels = remember(uiState.tunnels) {
|
||||||
// Hide FAB
|
uiState.tunnels.sortedWith(compareBy(collator) { it.name })
|
||||||
if (available.y < -1) {
|
}
|
||||||
isVisible.value = false
|
|
||||||
}
|
val startAutoTunnel = withVpnPermission<Unit> { viewModel.onToggleAutoTunnel() }
|
||||||
// Show FAB
|
val startTunnel = withVpnPermission<TunnelConfig> {
|
||||||
if (available.y > 1) {
|
viewModel.onTunnelStart(it, uiState.settings.isKernelEnabled)
|
||||||
isVisible.value = true
|
}
|
||||||
}
|
val autoTunnelToggleBattery = withIgnoreBatteryOpt(uiState.generalState.isBatteryOptimizationDisableShown) {
|
||||||
return Offset.Zero
|
if (!uiState.generalState.isBatteryOptimizationDisableShown) viewModel.setBatteryOptimizeDisableShown()
|
||||||
}
|
if (uiState.settings.isKernelEnabled) {
|
||||||
}
|
viewModel.onToggleAutoTunnel()
|
||||||
|
} else {
|
||||||
|
startAutoTunnel.invoke(Unit)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val vpnActivityResultState =
|
val nestedScrollConnection = remember {
|
||||||
rememberLauncherForActivityResult(
|
NestedScrollListener({ isFabVisible = false }, { isFabVisible = true })
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
onResult = {
|
|
||||||
val accepted = (it.resultCode == RESULT_OK)
|
|
||||||
if (accepted) {
|
|
||||||
Timber.d("VPN permission granted")
|
|
||||||
} else {
|
|
||||||
showVpnPermissionDialog = true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
|
||||||
kotlin.runCatching {
|
|
||||||
focusRequester.requestFocus()
|
|
||||||
}.onFailure {
|
|
||||||
delay(Constants.FOCUS_REQUEST_DELAY)
|
|
||||||
focusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
||||||
@@ -152,23 +106,18 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
|||||||
viewModel.onTunnelFileSelected(data, context)
|
viewModel.onTunnelFileSelected(data, context)
|
||||||
})
|
})
|
||||||
|
|
||||||
val scanLauncher =
|
val requestPermissionLauncher = rememberLauncherForActivityResult(
|
||||||
rememberLauncherForActivityResult(
|
ActivityResultContracts.RequestPermission(),
|
||||||
contract = ScanContract(),
|
) { isGranted ->
|
||||||
onResult = {
|
if (!isGranted) return@rememberLauncherForActivityResult snackbar.showMessage("Camera permission required")
|
||||||
if (it.contents != null) {
|
navController.navigate(Route.Scanner)
|
||||||
viewModel.onTunnelQrResult(it.contents)
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
|
||||||
|
|
||||||
if (showDeleteTunnelAlertDialog) {
|
if (showDeleteTunnelAlertDialog) {
|
||||||
InfoDialog(
|
InfoDialog(
|
||||||
onDismiss = { showDeleteTunnelAlertDialog = false },
|
onDismiss = { showDeleteTunnelAlertDialog = false },
|
||||||
onAttest = {
|
onAttest = {
|
||||||
selectedTunnel?.let { viewModel.onDelete(it, context) }
|
selectedTunnel?.let { viewModel.onDelete(it) }
|
||||||
showDeleteTunnelAlertDialog = false
|
showDeleteTunnelAlertDialog = false
|
||||||
selectedTunnel = null
|
selectedTunnel = null
|
||||||
},
|
},
|
||||||
@@ -179,60 +128,69 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
||||||
if (checked) {
|
if (!checked) viewModel.onTunnelStop().also { return }
|
||||||
if (uiState.settings.isKernelEnabled) {
|
if (uiState.settings.isKernelEnabled) {
|
||||||
context.startTunnelBackground(tunnel.id)
|
viewModel.onTunnelStart(tunnel, uiState.settings.isKernelEnabled)
|
||||||
} else {
|
|
||||||
viewModel.onTunnelStart(tunnel)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
viewModel.onTunnelStop(
|
startTunnel.invoke(tunnel)
|
||||||
tunnel,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.isNotEmpty()) {
|
if (uiState.tunnels.isEmpty()) return@pointerInput
|
||||||
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,
|
) { padding ->
|
||||||
floatingActionButton = {
|
|
||||||
ScrollDismissFab(icon = {
|
|
||||||
val icon = Icons.Filled.Add
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
tint = MaterialTheme.colorScheme.onPrimary,
|
|
||||||
)
|
|
||||||
}, focusRequester, isVisible = isVisible.value, onClick = {
|
|
||||||
showBottomSheet = true
|
|
||||||
})
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
TunnelImportSheet(
|
TunnelImportSheet(
|
||||||
showBottomSheet,
|
showBottomSheet,
|
||||||
onDismiss = { showBottomSheet = false },
|
onDismiss = { showBottomSheet = false },
|
||||||
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_FILE_TYPES) },
|
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
||||||
onQrClick = { launchQrScanner() },
|
onQrClick = { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) },
|
||||||
|
onClipboardClick = {
|
||||||
|
clipboard.getText()?.text?.let {
|
||||||
|
viewModel.onClipboardImport(it)
|
||||||
|
}
|
||||||
|
},
|
||||||
onManualImportClick = {
|
onManualImportClick = {
|
||||||
navController.navigate(
|
navController.navigate(
|
||||||
Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID),
|
Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID),
|
||||||
@@ -241,10 +199,10 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
|||||||
)
|
)
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.spacedBy(5.dp.scaledHeight(), Alignment.Top),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize().padding(padding)
|
||||||
.overscroll(ScrollableDefaults.overscrollEffect())
|
.overscroll(ScrollableDefaults.overscrollEffect())
|
||||||
.nestedScroll(nestedScrollConnection),
|
.nestedScroll(nestedScrollConnection),
|
||||||
state = rememberLazyListState(0, uiState.tunnels.count()),
|
state = rememberLazyListState(0, uiState.tunnels.count()),
|
||||||
@@ -256,269 +214,30 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState,
|
|||||||
item {
|
item {
|
||||||
GettingStartedLabel(onClick = { context.openWebUrl(it) })
|
GettingStartedLabel(onClick = { context.openWebUrl(it) })
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
item {
|
item {
|
||||||
if (uiState.settings.isAutoTunnelEnabled) {
|
AutoTunnelRowItem(uiState) {
|
||||||
val itemFocusRequester = remember { FocusRequester() }
|
autoTunnelToggleBattery.invoke()
|
||||||
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 (context.isRunningOnTv()) {
|
|
||||||
itemFocusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onHold = {},
|
|
||||||
expanded = false,
|
|
||||||
statistics = null,
|
|
||||||
focusRequester = focusRequester,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
items(
|
items(
|
||||||
uiState.tunnels,
|
sortedTunnels,
|
||||||
key = { tunnel -> tunnel.id },
|
key = { tunnel -> tunnel.id },
|
||||||
) { tunnel ->
|
) { tunnel ->
|
||||||
val isActive = uiState.tunnels.any {
|
val expanded = uiState.generalState.isTunnelStatsExpanded
|
||||||
it.id == tunnel.id &&
|
TunnelRowItem(
|
||||||
it.isActive
|
tunnel.id == uiState.vpnState.tunnelConfig?.id &&
|
||||||
}
|
uiState.vpnState.status.isUp(),
|
||||||
val leadingIconColor =
|
expanded,
|
||||||
(
|
selectedTunnel?.id == tunnel.id,
|
||||||
if (
|
tunnel,
|
||||||
isActive && uiState.vpnState.statistics != null
|
vpnState = uiState.vpnState,
|
||||||
) {
|
{ selectedTunnel = tunnel },
|
||||||
uiState.vpnState.statistics.mapPeerStats()
|
{ viewModel.onExpandedChanged(!expanded) },
|
||||||
.map { it.value?.handshakeStatus() }
|
onDelete = { showDeleteTunnelAlertDialog = true },
|
||||||
.let { statuses ->
|
onCopy = { viewModel.onCopyTunnel(tunnel) },
|
||||||
when {
|
onSwitchClick = { onTunnelToggle(it, tunnel) },
|
||||||
statuses.all { it == HandshakeStatus.HEALTHY } -> mint
|
|
||||||
statuses.any { it == HandshakeStatus.STALE } -> corn
|
|
||||||
statuses.all { it == HandshakeStatus.NOT_STARTED } ->
|
|
||||||
Color.Gray
|
|
||||||
|
|
||||||
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 = {
|
|
||||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
|
||||||
selectedTunnel = tunnel
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
if (!context.isRunningOnTv()) {
|
|
||||||
if (
|
|
||||||
isActive
|
|
||||||
) {
|
|
||||||
expanded.value = !expanded.value
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
selectedTunnel = tunnel
|
|
||||||
itemFocusRequester.requestFocus()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
statistics = uiState.vpnState.statistics,
|
|
||||||
expanded = expanded.value,
|
|
||||||
focusRequester = focusRequester,
|
|
||||||
rowButton = {
|
|
||||||
if (
|
|
||||||
tunnel.id == selectedTunnel?.id &&
|
|
||||||
!context.isRunningOnTv()
|
|
||||||
) {
|
|
||||||
Row {
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
selectedTunnel?.let {
|
|
||||||
navController.navigate(
|
|
||||||
Route.Option(it.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(
|
|
||||||
enabled = !isActive,
|
|
||||||
modifier = Modifier.focusable(),
|
|
||||||
onClick = { showDeleteTunnelAlertDialog = true },
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!isActive) expanded.value = false
|
|
||||||
@Composable
|
|
||||||
fun TunnelSwitch() = Switch(
|
|
||||||
modifier = Modifier.focusRequester(itemFocusRequester),
|
|
||||||
checked = isActive,
|
|
||||||
onCheckedChange = { checked ->
|
|
||||||
if (!checked) expanded.value = false
|
|
||||||
val intent = if (uiState.settings.isKernelEnabled) null else GoBackend.VpnService.prepare(context)
|
|
||||||
if (intent != null) return@Switch vpnActivityResultState.launch(intent)
|
|
||||||
onTunnelToggle(checked, tunnel)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
Row {
|
|
||||||
IconButton(
|
|
||||||
onClick = {
|
|
||||||
selectedTunnel = tunnel
|
|
||||||
selectedTunnel?.let {
|
|
||||||
navController.navigate(
|
|
||||||
Route.Option(it.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 {
|
|
||||||
snackbar.showMessage(
|
|
||||||
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
|
|
||||||
) {
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.turn_off_tunnel),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
selectedTunnel = tunnel
|
|
||||||
showDeleteTunnelAlertDialog = true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
TunnelSwitch()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
TunnelSwitch()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-50
@@ -19,6 +19,8 @@ import com.zaneschepke.wireguardautotunnel.util.FileReadException
|
|||||||
import com.zaneschepke.wireguardautotunnel.util.InvalidFileExtensionException
|
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.StringValue
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
|
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
|
||||||
@@ -28,26 +30,24 @@ 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,
|
||||||
val tunnelService: TunnelService,
|
private val tunnelService: Provider<TunnelService>,
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
private val serviceManager: ServiceManager,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private fun stopWatcherService(context: Context) {
|
fun onDelete(tunnel: TunnelConfig) {
|
||||||
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) {
|
||||||
stopWatcherService(context)
|
serviceManager.stopAutoTunnel()
|
||||||
resetTunnelSetting(settings)
|
resetTunnelSetting(settings)
|
||||||
}
|
}
|
||||||
appDataRepository.tunnels.delete(tunnel)
|
appDataRepository.tunnels.delete(tunnel)
|
||||||
@@ -63,14 +63,18 @@ constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelStart(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
fun onExpandedChanged(expanded: Boolean) = viewModelScope.launch {
|
||||||
Timber.i("Starting tunnel ${tunnelConfig.name}")
|
appDataRepository.appState.setTunnelStatsExpanded(expanded)
|
||||||
tunnelService.startTunnel(tunnelConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelStop(tunnel: TunnelConfig) = viewModelScope.launch {
|
fun onTunnelStart(tunnelConfig: TunnelConfig, background: Boolean) = viewModelScope.launch {
|
||||||
|
Timber.i("Starting tunnel ${tunnelConfig.name}")
|
||||||
|
tunnelService.get().startTunnel(tunnelConfig, background)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onTunnelStop() = viewModelScope.launch {
|
||||||
Timber.i("Stopping active tunnel")
|
Timber.i("Stopping active tunnel")
|
||||||
tunnelService.stopTunnel(tunnel)
|
tunnelService.get().stopTunnel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCodeDefaultName(config: String): String {
|
private fun generateQrCodeDefaultName(config: String): String {
|
||||||
@@ -96,28 +100,18 @@ constructor(
|
|||||||
return defaultName
|
return defaultName
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelQrResult(result: String) = viewModelScope.launch(ioDispatcher) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
val amConfig = TunnelConfig.configFromAmQuick(result)
|
|
||||||
val amQuick = amConfig.toAwgQuickString(true)
|
|
||||||
val wgQuick = amConfig.toWgQuickString()
|
|
||||||
|
|
||||||
val tunnelName = makeTunnelNameUnique(generateQrCodeTunnelName(result))
|
|
||||||
val tunnelConfig = TunnelConfig(name = tunnelName, wgQuick = wgQuick, amQuick = amQuick)
|
|
||||||
saveTunnel(tunnelConfig)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_invalid_code))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = "$name($num)"
|
tunnelName = if (!tunnelName.hasNumberInParentheses()) {
|
||||||
|
"$name($num)"
|
||||||
|
} else {
|
||||||
|
val pair = tunnelName.extractNameAndNumber()
|
||||||
|
"${pair?.first}($num)"
|
||||||
|
}
|
||||||
num++
|
num++
|
||||||
}
|
}
|
||||||
tunnelName
|
tunnelName
|
||||||
@@ -164,6 +158,10 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun onToggleAutoTunnel() = viewModelScope.launch {
|
||||||
|
serviceManager.toggleAutoTunnel(false)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
||||||
ZipInputStream(getInputStreamFromUri(uri, context)).use { zip ->
|
ZipInputStream(getInputStreamFromUri(uri, context)).use { zip ->
|
||||||
generateSequence { zip.nextEntry }
|
generateSequence { zip.nextEntry }
|
||||||
@@ -185,25 +183,15 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setBatteryOptimizeDisableShown() = viewModelScope.launch {
|
||||||
|
appDataRepository.appState.setBatteryOptimizationDisableShown(true)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri, context: Context) {
|
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri, context: Context) {
|
||||||
val stream = getInputStreamFromUri(uri, context) ?: throw FileReadException
|
val stream = getInputStreamFromUri(uri, context) ?: throw FileReadException
|
||||||
saveTunnelConfigFromStream(stream, name)
|
saveTunnelConfigFromStream(stream, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun pauseAutoTunneling() = viewModelScope.launch {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
settings.copy(isAutoTunnelPaused = true),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun resumeAutoTunneling() = viewModelScope.launch {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
settings.copy(isAutoTunnelPaused = false),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
private fun saveTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||||
appDataRepository.tunnels.save(tunnelConfig)
|
appDataRepository.tunnels.save(tunnelConfig)
|
||||||
}
|
}
|
||||||
@@ -251,14 +239,19 @@ 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 {
|
||||||
tunnel?.let {
|
saveTunnel(
|
||||||
saveTunnel(
|
TunnelConfig(name = makeTunnelNameUnique(tunnel.name), wgQuick = tunnel.wgQuick, amQuick = tunnel.amQuick),
|
||||||
TunnelConfig(
|
)
|
||||||
name = it.name.plus(NumberUtils.randomThree()),
|
}
|
||||||
wgQuick = it.wgQuick,
|
|
||||||
),
|
fun onClipboardImport(config: String) = viewModelScope.launch(ioDispatcher) {
|
||||||
)
|
runCatching {
|
||||||
|
val amConfig = TunnelConfig.configFromAmQuick(config)
|
||||||
|
val tunnelConfig = TunnelConfig.tunnelConfigFromAmConfig(amConfig, makeTunnelNameUnique(generateQrCodeDefaultName(config)))
|
||||||
|
saveTunnel(tunnelConfig)
|
||||||
|
}.onFailure {
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.error_file_format))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+57
@@ -0,0 +1,57 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Bolt
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.scale
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
ExpandingRowListItem(
|
||||||
|
leading = {
|
||||||
|
val icon = Icons.Rounded.Bolt
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.size(16.dp).scale(1.5f),
|
||||||
|
tint =
|
||||||
|
if (!appUiState.autoTunnelActive) {
|
||||||
|
Color.Gray
|
||||||
|
} else {
|
||||||
|
SilverTree
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
|
text = stringResource(R.string.auto_tunneling),
|
||||||
|
trailing = {
|
||||||
|
ScaledSwitch(
|
||||||
|
appUiState.settings.isAutoTunnelEnabled,
|
||||||
|
onClick = {
|
||||||
|
onToggle()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
if (context.isRunningOnTv()) {
|
||||||
|
onToggle()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
isExpanded = false,
|
||||||
|
)
|
||||||
|
}
|
||||||
+1
-4
@@ -9,19 +9,16 @@ import androidx.compose.material3.FloatingActionButton
|
|||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
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.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScrollDismissFab(icon: @Composable () -> Unit, focusRequester: FocusRequester, isVisible: Boolean, onClick: () -> Unit) {
|
fun ScrollDismissFab(icon: @Composable () -> Unit, isVisible: Boolean, onClick: () -> Unit) {
|
||||||
AnimatedVisibility(
|
AnimatedVisibility(
|
||||||
visible = isVisible,
|
visible = isVisible,
|
||||||
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
||||||
exit = slideOutVertically(targetOffsetY = { it * 2 }),
|
exit = slideOutVertically(targetOffsetY = { it * 2 }),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.focusRequester(focusRequester)
|
|
||||||
.focusGroup(),
|
.focusGroup(),
|
||||||
) {
|
) {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
|
|||||||
+32
-1
@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Row
|
|||||||
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.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.ContentPasteGo
|
||||||
import androidx.compose.material.icons.filled.Create
|
import androidx.compose.material.icons.filled.Create
|
||||||
import androidx.compose.material.icons.filled.FileOpen
|
import androidx.compose.material.icons.filled.FileOpen
|
||||||
import androidx.compose.material.icons.filled.QrCode
|
import androidx.compose.material.icons.filled.QrCode
|
||||||
@@ -22,9 +23,17 @@ import androidx.compose.ui.unit.dp
|
|||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
|
|
||||||
|
// TODO refactor this component
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun TunnelImportSheet(show: Boolean, onDismiss: () -> Unit, onFileClick: () -> Unit, onQrClick: () -> Unit, onManualImportClick: () -> Unit) {
|
fun TunnelImportSheet(
|
||||||
|
show: Boolean,
|
||||||
|
onDismiss: () -> Unit,
|
||||||
|
onFileClick: () -> Unit,
|
||||||
|
onQrClick: () -> Unit,
|
||||||
|
onManualImportClick: () -> Unit,
|
||||||
|
onClipboardClick: () -> Unit,
|
||||||
|
) {
|
||||||
val sheetState = rememberModalBottomSheetState()
|
val sheetState = rememberModalBottomSheetState()
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
@@ -77,6 +86,28 @@ fun TunnelImportSheet(show: Boolean, onDismiss: () -> Unit, onFileClick: () -> U
|
|||||||
modifier = Modifier.padding(10.dp),
|
modifier = Modifier.padding(10.dp),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
HorizontalDivider()
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable {
|
||||||
|
onDismiss()
|
||||||
|
onClipboardClick()
|
||||||
|
}
|
||||||
|
.padding(10.dp),
|
||||||
|
) {
|
||||||
|
val icon = Icons.Filled.ContentPasteGo
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
contentDescription = icon.name,
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.add_from_clipboard),
|
||||||
|
modifier = Modifier.padding(10.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
HorizontalDivider()
|
HorizontalDivider()
|
||||||
Row(
|
Row(
|
||||||
|
|||||||
+195
@@ -0,0 +1,195 @@
|
|||||||
|
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.SettingsEthernet
|
||||||
|
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
|
||||||
|
|
||||||
|
@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 icon = when {
|
||||||
|
tunnel.isPrimaryTunnel -> Icons.Rounded.Star
|
||||||
|
tunnel.isMobileDataTunnel -> Icons.Rounded.Smartphone
|
||||||
|
tunnel.isEthernetTunnel -> Icons.Rounded.SettingsEthernet
|
||||||
|
else -> Icons.Rounded.Circle
|
||||||
|
}
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
icon.name,
|
||||||
|
tint = leadingIconColor,
|
||||||
|
modifier = Modifier.size(16.dp),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
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.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.data.domain.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.toThreeDecimalPlaceString
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfig) {
|
||||||
|
val config = TunnelConfig.configFromAmQuick(tunnelConfig.wgQuick)
|
||||||
|
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(end = 10.dp, bottom = 10.dp, start = 45.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.Start),
|
||||||
|
) {
|
||||||
|
config.peers.forEach {
|
||||||
|
val peerId = it.publicKey.toBase64().subSequence(0, 3).toString() + "***"
|
||||||
|
val peerRx = statistics?.peerStats(it.publicKey)?.rxBytes ?: 0
|
||||||
|
val peerTx = statistics?.peerStats(it.publicKey)?.txBytes ?: 0
|
||||||
|
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
||||||
|
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
||||||
|
val handshake = statistics?.peerStats(it.publicKey)?.latestHandshakeEpochMillis?.let {
|
||||||
|
if (it == 0L) {
|
||||||
|
stringResource(R.string.never)
|
||||||
|
} else {
|
||||||
|
"${NumberUtils.getSecondsBetweenTimestampAndNow(it)} ${stringResource(R.string.sec)}"
|
||||||
|
}
|
||||||
|
} ?: stringResource(R.string.never)
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.peer).lowercase() + ": $peerId",
|
||||||
|
style = MaterialTheme.typography.bodySmall,
|
||||||
|
color = MaterialTheme.colorScheme.outline,
|
||||||
|
)
|
||||||
|
Text("tx: $peerTxMB MB", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||||
|
}
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.handshake) + ": $handshake", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||||
|
Text("rx: $peerRxMB MB", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user