mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
112 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cbe3311c9 | |||
| a5898d4ad1 | |||
| 48c01aa0e3 | |||
| b1dc6c5d59 | |||
| 2a7397edba | |||
| 53c09267df | |||
| f772dc0f8a | |||
| ba064b267f | |||
| 2c04b9d69c | |||
| 48adaae0a0 | |||
| 8e49a4d343 | |||
| 67f53915cc | |||
| 156c5478ae | |||
| 85319ba874 | |||
| ab858ab59e | |||
| 4196a543b2 | |||
| 02a8db0f9a | |||
| eb7c6ca7ba | |||
| 8ffe145ade | |||
| 4bdc43deb3 | |||
| 83c0ff497b | |||
| c8ac40d370 | |||
| 4dc91b5fae | |||
| 7a3627bf6a | |||
| e86bba6a0e | |||
| ac846bfdc9 | |||
| 11e385d350 | |||
| b2e266fc9f | |||
| 7cbbf00e52 | |||
| 6a0b2b678f | |||
| 1bfea142ad | |||
| 6fdf80e84d | |||
| b7ec9e2696 | |||
| 6502f553ac | |||
| 946ac71fb1 | |||
| dca8b50083 | |||
| 1ab611aa7f | |||
| 40a2732ec9 | |||
| ffcb4f6ed8 | |||
| 7b6bdec133 | |||
| 6ece224592 | |||
| 66051e4a0a | |||
| c8298297e2 | |||
| 83bec24ca9 | |||
| 44353df400 | |||
| 0da6e3e82c | |||
| dbaf0312f8 | |||
| 7cf7817cea | |||
| 133bd6caf7 | |||
| 25b986ef2f | |||
| fe45a2fad9 | |||
| a3eb53f90f | |||
| 246916ab0d | |||
| 7f89dcaab0 | |||
| 1d2b305047 | |||
| 0201523262 | |||
| fcc1c1e7a1 | |||
| cd82b83789 | |||
| 9f1af0140f | |||
| 14cd49983a | |||
| 40dd14ca16 | |||
| 048bcd4ad7 | |||
| 94bd23f2a4 | |||
| 7285af5c2d | |||
| 11efad703c | |||
| b81f43c7cf | |||
| 68865843dc | |||
| 62daf138dd | |||
| 1120a8fc81 | |||
| ab10be7266 | |||
| 88f9d43e51 | |||
| 2877757396 | |||
| c657c62640 | |||
| d84d9df57d | |||
| 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 |
@@ -0,0 +1,125 @@
|
|||||||
|
name: build
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
build_type:
|
||||||
|
type: choice
|
||||||
|
description: "Build type"
|
||||||
|
required: true
|
||||||
|
default: debug
|
||||||
|
options:
|
||||||
|
- debug
|
||||||
|
- prerelease
|
||||||
|
- nightly
|
||||||
|
- release
|
||||||
|
secrets:
|
||||||
|
SIGNING_KEY_ALIAS:
|
||||||
|
required: false
|
||||||
|
SIGNING_KEY_PASSWORD:
|
||||||
|
required: false
|
||||||
|
SIGNING_STORE_PASSWORD:
|
||||||
|
required: false
|
||||||
|
SERVICE_ACCOUNT_JSON:
|
||||||
|
required: false
|
||||||
|
KEYSTORE:
|
||||||
|
required: false
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
build_type:
|
||||||
|
type: string
|
||||||
|
description: "Build type"
|
||||||
|
required: true
|
||||||
|
default: debug
|
||||||
|
secrets:
|
||||||
|
SIGNING_KEY_ALIAS:
|
||||||
|
required: false
|
||||||
|
SIGNING_KEY_PASSWORD:
|
||||||
|
required: false
|
||||||
|
SIGNING_STORE_PASSWORD:
|
||||||
|
required: false
|
||||||
|
SERVICE_ACCOUNT_JSON:
|
||||||
|
required: false
|
||||||
|
KEYSTORE:
|
||||||
|
required: false
|
||||||
|
env:
|
||||||
|
UPLOAD_DIR_ANDROID: android_artifacts
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||||
|
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||||
|
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
|
||||||
|
KEY_STORE_FILE: 'android_keystore.jks'
|
||||||
|
KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
|
||||||
|
outputs:
|
||||||
|
UPLOAD_DIR_ANDROID: ${{ env.UPLOAD_DIR_ANDROID }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up JDK 17
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
distribution: 'temurin'
|
||||||
|
java-version: '17'
|
||||||
|
cache: gradle
|
||||||
|
- name: Grant execute permission for gradlew
|
||||||
|
run: chmod +x gradlew
|
||||||
|
|
||||||
|
# Here we need to decode keystore.jks from base64 string and place it
|
||||||
|
# in the folder specified in the release signing configuration
|
||||||
|
- name: Decode Keystore
|
||||||
|
id: decode_keystore
|
||||||
|
uses: timheuer/base64-to-file@v1.2
|
||||||
|
with:
|
||||||
|
fileName: ${{ env.KEY_STORE_FILE }}
|
||||||
|
fileDir: ${{ env.KEY_STORE_LOCATION }}
|
||||||
|
encodedString: ${{ secrets.KEYSTORE }}
|
||||||
|
|
||||||
|
# create keystore path for gradle to read
|
||||||
|
- name: Create keystore path env var
|
||||||
|
if: ${{ inputs.build_type != 'debug' }}
|
||||||
|
run: |
|
||||||
|
store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
|
||||||
|
echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Create service_account.json
|
||||||
|
if: ${{ inputs.build_type != 'debug' }}
|
||||||
|
id: createServiceAccount
|
||||||
|
run: echo '${{ secrets.ANDROID_SERVICE_ACCOUNT_JSON }}' > service_account.json
|
||||||
|
|
||||||
|
- name: Build Fdroid Release APK
|
||||||
|
if: ${{ inputs.build_type == 'release' }}
|
||||||
|
run: ./gradlew :app:assembleFdroidRelease --info
|
||||||
|
|
||||||
|
- name: Build Fdroid Prerelease APK
|
||||||
|
if: ${{ inputs.build_type == 'prerelease' }}
|
||||||
|
run: ./gradlew :app:assembleFdroidPrerelease --info
|
||||||
|
|
||||||
|
- name: Build Fdroid Nightly APK
|
||||||
|
if: ${{ inputs.build_type == 'nightly' }}
|
||||||
|
run: ./gradlew :app:assembleFdroidNightly --info
|
||||||
|
|
||||||
|
- name: Build Debug APK
|
||||||
|
if: ${{ inputs.build_type == 'debug' }}
|
||||||
|
run: ./gradlew :app:assembleFdroidDebug --stacktrace
|
||||||
|
|
||||||
|
# bump versionCode for nightly and prerelease builds
|
||||||
|
- name: Commit and push versionCode changes
|
||||||
|
if: ${{ inputs.build_type == 'nightly' || inputs.build_type == 'prerelease' }}
|
||||||
|
run: |
|
||||||
|
git config --global user.name 'GitHub Actions'
|
||||||
|
git config --global user.email 'actions@github.com'
|
||||||
|
git add versionCode.txt
|
||||||
|
git commit -m "Automated build update"
|
||||||
|
|
||||||
|
- name: Get release apk path
|
||||||
|
id: apk-path
|
||||||
|
run: echo "path=$(find . -regex '^.*/build/outputs/apk/fdroid/${{ inputs.build_type }}/.*\.apk$' -type f | head -1 | tail -c+2)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Upload release apk
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: ${{ env.UPLOAD_DIR_ANDROID }}
|
||||||
|
path: ${{github.workspace}}/${{ steps.apk-path.outputs.path }}
|
||||||
|
retention-days: 1
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
name: ci-android
|
name: on-pr
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
format:
|
format_check:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
name: release-android
|
name: publish
|
||||||
|
|
||||||
on:
|
on:
|
||||||
schedule:
|
schedule:
|
||||||
@@ -31,6 +31,8 @@ on:
|
|||||||
required: false
|
required: false
|
||||||
default: nightly
|
default: nightly
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
env:
|
||||||
|
UPLOAD_DIR_ANDROID: android_artifacts
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check_commits:
|
check_commits:
|
||||||
@@ -53,17 +55,22 @@ jobs:
|
|||||||
# This script checks for commits newer than 23 hours ago
|
# 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 }})
|
NEW_COMMITS=$(git rev-list --count --after="$(date -Iseconds -d '23 hours ago')" ${{ github.sha }})
|
||||||
echo "new_commits=$NEW_COMMITS" >> $GITHUB_OUTPUT
|
echo "new_commits=$NEW_COMMITS" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
build:
|
build:
|
||||||
needs: check_commits
|
if: ${{ inputs.release_type != 'none' }}
|
||||||
|
uses: ./.github/workflows/build.yml
|
||||||
|
secrets: inherit
|
||||||
|
with:
|
||||||
|
build_type: ${{ inputs.release_type == '' && 'nightly' || inputs.release_type }}
|
||||||
|
|
||||||
|
publish:
|
||||||
|
needs:
|
||||||
|
- check_commits
|
||||||
|
- build
|
||||||
if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
|
if: ${{ needs.check_commits.outputs.has_new_commits > 0 && inputs.release_type != 'none' }}
|
||||||
name: Build Signed APK
|
name: publish-github
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
|
||||||
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
|
||||||
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
|
|
||||||
KEY_STORE_FILE: 'android_keystore.jks'
|
|
||||||
KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
|
|
||||||
GH_USER: ${{ secrets.GH_USER }}
|
GH_USER: ${{ secrets.GH_USER }}
|
||||||
# GH needed for gh cli
|
# GH needed for gh cli
|
||||||
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
GH_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||||
@@ -71,59 +78,35 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Set up JDK 17
|
|
||||||
uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
distribution: 'temurin'
|
|
||||||
java-version: '17'
|
|
||||||
cache: gradle
|
|
||||||
- name: Grant execute permission for gradlew
|
|
||||||
run: chmod +x gradlew
|
|
||||||
|
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt update && sudo apt install -y gh apksigner
|
sudo apt update && sudo apt install -y gh apksigner
|
||||||
|
|
||||||
# Here we need to decode keystore.jks from base64 string and place it
|
# update latest tag
|
||||||
# in the folder specified in the release signing configuration
|
- name: Set latest tag
|
||||||
- name: Decode Keystore
|
uses: rickstaa/action-create-tag@v1
|
||||||
id: decode_keystore
|
id: tag_creation
|
||||||
uses: timheuer/base64-to-file@v1.2
|
|
||||||
with:
|
with:
|
||||||
fileName: ${{ env.KEY_STORE_FILE }}
|
tag: "latest" # or any tag name you wish to use
|
||||||
fileDir: ${{ env.KEY_STORE_LOCATION }}
|
message: "Automated tag for HEAD commit"
|
||||||
encodedString: ${{ secrets.KEYSTORE }}
|
force_push_tag: true
|
||||||
|
tag_exists_error: false
|
||||||
|
|
||||||
# create keystore path for gradle to read
|
- name: Get latest release
|
||||||
- name: Create keystore path env var
|
id: latest_release
|
||||||
run: |
|
uses: kaliber5/action-get-release@v1
|
||||||
store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
|
with:
|
||||||
echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
latest: true
|
||||||
|
|
||||||
- name: Create service_account.json
|
- name: Generate Changelog
|
||||||
id: createServiceAccount
|
id: changelog
|
||||||
run: echo '${{ secrets.SERVICE_ACCOUNT_JSON }}' > service_account.json
|
uses: requarks/changelog-action@v1
|
||||||
|
with:
|
||||||
# Build and sign APK ("-x test" argument is used to skip tests)
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
# add fdroid flavor for apk upload
|
toTag: ${{ github.event_name == 'schedule' && 'nightly' || steps.latest_release.outputs.tag_name }}
|
||||||
- name: Build Fdroid Release APK
|
fromTag: "latest"
|
||||||
if: ${{ inputs.release_type != '' && inputs.release_type == 'release' }}
|
writeToFile: false # we won't write to file, just output
|
||||||
run: ./gradlew :app:assembleFdroidRelease -x test
|
|
||||||
|
|
||||||
- name: Build Fdroid Prerelease APK
|
|
||||||
if: ${{ inputs.release_type != '' && inputs.release_type == 'prerelease' }}
|
|
||||||
run: ./gradlew :app:assembleFdroidPrerelease -x test
|
|
||||||
|
|
||||||
- name: Build Fdroid Nightly APK
|
|
||||||
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' }}
|
|
||||||
run: ./gradlew :app:assembleFdroidNightly -x test
|
|
||||||
|
|
||||||
- if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' }}
|
|
||||||
run: echo "APK_PATH=$(find . -regex '^.*/build/outputs/apk/fdroid/nightly/.*\.apk$' -type f | head -1)" >> $GITHUB_ENV
|
|
||||||
- if: ${{ inputs.release_type != '' && inputs.release_type == 'release' }}
|
|
||||||
run: echo "APK_PATH=$(find . -regex '^.*/build/outputs/apk/fdroid/release/.*\.apk$' -type f | head -1)" >> $GITHUB_ENV
|
|
||||||
- if: ${{ inputs.release_type != '' && inputs.release_type == 'prerelease' }}
|
|
||||||
run: echo "APK_PATH=$(find . -regex '^.*/build/outputs/apk/fdroid/prerelease/.*\.apk$' -type f | head -1)" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- name: Get version code
|
- name: Get version code
|
||||||
if: ${{ inputs.release_type == 'release' }}
|
if: ${{ inputs.release_type == 'release' }}
|
||||||
@@ -131,14 +114,6 @@ jobs:
|
|||||||
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
|
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
|
||||||
echo "VERSION_CODE=$version_code" >> $GITHUB_ENV
|
echo "VERSION_CODE=$version_code" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Commit and push versionCode changes
|
|
||||||
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' || inputs.release_type == 'prerelease' }}
|
|
||||||
run: |
|
|
||||||
git config --global user.name 'GitHub Actions'
|
|
||||||
git config --global user.email 'actions@github.com'
|
|
||||||
git add versionCode.txt
|
|
||||||
git commit -m "Automated build update"
|
|
||||||
|
|
||||||
- name: Push changes
|
- name: Push changes
|
||||||
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' || inputs.release_type == 'prerelease' }}
|
if: ${{ inputs.release_type == '' || inputs.release_type == 'nightly' || inputs.release_type == 'prerelease' }}
|
||||||
uses: ad-m/github-push-action@master
|
uses: ad-m/github-push-action@master
|
||||||
@@ -146,25 +121,14 @@ jobs:
|
|||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
branch: ${{ github.ref }}
|
branch: ${{ github.ref }}
|
||||||
|
|
||||||
# Save the APK after the Build job is complete to publish it as a Github release in the next job
|
- name: Make download dir
|
||||||
- name: Upload APK
|
run: mkdir ${{ github.workspace }}/temp
|
||||||
uses: actions/upload-artifact@v4.4.3
|
|
||||||
with:
|
|
||||||
name: wgtunnel
|
|
||||||
path: ${{ env.APK_PATH }}
|
|
||||||
|
|
||||||
- name: Download APK from build
|
- name: Download artifacts
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: wgtunnel
|
name: ${{ env.UPLOAD_DIR_ANDROID }}
|
||||||
|
path: ${{ github.workspace }}/temp
|
||||||
- name: Repository Dispatch for my F-Droid repo
|
|
||||||
uses: peter-evans/repository-dispatch@v3
|
|
||||||
if: ${{ inputs.release_type == 'release' }}
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.PAT }}
|
|
||||||
repository: zaneschepke/fdroid
|
|
||||||
event-type: fdroid-update
|
|
||||||
|
|
||||||
# Setup TAG_NAME, which is used as a general "name"
|
# Setup TAG_NAME, which is used as a general "name"
|
||||||
- if: github.event_name == 'workflow_dispatch'
|
- if: github.event_name == 'workflow_dispatch'
|
||||||
@@ -195,7 +159,9 @@ jobs:
|
|||||||
|
|
||||||
- name: Get checksum
|
- name: Get checksum
|
||||||
id: checksum
|
id: checksum
|
||||||
run: echo "checksum=$(apksigner verify -print-certs ${{ env.APK_PATH }} | grep -Po "(?<=SHA-256 digest:) .*" | tr -d "[:blank:]")" >> $GITHUB_OUTPUT
|
run: |
|
||||||
|
file_path=$(find ${{ github.workspace }}/temp -type f -iname "*.apk" | tail -n1)
|
||||||
|
echo "checksum=$(apksigner verify -print-certs $file_path | grep -Po "(?<=SHA-256 digest:) .*" | tr -d "[:blank:]")" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
|
||||||
- name: Create Release with Fastlane changelog notes
|
- name: Create Release with Fastlane changelog notes
|
||||||
@@ -209,12 +175,29 @@ 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
|
||||||
prerelease: ${{ inputs.release_type == 'prerelease' || inputs.release_type == '' || inputs.release_type == 'nightly' }}
|
prerelease: ${{ inputs.release_type == 'prerelease' || inputs.release_type == '' || inputs.release_type == 'nightly' }}
|
||||||
make_latest: ${{ inputs.release_type == 'release' }}
|
make_latest: ${{ inputs.release_type == 'release' }}
|
||||||
files: ${{ github.workspace }}/${{ env.APK_PATH }}
|
files: |
|
||||||
|
${{ github.workspace }}/temp/*
|
||||||
|
|
||||||
|
publish-fdroid:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
if: inputs.release_type == 'release'
|
||||||
|
steps:
|
||||||
|
- name: Dispatch update for fdroid repo
|
||||||
|
uses: peter-evans/repository-dispatch@v3
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.ANDROID_PAT }}
|
||||||
|
repository: zaneschepke/fdroid
|
||||||
|
event-type: fdroid-update
|
||||||
|
|
||||||
publish-play:
|
publish-play:
|
||||||
if: ${{ inputs.track != 'none' && inputs.track != '' }}
|
if: ${{ inputs.track != 'none' && inputs.track != '' }}
|
||||||
@@ -4,80 +4,107 @@ WG Tunnel
|
|||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
|
An alternative Android client app for [WireGuard®](https://www.wireguard.com/)
|
||||||
|
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/)
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
<a href="https://github.com/zaneschepke/wgtunnel/issues/new?assignees=zaneschepke&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D+-+Problem+with+app">Report a Bug</a>
|
||||||
|
·
|
||||||
|
<a href="https://github.com/zaneschepke/wgtunnel/issues/new?assignees=zaneschepke&labels=enhancement&projects=&template=feature_request.md&title=%5BFEATURE%5D+-+New+feature+request">Request a Feature</a>
|
||||||
|
·
|
||||||
|
<a href="https://github.com/zaneschepke/wgtunnel/discussions">Ask a Question</a>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<br/>
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
||||||
|
[](https://f-droid.org/packages/com.zaneschepke.wireguardautotunnel/)
|
||||||
|
[](https://github.com/zaneschepke/fdroid)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
||||||
<div align="center">
|
<details open="open">
|
||||||
|
<summary>Table of Contents</summary>
|
||||||
|
|
||||||
|
- [About](#about)
|
||||||
|
- [Acknowledgements](#acknowledgements)
|
||||||
|
- [Screenshots](#screenshots)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Building](#building)
|
||||||
|
- [Translation](#translation)
|
||||||
|
- [Contributing](#contributing)
|
||||||
|
|
||||||
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
</details>
|
||||||
[](https://f-droid.org/packages/com.zaneschepke.wireguardautotunnel/)
|
|
||||||
|
|
||||||
|
<div style="text-align: left;">
|
||||||
|
|
||||||
|
## About
|
||||||
|
Inspired by the official [wireguard-android](https://github.com/WireGuard/wireguard-android) app, WG Tunnel was created to address features and support missing from the official app. This app combines support for both [WireGuard®](https://www.wireguard.com/)
|
||||||
|
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/), with its primary feature of auto-tunneling (on-demand tunneling).
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div style="text-align: left;">
|
||||||
|
|
||||||
<div align="left">
|
## Acknowledgements
|
||||||
|
|
||||||
This is an alternative Android Application for [WireGuard](https://www.wireguard.com/)
|
Thank you to the following:
|
||||||
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) with added
|
|
||||||
features. Built using the [wireguard-android](https://github.com/WireGuard/wireguard-android)
|
|
||||||
library and [Jetpack Compose](https://developer.android.com/jetpack/compose), this application was
|
|
||||||
inspired by the official [WireGuard Android](https://github.com/WireGuard/wireguard-android) app.
|
|
||||||
|
|
||||||
</div>
|
- All of the users that have helped contribute to the project with ideas, translations, feedback, bug reports, testing, and donations.
|
||||||
|
- [WireGuard®](https://www.wireguard.com/) - © Jason A. Donenfeld (https://github.com/WireGuard/wireguard-android)
|
||||||
|
|
||||||
<div align="center">
|
- [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) - Amnezia Team (https://github.com/amnezia-vpn/amneziawg-android)
|
||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
<p float="center">
|
</div>
|
||||||
<img label="Main" style="padding-right:25px" src="fastlane/metadata/android/en-US/images/phoneScreenshots/main_screen.png" width="200" />
|
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 10px;">
|
||||||
<img label="Config" style="padding-left:25px" src="fastlane/metadata/android/en-US/images/phoneScreenshots/config_screen.png" width="200" />
|
<img label="Main" src="fastlane/metadata/android/en-US/images/phoneScreenshots/main_screen.png" width="200" />
|
||||||
<img label="Settings" style="padding-left:25px" src="fastlane/metadata/android/en-US/images/phoneScreenshots/settings_screen.png" width="200" />
|
<img label="Settings" src="fastlane/metadata/android/en-US/images/phoneScreenshots/settings_screen.png" width="200" />
|
||||||
<img label="Support" style="padding-left:25px" src="fastlane/metadata/android/en-US/images/phoneScreenshots/support_screen.png" width="200" />
|
<img label="Auto" src="fastlane/metadata/android/en-US/images/phoneScreenshots/auto_screen.png" width="200" />
|
||||||
</p>
|
<img label="Config" src="fastlane/metadata/android/en-US/images/phoneScreenshots/config_screen.png" width="200" />
|
||||||
|
</div>
|
||||||
|
|
||||||
<div align="left">
|
<div style="text-align: left;">
|
||||||
|
|
||||||
## Inspiration
|
|
||||||
|
|
||||||
The original inspiration for this app came from the inconvenience of having to manually turn VPN off
|
|
||||||
and on while on different networks. This app was created to offer a free solution to this problem.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* Add tunnels via .conf file, zip, manual entry, or QR code
|
* Add tunnels via .conf file, zip, manual entry, clipboard, or QR code
|
||||||
* Auto connect to tunnels based on Wi-Fi SSID, ethernet, or mobile data
|
* Auto-tunnel based on Wi-Fi SSID, ethernet, or mobile data
|
||||||
* Split tunneling by application with search
|
* Split tunneling by application with search
|
||||||
* WireGuard support for kernel and userspace modes
|
* Support for kernel and userspace modes
|
||||||
* Amnezia support for userspace mode for DPI/censorship protection
|
* Amnezia support for userspace mode for DPI/censorship protection
|
||||||
* Pre/Post Up/Down scripts support for all modes on a rooted device
|
* Pre/Post Up/Down scripts support for all modes on a rooted device
|
||||||
* Always-On VPN support
|
* Always-On VPN support
|
||||||
* Export Amnezia and WireGuard tunnels to zip
|
* Export tunnels to zip
|
||||||
* Quick tile support for tunnel toggling, auto-tunneling
|
* Quick tile support for tunnel toggling, auto-tunneling
|
||||||
* Static shortcuts support for tunnel toggling, auto-tunneling
|
* Shortcuts support for tunnel toggling, auto-tunneling
|
||||||
* Intent automation support for all tunnels
|
* Intent automation support for all tunnels
|
||||||
|
* In app VPN kill switch with LAN bypass
|
||||||
* Automatic auto-tunneling service and/or tunnel restart after reboot or app update
|
* Automatic auto-tunneling service and/or tunnel restart after reboot or app update
|
||||||
* Battery preservation measures
|
* Battery preservation measures
|
||||||
* Restart tunnel on ping failure (beta)
|
* Restart tunnel on ping failure
|
||||||
|
|
||||||
## Fdroid
|
## Building
|
||||||
|
|
||||||
Want updates faster?
|
```sh
|
||||||
|
git clone https://github.com/zaneschepke/wgtunnel
|
||||||
|
cd wgtunnel
|
||||||
|
```
|
||||||
|
|
||||||
Check out my personal [fdroid repository](https://github.com/zaneschepke/fdroid) to get updates the
|
```sh
|
||||||
moment they are released.
|
./gradlew assembleDebug
|
||||||
|
```
|
||||||
## Docs
|
|
||||||
|
|
||||||
Information about features, behaviors, and answers to common questions can be found in the
|
|
||||||
app [documentation](https://zaneschepke.com/wgtunnel-docs/overview.html).
|
|
||||||
|
|
||||||
The repository for these docs can be found [here](https://github.com/zaneschepke/wgtunnel-docs).
|
|
||||||
|
|
||||||
## Translation
|
## Translation
|
||||||
|
|
||||||
@@ -87,19 +114,6 @@ Help translate WG Tunnel into your language
|
|||||||
at [Hosted Weblate](https://hosted.weblate.org/engage/wg-tunnel/).\
|
at [Hosted Weblate](https://hosted.weblate.org/engage/wg-tunnel/).\
|
||||||
[](https://hosted.weblate.org/engage/wg-tunnel/)
|
[](https://hosted.weblate.org/engage/wg-tunnel/)
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
```
|
|
||||||
$ git clone https://github.com/zaneschepke/wgtunnel
|
|
||||||
$ cd wgtunnel
|
|
||||||
```
|
|
||||||
|
|
||||||
And then build the app:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ ./gradlew assembleDebug
|
|
||||||
```
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Any contributions in the form of feedback, issues, code, or translations are welcome and much
|
Any contributions in the form of feedback, issues, code, or translations are welcome and much
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ val versionCodeIncrement = with(getBuildTaskName().lowercase()) {
|
|||||||
when {
|
when {
|
||||||
this.contains(Constants.NIGHTLY) || this.contains(Constants.PRERELEASE) -> {
|
this.contains(Constants.NIGHTLY) || this.contains(Constants.PRERELEASE) -> {
|
||||||
if (versionFile.exists()) {
|
if (versionFile.exists()) {
|
||||||
versionFile.readText().toInt() + 1
|
versionFile.readText().trim().toInt() + 1
|
||||||
} else {
|
} else {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,14 @@ android {
|
|||||||
generateLocaleConfig = true
|
generateLocaleConfig = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reproducibility
|
||||||
|
dependenciesInfo {
|
||||||
|
// Disables dependency metadata when building APKs.
|
||||||
|
includeInApk = false
|
||||||
|
// Disables dependency metadata when building Android App Bundles.
|
||||||
|
includeInBundle = false
|
||||||
|
}
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = Constants.APP_ID
|
applicationId = Constants.APP_ID
|
||||||
minSdk = Constants.MIN_SDK
|
minSdk = Constants.MIN_SDK
|
||||||
@@ -211,7 +219,6 @@ dependencies {
|
|||||||
|
|
||||||
// shortcuts
|
// shortcuts
|
||||||
implementation(libs.androidx.core)
|
implementation(libs.androidx.core)
|
||||||
implementation(libs.androidx.core.google.shortcuts)
|
|
||||||
|
|
||||||
// splash
|
// splash
|
||||||
implementation(libs.androidx.core.splashscreen)
|
implementation(libs.androidx.core.splashscreen)
|
||||||
|
|||||||
@@ -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')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 14,
|
||||||
|
"identityHash": "f2b260c389fb2e53216de40e4b1047f3",
|
||||||
|
"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, `debounce_delay_seconds` INTEGER NOT NULL DEFAULT 3)",
|
||||||
|
"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"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "debounceDelaySeconds",
|
||||||
|
"columnName": "debounce_delay_seconds",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true,
|
||||||
|
"defaultValue": "3"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, 'f2b260c389fb2e53216de40e4b1047f3')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,7 +68,10 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".ui.MainActivity"
|
android:name=".ui.MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:theme="@style/Theme.WireguardAutoTunnel">
|
android:windowSoftInputMode="adjustResize"
|
||||||
|
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" />
|
||||||
|
|
||||||
@@ -76,9 +79,6 @@
|
|||||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||||
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
|
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
<meta-data
|
|
||||||
android:name="android.app.shortcuts"
|
|
||||||
android:resource="@xml/shortcuts" />
|
|
||||||
</activity>
|
</activity>
|
||||||
<activity
|
<activity
|
||||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
||||||
@@ -155,7 +155,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,7 +164,7 @@
|
|||||||
tools:node="merge" />
|
tools:node="merge" />
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".service.foreground.TunnelBackgroundService"
|
android:name=".service.foreground.TunnelForegroundService"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:persistent="true"
|
android:persistent="true"
|
||||||
android:foregroundServiceType="systemExempted"
|
android:foregroundServiceType="systemExempted"
|
||||||
@@ -202,5 +202,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>
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
|
||||||
import android.os.StrictMode
|
import android.os.StrictMode
|
||||||
import android.os.StrictMode.ThreadPolicy
|
import android.os.StrictMode.ThreadPolicy
|
||||||
|
import androidx.lifecycle.DefaultLifecycleObserver
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.lifecycle.ProcessLifecycleOwner
|
||||||
import com.zaneschepke.logcatter.LogReader
|
import com.zaneschepke.logcatter.LogReader
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
|
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.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
|
||||||
@@ -15,16 +21,13 @@ 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
|
||||||
|
|
||||||
@HiltAndroidApp
|
@HiltAndroidApp
|
||||||
class WireGuardAutoTunnel : Application() {
|
class WireGuardAutoTunnel : Application() {
|
||||||
|
|
||||||
val localeStorage: LocaleStorage by lazy {
|
|
||||||
LocaleStorage(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
@@ -32,13 +35,27 @@ class WireGuardAutoTunnel : Application() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var logReader: LogReader
|
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
|
||||||
|
ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver())
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
Timber.plant(Timber.DebugTree())
|
Timber.plant(Timber.DebugTree())
|
||||||
StrictMode.setThreadPolicy(
|
StrictMode.setThreadPolicy(
|
||||||
@@ -52,18 +69,48 @@ class WireGuardAutoTunnel : Application() {
|
|||||||
} else {
|
} else {
|
||||||
Timber.plant(ReleaseTree())
|
Timber.plant(ReleaseTree())
|
||||||
}
|
}
|
||||||
if (!isRunningOnTv()) {
|
|
||||||
applicationScope.launch(ioDispatcher) {
|
applicationScope.launch {
|
||||||
logReader.start()
|
withContext(mainDispatcher) {
|
||||||
|
if (appStateRepository.isLocalLogsEnabled() && !isRunningOnTv()) logReader.initialize()
|
||||||
|
}
|
||||||
|
if (!settingsRepository.getSettings().isKernelEnabled) {
|
||||||
|
tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
|
||||||
|
}
|
||||||
|
appStateRepository.getLocale()?.let {
|
||||||
|
withContext(mainDispatcher) {
|
||||||
|
LocaleUtil.changeLocale(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun attachBaseContext(base: Context) {
|
override fun onTerminate() {
|
||||||
super.attachBaseContext(LocaleUtil.getLocalizedContext(base, LocaleStorage(base).getPreferredLocale()))
|
applicationScope.launch {
|
||||||
|
tunnelService.setBackendState(BackendState.INACTIVE, emptyList())
|
||||||
|
}
|
||||||
|
super.onTerminate()
|
||||||
|
}
|
||||||
|
|
||||||
|
class AppLifecycleObserver : DefaultLifecycleObserver {
|
||||||
|
|
||||||
|
override fun onStart(owner: LifecycleOwner) {
|
||||||
|
Timber.d("Application entered foreground")
|
||||||
|
foreground = true
|
||||||
|
}
|
||||||
|
override fun onPause(owner: LifecycleOwner) {
|
||||||
|
Timber.d("Application entered background")
|
||||||
|
foreground = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private var foreground = false
|
||||||
|
|
||||||
|
fun isForeground(): Boolean {
|
||||||
|
return foreground
|
||||||
|
}
|
||||||
|
|
||||||
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 = 11,
|
version = 14,
|
||||||
autoMigrations =
|
autoMigrations =
|
||||||
[
|
[
|
||||||
AutoMigration(from = 1, to = 2),
|
AutoMigration(from = 1, to = 2),
|
||||||
@@ -41,6 +41,18 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|||||||
to = 11,
|
to = 11,
|
||||||
spec = RemoveTunnelPauseMigration::class,
|
spec = RemoveTunnelPauseMigration::class,
|
||||||
),
|
),
|
||||||
|
AutoMigration(
|
||||||
|
from = 11,
|
||||||
|
to = 12,
|
||||||
|
),
|
||||||
|
AutoMigration(
|
||||||
|
from = 12,
|
||||||
|
to = 13,
|
||||||
|
),
|
||||||
|
AutoMigration(
|
||||||
|
from = 13,
|
||||||
|
to = 14,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
exportSchema = true,
|
exportSchema = true,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
+2
@@ -26,6 +26,8 @@ class DataStoreManager(
|
|||||||
val currentSSID = stringPreferencesKey("CURRENT_SSID")
|
val currentSSID = stringPreferencesKey("CURRENT_SSID")
|
||||||
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
|
||||||
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
|
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
|
||||||
|
val isLocalLogsEnabled = booleanPreferencesKey("LOCAL_LOGS_ENABLED")
|
||||||
|
val locale = stringPreferencesKey("LOCALE")
|
||||||
val theme = stringPreferencesKey("THEME")
|
val theme = stringPreferencesKey("THEME")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.data.datastore
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
|
||||||
|
|
||||||
class LocaleStorage(context: Context) {
|
|
||||||
private var preferences: SharedPreferences = context.getSharedPreferences("sp", Context.MODE_PRIVATE)
|
|
||||||
|
|
||||||
fun getPreferredLocale(): String {
|
|
||||||
return preferences.getString("preferred_locale", LocaleUtil.OPTION_PHONE_LANGUAGE)!!
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreferredLocale(localeCode: String) {
|
|
||||||
preferences.edit().putString("preferred_locale", localeCode).apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,8 @@ data class GeneralState(
|
|||||||
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 isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED,
|
||||||
|
val isLocalLogsEnabled: Boolean = IS_LOGS_ENABLED_DEFAULT,
|
||||||
|
val locale: String? = null,
|
||||||
val theme: Theme = Theme.AUTOMATIC,
|
val theme: Theme = Theme.AUTOMATIC,
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@@ -14,5 +16,6 @@ data class GeneralState(
|
|||||||
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_TUNNEL_STATS_EXPANDED = false
|
||||||
|
const val IS_LOGS_ENABLED_DEFAULT = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,4 +60,29 @@ data class Settings(
|
|||||||
defaultValue = "false",
|
defaultValue = "false",
|
||||||
)
|
)
|
||||||
val isWifiNameByShellEnabled: Boolean = 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,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "debounce_delay_seconds",
|
||||||
|
defaultValue = "3",
|
||||||
|
)
|
||||||
|
val debounceDelaySeconds: Int = 3,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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,10 +58,19 @@ 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 {
|
||||||
return configFromAmQuick(if (amQuick != "") amQuick else wgQuick)
|
return configFromAmQuick(if (amQuick.isNotBlank()) amQuick else wgQuick)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toWgConfig(): Config {
|
||||||
|
return configFromWgQuick(wgQuick)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -79,6 +89,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",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-4
@@ -17,10 +17,6 @@ interface AppStateRepository {
|
|||||||
|
|
||||||
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
|
||||||
|
|
||||||
suspend fun getCurrentSsid(): String?
|
|
||||||
|
|
||||||
suspend fun setCurrentSsid(ssid: String)
|
|
||||||
|
|
||||||
suspend fun isTunnelStatsExpanded(): Boolean
|
suspend fun isTunnelStatsExpanded(): Boolean
|
||||||
|
|
||||||
suspend fun setTunnelStatsExpanded(expanded: Boolean)
|
suspend fun setTunnelStatsExpanded(expanded: Boolean)
|
||||||
@@ -29,5 +25,13 @@ interface AppStateRepository {
|
|||||||
|
|
||||||
suspend fun getTheme(): 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>
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-8
@@ -38,14 +38,6 @@ class DataStoreAppStateRepository(
|
|||||||
dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown)
|
dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCurrentSsid(): String? {
|
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.currentSSID)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun setCurrentSsid(ssid: String) {
|
|
||||||
dataStoreManager.saveToDataStore(DataStoreManager.currentSSID, ssid)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun isTunnelStatsExpanded(): Boolean {
|
override suspend fun isTunnelStatsExpanded(): Boolean {
|
||||||
return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded)
|
return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded)
|
||||||
?: GeneralState.IS_TUNNEL_STATS_EXPANDED
|
?: GeneralState.IS_TUNNEL_STATS_EXPANDED
|
||||||
@@ -69,6 +61,22 @@ class DataStoreAppStateRepository(
|
|||||||
} ?: 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> =
|
||||||
dataStoreManager.preferencesFlow.map { prefs ->
|
dataStoreManager.preferencesFlow.map { prefs ->
|
||||||
prefs?.let { pref ->
|
prefs?.let { pref ->
|
||||||
@@ -84,6 +92,8 @@ class DataStoreAppStateRepository(
|
|||||||
pref[DataStoreManager.pinLockEnabled]
|
pref[DataStoreManager.pinLockEnabled]
|
||||||
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
|
||||||
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
|
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
|
||||||
|
isLocalLogsEnabled = pref[DataStoreManager.isLocalLogsEnabled] ?: GeneralState.IS_LOGS_ENABLED_DEFAULT,
|
||||||
|
locale = pref[DataStoreManager.locale],
|
||||||
theme = getTheme(),
|
theme = getTheme(),
|
||||||
)
|
)
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
|
|||||||
+13
@@ -53,6 +53,19 @@ 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)
|
||||||
|
|||||||
+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?
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ package com.zaneschepke.wireguardautotunnel.module
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.zaneschepke.logcatter.LogReader
|
import com.zaneschepke.logcatter.LogReader
|
||||||
import com.zaneschepke.logcatter.LogcatCollector
|
import com.zaneschepke.logcatter.LogcatReader
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.shortcut.DynamicShortcutManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutManager
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -25,6 +29,18 @@ class AppModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideLogCollect(@ApplicationContext context: Context): LogReader {
|
fun provideLogCollect(@ApplicationContext context: Context): LogReader {
|
||||||
return LogcatCollector.init(context = context)
|
return LogcatReader.init(storageDir = context.filesDir.absolutePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideNotificationService(@ApplicationContext context: Context): NotificationService {
|
||||||
|
return WireGuardNotification(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideShortcutManager(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): ShortcutManager {
|
||||||
|
return DynamicShortcutManager(context, ioDispatcher)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
|
import javax.inject.Qualifier
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class Wifi
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class MobileData
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class Ethernet
|
||||||
@@ -4,30 +4,24 @@ 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
|
||||||
import dagger.hilt.android.components.ServiceComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import dagger.hilt.android.scopes.ServiceScoped
|
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(ServiceComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
abstract class ServiceModule {
|
abstract class ServiceModule {
|
||||||
@Binds
|
|
||||||
@ServiceScoped
|
|
||||||
abstract fun provideNotificationService(wireGuardNotification: WireGuardNotification): NotificationService
|
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@Wifi
|
||||||
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
|
abstract fun provideWifiService(wifiService: WifiService): NetworkService
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@MobileData
|
||||||
abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService<MobileDataService>
|
abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@Ethernet
|
||||||
abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService<EthernetService>
|
abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ 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.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||||
|
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
|
||||||
@@ -76,6 +78,10 @@ class TunnelModule {
|
|||||||
@ApplicationScope applicationScope: CoroutineScope,
|
@ApplicationScope applicationScope: CoroutineScope,
|
||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
serviceManager: ServiceManager,
|
serviceManager: ServiceManager,
|
||||||
|
notificationService: NotificationService,
|
||||||
|
@Wifi wifiService: NetworkService,
|
||||||
|
@MobileData mobileDataService: NetworkService,
|
||||||
|
@Ethernet ethernetService: NetworkService,
|
||||||
): TunnelService {
|
): TunnelService {
|
||||||
return WireGuardTunnel(
|
return WireGuardTunnel(
|
||||||
amneziaBackend,
|
amneziaBackend,
|
||||||
@@ -85,12 +91,20 @@ class TunnelModule {
|
|||||||
applicationScope,
|
applicationScope,
|
||||||
ioDispatcher,
|
ioDispatcher,
|
||||||
serviceManager,
|
serviceManager,
|
||||||
|
notificationService,
|
||||||
|
wifiService,
|
||||||
|
mobileDataService,
|
||||||
|
ethernetService,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideServiceManager(@ApplicationContext context: Context): ServiceManager {
|
fun provideServiceManager(
|
||||||
return ServiceManager.getInstance(context)
|
@ApplicationContext context: Context,
|
||||||
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
|
appDataRepository: AppDataRepository,
|
||||||
|
): ServiceManager {
|
||||||
|
return ServiceManager(context, ioDispatcher, appDataRepository)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
|||||||
|
|
||||||
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
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
|
serviceManager.updateAutoTunnelTile()
|
||||||
applicationScope.launch {
|
applicationScope.launch {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
val settings = appDataRepository.settings.getSettings()
|
||||||
if (settings.isAutoTunnelEnabled) {
|
if (settings.isAutoTunnelEnabled) {
|
||||||
@@ -40,7 +42,7 @@ class AppUpdateReceiver : BroadcastReceiver() {
|
|||||||
}
|
}
|
||||||
if (!settings.isAutoTunnelEnabled) {
|
if (!settings.isAutoTunnelEnabled) {
|
||||||
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
|
||||||
if (tunnels.isNotEmpty()) tunnelService.get().startTunnel(tunnels.first(), true)
|
if (tunnels.isNotEmpty()) tunnelService.get().startTunnel(tunnels.first())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ class BootReceiver : BroadcastReceiver() {
|
|||||||
|
|
||||||
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
|
||||||
|
serviceManager.updateTunnelTile()
|
||||||
|
serviceManager.updateAutoTunnelTile()
|
||||||
applicationScope.launch {
|
applicationScope.launch {
|
||||||
with(appDataRepository.settings.getSettings()) {
|
with(appDataRepository.settings.getSettings()) {
|
||||||
if (isRestoreOnBootEnabled) {
|
if (isRestoreOnBootEnabled) {
|
||||||
@@ -39,7 +41,7 @@ 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")
|
||||||
tunnelService.get().startTunnel(activeTunnels.first(), true)
|
tunnelService.get().startTunnel(activeTunnels.first())
|
||||||
}
|
}
|
||||||
if (isAutoTunnelEnabled) {
|
if (isAutoTunnelEnabled) {
|
||||||
Timber.i("Starting watcher service from boot")
|
Timber.i("Starting watcher service from boot")
|
||||||
|
|||||||
@@ -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,
|
|
||||||
}
|
|
||||||
-556
@@ -1,556 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
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.network.EthernetService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.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
|
|
||||||
@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: AppDataRepository
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var notificationService: NotificationService
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@IoDispatcher
|
|
||||||
lateinit var ioDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var serviceManager: ServiceManager
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
@MainImmediateDispatcher
|
|
||||||
lateinit var mainImmediateDispatcher: CoroutineDispatcher
|
|
||||||
|
|
||||||
private val autoTunnelStateFlow = MutableStateFlow(AutoTunnelState())
|
|
||||||
|
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
|
||||||
|
|
||||||
private var wifiJob: Job? = null
|
|
||||||
private var mobileDataJob: Job? = null
|
|
||||||
private var ethernetJob: Job? = null
|
|
||||||
private var pingJob: Job? = null
|
|
||||||
private var networkEventJob: Job? = null
|
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
launchWatcherNotification()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
|
||||||
super.onBind(intent)
|
|
||||||
// We don't provide binding, so return null
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
||||||
Timber.d("onStartCommand executed with startId: $startId")
|
|
||||||
serviceManager.autoTunnelService.complete(this)
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start() {
|
|
||||||
kotlin.runCatching {
|
|
||||||
lifecycleScope.launch(mainImmediateDispatcher) {
|
|
||||||
launchWatcherNotification()
|
|
||||||
initWakeLock()
|
|
||||||
}
|
|
||||||
startSettingsJob()
|
|
||||||
startVpnStateJob()
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stop() {
|
|
||||||
wakeLock?.let {
|
|
||||||
if (it.isHeld) {
|
|
||||||
it.release()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stopSelf()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
cancelAndResetNetworkJobs()
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
serviceManager.autoTunnelService = CompletableDeferred()
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun launchWatcherNotification(description: String = getString(R.string.monitoring_state_changes)) {
|
|
||||||
val notification =
|
|
||||||
notificationService.createNotification(
|
|
||||||
channelId = getString(R.string.watcher_channel_id),
|
|
||||||
channelName = getString(R.string.watcher_channel_name),
|
|
||||||
title = getString(R.string.auto_tunnel_title),
|
|
||||||
description = description,
|
|
||||||
)
|
|
||||||
ServiceCompat.startForeground(
|
|
||||||
this,
|
|
||||||
foregroundId,
|
|
||||||
notification,
|
|
||||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun initWakeLock() {
|
|
||||||
wakeLock =
|
|
||||||
(getSystemService(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")
|
|
||||||
emitMobileDataConnected(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
emitMobileDataConnected(true)
|
|
||||||
Timber.i("Mobile data capabilities changed")
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
emitMobileDataConnected(false)
|
|
||||||
Timber.i("Lost mobile data connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForPingFailure() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
Timber.i("Starting ping watcher")
|
|
||||||
runCatching {
|
|
||||||
do {
|
|
||||||
val vpnState = tunnelService.get().vpnState.value
|
|
||||||
if (vpnState.status == TunnelState.UP) {
|
|
||||||
if (vpnState.tunnelConfig != null) {
|
|
||||||
val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick)
|
|
||||||
val results = if (vpnState.tunnelConfig.pingIp != null) {
|
|
||||||
Timber.d("Pinging custom ip : ${vpnState.tunnelConfig.pingIp}")
|
|
||||||
listOf(InetAddress.getByName(vpnState.tunnelConfig.pingIp).isReachable(Constants.PING_TIMEOUT.toInt()))
|
|
||||||
} else {
|
|
||||||
Timber.d("Pinging all peers")
|
|
||||||
config.peers.map { peer ->
|
|
||||||
peer.isReachable()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Timber.i("Ping results reachable: $results")
|
|
||||||
if (results.contains(false)) {
|
|
||||||
Timber.i("Restarting VPN for ping failure")
|
|
||||||
val cooldown = vpnState.tunnelConfig.pingCooldown
|
|
||||||
tunnelService.get().bounceTunnel(vpnState.tunnelConfig)
|
|
||||||
delay(cooldown ?: Constants.PING_COOLDOWN)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delay(vpnState.tunnelConfig?.pingInterval ?: Constants.PING_INTERVAL)
|
|
||||||
} while (true)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForSettingsChanges() {
|
|
||||||
Timber.i("Starting settings watcher")
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
appDataRepository.settings.getSettingsFlow().combine(
|
|
||||||
// ignore isActive changes to allow manual tunnel overrides
|
|
||||||
appDataRepository.tunnels.getTunnelConfigsFlow().distinctUntilChanged { old, new ->
|
|
||||||
old.map { it.isActive } != new.map { it.isActive }
|
|
||||||
},
|
|
||||||
) { settings, tunnels ->
|
|
||||||
Timber.d("Tunnels or settings changed!")
|
|
||||||
autoTunnelStateFlow.value.copy(
|
|
||||||
settings = settings,
|
|
||||||
tunnels = tunnels,
|
|
||||||
)
|
|
||||||
}.collect {
|
|
||||||
Timber.d("got new settings: ${it.settings}")
|
|
||||||
manageJobsBySettings(it.settings)
|
|
||||||
autoTunnelStateFlow.emit(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForVpnStateChanges() {
|
|
||||||
Timber.i("Starting vpn state watcher")
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
tunnelService.get().vpnState.distinctUntilChanged { old, new ->
|
|
||||||
old.tunnelConfig?.id == new.tunnelConfig?.id
|
|
||||||
}.collect { state ->
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(vpnState = state)
|
|
||||||
}
|
|
||||||
state.tunnelConfig?.let {
|
|
||||||
val settings = appDataRepository.settings.getSettings()
|
|
||||||
if (it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
|
||||||
}
|
|
||||||
if (!it.isPingEnabled && !settings.isPingEnabled) {
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun manageJobsBySettings(settings: Settings) {
|
|
||||||
with(settings) {
|
|
||||||
if (isPingEnabled) {
|
|
||||||
pingJob.onNotRunning { pingJob = startPingJob() }
|
|
||||||
} else {
|
|
||||||
cancelAndResetPingJob()
|
|
||||||
}
|
|
||||||
if (isTunnelOnWifiEnabled || isTunnelOnEthernetEnabled || isTunnelOnMobileDataEnabled) {
|
|
||||||
startNetworkJobs()
|
|
||||||
} else {
|
|
||||||
cancelAndResetNetworkJobs()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun startNetworkJobs() {
|
|
||||||
wifiJob.onNotRunning {
|
|
||||||
Timber.i("Wifi job starting")
|
|
||||||
wifiJob = startWifiJob()
|
|
||||||
}
|
|
||||||
ethernetJob.onNotRunning {
|
|
||||||
ethernetJob = startEthernetJob()
|
|
||||||
Timber.i("Ethernet job starting")
|
|
||||||
}
|
|
||||||
mobileDataJob.onNotRunning {
|
|
||||||
mobileDataJob = startMobileDataJob()
|
|
||||||
Timber.i("Mobile data job starting")
|
|
||||||
}
|
|
||||||
networkEventJob.onNotRunning {
|
|
||||||
Timber.i("Network event job starting")
|
|
||||||
networkEventJob = startNetworkEventJob()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun cancelAndResetPingJob() {
|
|
||||||
pingJob?.cancelWithMessage("Ping job canceled")
|
|
||||||
pingJob = null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun cancelAndResetNetworkJobs() {
|
|
||||||
networkEventJob?.cancelWithMessage("Network event job canceled")
|
|
||||||
wifiJob?.cancelWithMessage("Wifi job canceled")
|
|
||||||
ethernetJob?.cancelWithMessage("Ethernet job canceled")
|
|
||||||
mobileDataJob?.cancelWithMessage("Mobile data job canceled")
|
|
||||||
networkEventJob = null
|
|
||||||
wifiJob = null
|
|
||||||
ethernetJob = null
|
|
||||||
mobileDataJob = null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitEthernetConnected(connected: Boolean) {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isEthernetConnected = connected,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitWifiConnected(connected: Boolean) {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isWifiConnected = connected,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitWifiSSID(ssid: String) {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
currentNetworkSSID = ssid,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitMobileDataConnected(connected: Boolean) {
|
|
||||||
autoTunnelStateFlow.update {
|
|
||||||
it.copy(
|
|
||||||
isMobileDataConnected = 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")
|
|
||||||
emitEthernetConnected(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.i("Ethernet capabilities changed")
|
|
||||||
emitEthernetConnected(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
emitEthernetConnected(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")
|
|
||||||
emitWifiConnected(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.i("Wifi capabilities changed")
|
|
||||||
emitWifiConnected(true)
|
|
||||||
val ssid = getWifiSSID(status.networkCapabilities)
|
|
||||||
ssid?.let { name ->
|
|
||||||
if (name.contains(Constants.UNREADABLE_SSID)) {
|
|
||||||
Timber.w("SSID unreadable: missing permissions")
|
|
||||||
} else {
|
|
||||||
Timber.i("Detected valid SSID")
|
|
||||||
}
|
|
||||||
appDataRepository.appState.setCurrentSsid(name)
|
|
||||||
emitWifiSSID(name)
|
|
||||||
} ?: Timber.w("Failed to read ssid")
|
|
||||||
}
|
|
||||||
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
emitWifiConnected(false)
|
|
||||||
Timber.i("Lost Wi-Fi connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getWifiSSID(networkCapabilities: NetworkCapabilities): String? {
|
|
||||||
return withContext(ioDispatcher) {
|
|
||||||
with(autoTunnelStateFlow.value.settings) {
|
|
||||||
if (isWifiNameByShellEnabled) return@withContext rootShell.get().getCurrentWifiName()
|
|
||||||
wifiService.getNetworkName(networkCapabilities)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getMobileDataTunnel(): TunnelConfig? {
|
|
||||||
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun handleNetworkEventChanges() {
|
|
||||||
withContext(ioDispatcher) {
|
|
||||||
Timber.i("Starting network event watcher")
|
|
||||||
autoTunnelStateFlow.collect { watcherState ->
|
|
||||||
val autoTunnel = "Auto-tunnel watcher"
|
|
||||||
// delay for rapid network state changes and then collect latest
|
|
||||||
delay(Constants.WATCHER_COLLECTION_DELAY)
|
|
||||||
val activeTunnel = watcherState.vpnState.tunnelConfig
|
|
||||||
val defaultTunnel = appDataRepository.getPrimaryOrFirstTunnel()
|
|
||||||
val isTunnelDown = tunnelService.get().getState() == TunnelState.DOWN
|
|
||||||
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 == null || watcherState.isCurrentSSIDActiveTunnelNetwork() == false ||
|
|
||||||
isTunnelDown
|
|
||||||
) {
|
|
||||||
Timber.i(
|
|
||||||
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
|
|
||||||
)
|
|
||||||
watcherState.getTunnelWithMatchingTunnelNetwork()?.let {
|
|
||||||
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
|
|
||||||
if (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) }
|
|
||||||
}
|
|
||||||
// TODO disable for this now
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-107
@@ -1,107 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
|
|
||||||
|
|
||||||
data class AutoTunnelState(
|
|
||||||
val vpnState: VpnState = VpnState(),
|
|
||||||
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 &&
|
|
||||||
!isCurrentSSIDTrusted() &&
|
|
||||||
settings.isTunnelOnWifiEnabled
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTrustedWifiConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
(
|
|
||||||
isWifiConnected &&
|
|
||||||
isCurrentSSIDTrusted()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTunnelOffOnWifiConditionMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
(
|
|
||||||
isWifiConnected &&
|
|
||||||
!settings.isTunnelOnWifiEnabled
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isTunnelOffOnNoConnectivityMet(): Boolean {
|
|
||||||
return (
|
|
||||||
!isEthernetConnected &&
|
|
||||||
!isWifiConnected &&
|
|
||||||
!isMobileDataConnected
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isCurrentSSIDTrusted(): Boolean {
|
|
||||||
return if (settings.isWildcardsEnabled) {
|
|
||||||
settings.trustedNetworkSSIDs.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fun isCurrentSSIDActiveTunnelNetwork(): Boolean {
|
|
||||||
val currentTunnelNetworks = vpnState.tunnelConfig?.tunnelNetworks
|
|
||||||
return (
|
|
||||||
if (settings.isWildcardsEnabled) {
|
|
||||||
currentTunnelNetworks?.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
currentTunnelNetworks?.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
) == true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getTunnelWithMatchingTunnelNetwork(): TunnelConfig? {
|
|
||||||
return tunnels.firstOrNull {
|
|
||||||
if (settings.isWildcardsEnabled) {
|
|
||||||
it.tunnelNetworks.isMatchingToWildcardList(currentNetworkSSID)
|
|
||||||
} else {
|
|
||||||
it.tunnelNetworks.contains(currentNetworkSSID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+51
-17
@@ -3,28 +3,35 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
|||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.util.SingletonHolder
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
|
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 com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
||||||
import jakarta.inject.Inject
|
import jakarta.inject.Inject
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class ServiceManager
|
class ServiceManager
|
||||||
@Inject constructor(private val context: Context) {
|
@Inject constructor(private val context: Context, private val ioDispatcher: CoroutineDispatcher, private val appDataRepository: AppDataRepository) {
|
||||||
|
|
||||||
private val _autoTunnelActive = MutableStateFlow(false)
|
private val _autoTunnelActive = MutableStateFlow(false)
|
||||||
|
|
||||||
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
val autoTunnelActive = _autoTunnelActive.asStateFlow()
|
||||||
|
|
||||||
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
var autoTunnelService = CompletableDeferred<AutoTunnelService>()
|
||||||
var backgroundService = CompletableDeferred<TunnelBackgroundService>()
|
var backgroundService = CompletableDeferred<TunnelForegroundService>()
|
||||||
|
var autoTunnelTile = CompletableDeferred<AutoTunnelControlTile>()
|
||||||
companion object : SingletonHolder<ServiceManager, Context>(::ServiceManager)
|
var tunnelControlTile = CompletableDeferred<TunnelControlTile>()
|
||||||
|
|
||||||
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
|
||||||
runCatching {
|
runCatching {
|
||||||
@@ -38,23 +45,26 @@ class ServiceManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startAutoTunnel(background: Boolean) {
|
suspend fun startAutoTunnel(background: Boolean) {
|
||||||
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
|
||||||
if (autoTunnelService.isCompleted) return _autoTunnelActive.update { true }
|
if (autoTunnelService.isCompleted) return _autoTunnelActive.update { true }
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
startService(AutoTunnelService::class.java, background)
|
startService(AutoTunnelService::class.java, background)
|
||||||
autoTunnelService.await()
|
autoTunnelService.await()
|
||||||
autoTunnelService.getCompleted().start()
|
autoTunnelService.getCompleted().start()
|
||||||
_autoTunnelActive.update { true }
|
_autoTunnelActive.update { true }
|
||||||
|
updateAutoTunnelTile()
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startBackgroundService() {
|
suspend fun startBackgroundService(tunnelConfig: TunnelConfig) {
|
||||||
if (backgroundService.isCompleted) return
|
if (backgroundService.isCompleted) return
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
startService(TunnelBackgroundService::class.java, true)
|
startService(TunnelForegroundService::class.java, true)
|
||||||
backgroundService.await()
|
backgroundService.await()
|
||||||
backgroundService.getCompleted().start()
|
backgroundService.getCompleted().start(tunnelConfig)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
@@ -69,17 +79,41 @@ class ServiceManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopAutoTunnel() {
|
suspend fun toggleAutoTunnel(background: Boolean) {
|
||||||
if (!autoTunnelService.isCompleted) return
|
withContext(ioDispatcher) {
|
||||||
runCatching {
|
if (_autoTunnelActive.value) return@withContext stopAutoTunnel()
|
||||||
autoTunnelService.getCompleted().stop()
|
startAutoTunnel(background)
|
||||||
_autoTunnelActive.update { false }
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestTunnelTileUpdate() {
|
fun updateAutoTunnelTile() {
|
||||||
context.requestTunnelTileServiceStateUpdate()
|
if (autoTunnelTile.isCompleted) {
|
||||||
|
autoTunnelTile.getCompleted().updateTileState()
|
||||||
|
} else {
|
||||||
|
context.requestAutoTunnelTileServiceUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateTunnelTile() {
|
||||||
|
if (tunnelControlTile.isCompleted) {
|
||||||
|
tunnelControlTile.getCompleted().updateTileState()
|
||||||
|
} else {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-14
@@ -6,14 +6,17 @@ import android.os.IBinder
|
|||||||
import androidx.core.app.ServiceCompat
|
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.data.domain.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction
|
||||||
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 com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelBackgroundService : LifecycleService() {
|
class TunnelForegroundService : LifecycleService() {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var notificationService: NotificationService
|
lateinit var notificationService: NotificationService
|
||||||
@@ -21,16 +24,13 @@ class TunnelBackgroundService : LifecycleService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var serviceManager: ServiceManager
|
lateinit var serviceManager: ServiceManager
|
||||||
|
|
||||||
private val foregroundId = 123
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
start()
|
serviceManager.backgroundService.complete(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
super.onBind(intent)
|
super.onBind(intent)
|
||||||
// We don't provide binding, so return null
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,11 +39,11 @@ class TunnelBackgroundService : LifecycleService() {
|
|||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun start() {
|
fun start(tunnelConfig: TunnelConfig) {
|
||||||
ServiceCompat.startForeground(
|
ServiceCompat.startForeground(
|
||||||
this,
|
this@TunnelForegroundService,
|
||||||
foregroundId,
|
NotificationService.KERNEL_SERVICE_NOTIFICATION_ID,
|
||||||
createNotification(),
|
createNotification(tunnelConfig),
|
||||||
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -58,12 +58,13 @@ class TunnelBackgroundService : LifecycleService() {
|
|||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createNotification(): Notification {
|
private fun createNotification(tunnelConfig: TunnelConfig?): Notification {
|
||||||
return notificationService.createNotification(
|
return notificationService.createNotification(
|
||||||
getString(R.string.vpn_channel_id),
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
getString(R.string.vpn_channel_name),
|
title = "${getString(R.string.tunnel_running)} - ${tunnelConfig?.name}",
|
||||||
getString(R.string.tunnel_running),
|
actions = listOf(
|
||||||
description = "",
|
notificationService.createNotificationAction(NotificationAction.TUNNEL_OFF),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+247
@@ -0,0 +1,247 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel
|
||||||
|
|
||||||
|
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.wireguard.android.util.RootShell
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.AppShell
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Ethernet
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.MobileData
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Wifi
|
||||||
|
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.NetworkService
|
||||||
|
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.getCurrentWifiName
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.FlowPreview
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.debounce
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Provider
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class AutoTunnelService : LifecycleService() {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@AppShell
|
||||||
|
lateinit var rootShell: Provider<RootShell>
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Wifi
|
||||||
|
lateinit var wifiService: NetworkService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@MobileData
|
||||||
|
lateinit var mobileDataService: NetworkService
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Ethernet
|
||||||
|
lateinit var ethernetService: NetworkService
|
||||||
|
|
||||||
|
@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 defaultState = AutoTunnelState()
|
||||||
|
|
||||||
|
private val autoTunnelStateFlow = MutableStateFlow(defaultState)
|
||||||
|
|
||||||
|
private var wakeLock: PowerManager.WakeLock? = 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()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stop() {
|
||||||
|
wakeLock?.let { if (it.isHeld) it.release() }
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
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 startAutoTunnelStateJob() = lifecycleScope.launch(ioDispatcher) {
|
||||||
|
combine(
|
||||||
|
combineSettings(),
|
||||||
|
combineNetworkEventsJob(),
|
||||||
|
) { double, networkState ->
|
||||||
|
var wifiName: String? = null
|
||||||
|
if (networkState.wifiName == Constants.UNREADABLE_SSID && double.first.isTunnelOnWifiEnabled) {
|
||||||
|
wifiName = getWifiName(double.first)
|
||||||
|
}
|
||||||
|
val netState = wifiName?.let { networkState.copy(wifiName = it) } ?: networkState
|
||||||
|
AutoTunnelState(tunnelService.get().vpnState.value, netState, double.first, double.second)
|
||||||
|
}.collect { state ->
|
||||||
|
Timber.d("Network state: ${state.networkState}")
|
||||||
|
autoTunnelStateFlow.update {
|
||||||
|
it.copy(vpnState = state.vpnState, networkState = state.networkState, settings = state.settings, tunnels = state.tunnels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getWifiName(setting: Settings): String? {
|
||||||
|
return if (setting.isWifiNameByShellEnabled) {
|
||||||
|
rootShell.get().getCurrentWifiName()
|
||||||
|
} else if (wifiService.capabilities != null) {
|
||||||
|
WifiService.getNetworkName(wifiService.capabilities!!, this@AutoTunnelService)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(FlowPreview::class)
|
||||||
|
private fun combineNetworkEventsJob(): Flow<NetworkState> {
|
||||||
|
return combine(
|
||||||
|
wifiService.status,
|
||||||
|
mobileDataService.status,
|
||||||
|
ethernetService.status,
|
||||||
|
) { wifi, mobileData, ethernet ->
|
||||||
|
NetworkState(
|
||||||
|
wifi.available,
|
||||||
|
mobileData.available,
|
||||||
|
ethernet.available,
|
||||||
|
wifi.name,
|
||||||
|
)
|
||||||
|
}.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun combineSettings(): Flow<Pair<Settings, TunnelConfigs>> {
|
||||||
|
return combine(
|
||||||
|
appDataRepository.get().settings.getSettingsFlow(),
|
||||||
|
appDataRepository.get().tunnels.getTunnelConfigsFlow().map { tunnels ->
|
||||||
|
// isActive is ignored for equality checks so user can manually toggle off tunnel with auto-tunnel
|
||||||
|
tunnels.map { it.copy(isActive = false) }
|
||||||
|
},
|
||||||
|
) { settings, tunnels ->
|
||||||
|
Pair(settings, tunnels)
|
||||||
|
}.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(FlowPreview::class)
|
||||||
|
private fun startAutoTunnelJob() = lifecycleScope.launch(ioDispatcher) {
|
||||||
|
Timber.i("Starting auto-tunnel network event watcher")
|
||||||
|
val settings = appDataRepository.get().settings.getSettings()
|
||||||
|
val debounce = settings.debounceDelaySeconds * 1000L
|
||||||
|
Timber.d("Starting with debounce delay of: $debounce")
|
||||||
|
autoTunnelStateFlow.debounce(debounce).collect { watcherState ->
|
||||||
|
if (watcherState == defaultState) return@collect
|
||||||
|
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()
|
||||||
|
}
|
||||||
+145
@@ -0,0 +1,145 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
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,
|
||||||
|
) {
|
||||||
|
fun hasNoCapabilities(): Boolean {
|
||||||
|
return !isWifiConnected && !isMobileDataConnected && !isEthernetConnected
|
||||||
|
}
|
||||||
|
}
|
||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.net.Network
|
|
||||||
import android.net.NetworkCapabilities
|
|
||||||
import android.net.NetworkRequest
|
|
||||||
import android.net.wifi.WifiManager
|
|
||||||
import android.os.Build
|
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.callbackFlow
|
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
|
|
||||||
abstract class BaseNetworkService<T : BaseNetworkService<T>>(
|
|
||||||
val context: Context,
|
|
||||||
networkCapability: Int,
|
|
||||||
) : NetworkService<T> {
|
|
||||||
private val connectivityManager =
|
|
||||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
||||||
|
|
||||||
val wifiManager =
|
|
||||||
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
||||||
|
|
||||||
override val networkStatus =
|
|
||||||
callbackFlow {
|
|
||||||
val networkStatusCallback =
|
|
||||||
when (Build.VERSION.SDK_INT) {
|
|
||||||
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
|
||||||
object :
|
|
||||||
ConnectivityManager.NetworkCallback(
|
|
||||||
FLAG_INCLUDE_LOCATION_INFO,
|
|
||||||
) {
|
|
||||||
override fun onAvailable(network: Network) {
|
|
||||||
trySend(NetworkStatus.Available(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
|
||||||
trySend(
|
|
||||||
NetworkStatus.CapabilitiesChanged(
|
|
||||||
network,
|
|
||||||
networkCapabilities,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
object : ConnectivityManager.NetworkCallback() {
|
|
||||||
override fun onAvailable(network: Network) {
|
|
||||||
trySend(NetworkStatus.Available(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
|
||||||
trySend(
|
|
||||||
NetworkStatus.CapabilitiesChanged(
|
|
||||||
network,
|
|
||||||
networkCapabilities,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val request =
|
|
||||||
NetworkRequest.Builder()
|
|
||||||
.addTransportType(networkCapability)
|
|
||||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
||||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
||||||
.build()
|
|
||||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
|
||||||
|
|
||||||
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <Result> Flow<NetworkStatus>.map(
|
|
||||||
crossinline onUnavailable: suspend (network: Network) -> Result,
|
|
||||||
crossinline onAvailable: suspend (network: Network) -> Result,
|
|
||||||
crossinline onCapabilitiesChanged:
|
|
||||||
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
|
||||||
): Flow<Result> = map { status ->
|
|
||||||
when (status) {
|
|
||||||
is NetworkStatus.Unavailable -> onUnavailable(status.network)
|
|
||||||
is NetworkStatus.Available -> onAvailable(status.network)
|
|
||||||
is NetworkStatus.CapabilitiesChanged ->
|
|
||||||
onCapabilitiesChanged(
|
|
||||||
status.network,
|
|
||||||
status.networkCapabilities,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+53
-4
@@ -1,18 +1,67 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
package com.zaneschepke.wireguardautotunnel.service.network
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
|
import android.net.NetworkRequest
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.onStart
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class EthernetService
|
class EthernetService
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
) :
|
) : NetworkService {
|
||||||
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET) {
|
|
||||||
|
|
||||||
override fun isNetworkSecure(): Boolean {
|
override var capabilities: NetworkCapabilities? = null
|
||||||
return true
|
|
||||||
|
private val connectivityManager =
|
||||||
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
|
||||||
|
override val status = callbackFlow {
|
||||||
|
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
trySend(NetworkStatus.Unavailable())
|
||||||
|
}
|
||||||
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
|
capabilities = networkCapabilities
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val request =
|
||||||
|
NetworkRequest.Builder()
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
.build()
|
||||||
|
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||||
|
|
||||||
|
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||||
|
}.onStart {
|
||||||
|
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.catch {
|
||||||
|
Timber.e(it)
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.map {
|
||||||
|
when (it) {
|
||||||
|
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||||
|
is NetworkStatus.Unavailable -> Status(false, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-4
@@ -1,17 +1,67 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
package com.zaneschepke.wireguardautotunnel.service.network
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
|
import android.net.NetworkRequest
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.onStart
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MobileDataService
|
class MobileDataService
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
) :
|
) : NetworkService {
|
||||||
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR) {
|
|
||||||
override fun isNetworkSecure(): Boolean {
|
override var capabilities: NetworkCapabilities? = null
|
||||||
return false
|
|
||||||
|
private val connectivityManager =
|
||||||
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
|
||||||
|
override val status = callbackFlow {
|
||||||
|
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
trySend(NetworkStatus.Unavailable())
|
||||||
|
}
|
||||||
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
|
capabilities = networkCapabilities
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val request =
|
||||||
|
NetworkRequest.Builder()
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
.build()
|
||||||
|
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||||
|
|
||||||
|
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||||
|
}.onStart {
|
||||||
|
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.catch {
|
||||||
|
Timber.e(it)
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.map {
|
||||||
|
when (it) {
|
||||||
|
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||||
|
is NetworkStatus.Unavailable -> Status(false, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-8
@@ -1,14 +1,28 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
package com.zaneschepke.wireguardautotunnel.service.network
|
||||||
|
|
||||||
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
interface NetworkService<T> {
|
interface NetworkService {
|
||||||
fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
val status: Flow<Status>
|
||||||
return null
|
var capabilities: NetworkCapabilities?
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isNetworkSecure(): Boolean
|
inline fun <Result> Flow<NetworkStatus>.map(
|
||||||
|
crossinline onUnavailable: suspend () -> Result,
|
||||||
val networkStatus: Flow<NetworkStatus>
|
crossinline onAvailable: suspend (network: Network) -> Result,
|
||||||
|
crossinline onCapabilitiesChanged:
|
||||||
|
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
|
||||||
|
): Flow<Result> = map { status ->
|
||||||
|
when (status) {
|
||||||
|
is NetworkStatus.Unavailable -> onUnavailable()
|
||||||
|
is NetworkStatus.Available -> onAvailable(status.network)
|
||||||
|
is NetworkStatus.CapabilitiesChanged ->
|
||||||
|
onCapabilitiesChanged(
|
||||||
|
status.network,
|
||||||
|
status.networkCapabilities,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.network
|
||||||
|
|
||||||
|
data class Status(
|
||||||
|
val available: Boolean,
|
||||||
|
val name: String?,
|
||||||
|
)
|
||||||
+129
-13
@@ -1,32 +1,148 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.network
|
package com.zaneschepke.wireguardautotunnel.service.network
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO
|
||||||
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
|
import android.net.NetworkRequest
|
||||||
import android.net.wifi.SupplicantState
|
import android.net.wifi.SupplicantState
|
||||||
|
import android.net.wifi.WifiManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import com.wireguard.android.util.RootShell
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.AppShell
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.onStart
|
||||||
|
import kotlinx.coroutines.flow.transform
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Provider
|
||||||
|
|
||||||
class WifiService
|
class WifiService
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext private val context: Context,
|
||||||
) :
|
private val settingsRepository: SettingsRepository,
|
||||||
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI) {
|
@AppShell private val rootShell: Provider<RootShell>,
|
||||||
|
) : NetworkService {
|
||||||
|
|
||||||
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
override var capabilities: NetworkCapabilities? = null
|
||||||
var ssid = networkCapabilities.getWifiName()
|
|
||||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
|
val mutex = Mutex()
|
||||||
val info = wifiManager.connectionInfo
|
|
||||||
if (info.supplicantState === SupplicantState.COMPLETED) {
|
private var ssid: String? = null
|
||||||
ssid = info.ssid
|
private var available: Boolean = false
|
||||||
|
|
||||||
|
private val connectivityManager =
|
||||||
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
|
||||||
|
override val status = callbackFlow {
|
||||||
|
val networkStatusCallback =
|
||||||
|
when (Build.VERSION.SDK_INT) {
|
||||||
|
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
||||||
|
object :
|
||||||
|
ConnectivityManager.NetworkCallback(
|
||||||
|
FLAG_INCLUDE_LOCATION_INFO,
|
||||||
|
) {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
trySend(NetworkStatus.Unavailable())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
trySend(NetworkStatus.Unavailable())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||||
|
capabilities = networkCapabilities
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
val request =
|
||||||
|
NetworkRequest.Builder()
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
.build()
|
||||||
|
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||||
|
|
||||||
|
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
|
||||||
|
}.onStart {
|
||||||
|
// needed for services that are not yet available as it will impact later combine flows if we don't emit
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.catch {
|
||||||
|
Timber.e(it)
|
||||||
|
emit(NetworkStatus.Unavailable())
|
||||||
|
}.transform {
|
||||||
|
when (it) {
|
||||||
|
is NetworkStatus.Available -> mutex.withLock {
|
||||||
|
available = true
|
||||||
|
}
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> mutex.withLock {
|
||||||
|
if (available || ssid == null || ssid == Constants.UNREADABLE_SSID) {
|
||||||
|
available = false
|
||||||
|
Timber.d("Getting SSID from capabilities")
|
||||||
|
ssid = if (settingsRepository.getSettings().isWifiNameByShellEnabled) {
|
||||||
|
rootShell.get().getCurrentWifiName()
|
||||||
|
} else {
|
||||||
|
getNetworkName(it.networkCapabilities, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emit(Status(true, ssid))
|
||||||
|
}
|
||||||
|
is NetworkStatus.Unavailable -> emit(Status(false, null))
|
||||||
}
|
}
|
||||||
return ssid?.trim('"')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isNetworkSecure(): Boolean {
|
companion object {
|
||||||
// TODO
|
fun getNetworkName(networkCapabilities: NetworkCapabilities, context: Context): String? {
|
||||||
return false
|
var ssid = networkCapabilities.getWifiName()
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
|
||||||
|
val wifiManager =
|
||||||
|
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
val info = wifiManager.connectionInfo
|
||||||
|
if (info.supplicantState === SupplicantState.COMPLETED) {
|
||||||
|
ssid = info.ssid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ssid?.trim('"')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+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(notificationAction: 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+102
-64
@@ -1,14 +1,19 @@
|
|||||||
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.receiver.NotificationActionReceiver
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.MainActivity
|
import com.zaneschepke.wireguardautotunnel.ui.MainActivity
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -16,90 +21,123 @@ 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 {
|
setContentIntent(
|
||||||
it.description = title
|
|
||||||
it.enableLights(lights)
|
|
||||||
it.lightColor = Color.RED
|
|
||||||
it.enableVibration(vibration)
|
|
||||||
it.vibrationPattern = longArrayOf(100, 200, 300)
|
|
||||||
it
|
|
||||||
}
|
|
||||||
notificationManager.createNotificationChannel(channel)
|
|
||||||
val pendingIntent: PendingIntent =
|
|
||||||
Intent(context, MainActivity::class.java).let { notificationIntent ->
|
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
context,
|
context,
|
||||||
0,
|
0,
|
||||||
notificationIntent,
|
Intent(context, MainActivity::class.java),
|
||||||
PendingIntent.FLAG_IMMUTABLE,
|
PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
setContentText(description)
|
||||||
|
setOnlyAlertOnce(onlyAlertOnce)
|
||||||
|
setOngoing(onGoing)
|
||||||
|
setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
|
setShowWhen(showTimestamp)
|
||||||
|
setSmallIcon(R.drawable.ic_launcher)
|
||||||
|
}.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun createNotificationAction(notificationAction: NotificationAction): NotificationCompat.Action {
|
||||||
|
val pendingIntent = PendingIntent.getBroadcast(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
|
||||||
|
return when (this) {
|
||||||
|
NotificationChannels.AUTO_TUNNEL -> {
|
||||||
|
NotificationCompat.Builder(
|
||||||
|
context,
|
||||||
|
context.getString(R.string.auto_tunnel_channel_id),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
NotificationChannels.VPN -> {
|
||||||
|
NotificationCompat.Builder(
|
||||||
|
context,
|
||||||
|
context.getString(R.string.vpn_channel_id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val builder =
|
private 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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
|
import androidx.core.graphics.drawable.IconCompat
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
class DynamicShortcutManager(private val context: Context, @IoDispatcher private val ioDispatcher: CoroutineDispatcher) : ShortcutManager {
|
||||||
|
override suspend fun addShortcuts() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
ShortcutManagerCompat.setDynamicShortcuts(context, createShortcuts())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun removeShortcuts() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
ShortcutManagerCompat.removeDynamicShortcuts(context, createShortcuts().map { it.id })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createShortcuts(): List<ShortcutInfoCompat> {
|
||||||
|
return listOf(
|
||||||
|
buildShortcut(
|
||||||
|
context.getString(R.string.vpn_off),
|
||||||
|
context.getString(R.string.vpn_off),
|
||||||
|
context.getString(R.string.vpn_off),
|
||||||
|
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||||
|
putExtra("className", "WireGuardTunnelService")
|
||||||
|
action = ShortcutsActivity.Action.STOP.name
|
||||||
|
},
|
||||||
|
shortcutIcon = R.drawable.vpn_off,
|
||||||
|
),
|
||||||
|
buildShortcut(
|
||||||
|
context.getString(R.string.vpn_on),
|
||||||
|
context.getString(R.string.vpn_on),
|
||||||
|
context.getString(R.string.vpn_on),
|
||||||
|
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||||
|
putExtra("className", "WireGuardTunnelService")
|
||||||
|
action = ShortcutsActivity.Action.START.name
|
||||||
|
},
|
||||||
|
shortcutIcon = R.drawable.vpn_on,
|
||||||
|
),
|
||||||
|
buildShortcut(
|
||||||
|
context.getString(R.string.start_auto),
|
||||||
|
context.getString(R.string.start_auto),
|
||||||
|
context.getString(R.string.start_auto),
|
||||||
|
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||||
|
putExtra("className", "WireGuardConnectivityWatcherService")
|
||||||
|
action = ShortcutsActivity.Action.START.name
|
||||||
|
},
|
||||||
|
shortcutIcon = R.drawable.auto_play,
|
||||||
|
),
|
||||||
|
buildShortcut(
|
||||||
|
context.getString(R.string.stop_auto),
|
||||||
|
context.getString(R.string.stop_auto),
|
||||||
|
context.getString(R.string.stop_auto),
|
||||||
|
intent = Intent(context, ShortcutsActivity::class.java).apply {
|
||||||
|
putExtra("className", "WireGuardConnectivityWatcherService")
|
||||||
|
action = ShortcutsActivity.Action.STOP.name
|
||||||
|
},
|
||||||
|
shortcutIcon = R.drawable.auto_pause,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildShortcut(id: String, shortLabel: String, longLabel: String, intent: Intent, shortcutIcon: Int): ShortcutInfoCompat {
|
||||||
|
return ShortcutInfoCompat.Builder(context, id)
|
||||||
|
.setShortLabel(shortLabel)
|
||||||
|
.setLongLabel(longLabel)
|
||||||
|
.setIntent(intent)
|
||||||
|
.setIcon(IconCompat.createWithResource(context, shortcutIcon))
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
||||||
|
|
||||||
|
interface ShortcutManager {
|
||||||
|
suspend fun addShortcuts()
|
||||||
|
suspend fun removeShortcuts()
|
||||||
|
}
|
||||||
+8
-4
@@ -4,9 +4,8 @@ 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.AutoTunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.AutoTunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -46,8 +45,8 @@ 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 -> tunnelService.get().startTunnel(it, true)
|
Action.START.name -> tunnelService.get().startTunnel(it)
|
||||||
Action.STOP.name -> tunnelService.get().stopTunnel(it)
|
Action.STOP.name -> tunnelService.get().stopTunnel()
|
||||||
else -> Unit
|
else -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,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"
|
||||||
|
|||||||
+14
-26
@@ -4,21 +4,18 @@ import android.content.Intent
|
|||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import 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 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
|
||||||
|
|
||||||
@@ -29,32 +26,26 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
@ApplicationScope
|
@ApplicationScope
|
||||||
lateinit var applicationScope: CoroutineScope
|
lateinit var applicationScope: CoroutineScope
|
||||||
|
|
||||||
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
|
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
serviceManager.autoTunnelTile.complete(this)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
||||||
updateTileState()
|
updateTileState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTileState() {
|
fun updateTileState() {
|
||||||
serviceManager.autoTunnelActive.value.let {
|
serviceManager.autoTunnelActive.value.let {
|
||||||
if (it) setActive() else setInactive()
|
if (it) setActive() else setInactive()
|
||||||
}
|
}
|
||||||
@@ -63,7 +54,7 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
if (serviceManager.autoTunnelActive.value) {
|
if (serviceManager.autoTunnelActive.value) {
|
||||||
serviceManager.stopAutoTunnel()
|
serviceManager.stopAutoTunnel()
|
||||||
setInactive()
|
setInactive()
|
||||||
@@ -89,13 +80,6 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUnavailable() {
|
|
||||||
kotlin.runCatching {
|
|
||||||
qsTile.state = Tile.STATE_UNAVAILABLE
|
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
var ret: IBinder? = null
|
var ret: IBinder? = null
|
||||||
@@ -107,6 +91,10 @@ class AutoTunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
private fun setUnavailable() {
|
||||||
get() = lifecycleRegistry
|
kotlin.runCatching {
|
||||||
|
qsTile.state = Tile.STATE_UNAVAILABLE
|
||||||
|
qsTile.updateTile()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-44
@@ -5,78 +5,68 @@ import android.os.Build
|
|||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import com.zaneschepke.wireguardautotunnel.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 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 timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelControlTile : TileService(), LifecycleOwner {
|
class TunnelControlTile : TileService() {
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appDataRepository: AppDataRepository
|
lateinit var appDataRepository: AppDataRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: Provider<TunnelService>
|
lateinit var tunnelService: TunnelService
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@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()
|
||||||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
serviceManager.tunnelControlTile.complete(this)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
Timber.d("Start listening called")
|
||||||
Timber.d("Updating tile!")
|
serviceManager.tunnelControlTile.complete(this)
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
|
||||||
updateTileState()
|
updateTileState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateTileState() {
|
fun updateTileState() = applicationScope.launch {
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
|
||||||
lastActive?.let {
|
with(tunnelService.vpnState.value) {
|
||||||
updateTile(it)
|
if (status.isUp() && tunnelConfig != null) return@launch updateTile(tunnelConfig.name, true)
|
||||||
|
}
|
||||||
|
appDataRepository.getStartTunnelConfig()?.let {
|
||||||
|
updateTile(it.name, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
lifecycleScope.launch {
|
applicationScope.launch {
|
||||||
val lastActive = appDataRepository.getStartTunnelConfig()
|
if (tunnelService.vpnState.value.status.isUp()) return@launch tunnelService.stopTunnel()
|
||||||
lastActive?.let { tunnel ->
|
appDataRepository.getStartTunnelConfig()?.let {
|
||||||
if (tunnel.isActive) {
|
tunnelService.startTunnel(it)
|
||||||
tunnelService.get().stopTunnel(tunnel)
|
|
||||||
} else {
|
|
||||||
tunnelService.get().startTunnel(tunnel, true)
|
|
||||||
}
|
|
||||||
updateTileState()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,16 +106,6 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTile(tunnelConfig: TunnelConfig?) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
tunnelConfig?.let {
|
|
||||||
setTileDescription(it.name)
|
|
||||||
if (it.isActive) return setActive()
|
|
||||||
setInactive()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
/* This works around an annoying unsolved frameworks bug some people are hitting. */
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
var ret: IBinder? = null
|
var ret: IBinder? = null
|
||||||
@@ -137,6 +117,13 @@ class TunnelControlTile : TileService(), LifecycleOwner {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
override val lifecycle: Lifecycle
|
private fun updateTile(name: String, active: Boolean) {
|
||||||
get() = lifecycleRegistry
|
kotlin.runCatching {
|
||||||
|
setTileDescription(name)
|
||||||
|
if (active) return setActive()
|
||||||
|
setInactive()
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
|
enum class BackendState {
|
||||||
|
KILL_SWITCH_ACTIVE,
|
||||||
|
SERVICE_ACTIVE,
|
||||||
|
INACTIVE,
|
||||||
|
}
|
||||||
+10
-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, background: Boolean = false): Result<TunnelState>
|
|
||||||
|
|
||||||
suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
|
suspend fun startTunnel(tunnelConfig: TunnelConfig?)
|
||||||
|
|
||||||
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,7 +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) {
|
||||||
|
|||||||
+327
-128
@@ -2,29 +2,52 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel
|
|||||||
|
|
||||||
import com.wireguard.android.backend.Backend
|
import com.wireguard.android.backend.Backend
|
||||||
import com.wireguard.android.backend.Tunnel.State
|
import com.wireguard.android.backend.Tunnel.State
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Ethernet
|
||||||
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.module.MobileData
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Wifi
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.NetworkState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService.Companion.VPN_NOTIFICATION_ID
|
||||||
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.ui.common.snackbar.SnackbarController
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
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.asStateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
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 timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.net.InetAddress
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
import javax.inject.Provider
|
||||||
@@ -33,31 +56,44 @@ 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 serviceManager: ServiceManager,
|
||||||
|
private val notificationService: NotificationService,
|
||||||
|
@Wifi private val wifiService: NetworkService,
|
||||||
|
@MobileData private val mobileDataService: NetworkService,
|
||||||
|
@Ethernet private val ethernetService: NetworkService,
|
||||||
) : 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
|
||||||
|
private var pingJob: Job? = null
|
||||||
|
private var networkJob: Job? = null
|
||||||
|
|
||||||
private val runningHandle = AtomicBoolean(false)
|
@get:Synchronized @set:Synchronized
|
||||||
|
private var isKernelBackend: Boolean? = null
|
||||||
|
private val isNetworkAvailable = AtomicBoolean(false)
|
||||||
|
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,158 +105,202 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO refactor duplicate
|
||||||
|
@OptIn(FlowPreview::class)
|
||||||
|
private fun combineNetworkEventsJob(): Flow<NetworkState> {
|
||||||
|
return combine(
|
||||||
|
wifiService.status,
|
||||||
|
mobileDataService.status,
|
||||||
|
ethernetService.status,
|
||||||
|
) { wifi, mobileData, ethernet ->
|
||||||
|
NetworkState(
|
||||||
|
wifi.available,
|
||||||
|
mobileData.available,
|
||||||
|
ethernet.available,
|
||||||
|
wifi.name,
|
||||||
|
)
|
||||||
|
}.distinctUntilChanged()
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun setState(tunnelConfig: TunnelConfig, tunnelState: TunnelState): Result<TunnelState> {
|
private suspend fun setState(tunnelConfig: TunnelConfig, tunnelState: TunnelState): Result<TunnelState> {
|
||||||
return runCatching {
|
return runCatching {
|
||||||
when (val backend = backend()) {
|
when (val backend = backend()) {
|
||||||
is Backend -> backend.setState(this, tunnelState.toWgState(), TunnelConfig.configFromWgQuick(tunnelConfig.wgQuick)).let { TunnelState.from(it) }
|
is Backend -> backend.setState(this, tunnelState.toWgState(), tunnelConfig.toWgConfig()).let { TunnelState.from(it) }
|
||||||
is org.amnezia.awg.backend.Backend -> {
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
val config = if (tunnelConfig.amQuick.isBlank()) {
|
backend.setState(this, tunnelState.toAmState(), tunnelConfig.toAmConfig()).let {
|
||||||
TunnelConfig.configFromAmQuick(
|
|
||||||
tunnelConfig.wgQuick,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
TunnelConfig.configFromAmQuick(tunnelConfig.amQuick)
|
|
||||||
}
|
|
||||||
backend.setState(this, tunnelState.toAmState(), config).let {
|
|
||||||
TunnelState.from(it)
|
TunnelState.from(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> throw NotImplementedError()
|
else -> throw NotImplementedError()
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
|
// TODO add better error message to user, especially for kernel as exceptions contain no details
|
||||||
Timber.e(it)
|
Timber.e(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> {
|
private fun isTunnelAlreadyRunning(tunnelConfig: TunnelConfig): Boolean {
|
||||||
return withContext(ioDispatcher) {
|
return with(_vpnState.value) {
|
||||||
if (runningHandle.get() == true && tunnelConfig == vpnState.value.tunnelConfig) {
|
this.tunnelConfig?.id == tunnelConfig.id && status.isUp().also {
|
||||||
Timber.w("Tunnel already running")
|
if (it) Timber.w("Tunnel already running")
|
||||||
return@withContext Result.success(vpnState.value.status)
|
|
||||||
}
|
}
|
||||||
runningHandle.set(true)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun startTunnel(tunnelConfig: TunnelConfig?) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
if (tunnelConfig == null || isTunnelAlreadyRunning(tunnelConfig)) return@withContext
|
||||||
onBeforeStart(tunnelConfig)
|
onBeforeStart(tunnelConfig)
|
||||||
val settings = appDataRepository.settings.getSettings()
|
updateTunnelConfig(tunnelConfig) // need to update this here
|
||||||
if (background || settings.isKernelEnabled) startBackgroundService()
|
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
||||||
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
withServiceActive {
|
||||||
emitTunnelState(it)
|
setState(tunnelConfig, TunnelState.UP).onSuccess {
|
||||||
}.onFailure {
|
updateTunnelState(it, tunnelConfig)
|
||||||
Timber.e(it)
|
startActiveTunnelJobs()
|
||||||
onStartFailed()
|
}.onFailure {
|
||||||
|
onTunnelStop(tunnelConfig)
|
||||||
|
// TODO improve this with better statuses and handling
|
||||||
|
showTunnelStartFailed()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
|
private fun showTunnelStartFailed() {
|
||||||
return withContext(ioDispatcher) {
|
if (WireGuardAutoTunnel.isForeground()) {
|
||||||
onBeforeStop(tunnelConfig)
|
SnackbarController.showMessage(StringValue.StringResource(R.string.error_tunnel_start))
|
||||||
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
} else {
|
||||||
emitTunnelState(it)
|
launchStartFailedNotification()
|
||||||
}.onFailure {
|
}
|
||||||
Timber.e(it)
|
}
|
||||||
onStopFailed()
|
|
||||||
}.also {
|
private fun launchStartFailedNotification() {
|
||||||
stopBackgroundService()
|
with(notificationService) {
|
||||||
runningHandle.set(false)
|
val notification = createNotification(
|
||||||
|
WireGuardNotification.NotificationChannels.VPN,
|
||||||
|
title = context.getString(R.string.error_tunnel_start),
|
||||||
|
)
|
||||||
|
show(VPN_NOTIFICATION_ID, notification)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun stopTunnel() {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
if (_vpnState.value.status.isDown()) return@withContext
|
||||||
|
with(_vpnState.value) {
|
||||||
|
if (tunnelConfig == null) return@withContext
|
||||||
|
setState(tunnelConfig, TunnelState.DOWN).onSuccess {
|
||||||
|
onTunnelStop(tunnelConfig)
|
||||||
|
updateTunnelState(it, null)
|
||||||
|
}.onFailure {
|
||||||
|
clearJobsAndStats()
|
||||||
|
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
|
||||||
_vpnState.value.tunnelConfig?.let {
|
private suspend fun withServiceActive(callback: suspend () -> Unit) {
|
||||||
appDataRepository.tunnels.save(it.copy(isActive = true))
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onStartFailed() {
|
override suspend fun bounceTunnel() {
|
||||||
_vpnState.value.tunnelConfig?.let {
|
|
||||||
appDataRepository.tunnels.save(it.copy(isActive = false))
|
|
||||||
}
|
|
||||||
cancelStatsJob()
|
|
||||||
resetBackendStatistics()
|
|
||||||
runningHandle.set(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun shutDownActiveTunnel(config: TunnelConfig) {
|
|
||||||
with(_vpnState.value) {
|
with(_vpnState.value) {
|
||||||
if (status == TunnelState.UP && tunnelConfig != config) {
|
if (tunnelConfig != null && status.isUp()) {
|
||||||
tunnelConfig?.let { stopTunnel(it) }
|
withServiceActive {
|
||||||
|
toggleTunnel(tunnelConfig)
|
||||||
|
toggleTunnel(tunnelConfig)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun startBackgroundService() {
|
override suspend fun getBackendState(): BackendState {
|
||||||
serviceManager.startBackgroundService()
|
return when (val backend = backend()) {
|
||||||
serviceManager.requestTunnelTileUpdate()
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
|
backend.backendState.asBackendState()
|
||||||
|
}
|
||||||
|
is Backend -> BackendState.SERVICE_ACTIVE
|
||||||
|
else -> BackendState.INACTIVE
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopBackgroundService() {
|
override suspend fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
|
||||||
serviceManager.stopBackgroundService()
|
kotlin.runCatching {
|
||||||
serviceManager.requestTunnelTileUpdate()
|
when (val backend = backend()) {
|
||||||
|
is org.amnezia.awg.backend.Backend -> {
|
||||||
|
backend.setBackendState(backendState.asAmBackendState(), allowedIps)
|
||||||
|
}
|
||||||
|
is Backend -> {
|
||||||
|
// TODO not yet implemented
|
||||||
|
Timber.d("Kernel backend state not yet implemented")
|
||||||
|
}
|
||||||
|
else -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onBeforeStart(tunnelConfig: TunnelConfig) {
|
private suspend fun onBeforeStart(tunnelConfig: TunnelConfig) {
|
||||||
shutDownActiveTunnel(tunnelConfig)
|
with(_vpnState.value) {
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
|
if (status.isUp()) stopTunnel() else clearJobsAndStats()
|
||||||
emitVpnStateConfig(tunnelConfig)
|
serviceManager.startBackgroundService(tunnelConfig)
|
||||||
resetBackendStatistics()
|
}
|
||||||
startStatsJob()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) {
|
private suspend fun onTunnelStop(tunnelConfig: TunnelConfig) {
|
||||||
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
runCatching {
|
||||||
cancelStatsJob()
|
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
|
||||||
|
serviceManager.stopBackgroundService()
|
||||||
|
notificationService.remove(VPN_NOTIFICATION_ID)
|
||||||
|
clearJobsAndStats()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun clearJobsAndStats() {
|
||||||
|
cancelActiveTunnelJobs()
|
||||||
resetBackendStatistics()
|
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 {
|
||||||
@@ -231,14 +311,25 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancelStatsJob() {
|
override fun cancelActiveTunnelJobs() {
|
||||||
statsJob?.cancel()
|
statsJob?.cancelWithMessage("Tunnel stats job cancelled")
|
||||||
|
tunnelChangesJob?.cancelWithMessage("Tunnel changes job cancelled")
|
||||||
|
cancelPingJobs()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startStatsJob() {
|
override fun startActiveTunnelJobs() {
|
||||||
statsJob = startTunnelStatisticsJob()
|
statsJob = startTunnelStatisticsJob()
|
||||||
|
tunnelChangesJob = startTunnelConfigChangesJob()
|
||||||
|
if (_vpnState.value.tunnelConfig?.isPingEnabled == true) {
|
||||||
|
startPingJobs()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun startPingJobs() {
|
||||||
|
cancelPingJobs()
|
||||||
|
pingJob = startPingJob()
|
||||||
|
networkJob = startNetworkJob()
|
||||||
|
}
|
||||||
override fun getName(): String {
|
override fun getName(): String {
|
||||||
return _vpnState.value.tunnelConfig?.name ?: ""
|
return _vpnState.value.tunnelConfig?.name ?: ""
|
||||||
}
|
}
|
||||||
@@ -248,32 +339,140 @@ 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 -> updateBackendStatistics(
|
||||||
emitBackendStatistics(
|
AmneziaStatistics(
|
||||||
AmneziaStatistics(
|
backend.getStatistics(this@WireGuardTunnel),
|
||||||
backend.getStatistics(this@WireGuardTunnel),
|
),
|
||||||
),
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
delay(VPN_STATISTIC_CHECK_INTERVAL)
|
delay(VPN_STATISTIC_CHECK_INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isQuickConfigChanged(config: TunnelConfig): Boolean {
|
||||||
|
return with(_vpnState.value) {
|
||||||
|
if (tunnelConfig == null) return false
|
||||||
|
config.wgQuick != tunnelConfig.wgQuick ||
|
||||||
|
config.amQuick != tunnelConfig.amQuick
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isPingConfigMatching(config: TunnelConfig): Boolean {
|
||||||
|
return with(_vpnState.value.tunnelConfig) {
|
||||||
|
if (this == null) return true
|
||||||
|
config.isPingEnabled == isPingEnabled &&
|
||||||
|
pingIp == config.pingIp &&
|
||||||
|
config.pingCooldown == pingCooldown &&
|
||||||
|
config.pingInterval == pingInterval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handlePingConfigChanges() {
|
||||||
|
with(_vpnState.value.tunnelConfig) {
|
||||||
|
if (this == null) return
|
||||||
|
if (!isPingEnabled && pingJob?.isActive == true) {
|
||||||
|
cancelPingJobs()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
restartPingJob()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun restartPingJob() {
|
||||||
|
cancelPingJobs()
|
||||||
|
startPingJobs()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelPingJobs() {
|
||||||
|
pingJob?.cancelWithMessage("Ping job cancelled")
|
||||||
|
networkJob?.cancelWithMessage("Network job cancelled")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startTunnelConfigChangesJob() = applicationScope.launch(ioDispatcher) {
|
||||||
|
tunnelConfigRepository.getTunnelConfigsFlow().collect { tunnels ->
|
||||||
|
with(_vpnState.value) {
|
||||||
|
if (tunnelConfig == null) return@collect
|
||||||
|
val storageConfig = tunnels.firstOrNull { it.id == tunnelConfig.id }
|
||||||
|
if (storageConfig == null) return@collect
|
||||||
|
val quickChanged = isQuickConfigChanged(storageConfig)
|
||||||
|
val pingMatching = isPingConfigMatching(storageConfig)
|
||||||
|
updateTunnelConfig(storageConfig)
|
||||||
|
if (quickChanged) bounceTunnel()
|
||||||
|
if (!pingMatching) handlePingConfigChanges()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun pingTunnel(tunnelConfig: TunnelConfig): List<Boolean> {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
val config = tunnelConfig.toWgConfig()
|
||||||
|
if (tunnelConfig.pingIp != null) {
|
||||||
|
Timber.i("Pinging custom ip")
|
||||||
|
listOf(InetAddress.getByName(tunnelConfig.pingIp).isReachable(Constants.PING_TIMEOUT.toInt()))
|
||||||
|
} else {
|
||||||
|
Timber.i("Pinging all peers")
|
||||||
|
config.peers.map { peer ->
|
||||||
|
peer.isReachable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startPingJob() = applicationScope.launch(ioDispatcher) {
|
||||||
|
do {
|
||||||
|
run {
|
||||||
|
with(_vpnState.value) {
|
||||||
|
if (status.isUp() && tunnelConfig != null && isNetworkAvailable.get()) {
|
||||||
|
val reachable = pingTunnel(tunnelConfig)
|
||||||
|
if (reachable.contains(false)) {
|
||||||
|
if (isNetworkAvailable.get()) {
|
||||||
|
Timber.i("Ping result: target was not reachable, bouncing the tunnel")
|
||||||
|
bounceTunnel()
|
||||||
|
delay(tunnelConfig.pingCooldown ?: Constants.PING_COOLDOWN)
|
||||||
|
} else {
|
||||||
|
Timber.i("Ping result: target was not reachable, but not network available")
|
||||||
|
}
|
||||||
|
return@run
|
||||||
|
} else {
|
||||||
|
Timber.i("Ping result: all ping targets were reached successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(tunnelConfig?.pingInterval ?: Constants.PING_INTERVAL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startNetworkJob() = applicationScope.launch(ioDispatcher) {
|
||||||
|
combineNetworkEventsJob().collect {
|
||||||
|
Timber.d("New network state: $it")
|
||||||
|
if (!it.isWifiConnected && !it.isEthernetConnected && !it.isMobileDataConnected) {
|
||||||
|
isNetworkAvailable.set(false)
|
||||||
|
} else {
|
||||||
|
isNetworkAvailable.set(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,49 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.wireguard.android.backend.WgQuickBackend
|
||||||
|
import com.wireguard.android.util.RootShell
|
||||||
|
import com.wireguard.config.Config
|
||||||
|
import com.zaneschepke.logcatter.LogReader
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
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.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.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.service.tunnel.TunnelState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel.SplitTunnelApp
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.getAllInternetCapablePackages
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
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.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.onCompletion
|
import kotlinx.coroutines.flow.onCompletion
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.takeWhile
|
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
|
import javax.inject.Provider
|
||||||
@@ -32,9 +55,23 @@ constructor(
|
|||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
private val tunnelService: Provider<TunnelService>,
|
private val tunnelService: Provider<TunnelService>,
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
@AppShell private val rootShell: Provider<RootShell>,
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
|
private val logReader: LogReader,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
|
private val _popBackStack = MutableSharedFlow<Boolean>()
|
||||||
|
val popBackStack = _popBackStack.asSharedFlow()
|
||||||
|
|
||||||
|
private val _isAppReady = MutableStateFlow(false)
|
||||||
|
val isAppReady = _isAppReady.asStateFlow()
|
||||||
|
|
||||||
|
private val _configurationChange = MutableStateFlow(false)
|
||||||
|
val configurationChange = _configurationChange.asStateFlow()
|
||||||
|
|
||||||
|
private val _splitTunnelApps = MutableStateFlow<List<SplitTunnelApp>>(emptyList())
|
||||||
|
val splitTunnelApps = _splitTunnelApps.asStateFlow()
|
||||||
|
|
||||||
val uiState =
|
val uiState =
|
||||||
combine(
|
combine(
|
||||||
appDataRepository.settings.getSettingsFlow(),
|
appDataRepository.settings.getSettingsFlow(),
|
||||||
@@ -56,14 +93,13 @@ constructor(
|
|||||||
AppUiState(),
|
AppUiState(),
|
||||||
)
|
)
|
||||||
|
|
||||||
private val _isAppReady = MutableStateFlow<Boolean>(false)
|
|
||||||
val isAppReady = _isAppReady.asStateFlow()
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
initPin()
|
initPin()
|
||||||
initAutoTunnel()
|
initServices()
|
||||||
initTunnel()
|
launch {
|
||||||
|
initTunnel()
|
||||||
|
}
|
||||||
appReadyCheck()
|
appReadyCheck()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,12 +112,14 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun initTunnel() {
|
private suspend fun initTunnel() {
|
||||||
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
|
withContext(ioDispatcher) {
|
||||||
val activeTunnels = appDataRepository.tunnels.getActive()
|
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startActiveTunnelJobs()
|
||||||
if (activeTunnels.isNotEmpty() &&
|
val activeTunnels = appDataRepository.tunnels.getActive()
|
||||||
tunnelService.get().getState() == TunnelState.DOWN
|
if (activeTunnels.isNotEmpty() &&
|
||||||
) {
|
tunnelService.get().getState() == TunnelState.DOWN
|
||||||
tunnelService.get().startTunnel(activeTunnels.first())
|
) {
|
||||||
|
tunnelService.get().startTunnel(activeTunnels.first())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,9 +128,16 @@ constructor(
|
|||||||
if (isPinEnabled) PinManager.initialize(WireGuardAutoTunnel.instance)
|
if (isPinEnabled) PinManager.initialize(WireGuardAutoTunnel.instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun initAutoTunnel() {
|
private suspend fun initServices() {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
withContext(ioDispatcher) {
|
||||||
if (settings.isAutoTunnelEnabled) serviceManager.startAutoTunnel(false)
|
val settings = appDataRepository.settings.getSettings()
|
||||||
|
handleVpnKillSwitchChange(settings.isVpnKillSwitchEnabled)
|
||||||
|
if (settings.isAutoTunnelEnabled) serviceManager.startAutoTunnel(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveSettings(settings: Settings) = viewModelScope.launch {
|
||||||
|
appDataRepository.settings.save(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
|
||||||
@@ -107,4 +152,269 @@ constructor(
|
|||||||
fun setLocationDisclosureShown() = viewModelScope.launch {
|
fun setLocationDisclosureShown() = viewModelScope.launch {
|
||||||
appDataRepository.appState.setLocationDisclosureShown(true)
|
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) return@withContext tunnelService.get().setBackendState(BackendState.SERVICE_ACTIVE, emptySet())
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
tunnelService.get().setBackendState(BackendState.INACTIVE, emptyList())
|
||||||
|
appDataRepository.settings.save(
|
||||||
|
copy(
|
||||||
|
isKernelEnabled = true,
|
||||||
|
isAmneziaEnabled = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
saveKernelMode(enabled = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun isKernelSupported(): Boolean {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
WgQuickBackend.hasKernelSupport()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun getEmitSplitTunnelApps(context: Context) {
|
||||||
|
withContext(ioDispatcher) {
|
||||||
|
val apps = context.getAllInternetCapablePackages().filter { it.applicationInfo != null }.map { pack ->
|
||||||
|
SplitTunnelApp(
|
||||||
|
context.packageManager.getApplicationIcon(pack.applicationInfo!!),
|
||||||
|
context.packageManager.getApplicationLabel(pack.applicationInfo!!).toString(),
|
||||||
|
pack.packageName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_splitTunnelApps.emit(apps)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun requestRoot(): Result<Unit> {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
rootShell.get().start()
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.root_accepted))
|
||||||
|
}.onFailure {
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.error_root_denied))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateExistingTunnelConfig(
|
||||||
|
tunnelConfig: TunnelConfig,
|
||||||
|
tunnelName: String? = null,
|
||||||
|
peers: List<PeerProxy>? = null,
|
||||||
|
`interface`: InterfaceProxy? = null,
|
||||||
|
) = viewModelScope.launch {
|
||||||
|
runCatching {
|
||||||
|
val amConfig = tunnelConfig.toAmConfig()
|
||||||
|
val wgConfig = tunnelConfig.toWgConfig()
|
||||||
|
updateTunnelConfig(tunnelConfig, tunnelName, amConfig, wgConfig, peers, `interface`)
|
||||||
|
_popBackStack.emit(true)
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.config_changes_saved))
|
||||||
|
}.onFailure {
|
||||||
|
onConfigSaveError(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveNewTunnel(tunnelName: String, peers: List<PeerProxy>, `interface`: InterfaceProxy) = viewModelScope.launch {
|
||||||
|
runCatching {
|
||||||
|
val config = buildConfigs(peers, `interface`)
|
||||||
|
appDataRepository.tunnels.save(
|
||||||
|
TunnelConfig(
|
||||||
|
name = tunnelName,
|
||||||
|
wgQuick = config.first.toWgQuickString(true),
|
||||||
|
amQuick = config.second.toAwgQuickString(true),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
_popBackStack.emit(true)
|
||||||
|
SnackbarController.showMessage(StringValue.StringResource(R.string.config_changes_saved))
|
||||||
|
}.onFailure {
|
||||||
|
onConfigSaveError(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onConfigSaveError(throwable: Throwable) {
|
||||||
|
Timber.e(throwable)
|
||||||
|
SnackbarController.showMessage(
|
||||||
|
throwable.message?.let { message ->
|
||||||
|
(StringValue.DynamicString(message))
|
||||||
|
} ?: StringValue.StringResource(R.string.unknown_error),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun updateTunnelConfig(
|
||||||
|
tunnelConfig: TunnelConfig,
|
||||||
|
tunnelName: String? = null,
|
||||||
|
amConfig: org.amnezia.awg.config.Config,
|
||||||
|
wgConfig: Config,
|
||||||
|
peers: List<PeerProxy>? = null,
|
||||||
|
`interface`: InterfaceProxy? = null,
|
||||||
|
) {
|
||||||
|
val configs = rebuildConfigs(amConfig, wgConfig, peers, `interface`)
|
||||||
|
appDataRepository.tunnels.save(
|
||||||
|
tunnelConfig.copy(
|
||||||
|
name = tunnelName ?: tunnelConfig.name,
|
||||||
|
amQuick = configs.second.toAwgQuickString(true),
|
||||||
|
wgQuick = configs.first.toWgQuickString(true),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cleanUpUninstalledApps(tunnelConfig: TunnelConfig, packages: List<String>) = viewModelScope.launch(ioDispatcher) {
|
||||||
|
runCatching {
|
||||||
|
val amConfig = tunnelConfig.toAmConfig()
|
||||||
|
val wgConfig = tunnelConfig.toWgConfig()
|
||||||
|
val proxy = InterfaceProxy.from(amConfig.`interface`)
|
||||||
|
if (proxy.includedApplications.isEmpty() && proxy.excludedApplications.isEmpty()) return@launch
|
||||||
|
if (proxy.includedApplications.retainAll(packages.toSet()) || proxy.excludedApplications.retainAll(packages.toSet())) {
|
||||||
|
updateTunnelConfig(tunnelConfig, amConfig = amConfig, wgConfig = wgConfig, `interface` = proxy)
|
||||||
|
Timber.i("Removed split tunnel package for app that no longer exists on the device")
|
||||||
|
}
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bounceAutoTunnel() = viewModelScope.launch(ioDispatcher) {
|
||||||
|
serviceManager.stopAutoTunnel()
|
||||||
|
delay(1000L)
|
||||||
|
serviceManager.startAutoTunnel(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun rebuildConfigs(
|
||||||
|
amConfig: org.amnezia.awg.config.Config,
|
||||||
|
wgConfig: Config,
|
||||||
|
peers: List<PeerProxy>? = null,
|
||||||
|
`interface`: InterfaceProxy? = null,
|
||||||
|
): Pair<Config, org.amnezia.awg.config.Config> {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
Pair(
|
||||||
|
Config.Builder().apply {
|
||||||
|
addPeers(peers?.map { it.toWgPeer() } ?: wgConfig.peers)
|
||||||
|
setInterface(`interface`?.toWgInterface() ?: wgConfig.`interface`)
|
||||||
|
}.build(),
|
||||||
|
org.amnezia.awg.config.Config.Builder().apply {
|
||||||
|
addPeers(peers?.map { it.toAmPeer() } ?: amConfig.peers)
|
||||||
|
setInterface(`interface`?.toAmInterface() ?: amConfig.`interface`)
|
||||||
|
}.build(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun buildConfigs(peers: List<PeerProxy>, `interface`: InterfaceProxy): Pair<Config, org.amnezia.awg.config.Config> {
|
||||||
|
return withContext(ioDispatcher) {
|
||||||
|
Pair(
|
||||||
|
Config.Builder().apply {
|
||||||
|
addPeers(peers.map { it.toWgPeer() })
|
||||||
|
setInterface(`interface`.toWgInterface())
|
||||||
|
}.build(),
|
||||||
|
org.amnezia.awg.config.Config.Builder().apply {
|
||||||
|
addPeers(peers.map { it.toAmPeer() })
|
||||||
|
setInterface(`interface`.toAmInterface())
|
||||||
|
}.build(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Intent
|
||||||
|
import android.graphics.Color.TRANSPARENT
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.activity.SystemBarStyle
|
||||||
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.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.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
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
|
||||||
@@ -24,31 +30,26 @@ import androidx.compose.material3.surfaceColorAtElevation
|
|||||||
import androidx.compose.runtime.CompositionLocalProvider
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.compose.rememberNavController
|
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.WireGuardAutoTunnel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
|
||||||
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.LocalFocusRequester
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarControllerProvider
|
||||||
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.pinlock.PinLockScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.pinlock.PinLockScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.scanner.ScannerScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.scanner.ScannerScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
||||||
@@ -56,38 +57,48 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.Appear
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.display.DisplayScreen
|
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.appearance.language.LanguageScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.AutoTunnelScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.AutoTunnelScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced.AdvancedScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.LocationDisclosureScreen
|
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.screens.tunneloptions.OptionsScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.ConfigScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.splittunnel.SplitTunnelScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.tunnelautotunnel.TunnelAutoTunnelScreen
|
||||||
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.LocaleUtil
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import timber.log.Timber
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private val localeStorage: LocaleStorage by lazy {
|
|
||||||
(application as WireGuardAutoTunnel).localeStorage
|
|
||||||
}
|
|
||||||
|
|
||||||
private lateinit var oldPrefLocaleCode: String
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var appStateRepository: AppStateRepository
|
lateinit var appStateRepository: AppStateRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var tunnelService: TunnelService
|
lateinit var tunnelService: TunnelService
|
||||||
|
|
||||||
private val viewModel by viewModels<AppViewModel>()
|
@Inject
|
||||||
|
lateinit var shortcutManager: ShortcutManager
|
||||||
|
|
||||||
|
@OptIn(ExperimentalLayoutApi::class)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
enableEdgeToEdge(
|
||||||
|
statusBarStyle = SystemBarStyle.auto(TRANSPARENT, TRANSPARENT),
|
||||||
|
navigationBarStyle = SystemBarStyle.auto(TRANSPARENT, TRANSPARENT),
|
||||||
|
)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
window.isNavigationBarContrastEnforced = false
|
||||||
|
}
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
val viewModel by viewModels<AppViewModel>()
|
||||||
|
|
||||||
installSplashScreen().apply {
|
installSplashScreen().apply {
|
||||||
setKeepOnScreenCondition {
|
setKeepOnScreenCondition {
|
||||||
!viewModel.isAppReady.value
|
!viewModel.isAppReady.value
|
||||||
@@ -95,13 +106,21 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val appUiState by viewModel.uiState.collectAsStateWithLifecycle(lifecycle = this.lifecycle)
|
val appUiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
val configurationChange by viewModel.configurationChange.collectAsStateWithLifecycle()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
val rootItemFocusRequester = remember { FocusRequester() }
|
|
||||||
|
|
||||||
LaunchedEffect(appUiState.tunnels) {
|
LaunchedEffect(configurationChange) {
|
||||||
Timber.d("Updating launched")
|
if (configurationChange) {
|
||||||
requestTunnelTileServiceStateUpdate()
|
Intent(this@MainActivity, MainActivity::class.java).also {
|
||||||
|
startActivity(it)
|
||||||
|
exitProcess(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
viewModel.getEmitSplitTunnelApps(this@MainActivity)
|
||||||
}
|
}
|
||||||
|
|
||||||
LaunchedEffect(appUiState.autoTunnelActive) {
|
LaunchedEffect(appUiState.autoTunnelActive) {
|
||||||
@@ -112,110 +131,130 @@ class MainActivity : AppCompatActivity() {
|
|||||||
LaunchedEffect(isAutoTunnelEnabled) {
|
LaunchedEffect(isAutoTunnelEnabled) {
|
||||||
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
this@MainActivity.requestAutoTunnelTileServiceUpdate()
|
||||||
}
|
}
|
||||||
|
LaunchedEffect(isShortcutsEnabled) {
|
||||||
|
if (!isShortcutsEnabled) return@LaunchedEffect shortcutManager.removeShortcuts()
|
||||||
|
shortcutManager.addShortcuts()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CompositionLocalProvider(LocalFocusRequester provides rootItemFocusRequester) {
|
CompositionLocalProvider(LocalNavController provides navController) {
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
SnackbarControllerProvider { host ->
|
||||||
SnackbarControllerProvider { host ->
|
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
||||||
WireguardAutoTunnelTheme(theme = appUiState.generalState.theme) {
|
Scaffold(
|
||||||
Scaffold(
|
contentWindowInsets = WindowInsets(0),
|
||||||
contentWindowInsets = WindowInsets(0.dp),
|
snackbarHost = {
|
||||||
snackbarHost = {
|
SnackbarHost(host) { snackbarData: SnackbarData ->
|
||||||
SnackbarHost(host) { snackbarData: SnackbarData ->
|
CustomSnackBar(
|
||||||
CustomSnackBar(
|
snackbarData.visuals.message,
|
||||||
snackbarData.visuals.message,
|
isRtl = false,
|
||||||
isRtl = false,
|
containerColor =
|
||||||
containerColor =
|
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||||
MaterialTheme.colorScheme.surfaceColorAtElevation(
|
2.dp,
|
||||||
2.dp,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
bottomBar = {
|
|
||||||
BottomNavBar(
|
|
||||||
navController,
|
|
||||||
listOf(
|
|
||||||
BottomNavItem(
|
|
||||||
name = stringResource(R.string.tunnels),
|
|
||||||
route = Route.Main,
|
|
||||||
icon = Icons.Rounded.Home,
|
|
||||||
),
|
|
||||||
BottomNavItem(
|
|
||||||
name = stringResource(R.string.settings),
|
|
||||||
route = Route.Settings,
|
|
||||||
icon = Icons.Rounded.Settings,
|
|
||||||
),
|
|
||||||
BottomNavItem(
|
|
||||||
name = stringResource(R.string.support),
|
|
||||||
route = Route.Support,
|
|
||||||
icon = Icons.Rounded.QuestionMark,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
) {
|
},
|
||||||
Box(modifier = Modifier.fillMaxSize().padding(it)) {
|
bottomBar = {
|
||||||
NavHost(
|
BottomNavBar(
|
||||||
navController,
|
navController,
|
||||||
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
listOf(
|
||||||
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
BottomNavItem(
|
||||||
startDestination = (if (appUiState.generalState.isPinLockEnabled == true) Route.Lock else Route.Main),
|
name = stringResource(R.string.tunnels),
|
||||||
) {
|
route = Route.Main,
|
||||||
composable<Route.Main> {
|
icon = Icons.Rounded.Home,
|
||||||
MainScreen(
|
),
|
||||||
uiState = appUiState,
|
BottomNavItem(
|
||||||
)
|
name = stringResource(R.string.settings),
|
||||||
}
|
route = Route.Settings,
|
||||||
composable<Route.Settings> {
|
icon = Icons.Rounded.Settings,
|
||||||
SettingsScreen(
|
),
|
||||||
appViewModel = viewModel,
|
BottomNavItem(
|
||||||
uiState = appUiState,
|
name = stringResource(R.string.support),
|
||||||
)
|
route = Route.Support,
|
||||||
}
|
icon = Icons.Rounded.QuestionMark,
|
||||||
composable<Route.LocationDisclosure> {
|
),
|
||||||
LocationDisclosureScreen(viewModel, appUiState)
|
),
|
||||||
}
|
)
|
||||||
composable<Route.AutoTunnel> {
|
},
|
||||||
AutoTunnelScreen(
|
) { padding ->
|
||||||
appUiState,
|
Box(modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||||
)
|
NavHost(
|
||||||
}
|
navController,
|
||||||
composable<Route.Appearance> {
|
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||||
AppearanceScreen()
|
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
|
||||||
}
|
startDestination = (if (appUiState.generalState.isPinLockEnabled) Route.Lock else Route.Main),
|
||||||
composable<Route.Language> {
|
) {
|
||||||
LanguageScreen(localeStorage)
|
composable<Route.Main> {
|
||||||
}
|
MainScreen(
|
||||||
composable<Route.Display> {
|
uiState = appUiState,
|
||||||
DisplayScreen(appUiState)
|
)
|
||||||
}
|
}
|
||||||
composable<Route.Support> {
|
composable<Route.Settings> {
|
||||||
SupportScreen()
|
SettingsScreen(
|
||||||
}
|
appViewModel = viewModel,
|
||||||
composable<Route.Logs> {
|
uiState = appUiState,
|
||||||
LogsScreen()
|
)
|
||||||
}
|
}
|
||||||
composable<Route.Config> {
|
composable<Route.LocationDisclosure> {
|
||||||
val args = it.toRoute<Route.Config>()
|
LocationDisclosureScreen(viewModel, appUiState)
|
||||||
ConfigScreen(
|
}
|
||||||
tunnelId = args.id,
|
composable<Route.AutoTunnel> {
|
||||||
)
|
AutoTunnelScreen(
|
||||||
}
|
appUiState,
|
||||||
composable<Route.Option> {
|
)
|
||||||
val args = it.toRoute<Route.Option>()
|
}
|
||||||
OptionsScreen(
|
composable<Route.Appearance> {
|
||||||
tunnelId = args.id,
|
AppearanceScreen()
|
||||||
appUiState = appUiState,
|
}
|
||||||
)
|
composable<Route.Language> {
|
||||||
}
|
LanguageScreen(appUiState, viewModel)
|
||||||
composable<Route.Lock> {
|
}
|
||||||
PinLockScreen(
|
composable<Route.Display> {
|
||||||
appViewModel = viewModel,
|
DisplayScreen(appUiState)
|
||||||
)
|
}
|
||||||
}
|
composable<Route.Support> {
|
||||||
composable<Route.Scanner> {
|
SupportScreen(appUiState, viewModel)
|
||||||
ScannerScreen()
|
}
|
||||||
|
composable<Route.AutoTunnelAdvanced> {
|
||||||
|
AdvancedScreen(appUiState, viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.Logs> {
|
||||||
|
LogsScreen()
|
||||||
|
}
|
||||||
|
composable<Route.Config> {
|
||||||
|
val args = it.toRoute<Route.Config>()
|
||||||
|
val config = appUiState.tunnels.firstOrNull { it.id == args.id }
|
||||||
|
ConfigScreen(config, viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.TunnelOptions> {
|
||||||
|
val args = it.toRoute<Route.TunnelOptions>()
|
||||||
|
val config = appUiState.tunnels.first { it.id == args.id }
|
||||||
|
OptionsScreen(config)
|
||||||
|
}
|
||||||
|
composable<Route.Lock> {
|
||||||
|
PinLockScreen(viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.Scanner> {
|
||||||
|
ScannerScreen()
|
||||||
|
}
|
||||||
|
composable<Route.KillSwitch> {
|
||||||
|
KillSwitchScreen(appUiState, viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.SplitTunnel> {
|
||||||
|
val args = it.toRoute<Route.SplitTunnel>()
|
||||||
|
val config = appUiState.tunnels.first { it.id == args.id }
|
||||||
|
SplitTunnelScreen(config, viewModel)
|
||||||
|
}
|
||||||
|
composable<Route.TunnelAutoTunnel> {
|
||||||
|
val args = it.toRoute<Route.TunnelOptions>()
|
||||||
|
val config = appUiState.tunnels.first { it.id == args.id }
|
||||||
|
TunnelAutoTunnelScreen(config, appUiState.settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BackHandler(enabled = true) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (!navController.popBackStack()) {
|
||||||
|
this@MainActivity.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,24 +265,4 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun attachBaseContext(newBase: Context) {
|
|
||||||
oldPrefLocaleCode = LocaleStorage(newBase).getPreferredLocale()
|
|
||||||
applyOverrideConfiguration(LocaleUtil.getLocalizedConfiguration(oldPrefLocaleCode))
|
|
||||||
super.attachBaseContext(newBase)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
val currentLocaleCode = LocaleStorage(this).getPreferredLocale()
|
|
||||||
if (oldPrefLocaleCode != currentLocaleCode) {
|
|
||||||
recreate() // locale is changed, restart the activity to update
|
|
||||||
oldPrefLocaleCode = currentLocaleCode
|
|
||||||
}
|
|
||||||
super.onResume()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
tunnelService.cancelStatsJob()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ sealed class Route {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data object AutoTunnel : Route()
|
data object AutoTunnel : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data object AutoTunnelAdvanced : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data object LocationDisclosure : Route()
|
data object LocationDisclosure : Route()
|
||||||
|
|
||||||
@@ -21,6 +24,9 @@ sealed class Route {
|
|||||||
@Serializable
|
@Serializable
|
||||||
data object Display : Route()
|
data object Display : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data object KillSwitch : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data object Language : Route()
|
data object Language : Route()
|
||||||
|
|
||||||
@@ -28,7 +34,7 @@ sealed class Route {
|
|||||||
data object Main : Route()
|
data object Main : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Option(
|
data class TunnelOptions(
|
||||||
val id: Int,
|
val id: Int,
|
||||||
) : Route()
|
) : Route()
|
||||||
|
|
||||||
@@ -43,6 +49,16 @@ sealed class Route {
|
|||||||
val id: Int,
|
val id: Int,
|
||||||
) : Route()
|
) : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SplitTunnel(
|
||||||
|
val id: Int,
|
||||||
|
) : Route()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class TunnelAutoTunnel(
|
||||||
|
val id: Int,
|
||||||
|
) : Route()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data object Logs : Route()
|
data object Logs : Route()
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-1
@@ -55,7 +55,13 @@ fun ExpandingRowListItem(
|
|||||||
modifier = Modifier.fillMaxWidth(13 / 20f),
|
modifier = Modifier.fillMaxWidth(13 / 20f),
|
||||||
) {
|
) {
|
||||||
leading()
|
leading()
|
||||||
Text(text, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.labelLarge)
|
Text(
|
||||||
|
text,
|
||||||
|
maxLines = 1,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
style = MaterialTheme.typography.labelLarge,
|
||||||
|
color = MaterialTheme.colorScheme.onBackground,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
trailing()
|
trailing()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,36 +2,22 @@ package com.zaneschepke.wireguardautotunnel.ui.common
|
|||||||
|
|
||||||
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.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material.icons.outlined.CheckBox
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Icon
|
||||||
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.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
|
@Composable
|
||||||
fun SelectedLabel() {
|
fun SelectedLabel() {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
horizontalArrangement = Arrangement.End,
|
horizontalArrangement = Arrangement.End,
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
) {
|
) {
|
||||||
Text(
|
val icon = Icons.Outlined.CheckBox
|
||||||
stringResource(id = R.string.selected),
|
Icon(icon, icon.name)
|
||||||
modifier =
|
|
||||||
Modifier.padding(
|
|
||||||
horizontal = 24.dp.scaledWidth(),
|
|
||||||
vertical = 16.dp.scaledHeight(),
|
|
||||||
),
|
|
||||||
color =
|
|
||||||
MaterialTheme.colorScheme.onSurfaceVariant,
|
|
||||||
style = MaterialTheme.typography.labelSmall,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-1
@@ -25,7 +25,6 @@ import androidx.compose.ui.unit.dp
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
import kotlin.let
|
|
||||||
|
|
||||||
@androidx.compose.runtime.Composable
|
@androidx.compose.runtime.Composable
|
||||||
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) {
|
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) {
|
||||||
|
|||||||
+10
@@ -1,6 +1,8 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
package com.zaneschepke.wireguardautotunnel.ui.common.button
|
||||||
|
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Switch
|
import androidx.compose.material3.Switch
|
||||||
|
import androidx.compose.material3.SwitchDefaults
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.scale
|
import androidx.compose.ui.draw.scale
|
||||||
@@ -14,5 +16,13 @@ fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled:
|
|||||||
{ onClick(it) },
|
{ onClick(it) },
|
||||||
modifier.scale((52.dp.scaledHeight() / 52.dp)),
|
modifier.scale((52.dp.scaledHeight() / 52.dp)),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
|
colors = SwitchDefaults.colors().copy(
|
||||||
|
checkedThumbColor = MaterialTheme.colorScheme.background,
|
||||||
|
checkedIconColor = MaterialTheme.colorScheme.background,
|
||||||
|
uncheckedTrackColor = MaterialTheme.colorScheme.surface,
|
||||||
|
uncheckedBorderColor = MaterialTheme.colorScheme.outline,
|
||||||
|
uncheckedThumbColor = MaterialTheme.colorScheme.outline,
|
||||||
|
uncheckedIconColor = MaterialTheme.colorScheme.outline,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-3
@@ -5,7 +5,9 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
|
|||||||
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.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.Card
|
import androidx.compose.material3.Card
|
||||||
import androidx.compose.material3.CardDefaults
|
import androidx.compose.material3.CardDefaults
|
||||||
@@ -17,8 +19,10 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SelectionItemButton(
|
fun SelectionItemButton(
|
||||||
@@ -30,7 +34,8 @@ fun SelectionItemButton(
|
|||||||
) {
|
) {
|
||||||
Card(
|
Card(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier.clip(RoundedCornerShape(8.dp))
|
Modifier
|
||||||
|
.clip(RoundedCornerShape(8.dp))
|
||||||
.clickable(
|
.clickable(
|
||||||
indication = if (ripple) ripple() else null,
|
indication = if (ripple) ripple() else null,
|
||||||
interactionSource = remember { MutableInteractionSource() },
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
@@ -45,15 +50,20 @@ fun SelectionItemButton(
|
|||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.Start,
|
horizontalArrangement = Arrangement.Start,
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(end = 10.dp.scaledWidth()),
|
||||||
) {
|
) {
|
||||||
leading?.let {
|
leading?.let {
|
||||||
it()
|
it()
|
||||||
}
|
}
|
||||||
Text(
|
Text(
|
||||||
buttonText,
|
buttonText,
|
||||||
style = MaterialTheme.typography.bodyLarge,
|
style = MaterialTheme.typography.labelMedium,
|
||||||
color = MaterialTheme.colorScheme.onSurface,
|
color = MaterialTheme.colorScheme.onSurface,
|
||||||
|
modifier = Modifier.fillMaxWidth(3 / 4f),
|
||||||
|
maxLines = 2,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
)
|
)
|
||||||
trailing?.let {
|
trailing?.let {
|
||||||
it()
|
it()
|
||||||
|
|||||||
+2
-1
@@ -52,6 +52,7 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|||||||
icon,
|
icon,
|
||||||
icon.name,
|
icon.name,
|
||||||
modifier = Modifier.size(iconSize),
|
modifier = Modifier.size(iconSize),
|
||||||
|
tint = MaterialTheme.colorScheme.onSurface,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Column(
|
Column(
|
||||||
@@ -80,7 +81,7 @@ fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
|
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(0.30f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -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.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
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
|
||||||
@@ -23,19 +24,20 @@ fun ConfigurationTextBox(
|
|||||||
capitalization = KeyboardCapitalization.None,
|
capitalization = KeyboardCapitalization.None,
|
||||||
imeAction = ImeAction.Done,
|
imeAction = ImeAction.Done,
|
||||||
),
|
),
|
||||||
trailing: @Composable () -> Unit = {},
|
trailing: (@Composable () -> Unit)? = null,
|
||||||
interactionSource: MutableInteractionSource? = null,
|
interactionSource: MutableInteractionSource? = null,
|
||||||
) {
|
) {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
isError = isError,
|
isError = isError,
|
||||||
|
textStyle = MaterialTheme.typography.labelLarge,
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
value = value,
|
value = value,
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
onValueChange = { onValueChange(it) },
|
onValueChange = { onValueChange(it) },
|
||||||
label = { Text(label) },
|
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.labelMedium) },
|
||||||
maxLines = 1,
|
maxLines = 1,
|
||||||
placeholder = { Text(hint) },
|
placeholder = { Text(hint, color = MaterialTheme.colorScheme.outline, style = MaterialTheme.typography.labelLarge) },
|
||||||
keyboardOptions = keyboardOptions,
|
keyboardOptions = keyboardOptions,
|
||||||
keyboardActions = keyboardActions,
|
keyboardActions = keyboardActions,
|
||||||
trailingIcon = trailing,
|
trailingIcon = trailing,
|
||||||
|
|||||||
+3
-3
@@ -4,12 +4,12 @@ import androidx.compose.foundation.layout.Arrangement
|
|||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
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 com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfigurationToggle(
|
fun ConfigurationToggle(
|
||||||
@@ -38,11 +38,11 @@ fun ConfigurationToggle(
|
|||||||
),
|
),
|
||||||
softWrap = true,
|
softWrap = true,
|
||||||
)
|
)
|
||||||
Switch(
|
ScaledSwitch(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
checked = checked,
|
checked = checked,
|
||||||
onCheckedChange = { onCheckChanged(it) },
|
onClick = { onCheckChanged(it) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-13
@@ -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
|
||||||
@@ -20,10 +20,11 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
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(
|
||||||
@@ -44,17 +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(),
|
|
||||||
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 = {
|
||||||
@@ -62,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,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ fun GroupLabel(title: String) {
|
|||||||
Text(
|
Text(
|
||||||
title,
|
title,
|
||||||
style = MaterialTheme.typography.titleMedium,
|
style = MaterialTheme.typography.titleMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onBackground,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
@@ -4,12 +4,14 @@ import androidx.compose.material3.Icon
|
|||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.NavigationBar
|
import androidx.compose.material3.NavigationBar
|
||||||
import androidx.compose.material3.NavigationBarItem
|
import androidx.compose.material3.NavigationBarItem
|
||||||
|
import androidx.compose.material3.NavigationBarItemDefaults
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
@@ -57,6 +59,13 @@ fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavIte
|
|||||||
contentDescription = "${item.name} Icon",
|
contentDescription = "${item.name} Icon",
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
colors = NavigationBarItemDefaults.colors().copy(
|
||||||
|
selectedIndicatorColor = Color.Transparent,
|
||||||
|
selectedIconColor = MaterialTheme.colorScheme.primary,
|
||||||
|
selectedTextColor = MaterialTheme.colorScheme.primary,
|
||||||
|
unselectedTextColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
||||||
|
unselectedIconColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.55f),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-3
@@ -1,11 +1,8 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
package com.zaneschepke.wireguardautotunnel.ui.common.navigation
|
||||||
|
|
||||||
import androidx.compose.runtime.compositionLocalOf
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
|
|
||||||
val LocalNavController = compositionLocalOf<NavHostController> {
|
val LocalNavController = compositionLocalOf<NavHostController> {
|
||||||
error("NavController was not provided")
|
error("NavController was not provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
val LocalFocusRequester = compositionLocalOf<FocusRequester> { error("FocusRequester is not provided") }
|
|
||||||
|
|||||||
+3
-1
@@ -19,7 +19,9 @@ fun TopNavBar(title: String, trailing: @Composable () -> Unit = {}, showBack: Bo
|
|||||||
},
|
},
|
||||||
navigationIcon = {
|
navigationIcon = {
|
||||||
if (showBack) {
|
if (showBack) {
|
||||||
IconButton(onClick = { navController.popBackStack() }) {
|
IconButton(onClick = {
|
||||||
|
navController.popBackStack()
|
||||||
|
}) {
|
||||||
val icon = Icons.AutoMirrored.Outlined.ArrowBack
|
val icon = Icons.AutoMirrored.Outlined.ArrowBack
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = icon,
|
imageVector = icon,
|
||||||
|
|||||||
+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}")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-4
@@ -10,7 +10,6 @@ import androidx.compose.material3.MaterialTheme
|
|||||||
import androidx.compose.material3.OutlinedTextFieldDefaults
|
import androidx.compose.material3.OutlinedTextFieldDefaults
|
||||||
import androidx.compose.material3.TextFieldDefaults
|
import androidx.compose.material3.TextFieldDefaults
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.SolidColor
|
import androidx.compose.ui.graphics.SolidColor
|
||||||
@@ -29,16 +28,16 @@ fun CustomTextField(
|
|||||||
onValueChange: (value: String) -> Unit = {},
|
onValueChange: (value: String) -> Unit = {},
|
||||||
singleLine: Boolean = false,
|
singleLine: Boolean = false,
|
||||||
placeholder: @Composable (() -> Unit)? = null,
|
placeholder: @Composable (() -> Unit)? = null,
|
||||||
keyboardOptions: KeyboardOptions,
|
keyboardOptions: KeyboardOptions = KeyboardOptions(),
|
||||||
keyboardActions: KeyboardActions,
|
keyboardActions: KeyboardActions = KeyboardActions(),
|
||||||
supportingText: @Composable (() -> Unit)? = null,
|
supportingText: @Composable (() -> Unit)? = null,
|
||||||
leading: @Composable (() -> Unit)? = null,
|
leading: @Composable (() -> Unit)? = null,
|
||||||
trailing: @Composable (() -> Unit)? = null,
|
trailing: @Composable (() -> Unit)? = null,
|
||||||
isError: Boolean = false,
|
isError: Boolean = false,
|
||||||
readOnly: Boolean = false,
|
readOnly: Boolean = false,
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
|
interactionSource: MutableInteractionSource,
|
||||||
) {
|
) {
|
||||||
val interactionSource = remember { MutableInteractionSource() }
|
|
||||||
val space = " "
|
val space = " "
|
||||||
BasicTextField(
|
BasicTextField(
|
||||||
value = value,
|
value = value,
|
||||||
|
|||||||
-601
@@ -1,601 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.focusGroup
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.IntrinsicSize
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.width
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.ContentCopy
|
|
||||||
import androidx.compose.material.icons.rounded.Delete
|
|
||||||
import androidx.compose.material.icons.rounded.Refresh
|
|
||||||
import androidx.compose.material.icons.rounded.Save
|
|
||||||
import androidx.compose.material3.FabPosition
|
|
||||||
import androidx.compose.material3.FloatingActionButton
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.derivedStateOf
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.focus.focusRequester
|
|
||||||
import androidx.compose.ui.platform.ClipboardManager
|
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.AnnotatedString
|
|
||||||
import androidx.compose.ui.text.buildAnnotatedString
|
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
|
||||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
||||||
import androidx.compose.ui.text.input.VisualTransformation
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
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.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.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.components.ApplicationSelectionDialog
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.ConfigType
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ConfigScreen(tunnelId: Int) {
|
|
||||||
val viewModel = hiltViewModel<ConfigViewModel, ConfigViewModel.ConfigViewModelFactory> { factory ->
|
|
||||||
factory.create(tunnelId)
|
|
||||||
}
|
|
||||||
|
|
||||||
val context = LocalContext.current
|
|
||||||
val snackbar = SnackbarController.current
|
|
||||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
var showApplicationsDialog by remember { mutableStateOf(false) }
|
|
||||||
var showAuthPrompt by remember { mutableStateOf(false) }
|
|
||||||
var isAuthenticated by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
|
||||||
var configType by remember { mutableStateOf<ConfigType?>(null) }
|
|
||||||
val derivedConfigType = remember {
|
|
||||||
derivedStateOf<ConfigType> {
|
|
||||||
configType ?: if (!uiState.hasAmneziaProperties()) ConfigType.WIREGUARD else ConfigType.AMNEZIA
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val saved by viewModel.saved.collectAsStateWithLifecycle(null)
|
|
||||||
|
|
||||||
LaunchedEffect(saved) {
|
|
||||||
if (saved == true) {
|
|
||||||
navController.navigate(Route.Main)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
delay(2_000L)
|
|
||||||
viewModel.cleanUpUninstalledApps()
|
|
||||||
}
|
|
||||||
|
|
||||||
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
|
||||||
val keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done)
|
|
||||||
|
|
||||||
val fillMaxHeight = .85f
|
|
||||||
val fillMaxWidth = .85f
|
|
||||||
val screenPadding = 5.dp
|
|
||||||
|
|
||||||
val applicationButtonText =
|
|
||||||
buildAnnotatedString {
|
|
||||||
append(stringResource(id = R.string.tunneling_apps))
|
|
||||||
append(": ")
|
|
||||||
if (uiState.isAllApplicationsEnabled) {
|
|
||||||
append(stringResource(id = R.string.all))
|
|
||||||
} else {
|
|
||||||
append("${uiState.checkedPackageNames.size} ")
|
|
||||||
(
|
|
||||||
if (uiState.include) {
|
|
||||||
append(stringResource(id = R.string.included))
|
|
||||||
} else {
|
|
||||||
append(
|
|
||||||
stringResource(id = R.string.excluded),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showAuthPrompt) {
|
|
||||||
AuthorizationPrompt(
|
|
||||||
onSuccess = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
isAuthenticated = true
|
|
||||||
},
|
|
||||||
onError = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.error_authentication_failed),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onFailure = {
|
|
||||||
showAuthPrompt = false
|
|
||||||
snackbar.showMessage(
|
|
||||||
context.getString(R.string.error_authorization_failed),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showApplicationsDialog) {
|
|
||||||
ApplicationSelectionDialog(viewModel, uiState) {
|
|
||||||
showApplicationsDialog = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(stringResource(R.string.edit_tunnel))
|
|
||||||
},
|
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
|
||||||
floatingActionButton = {
|
|
||||||
FloatingActionButton(
|
|
||||||
onClick = {
|
|
||||||
viewModel.onSaveAllChanges()
|
|
||||||
},
|
|
||||||
containerColor = MaterialTheme.colorScheme.primary,
|
|
||||||
shape = RoundedCornerShape(16.dp),
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
imageVector = Icons.Rounded.Save,
|
|
||||||
contentDescription = stringResource(id = R.string.save_changes),
|
|
||||||
tint = MaterialTheme.colorScheme.background,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Column(Modifier.padding(it)) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.verticalScroll(rememberScrollState())
|
|
||||||
.weight(1f, true)
|
|
||||||
.fillMaxSize(),
|
|
||||||
) {
|
|
||||||
Surface(
|
|
||||||
tonalElevation = 2.dp,
|
|
||||||
shadowElevation = 2.dp,
|
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
color = MaterialTheme.colorScheme.surface,
|
|
||||||
modifier =
|
|
||||||
(
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
Modifier
|
|
||||||
.fillMaxHeight(fillMaxHeight)
|
|
||||||
.fillMaxWidth(fillMaxWidth)
|
|
||||||
} else {
|
|
||||||
Modifier.fillMaxWidth(fillMaxWidth)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.padding(bottom = 10.dp.scaledHeight()).padding(top = 24.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(15.dp)
|
|
||||||
.focusGroup(),
|
|
||||||
) {
|
|
||||||
SectionTitle(
|
|
||||||
stringResource(R.string.interface_),
|
|
||||||
padding = screenPadding,
|
|
||||||
)
|
|
||||||
ConfigurationToggle(
|
|
||||||
stringResource(id = R.string.show_amnezia_properties),
|
|
||||||
checked = derivedConfigType.value == ConfigType.AMNEZIA,
|
|
||||||
onCheckChanged = { configType = if (it) ConfigType.AMNEZIA else ConfigType.WIREGUARD },
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.tunnelName,
|
|
||||||
onValueChange = viewModel::onTunnelNameChange,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.name),
|
|
||||||
hint = stringResource(R.string.tunnel_name).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.clickable { showAuthPrompt = true },
|
|
||||||
value = uiState.interfaceProxy.privateKey,
|
|
||||||
visualTransformation =
|
|
||||||
if ((tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated) {
|
|
||||||
VisualTransformation.None
|
|
||||||
} else {
|
|
||||||
PasswordVisualTransformation()
|
|
||||||
},
|
|
||||||
enabled = (tunnelId == Constants.MANUAL_TUNNEL_CONFIG_ID.toInt()) || isAuthenticated,
|
|
||||||
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
|
||||||
trailingIcon = {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
|
||||||
onClick = { viewModel.generateKeyPair() },
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.Refresh,
|
|
||||||
stringResource(R.string.rotate_keys),
|
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = { Text(stringResource(R.string.private_key)) },
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = { Text(stringResource(R.string.base64_key)) },
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.focusRequester(FocusRequester.Default),
|
|
||||||
value = uiState.interfaceProxy.publicKey,
|
|
||||||
enabled = false,
|
|
||||||
onValueChange = {},
|
|
||||||
trailingIcon = {
|
|
||||||
IconButton(
|
|
||||||
modifier = Modifier.focusRequester(FocusRequester.Default),
|
|
||||||
onClick = {
|
|
||||||
clipboardManager.setText(
|
|
||||||
AnnotatedString(uiState.interfaceProxy.publicKey),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.ContentCopy,
|
|
||||||
stringResource(R.string.copy_public_key),
|
|
||||||
tint = MaterialTheme.colorScheme.onSurface,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
label = { Text(stringResource(R.string.public_key)) },
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = { Text(stringResource(R.string.base64_key)) },
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.addresses,
|
|
||||||
onValueChange = viewModel::onAddressesChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.addresses),
|
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(end = 5.dp),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.listenPort,
|
|
||||||
onValueChange = viewModel::onListenPortChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.listen_port),
|
|
||||||
hint = stringResource(R.string.random),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
Row(modifier = Modifier.fillMaxWidth()) {
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.dnsServers,
|
|
||||||
onValueChange = viewModel::onDnsServersChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.dns_servers),
|
|
||||||
hint = stringResource(R.string.comma_separated_list),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(3 / 5f)
|
|
||||||
.padding(end = 5.dp),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.mtu,
|
|
||||||
onValueChange = viewModel::onMtuChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.mtu),
|
|
||||||
hint = stringResource(R.string.auto),
|
|
||||||
modifier = Modifier.width(IntrinsicSize.Min),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (derivedConfigType.value == ConfigType.AMNEZIA) {
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.junkPacketCount,
|
|
||||||
onValueChange = viewModel::onJunkPacketCountChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_count),
|
|
||||||
hint = stringResource(R.string.junk_packet_count).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.junkPacketMinSize,
|
|
||||||
onValueChange = viewModel::onJunkPacketMinSizeChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_minimum_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.junk_packet_minimum_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.junkPacketMaxSize,
|
|
||||||
onValueChange = viewModel::onJunkPacketMaxSizeChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.junk_packet_maximum_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.junk_packet_maximum_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.initPacketJunkSize,
|
|
||||||
onValueChange = viewModel::onInitPacketJunkSizeChanged,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.init_packet_junk_size),
|
|
||||||
hint = stringResource(R.string.init_packet_junk_size).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.responsePacketJunkSize,
|
|
||||||
onValueChange = viewModel::onResponsePacketJunkSize,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.response_packet_junk_size),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.response_packet_junk_size,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.initPacketMagicHeader,
|
|
||||||
onValueChange = viewModel::onInitPacketMagicHeader,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.init_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.init_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.responsePacketMagicHeader,
|
|
||||||
onValueChange = viewModel::onResponsePacketMagicHeader,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.response_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.response_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.underloadPacketMagicHeader,
|
|
||||||
onValueChange = viewModel::onUnderloadPacketMagicHeader,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.underload_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.underload_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = uiState.interfaceProxy.transportPacketMagicHeader,
|
|
||||||
onValueChange = viewModel::onTransportPacketMagicHeader,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.transport_packet_magic_header),
|
|
||||||
hint =
|
|
||||||
stringResource(
|
|
||||||
R.string.transport_packet_magic_header,
|
|
||||||
).lowercase(),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(top = 5.dp),
|
|
||||||
horizontalArrangement = Arrangement.Center,
|
|
||||||
) {
|
|
||||||
TextButton(onClick = { showApplicationsDialog = true }) {
|
|
||||||
Text(applicationButtonText.text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
uiState.proxyPeers.forEachIndexed { index, peer ->
|
|
||||||
Surface(
|
|
||||||
tonalElevation = 2.dp,
|
|
||||||
shadowElevation = 2.dp,
|
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
color = MaterialTheme.colorScheme.surface,
|
|
||||||
modifier =
|
|
||||||
(
|
|
||||||
if (context.isRunningOnTv()) {
|
|
||||||
Modifier
|
|
||||||
.fillMaxHeight(fillMaxHeight)
|
|
||||||
.fillMaxWidth(fillMaxWidth)
|
|
||||||
} else {
|
|
||||||
Modifier.fillMaxWidth(fillMaxWidth)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.padding(top = 10.dp, bottom = 10.dp),
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.padding(horizontal = 15.dp)
|
|
||||||
.padding(bottom = 10.dp),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 5.dp),
|
|
||||||
) {
|
|
||||||
SectionTitle(
|
|
||||||
stringResource(R.string.peer),
|
|
||||||
padding = screenPadding,
|
|
||||||
)
|
|
||||||
IconButton(onClick = { viewModel.onDeletePeer(index) }) {
|
|
||||||
val icon = Icons.Rounded.Delete
|
|
||||||
Icon(icon, icon.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = peer.publicKey,
|
|
||||||
onValueChange = { value ->
|
|
||||||
viewModel.onPeerPublicKeyChange(index, value)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.public_key),
|
|
||||||
hint = stringResource(R.string.base64_key),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = peer.preSharedKey,
|
|
||||||
onValueChange = { value ->
|
|
||||||
viewModel.onPreSharedKeyChange(index, value)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.preshared_key),
|
|
||||||
hint = stringResource(R.string.optional),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = peer.persistentKeepalive,
|
|
||||||
enabled = true,
|
|
||||||
onValueChange = { value ->
|
|
||||||
viewModel.onPersistentKeepaliveChanged(index, value)
|
|
||||||
},
|
|
||||||
trailingIcon = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.seconds),
|
|
||||||
modifier = Modifier.padding(end = 10.dp),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
label = { Text(stringResource(R.string.persistent_keepalive)) },
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(stringResource(R.string.optional_no_recommend))
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
ConfigurationTextBox(
|
|
||||||
value = peer.endpoint,
|
|
||||||
onValueChange = { value ->
|
|
||||||
viewModel.onEndpointChange(index, value)
|
|
||||||
},
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
label = stringResource(R.string.endpoint),
|
|
||||||
hint = stringResource(R.string.endpoint).lowercase(),
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
)
|
|
||||||
OutlinedTextField(
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = peer.allowedIps,
|
|
||||||
enabled = true,
|
|
||||||
onValueChange = { value ->
|
|
||||||
viewModel.onAllowedIpsChange(index, value)
|
|
||||||
},
|
|
||||||
label = { Text(stringResource(R.string.allowed_ips)) },
|
|
||||||
singleLine = true,
|
|
||||||
placeholder = {
|
|
||||||
Text(stringResource(R.string.comma_separated_list))
|
|
||||||
},
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
horizontalArrangement = Arrangement.SpaceEvenly,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(bottom = 140.dp),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Center,
|
|
||||||
) {
|
|
||||||
TextButton(onClick = { viewModel.addEmptyPeer() }) {
|
|
||||||
Text(stringResource(R.string.add_peer))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-83
@@ -1,83 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
|
||||||
|
|
||||||
import com.wireguard.config.Config
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.InterfaceProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.Packages
|
|
||||||
|
|
||||||
data class ConfigUiState(
|
|
||||||
val proxyPeers: List<PeerProxy> = arrayListOf(PeerProxy()),
|
|
||||||
val interfaceProxy: InterfaceProxy = InterfaceProxy(),
|
|
||||||
val packages: Packages = emptyList(),
|
|
||||||
val checkedPackageNames: List<String> = emptyList(),
|
|
||||||
val include: Boolean = true,
|
|
||||||
val isAllApplicationsEnabled: Boolean = false,
|
|
||||||
val loading: Boolean = true,
|
|
||||||
val tunnel: TunnelConfig? = null,
|
|
||||||
var tunnelName: String = "",
|
|
||||||
val isAmneziaEnabled: Boolean = false,
|
|
||||||
) {
|
|
||||||
fun hasAmneziaProperties(): Boolean {
|
|
||||||
return this.interfaceProxy.junkPacketCount != ""
|
|
||||||
}
|
|
||||||
companion object {
|
|
||||||
fun from(config: Config): ConfigUiState {
|
|
||||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
|
||||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
|
||||||
var include = true
|
|
||||||
var isAllApplicationsEnabled = false
|
|
||||||
val checkedPackages =
|
|
||||||
if (config.`interface`.includedApplications.isNotEmpty()) {
|
|
||||||
config.`interface`.includedApplications
|
|
||||||
} else if (config.`interface`.excludedApplications.isNotEmpty()) {
|
|
||||||
include = false
|
|
||||||
config.`interface`.excludedApplications
|
|
||||||
} else {
|
|
||||||
isAllApplicationsEnabled = true
|
|
||||||
emptySet()
|
|
||||||
}
|
|
||||||
return ConfigUiState(
|
|
||||||
proxyPeers,
|
|
||||||
proxyInterface,
|
|
||||||
emptyList(),
|
|
||||||
checkedPackages.toList(),
|
|
||||||
include,
|
|
||||||
isAllApplicationsEnabled,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun from(config: org.amnezia.awg.config.Config): ConfigUiState {
|
|
||||||
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
|
||||||
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
|
||||||
var include = true
|
|
||||||
var isAllApplicationsEnabled = false
|
|
||||||
val checkedPackages =
|
|
||||||
if (config.`interface`.includedApplications.isNotEmpty()) {
|
|
||||||
config.`interface`.includedApplications
|
|
||||||
} else if (config.`interface`.excludedApplications.isNotEmpty()) {
|
|
||||||
include = false
|
|
||||||
config.`interface`.excludedApplications
|
|
||||||
} else {
|
|
||||||
isAllApplicationsEnabled = true
|
|
||||||
emptySet()
|
|
||||||
}
|
|
||||||
return ConfigUiState(
|
|
||||||
proxyPeers,
|
|
||||||
proxyInterface,
|
|
||||||
emptyList(),
|
|
||||||
checkedPackages.toList(),
|
|
||||||
include,
|
|
||||||
isAllApplicationsEnabled,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun from(tunnel: TunnelConfig): ConfigUiState {
|
|
||||||
val config = tunnel.toAmConfig()
|
|
||||||
return from(config).copy(
|
|
||||||
tunnelName = tunnel.name,
|
|
||||||
tunnel = tunnel,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-591
@@ -1,591 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
|
||||||
|
|
||||||
import android.Manifest
|
|
||||||
import android.content.pm.PackageInfo
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.os.Build
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.wireguard.config.Config
|
|
||||||
import com.wireguard.config.Interface
|
|
||||||
import com.wireguard.config.Peer
|
|
||||||
import com.wireguard.crypto.Key
|
|
||||||
import com.wireguard.crypto.KeyPair
|
|
||||||
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.module.IoDispatcher
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.model.PeerProxy
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.removeAt
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.update
|
|
||||||
import dagger.assisted.Assisted
|
|
||||||
import dagger.assisted.AssistedFactory
|
|
||||||
import dagger.assisted.AssistedInject
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.onStart
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.flow.update
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.plus
|
|
||||||
import timber.log.Timber
|
|
||||||
|
|
||||||
@HiltViewModel(assistedFactory = ConfigViewModel.ConfigViewModelFactory::class)
|
|
||||||
class ConfigViewModel
|
|
||||||
@AssistedInject
|
|
||||||
constructor(
|
|
||||||
private val appDataRepository: AppDataRepository,
|
|
||||||
@Assisted val id: Int,
|
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
) : ViewModel() {
|
|
||||||
private val packageManager = WireGuardAutoTunnel.instance.packageManager
|
|
||||||
|
|
||||||
private val _saved = MutableSharedFlow<Boolean>()
|
|
||||||
val saved = _saved.asSharedFlow()
|
|
||||||
|
|
||||||
private val _uiState = MutableStateFlow(ConfigUiState())
|
|
||||||
val uiState = _uiState.onStart {
|
|
||||||
appDataRepository.tunnels.getById(id)?.let {
|
|
||||||
val packages = getQueriedPackages()
|
|
||||||
_uiState.value = ConfigUiState.from(it).copy(
|
|
||||||
packages = packages,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}.stateIn(
|
|
||||||
viewModelScope + ioDispatcher,
|
|
||||||
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
|
||||||
ConfigUiState(),
|
|
||||||
)
|
|
||||||
|
|
||||||
fun onTunnelNameChange(name: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(tunnelName = name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onIncludeChange(include: Boolean) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(include = include)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cleanUpUninstalledApps() = viewModelScope.launch(ioDispatcher) {
|
|
||||||
uiState.value.tunnel?.let {
|
|
||||||
val config = it.toAmConfig()
|
|
||||||
val packages = getQueriedPackages()
|
|
||||||
val packageSet = packages.map { pack -> pack.packageName }.toSet()
|
|
||||||
val includedApps = config.`interface`.includedApplications.toMutableList()
|
|
||||||
val excludedApps = config.`interface`.excludedApplications.toMutableList()
|
|
||||||
if (includedApps.isEmpty() && excludedApps.isEmpty()) return@launch
|
|
||||||
if (includedApps.retainAll(packageSet) || excludedApps.retainAll(packageSet)) {
|
|
||||||
Timber.i("Removing split tunnel package name that no longer exists on the device")
|
|
||||||
_uiState.update { state ->
|
|
||||||
state.copy(
|
|
||||||
checkedPackageNames = if (_uiState.value.include) includedApps else excludedApps,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val wgQuick = buildConfig().toWgQuickString(true)
|
|
||||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
|
||||||
saveConfig(
|
|
||||||
it.copy(
|
|
||||||
amQuick = amQuick,
|
|
||||||
wgQuick = wgQuick,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAddCheckedPackage(packageName: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
checkedPackageNames = it.checkedPackageNames + packageName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onRemoveCheckedPackage(packageName: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
checkedPackageNames = it.checkedPackageNames - packageName,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getQueriedPackages(query: String = ""): List<PackageInfo> {
|
|
||||||
return getAllInternetCapablePackages().filter {
|
|
||||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getPackageLabel(packageInfo: PackageInfo): String {
|
|
||||||
return packageInfo.applicationInfo?.loadLabel(packageManager).toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getAllInternetCapablePackages(): List<PackageInfo> {
|
|
||||||
return getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getPackagesHoldingPermissions(permissions: Array<String>): List<PackageInfo> {
|
|
||||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
packageManager.getPackagesHoldingPermissions(
|
|
||||||
permissions,
|
|
||||||
PackageManager.PackageInfoFlags.of(0L),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
packageManager.getPackagesHoldingPermissions(permissions, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isAllApplicationsEnabled(): Boolean {
|
|
||||||
return _uiState.value.isAllApplicationsEnabled
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveConfig(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
|
||||||
appDataRepository.tunnels.save(tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) = viewModelScope.launch {
|
|
||||||
if (tunnelConfig != null) {
|
|
||||||
saveConfig(tunnelConfig).join()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildPeerListFromProxyPeers(): List<Peer> {
|
|
||||||
return _uiState.value.proxyPeers.map {
|
|
||||||
val builder = Peer.Builder()
|
|
||||||
if (it.allowedIps.isNotEmpty()) builder.parseAllowedIPs(it.allowedIps.trim())
|
|
||||||
if (it.publicKey.isNotEmpty()) builder.parsePublicKey(it.publicKey.trim())
|
|
||||||
if (it.preSharedKey.isNotEmpty()) builder.parsePreSharedKey(it.preSharedKey.trim())
|
|
||||||
if (it.endpoint.isNotEmpty()) builder.parseEndpoint(it.endpoint.trim())
|
|
||||||
if (it.persistentKeepalive.isNotEmpty()) {
|
|
||||||
builder.parsePersistentKeepalive(it.persistentKeepalive.trim())
|
|
||||||
}
|
|
||||||
builder.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildAmPeerListFromProxyPeers(): List<org.amnezia.awg.config.Peer> {
|
|
||||||
return _uiState.value.proxyPeers.map {
|
|
||||||
val builder = org.amnezia.awg.config.Peer.Builder()
|
|
||||||
if (it.allowedIps.isNotEmpty()) builder.parseAllowedIPs(it.allowedIps.trim())
|
|
||||||
if (it.publicKey.isNotEmpty()) builder.parsePublicKey(it.publicKey.trim())
|
|
||||||
if (it.preSharedKey.isNotEmpty()) builder.parsePreSharedKey(it.preSharedKey.trim())
|
|
||||||
if (it.endpoint.isNotEmpty()) builder.parseEndpoint(it.endpoint.trim())
|
|
||||||
if (it.persistentKeepalive.isNotEmpty()) {
|
|
||||||
builder.parsePersistentKeepalive(it.persistentKeepalive.trim())
|
|
||||||
}
|
|
||||||
builder.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emptyCheckedPackagesList() {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(checkedPackageNames = emptyList())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildInterfaceListFromProxyInterface(): Interface {
|
|
||||||
val builder = Interface.Builder()
|
|
||||||
with(_uiState.value.interfaceProxy) {
|
|
||||||
builder.parsePrivateKey(this.privateKey.trim())
|
|
||||||
builder.parseAddresses(this.addresses.trim())
|
|
||||||
if (this.dnsServers.isNotEmpty()) {
|
|
||||||
builder.parseDnsServers(this.dnsServers.trim())
|
|
||||||
}
|
|
||||||
if (this.mtu.isNotEmpty()) {
|
|
||||||
builder.parseMtu(this.mtu.trim())
|
|
||||||
}
|
|
||||||
if (this.listenPort.isNotEmpty()) {
|
|
||||||
builder.parseListenPort(this.listenPort.trim())
|
|
||||||
}
|
|
||||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
|
||||||
if (_uiState.value.include) {
|
|
||||||
builder.includeApplications(
|
|
||||||
_uiState.value.checkedPackageNames,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!_uiState.value.include) {
|
|
||||||
builder.excludeApplications(
|
|
||||||
_uiState.value.checkedPackageNames,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildAmInterfaceListFromProxyInterface(): org.amnezia.awg.config.Interface {
|
|
||||||
val builder = org.amnezia.awg.config.Interface.Builder()
|
|
||||||
with(_uiState.value.interfaceProxy) {
|
|
||||||
builder.parsePrivateKey(this.privateKey.trim())
|
|
||||||
builder.parseAddresses(this.addresses.trim())
|
|
||||||
if (this.dnsServers.isNotEmpty()) {
|
|
||||||
builder.parseDnsServers(this.dnsServers.trim())
|
|
||||||
}
|
|
||||||
if (this.mtu.isNotEmpty()) {
|
|
||||||
builder.parseMtu(this.mtu.trim())
|
|
||||||
}
|
|
||||||
if (this.listenPort.isNotEmpty()) {
|
|
||||||
builder.parseListenPort(this.listenPort.trim())
|
|
||||||
}
|
|
||||||
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
|
||||||
if (_uiState.value.include) {
|
|
||||||
builder.includeApplications(
|
|
||||||
_uiState.value.checkedPackageNames,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!_uiState.value.include) {
|
|
||||||
builder.excludeApplications(
|
|
||||||
_uiState.value.checkedPackageNames,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.junkPacketCount.isNotEmpty()) {
|
|
||||||
builder.setJunkPacketCount(
|
|
||||||
this.junkPacketCount.trim().toInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.junkPacketMinSize.isNotEmpty()) {
|
|
||||||
builder.setJunkPacketMinSize(
|
|
||||||
this.junkPacketMinSize.trim().toInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.junkPacketMaxSize.isNotEmpty()) {
|
|
||||||
builder.setJunkPacketMaxSize(
|
|
||||||
this.junkPacketMaxSize.trim().toInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.initPacketJunkSize.isNotEmpty()) {
|
|
||||||
builder.setInitPacketJunkSize(
|
|
||||||
this.initPacketJunkSize.trim().toInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.responsePacketJunkSize.isNotEmpty()) {
|
|
||||||
builder.setResponsePacketJunkSize(
|
|
||||||
this.responsePacketJunkSize.trim().toInt(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.initPacketMagicHeader.isNotEmpty()) {
|
|
||||||
builder.setInitPacketMagicHeader(
|
|
||||||
this.initPacketMagicHeader.trim().toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.responsePacketMagicHeader.isNotEmpty()) {
|
|
||||||
builder.setResponsePacketMagicHeader(
|
|
||||||
this.responsePacketMagicHeader.trim().toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.transportPacketMagicHeader.isNotEmpty()) {
|
|
||||||
builder.setTransportPacketMagicHeader(
|
|
||||||
this.transportPacketMagicHeader.trim().toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (this.underloadPacketMagicHeader.isNotEmpty()) {
|
|
||||||
builder.setUnderloadPacketMagicHeader(
|
|
||||||
this.underloadPacketMagicHeader.trim().toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildConfig(): Config {
|
|
||||||
val peerList = buildPeerListFromProxyPeers()
|
|
||||||
val wgInterface = buildInterfaceListFromProxyInterface()
|
|
||||||
return Config.Builder().addPeers(peerList).setInterface(wgInterface).build()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildAmConfig(): org.amnezia.awg.config.Config {
|
|
||||||
val peerList = buildAmPeerListFromProxyPeers()
|
|
||||||
val amInterface = buildAmInterfaceListFromProxyInterface()
|
|
||||||
return org.amnezia.awg.config.Config.Builder().addPeers(
|
|
||||||
peerList,
|
|
||||||
).setInterface(amInterface)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onSaveAllChanges() = viewModelScope.launch {
|
|
||||||
kotlin.runCatching {
|
|
||||||
val wgQuick = buildConfig().toWgQuickString(true)
|
|
||||||
val amQuick = buildAmConfig().toAwgQuickString(true)
|
|
||||||
val tunnelConfig = uiState.value.tunnel?.copy(
|
|
||||||
name = _uiState.value.tunnelName,
|
|
||||||
amQuick = amQuick,
|
|
||||||
wgQuick = wgQuick,
|
|
||||||
) ?: TunnelConfig(
|
|
||||||
name = _uiState.value.tunnelName,
|
|
||||||
wgQuick = wgQuick,
|
|
||||||
amQuick = amQuick,
|
|
||||||
)
|
|
||||||
updateTunnelConfig(tunnelConfig)
|
|
||||||
SnackbarController.showMessage(
|
|
||||||
StringValue.StringResource(R.string.config_changes_saved),
|
|
||||||
)
|
|
||||||
_saved.emit(true)
|
|
||||||
}.onFailure {
|
|
||||||
Timber.e(it)
|
|
||||||
val message = it.message?.substringAfter(":", missingDelimiterValue = "")
|
|
||||||
val stringValue = if (message.isNullOrBlank()) {
|
|
||||||
StringValue.StringResource(R.string.unknown_error)
|
|
||||||
} else {
|
|
||||||
StringValue.DynamicString(message)
|
|
||||||
}
|
|
||||||
SnackbarController.showMessage(stringValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onPeerPublicKeyChange(index: Int, value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers =
|
|
||||||
_uiState.value.proxyPeers.update(
|
|
||||||
index,
|
|
||||||
_uiState.value.proxyPeers[index].copy(publicKey = value),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onPreSharedKeyChange(index: Int, value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers =
|
|
||||||
_uiState.value.proxyPeers.update(
|
|
||||||
index,
|
|
||||||
_uiState.value.proxyPeers[index].copy(preSharedKey = value),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onEndpointChange(index: Int, value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers =
|
|
||||||
_uiState.value.proxyPeers.update(
|
|
||||||
index,
|
|
||||||
_uiState.value.proxyPeers[index].copy(endpoint = value),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAllowedIpsChange(index: Int, value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers =
|
|
||||||
_uiState.value.proxyPeers.update(
|
|
||||||
index,
|
|
||||||
_uiState.value.proxyPeers[index].copy(allowedIps = value),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onPersistentKeepaliveChanged(index: Int, value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers =
|
|
||||||
_uiState.value.proxyPeers.update(
|
|
||||||
index,
|
|
||||||
_uiState.value.proxyPeers[index].copy(persistentKeepalive = value),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onDeletePeer(index: Int) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
proxyPeers = _uiState.value.proxyPeers.removeAt(index),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addEmptyPeer() {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(proxyPeers = _uiState.value.proxyPeers + PeerProxy())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun generateKeyPair() {
|
|
||||||
val keyPair = KeyPair()
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
privateKey = keyPair.privateKey.toBase64(),
|
|
||||||
publicKey = keyPair.publicKey.toBase64(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAddressesChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(addresses = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onListenPortChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(listenPort = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onDnsServersChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(dnsServers = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onMtuChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(interfaceProxy = it.interfaceProxy.copy(mtu = value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onInterfacePublicKeyChange(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(publicKey = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onPrivateKeyChange(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(privateKey = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (NumberUtils.isValidKey(value)) {
|
|
||||||
val pair = KeyPair(Key.fromBase64(value))
|
|
||||||
onInterfacePublicKeyChange(pair.publicKey.toBase64())
|
|
||||||
} else {
|
|
||||||
onInterfacePublicKeyChange("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun emitQueriedPackages(query: String) {
|
|
||||||
val packages =
|
|
||||||
getAllInternetCapablePackages().filter {
|
|
||||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
|
||||||
}
|
|
||||||
_uiState.update { it.copy(packages = packages) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onJunkPacketCountChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketCount = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onJunkPacketMinSizeChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMinSize = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onJunkPacketMaxSizeChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(junkPacketMaxSize = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onInitPacketJunkSizeChanged(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy = it.interfaceProxy.copy(initPacketJunkSize = value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onResponsePacketJunkSize(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
responsePacketJunkSize = value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onInitPacketMagicHeader(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
initPacketMagicHeader = value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onResponsePacketMagicHeader(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
responsePacketMagicHeader = value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTransportPacketMagicHeader(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
transportPacketMagicHeader = value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onUnderloadPacketMagicHeader(value: String) {
|
|
||||||
_uiState.update {
|
|
||||||
it.copy(
|
|
||||||
interfaceProxy =
|
|
||||||
it.interfaceProxy.copy(
|
|
||||||
underloadPacketMagicHeader = value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@AssistedFactory
|
|
||||||
interface ConfigViewModelFactory {
|
|
||||||
fun create(id: Int): ConfigViewModel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-199
@@ -1,199 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.components
|
|
||||||
|
|
||||||
import android.content.pm.PackageInfo
|
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.Spacer
|
|
||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Android
|
|
||||||
import androidx.compose.material3.BasicAlertDialog
|
|
||||||
import androidx.compose.material3.Checkbox
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
import androidx.compose.material3.Switch
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.google.accompanist.drawablepainter.DrawablePainter
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigViewModel
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun ApplicationSelectionDialog(viewModel: ConfigViewModel, uiState: ConfigUiState, onDismiss: () -> Unit) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val licenseComparator = compareBy<PackageInfo> { viewModel.getPackageLabel(it) }
|
|
||||||
|
|
||||||
val sortedPackages = remember(uiState.packages, licenseComparator) {
|
|
||||||
uiState.packages.sortedWith(licenseComparator)
|
|
||||||
}
|
|
||||||
BasicAlertDialog(
|
|
||||||
onDismissRequest = { onDismiss() },
|
|
||||||
) {
|
|
||||||
Surface(
|
|
||||||
tonalElevation = 2.dp,
|
|
||||||
shadowElevation = 2.dp,
|
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
color = MaterialTheme.colorScheme.surface,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.fillMaxHeight(if (uiState.isAllApplicationsEnabled) 1 / 5f else 4 / 5f),
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
Text(stringResource(id = R.string.tunnel_all))
|
|
||||||
Switch(
|
|
||||||
checked = uiState.isAllApplicationsEnabled,
|
|
||||||
onCheckedChange = viewModel::onAllApplicationsChange,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!uiState.isAllApplicationsEnabled) {
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
Text(stringResource(id = R.string.include))
|
|
||||||
Checkbox(
|
|
||||||
checked = uiState.include,
|
|
||||||
onCheckedChange = {
|
|
||||||
viewModel.onIncludeChange(!uiState.include)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
Text(stringResource(id = R.string.exclude))
|
|
||||||
Checkbox(
|
|
||||||
checked = !uiState.include,
|
|
||||||
onCheckedChange = {
|
|
||||||
viewModel.onIncludeChange(!uiState.include)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
|
||||||
SearchBar(viewModel::emitQueriedPackages)
|
|
||||||
}
|
|
||||||
Spacer(Modifier.padding(5.dp))
|
|
||||||
LazyColumn(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier = Modifier.fillMaxHeight(19 / 22f),
|
|
||||||
) {
|
|
||||||
items(sortedPackages, key = { it.packageName }) { pack ->
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(5.dp).padding(end = 25.dp),
|
|
||||||
) {
|
|
||||||
Row(modifier = Modifier.fillMaxWidth().padding(start = 5.dp)) {
|
|
||||||
val drawable =
|
|
||||||
pack.applicationInfo?.loadIcon(context.packageManager)
|
|
||||||
val iconSize = 35.dp
|
|
||||||
if (drawable != null) {
|
|
||||||
Image(
|
|
||||||
painter = DrawablePainter(drawable),
|
|
||||||
stringResource(id = R.string.icon),
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
val icon = Icons.Rounded.Android
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
viewModel.getPackageLabel(pack),
|
|
||||||
modifier = Modifier.padding(5.dp),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Checkbox(
|
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
checked =
|
|
||||||
(
|
|
||||||
uiState.checkedPackageNames.contains(
|
|
||||||
pack.packageName,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
onCheckedChange = {
|
|
||||||
if (it) {
|
|
||||||
viewModel.onAddCheckedPackage(pack.packageName)
|
|
||||||
} else {
|
|
||||||
viewModel.onRemoveCheckedPackage(pack.packageName)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(top = 5.dp),
|
|
||||||
horizontalArrangement = Arrangement.Center,
|
|
||||||
) {
|
|
||||||
TextButton(onClick = { onDismiss() }) {
|
|
||||||
Text(stringResource(R.string.done))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-121
@@ -1,121 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config.model
|
|
||||||
|
|
||||||
import com.wireguard.config.Interface
|
|
||||||
|
|
||||||
data class InterfaceProxy(
|
|
||||||
val privateKey: String = "",
|
|
||||||
val publicKey: String = "",
|
|
||||||
val addresses: String = "",
|
|
||||||
val dnsServers: String = "",
|
|
||||||
val listenPort: String = "",
|
|
||||||
val mtu: String = "",
|
|
||||||
val junkPacketCount: String = "",
|
|
||||||
val junkPacketMinSize: String = "",
|
|
||||||
val junkPacketMaxSize: String = "",
|
|
||||||
val initPacketJunkSize: String = "",
|
|
||||||
val responsePacketJunkSize: String = "",
|
|
||||||
val initPacketMagicHeader: String = "",
|
|
||||||
val responsePacketMagicHeader: String = "",
|
|
||||||
val underloadPacketMagicHeader: String = "",
|
|
||||||
val transportPacketMagicHeader: String = "",
|
|
||||||
) {
|
|
||||||
companion object {
|
|
||||||
fun from(i: Interface): InterfaceProxy {
|
|
||||||
return InterfaceProxy(
|
|
||||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
|
||||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
|
||||||
addresses = i.addresses.joinToString(", ").trim(),
|
|
||||||
dnsServers = listOf(
|
|
||||||
i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
|
||||||
i.dnsSearchDomains.joinToString(", ").trim(),
|
|
||||||
).filter { it.length > 0 }.joinToString(", "),
|
|
||||||
listenPort =
|
|
||||||
if (i.listenPort.isPresent) {
|
|
||||||
i.listenPort.get().toString().trim()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun from(i: org.amnezia.awg.config.Interface): InterfaceProxy {
|
|
||||||
return InterfaceProxy(
|
|
||||||
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
|
||||||
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
|
||||||
addresses = i.addresses.joinToString(", ").trim(),
|
|
||||||
dnsServers = (i.dnsServers + i.dnsSearchDomains).joinToString(", ").replace("/", "").trim(),
|
|
||||||
listenPort =
|
|
||||||
if (i.listenPort.isPresent) {
|
|
||||||
i.listenPort.get().toString().trim()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else "",
|
|
||||||
junkPacketCount =
|
|
||||||
if (i.junkPacketCount.isPresent) {
|
|
||||||
i.junkPacketCount.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
junkPacketMinSize =
|
|
||||||
if (i.junkPacketMinSize.isPresent) {
|
|
||||||
i.junkPacketMinSize.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
junkPacketMaxSize =
|
|
||||||
if (i.junkPacketMaxSize.isPresent) {
|
|
||||||
i.junkPacketMaxSize.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
initPacketJunkSize =
|
|
||||||
if (i.initPacketJunkSize.isPresent) {
|
|
||||||
i.initPacketJunkSize.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
responsePacketJunkSize =
|
|
||||||
if (i.responsePacketJunkSize.isPresent) {
|
|
||||||
i.responsePacketJunkSize.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
initPacketMagicHeader =
|
|
||||||
if (i.initPacketMagicHeader.isPresent) {
|
|
||||||
i.initPacketMagicHeader.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
responsePacketMagicHeader =
|
|
||||||
if (i.responsePacketMagicHeader.isPresent) {
|
|
||||||
i.responsePacketMagicHeader.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
transportPacketMagicHeader =
|
|
||||||
if (i.transportPacketMagicHeader.isPresent) {
|
|
||||||
i.transportPacketMagicHeader.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
underloadPacketMagicHeader =
|
|
||||||
if (i.underloadPacketMagicHeader.isPresent) {
|
|
||||||
i.underloadPacketMagicHeader.get()
|
|
||||||
.toString()
|
|
||||||
} else {
|
|
||||||
""
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+47
-66
@@ -1,13 +1,7 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import android.net.VpnService
|
|
||||||
import android.provider.Settings
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.ActivityResult
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.gestures.ScrollableDefaults
|
import androidx.compose.foundation.gestures.ScrollableDefaults
|
||||||
import androidx.compose.foundation.gestures.detectTapGestures
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
@@ -37,6 +31,7 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
import androidx.compose.ui.input.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.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
@@ -50,51 +45,58 @@ import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.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.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.TunnelRowItem
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelRowItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.VpnDeniedDialog
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isBatteryOptimizationsDisabled
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
|
import java.text.Collator
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) {
|
fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val navController = LocalNavController.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) }
|
var isFabVisible by 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 isRunningOnTv = remember { context.isRunningOnTv() }
|
||||||
|
|
||||||
|
val collator = Collator.getInstance(Locale.getDefault())
|
||||||
|
|
||||||
|
val sortedTunnels = remember(uiState.tunnels) {
|
||||||
|
uiState.tunnels.sortedWith(compareBy(collator) { it.name })
|
||||||
|
}
|
||||||
|
|
||||||
|
val startAutoTunnel = withVpnPermission<Unit> { viewModel.onToggleAutoTunnel() }
|
||||||
|
val startTunnel = withVpnPermission<TunnelConfig> {
|
||||||
|
viewModel.onTunnelStart(it)
|
||||||
|
}
|
||||||
|
val autoTunnelToggleBattery = withIgnoreBatteryOpt(uiState.generalState.isBatteryOptimizationDisableShown) {
|
||||||
|
if (!uiState.generalState.isBatteryOptimizationDisableShown) viewModel.setBatteryOptimizeDisableShown()
|
||||||
|
if (uiState.settings.isKernelEnabled) {
|
||||||
|
viewModel.onToggleAutoTunnel()
|
||||||
|
} else {
|
||||||
|
startAutoTunnel.invoke(Unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val nestedScrollConnection = remember {
|
val nestedScrollConnection = remember {
|
||||||
NestedScrollListener({ isFabVisible = false }, { isFabVisible = true })
|
NestedScrollListener({ isFabVisible = false }, { isFabVisible = true })
|
||||||
}
|
}
|
||||||
|
|
||||||
val vpnActivity =
|
|
||||||
rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
onResult = {
|
|
||||||
if (it.resultCode != RESULT_OK) showVpnPermissionDialog = true
|
|
||||||
},
|
|
||||||
)
|
|
||||||
val batteryActivity =
|
|
||||||
rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
) { result: ActivityResult ->
|
|
||||||
viewModel.setBatteryOptimizeDisableShown()
|
|
||||||
}
|
|
||||||
|
|
||||||
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
val tunnelFileImportResultLauncher = rememberFileImportLauncherForResult(onNoFileExplorer = {
|
||||||
snackbar.showMessage(
|
snackbar.showMessage(
|
||||||
context.getString(R.string.error_no_file_explorer),
|
context.getString(R.string.error_no_file_explorer),
|
||||||
@@ -110,13 +112,11 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
navController.navigate(Route.Scanner)
|
navController.navigate(Route.Scanner)
|
||||||
}
|
}
|
||||||
|
|
||||||
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false })
|
|
||||||
|
|
||||||
if (showDeleteTunnelAlertDialog) {
|
if (showDeleteTunnelAlertDialog) {
|
||||||
InfoDialog(
|
InfoDialog(
|
||||||
onDismiss = { showDeleteTunnelAlertDialog = false },
|
onDismiss = { showDeleteTunnelAlertDialog = false },
|
||||||
onAttest = {
|
onAttest = {
|
||||||
selectedTunnel?.let { viewModel::onDelete }
|
selectedTunnel?.let { viewModel.onDelete(it) }
|
||||||
showDeleteTunnelAlertDialog = false
|
showDeleteTunnelAlertDialog = false
|
||||||
selectedTunnel = null
|
selectedTunnel = null
|
||||||
},
|
},
|
||||||
@@ -126,35 +126,13 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestBatteryOptimizationsDisabled() {
|
|
||||||
val intent =
|
|
||||||
Intent().apply {
|
|
||||||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
|
||||||
data = Uri.parse("package:${context.packageName}")
|
|
||||||
}
|
|
||||||
batteryActivity.launch(intent)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAutoTunnelToggle() {
|
|
||||||
if (!uiState.generalState.isBatteryOptimizationDisableShown &&
|
|
||||||
!context.isBatteryOptimizationsDisabled() && !isRunningOnTv
|
|
||||||
) {
|
|
||||||
return requestBatteryOptimizationsDisabled()
|
|
||||||
}
|
|
||||||
val intent = if (!uiState.settings.isKernelEnabled) {
|
|
||||||
VpnService.prepare(context)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (intent != null) return vpnActivity.launch(intent)
|
|
||||||
viewModel.onToggleAutoTunnel()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
||||||
val intent = if (uiState.settings.isKernelEnabled) null else VpnService.prepare(context)
|
if (!checked) viewModel.onTunnelStop().also { return }
|
||||||
if (intent != null) return vpnActivity.launch(intent)
|
if (uiState.settings.isKernelEnabled) {
|
||||||
if (!checked) viewModel.onTunnelStop(tunnel).also { return }
|
viewModel.onTunnelStart(tunnel)
|
||||||
viewModel.onTunnelStart(tunnel, uiState.settings.isKernelEnabled)
|
} else {
|
||||||
|
startTunnel.invoke(tunnel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
@@ -201,12 +179,17 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) {
|
) { padding ->
|
||||||
TunnelImportSheet(
|
TunnelImportSheet(
|
||||||
showBottomSheet,
|
showBottomSheet,
|
||||||
onDismiss = { showBottomSheet = false },
|
onDismiss = { showBottomSheet = false },
|
||||||
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
onFileClick = { tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES) },
|
||||||
onQrClick = { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) },
|
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),
|
||||||
@@ -215,10 +198,10 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
)
|
)
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
verticalArrangement = Arrangement.spacedBy(5.dp.scaledHeight(), Alignment.Top),
|
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.Top),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxSize().padding(it)
|
.fillMaxSize().padding(padding).padding(top = 24.dp.scaledHeight())
|
||||||
.overscroll(ScrollableDefaults.overscrollEffect())
|
.overscroll(ScrollableDefaults.overscrollEffect())
|
||||||
.nestedScroll(nestedScrollConnection),
|
.nestedScroll(nestedScrollConnection),
|
||||||
state = rememberLazyListState(0, uiState.tunnels.count()),
|
state = rememberLazyListState(0, uiState.tunnels.count()),
|
||||||
@@ -232,22 +215,20 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
item {
|
item {
|
||||||
AutoTunnelRowItem(uiState, {
|
AutoTunnelRowItem(uiState) {
|
||||||
onAutoTunnelToggle()
|
autoTunnelToggleBattery.invoke()
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
items(
|
items(
|
||||||
uiState.tunnels,
|
sortedTunnels,
|
||||||
key = { tunnel -> tunnel.id },
|
key = { tunnel -> tunnel.id },
|
||||||
) { tunnel ->
|
) { tunnel ->
|
||||||
val isActive = uiState.tunnels.any {
|
|
||||||
it.id == tunnel.id &&
|
|
||||||
it.isActive
|
|
||||||
}
|
|
||||||
val expanded = uiState.generalState.isTunnelStatsExpanded
|
val expanded = uiState.generalState.isTunnelStatsExpanded
|
||||||
TunnelRowItem(
|
TunnelRowItem(
|
||||||
isActive,
|
tunnel.id == uiState.vpnState.tunnelConfig?.id && (
|
||||||
|
uiState.vpnState.status.isUp() || (uiState.settings.isKernelEnabled && tunnel.isActive)
|
||||||
|
),
|
||||||
expanded,
|
expanded,
|
||||||
selectedTunnel?.id == tunnel.id,
|
selectedTunnel?.id == tunnel.id,
|
||||||
tunnel,
|
tunnel,
|
||||||
|
|||||||
+16
-37
@@ -67,14 +67,14 @@ constructor(
|
|||||||
appDataRepository.appState.setTunnelStatsExpanded(expanded)
|
appDataRepository.appState.setTunnelStatsExpanded(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelStart(tunnelConfig: TunnelConfig, background: Boolean) = viewModelScope.launch {
|
fun onTunnelStart(tunnelConfig: TunnelConfig) = viewModelScope.launch {
|
||||||
Timber.i("Starting tunnel ${tunnelConfig.name}")
|
Timber.i("Starting tunnel ${tunnelConfig.name}")
|
||||||
tunnelService.get().startTunnel(tunnelConfig, background)
|
tunnelService.get().startTunnel(tunnelConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onTunnelStop(tunnel: TunnelConfig) = viewModelScope.launch {
|
fun onTunnelStop() = viewModelScope.launch {
|
||||||
Timber.i("Stopping active tunnel")
|
Timber.i("Stopping active tunnel")
|
||||||
tunnelService.get().stopTunnel(tunnel)
|
tunnelService.get().stopTunnel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCodeDefaultName(config: String): String {
|
private fun generateQrCodeDefaultName(config: String): String {
|
||||||
@@ -86,20 +86,6 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateQrCodeTunnelName(config: String): String {
|
|
||||||
var defaultName = generateQrCodeDefaultName(config)
|
|
||||||
val lines = config.lines().toMutableList()
|
|
||||||
val linesIterator = lines.iterator()
|
|
||||||
while (linesIterator.hasNext()) {
|
|
||||||
val next = linesIterator.next()
|
|
||||||
if (next.contains(Constants.QR_CODE_NAME_PROPERTY)) {
|
|
||||||
defaultName = next.substringAfter(Constants.QR_CODE_NAME_PROPERTY).trim()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultName
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
||||||
@@ -159,18 +145,7 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun onToggleAutoTunnel() = viewModelScope.launch {
|
fun onToggleAutoTunnel() = viewModelScope.launch {
|
||||||
val settings = appDataRepository.settings.getSettings()
|
serviceManager.toggleAutoTunnel(false)
|
||||||
val toggled = !settings.isAutoTunnelEnabled
|
|
||||||
if (toggled) {
|
|
||||||
serviceManager.startAutoTunnel(false)
|
|
||||||
} else {
|
|
||||||
serviceManager.stopAutoTunnel()
|
|
||||||
}
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
settings.copy(
|
|
||||||
isAutoTunnelEnabled = toggled,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
private suspend fun saveTunnelsFromZipUri(uri: Uri, context: Context) {
|
||||||
@@ -252,13 +227,17 @@ constructor(
|
|||||||
|
|
||||||
fun onCopyTunnel(tunnel: TunnelConfig) = viewModelScope.launch {
|
fun onCopyTunnel(tunnel: TunnelConfig) = viewModelScope.launch {
|
||||||
saveTunnel(
|
saveTunnel(
|
||||||
tunnel.copy(
|
TunnelConfig(name = makeTunnelNameUnique(tunnel.name), wgQuick = tunnel.wgQuick, amQuick = tunnel.amQuick),
|
||||||
id = 0,
|
|
||||||
isPrimaryTunnel = false,
|
|
||||||
isMobileDataTunnel = false,
|
|
||||||
isActive = false,
|
|
||||||
name = makeTunnelNameUnique(tunnel.name),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-6
@@ -5,10 +5,8 @@ import androidx.compose.material.icons.Icons
|
|||||||
import androidx.compose.material.icons.rounded.Bolt
|
import androidx.compose.material.icons.rounded.Bolt
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.scale
|
import androidx.compose.ui.draw.scale
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
@@ -19,12 +17,10 @@ import com.zaneschepke.wireguardautotunnel.ui.common.ExpandingRowListItem
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val itemFocusRequester = remember { FocusRequester() }
|
|
||||||
ExpandingRowListItem(
|
ExpandingRowListItem(
|
||||||
leading = {
|
leading = {
|
||||||
val icon = Icons.Rounded.Bolt
|
val icon = Icons.Rounded.Bolt
|
||||||
@@ -33,7 +29,7 @@ fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
|||||||
icon.name,
|
icon.name,
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.size(16.dp.scaledHeight()).scale(1.5f),
|
.size(16.dp).scale(1.5f),
|
||||||
tint =
|
tint =
|
||||||
if (!appUiState.autoTunnelActive) {
|
if (!appUiState.autoTunnelActive) {
|
||||||
Color.Gray
|
Color.Gray
|
||||||
@@ -53,7 +49,7 @@ fun AutoTunnelRowItem(appUiState: AppUiState, onToggle: () -> Unit) {
|
|||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
if (context.isRunningOnTv()) {
|
if (context.isRunningOnTv()) {
|
||||||
itemFocusRequester.requestFocus()
|
onToggle()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isExpanded = false,
|
isExpanded = false,
|
||||||
|
|||||||
+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(
|
||||||
|
|||||||
+10
-13
@@ -9,6 +9,7 @@ import androidx.compose.material.icons.rounded.CopyAll
|
|||||||
import androidx.compose.material.icons.rounded.Delete
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
import androidx.compose.material.icons.rounded.Info
|
import androidx.compose.material.icons.rounded.Info
|
||||||
import androidx.compose.material.icons.rounded.Settings
|
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.Smartphone
|
||||||
import androidx.compose.material.icons.rounded.Star
|
import androidx.compose.material.icons.rounded.Star
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
@@ -33,7 +34,6 @@ import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavControll
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.asColor
|
import com.zaneschepke.wireguardautotunnel.util.extensions.asColor
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TunnelRowItem(
|
fun TunnelRowItem(
|
||||||
@@ -56,20 +56,17 @@ fun TunnelRowItem(
|
|||||||
val itemFocusRequester = remember { FocusRequester() }
|
val itemFocusRequester = remember { FocusRequester() }
|
||||||
ExpandingRowListItem(
|
ExpandingRowListItem(
|
||||||
leading = {
|
leading = {
|
||||||
val circleIcon = Icons.Rounded.Circle
|
val icon = when {
|
||||||
val icon =
|
tunnel.isPrimaryTunnel -> Icons.Rounded.Star
|
||||||
if (tunnel.isPrimaryTunnel) {
|
tunnel.isMobileDataTunnel -> Icons.Rounded.Smartphone
|
||||||
Icons.Rounded.Star
|
tunnel.isEthernetTunnel -> Icons.Rounded.SettingsEthernet
|
||||||
} else if (tunnel.isMobileDataTunnel) {
|
else -> Icons.Rounded.Circle
|
||||||
Icons.Rounded.Smartphone
|
}
|
||||||
} else {
|
|
||||||
circleIcon
|
|
||||||
}
|
|
||||||
Icon(
|
Icon(
|
||||||
icon,
|
icon,
|
||||||
icon.name,
|
icon.name,
|
||||||
tint = leadingIconColor,
|
tint = leadingIconColor,
|
||||||
modifier = Modifier.size(16.dp.scaledHeight()),
|
modifier = Modifier.size(16.dp),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
text = tunnel.name,
|
text = tunnel.name,
|
||||||
@@ -98,7 +95,7 @@ fun TunnelRowItem(
|
|||||||
IconButton(
|
IconButton(
|
||||||
onClick = {
|
onClick = {
|
||||||
navController.navigate(
|
navController.navigate(
|
||||||
Route.Option(tunnel.id),
|
Route.TunnelOptions(tunnel.id),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -131,7 +128,7 @@ fun TunnelRowItem(
|
|||||||
onClick = {
|
onClick = {
|
||||||
onHold()
|
onHold()
|
||||||
navController.navigate(
|
navController.navigate(
|
||||||
Route.Option(tunnel.id),
|
Route.TunnelOptions(tunnel.id),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
|||||||
+18
-15
@@ -21,22 +21,21 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.toThreeDecimalPlaceSt
|
|||||||
@Composable
|
@Composable
|
||||||
fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfig) {
|
fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfig) {
|
||||||
val config = TunnelConfig.configFromAmQuick(tunnelConfig.wgQuick)
|
val config = TunnelConfig.configFromAmQuick(tunnelConfig.wgQuick)
|
||||||
|
config.peers.forEach { peer ->
|
||||||
Row(
|
Row(
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(end = 10.dp, bottom = 10.dp, start = 45.dp),
|
.padding(end = 10.dp, bottom = 10.dp, start = 45.dp),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.Start),
|
horizontalArrangement = Arrangement.spacedBy(5.dp, Alignment.Start),
|
||||||
) {
|
) {
|
||||||
config.peers.forEach {
|
val peerId = peer.publicKey.toBase64().subSequence(0, 3).toString() + "***"
|
||||||
val peerId = it.publicKey.toBase64().subSequence(0, 3).toString() + "***"
|
val peerRx = statistics?.peerStats(peer.publicKey)?.rxBytes ?: 0
|
||||||
val peerRx = statistics?.peerStats(it.publicKey)?.rxBytes ?: 0
|
val peerTx = statistics?.peerStats(peer.publicKey)?.txBytes ?: 0
|
||||||
val peerTx = statistics?.peerStats(it.publicKey)?.txBytes ?: 0
|
|
||||||
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
||||||
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
||||||
val handshake = statistics?.peerStats(it.publicKey)?.latestHandshakeEpochMillis?.let {
|
val handshake = statistics?.peerStats(peer.publicKey)?.latestHandshakeEpochMillis?.let {
|
||||||
if (it == 0L) {
|
if (it == 0L) {
|
||||||
stringResource(R.string.never)
|
stringResource(R.string.never)
|
||||||
} else {
|
} else {
|
||||||
@@ -56,7 +55,11 @@ fun TunnelStatisticsRow(statistics: TunnelStatistics?, tunnelConfig: TunnelConfi
|
|||||||
Column(
|
Column(
|
||||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||||
) {
|
) {
|
||||||
Text(stringResource(R.string.handshake) + ": $handshake", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
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)
|
Text("rx: $peerRxMB MB", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-197
@@ -1,197 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.options
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.outlined.Edit
|
|
||||||
import androidx.compose.material.icons.outlined.NetworkPing
|
|
||||||
import androidx.compose.material.icons.outlined.PhoneAndroid
|
|
||||||
import androidx.compose.material.icons.outlined.Security
|
|
||||||
import androidx.compose.material.icons.outlined.Star
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.IconButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.TrustedNetworkTextBox
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.WildcardsLabel
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|
||||||
|
|
||||||
@OptIn(ExperimentalLayoutApi::class)
|
|
||||||
@Composable
|
|
||||||
fun OptionsScreen(optionsViewModel: OptionsViewModel = hiltViewModel(), appUiState: AppUiState, tunnelId: Int) {
|
|
||||||
val navController = LocalNavController.current
|
|
||||||
val config = appUiState.tunnels.first { it.id == tunnelId }
|
|
||||||
|
|
||||||
var currentText by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
LaunchedEffect(config.tunnelNetworks) {
|
|
||||||
currentText = ""
|
|
||||||
}
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopNavBar(config.name, trailing = {
|
|
||||||
IconButton(onClick = {
|
|
||||||
navController.navigate(
|
|
||||||
Route.Config(config.id),
|
|
||||||
)
|
|
||||||
}) {
|
|
||||||
val icon = Icons.Outlined.Edit
|
|
||||||
Icon(
|
|
||||||
imageVector = icon,
|
|
||||||
contentDescription = icon.name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
|
||||||
modifier =
|
|
||||||
Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(it)
|
|
||||||
.padding(top = 24.dp.scaledHeight())
|
|
||||||
.padding(horizontal = 24.dp.scaledWidth()),
|
|
||||||
) {
|
|
||||||
GroupLabel(stringResource(R.string.auto_tunneling))
|
|
||||||
SurfaceSelectionGroupButton(
|
|
||||||
listOf(
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.Star,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.primary_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.set_primary_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
config.isPrimaryTunnel,
|
|
||||||
onClick = { optionsViewModel.onTogglePrimaryTunnel(config) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { optionsViewModel.onTogglePrimaryTunnel(config) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.PhoneAndroid,
|
|
||||||
title = { Text(stringResource(R.string.mobile_tunnel), style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface)) },
|
|
||||||
description = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.mobile_data_tunnel),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.outline),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
config.isMobileDataTunnel,
|
|
||||||
onClick = { optionsViewModel.onToggleIsMobileDataTunnel(config) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { optionsViewModel.onToggleIsMobileDataTunnel(config) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.NetworkPing,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.restart_on_ping),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ScaledSwitch(
|
|
||||||
checked = config.isPingEnabled,
|
|
||||||
onClick = { optionsViewModel.onToggleRestartOnPing(config) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { optionsViewModel.onToggleRestartOnPing(config) },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
title = {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier
|
|
||||||
.weight(4f, false)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
) {
|
|
||||||
val icon = Icons.Outlined.Security
|
|
||||||
Icon(
|
|
||||||
icon,
|
|
||||||
icon.name,
|
|
||||||
modifier = Modifier.size(iconSize),
|
|
||||||
)
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(start = 16.dp.scaledWidth())
|
|
||||||
.padding(vertical = 6.dp.scaledHeight()),
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.use_tunnel_on_wifi_name),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
description = {
|
|
||||||
TrustedNetworkTextBox(
|
|
||||||
config.tunnelNetworks,
|
|
||||||
onDelete = { optionsViewModel.onDeleteRunSSID(it, config) },
|
|
||||||
currentText = currentText,
|
|
||||||
onSave = { optionsViewModel.onSaveRunSSID(it, config) },
|
|
||||||
onValueChange = { currentText = it },
|
|
||||||
supporting = {
|
|
||||||
if (appUiState.settings.isWildcardsEnabled) {
|
|
||||||
WildcardsLabel()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-2
@@ -9,11 +9,9 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.viewinterop.AndroidView
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
|
||||||
import com.journeyapps.barcodescanner.CompoundBarcodeView
|
import com.journeyapps.barcodescanner.CompoundBarcodeView
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
|
|
||||||
@OptIn(ExperimentalPermissionsApi::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScannerScreen(viewModel: ScannerViewModel = hiltViewModel()) {
|
fun ScannerScreen(viewModel: ScannerViewModel = hiltViewModel()) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|||||||
+1
-5
@@ -9,7 +9,6 @@ import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController
|
||||||
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.toWgQuickString
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
@@ -45,10 +44,7 @@ constructor(
|
|||||||
fun onTunnelQrResult(result: String) = viewModelScope.launch(ioDispatcher) {
|
fun onTunnelQrResult(result: String) = viewModelScope.launch(ioDispatcher) {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
val amConfig = TunnelConfig.configFromAmQuick(result)
|
val amConfig = TunnelConfig.configFromAmQuick(result)
|
||||||
val amQuick = amConfig.toAwgQuickString(true)
|
val tunnelConfig = TunnelConfig.tunnelConfigFromAmConfig(amConfig, makeTunnelNameUnique(generateQrCodeDefaultName(result)))
|
||||||
val wgQuick = amConfig.toWgQuickString()
|
|
||||||
val tunnelName = makeTunnelNameUnique(generateQrCodeDefaultName(result))
|
|
||||||
val tunnelConfig = TunnelConfig(name = tunnelName, wgQuick = wgQuick, amQuick = amQuick)
|
|
||||||
appDataRepository.tunnels.save(tunnelConfig)
|
appDataRepository.tunnels.save(tunnelConfig)
|
||||||
_success.emit(true)
|
_success.emit(true)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
|
|||||||
+79
-77
@@ -7,19 +7,21 @@ import androidx.compose.foundation.layout.Arrangement
|
|||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.imePadding
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.systemBarsPadding
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.automirrored.outlined.ViewQuilt
|
import androidx.compose.material.icons.automirrored.outlined.ViewQuilt
|
||||||
import androidx.compose.material.icons.filled.AppShortcut
|
import androidx.compose.material.icons.filled.AppShortcut
|
||||||
import androidx.compose.material.icons.outlined.AdminPanelSettings
|
|
||||||
import androidx.compose.material.icons.outlined.Bolt
|
import androidx.compose.material.icons.outlined.Bolt
|
||||||
import androidx.compose.material.icons.outlined.Code
|
import androidx.compose.material.icons.outlined.Code
|
||||||
import androidx.compose.material.icons.outlined.FolderZip
|
import androidx.compose.material.icons.outlined.FolderZip
|
||||||
import androidx.compose.material.icons.outlined.Notifications
|
import androidx.compose.material.icons.outlined.Notifications
|
||||||
import androidx.compose.material.icons.outlined.Pin
|
import androidx.compose.material.icons.outlined.Pin
|
||||||
import androidx.compose.material.icons.outlined.Restore
|
import androidx.compose.material.icons.outlined.Restore
|
||||||
|
import androidx.compose.material.icons.outlined.VpnKeyOff
|
||||||
import androidx.compose.material.icons.outlined.VpnLock
|
import androidx.compose.material.icons.outlined.VpnLock
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
@@ -30,13 +32,11 @@ import androidx.compose.runtime.remember
|
|||||||
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.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalFocusManager
|
import androidx.compose.ui.platform.LocalFocusManager
|
||||||
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.hilt.navigation.compose.hiltViewModel
|
||||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
@@ -45,31 +45,24 @@ import com.zaneschepke.wireguardautotunnel.ui.Route
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalFocusRequester
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
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.screens.settings.components.ForwardButton
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.topPadding
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.showToast
|
import com.zaneschepke.wireguardautotunnel.util.extensions.showToast
|
||||||
import xyz.teamgravity.pin_lock_compose.PinManager
|
import xyz.teamgravity.pin_lock_compose.PinManager
|
||||||
|
|
||||||
@OptIn(
|
@OptIn(ExperimentalLayoutApi::class)
|
||||||
ExperimentalPermissionsApi::class,
|
|
||||||
ExperimentalLayoutApi::class,
|
|
||||||
)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel: AppViewModel, uiState: AppUiState) {
|
fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel: AppViewModel, uiState: AppUiState) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
val snackbar = SnackbarController.current
|
val snackbar = SnackbarController.current
|
||||||
val rootFocusRequester = LocalFocusRequester.current
|
|
||||||
val isRunningOnTv = remember { context.isRunningOnTv() }
|
val isRunningOnTv = remember { context.isRunningOnTv() }
|
||||||
|
|
||||||
val interactionSource = remember { MutableInteractionSource() }
|
val interactionSource = remember { MutableInteractionSource() }
|
||||||
@@ -102,8 +95,8 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.verticalScroll(rememberScrollState())
|
.verticalScroll(rememberScrollState())
|
||||||
.fillMaxSize()
|
.fillMaxSize().systemBarsPadding().imePadding()
|
||||||
.padding(top = topPadding)
|
.padding(top = 24.dp.scaledHeight())
|
||||||
.padding(bottom = 40.dp.scaledHeight())
|
.padding(bottom = 40.dp.scaledHeight())
|
||||||
.padding(horizontal = 24.dp.scaledWidth())
|
.padding(horizontal = 24.dp.scaledWidth())
|
||||||
.then(
|
.then(
|
||||||
@@ -119,6 +112,13 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
) {
|
) {
|
||||||
|
val onAutoTunnelClick = {
|
||||||
|
if (!uiState.generalState.isLocationDisclosureShown) {
|
||||||
|
navController.navigate(Route.LocationDisclosure)
|
||||||
|
} else {
|
||||||
|
navController.navigate(Route.AutoTunnel)
|
||||||
|
}
|
||||||
|
}
|
||||||
SurfaceSelectionGroupButton(
|
SurfaceSelectionGroupButton(
|
||||||
listOf(
|
listOf(
|
||||||
SelectionItem(
|
SelectionItem(
|
||||||
@@ -131,85 +131,87 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!uiState.generalState.isLocationDisclosureShown) return@SelectionItem navController.navigate(Route.LocationDisclosure)
|
onAutoTunnelClick()
|
||||||
navController.navigate(Route.AutoTunnel)
|
|
||||||
},
|
},
|
||||||
trailing = {
|
trailing = {
|
||||||
ForwardButton(Modifier.focusable().focusRequester(rootFocusRequester)) { navController.navigate(Route.AutoTunnel) }
|
ForwardButton(Modifier.focusable()) { onAutoTunnelClick() }
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
SurfaceSelectionGroupButton(
|
SurfaceSelectionGroupButton(
|
||||||
buildList {
|
buildList {
|
||||||
|
add(
|
||||||
|
SelectionItem(
|
||||||
|
Icons.Filled.AppShortcut,
|
||||||
|
{
|
||||||
|
ScaledSwitch(
|
||||||
|
uiState.settings.isShortcutsEnabled,
|
||||||
|
onClick = { appViewModel.onToggleShortcutsEnabled() },
|
||||||
|
)
|
||||||
|
},
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.enabled_app_shortcuts),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = { appViewModel.onToggleShortcutsEnabled() },
|
||||||
|
),
|
||||||
|
)
|
||||||
if (!isRunningOnTv) {
|
if (!isRunningOnTv) {
|
||||||
addAll(
|
add(
|
||||||
listOf(
|
SelectionItem(
|
||||||
SelectionItem(
|
Icons.Outlined.VpnLock,
|
||||||
Icons.Filled.AppShortcut,
|
{
|
||||||
{
|
ScaledSwitch(
|
||||||
ScaledSwitch(
|
enabled = !(
|
||||||
uiState.settings.isShortcutsEnabled,
|
(
|
||||||
onClick = { viewModel.onToggleShortcutsEnabled() },
|
uiState.settings.isTunnelOnWifiEnabled ||
|
||||||
)
|
uiState.settings.isTunnelOnEthernetEnabled ||
|
||||||
},
|
uiState.settings.isTunnelOnMobileDataEnabled
|
||||||
title = {
|
) &&
|
||||||
Text(
|
uiState.settings.isAutoTunnelEnabled
|
||||||
stringResource(R.string.enabled_app_shortcuts),
|
),
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||||
)
|
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||||
},
|
)
|
||||||
onClick = { viewModel.onToggleShortcutsEnabled() },
|
},
|
||||||
),
|
title = {
|
||||||
SelectionItem(
|
Text(
|
||||||
Icons.Outlined.VpnLock,
|
stringResource(R.string.always_on_vpn_support),
|
||||||
{
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
ScaledSwitch(
|
)
|
||||||
enabled = !(
|
},
|
||||||
(
|
onClick = { appViewModel.onToggleAlwaysOnVPN() },
|
||||||
uiState.settings.isTunnelOnWifiEnabled ||
|
|
||||||
uiState.settings.isTunnelOnEthernetEnabled ||
|
|
||||||
uiState.settings.isTunnelOnMobileDataEnabled
|
|
||||||
) &&
|
|
||||||
uiState.settings.isAutoTunnelEnabled
|
|
||||||
),
|
|
||||||
onClick = { viewModel.onToggleAlwaysOnVPN() },
|
|
||||||
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.always_on_vpn_support),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = { viewModel.onToggleAlwaysOnVPN() },
|
|
||||||
),
|
|
||||||
SelectionItem(
|
|
||||||
Icons.Outlined.AdminPanelSettings,
|
|
||||||
title = {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.kill_switch),
|
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onClick = {
|
|
||||||
context.launchVpnSettings()
|
|
||||||
},
|
|
||||||
trailing = {
|
|
||||||
ForwardButton { context.launchVpnSettings() }
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
add(
|
||||||
|
SelectionItem(
|
||||||
|
Icons.Outlined.VpnKeyOff,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.kill_switch_options),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
navController.navigate(Route.KillSwitch)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ForwardButton { navController.navigate(Route.KillSwitch) }
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
add(
|
add(
|
||||||
SelectionItem(
|
SelectionItem(
|
||||||
Icons.Outlined.Restore,
|
Icons.Outlined.Restore,
|
||||||
{
|
{
|
||||||
ScaledSwitch(
|
ScaledSwitch(
|
||||||
uiState.settings.isRestoreOnBootEnabled,
|
uiState.settings.isRestoreOnBootEnabled,
|
||||||
onClick = { viewModel.onToggleRestartAtBoot() },
|
onClick = { appViewModel.onToggleRestartAtBoot() },
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
title = {
|
title = {
|
||||||
@@ -218,7 +220,7 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onClick = { viewModel.onToggleRestartAtBoot() },
|
onClick = { appViewModel.onToggleRestartAtBoot() },
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@@ -294,7 +296,7 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
trailing = {
|
trailing = {
|
||||||
ScaledSwitch(
|
ScaledSwitch(
|
||||||
uiState.settings.isKernelEnabled,
|
uiState.settings.isKernelEnabled,
|
||||||
onClick = { viewModel.onToggleKernelMode() },
|
onClick = { appViewModel.onToggleKernelMode() },
|
||||||
enabled = !(
|
enabled = !(
|
||||||
uiState.settings.isAutoTunnelEnabled ||
|
uiState.settings.isAutoTunnelEnabled ||
|
||||||
uiState.settings.isAlwaysOnVpnEnabled ||
|
uiState.settings.isAlwaysOnVpnEnabled ||
|
||||||
@@ -303,7 +305,7 @@ fun SettingsScreen(viewModel: SettingsViewModel = hiltViewModel(), appViewModel:
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
viewModel.onToggleKernelMode()
|
appViewModel.onToggleKernelMode()
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
-96
@@ -4,119 +4,23 @@ import android.content.Context
|
|||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.wireguard.android.backend.WgQuickBackend
|
|
||||||
import com.wireguard.android.util.RootShell
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
|
||||||
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.ui.common.snackbar.SnackbarController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
||||||
import com.zaneschepke.wireguardautotunnel.util.StringValue
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.launchShareFile
|
import com.zaneschepke.wireguardautotunnel.util.extensions.launchShareFile
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class SettingsViewModel
|
class SettingsViewModel
|
||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val appDataRepository: AppDataRepository,
|
private val appDataRepository: AppDataRepository,
|
||||||
@AppShell private val rootShell: Provider<RootShell>,
|
|
||||||
private val fileUtils: FileUtils,
|
private val fileUtils: FileUtils,
|
||||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
|
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val settings = appDataRepository.settings.getSettingsFlow()
|
|
||||||
.stateIn(viewModelScope, SharingStarted.Eagerly, Settings())
|
|
||||||
|
|
||||||
fun setLocationDisclosureShown() = viewModelScope.launch {
|
|
||||||
appDataRepository.appState.setLocationDisclosureShown(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onToggleAlwaysOnVPN() = viewModelScope.launch {
|
|
||||||
with(settings.value) {
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
copy(
|
|
||||||
isAlwaysOnVpnEnabled = !isAlwaysOnVpnEnabled,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onToggleShortcutsEnabled() = viewModelScope.launch {
|
|
||||||
with(settings.value) {
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
this.copy(
|
|
||||||
isShortcutsEnabled = !isShortcutsEnabled,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveKernelMode(enabled: Boolean) = viewModelScope.launch {
|
|
||||||
with(settings.value) {
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
this.copy(
|
|
||||||
isKernelEnabled = enabled,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onToggleKernelMode() = viewModelScope.launch {
|
|
||||||
with(settings.value) {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onToggleRestartAtBoot() = viewModelScope.launch {
|
|
||||||
with(settings.value) {
|
|
||||||
appDataRepository.settings.save(
|
|
||||||
copy(
|
|
||||||
isRestoreOnBootEnabled = !isRestoreOnBootEnabled,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun requestRoot(): Result<Unit> {
|
|
||||||
return withContext(ioDispatcher) {
|
|
||||||
kotlin.runCatching {
|
|
||||||
rootShell.get().start()
|
|
||||||
SnackbarController.showMessage(StringValue.StringResource(R.string.root_accepted))
|
|
||||||
}.onFailure {
|
|
||||||
SnackbarController.showMessage(StringValue.StringResource(R.string.error_root_denied))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun exportAllConfigs(context: Context) = viewModelScope.launch {
|
fun exportAllConfigs(context: Context) = viewModelScope.launch {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
val shareFile = fileUtils.createNewShareFile("wg-export_${Instant.now().epochSecond}.zip")
|
val shareFile = fileUtils.createNewShareFile("wg-export_${Instant.now().epochSecond}.zip")
|
||||||
|
|||||||
+18
@@ -6,6 +6,7 @@ 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
|
||||||
import androidx.compose.material.icons.outlined.Contrast
|
import androidx.compose.material.icons.outlined.Contrast
|
||||||
|
import androidx.compose.material.icons.outlined.Notifications
|
||||||
import androidx.compose.material.icons.outlined.Translate
|
import androidx.compose.material.icons.outlined.Translate
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
@@ -13,6 +14,7 @@ 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.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
@@ -22,12 +24,14 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelec
|
|||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.launchNotificationSettings
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun AppearanceScreen() {
|
fun AppearanceScreen() {
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
@@ -55,6 +59,20 @@ fun AppearanceScreen() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
listOf(
|
||||||
|
SelectionItem(
|
||||||
|
Icons.Outlined.Notifications,
|
||||||
|
title = { Text(stringResource(R.string.notifications), style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface)) },
|
||||||
|
onClick = {
|
||||||
|
context.launchNotificationSettings()
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ForwardButton { context.launchNotificationSettings() }
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
SurfaceSelectionGroupButton(
|
SurfaceSelectionGroupButton(
|
||||||
listOf(
|
listOf(
|
||||||
SelectionItem(
|
SelectionItem(
|
||||||
|
|||||||
+20
-39
@@ -2,48 +2,33 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.langu
|
|||||||
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
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.navigationBars
|
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.windowInsetsPadding
|
|
||||||
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.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.data.datastore.LocaleStorage
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Route
|
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.SelectedLabel
|
import com.zaneschepke.wireguardautotunnel.ui.common.SelectedLabel
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.SelectionItemButton
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.SelectionItemButton
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||||
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.navigateAndForget
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
import timber.log.Timber
|
|
||||||
import java.text.Collator
|
import java.text.Collator
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LanguageScreen(localeStorage: LocaleStorage) {
|
fun LanguageScreen(appUiState: AppUiState, appViewModel: AppViewModel) {
|
||||||
val navController = LocalNavController.current
|
|
||||||
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
val collator = Collator.getInstance(Locale.getDefault())
|
val collator = Collator.getInstance(Locale.getDefault())
|
||||||
|
|
||||||
val currentLocale = remember { mutableStateOf(LocaleUtil.OPTION_PHONE_LANGUAGE) }
|
|
||||||
|
|
||||||
val locales = LocaleUtil.supportedLocales.map {
|
val locales = LocaleUtil.supportedLocales.map {
|
||||||
val tag = it.replace("_", "-")
|
val tag = it.replace("_", "-")
|
||||||
Locale.forLanguageTag(tag)
|
Locale.forLanguageTag(tag)
|
||||||
@@ -54,40 +39,31 @@ fun LanguageScreen(localeStorage: LocaleStorage) {
|
|||||||
locales.sortedWith(compareBy(collator) { it.getDisplayName(it) }).toList()
|
locales.sortedWith(compareBy(collator) { it.getDisplayName(it) }).toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
currentLocale.value = localeStorage.getPreferredLocale()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onChangeLocale(locale: String) {
|
|
||||||
Timber.d("Setting preferred locale: $locale")
|
|
||||||
localeStorage.setPreferredLocale(locale)
|
|
||||||
LocaleUtil.applyLocalizedContext(context, locale)
|
|
||||||
navController.navigateAndForget(Route.Main)
|
|
||||||
}
|
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
TopNavBar(stringResource(R.string.language))
|
TopNavBar(stringResource(R.string.language))
|
||||||
},
|
},
|
||||||
) {
|
) { padding ->
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxSize().padding(it)
|
.fillMaxSize().padding(padding)
|
||||||
.padding(horizontal = 24.dp.scaledWidth()).windowInsetsPadding(WindowInsets.navigationBars),
|
.padding(horizontal = 24.dp.scaledWidth()),
|
||||||
) {
|
) {
|
||||||
item {
|
item {
|
||||||
Box(modifier = Modifier.padding(top = 24.dp.scaledHeight())) {
|
Box(modifier = Modifier.padding(top = 24.dp.scaledHeight())) {
|
||||||
SelectionItemButton(
|
SelectionItemButton(
|
||||||
buttonText = stringResource(R.string.automatic),
|
buttonText = stringResource(R.string.automatic),
|
||||||
onClick = {
|
onClick = {
|
||||||
onChangeLocale(LocaleUtil.OPTION_PHONE_LANGUAGE)
|
appViewModel.onLocaleChange(LocaleUtil.OPTION_PHONE_LANGUAGE)
|
||||||
},
|
},
|
||||||
trailing = {
|
trailing = {
|
||||||
if (currentLocale.value == LocaleUtil.OPTION_PHONE_LANGUAGE) {
|
with(appUiState.generalState.locale) {
|
||||||
SelectedLabel()
|
if (this == LocaleUtil.OPTION_PHONE_LANGUAGE || this == null) {
|
||||||
|
SelectedLabel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ripple = false,
|
ripple = false,
|
||||||
@@ -96,13 +72,18 @@ fun LanguageScreen(localeStorage: LocaleStorage) {
|
|||||||
}
|
}
|
||||||
items(sortedLocales, key = { it }) { locale ->
|
items(sortedLocales, key = { it }) { locale ->
|
||||||
SelectionItemButton(
|
SelectionItemButton(
|
||||||
buttonText = locale.getDisplayLanguage(locale).capitalize(locale) +
|
buttonText = locale.getDisplayLanguage(locale).replaceFirstChar { if (it.isLowerCase()) it.titlecase(locale) else it.toString() } +
|
||||||
if (locale.toLanguageTag().contains("-")) " (${locale.getDisplayCountry(locale).capitalize(locale)})" else "",
|
if (locale.toLanguageTag().contains("-")) {
|
||||||
|
" (${locale.getDisplayCountry(locale)
|
||||||
|
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(locale) else it.toString() }})"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
onChangeLocale(locale.toLanguageTag())
|
appViewModel.onLocaleChange(locale.toLanguageTag())
|
||||||
},
|
},
|
||||||
trailing = {
|
trailing = {
|
||||||
if (locale.toLanguageTag() == currentLocale.value) {
|
if (locale.toLanguageTag() == appUiState.generalState.locale) {
|
||||||
SelectedLabel()
|
SelectedLabel()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
+60
-25
@@ -6,16 +6,19 @@ import androidx.compose.foundation.layout.Arrangement
|
|||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.imePadding
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.outlined.AirplanemodeActive
|
||||||
import androidx.compose.material.icons.outlined.Code
|
import androidx.compose.material.icons.outlined.Code
|
||||||
import androidx.compose.material.icons.outlined.Filter1
|
import androidx.compose.material.icons.outlined.Filter1
|
||||||
import androidx.compose.material.icons.outlined.NetworkPing
|
|
||||||
import androidx.compose.material.icons.outlined.Security
|
import androidx.compose.material.icons.outlined.Security
|
||||||
|
import androidx.compose.material.icons.outlined.Settings
|
||||||
import androidx.compose.material.icons.outlined.SettingsEthernet
|
import androidx.compose.material.icons.outlined.SettingsEthernet
|
||||||
import androidx.compose.material.icons.outlined.SignalCellular4Bar
|
import androidx.compose.material.icons.outlined.SignalCellular4Bar
|
||||||
import androidx.compose.material.icons.outlined.Wifi
|
import androidx.compose.material.icons.outlined.Wifi
|
||||||
@@ -40,13 +43,16 @@ import com.google.accompanist.permissions.isGranted
|
|||||||
import com.google.accompanist.permissions.rememberPermissionState
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Route
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.TrustedNetworkTextBox
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.TrustedNetworkTextBox
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.WildcardsLabel
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components.WildcardsLabel
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.BackgroundLocationDialog
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.BackgroundLocationDialog
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.ForwardButton
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LearnMoreLinkLabel
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LearnMoreLinkLabel
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LocationServicesDialog
|
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LocationServicesDialog
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
|
||||||
@@ -60,6 +66,7 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
|||||||
@Composable
|
@Composable
|
||||||
fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltViewModel()) {
|
fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltViewModel()) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val navController = LocalNavController.current
|
||||||
|
|
||||||
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
var currentText by remember { mutableStateOf("") }
|
var currentText by remember { mutableStateOf("") }
|
||||||
@@ -71,16 +78,18 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
isBackgroundLocationGranted = fineLocationState.status.isGranted
|
isBackgroundLocationGranted = fineLocationState.status.isGranted
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onAutoTunnelWifiChecked() {
|
fun isWifiNameReadable(): Boolean {
|
||||||
if (uiState.settings.isTunnelOnWifiEnabled) viewModel.onToggleTunnelOnWifi().also { return }
|
return when {
|
||||||
when (false) {
|
!isBackgroundLocationGranted ||
|
||||||
isBackgroundLocationGranted -> showLocationDialog = true
|
!fineLocationState.status.isGranted -> {
|
||||||
fineLocationState.status.isGranted -> showLocationDialog = true
|
showLocationDialog = true
|
||||||
context.isLocationServicesEnabled() ->
|
false
|
||||||
showLocationServicesAlertDialog = true
|
|
||||||
else -> {
|
|
||||||
viewModel.onToggleTunnelOnWifi()
|
|
||||||
}
|
}
|
||||||
|
!context.isLocationServicesEnabled() -> {
|
||||||
|
showLocationServicesAlertDialog = true
|
||||||
|
false
|
||||||
|
}
|
||||||
|
else -> true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +111,7 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
showLocationServicesAlertDialog,
|
showLocationServicesAlertDialog,
|
||||||
onDismiss = { showLocationServicesAlertDialog = false },
|
onDismiss = { showLocationServicesAlertDialog = false },
|
||||||
onAttest = {
|
onAttest = {
|
||||||
viewModel.onToggleTunnelOnWifi()
|
showLocationServicesAlertDialog = false
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -113,18 +122,19 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
)
|
)
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
contentWindowInsets = WindowInsets(0.dp),
|
|
||||||
topBar = {
|
topBar = {
|
||||||
TopNavBar(stringResource(R.string.auto_tunneling))
|
TopNavBar(stringResource(R.string.auto_tunneling))
|
||||||
},
|
},
|
||||||
) {
|
) { padding ->
|
||||||
Column(
|
Column(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||||
modifier =
|
modifier =
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(it)
|
.padding(padding)
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.imePadding()
|
||||||
.padding(top = 24.dp.scaledHeight())
|
.padding(top = 24.dp.scaledHeight())
|
||||||
.padding(horizontal = 24.dp.scaledWidth()),
|
.padding(horizontal = 24.dp.scaledWidth()),
|
||||||
) {
|
) {
|
||||||
@@ -147,14 +157,12 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||||
checked = uiState.settings.isTunnelOnWifiEnabled,
|
checked = uiState.settings.isTunnelOnWifiEnabled,
|
||||||
onClick = {
|
onClick = {
|
||||||
if (uiState.settings.isWifiNameByShellEnabled) viewModel.onToggleTunnelOnWifi().also { return@ScaledSwitch }
|
viewModel.onToggleTunnelOnWifi()
|
||||||
onAutoTunnelWifiChecked()
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
if (uiState.settings.isWifiNameByShellEnabled) viewModel.onToggleTunnelOnWifi().also { return@SelectionItem }
|
viewModel.onToggleTunnelOnWifi()
|
||||||
onAutoTunnelWifiChecked()
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SelectionItem(
|
SelectionItem(
|
||||||
@@ -250,7 +258,9 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
uiState.settings.trustedNetworkSSIDs,
|
uiState.settings.trustedNetworkSSIDs,
|
||||||
onDelete = viewModel::onDeleteTrustedSSID,
|
onDelete = viewModel::onDeleteTrustedSSID,
|
||||||
currentText = currentText,
|
currentText = currentText,
|
||||||
onSave = viewModel::onSaveTrustedSSID,
|
onSave = { ssid ->
|
||||||
|
if (uiState.settings.isWifiNameByShellEnabled || isWifiNameReadable()) viewModel.onSaveTrustedSSID(ssid)
|
||||||
|
},
|
||||||
onValueChange = { currentText = it },
|
onValueChange = { currentText = it },
|
||||||
supporting = {
|
supporting = {
|
||||||
if (uiState.settings.isWildcardsEnabled) {
|
if (uiState.settings.isWildcardsEnabled) {
|
||||||
@@ -306,21 +316,46 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AutoTunnelViewModel = hiltV
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
SelectionItem(
|
SelectionItem(
|
||||||
Icons.Outlined.NetworkPing,
|
Icons.Outlined.AirplanemodeActive,
|
||||||
title = {
|
title = {
|
||||||
Text(
|
Text(
|
||||||
stringResource(R.string.restart_on_ping),
|
stringResource(R.string.stop_on_no_internet),
|
||||||
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
description = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.stop_on_internet_loss),
|
||||||
|
style = MaterialTheme.typography.bodySmall.copy(MaterialTheme.colorScheme.outline),
|
||||||
|
)
|
||||||
|
},
|
||||||
trailing = {
|
trailing = {
|
||||||
ScaledSwitch(
|
ScaledSwitch(
|
||||||
checked = uiState.settings.isPingEnabled,
|
checked = uiState.settings.isStopOnNoInternetEnabled,
|
||||||
onClick = { viewModel.onToggleRestartOnPing() },
|
onClick = { viewModel.onToggleStopOnNoInternet() },
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
viewModel.onToggleRestartOnPing()
|
viewModel.onToggleStopOnNoInternet()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
listOf(
|
||||||
|
SelectionItem(
|
||||||
|
Icons.Outlined.Settings,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.advanced_settings),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
navController.navigate(Route.AutoTunnelAdvanced)
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
ForwardButton { navController.navigate(Route.AutoTunnelAdvanced) }
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
+2
-4
@@ -119,12 +119,10 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onToggleRestartOnPing() = viewModelScope.launch {
|
fun onToggleStopOnNoInternet() = viewModelScope.launch {
|
||||||
with(settings.value) {
|
with(settings.value) {
|
||||||
appDataRepository.settings.save(
|
appDataRepository.settings.save(
|
||||||
copy(
|
copy(isStopOnNoInternetEnabled = !isStopOnNoInternetEnabled),
|
||||||
isPingEnabled = !isPingEnabled,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+121
@@ -0,0 +1,121 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.ArrowDropDown
|
||||||
|
import androidx.compose.material.icons.outlined.PauseCircle
|
||||||
|
import androidx.compose.material3.DropdownMenu
|
||||||
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableIntStateOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AdvancedScreen(appUiState: AppUiState, appViewModel: AppViewModel) {
|
||||||
|
var isDropDownExpanded by remember {
|
||||||
|
mutableStateOf(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
var selected by remember { mutableIntStateOf(appUiState.settings.debounceDelaySeconds) }
|
||||||
|
|
||||||
|
LaunchedEffect(selected) {
|
||||||
|
if (selected == appUiState.settings.debounceDelaySeconds) return@LaunchedEffect
|
||||||
|
appViewModel.saveSettings(appUiState.settings.copy(debounceDelaySeconds = selected))
|
||||||
|
if (appUiState.settings.isAutoTunnelEnabled) {
|
||||||
|
appViewModel.bounceAutoTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Scaffold(
|
||||||
|
topBar = {
|
||||||
|
TopNavBar(stringResource(R.string.advanced_settings))
|
||||||
|
},
|
||||||
|
) { padding ->
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.spacedBy(24.dp.scaledHeight(), Alignment.Top),
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(padding)
|
||||||
|
.verticalScroll(rememberScrollState())
|
||||||
|
.padding(top = 24.dp.scaledHeight())
|
||||||
|
.padding(horizontal = 24.dp.scaledWidth()),
|
||||||
|
) {
|
||||||
|
SurfaceSelectionGroupButton(
|
||||||
|
listOf(
|
||||||
|
SelectionItem(
|
||||||
|
Icons.Outlined.PauseCircle,
|
||||||
|
title = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.debounce_delay),
|
||||||
|
style = MaterialTheme.typography.bodyMedium.copy(MaterialTheme.colorScheme.onSurface),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
isDropDownExpanded = true
|
||||||
|
},
|
||||||
|
trailing = {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(5.dp, Alignment.CenterHorizontally),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
Text(text = selected.toString(), style = MaterialTheme.typography.bodyMedium)
|
||||||
|
val icon = Icons.Default.ArrowDropDown
|
||||||
|
Icon(icon, icon.name)
|
||||||
|
}
|
||||||
|
DropdownMenu(
|
||||||
|
modifier = Modifier.height(250.dp.scaledHeight()),
|
||||||
|
scrollState = rememberScrollState(),
|
||||||
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
|
expanded = isDropDownExpanded,
|
||||||
|
onDismissRequest = {
|
||||||
|
isDropDownExpanded = false
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
(0..10).forEachIndexed { index, num ->
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = {
|
||||||
|
Text(text = num.toString())
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
isDropDownExpanded = false
|
||||||
|
selected = num
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
@@ -1,5 +1,6 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components
|
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
@@ -16,6 +17,7 @@ import androidx.compose.material3.IconButton
|
|||||||
import androidx.compose.material3.MaterialTheme
|
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.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
@@ -66,6 +68,7 @@ fun TrustedNetworkTextBox(
|
|||||||
textStyle = MaterialTheme.typography.bodySmall,
|
textStyle = MaterialTheme.typography.bodySmall,
|
||||||
value = currentText,
|
value = currentText,
|
||||||
onValueChange = onValueChange,
|
onValueChange = onValueChange,
|
||||||
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
label = { Text(stringResource(R.string.add_wifi_name)) },
|
label = { Text(stringResource(R.string.add_wifi_name)) },
|
||||||
containerColor = MaterialTheme.colorScheme.surface,
|
containerColor = MaterialTheme.colorScheme.surface,
|
||||||
supportingText = supporting,
|
supportingText = supporting,
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user