Compare commits

..

77 Commits

Author SHA1 Message Date
Roy Orbitson 6e961e0994 Preserve DNS search domains (#344) 2024-09-06 23:01:59 -04:00
Zane Schepke b2a2b9fcf4 fix: android nightly workflow
bump deps
2024-08-27 01:12:34 -04:00
Zane Schepke 543a61efe0 add package build types 2024-08-25 22:34:28 -04:00
Zane Schepke 688fad770c chore: typo fix readme 2024-08-17 23:12:58 -04:00
Zane Schepke e87dd8d3ce chore: update README.md 2024-08-17 23:12:03 -04:00
Zane Schepke 30851a7d7b fix: tile control and kernel sync (#320)
increase auto tunnel delay to 3 seconds

optimize stats job by killing it when app is backgrounded

fix tunnel launch from background

add restart of services and tunnels after update
2024-08-17 21:29:31 -04:00
Zane Schepke 3f4673b2a7 fix: improve navigation animation speed
Fixes possible crashes on slow androidTVs

Closes #49
2024-08-17 00:43:57 -04:00
Zane Schepke 528a1f84e4 fix: minor ui changes 2024-08-16 22:13:31 -04:00
Zane Schepke 1af474c449 bump version code 2024-08-13 17:07:30 -04:00
Zane Schepke 7e3405f3fd fix: location disclosure missing 2024-08-13 16:55:14 -04:00
Zane Schepke ffeb089aa7 Merge branch 'main' of https://github.com/zaneschepke/wgtunnel 2024-08-11 00:32:39 -04:00
Zane Schepke 3838c32ddf remove duplicate language 2024-08-11 00:32:08 -04:00
Zane Schepke 0c1cb40add bump version (#311) 2024-08-11 00:07:46 -04:00
Zane Schepke bfb8d59827 fix: improve tunnel reliability (#298)
- Attempts to fix tunnel and auto-tunnel reliability by removing the tunnel foreground service and circumventing the limitation of starting the vpn service from by background by using a broadcast receiver.

- Removes tunnel foreground notification.

- Improves the reliability auto-tunnel start on reboot by adding an additional notification launch calls.

- Fixes bug where pin feature could be turned on without the pin being set.

- Improves quick tile reliability and sync.

- Improves reliability of app shortcuts.

- Improves kernel mode

- Improves permissions flow

- Adds support for dynamic app colors Android 12+

- Add support for light/dark system modes
2024-08-10 23:59:05 -04:00
dependabot[bot] 19961ca343 build(deps): bump actions/upload-artifact from 4.3.5 to 4.3.6 (#308)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-08-08 07:51:05 -04:00
dependabot[bot] 6c007a8ca8 build(deps): bump actions/upload-artifact from 4.3.4 to 4.3.5 (#302)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-08-05 16:51:41 -04:00
Zane Schepke 8e6a9bb309 fix: remove old nightly versions (#295) 2024-07-31 00:00:14 -04:00
Zane Schepke 594834a908 fix: tasker launch of shortcuts (#290) 2024-07-29 17:14:08 -04:00
Zane Schepke a5e9aa83b8 feat: check for always-on VPN (#289) 2024-07-28 22:21:32 -04:00
Languages add-on 5a77661fb3 Added translation using Weblate (Ukrainian) 2024-07-28 15:37:44 -04:00
Luiz Fellipe Carneiro ee5d3ea6a9 Translated using Weblate (Portuguese)
Currently translated at 18.5% (5 of 27 strings)

Translation: WG Tunnel/fastlane
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/fastlane/pt/
2024-07-28 15:37:06 -04:00
Languages add-on f6da0fe31b Added translation using Weblate (Portuguese) 2024-07-28 15:37:06 -04:00
Zane Schepke 80a02382e1 ci: add basic ci (#287)
- add ktlint
2024-07-28 14:49:35 -04:00
Zane Schepke b9a8400453 fix: app lock bypass (#286) 2024-07-28 14:24:22 -04:00
Zane Schepke 3a17d2855b fix: fastlane deploy (#285) 2024-07-28 14:05:15 -04:00
Zane Schepke 086b48c79d fix: signing config bug 2024-07-28 12:18:17 -04:00
Zane Schepke 1f561fbf38 Fix/app signature (#284) 2024-07-28 12:11:16 -04:00
Zane Schepke 45e63e9910 fix versioning (#280) 2024-07-28 03:27:12 -04:00
Zane Schepke 66e89c83e2 chore: fmt 2024-07-28 02:17:01 -04:00
Zane Schepke 470fa0191b fix: signing issue (#279) 2024-07-28 02:13:18 -04:00
Zane Schepke 7c15943a81 fix: nightly release bug 2024-07-27 23:32:57 -04:00
Zane Schepke 05adf7539f fix: github cli for cd 2024-07-27 23:18:30 -04:00
Zane Schepke a9a49e3421 fix: nightly versioning 2024-07-27 22:56:56 -04:00
Zane Schepke 4dd8241fa1 fix: nightly check 2024-07-27 22:34:07 -04:00
Zane Schepke 431b2f9061 fix: nightly grep 2024-07-27 07:59:55 -04:00
Zane Schepke f822292584 fix: nightly check 2024-07-27 07:54:52 -04:00
Zane Schepke 4ffc5d4069 fix: play deploy job 2024-07-27 07:42:02 -04:00
Zane Schepke 680fbed28c fix: nightly detection 2024-07-27 07:37:47 -04:00
Zane Schepke 737524831b fix: auto tunnel tile bug
Fixes bug where auto tunnel tile was the opposite state

Closes #241
2024-07-27 07:21:27 -04:00
Zane Schepke b8c36ac192 fix: notification pin lock bypass
Closes #242
2024-07-27 06:44:02 -04:00
Zane Schepke 547686069f fix: androidtv multiple tunnel control
Fixes a bug where androidTV was only allowing control of a single tunnel.
Closes #268
2024-07-27 06:04:25 -04:00
Zane Schepke ac18ac8274 fix: cd upload bug 2024-07-27 04:53:16 -04:00
Zane Schepke cb983da990 fix: cd apk upload 2024-07-27 04:42:21 -04:00
Zane Schepke cfe64dcb61 fix: cd fdroid dispatch bug 2024-07-27 04:24:26 -04:00
Zane Schepke 2db521d510 fix: add missing cd deps 2024-07-27 04:20:35 -04:00
Zane Schepke ff6c763b7b fix: cd typo 2024-07-27 04:18:04 -04:00
Zane Schepke ebf7521fa1 cd: improve release workflow w/nightly 2024-07-27 04:15:57 -04:00
Zane Schepke 7a2d96fcd7 fix: remove portrait lock
bump deps

Closes #259
Closes #227
Closes #226
Closes #219
Closes #218
2024-07-27 02:20:01 -04:00
𝗪𝗜𝗡𝗭𝗢𝗥𝗧 c6c8047982 update-tr-locales (#255) 2024-07-11 01:00:40 -04:00
dependabot[bot] 9cfb7250de build(deps): bump actions/upload-artifact from 4.3.3 to 4.3.4 (#257)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-07-11 00:21:25 -04:00
Zane Schepke 79b5b039b0 fix: crashing and pin lock
Re-enabled pin lock after disablement from crashes.

Fixed crashing issues.

Closes #237

Fixed bug where pin lock will no longer initialize if never not enabled/in use.

Improved tunnel control tile performance.

Fix bad address crash when user enters bad addresses into allowedIps.
Closes #229

Disabled auto rotate
Closes #212

Add restart on boot toggle to make restart of services feature more obvious and configurable.
2024-06-18 23:08:40 -04:00
Weblate (bot) 29616f8325 Translations update from Hosted Weblate (#228)
Co-authored-by: gallegonovato <fran-carro@hotmail.es>
Co-authored-by: Kirill Isakov <k@isakov.net>
Co-authored-by: Roman Nahálka <nahalkaroman@gmail.com>
Co-authored-by: Languages add-on <noreply-addon-languages@weblate.org>
Co-authored-by: Luiz Fellipe Carneiro <fellipec@outlook.com>
Co-authored-by: Kachelkaiser <kachelkaiser@outlook.com>
Co-authored-by: yura04 <yura.panasiuk04@gmail.com>
2024-06-18 19:13:41 -04:00
Zane Schepke 8bbe81d294 fix: disable pin lock 2024-06-01 06:00:27 -04:00
Zane Schepke 571fb1b12c chore: add missing title for cz 2024-06-01 03:05:04 -04:00
Zane Schepke 02f6f97aa1 docs: update readme 2024-06-01 03:00:41 -04:00
Zane Schepke 1d74d0984e fix: auto tunneling and backup
Fixes a bug where android backups can cause app crashes due to pin lock feature keystore.

Fixes a bug where auto tunneling to SSID tunnel was not working correctly.

Fixes a mobile data tunneling bug which was causing mobile data tunneling to not perform correctly.

Additional strictmode improvements.
2024-06-01 02:37:32 -04:00
Zane Schepke 6448386f76 update gradle checksum 2024-05-31 00:02:41 -04:00
Zane Schepke d09e85ba45 remove unneeded strings 2024-05-30 23:29:09 -04:00
Zane Schepke a9bc1cc7f0 Merge remote-tracking branch 'weblate/main' 2024-05-30 23:15:58 -04:00
Zane Schepke 54d9653f04 fix: mobile data tunneling
Fixes a bug where mobile data tunneling was not working properly in certain scenarios.

Fixes an issue where the new floating action button was not working correctly on AndroidTV.

Improved local logging.

Additional refactors and optimizations.
2024-05-30 23:10:28 -04:00
Eryk Michalak efc66821a6 Translated using Weblate (Polish)
Currently translated at 6.1% (11 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/pl/
2024-05-24 15:09:16 +00:00
Kachelkaiser 3af7adc45b Translated using Weblate (German)
Currently translated at 100.0% (179 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/de/
2024-05-24 15:09:15 +00:00
Kachelkaiser 5754f2183c Translated using Weblate (German)
Currently translated at 100.0% (25 of 25 strings)

Translation: WG Tunnel/fastlane
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/fastlane/de/
2024-05-24 15:09:14 +00:00
Kirill Isakov f7f7f1bd9d Translated using Weblate (Russian)
Currently translated at 100.0% (179 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/ru/
2024-05-24 15:09:12 +00:00
Weblate (bot) 57bb3f5e74 Translations update from Hosted Weblate (#216)
Co-authored-by: Languages add-on <noreply-addon-languages@weblate.org>
Co-authored-by: er2de2 <romandrajer@gmail.com>
Co-authored-by: Kirill Isakov <k@isakov.net>
Co-authored-by: Kachelkaiser <kachelkaiser@outlook.com>
Co-authored-by: Golbinex <baksa@protonmail.com>
2024-05-24 00:16:32 -04:00
Hosted Weblate 49196e7c7b Update translation files
Updated by "Remove blank strings" hook in Weblate.

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/
2024-05-23 16:52:57 +02:00
Golbinex 894b63e668 Translated using Weblate (Czech)
Currently translated at 86.5% (155 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/cs/
2024-05-23 16:52:57 +02:00
Golbinex e16d44ff20 Translated using Weblate (Czech)
Currently translated at 8.0% (2 of 25 strings)

Translation: WG Tunnel/fastlane
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/fastlane/cs/
2024-05-23 16:52:57 +02:00
Kachelkaiser b8b3f3001b Translated using Weblate (German)
Currently translated at 98.8% (177 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/de/
2024-05-23 16:52:57 +02:00
Kirill Isakov d142ecea6e Translated using Weblate (Russian)
Currently translated at 100.0% (179 of 179 strings)

Translation: WG Tunnel/strings
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/strings/ru/
2024-05-23 16:52:57 +02:00
Kirill Isakov b793984ede Translated using Weblate (Russian)
Currently translated at 100.0% (25 of 25 strings)

Translation: WG Tunnel/fastlane
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/fastlane/ru/
2024-05-23 16:52:56 +02:00
Languages add-on ae2532afe5 Added translation using Weblate (Czech) 2024-05-23 10:20:34 +02:00
er2de2 2720a3b35e Translated using Weblate (Polish)
Currently translated at 12.0% (3 of 25 strings)

Translation: WG Tunnel/fastlane
Translate-URL: https://hosted.weblate.org/projects/wg-tunnel/fastlane/pl/
2024-05-22 19:45:01 +02:00
Languages add-on 2350364543 Added translation using Weblate (Polish) 2024-05-22 06:08:54 +02:00
Languages add-on f4172cb1fc Added translation using Weblate (Hungarian) 2024-05-15 22:45:19 +02:00
Weblate (bot) 90c482ae4f Translations update from Hosted Weblate (#214)
Co-authored-by: Rainer <ram002@web.de>
Co-authored-by: Philip <philip.web@directbox.com>
2024-05-14 18:52:56 -04:00
Weblate (bot) 1eb8ad62e0 Translations update from Hosted Weblate (#210) 2024-05-12 15:14:05 -04:00
233 changed files with 10647 additions and 7917 deletions
+23 -11
View File
@@ -1,8 +1,14 @@
[{*.kt,*.kts}] root = true
indent_style = space
insert_final_newline = true [*]
max_line_length = 100 charset = utf-8
indent_size = 4 indent_size = 4
indent_style = tab
max_line_length = 150
trim_trailing_whitespace = true
insert_final_newline = true
[{*.kt,*.kts}]
ij_continuation_indent_size = 4 ij_continuation_indent_size = 4
ij_java_names_count_to_use_import_on_demand = 9999 ij_java_names_count_to_use_import_on_demand = 9999
ij_kotlin_align_in_columns_case_branch = false ij_kotlin_align_in_columns_case_branch = false
@@ -11,8 +17,6 @@ ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_assignment_wrap = normal ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0 ij_kotlin_blank_lines_after_class_header = 0
ij_kotlin_blank_lines_around_block_when_branches = 0 ij_kotlin_blank_lines_around_block_when_branches = 0
@@ -20,10 +24,7 @@ ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_
ij_kotlin_block_comment_at_first_column = true ij_kotlin_block_comment_at_first_column = true
ij_kotlin_call_parameters_new_line_after_left_paren = true ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = false ij_kotlin_call_parameters_right_paren_on_new_line = false
ij_kotlin_call_parameters_wrap = on_every_item
ij_kotlin_catch_on_new_line = false ij_kotlin_catch_on_new_line = false
ij_kotlin_class_annotation_wrap = split_into_lines
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_for_chained_calls = true ij_kotlin_continuation_indent_for_chained_calls = true
ij_kotlin_continuation_indent_for_expression_bodies = true ij_kotlin_continuation_indent_for_expression_bodies = true
ij_kotlin_continuation_indent_in_argument_lists = true ij_kotlin_continuation_indent_in_argument_lists = true
@@ -52,7 +53,6 @@ ij_kotlin_method_annotation_wrap = split_into_lines
ij_kotlin_method_call_chain_wrap = normal ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_method_parameters_wrap = on_every_item
ij_kotlin_name_count_to_use_star_import = 9999 ij_kotlin_name_count_to_use_star_import = 9999
ij_kotlin_name_count_to_use_star_import_for_members = 9999 ij_kotlin_name_count_to_use_star_import_for_members = 9999
ij_kotlin_parameter_annotation_wrap = off ij_kotlin_parameter_annotation_wrap = off
@@ -82,4 +82,16 @@ ij_kotlin_variable_annotation_wrap = off
ij_kotlin_while_on_new_line = false ij_kotlin_while_on_new_line = false
ij_kotlin_wrap_elvis_expressions = 1 ij_kotlin_wrap_elvis_expressions = 1
ij_kotlin_wrap_expression_body_functions = 1 ij_kotlin_wrap_expression_body_functions = 1
ij_kotlin_wrap_first_method_in_call_chain = false ij_kotlin_wrap_first_method_in_call_chain = false
#compose
ktlint_standard_filename = disabled
ktlint_standard_no-wildcard-imports = disabled
ktlint_standard_function-naming = disabled
ktlint_standard_property-naming = disabled
ktlint_standard_package-naming = disabled
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_code_style = android_studio
ktlint_standard_import-ordering = disabled
ktlint_standard_package-naming = disabled
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
+1 -1
View File
@@ -4,7 +4,7 @@
We as individuals involved in this project, pledge to participate in this We as individuals involved in this project, pledge to participate in this
community in a respectful, constructive, and civil manner as we work towards a common goal community in a respectful, constructive, and civil manner as we work towards a common goal
of delivering free, open source, and value adding software for all. of delivering free, open source, and value adding software for all.
## Standard ## Standard
+23
View File
@@ -0,0 +1,23 @@
name: ci-android
on:
workflow_dispatch:
pull_request:
jobs:
format:
runs-on: ubuntu-latest
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
- name: Run ktlint
run: ./gradlew ktlintCheck
+1 -1
View File
@@ -2,7 +2,7 @@ name: Issue Updates Workflow
on: on:
issues: issues:
types: [opened, closed, reopened] types: [ opened, closed, reopened ]
jobs: jobs:
-124
View File
@@ -1,124 +0,0 @@
name: Android CI Tag Deployment (Pre-release)
on:
workflow_dispatch:
push:
tags:
- '*.*.*-**'
jobs:
build:
name: Build Signed APK
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/
GH_USER: ${{ secrets.GH_USER }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
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
run: |
store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV
- name: Create service_account.json
id: createServiceAccount
run: echo '${{ secrets.SERVICE_ACCOUNT_JSON }}' > service_account.json
# Build and sign APK ("-x test" argument is used to skip tests)
# add fdroid flavor for apk upload
- name: Build Fdroid Release APK
run: ./gradlew :app:assembleFdroidRelease -x test
# get fdroid flavor release apk path
- name: Get apk path
id: apk-path
run: echo "path=$(find . -regex '^.*/build/outputs/apk/fdroid/release/.*\.apk$' -type f | head -1)" >> $GITHUB_OUTPUT
- name: Get version code
run: |
version_code=$(grep "VERSION_CODE" buildSrc/src/main/kotlin/Constants.kt | awk '{print $5}' | tr -d '\n')
echo "VERSION_CODE=$version_code" >> $GITHUB_ENV
# Save the APK after the Build job is complete to publish it as a Github release in the next job
- name: Upload APK
uses: actions/upload-artifact@v4.3.3
with:
name: wgtunnel
path: ${{ steps.apk-path.outputs.path }}
- name: Download APK from build
uses: actions/download-artifact@v4
with:
name: wgtunnel
- name: Create Release with Fastlane changelog notes
id: create_release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# fix hardcode changelog file name
body_path: ${{ github.workspace }}/fastlane/metadata/android/en-US/changelogs/${{ env.VERSION_CODE }}.txt
tag_name: ${{ github.ref_name }}
name: ${{ github.ref_name }}
draft: false
prerelease: true
files: ${{ github.workspace }}/${{ steps.apk-path.outputs.path }}
- name: Install apksigner
run: |
sudo apt-get update
sudo apt-get install -y apksigner
- name: Get checksum
id: checksum
run: echo "checksum=$(apksigner verify -print-certs ${{ steps.apk-path.outputs.path }} | grep -Po "(?<=SHA-256 digest:) .*" | tr -d "[:blank:]")" >> $GITHUB_OUTPUT
- name: Append checksum
id: append_checksum
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
body: |
SHA256 fingerprint:
```${{ steps.checksum.outputs.checksum }}```
tag_name: ${{ github.ref_name }}
name: ${{ github.ref_name }}
draft: false
prerelease: true
append_body: true
- name: Deploy with fastlane
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2' # Not needed with a .ruby-version file
bundler-cache: true
- name: Distribute app to Beta track 🚀
run: (cd ${{ github.workspace }} && bundle install && bundle exec fastlane beta)
+1 -1
View File
@@ -2,7 +2,7 @@ name: Release Updates Workflow
on: on:
release: release:
types: [published] types: [ published ]
jobs: jobs:
+148 -42
View File
@@ -1,17 +1,40 @@
# name of the workflow name: release-android
name: Android CI Tag Deployment (Release)
on: on:
schedule:
- cron: "4 3 * * *"
workflow_dispatch: workflow_dispatch:
push: inputs:
tags: track:
- '*.*.*' type: choice
- '!*.*.*-**' description: "Google play release track"
options:
- none
- internal
- alpha
- beta
- production
default: alpha
required: true
release_type:
type: choice
description: "GitHub release type"
options:
- none
- prerelease
- nightly
- release
default: release
required: true
tag_name:
description: "Tag name for release"
required: false
default: nightly
jobs: jobs:
build: build:
name: Build Signed APK name: Build Signed APK
if: ${{ inputs.release_type != 'none' }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
@@ -21,7 +44,9 @@ jobs:
KEY_STORE_FILE: 'android_keystore.jks' KEY_STORE_FILE: 'android_keystore.jks'
KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/ KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
GH_USER: ${{ secrets.GH_USER }} GH_USER: ${{ secrets.GH_USER }}
# GH needed for gh cli
GH_TOKEN: ${{ secrets.GH_TOKEN }} GH_TOKEN: ${{ secrets.GH_TOKEN }}
GH_REPO: ${{ github.repository }}
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -34,6 +59,10 @@ jobs:
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x gradlew run: chmod +x gradlew
- name: Install system dependencies
run: |
sudo apt update && sudo apt install -y gh apksigner
# Here we need to decode keystore.jks from base64 string and place it # Here we need to decode keystore.jks from base64 string and place it
# in the folder specified in the release signing configuration # in the folder specified in the release signing configuration
- name: Decode Keystore - name: Decode Keystore
@@ -57,75 +86,152 @@ jobs:
# Build and sign APK ("-x test" argument is used to skip tests) # Build and sign APK ("-x test" argument is used to skip tests)
# add fdroid flavor for apk upload # add fdroid flavor for apk upload
- name: Build Fdroid Release APK - name: Build Fdroid Release APK
if: ${{ inputs.release_type != '' && inputs.release_type == 'release' }}
run: ./gradlew :app:assembleFdroidRelease -x test run: ./gradlew :app:assembleFdroidRelease -x test
# get fdroid flavor release apk path - name: Build Fdroid Prerelease APK
- name: Get apk path if: ${{ inputs.release_type != '' && inputs.release_type == 'prerelease' }}
id: apk-path run: ./gradlew :app:assembleFdroidPrerelease -x test
run: echo "path=$(find . -regex '^.*/build/outputs/apk/fdroid/release/.*\.apk$' -type f | head -1)" >> $GITHUB_OUTPUT
- 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' }}
run: | run: |
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
# Save the APK after the Build job is complete to publish it as a Github release in the next job # Save the APK after the Build job is complete to publish it as a Github release in the next job
- name: Upload APK - name: Upload APK
uses: actions/upload-artifact@v4.3.3 uses: actions/upload-artifact@v4.3.6
with: with:
name: wgtunnel name: wgtunnel
path: ${{ steps.apk-path.outputs.path }} path: ${{ env.APK_PATH }}
- name: Download APK from build - name: Download APK from build
uses: actions/download-artifact@v4 uses: actions/download-artifact@v4
with: with:
name: wgtunnel name: wgtunnel
- name: Repository Dispatch for my F-Droid repo - name: Repository Dispatch for my F-Droid repo
uses: peter-evans/repository-dispatch@v3 uses: peter-evans/repository-dispatch@v3
if: ${{ inputs.release_type == 'release' }}
with: with:
token: ${{ secrets.PAT }} token: ${{ secrets.PAT }}
repository: zaneschepke/fdroid repository: zaneschepke/fdroid
event-type: fdroid-update event-type: fdroid-update
# Setup TAG_NAME, which is used as a general "name"
- if: github.event_name == 'workflow_dispatch'
run: echo "TAG_NAME=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV
- if: github.event_name == 'schedule'
run: echo "TAG_NAME=nightly" >> $GITHUB_ENV
- name: Set version release notes
if: ${{ inputs.release_type == 'release' }}
run: |
RELEASE_NOTES="$(cat ${{ github.workspace }}/fastlane/metadata/android/en-US/changelogs/${{ env.VERSION_CODE }}.txt)"
echo "RELEASE_NOTES<<EOF" >> $GITHUB_ENV
echo "$RELEASE_NOTES" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: On nightly release notes
if: ${{ contains(env.TAG_NAME, 'nightly') }}
run: |
echo "RELEASE_NOTES=Nightly build for the latest development version of the app." >> $GITHUB_ENV
gh release delete nightly --yes || true
git push origin :nightly || true
- name: On prerelease release notes
if: ${{ inputs.release_type == 'prerelease' }}
run: |
echo "RELEASE_NOTES=Testing version of app for specific feature." >> $GITHUB_ENV
gh release delete ${{ github.event.inputs.tag_name }} --yes || true
- name: Get checksum
id: checksum
run: echo "checksum=$(apksigner verify -print-certs ${{ env.APK_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
id: create_release id: create_release
uses: softprops/action-gh-release@v2 uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
body_path: ${{ github.workspace }}/fastlane/metadata/android/en-US/changelogs/${{ env.VERSION_CODE }}.txt
tag_name: ${{ github.ref_name }}
name: ${{ github.ref_name }}
draft: false
prerelease: false
files: ${{ github.workspace }}/${{ steps.apk-path.outputs.path }}
- name: Install apksigner
run: |
sudo apt-get update
sudo apt-get install -y apksigner
- name: Get checksum
id: checksum
run: echo "checksum=$(apksigner verify -print-certs ${{ steps.apk-path.outputs.path }} | grep -Po "(?<=SHA-256 digest:) .*" | tr -d "[:blank:]")" >> $GITHUB_OUTPUT
- name: Append checksum
id: append_checksum
uses: softprops/action-gh-release@v2
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
body: | body: |
${{ env.RELEASE_NOTES }}
SHA256 fingerprint: SHA256 fingerprint:
```${{ steps.checksum.outputs.checksum }}``` ```${{ steps.checksum.outputs.checksum }}```
tag_name: ${{ github.ref_name }} tag_name: ${{ env.TAG_NAME }}
name: ${{ github.ref_name }} name: ${{ env.TAG_NAME }}
draft: false draft: false
prerelease: false prerelease: ${{ inputs.release_type == 'prerelease' || inputs.release_type == '' || inputs.release_type == 'nightly' }}
append_body: true make_latest: ${{ inputs.release_type == 'release' }}
files: ${{ github.workspace }}/${{ env.APK_PATH }}
publish-play:
if: ${{ inputs.track != 'none' && inputs.track != '' }}
name: Publish to Google Play
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/
GH_USER: ${{ secrets.GH_USER }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
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
run: |
store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV
- name: Create service_account.json
id: createServiceAccount
run: echo '${{ secrets.SERVICE_ACCOUNT_JSON }}' > service_account.json
- name: Deploy with fastlane - name: Deploy with fastlane
uses: ruby/setup-ruby@v1 uses: ruby/setup-ruby@v1
with: with:
ruby-version: '3.2' # Not needed with a .ruby-version file ruby-version: '3.2' # Not needed with a .ruby-version file
bundler-cache: true bundler-cache: true
- name: Distribute app to Prod track 🚀
run: (cd ${{ github.workspace }} && bundle install && bundle exec fastlane production) - name: Distribute app to Prod track 🚀
run: (cd ${{ github.workspace }} && bundle install && bundle exec fastlane ${{ inputs.track }})
+1
View File
@@ -71,3 +71,4 @@ app/release/output.json
.idea/codeStyles/ .idea/codeStyles/
# where we keep our signing secrets locally # where we keep our signing secrets locally
app/signing.properties app/signing.properties
/.kotlin/
+24 -15
View File
@@ -22,7 +22,8 @@ WG Tunnel
<div align="left"> <div align="left">
This is an alternative Android Application for [WireGuard](https://www.wireguard.com/) and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) with added This is an alternative Android Application for [WireGuard](https://www.wireguard.com/)
and [AmneziaWG](https://docs.amnezia.org/documentation/amnezia-wg/) with added
features. Built using the [wireguard-android](https://github.com/WireGuard/wireguard-android) 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 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. inspired by the official [WireGuard Android](https://github.com/WireGuard/wireguard-android) app.
@@ -54,37 +55,38 @@ and on while on different networks. This app was created to offer a free solutio
* Split tunneling by application with search * Split tunneling by application with search
* WireGuard support for kernel and userspace modes * WireGuard support for kernel and userspace modes
* Amnezia support for userspace mode for DPI/censorship protection * Amnezia support for userspace mode for DPI/censorship protection
* Pre/Post Up/Down scripts support for all modes on a rooted device
* Always-On VPN support * Always-On VPN support
* Export Amnezia and WireGuard tunnels to zip * Export Amnezia and WireGuard tunnels to zip
* Quick tile support for tunnel toggling, auto-tunneling * Quick tile support for tunnel toggling, auto-tunneling
* Static shortcuts support for tunnel toggling, auto-tunneling * Static shortcuts support for tunnel toggling, auto-tunneling
* Intent automation support for all tunnels * Intent automation support for all tunnels
* Automatic auto-tunneling service restart after reboot * Automatic auto-tunneling service and/or tunnel restart after reboot or app update
* Automatic tunnel restart after reboot
* Battery preservation measures * Battery preservation measures
* Restart tunnel on ping failure (beta) * Restart tunnel on ping failure (beta)
## Fdroid
Want updates faster?
Check out my personal [fdroid repository](https://github.com/zaneschepke/fdroid) to get updates the
moment they are released.
## Docs ## Docs
Basic documentation of the feature and behaviors of this app can be Information about features, behaviors, and answers to common questions can be found in the
found [here](https://zaneschepke.com/wgtunnel-docs/overview.html). app [documentation](https://zaneschepke.com/wgtunnel-docs/overview.html).
The repository for these docs can be found [here](https://github.com/zaneschepke/wgtunnel-docs). The repository for these docs can be found [here](https://github.com/zaneschepke/wgtunnel-docs).
## Contributing
Any contributions in the form of feedback, issues, code, or translations are welcome and much appreciated!
Please read the [code of conduct](https://github.com/zaneschepke/wgtunnel?tab=coc-ov-file#contributor-code-of-conduct) before contributing.
## Translation ## Translation
This app is using [Weblate](https://weblate.org) to assist with translations. This app is using [Weblate](https://weblate.org) to assist with translations.
Help translate WG Tunnel into your language at [Hosted Weblate](https://hosted.weblate.org/engage/wg-tunnel/).\ Help translate WG Tunnel into your language
at [Hosted Weblate](https://hosted.weblate.org/engage/wg-tunnel/).\
[![Translation status](https://hosted.weblate.org/widgets/wg-tunnel/-/multi-auto.svg)](https://hosted.weblate.org/engage/wg-tunnel/) [![Translation status](https://hosted.weblate.org/widgets/wg-tunnel/-/multi-auto.svg)](https://hosted.weblate.org/engage/wg-tunnel/)
## Building ## Building
``` ```
@@ -98,4 +100,11 @@ And then build the app:
$ ./gradlew assembleDebug $ ./gradlew assembleDebug
``` ```
</span> ## Contributing
Any contributions in the form of feedback, issues, code, or translations are welcome and much
appreciated!
Please read
the [code of conduct](https://github.com/zaneschepke/wgtunnel?tab=coc-ov-file#contributor-code-of-conduct)
before contributing.
+185 -179
View File
@@ -1,215 +1,221 @@
import java.util.Properties
plugins { plugins {
alias(libs.plugins.android.application) alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.android)
alias(libs.plugins.hilt.android) alias(libs.plugins.hilt.android)
alias(libs.plugins.kotlinxSerialization) alias(libs.plugins.kotlinxSerialization)
alias(libs.plugins.ksp) alias(libs.plugins.ksp)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.grgit)
} }
android { android {
namespace = Constants.APP_ID namespace = Constants.APP_ID
compileSdk = Constants.TARGET_SDK compileSdk = Constants.TARGET_SDK
androidResources { androidResources {
generateLocaleConfig = true generateLocaleConfig = true
} }
defaultConfig { defaultConfig {
applicationId = Constants.APP_ID applicationId = Constants.APP_ID
minSdk = Constants.MIN_SDK minSdk = Constants.MIN_SDK
targetSdk = Constants.TARGET_SDK targetSdk = Constants.TARGET_SDK
versionCode = Constants.VERSION_CODE versionCode = determineVersionCode()
versionName = Constants.VERSION_NAME versionName = determineVersionName()
ksp { arg("room.schemaLocation", "$projectDir/schemas") } ksp { arg("room.schemaLocation", "$projectDir/schemas") }
sourceSets { sourceSets {
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
} }
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { useSupportLibrary = true } vectorDrawables { useSupportLibrary = true }
} }
signingConfigs { signingConfigs {
create(Constants.RELEASE) { create(Constants.RELEASE) {
val properties = storeFile = getStoreFile()
Properties().apply { storePassword = getSigningProperty(Constants.STORE_PASS_VAR)
// created local file for signing details keyAlias = getSigningProperty(Constants.KEY_ALIAS_VAR)
try { keyPassword = getSigningProperty(Constants.KEY_PASS_VAR)
load(file("signing.properties").reader()) }
} catch (_: Exception) { }
load(file("signing_template.properties").reader())
}
}
// try to get secrets from env first for pipeline build, then properties file for local buildTypes {
// build // don't strip
storeFile = packaging.jniLibs.keepDebugSymbols.addAll(
file( listOf("libwg-go.so", "libwg-quick.so", "libwg.so"),
System.getenv() )
.getOrDefault(
Constants.KEY_STORE_PATH_VAR,
properties.getProperty(Constants.KEY_STORE_PATH_VAR),
),
)
storePassword =
System.getenv()
.getOrDefault(
Constants.STORE_PASS_VAR,
properties.getProperty(Constants.STORE_PASS_VAR),
)
keyAlias =
System.getenv()
.getOrDefault(
Constants.KEY_ALIAS_VAR,
properties.getProperty(Constants.KEY_ALIAS_VAR),
)
keyPassword =
System.getenv()
.getOrDefault(
Constants.KEY_PASS_VAR,
properties.getProperty(Constants.KEY_PASS_VAR),
)
}
}
buildTypes { release {
// don't strip isDebuggable = false
packaging.jniLibs.keepDebugSymbols.addAll( isMinifyEnabled = true
listOf("libwg-go.so", "libwg-quick.so", "libwg.so"), isShrinkResources = true
) proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
signingConfig = signingConfigs.getByName(Constants.RELEASE)
}
debug {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
resValue("string", "app_name", "WG Tunnel - Debug")
isDebuggable = true
}
applicationVariants.all { create(Constants.PRERELEASE) {
val variant = this initWith(buildTypes.getByName(Constants.RELEASE))
variant.outputs applicationIdSuffix = ".prerelease"
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl } versionNameSuffix = "-pre"
.forEach { output -> resValue("string", "app_name", "WG Tunnel - Pre")
val outputFileName = }
"${Constants.APP_NAME}-${variant.flavorName}-${variant.buildType.name}-${variant.versionName}.apk"
output.outputFileName = outputFileName create(Constants.NIGHTLY) {
} initWith(buildTypes.getByName(Constants.RELEASE))
} applicationIdSuffix = ".nightly"
release { versionNameSuffix = "-nightly"
isDebuggable = false resValue("string", "app_name", "WG Tunnel - Nightly")
isMinifyEnabled = true }
isShrinkResources = true
proguardFiles( applicationVariants.all {
getDefaultProguardFile("proguard-android-optimize.txt"), val variant = this
"proguard-rules.pro", variant.outputs
) .map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
signingConfig = signingConfigs.getByName(Constants.RELEASE) .forEach { output ->
} val outputFileName =
debug { isDebuggable = true } "${Constants.APP_NAME}-${variant.flavorName}-" +
} "${variant.buildType.name}-${variant.versionName}.apk"
flavorDimensions.add(Constants.TYPE) output.outputFileName = outputFileName
productFlavors { }
create("fdroid") { }
dimension = Constants.TYPE }
proguardFile("fdroid-rules.pro") flavorDimensions.add(Constants.TYPE)
} productFlavors {
create("general") { create("fdroid") {
dimension = Constants.TYPE dimension = Constants.TYPE
if (BuildHelper.isReleaseBuild(gradle) && BuildHelper.isGeneralFlavor(gradle)) { proguardFile("fdroid-rules.pro")
//any plugins general specific }
} create("general") {
} dimension = Constants.TYPE
} }
compileOptions { }
sourceCompatibility = JavaVersion.VERSION_17 compileOptions {
targetCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = true targetCompatibility = JavaVersion.VERSION_17
} isCoreLibraryDesugaringEnabled = true
kotlinOptions { jvmTarget = Constants.JVM_TARGET } }
buildFeatures { kotlinOptions { jvmTarget = Constants.JVM_TARGET }
compose = true buildFeatures {
buildConfig = true compose = true
} buildConfig = true
composeOptions { kotlinCompilerExtensionVersion = Constants.COMPOSE_COMPILER_EXTENSION_VERSION } }
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } } packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
} }
val generalImplementation by configurations val generalImplementation by configurations
dependencies { dependencies {
implementation(project(":logcatter")) implementation(project(":logcatter"))
implementation(libs.androidx.core.ktx) implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.runtime.ktx)
// helpers for implementing LifecycleOwner in a Service // helpers for implementing LifecycleOwner in a Service
implementation(libs.androidx.lifecycle.service) implementation(libs.androidx.lifecycle.service)
implementation(libs.androidx.activity.compose) implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom)) implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui) implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics) implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.material3) implementation(libs.androidx.material3)
implementation(libs.androidx.appcompat) implementation(libs.androidx.appcompat)
// test // test
testImplementation(libs.junit) testImplementation(libs.junit)
testImplementation(libs.androidx.junit) testImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test) androidTestImplementation(libs.androidx.compose.ui.test)
androidTestImplementation(libs.androidx.room.testing) androidTestImplementation(libs.androidx.room.testing)
debugImplementation(libs.androidx.compose.ui.tooling) debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.manifest) debugImplementation(libs.androidx.compose.manifest)
// get tunnel lib from github packages or mavenLocal // get tunnel lib from github packages or mavenLocal
implementation(libs.tunnel) // implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))
implementation(libs.amneziawg.android) implementation(libs.tunnel)
coreLibraryDesugaring(libs.desugar.jdk.libs) implementation(libs.amneziawg.android)
coreLibraryDesugaring(libs.desugar.jdk.libs)
// logging // logging
implementation(libs.timber) implementation(libs.timber)
// compose navigation
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.hilt.navigation.compose)
// compose navigation implementation(libs.zaneschepke.multifab)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.zaneschepke.multifab) // hilt
implementation(libs.hilt.android)
ksp(libs.hilt.android.compiler)
// hilt // accompanist
implementation(libs.hilt.android) implementation(libs.accompanist.permissions)
ksp(libs.hilt.android.compiler) implementation(libs.accompanist.flowlayout)
implementation(libs.accompanist.drawablepainter)
// accompanist // storage
implementation(libs.accompanist.permissions) implementation(libs.androidx.room.runtime)
implementation(libs.accompanist.flowlayout) ksp(libs.androidx.room.compiler)
implementation(libs.accompanist.drawablepainter) implementation(libs.androidx.room.ktx)
implementation(libs.androidx.datastore.preferences)
// storage // lifecycle
implementation(libs.androidx.room.runtime) implementation(libs.lifecycle.runtime.compose)
ksp(libs.androidx.room.compiler) implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.room.ktx) implementation(libs.androidx.lifecycle.process)
implementation(libs.androidx.datastore.preferences)
// lifecycle // icons
implementation(libs.lifecycle.runtime.compose) implementation(libs.material.icons.extended)
implementation(libs.androidx.lifecycle.runtime.ktx) // serialization
implementation(libs.androidx.lifecycle.process) implementation(libs.kotlinx.serialization.json)
// icons // barcode scanning
implementation(libs.material.icons.extended) implementation(libs.zxing.android.embedded)
// serialization
implementation(libs.kotlinx.serialization.json)
// barcode scanning // bio
implementation(libs.zxing.android.embedded) implementation(libs.androidx.biometric.ktx)
implementation(libs.zxing.core) implementation(libs.pin.lock.compose)
// bio // shortcuts
implementation(libs.androidx.biometric.ktx) implementation(libs.androidx.core)
implementation(libs.pin.lock.compose) implementation(libs.androidx.core.google.shortcuts)
// shortcuts // splash
implementation(libs.androidx.core) implementation(libs.androidx.core.splashscreen)
implementation(libs.androidx.core.google.shortcuts) }
fun determineVersionCode(): Int {
return with(getBuildTaskName().lowercase()) {
when {
contains(Constants.NIGHTLY) -> Constants.VERSION_CODE + Constants.NIGHTLY_CODE
contains(Constants.PRERELEASE) -> Constants.VERSION_CODE + Constants.PRERELEASE_CODE
else -> Constants.VERSION_CODE
}
}
}
fun determineVersionName(): String {
return with(getBuildTaskName().lowercase()) {
when {
contains(Constants.NIGHTLY) || contains(Constants.PRERELEASE) ->
Constants.VERSION_NAME +
"-${grgitService.service.get().grgit.head().abbreviatedId}"
else -> Constants.VERSION_NAME
}
}
} }
+1 -1
View File
@@ -2,4 +2,4 @@
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite { -keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
<fields>; <fields>;
} }
+1 -3
View File
@@ -21,6 +21,4 @@
#-renamesourcefileattribute SourceFile #-renamesourcefileattribute SourceFile
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite { -keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
<fields>; <fields>;
} }
@@ -0,0 +1,197 @@
{
"formatVersion": 1,
"database": {
"version": 9,
"identityHash": "e2c91dbf1885a9da592d3f54f1e08302",
"entities": [
{
"tableName": "Settings",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `is_tunnel_enabled` INTEGER NOT NULL, `is_tunnel_on_mobile_data_enabled` INTEGER NOT NULL, `trusted_network_ssids` TEXT NOT NULL, `is_always_on_vpn_enabled` INTEGER NOT NULL, `is_tunnel_on_ethernet_enabled` INTEGER NOT NULL, `is_shortcuts_enabled` INTEGER NOT NULL DEFAULT false, `is_tunnel_on_wifi_enabled` INTEGER NOT NULL DEFAULT false, `is_kernel_enabled` INTEGER NOT NULL DEFAULT false, `is_restore_on_boot_enabled` INTEGER NOT NULL DEFAULT false, `is_multi_tunnel_enabled` INTEGER NOT NULL DEFAULT false, `is_auto_tunnel_paused` INTEGER NOT NULL DEFAULT false, `is_ping_enabled` INTEGER NOT NULL DEFAULT false, `is_amnezia_enabled` INTEGER NOT NULL DEFAULT false)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isAutoTunnelEnabled",
"columnName": "is_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isTunnelOnMobileDataEnabled",
"columnName": "is_tunnel_on_mobile_data_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "trustedNetworkSSIDs",
"columnName": "trusted_network_ssids",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "isAlwaysOnVpnEnabled",
"columnName": "is_always_on_vpn_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isTunnelOnEthernetEnabled",
"columnName": "is_tunnel_on_ethernet_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isShortcutsEnabled",
"columnName": "is_shortcuts_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isTunnelOnWifiEnabled",
"columnName": "is_tunnel_on_wifi_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isKernelEnabled",
"columnName": "is_kernel_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isRestoreOnBootEnabled",
"columnName": "is_restore_on_boot_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isMultiTunnelEnabled",
"columnName": "is_multi_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isAutoTunnelPaused",
"columnName": "is_auto_tunnel_paused",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isPingEnabled",
"columnName": "is_ping_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isAmneziaEnabled",
"columnName": "is_amnezia_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "TunnelConfig",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `wg_quick` TEXT NOT NULL, `tunnel_networks` TEXT NOT NULL DEFAULT '', `is_mobile_data_tunnel` INTEGER NOT NULL DEFAULT false, `is_primary_tunnel` INTEGER NOT NULL DEFAULT false, `am_quick` TEXT NOT NULL DEFAULT '', `is_Active` INTEGER NOT NULL DEFAULT false)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "wgQuick",
"columnName": "wg_quick",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "tunnelNetworks",
"columnName": "tunnel_networks",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isMobileDataTunnel",
"columnName": "is_mobile_data_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isPrimaryTunnel",
"columnName": "is_primary_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "amQuick",
"columnName": "am_quick",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isActive",
"columnName": "is_Active",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_TunnelConfig_name",
"unique": true,
"columnNames": [
"name"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_TunnelConfig_name` ON `${TABLE_NAME}` (`name`)"
}
],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'e2c91dbf1885a9da592d3f54f1e08302')"
]
}
}
@@ -13,10 +13,10 @@ import org.junit.runner.RunWith
*/ */
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest { class ExampleInstrumentedTest {
@Test @Test
fun useAppContext() { fun useAppContext() {
// Context of the app under test. // Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName) assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName)
} }
} }
@@ -12,33 +12,33 @@ import java.io.IOException
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class MigrationTest { class MigrationTest {
private val dbName = "migration-test" private val dbName = "migration-test"
@get:Rule @get:Rule
val helper: MigrationTestHelper = val helper: MigrationTestHelper =
MigrationTestHelper( MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(), InstrumentationRegistry.getInstrumentation(),
AppDatabase::class.java, AppDatabase::class.java,
) )
@Test @Test
@Throws(IOException::class) @Throws(IOException::class)
fun migrate6To7() { fun migrate6To7() {
helper.createDatabase(dbName, 6).apply { helper.createDatabase(dbName, 6).apply {
// Database has schema version 1. Insert some data using SQL queries. // Database has schema version 1. Insert some data using SQL queries.
// You can't use DAO classes because they expect the latest schema. // You can't use DAO classes because they expect the latest schema.
execSQL(Queries.createDefaultSettings()) execSQL(Queries.createDefaultSettings())
execSQL( execSQL(
Queries.createTunnelConfig(), Queries.createTunnelConfig(),
) )
// Prepare for the next version. // Prepare for the next version.
close() close()
} }
// Re-open the database with version 2 and provide // Re-open the database with version 2 and provide
// MIGRATION_1_2 as the migration process. // MIGRATION_1_2 as the migration process.
helper.runMigrationsAndValidate(dbName, 7, true) helper.runMigrationsAndValidate(dbName, 7, true)
// MigrationTestHelper automatically verifies the schema changes, // MigrationTestHelper automatically verifies the schema changes,
// but you need to validate that the data was migrated properly. // but you need to validate that the data was migrated properly.
} }
} }
+69 -32
View File
@@ -31,6 +31,12 @@
<!--start service on boot permission--> <!--start service on boot permission-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--android tv support--> <!--android tv support-->
<permission
android:name="${applicationId}.permission.CONTROL_TUNNELS"
android:icon="@mipmap/ic_launcher"
android:protectionLevel="dangerous" />
<uses-feature <uses-feature
android:name="android.software.leanback" android:name="android.software.leanback"
android:required="false" /> android:required="false" />
@@ -51,7 +57,7 @@
</queries> </queries>
<application <application
android:name=".WireGuardAutoTunnel" android:name=".WireGuardAutoTunnel"
android:allowBackup="true" android:allowBackup="false"
android:banner="@drawable/ic_banner" android:banner="@drawable/ic_banner"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:enableOnBackInvokedCallback="true" android:enableOnBackInvokedCallback="true"
@@ -60,18 +66,17 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.WireguardAutoTunnel" android:theme="@style/Theme.AppSplashScreen"
tools:targetApi="tiramisu"> tools:targetApi="tiramisu">
<activity <activity
android:name=".ui.MainActivity" android:name=".ui.SplashActivity"
android:exported="true" android:exported="true"
android:theme="@style/Theme.WireguardAutoTunnel"> android:theme="@style/Theme.AppSplashScreen">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
<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 <meta-data
@@ -79,17 +84,23 @@
android:resource="@xml/shortcuts" /> android:resource="@xml/shortcuts" />
</activity> </activity>
<activity <activity
android:name=".ui.CaptureActivityPortrait" android:name=".ui.MainActivity"
android:screenOrientation="fullSensor" android:exported="true"
android:stateNotNeeded="true" android:theme="@style/Theme.WireguardAutoTunnel">
android:theme="@style/zxing_CaptureTheme" </activity>
android:windowSoftInputMode="stateAlwaysHidden" <activity
tools:ignore="DiscouragedApi" /> android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="screenOrientation" />
<activity <activity
android:name=".service.shortcut.ShortcutsActivity" android:name=".service.shortcut.ShortcutsActivity"
android:enabled="true" android:enabled="true"
android:exported="true" android:exported="true"
android:finishOnTaskLaunch="true" android:noHistory="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.NoDisplay" /> android:theme="@android:style/Theme.NoDisplay" />
<service <service
@@ -132,23 +143,23 @@
<action android:name="android.service.quicksettings.action.QS_TILE" /> <action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter> </intent-filter>
</service> </service>
<service
android:name=".service.tunnel.AlwaysOnVpnService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="systemExempted"
android:permission="android.permission.BIND_VPN_SERVICE"
android:persistent="true"
tools:node="merge">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
<meta-data
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
android:value="true" />
</service>
<service <service
android:name=".service.foreground.WireGuardTunnelService" android:name=".service.foreground.AutoTunnelService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="systemExempted"
android:permission="android.permission.BIND_VPN_SERVICE"
android:persistent="true"
tools:node="merge">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
<meta-data
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
android:value="true" />
</service>
<service
android:name=".service.foreground.WireGuardConnectivityWatcherService"
android:enabled="true" android:enabled="true"
android:exported="false" android:exported="false"
android:foregroundServiceType="systemExempted" android:foregroundServiceType="systemExempted"
@@ -156,6 +167,16 @@
android:stopWithTask="false" android:stopWithTask="false"
tools:node="merge" /> tools:node="merge" />
<service
android:name=".service.foreground.TunnelBackgroundService"
android:exported="false"
android:foregroundServiceType="systemExempted"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
</service>
<receiver <receiver
android:name=".receiver.BootReceiver" android:name=".receiver.BootReceiver"
android:enabled="true" android:enabled="true"
@@ -169,8 +190,24 @@
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver <receiver
android:name=".receiver.NotificationActionReceiver" android:name=".receiver.BackgroundActionReceiver"
android:exported="false" /> android:enabled="true"
android:exported="false"/>
<receiver
android:name=".receiver.AppUpdateReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.KernelReceiver"
android:exported="false"
android:permission="${applicationId}.permission.CONTROL_TUNNELS">
<intent-filter>
<action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" />
</intent-filter>
</receiver>
</application> </application>
</manifest> </manifest>
@@ -1,56 +1,59 @@
package com.zaneschepke.wireguardautotunnel package com.zaneschepke.wireguardautotunnel
import android.app.Application import android.app.Application
import android.content.ComponentName import android.os.StrictMode
import android.content.pm.PackageManager import android.os.StrictMode.ThreadPolicy
import android.service.quicksettings.TileService import com.zaneschepke.logcatter.LocalLogCollector
import com.zaneschepke.wireguardautotunnel.service.tile.AutoTunnelControlTile import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.tile.TunnelControlTile import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.MainScope import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.cancel import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import xyz.teamgravity.pin_lock_compose.PinManager import javax.inject.Inject
@HiltAndroidApp @HiltAndroidApp
class WireGuardAutoTunnel : Application() { class WireGuardAutoTunnel : Application() {
override fun onCreate() {
super.onCreate()
instance = this
if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree()) else Timber.plant(ReleaseTree())
PinManager.initialize(this)
}
override fun onLowMemory() { @Inject
super.onLowMemory() @ApplicationScope
applicationScope.cancel("onLowMemory() called by system") lateinit var applicationScope: CoroutineScope
applicationScope = MainScope()
}
companion object { @Inject
lateinit var localLogCollector: LocalLogCollector
var applicationScope = MainScope() @Inject
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
lateinit var instance: WireGuardAutoTunnel override fun onCreate() {
private set super.onCreate()
instance = this
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
StrictMode.setThreadPolicy(
ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build(),
)
} else {
Timber.plant(ReleaseTree())
}
if (!isRunningOnTv()) {
applicationScope.launch(ioDispatcher) {
localLogCollector.start()
}
}
}
fun isRunningOnAndroidTv(): Boolean { companion object {
return instance.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK) lateinit var instance: WireGuardAutoTunnel
} private set
}
fun requestTunnelTileServiceStateUpdate() {
TileService.requestListeningState(
instance,
ComponentName(instance, TunnelControlTile::class.java),
)
}
fun requestAutoTunnelTileServiceUpdate() {
TileService.requestListeningState(
instance,
ComponentName(instance, AutoTunnelControlTile::class.java),
)
}
}
} }
@@ -10,46 +10,47 @@ import com.zaneschepke.wireguardautotunnel.data.domain.Settings
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
@Database( @Database(
entities = [Settings::class, TunnelConfig::class], entities = [Settings::class, TunnelConfig::class],
version = 8, version = 9,
autoMigrations = autoMigrations =
[ [
AutoMigration(from = 1, to = 2), AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3), AutoMigration(from = 2, to = 3),
AutoMigration( AutoMigration(
from = 3, from = 3,
to = 4, to = 4,
), ),
AutoMigration( AutoMigration(
from = 4, from = 4,
to = 5, to = 5,
), ),
AutoMigration( AutoMigration(
from = 5, from = 5,
to = 6, to = 6,
), ),
AutoMigration( AutoMigration(
from = 6, from = 6,
to = 7, to = 7,
spec = RemoveLegacySettingColumnsMigration::class, spec = RemoveLegacySettingColumnsMigration::class,
), ),
AutoMigration(7, 8) AutoMigration(7, 8),
], AutoMigration(8, 9),
exportSchema = true, ],
exportSchema = true,
) )
@TypeConverters(DatabaseListConverters::class) @TypeConverters(DatabaseListConverters::class)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
abstract fun settingDao(): SettingsDao abstract fun settingDao(): SettingsDao
abstract fun tunnelConfigDoa(): TunnelConfigDao abstract fun tunnelConfigDoa(): TunnelConfigDao
} }
@DeleteColumn( @DeleteColumn(
tableName = "Settings", tableName = "Settings",
columnName = "default_tunnel", columnName = "default_tunnel",
) )
@DeleteColumn( @DeleteColumn(
tableName = "Settings", tableName = "Settings",
columnName = "is_battery_saver_enabled", columnName = "is_battery_saver_enabled",
) )
class RemoveLegacySettingColumnsMigration : AutoMigrationSpec class RemoveLegacySettingColumnsMigration : AutoMigrationSpec
@@ -5,17 +5,17 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import timber.log.Timber import timber.log.Timber
class DatabaseCallback : RoomDatabase.Callback() { class DatabaseCallback : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) = db.run { override fun onCreate(db: SupportSQLiteDatabase) = db.run {
// Notice non-ui thread is here // Notice non-ui thread is here
beginTransaction() beginTransaction()
try { try {
execSQL(Queries.createDefaultSettings()) execSQL(Queries.createDefaultSettings())
Timber.i("Bootstrapping settings data") Timber.i("Bootstrapping settings data")
setTransactionSuccessful() setTransactionSuccessful()
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
} finally { } finally {
endTransaction() endTransaction()
} }
} }
} }
@@ -5,20 +5,20 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class DatabaseListConverters { class DatabaseListConverters {
@TypeConverter @TypeConverter
fun listToString(value: MutableList<String>): String { fun listToString(value: MutableList<String>): String {
return Json.encodeToString(value) return Json.encodeToString(value)
} }
@TypeConverter @TypeConverter
fun stringToList(value: String): MutableList<String> { fun stringToList(value: String): MutableList<String> {
if (value.isBlank() || value.isEmpty()) return mutableListOf() if (value.isBlank() || value.isEmpty()) return mutableListOf()
return try { return try {
Json.decodeFromString<MutableList<String>>(value) Json.decodeFromString<MutableList<String>>(value)
} catch (e: Exception) { } catch (e: Exception) {
val list = value.split(",").toMutableList() val list = value.split(",").toMutableList()
val json = listToString(list) val json = listToString(list)
Json.decodeFromString<MutableList<String>>(json) Json.decodeFromString<MutableList<String>>(json)
} }
} }
} }
@@ -1,35 +1,35 @@
package com.zaneschepke.wireguardautotunnel.data package com.zaneschepke.wireguardautotunnel.data
object Queries { object Queries {
fun createDefaultSettings(): String { fun createDefaultSettings(): String {
return """ return """
INSERT INTO Settings (is_tunnel_enabled, INSERT INTO Settings (is_tunnel_enabled,
is_tunnel_on_mobile_data_enabled, is_tunnel_on_mobile_data_enabled,
trusted_network_ssids, trusted_network_ssids,
is_always_on_vpn_enabled, is_always_on_vpn_enabled,
is_tunnel_on_ethernet_enabled, is_tunnel_on_ethernet_enabled,
is_shortcuts_enabled, is_shortcuts_enabled,
is_tunnel_on_wifi_enabled, is_tunnel_on_wifi_enabled,
is_kernel_enabled, is_kernel_enabled,
is_restore_on_boot_enabled, is_restore_on_boot_enabled,
is_multi_tunnel_enabled) is_multi_tunnel_enabled)
VALUES VALUES
('false', ('false',
'false', 'false',
'sampleSSID1,sampleSSID2', 'sampleSSID1,sampleSSID2',
'false', 'false',
'false', 'false',
'false', 'false',
'false', 'false',
'false', 'false',
'false', 'false',
'false') 'false')
""".trimIndent() """.trimIndent()
} }
fun createTunnelConfig(): String { fun createTunnelConfig(): String {
return """ return """
INSERT INTO TunnelConfig (name, wg_quick) VALUES ('test', 'test') INSERT INTO TunnelConfig (name, wg_quick) VALUES ('test', 'test')
""".trimIndent() """.trimIndent()
} }
} }
@@ -10,27 +10,27 @@ import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface SettingsDao { interface SettingsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun save(t: Settings) suspend fun save(t: Settings)
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveAll(t: List<Settings>) suspend fun saveAll(t: List<Settings>)
@Query("SELECT * FROM settings WHERE id=:id") @Query("SELECT * FROM settings WHERE id=:id")
suspend fun getById(id: Long): Settings? suspend fun getById(id: Long): Settings?
@Query("SELECT * FROM settings") @Query("SELECT * FROM settings")
suspend fun getAll(): List<Settings> suspend fun getAll(): List<Settings>
@Query("SELECT * FROM settings LIMIT 1") @Query("SELECT * FROM settings LIMIT 1")
fun getSettingsFlow(): Flow<Settings> fun getSettingsFlow(): Flow<Settings>
@Query("SELECT * FROM settings") @Query("SELECT * FROM settings")
fun getAllFlow(): Flow<MutableList<Settings>> fun getAllFlow(): Flow<MutableList<Settings>>
@Delete @Delete
suspend fun delete(t: Settings) suspend fun delete(t: Settings)
@Query("SELECT COUNT('id') FROM settings") @Query("SELECT COUNT('id') FROM settings")
suspend fun count(): Long suspend fun count(): Long
} }
@@ -6,47 +6,50 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface TunnelConfigDao { interface TunnelConfigDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun save(t: TunnelConfig) suspend fun save(t: TunnelConfig)
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveAll(t: TunnelConfigs) suspend fun saveAll(t: TunnelConfigs)
@Query("SELECT * FROM TunnelConfig WHERE id=:id") @Query("SELECT * FROM TunnelConfig WHERE id=:id")
suspend fun getById(id: Long): TunnelConfig? suspend fun getById(id: Long): TunnelConfig?
@Query("SELECT * FROM TunnelConfig WHERE name=:name") @Query("SELECT * FROM TunnelConfig WHERE name=:name")
suspend fun getByName(name: String) : TunnelConfig? suspend fun getByName(name: String): TunnelConfig?
@Query("SELECT * FROM TunnelConfig") @Query("SELECT * FROM TunnelConfig WHERE is_Active=1")
suspend fun getAll(): TunnelConfigs suspend fun getActive(): TunnelConfigs
@Delete @Query("SELECT * FROM TunnelConfig")
suspend fun delete(t: TunnelConfig) suspend fun getAll(): TunnelConfigs
@Query("SELECT COUNT('id') FROM TunnelConfig") @Delete
suspend fun count(): Long suspend fun delete(t: TunnelConfig)
@Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'") @Query("SELECT COUNT('id') FROM TunnelConfig")
suspend fun findByTunnelNetworkName(name: String): TunnelConfigs suspend fun count(): Long
@Query("UPDATE TunnelConfig SET is_primary_tunnel = 0 WHERE is_primary_tunnel =1") @Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'")
fun resetPrimaryTunnel() suspend fun findByTunnelNetworkName(name: String): TunnelConfigs
@Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1") @Query("UPDATE TunnelConfig SET is_primary_tunnel = 0 WHERE is_primary_tunnel =1")
fun resetMobileDataTunnel() suspend fun resetPrimaryTunnel()
@Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1") @Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1")
suspend fun findByPrimary(): TunnelConfigs suspend fun resetMobileDataTunnel()
@Query("SELECT * FROM TUNNELCONFIG WHERE is_mobile_data_tunnel=1") @Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1")
suspend fun findByMobileDataTunnel(): TunnelConfigs suspend fun findByPrimary(): TunnelConfigs
@Query("SELECT * FROM tunnelconfig") @Query("SELECT * FROM TUNNELCONFIG WHERE is_mobile_data_tunnel=1")
fun getAllFlow(): Flow<MutableList<TunnelConfig>> suspend fun findByMobileDataTunnel(): TunnelConfigs
@Query("SELECT * FROM tunnelconfig")
fun getAllFlow(): Flow<MutableList<TunnelConfig>>
} }
@@ -7,64 +7,73 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore import androidx.datastore.preferences.preferencesDataStore
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
class DataStoreManager(private val context: Context) { class DataStoreManager(
companion object { private val context: Context,
val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN") @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
val BATTERY_OPTIMIZE_DISABLE_SHOWN = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN") ) {
val TUNNEL_RUNNING_FROM_MANUAL_START = companion object {
booleanPreferencesKey("TUNNEL_RUNNING_FROM_MANUAL_START") val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
val ACTIVE_TUNNEL = intPreferencesKey("ACTIVE_TUNNEL") val BATTERY_OPTIMIZE_DISABLE_SHOWN = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
val CURRENT_SSID = stringPreferencesKey("CURRENT_SSID") val LAST_ACTIVE_TUNNEL = intPreferencesKey("LAST_ACTIVE_TUNNEL")
} val CURRENT_SSID = stringPreferencesKey("CURRENT_SSID")
val IS_PIN_LOCK_ENABLED = booleanPreferencesKey("PIN_LOCK_ENABLED")
}
// preferences // preferences
private val preferencesKey = "preferences" private val preferencesKey = "preferences"
private val Context.dataStore by private val Context.dataStore by
preferencesDataStore( preferencesDataStore(
name = preferencesKey, name = preferencesKey,
) )
suspend fun init() { suspend fun init() {
try { withContext(ioDispatcher) {
context.dataStore.data.first() try {
} catch (e: IOException) { context.dataStore.data.first()
Timber.e(e) } catch (e: IOException) {
} Timber.e(e)
} }
}
}
suspend fun <T> saveToDataStore(key: Preferences.Key<T>, value: T) { suspend fun <T> saveToDataStore(key: Preferences.Key<T>, value: T) {
try { withContext(ioDispatcher) {
context.dataStore.edit { it[key] = value } try {
} catch (e: IOException) { context.dataStore.edit { it[key] = value }
Timber.e(e) } catch (e: IOException) {
} catch (e: Exception) { Timber.e(e)
Timber.e(e) } catch (e: Exception) {
} Timber.e(e)
} }
}
}
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] }
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] } suspend fun <T> getFromStore(key: Preferences.Key<T>): T? {
return withContext(ioDispatcher) {
try {
context.dataStore.data.map { it[key] }.first()
} catch (e: IOException) {
Timber.e(e)
null
}
}
}
suspend fun <T> getFromStore(key: Preferences.Key<T>): T? { fun <T> getFromStoreBlocking(key: Preferences.Key<T>) = runBlocking {
return try { context.dataStore.data.map { it[key] }.first()
context.dataStore.data.map { it[key] }.first() }
} catch (e: IOException) {
Timber.e(e)
null
}
}
val preferencesFlow: Flow<Preferences?> = context.dataStore.data
fun <T> getFromStoreBlocking(key: Preferences.Key<T>) = runBlocking {
context.dataStore.data.map { it[key] }.first()
}
val preferencesFlow: Flow<Preferences?> = context.dataStore.data
} }
@@ -1,14 +1,14 @@
package com.zaneschepke.wireguardautotunnel.data.domain package com.zaneschepke.wireguardautotunnel.data.domain
data class GeneralState( data class GeneralState(
val locationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT, val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT,
val batteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT, val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
val tunnelRunningFromManualStart: Boolean = TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT, val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
val activeTunnelId: Int? = null val lastActiveTunnelId: Int? = null,
) { ) {
companion object { companion object {
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false
const val TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT = false const val PIN_LOCK_ENABLED_DEFAULT = false
} }
} }
@@ -6,53 +6,53 @@ import androidx.room.PrimaryKey
@Entity @Entity
data class Settings( data class Settings(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "is_tunnel_enabled") val isAutoTunnelEnabled: Boolean = false, @ColumnInfo(name = "is_tunnel_enabled") val isAutoTunnelEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled") @ColumnInfo(name = "is_tunnel_on_mobile_data_enabled")
val isTunnelOnMobileDataEnabled: Boolean = false, val isTunnelOnMobileDataEnabled: Boolean = false,
@ColumnInfo(name = "trusted_network_ssids") @ColumnInfo(name = "trusted_network_ssids")
val trustedNetworkSSIDs: MutableList<String> = mutableListOf(), val trustedNetworkSSIDs: MutableList<String> = mutableListOf(),
@ColumnInfo(name = "is_always_on_vpn_enabled") val isAlwaysOnVpnEnabled: Boolean = false, @ColumnInfo(name = "is_always_on_vpn_enabled") val isAlwaysOnVpnEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled") @ColumnInfo(name = "is_tunnel_on_ethernet_enabled")
val isTunnelOnEthernetEnabled: Boolean = false, val isTunnelOnEthernetEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_shortcuts_enabled", name = "is_shortcuts_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isShortcutsEnabled: Boolean = false, val isShortcutsEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_tunnel_on_wifi_enabled", name = "is_tunnel_on_wifi_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isTunnelOnWifiEnabled: Boolean = false, val isTunnelOnWifiEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_kernel_enabled", name = "is_kernel_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isKernelEnabled: Boolean = false, val isKernelEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_restore_on_boot_enabled", name = "is_restore_on_boot_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isRestoreOnBootEnabled: Boolean = false, val isRestoreOnBootEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_multi_tunnel_enabled", name = "is_multi_tunnel_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isMultiTunnelEnabled: Boolean = false, val isMultiTunnelEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_auto_tunnel_paused", name = "is_auto_tunnel_paused",
defaultValue = "false", defaultValue = "false",
) )
val isAutoTunnelPaused: Boolean = false, val isAutoTunnelPaused: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_ping_enabled", name = "is_ping_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isPingEnabled: Boolean = false, val isPingEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_amnezia_enabled", name = "is_amnezia_enabled",
defaultValue = "false", defaultValue = "false",
) )
val isAmneziaEnabled: Boolean = false, val isAmneziaEnabled: Boolean = false,
) )
@@ -9,43 +9,54 @@ import java.io.InputStream
@Entity(indices = [Index(value = ["name"], unique = true)]) @Entity(indices = [Index(value = ["name"], unique = true)])
data class TunnelConfig( data class TunnelConfig(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "name") val name: String, @ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "wg_quick") val wgQuick: String, @ColumnInfo(name = "wg_quick") val wgQuick: String,
@ColumnInfo( @ColumnInfo(
name = "tunnel_networks", name = "tunnel_networks",
defaultValue = "", defaultValue = "",
) )
val tunnelNetworks: MutableList<String> = mutableListOf(), val tunnelNetworks: MutableList<String> = mutableListOf(),
@ColumnInfo( @ColumnInfo(
name = "is_mobile_data_tunnel", name = "is_mobile_data_tunnel",
defaultValue = "false", defaultValue = "false",
) )
val isMobileDataTunnel: Boolean = false, val isMobileDataTunnel: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "is_primary_tunnel", name = "is_primary_tunnel",
defaultValue = "false", defaultValue = "false",
) )
val isPrimaryTunnel: Boolean = false, val isPrimaryTunnel: Boolean = false,
@ColumnInfo( @ColumnInfo(
name = "am_quick", name = "am_quick",
defaultValue = "", defaultValue = "",
) )
val amQuick: String = AM_QUICK_DEFAULT, val amQuick: String = AM_QUICK_DEFAULT,
@ColumnInfo(
name = "is_Active",
defaultValue = "false",
)
val isActive: Boolean = false,
) { ) {
companion object { companion object {
fun configFromWgQuick(wgQuick: String): Config { fun findDefault(tunnels: List<TunnelConfig>): TunnelConfig? {
val inputStream: InputStream = wgQuick.byteInputStream() return tunnels.find { it.isPrimaryTunnel } ?: tunnels.firstOrNull()
return inputStream.bufferedReader(Charsets.UTF_8).use { }
Config.parse(it)
} fun configFromWgQuick(wgQuick: String): Config {
} val inputStream: InputStream = wgQuick.byteInputStream()
fun configFromAmQuick(amQuick: String) : org.amnezia.awg.config.Config { return inputStream.bufferedReader(Charsets.UTF_8).use {
val inputStream: InputStream = amQuick.byteInputStream() Config.parse(it)
return inputStream.bufferedReader(Charsets.UTF_8).use { }
org.amnezia.awg.config.Config.parse(it) }
}
} fun configFromAmQuick(amQuick: String): org.amnezia.awg.config.Config {
const val AM_QUICK_DEFAULT = "" val inputStream: InputStream = amQuick.byteInputStream()
} return inputStream.bufferedReader(Charsets.UTF_8).use {
org.amnezia.awg.config.Config.parse(it)
}
}
const val AM_QUICK_DEFAULT = ""
}
} }
@@ -3,12 +3,11 @@ package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
interface AppDataRepository { interface AppDataRepository {
suspend fun getPrimaryOrFirstTunnel(): TunnelConfig? suspend fun getPrimaryOrFirstTunnel(): TunnelConfig?
suspend fun getStartTunnelConfig(): TunnelConfig?
suspend fun toggleWatcherServicePause() suspend fun getStartTunnelConfig(): TunnelConfig?
val settings: SettingsRepository val settings: SettingsRepository
val tunnels: TunnelConfigRepository val tunnels: TunnelConfigRepository
val appState: AppStateRepository val appState: AppStateRepository
} }
@@ -3,32 +3,20 @@ package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import javax.inject.Inject import javax.inject.Inject
class AppDataRoomRepository @Inject constructor( class AppDataRoomRepository
override val settings: SettingsRepository, @Inject
override val tunnels: TunnelConfigRepository, constructor(
override val appState: AppStateRepository override val settings: SettingsRepository,
override val tunnels: TunnelConfigRepository,
override val appState: AppStateRepository,
) : AppDataRepository { ) : AppDataRepository {
override suspend fun getPrimaryOrFirstTunnel(): TunnelConfig? { override suspend fun getPrimaryOrFirstTunnel(): TunnelConfig? {
return tunnels.findPrimary().firstOrNull() ?: tunnels.getAll().firstOrNull() return tunnels.findPrimary().firstOrNull() ?: tunnels.getAll().firstOrNull()
} }
override suspend fun getStartTunnelConfig(): TunnelConfig? { override suspend fun getStartTunnelConfig(): TunnelConfig? {
return if (appState.isTunnelRunningFromManualStart()) { return appState.getLastActiveTunnelId()?.let {
appState.getActiveTunnelId()?.let { tunnels.getById(it)
tunnels.getById(it) } ?: getPrimaryOrFirstTunnel()
} }
} else null
}
override suspend fun toggleWatcherServicePause() {
val settings = settings.getSettings()
if (settings.isAutoTunnelEnabled) {
val pauseAutoTunnel = !settings.isAutoTunnelPaused
this.settings.save(
settings.copy(
isAutoTunnelPaused = pauseAutoTunnel,
),
)
}
}
} }
@@ -4,23 +4,25 @@ import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface AppStateRepository { interface AppStateRepository {
suspend fun isLocationDisclosureShown(): Boolean suspend fun isLocationDisclosureShown(): Boolean
suspend fun setLocationDisclosureShown(shown: Boolean)
suspend fun isBatteryOptimizationDisableShown(): Boolean suspend fun setLocationDisclosureShown(shown: Boolean)
suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
suspend fun isTunnelRunningFromManualStart(): Boolean suspend fun isPinLockEnabled(): Boolean
suspend fun setTunnelRunningFromManualStart(id: Int)
suspend fun setManualStop() suspend fun setPinLockEnabled(enabled: Boolean)
suspend fun getActiveTunnelId(): Int? suspend fun isBatteryOptimizationDisableShown(): Boolean
suspend fun getCurrentSsid(): String? suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
suspend fun setCurrentSsid(ssid: String) suspend fun getLastActiveTunnelId(): Int?
val generalStateFlow: Flow<GeneralState> suspend fun setLastActiveTunnelId(id: Int)
suspend fun getCurrentSsid(): String?
suspend fun setCurrentSsid(ssid: String)
val generalStateFlow: Flow<GeneralState>
} }
@@ -2,80 +2,87 @@ package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState import com.zaneschepke.wireguardautotunnel.data.domain.GeneralState
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
class DataStoreAppStateRepository(private val dataStoreManager: DataStoreManager) : class DataStoreAppStateRepository(
AppStateRepository { private val dataStoreManager: DataStoreManager,
override suspend fun isLocationDisclosureShown(): Boolean { @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
return dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN) ) :
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT AppStateRepository {
} override suspend fun isLocationDisclosureShown(): Boolean {
return withContext(ioDispatcher) {
dataStoreManager.getFromStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN)
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
}
}
override suspend fun setLocationDisclosureShown(shown: Boolean) { override suspend fun setLocationDisclosureShown(shown: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, shown) withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, shown) }
} }
override suspend fun isBatteryOptimizationDisableShown(): Boolean { override suspend fun isPinLockEnabled(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN) return withContext(ioDispatcher) {
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT dataStoreManager.getFromStore(DataStoreManager.IS_PIN_LOCK_ENABLED)
} ?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
}
}
override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) { override suspend fun setPinLockEnabled(enabled: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown) withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.IS_PIN_LOCK_ENABLED, enabled) }
} }
override suspend fun isTunnelRunningFromManualStart(): Boolean { override suspend fun isBatteryOptimizationDisableShown(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START) return withContext(ioDispatcher) {
?: GeneralState.TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT dataStoreManager.getFromStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN)
} ?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
}
}
override suspend fun setTunnelRunningFromManualStart(id: Int) { override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) {
setTunnelRunningFromManualStart(true) withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN, shown) }
setActiveTunnelId(id) }
}
override suspend fun setManualStop() { override suspend fun getLastActiveTunnelId(): Int? {
setTunnelRunningFromManualStart(false) return withContext(ioDispatcher) { dataStoreManager.getFromStore(DataStoreManager.LAST_ACTIVE_TUNNEL) }
} }
private suspend fun setTunnelRunningFromManualStart(running: Boolean) { override suspend fun setLastActiveTunnelId(id: Int) {
dataStoreManager.saveToDataStore(DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START, running) return withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.LAST_ACTIVE_TUNNEL, id) }
} }
override suspend fun getActiveTunnelId(): Int? { override suspend fun getCurrentSsid(): String? {
return dataStoreManager.getFromStore(DataStoreManager.ACTIVE_TUNNEL) return withContext(ioDispatcher) { dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID) }
} }
private suspend fun setActiveTunnelId(id: Int) { override suspend fun setCurrentSsid(ssid: String) {
dataStoreManager.saveToDataStore(DataStoreManager.ACTIVE_TUNNEL, id) withContext(ioDispatcher) { dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid) }
} }
override suspend fun getCurrentSsid(): String? { override val generalStateFlow: Flow<GeneralState> =
return dataStoreManager.getFromStore(DataStoreManager.CURRENT_SSID) dataStoreManager.preferencesFlow.map { prefs ->
} prefs?.let { pref ->
try {
override suspend fun setCurrentSsid(ssid: String) { GeneralState(
dataStoreManager.saveToDataStore(DataStoreManager.CURRENT_SSID, ssid) isLocationDisclosureShown =
} pref[DataStoreManager.LOCATION_DISCLOSURE_SHOWN]
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
override val generalStateFlow: Flow<GeneralState> = isBatteryOptimizationDisableShown =
dataStoreManager.preferencesFlow.map { prefs -> pref[DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN]
prefs?.let { pref -> ?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
try { isPinLockEnabled =
GeneralState( pref[DataStoreManager.IS_PIN_LOCK_ENABLED]
locationDisclosureShown = pref[DataStoreManager.LOCATION_DISCLOSURE_SHOWN] ?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT, lastActiveTunnelId = pref[DataStoreManager.LAST_ACTIVE_TUNNEL],
batteryOptimizationDisableShown = pref[DataStoreManager.BATTERY_OPTIMIZE_DISABLE_SHOWN] )
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT, } catch (e: IllegalArgumentException) {
tunnelRunningFromManualStart = pref[DataStoreManager.TUNNEL_RUNNING_FROM_MANUAL_START] Timber.e(e)
?: GeneralState.TUNNELING_RUNNING_FROM_MANUAL_START_DEFAULT, GeneralState()
) }
} catch (e: IllegalArgumentException) { } ?: GeneralState()
Timber.e(e) }
GeneralState()
}
} ?: GeneralState()
}
} }
@@ -2,23 +2,29 @@ package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.SettingsDao import com.zaneschepke.wireguardautotunnel.data.SettingsDao
import com.zaneschepke.wireguardautotunnel.data.domain.Settings import com.zaneschepke.wireguardautotunnel.data.domain.Settings
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
class RoomSettingsRepository(private val settingsDoa: SettingsDao) : SettingsRepository { class RoomSettingsRepository(private val settingsDoa: SettingsDao, @IoDispatcher private val ioDispatcher: CoroutineDispatcher) : SettingsRepository {
override suspend fun save(settings: Settings) {
withContext(ioDispatcher) {
settingsDoa.save(settings)
}
}
override suspend fun save(settings: Settings) { override fun getSettingsFlow(): Flow<Settings> {
settingsDoa.save(settings) return settingsDoa.getSettingsFlow()
} }
override fun getSettingsFlow(): Flow<Settings> { override suspend fun getSettings(): Settings {
return settingsDoa.getSettingsFlow() return withContext(ioDispatcher) {
} settingsDoa.getAll().firstOrNull() ?: Settings()
}
}
override suspend fun getSettings(): Settings { override suspend fun getAll(): List<Settings> {
return settingsDoa.getAll().firstOrNull() ?: Settings() return withContext(ioDispatcher) { settingsDoa.getAll() }
} }
override suspend fun getAll(): List<Settings> {
return settingsDoa.getAll()
}
} }
@@ -1,72 +1,97 @@
package com.zaneschepke.wireguardautotunnel.data.repository package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
class RoomTunnelConfigRepository(private val tunnelConfigDao: TunnelConfigDao) : class RoomTunnelConfigRepository(
TunnelConfigRepository { private val tunnelConfigDao: TunnelConfigDao,
override fun getTunnelConfigsFlow(): Flow<TunnelConfigs> { @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
return tunnelConfigDao.getAllFlow() ) :
} TunnelConfigRepository {
override fun getTunnelConfigsFlow(): Flow<TunnelConfigs> {
return tunnelConfigDao.getAllFlow()
}
override suspend fun getAll(): TunnelConfigs { override suspend fun getAll(): TunnelConfigs {
return tunnelConfigDao.getAll() return withContext(ioDispatcher) { tunnelConfigDao.getAll() }
} }
override suspend fun save(tunnelConfig: TunnelConfig) { override suspend fun save(tunnelConfig: TunnelConfig) {
tunnelConfigDao.save(tunnelConfig) withContext(ioDispatcher) {
} tunnelConfigDao.save(tunnelConfig)
}.also {
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
}
}
override suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?) { override suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?) {
tunnelConfigDao.resetPrimaryTunnel() withContext(ioDispatcher) {
tunnelConfig?.let { tunnelConfigDao.resetPrimaryTunnel()
save( tunnelConfig?.let {
it.copy( save(
isPrimaryTunnel = true, it.copy(
), isPrimaryTunnel = true,
) ),
} )
}
}
}
} override suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?) {
withContext(ioDispatcher) {
tunnelConfigDao.resetMobileDataTunnel()
tunnelConfig?.let {
save(
it.copy(
isMobileDataTunnel = true,
),
)
}
}
}
override suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?) { override suspend fun delete(tunnelConfig: TunnelConfig) {
tunnelConfigDao.resetMobileDataTunnel() withContext(ioDispatcher) {
tunnelConfig?.let { tunnelConfigDao.delete(tunnelConfig)
save( }.also {
it.copy( WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
isMobileDataTunnel = true, }
), }
)
}
}
override suspend fun delete(tunnelConfig: TunnelConfig) { override suspend fun getById(id: Int): TunnelConfig? {
tunnelConfigDao.delete(tunnelConfig) return withContext(ioDispatcher) { tunnelConfigDao.getById(id.toLong()) }
} }
override suspend fun getById(id: Int): TunnelConfig? { override suspend fun getActive(): TunnelConfigs {
return tunnelConfigDao.getById(id.toLong()) return withContext(ioDispatcher) {
} tunnelConfigDao.getActive()
}
}
override suspend fun count(): Int { override suspend fun count(): Int {
return tunnelConfigDao.count().toInt() return withContext(ioDispatcher) { tunnelConfigDao.count().toInt() }
} }
override suspend fun findByTunnelName(name: String): TunnelConfig? { override suspend fun findByTunnelName(name: String): TunnelConfig? {
return tunnelConfigDao.getByName(name) return withContext(ioDispatcher) { tunnelConfigDao.getByName(name) }
} }
override suspend fun findByTunnelNetworksName(name: String): TunnelConfigs { override suspend fun findByTunnelNetworksName(name: String): TunnelConfigs {
return tunnelConfigDao.findByTunnelNetworkName(name) return withContext(ioDispatcher) { tunnelConfigDao.findByTunnelNetworkName(name) }
} }
override suspend fun findByMobileDataTunnel(): TunnelConfigs { override suspend fun findByMobileDataTunnel(): TunnelConfigs {
return tunnelConfigDao.findByMobileDataTunnel() return withContext(ioDispatcher) { tunnelConfigDao.findByMobileDataTunnel() }
} }
override suspend fun findPrimary(): TunnelConfigs { override suspend fun findPrimary(): TunnelConfigs {
return tunnelConfigDao.findByPrimary() return withContext(ioDispatcher) { tunnelConfigDao.findByPrimary() }
} }
} }
@@ -4,11 +4,11 @@ import com.zaneschepke.wireguardautotunnel.data.domain.Settings
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface SettingsRepository { interface SettingsRepository {
suspend fun save(settings: Settings) suspend fun save(settings: Settings)
fun getSettingsFlow(): Flow<Settings> fun getSettingsFlow(): Flow<Settings>
suspend fun getSettings(): Settings suspend fun getSettings(): Settings
suspend fun getAll(): List<Settings> suspend fun getAll(): List<Settings>
} }
@@ -1,32 +1,33 @@
package com.zaneschepke.wireguardautotunnel.data.repository package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface TunnelConfigRepository { interface TunnelConfigRepository {
fun getTunnelConfigsFlow(): Flow<TunnelConfigs>
fun getTunnelConfigsFlow(): Flow<TunnelConfigs> suspend fun getAll(): TunnelConfigs
suspend fun getAll(): TunnelConfigs suspend fun save(tunnelConfig: TunnelConfig)
suspend fun save(tunnelConfig: TunnelConfig) suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?)
suspend fun updatePrimaryTunnel(tunnelConfig: TunnelConfig?) suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?)
suspend fun updateMobileDataTunnel(tunnelConfig: TunnelConfig?) suspend fun delete(tunnelConfig: TunnelConfig)
suspend fun delete(tunnelConfig: TunnelConfig) suspend fun getById(id: Int): TunnelConfig?
suspend fun getById(id: Int): TunnelConfig? suspend fun getActive(): TunnelConfigs
suspend fun count(): Int suspend fun count(): Int
suspend fun findByTunnelName(name : String) : TunnelConfig? suspend fun findByTunnelName(name: String): TunnelConfig?
suspend fun findByTunnelNetworksName(name: String): TunnelConfigs suspend fun findByTunnelNetworksName(name: String): TunnelConfigs
suspend fun findByMobileDataTunnel(): TunnelConfigs suspend fun findByMobileDataTunnel(): TunnelConfigs
suspend fun findPrimary(): TunnelConfigs suspend fun findPrimary(): TunnelConfigs
} }
@@ -0,0 +1,30 @@
package com.zaneschepke.wireguardautotunnel.module
import android.content.Context
import com.zaneschepke.logcatter.LocalLogCollector
import com.zaneschepke.logcatter.LogcatUtil
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Singleton
@ApplicationScope
@Provides
fun providesApplicationScope(@DefaultDispatcher defaultDispatcher: CoroutineDispatcher): CoroutineScope =
CoroutineScope(SupervisorJob() + defaultDispatcher)
@Singleton
@Provides
fun provideLogCollect(@ApplicationContext context: Context): LocalLogCollector {
return LogcatUtil.init(context = context)
}
}
@@ -2,6 +2,10 @@ package com.zaneschepke.wireguardautotunnel.module
import javax.inject.Qualifier import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class Kernel
@Qualifier @Qualifier
@Retention(AnnotationRetention.BINARY) @Retention(AnnotationRetention.BINARY)
annotation class Userspace annotation class Userspace
@@ -0,0 +1,27 @@
package com.zaneschepke.wireguardautotunnel.module
import javax.inject.Qualifier
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class DefaultDispatcher
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class IoDispatcher
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class MainDispatcher
@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class MainImmediateDispatcher
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ServiceScope
@@ -0,0 +1,28 @@
package com.zaneschepke.wireguardautotunnel.module
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
@Module
@InstallIn(SingletonComponent::class)
object CoroutinesDispatchersModule {
@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
@IoDispatcher
@Provides
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
@MainDispatcher
@Provides
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
@MainImmediateDispatcher
@Provides
fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
}
@@ -1,30 +0,0 @@
package com.zaneschepke.wireguardautotunnel.module
import android.content.Context
import androidx.room.Room
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
import com.zaneschepke.wireguardautotunnel.data.DatabaseCallback
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
class DatabaseModule {
@Provides
@Singleton
fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java,
context.getString(R.string.db_name),
)
.fallbackToDestructiveMigration()
.addCallback(DatabaseCallback())
.build()
}
}
@@ -1,7 +0,0 @@
package com.zaneschepke.wireguardautotunnel.module
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class Kernel
@@ -1,7 +1,10 @@
package com.zaneschepke.wireguardautotunnel.module package com.zaneschepke.wireguardautotunnel.module
import android.content.Context import android.content.Context
import androidx.room.Room
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.AppDatabase import com.zaneschepke.wireguardautotunnel.data.AppDatabase
import com.zaneschepke.wireguardautotunnel.data.DatabaseCallback
import com.zaneschepke.wireguardautotunnel.data.SettingsDao import com.zaneschepke.wireguardautotunnel.data.SettingsDao
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
@@ -18,56 +21,68 @@ import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Singleton import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class RepositoryModule { class RepositoryModule {
@Singleton @Provides
@Provides @Singleton
fun provideSettingsDoa(appDatabase: AppDatabase): SettingsDao { fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
return appDatabase.settingDao() return Room.databaseBuilder(
} context,
AppDatabase::class.java,
context.getString(R.string.db_name),
)
.fallbackToDestructiveMigration()
.addCallback(DatabaseCallback())
.build()
}
@Singleton @Singleton
@Provides @Provides
fun provideTunnelConfigDoa(appDatabase: AppDatabase): TunnelConfigDao { fun provideSettingsDoa(appDatabase: AppDatabase): SettingsDao {
return appDatabase.tunnelConfigDoa() return appDatabase.settingDao()
} }
@Singleton @Singleton
@Provides @Provides
fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao): TunnelConfigRepository { fun provideTunnelConfigDoa(appDatabase: AppDatabase): TunnelConfigDao {
return RoomTunnelConfigRepository(tunnelConfigDao) return appDatabase.tunnelConfigDoa()
} }
@Singleton @Singleton
@Provides @Provides
fun provideSettingsRepository(settingsDao: SettingsDao): SettingsRepository { fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): TunnelConfigRepository {
return RoomSettingsRepository(settingsDao) return RoomTunnelConfigRepository(tunnelConfigDao, ioDispatcher)
} }
@Singleton @Singleton
@Provides @Provides
fun providePreferencesDataStore(@ApplicationContext context: Context): DataStoreManager { fun provideSettingsRepository(settingsDao: SettingsDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): SettingsRepository {
return DataStoreManager(context) return RoomSettingsRepository(settingsDao, ioDispatcher)
} }
@Provides @Singleton
@Singleton @Provides
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager): AppStateRepository { fun providePreferencesDataStore(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): DataStoreManager {
return DataStoreAppStateRepository(dataStoreManager) return DataStoreManager(context, ioDispatcher)
} }
@Provides
@Singleton
fun provideAppDataRepository(
settingsRepository: SettingsRepository,
tunnelConfigRepository: TunnelConfigRepository,
appStateRepository: AppStateRepository
): AppDataRepository {
return AppDataRoomRepository(settingsRepository, tunnelConfigRepository, appStateRepository)
}
@Provides
@Singleton
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager, @IoDispatcher ioDispatcher: CoroutineDispatcher): AppStateRepository {
return DataStoreAppStateRepository(dataStoreManager, ioDispatcher)
}
@Provides
@Singleton
fun provideAppDataRepository(
settingsRepository: SettingsRepository,
tunnelConfigRepository: TunnelConfigRepository,
appStateRepository: AppStateRepository,
): AppDataRepository {
return AppDataRoomRepository(settingsRepository, tunnelConfigRepository, appStateRepository)
}
} }
@@ -15,25 +15,19 @@ import dagger.hilt.android.scopes.ServiceScoped
@Module @Module
@InstallIn(ServiceComponent::class) @InstallIn(ServiceComponent::class)
abstract class ServiceModule { abstract class ServiceModule {
@Binds @Binds
@ServiceScoped @ServiceScoped
abstract fun provideNotificationService( abstract fun provideNotificationService(wireGuardNotification: WireGuardNotification): NotificationService
wireGuardNotification: WireGuardNotification
): NotificationService
@Binds @Binds
@ServiceScoped @ServiceScoped
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService> abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
@Binds @Binds
@ServiceScoped @ServiceScoped
abstract fun provideMobileDataService( abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService<MobileDataService>
mobileDataService: MobileDataService
): NetworkService<MobileDataService>
@Binds @Binds
@ServiceScoped @ServiceScoped
abstract fun provideEthernetService( abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService<EthernetService>
ethernetService: EthernetService
): NetworkService<EthernetService>
} }
@@ -3,63 +3,82 @@ package com.zaneschepke.wireguardautotunnel.module
import android.content.Context import android.content.Context
import com.wireguard.android.backend.Backend import com.wireguard.android.backend.Backend
import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.GoBackend
import com.wireguard.android.backend.RootTunnelActionHandler
import com.wireguard.android.backend.WgQuickBackend import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.util.RootShell import com.wireguard.android.util.RootShell
import com.wireguard.android.util.ToolsInstaller import com.wireguard.android.util.ToolsInstaller
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService 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
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import javax.inject.Provider
import javax.inject.Singleton import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class TunnelModule { class TunnelModule {
@Provides @Provides
@Singleton @Singleton
fun provideRootShell(@ApplicationContext context: Context): RootShell { fun provideRootShell(@ApplicationContext context: Context): RootShell {
return RootShell(context) return RootShell(context)
} }
@Provides @Provides
@Singleton @Singleton
@Userspace fun provideRootShellAm(@ApplicationContext context: Context): org.amnezia.awg.util.RootShell {
fun provideUserspaceBackend(@ApplicationContext context: Context): Backend { return org.amnezia.awg.util.RootShell(context)
return GoBackend(context) }
}
@Provides @Provides
@Singleton @Singleton
@Kernel @Userspace
fun provideKernelBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend { fun provideUserspaceBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell)) return GoBackend(context, RootTunnelActionHandler(rootShell))
} }
@Provides @Provides
@Singleton @Singleton
fun provideAmneziaBackend(@ApplicationContext context: Context) : org.amnezia.awg.backend.Backend { @Kernel
return org.amnezia.awg.backend.GoBackend(context) fun provideKernelBackend(@ApplicationContext context: Context, rootShell: RootShell): Backend {
} return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell), RootTunnelActionHandler(rootShell))
}
@Provides @Provides
@Singleton @Singleton
fun provideVpnService( fun provideAmneziaBackend(@ApplicationContext context: Context, rootShell: org.amnezia.awg.util.RootShell): org.amnezia.awg.backend.Backend {
amneziaBackend: org.amnezia.awg.backend.Backend, return org.amnezia.awg.backend.GoBackend(context, org.amnezia.awg.backend.RootTunnelActionHandler(rootShell))
@Userspace userspaceBackend: Backend, }
@Kernel kernelBackend: Backend,
appDataRepository: AppDataRepository
): VpnService {
return WireGuardTunnel(amneziaBackend,userspaceBackend, kernelBackend, appDataRepository)
}
@Provides @Provides
@Singleton @Singleton
fun provideServiceManager(appDataRepository: AppDataRepository): ServiceManager { fun provideVpnService(
return ServiceManager(appDataRepository) amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
} @Userspace userspaceBackend: Provider<Backend>,
@Kernel kernelBackend: Provider<Backend>,
appDataRepository: AppDataRepository,
@ApplicationScope applicationScope: CoroutineScope,
@IoDispatcher ioDispatcher: CoroutineDispatcher,
): TunnelService {
return WireGuardTunnel(
amneziaBackend,
userspaceBackend,
kernelBackend,
appDataRepository,
applicationScope,
ioDispatcher,
)
}
@Provides
@Singleton
fun provideServiceManager(): ServiceManager {
return ServiceManager()
}
} }
@@ -0,0 +1,21 @@
package com.zaneschepke.wireguardautotunnel.module
import android.content.Context
import com.zaneschepke.wireguardautotunnel.util.FileUtils
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.CoroutineDispatcher
@Module
@InstallIn(ViewModelComponent::class)
class ViewModelModule {
@ViewModelScoped
@Provides
fun provideFileUtils(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): FileUtils {
return FileUtils(context, ioDispatcher)
}
}
@@ -0,0 +1,47 @@
package com.zaneschepke.wireguardautotunnel.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class AppUpdateReceiver : BroadcastReceiver() {
@Inject
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject
lateinit var serviceManager: ServiceManager
@Inject
lateinit var tunnelService: TunnelService
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
applicationScope.launch {
val settings = appDataRepository.settings.getSettings()
if (settings.isAutoTunnelEnabled) {
Timber.i("Restarting services after upgrade")
serviceManager.startWatcherServiceForeground(context)
}
if (!settings.isAutoTunnelEnabled || settings.isAutoTunnelPaused) {
val tunnels = appDataRepository.tunnels.getAll().filter { it.isActive }
if (tunnels.isNotEmpty()) context.startTunnelBackground(tunnels.first().id)
}
}
}
}
@@ -0,0 +1,64 @@
package com.zaneschepke.wireguardautotunnel.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint
class BackgroundActionReceiver : BroadcastReceiver() {
@Inject
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject
lateinit var tunnelService: Provider<TunnelService>
@Inject
lateinit var tunnelConfigRepository: TunnelConfigRepository
@Inject
lateinit var serviceManager: ServiceManager
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra(TUNNEL_ID_EXTRA_KEY, 0)
if (id == 0) return
when (intent.action) {
ACTION_CONNECT -> {
Timber.d("Connect actions")
applicationScope.launch {
val tunnel = tunnelConfigRepository.getById(id)
tunnel?.let {
serviceManager.startTunnelBackgroundService(context)
tunnelService.get().startTunnel(it)
}
}
}
ACTION_DISCONNECT -> {
applicationScope.launch {
val tunnel = tunnelConfigRepository.getById(id)
tunnel?.let {
serviceManager.stopTunnelBackgroundService(context)
tunnelService.get().stopTunnel(it)
}
}
}
}
}
companion object {
const val ACTION_CONNECT = "ACTION_CONNECT"
const val ACTION_DISCONNECT = "ACTION_DISCONNECT"
const val TUNNEL_ID_EXTRA_KEY = "tunnelId"
}
}
@@ -4,42 +4,45 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.util.goAsync import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint @AndroidEntryPoint
class BootReceiver : BroadcastReceiver() { class BootReceiver : BroadcastReceiver() {
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject @Inject
lateinit var appDataRepository: AppDataRepository lateinit var tunnelService: Provider<TunnelService>
@Inject @Inject
lateinit var serviceManager: ServiceManager lateinit var serviceManager: ServiceManager
override fun onReceive(context: Context?, intent: Intent?) = goAsync { @Inject
if (Intent.ACTION_BOOT_COMPLETED != intent?.action) return@goAsync @ApplicationScope
context?.run { lateinit var applicationScope: CoroutineScope
val settings = appDataRepository.settings.getSettings()
if (settings.isAutoTunnelEnabled) { override fun onReceive(context: Context, intent: Intent) {
Timber.i("Starting watcher service from boot") if (Intent.ACTION_BOOT_COMPLETED != intent.action) return
serviceManager.startWatcherServiceForeground(context) applicationScope.launch {
} val settings = appDataRepository.settings.getSettings()
if (appDataRepository.appState.isTunnelRunningFromManualStart()) { if (settings.isRestoreOnBootEnabled) {
appDataRepository.appState.getActiveTunnelId()?.let { appDataRepository.getStartTunnelConfig()?.let {
Timber.i("Starting tunnel that was active before reboot") context.startTunnelBackground(it.id)
serviceManager.startVpnServiceForeground( }
context, }
appDataRepository.tunnels.getById(it)?.id, if (settings.isAutoTunnelEnabled) {
) Timber.i("Starting watcher service from boot")
} serviceManager.startWatcherServiceForeground(context)
} }
if (settings.isAlwaysOnVpnEnabled) { }
Timber.i("Starting vpn service from boot AOVPN") }
serviceManager.startVpnServiceForeground(context)
}
}
}
} }
@@ -0,0 +1,48 @@
package com.zaneschepke.wireguardautotunnel.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint
class KernelReceiver : BroadcastReceiver() {
@Inject
lateinit var tunnelService: Provider<TunnelService>
@Inject
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject
lateinit var tunnelConfigRepository: TunnelConfigRepository
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action ?: return
applicationScope.launch {
if (action == REFRESH_TUNNELS_ACTION) {
tunnelService.get().runningTunnelNames().forEach { name ->
// TODO can optimize later
val tunnel = tunnelConfigRepository.findByTunnelName(name)
tunnel?.let {
tunnelConfigRepository.save(it.copy(isActive = true))
}
}
context.requestTunnelTileServiceStateUpdate()
}
}
}
companion object {
const val REFRESH_TUNNELS_ACTION = "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
}
}
@@ -1,36 +0,0 @@
package com.zaneschepke.wireguardautotunnel.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.goAsync
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class NotificationActionReceiver : BroadcastReceiver() {
@Inject
lateinit var settingsRepository: SettingsRepository
@Inject
lateinit var serviceManager: ServiceManager
override fun onReceive(context: Context, intent: Intent?) = goAsync {
try {
//TODO fix for manual start changes when enabled
serviceManager.stopVpnServiceForeground(context)
delay(Constants.TOGGLE_TUNNEL_DELAY)
serviceManager.startVpnServiceForeground(context)
} catch (e: Exception) {
Timber.e(e)
} finally {
cancel()
}
}
}
@@ -1,8 +1,8 @@
package com.zaneschepke.wireguardautotunnel.service.foreground package com.zaneschepke.wireguardautotunnel.service.foreground
enum class Action { enum class Action {
START, START,
START_FOREGROUND, START_FOREGROUND,
STOP, STOP,
STOP_FOREGROUND STOP_FOREGROUND,
} }
@@ -0,0 +1,467 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import androidx.core.app.ServiceCompat
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.module.MainImmediateDispatcher
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.util.Constants
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.net.InetAddress
import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint
class AutoTunnelService : ForegroundService() {
private val foregroundId = 122
@Inject
lateinit var wifiService: NetworkService<WifiService>
@Inject
lateinit var mobileDataService: NetworkService<MobileDataService>
@Inject
lateinit var ethernetService: NetworkService<EthernetService>
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject
lateinit var notificationService: NotificationService
@Inject
lateinit var tunnelService: Provider<TunnelService>
@Inject
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
@Inject
@MainImmediateDispatcher
lateinit var mainImmediateDispatcher: CoroutineDispatcher
private val networkEventsFlow = MutableStateFlow(AutoTunnelState())
private var wakeLock: PowerManager.WakeLock? = null
private val tag = this.javaClass.name
private var running: Boolean = false
override fun onCreate() {
super.onCreate()
lifecycleScope.launch(mainImmediateDispatcher) {
kotlin.runCatching {
launchNotification()
}.onFailure {
Timber.e(it)
}
}
}
private suspend fun launchNotification() {
if (appDataRepository.settings.getSettings().isAutoTunnelPaused) {
launchWatcherPausedNotification()
} else {
launchWatcherNotification()
}
}
override fun startService(extras: Bundle?) {
super.startService(extras)
if (running) return
kotlin.runCatching {
lifecycleScope.launch(mainImmediateDispatcher) {
launchNotification()
initWakeLock()
}
startWatcherJob()
}.onFailure {
Timber.e(it)
}
}
override fun stopService() {
super.stopService()
wakeLock?.let {
if (it.isHeld) {
it.release()
}
}
}
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
val notification =
notificationService.createNotification(
channelId = getString(R.string.watcher_channel_id),
channelName = getString(R.string.watcher_channel_name),
title = getString(R.string.auto_tunnel_title),
description = description,
)
ServiceCompat.startForeground(
this,
foregroundId,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun launchWatcherPausedNotification() {
launchWatcherNotification(getString(R.string.watcher_notification_text_paused))
}
private fun initWakeLock() {
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
try {
Timber.i("Initiating wakelock with 10 min timeout")
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
} finally {
release()
}
}
}
}
private fun startWatcherJob() = lifecycleScope.launch {
val setting = appDataRepository.settings.getSettings()
launch {
Timber.i("Starting wifi watcher")
watchForWifiConnectivityChanges()
}
if (setting.isTunnelOnMobileDataEnabled) {
launch {
Timber.i("Starting mobile data watcher")
watchForMobileDataConnectivityChanges()
}
}
if (setting.isTunnelOnEthernetEnabled) {
launch {
Timber.i("Starting ethernet data watcher")
watchForEthernetConnectivityChanges()
}
}
launch {
Timber.i("Starting settings watcher")
watchForSettingsChanges()
}
if (setting.isPingEnabled) {
launch {
Timber.i("Starting ping watcher")
watchForPingFailure()
}
}
launch {
Timber.i("Starting management watcher")
manageVpn()
}
running = true
}
private suspend fun watchForMobileDataConnectivityChanges() {
withContext(ioDispatcher) {
mobileDataService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Mobile data connection")
networkEventsFlow.update {
it.copy(
isMobileDataConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
networkEventsFlow.update {
it.copy(
isMobileDataConnected = true,
)
}
Timber.i("Mobile data capabilities changed")
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isMobileDataConnected = false,
)
}
Timber.i("Lost mobile data connection")
}
}
}
}
}
private suspend fun watchForPingFailure() {
withContext(ioDispatcher) {
try {
do {
if (tunnelService.get().vpnState.value.status == TunnelState.UP) {
val tunnelConfig = tunnelService.get().vpnState.value.tunnelConfig
tunnelConfig?.let {
val config = TunnelConfig.configFromWgQuick(it.wgQuick)
val results =
config.peers.map { peer ->
val host =
if (peer.endpoint.isPresent &&
peer.endpoint.get().resolved.isPresent
) {
peer.endpoint.get().resolved.get().host
} else {
Constants.DEFAULT_PING_IP
}
Timber.i("Checking reachability of: $host")
val reachable =
InetAddress.getByName(host)
.isReachable(Constants.PING_TIMEOUT.toInt())
Timber.i("Result: reachable - $reachable")
reachable
}
if (results.contains(false)) {
Timber.i("Restarting VPN for ping failure")
tunnelService.get().stopTunnel(it)
delay(Constants.VPN_RESTART_DELAY)
tunnelService.get().startTunnel(it)
delay(Constants.PING_COOLDOWN)
}
}
}
delay(Constants.PING_INTERVAL)
} while (true)
} catch (e: Exception) {
Timber.e(e)
}
}
}
private suspend fun watchForSettingsChanges() {
appDataRepository.settings.getSettingsFlow().collect { settings ->
if (networkEventsFlow.value.settings.isAutoTunnelPaused
!= settings.isAutoTunnelPaused
) {
when (settings.isAutoTunnelPaused) {
true -> launchWatcherPausedNotification()
false -> launchWatcherNotification()
}
}
networkEventsFlow.update {
it.copy(
settings = settings,
)
}
}
}
private suspend fun watchForEthernetConnectivityChanges() {
withContext(ioDispatcher) {
ethernetService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Ethernet connection")
networkEventsFlow.update {
it.copy(
isEthernetConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
Timber.i("Ethernet capabilities changed")
networkEventsFlow.update {
it.copy(
isEthernetConnected = true,
)
}
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isEthernetConnected = false,
)
}
Timber.i("Lost Ethernet connection")
}
}
}
}
}
private suspend fun watchForWifiConnectivityChanges() {
withContext(ioDispatcher) {
wifiService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Wi-Fi connection")
networkEventsFlow.update {
it.copy(
isWifiConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
Timber.i("Wifi capabilities changed")
networkEventsFlow.update {
it.copy(
isWifiConnected = true,
)
}
val ssid = wifiService.getNetworkName(status.networkCapabilities)
ssid?.let { name ->
if (name.contains(Constants.UNREADABLE_SSID)) {
Timber.w("SSID unreadable: missing permissions")
} else {
Timber.i("Detected valid SSID")
}
appDataRepository.appState.setCurrentSsid(name)
networkEventsFlow.update {
it.copy(
currentNetworkSSID = name,
)
}
} ?: Timber.w("Failed to read ssid")
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isWifiConnected = false,
)
}
Timber.i("Lost Wi-Fi connection")
}
}
}
}
}
private suspend fun getMobileDataTunnel(): TunnelConfig? {
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
}
private suspend fun getSsidTunnel(ssid: String): TunnelConfig? {
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
}
private fun isTunnelDown(): Boolean {
return tunnelService.get().vpnState.value.status == TunnelState.DOWN
}
private suspend fun manageVpn() {
withContext(ioDispatcher) {
networkEventsFlow.collectLatest { watcherState ->
val autoTunnel = "Auto-tunnel watcher"
if (!watcherState.settings.isAutoTunnelPaused) {
// delay for rapid network state changes and then collect latest
delay(Constants.WATCHER_COLLECTION_DELAY)
val activeTunnel = tunnelService.get().vpnState.value.tunnelConfig
val defaultTunnel = appDataRepository.getPrimaryOrFirstTunnel()
when {
watcherState.isEthernetConditionMet() -> {
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
if (isTunnelDown()) {
defaultTunnel?.let {
tunnelService.get().startTunnel(it)
}
}
}
watcherState.isMobileDataConditionMet() -> {
Timber.i("$autoTunnel - tunnel on mobile data condition met")
val mobileDataTunnel = getMobileDataTunnel()
val tunnel =
mobileDataTunnel ?: defaultTunnel
if (isTunnelDown() || activeTunnel?.isMobileDataTunnel == false) {
tunnel?.let {
tunnelService.get().startTunnel(it)
}
}
}
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
if (!isTunnelDown()) {
activeTunnel?.let {
tunnelService.get().stopTunnel(it)
}
}
}
watcherState.isUntrustedWifiConditionMet() -> {
if (activeTunnel?.tunnelNetworks?.contains(watcherState.currentNetworkSSID) == false ||
activeTunnel == null
) {
Timber.i(
"$autoTunnel - tunnel on ssid not associated with current tunnel condition met",
)
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
Timber.i("Found tunnel associated with this SSID, bringing tunnel up: ${it.name}")
if (isTunnelDown() || activeTunnel?.id != it.id) {
tunnelService.get().startTunnel(it)
}
} ?: suspend {
Timber.i("No tunnel associated with this SSID, using defaults")
val default = appDataRepository.getPrimaryOrFirstTunnel()
if (default?.name != tunnelService.get().name || isTunnelDown()) {
default?.let {
tunnelService.get().startTunnel(it)
}
}
}.invoke()
}
}
watcherState.isTrustedWifiConditionMet() -> {
Timber.i(
"$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off",
)
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
}
watcherState.isTunnelOffOnWifiConditionMet() -> {
Timber.i(
"$autoTunnel - tunnel off on wifi condition met, turning vpn off",
)
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
}
watcherState.isTunnelOffOnNoConnectivityMet() -> {
Timber.i(
"$autoTunnel - tunnel off on no connectivity met, turning vpn off",
)
if (!isTunnelDown()) activeTunnel?.let { tunnelService.get().stopTunnel(it) }
}
else -> {
Timber.i("$autoTunnel - no condition met")
}
}
}
}
}
}
}
@@ -0,0 +1,73 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
data class AutoTunnelState(
val isWifiConnected: Boolean = false,
val isEthernetConnected: Boolean = false,
val isMobileDataConnected: Boolean = false,
val currentNetworkSSID: String = "",
val settings: Settings = Settings(),
) {
fun isEthernetConditionMet(): Boolean {
return (
isEthernetConnected &&
settings.isTunnelOnEthernetEnabled
)
}
fun isMobileDataConditionMet(): Boolean {
return (
!isEthernetConnected &&
settings.isTunnelOnMobileDataEnabled &&
!isWifiConnected &&
isMobileDataConnected
)
}
fun isTunnelOffOnMobileDataConditionMet(): Boolean {
return (
!isEthernetConnected &&
!settings.isTunnelOnMobileDataEnabled &&
isMobileDataConnected &&
!isWifiConnected
)
}
fun isUntrustedWifiConditionMet(): Boolean {
return (
!isEthernetConnected &&
isWifiConnected &&
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
settings.isTunnelOnWifiEnabled
)
}
fun isTrustedWifiConditionMet(): Boolean {
return (
!isEthernetConnected &&
(
isWifiConnected &&
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)
)
)
}
fun isTunnelOffOnWifiConditionMet(): Boolean {
return (
!isEthernetConnected &&
(
isWifiConnected &&
!settings.isTunnelOnWifiEnabled
)
)
}
fun isTunnelOffOnNoConnectivityMet(): Boolean {
return (
!isEthernetConnected &&
!isWifiConnected &&
!isMobileDataConnected
)
}
}
@@ -8,48 +8,50 @@ import com.zaneschepke.wireguardautotunnel.util.Constants
import timber.log.Timber import timber.log.Timber
open class ForegroundService : LifecycleService() { open class ForegroundService : LifecycleService() {
private var isServiceStarted = false private var isServiceStarted = false
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 // We don't provide binding, so return null
return null return null
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
Timber.d("onStartCommand executed with startId: $startId") Timber.d("onStartCommand executed with startId: $startId")
if (intent != null) { if (intent != null) {
val action = intent.action val action = intent.action
when (action) { when (action) {
Action.START.name, Action.START.name,
Action.START_FOREGROUND.name -> startService(intent.extras) Action.START_FOREGROUND.name,
Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService() -> startService(intent.extras)
Constants.ALWAYS_ON_VPN_ACTION -> {
Timber.i("Always-on VPN starting service")
startService(intent.extras)
}
else -> Timber.d("This should never happen. No action in the received intent") Action.STOP.name, Action.STOP_FOREGROUND.name -> stopService()
} Constants.ALWAYS_ON_VPN_ACTION -> {
} else { Timber.i("Always-on VPN starting service")
Timber.d( startService(intent.extras)
"with a null intent. It has been probably restarted by the system.", }
)
}
return START_STICKY
}
protected open fun startService(extras: Bundle?) { else -> Timber.d("This should never happen. No action in the received intent")
if (isServiceStarted) return }
Timber.d("Starting ${this.javaClass.simpleName}") } else {
isServiceStarted = true Timber.d(
} "with a null intent. It has been probably restarted by the system.",
)
}
return START_STICKY
}
protected open fun stopService() { protected open fun startService(extras: Bundle?) {
Timber.d("Stopping ${this.javaClass.simpleName}") if (isServiceStarted) return
stopForeground(STOP_FOREGROUND_REMOVE) Timber.d("Starting ${this.javaClass.simpleName}")
stopSelf() isServiceStarted = true
isServiceStarted = false }
}
protected open fun stopService() {
Timber.d("Stopping ${this.javaClass.simpleName}")
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
isServiceStarted = false
}
} }
@@ -3,115 +3,67 @@ 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.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.util.Constants
import timber.log.Timber import timber.log.Timber
class ServiceManager(private val appDataRepository: AppDataRepository) { class ServiceManager {
private fun <T : Service> actionOnService(action: Action, context: Context, cls: Class<T>, extras: Map<String, Int>? = null) {
val intent =
Intent(context, cls).also {
it.action = action.name
extras?.forEach { (k, v) -> it.putExtra(k, v) }
}
intent.component?.javaClass
try {
when (action) {
Action.START_FOREGROUND, Action.STOP_FOREGROUND ->
context.startForegroundService(
intent,
)
private fun <T : Service> actionOnService( Action.START, Action.STOP -> context.startService(intent)
action: Action, }
context: Context, } catch (e: Exception) {
cls: Class<T>, Timber.e(e.message)
extras: Map<String, Int>? = null }
) { }
val intent =
Intent(context, cls).also {
it.action = action.name
extras?.forEach { (k, v) -> it.putExtra(k, v) }
}
intent.component?.javaClass
try {
when (action) {
Action.START_FOREGROUND, Action.STOP_FOREGROUND -> context.startForegroundService(intent)
Action.START, Action.STOP -> context.startService(intent)
}
} catch (e: Exception) {
Timber.e(e.message)
}
}
suspend fun startVpnService( fun startWatcherServiceForeground(context: Context) {
context: Context, actionOnService(
tunnelId: Int? = null, Action.START_FOREGROUND,
isManualStart: Boolean = false context,
) { AutoTunnelService::class.java,
if (isManualStart) onManualStart(tunnelId) )
actionOnService( }
Action.START,
context,
WireGuardTunnelService::class.java,
tunnelId?.let { mapOf(Constants.TUNNEL_EXTRA_KEY to it) },
)
}
suspend fun stopVpnServiceForeground(context: Context, isManualStop: Boolean = false) { fun startWatcherService(context: Context) {
if (isManualStop) onManualStop() actionOnService(
Timber.i("Stopping vpn service") Action.START,
actionOnService( context,
Action.STOP_FOREGROUND, AutoTunnelService::class.java,
context, )
WireGuardTunnelService::class.java, }
)
}
suspend fun stopVpnService(context: Context, isManualStop: Boolean = false) { fun stopWatcherService(context: Context) {
if (isManualStop) onManualStop() actionOnService(
Timber.i("Stopping vpn service") Action.STOP,
actionOnService( context,
Action.STOP, AutoTunnelService::class.java,
context, )
WireGuardTunnelService::class.java, }
)
}
private suspend fun onManualStop() { fun startTunnelBackgroundService(context: Context) {
appDataRepository.appState.setManualStop() actionOnService(
} Action.START_FOREGROUND,
context,
TunnelBackgroundService::class.java,
)
}
private suspend fun onManualStart(tunnelId: Int?) { fun stopTunnelBackgroundService(context: Context) {
tunnelId?.let { actionOnService(
appDataRepository.appState.setTunnelRunningFromManualStart(it) Action.STOP,
} context,
} TunnelBackgroundService::class.java,
)
suspend fun startVpnServiceForeground( }
context: Context,
tunnelId: Int? = null,
isManualStart: Boolean = false
) {
if (isManualStart) onManualStart(tunnelId)
actionOnService(
Action.START_FOREGROUND,
context,
WireGuardTunnelService::class.java,
tunnelId?.let { mapOf(Constants.TUNNEL_EXTRA_KEY to it) },
)
}
fun startWatcherServiceForeground(
context: Context,
) {
actionOnService(
Action.START_FOREGROUND,
context,
WireGuardConnectivityWatcherService::class.java,
)
}
fun startWatcherService(context: Context) {
actionOnService(
Action.START,
context,
WireGuardConnectivityWatcherService::class.java,
)
}
fun stopWatcherService(context: Context) {
actionOnService(
Action.STOP,
context,
WireGuardConnectivityWatcherService::class.java,
)
}
} }
@@ -0,0 +1,41 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import android.app.Notification
import android.os.Bundle
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class TunnelBackgroundService : ForegroundService() {
@Inject
lateinit var notificationService: NotificationService
private val foregroundId = 123
override fun onCreate() {
super.onCreate()
startForeground(foregroundId, createNotification())
}
override fun startService(extras: Bundle?) {
super.startService(extras)
startForeground(foregroundId, createNotification())
}
override fun stopService() {
super.stopService()
stopForeground(STOP_FOREGROUND_REMOVE)
}
private fun createNotification(): Notification {
return notificationService.createNotification(
getString(R.string.vpn_channel_id),
getString(R.string.vpn_channel_name),
getString(R.string.tunnel_start_text),
description = "",
)
}
}
@@ -1,63 +0,0 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
data class WatcherState(
val isWifiConnected: Boolean = false,
val isEthernetConnected: Boolean = false,
val isMobileDataConnected: Boolean = false,
val currentNetworkSSID: String = "",
val settings: Settings = Settings()
) {
fun isEthernetConditionMet(): Boolean {
return (isEthernetConnected &&
settings.isTunnelOnEthernetEnabled)
}
fun isMobileDataConditionMet(): Boolean {
return (!isEthernetConnected &&
settings.isTunnelOnMobileDataEnabled &&
!isWifiConnected &&
isMobileDataConnected)
}
fun isTunnelOnMobileDataPreferredConditionMet(): Boolean {
return (!isEthernetConnected &&
settings.isTunnelOnMobileDataEnabled &&
!isWifiConnected &&
isMobileDataConnected)
}
fun isTunnelOffOnMobileDataConditionMet(): Boolean {
return (!isEthernetConnected &&
!settings.isTunnelOnMobileDataEnabled &&
isMobileDataConnected &&
!isWifiConnected)
}
fun isUntrustedWifiConditionMet(): Boolean {
return (!isEthernetConnected &&
isWifiConnected &&
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
settings.isTunnelOnWifiEnabled)
}
fun isTrustedWifiConditionMet(): Boolean {
return (!isEthernetConnected &&
(isWifiConnected &&
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)))
}
fun isTunnelOffOnWifiConditionMet(): Boolean {
return (!isEthernetConnected &&
(isWifiConnected &&
!settings.isTunnelOnWifiEnabled))
}
fun isTunnelOffOnNoConnectivityMet(): Boolean {
return (!isEthernetConnected &&
!isWifiConnected &&
!isMobileDataConnected)
}
}
@@ -1,429 +0,0 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import androidx.core.app.ServiceCompat
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
import com.zaneschepke.wireguardautotunnel.util.Constants
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
import java.net.InetAddress
import javax.inject.Inject
@AndroidEntryPoint
class WireGuardConnectivityWatcherService : ForegroundService() {
private val foregroundId = 122
@Inject
lateinit var wifiService: NetworkService<WifiService>
@Inject
lateinit var mobileDataService: NetworkService<MobileDataService>
@Inject
lateinit var ethernetService: NetworkService<EthernetService>
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject
lateinit var notificationService: NotificationService
@Inject
lateinit var vpnService: VpnService
@Inject
lateinit var serviceManager: ServiceManager
private val networkEventsFlow = MutableStateFlow(WatcherState())
private var watcherJob: Job? = null
private var wakeLock: PowerManager.WakeLock? = null
private val tag = this.javaClass.name
override fun onCreate() {
super.onCreate()
lifecycleScope.launch(Dispatchers.Main) {
try {
if (appDataRepository.settings.getSettings().isAutoTunnelPaused) {
launchWatcherPausedNotification()
} else launchWatcherNotification()
} catch (e: Exception) {
Timber.e("Failed to start watcher service, not enough permissions")
}
}
}
override fun startService(extras: Bundle?) {
super.startService(extras)
try {
// we need this lock so our service gets not affected by Doze Mode
lifecycleScope.launch { initWakeLock() }
cancelWatcherJob()
startWatcherJob()
} catch (e: Exception) {
Timber.e("Failed to launch watcher service, no permissions")
}
}
override fun stopService() {
super.stopService()
wakeLock?.let {
if (it.isHeld) {
it.release()
}
}
cancelWatcherJob()
stopSelf()
}
private fun launchWatcherNotification(
description: String = getString(R.string.watcher_notification_text_active)
) {
val notification =
notificationService.createNotification(
channelId = getString(R.string.watcher_channel_id),
channelName = getString(R.string.watcher_channel_name),
title = getString(R.string.auto_tunnel_title),
description = description,
)
ServiceCompat.startForeground(
this,
foregroundId,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun launchWatcherPausedNotification() {
launchWatcherNotification(getString(R.string.watcher_notification_text_paused))
}
private fun initWakeLock() {
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
try {
Timber.i("Initiating wakelock with 10 min timeout")
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
} finally {
release()
}
}
}
}
private fun cancelWatcherJob() {
try {
watcherJob?.cancel()
} catch (e : CancellationException) {
Timber.i("Watcher job cancelled")
}
}
private fun startWatcherJob() {
watcherJob =
lifecycleScope.launch(Dispatchers.IO) {
val setting = appDataRepository.settings.getSettings()
launch {
Timber.i("Starting wifi watcher")
watchForWifiConnectivityChanges()
}
if (setting.isTunnelOnMobileDataEnabled) {
launch {
Timber.i("Starting mobile data watcher")
watchForMobileDataConnectivityChanges()
}
}
if (setting.isTunnelOnEthernetEnabled) {
launch {
Timber.i("Starting ethernet data watcher")
watchForEthernetConnectivityChanges()
}
}
launch {
Timber.i("Starting settings watcher")
watchForSettingsChanges()
}
if (setting.isPingEnabled) {
launch {
Timber.i("Starting ping watcher")
watchForPingFailure()
}
}
launch {
Timber.i("Starting management watcher")
manageVpn()
}
}
}
private suspend fun watchForMobileDataConnectivityChanges() {
mobileDataService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Mobile data connection")
networkEventsFlow.update {
it.copy(
isMobileDataConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
networkEventsFlow.update {
it.copy(
isMobileDataConnected = true,
)
}
Timber.i("Mobile data capabilities changed")
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isMobileDataConnected = false,
)
}
Timber.i("Lost mobile data connection")
}
}
}
}
private suspend fun watchForPingFailure() {
try {
do {
if (vpnService.vpnState.value.status == TunnelState.UP) {
val tunnelConfig = vpnService.vpnState.value.tunnelConfig
tunnelConfig?.let {
val config = TunnelConfig.configFromWgQuick(it.wgQuick)
val results = config.peers.map { peer ->
val host = if (peer.endpoint.isPresent &&
peer.endpoint.get().resolved.isPresent)
peer.endpoint.get().resolved.get().host
else Constants.DEFAULT_PING_IP
Timber.i("Checking reachability of: $host")
val reachable = InetAddress.getByName(host)
.isReachable(Constants.PING_TIMEOUT.toInt())
Timber.i("Result: reachable - $reachable")
reachable
}
if (results.contains(false)) {
Timber.i("Restarting VPN for ping failure")
serviceManager.stopVpnServiceForeground(this)
delay(Constants.VPN_RESTART_DELAY)
serviceManager.startVpnServiceForeground(this, it.id)
delay(Constants.PING_COOLDOWN)
}
}
}
delay(Constants.PING_INTERVAL)
} while (true)
} catch (e: Exception) {
Timber.e(e)
}
}
private suspend fun watchForSettingsChanges() {
appDataRepository.settings.getSettingsFlow().collect { settings ->
if (networkEventsFlow.value.settings.isAutoTunnelPaused != settings.isAutoTunnelPaused) {
when (settings.isAutoTunnelPaused) {
true -> launchWatcherPausedNotification()
false -> launchWatcherNotification()
}
}
networkEventsFlow.update {
it.copy(
settings = settings,
)
}
}
}
private suspend fun watchForEthernetConnectivityChanges() {
ethernetService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Ethernet connection")
networkEventsFlow.update {
it.copy(
isEthernetConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
Timber.i("Ethernet capabilities changed")
networkEventsFlow.update {
it.copy(
isEthernetConnected = true,
)
}
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isEthernetConnected = false,
)
}
Timber.i("Lost Ethernet connection")
}
}
}
}
private suspend fun watchForWifiConnectivityChanges() {
wifiService.networkStatus.collect { status ->
when (status) {
is NetworkStatus.Available -> {
Timber.i("Gained Wi-Fi connection")
networkEventsFlow.update {
it.copy(
isWifiConnected = true,
)
}
}
is NetworkStatus.CapabilitiesChanged -> {
Timber.i("Wifi capabilities changed")
networkEventsFlow.update {
it.copy(
isWifiConnected = true,
)
}
val ssid = wifiService.getNetworkName(status.networkCapabilities)
ssid?.let { name ->
if(name.contains(Constants.UNREADABLE_SSID)) {
Timber.w("SSID unreadable: missing permissions")
} else Timber.i("Detected valid SSID")
appDataRepository.appState.setCurrentSsid(name)
networkEventsFlow.update {
it.copy(
currentNetworkSSID = name,
)
}
} ?: Timber.w("Failed to read ssid")
}
is NetworkStatus.Unavailable -> {
networkEventsFlow.update {
it.copy(
isWifiConnected = false,
)
}
Timber.i("Lost Wi-Fi connection")
}
}
}
}
private suspend fun getMobileDataTunnel(): TunnelConfig? {
return appDataRepository.tunnels.findByMobileDataTunnel().firstOrNull()
}
private suspend fun getSsidTunnel(ssid: String): TunnelConfig? {
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
}
private fun isTunnelDown() : Boolean {
return vpnService.vpnState.value.status == TunnelState.DOWN
}
private suspend fun manageVpn() {
networkEventsFlow.collectLatest { watcherState ->
val autoTunnel = "Auto-tunnel watcher"
if (!watcherState.settings.isAutoTunnelPaused) {
//delay for rapid network state changes and then collect latest
delay(Constants.WATCHER_COLLECTION_DELAY)
val tunnelConfig = vpnService.vpnState.value.tunnelConfig
when {
watcherState.isEthernetConditionMet() -> {
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this)
}
watcherState.isMobileDataConditionMet() -> {
Timber.i("$autoTunnel - tunnel on on mobile data condition met")
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this, getMobileDataTunnel()?.id)
}
watcherState.isTunnelOnMobileDataPreferredConditionMet() -> {
if(tunnelConfig?.isMobileDataTunnel == false) {
getMobileDataTunnel()?.let {
Timber.i("$autoTunnel - tunnel connected on mobile data is not preferred condition met, switching to preferred")
if(isTunnelDown()) serviceManager.startVpnServiceForeground(
this,
getMobileDataTunnel()?.id,
)
}
}
}
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
}
watcherState.isUntrustedWifiConditionMet() -> {
if(tunnelConfig?.tunnelNetworks?.contains(watcherState.currentNetworkSSID) == false ||
tunnelConfig == null) {
Timber.i("$autoTunnel - tunnel on ssid not associated with current tunnel condition met")
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
Timber.i("Found tunnel associated with this SSID, bringing tunnel up")
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this, it.id)
} ?: suspend {
Timber.i("No tunnel associated with this SSID, using defaults")
if (appDataRepository.getPrimaryOrFirstTunnel()?.name != vpnService.name) {
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this)
}
}.invoke()
}
}
watcherState.isTrustedWifiConditionMet() -> {
Timber.i("$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off")
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
}
watcherState.isTunnelOffOnWifiConditionMet() -> {
Timber.i("$autoTunnel - tunnel off on wifi condition met, turning vpn off")
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
}
watcherState.isTunnelOffOnNoConnectivityMet() -> {
Timber.i("$autoTunnel - tunnel off on no connectivity met, turning vpn off")
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
}
else -> {
Timber.i("$autoTunnel - no condition met")
}
}
}
}
}
}
@@ -1,188 +0,0 @@
package com.zaneschepke.wireguardautotunnel.service.foreground
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import androidx.core.app.ServiceCompat
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.handshakeStatus
import com.zaneschepke.wireguardautotunnel.util.mapPeerStats
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint
class WireGuardTunnelService : ForegroundService() {
private val foregroundId = 123
@Inject
lateinit var vpnService: VpnService
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject
lateinit var notificationService: NotificationService
private var job: Job? = null
private var didShowConnected = false
override fun onCreate() {
super.onCreate()
lifecycleScope.launch(Dispatchers.Main) {
//TODO fix this to not launch if AOVPN
if (appDataRepository.tunnels.count() != 0) {
launchVpnNotification()
}
}
}
override fun startService(extras: Bundle?) {
super.startService(extras)
cancelJob()
job =
lifecycleScope.launch(Dispatchers.IO) {
launch {
val tunnelId = extras?.getInt(Constants.TUNNEL_EXTRA_KEY)
if (vpnService.getState() == TunnelState.UP) {
vpnService.stopTunnel()
}
vpnService.startTunnel(
tunnelId?.let {
appDataRepository.tunnels.getById(it)
},
)
}
launch {
handshakeNotifications()
}
}
}
//TODO improve tunnel notifications
private suspend fun handshakeNotifications() {
var tunnelName: String? = null
vpnService.vpnState.collect { state ->
state.statistics
?.mapPeerStats()
?.map { it.value?.handshakeStatus() }
.let { statuses ->
when {
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> {
if (!didShowConnected) {
delay(Constants.VPN_CONNECTED_NOTIFICATION_DELAY)
tunnelName = state.tunnelConfig?.name
launchVpnNotification(
getString(R.string.tunnel_start_title),
"${getString(R.string.tunnel_start_text)} - $tunnelName",
)
didShowConnected = true
}
}
statuses?.any { it == HandshakeStatus.STALE } == true -> {}
statuses?.all { it == HandshakeStatus.NOT_STARTED } ==
true -> {
}
else -> {}
}
}
if (state.status == TunnelState.UP && state.tunnelConfig?.name != tunnelName) {
tunnelName = state.tunnelConfig?.name
launchVpnNotification(
getString(R.string.tunnel_start_title),
"${getString(R.string.tunnel_start_text)} - $tunnelName",
)
}
}
}
private fun launchAlwaysOnDisabledNotification() {
launchVpnNotification(
title = this.getString(R.string.vpn_connection_failed),
description = this.getString(R.string.always_on_disabled),
)
}
override fun stopService() {
super.stopService()
lifecycleScope.launch(Dispatchers.IO) {
vpnService.stopTunnel()
didShowConnected = false
}
cancelJob()
stopSelf()
}
private fun launchVpnNotification(
title: String = getString(R.string.vpn_starting),
description: String = getString(R.string.attempt_connection)
) {
val notification =
notificationService.createNotification(
channelId = getString(R.string.vpn_channel_id),
channelName = getString(R.string.vpn_channel_name),
title = title,
onGoing = false,
vibration = false,
showTimestamp = true,
description = description,
)
ServiceCompat.startForeground(
this,
foregroundId,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun launchVpnConnectionFailedNotification(message: String) {
val notification =
notificationService.createNotification(
channelId = getString(R.string.vpn_channel_id),
channelName = getString(R.string.vpn_channel_name),
action =
PendingIntent.getBroadcast(
this,
0,
Intent(this, NotificationActionReceiver::class.java),
PendingIntent.FLAG_IMMUTABLE,
),
actionText = getString(R.string.restart),
title = getString(R.string.vpn_connection_failed),
onGoing = false,
vibration = true,
showTimestamp = true,
description = message,
)
ServiceCompat.startForeground(
this,
foregroundId,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun cancelJob() {
try {
job?.cancel()
} catch (e : CancellationException) {
Timber.i("Tunnel job cancelled")
}
}
}
@@ -15,118 +15,113 @@ import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
abstract class BaseNetworkService<T : BaseNetworkService<T>>( abstract class BaseNetworkService<T : BaseNetworkService<T>>(
val context: Context, val context: Context,
networkCapability: Int networkCapability: Int,
) : NetworkService<T> { ) : NetworkService<T> {
private val connectivityManager = private val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val wifiManager = private val wifiManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
override val networkStatus = callbackFlow { override val networkStatus =
val networkStatusCallback = callbackFlow {
when (Build.VERSION.SDK_INT) { val networkStatusCallback =
in Build.VERSION_CODES.S..Int.MAX_VALUE -> { when (Build.VERSION.SDK_INT) {
object : in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
ConnectivityManager.NetworkCallback( object :
FLAG_INCLUDE_LOCATION_INFO, ConnectivityManager.NetworkCallback(
) { FLAG_INCLUDE_LOCATION_INFO,
override fun onAvailable(network: Network) { ) {
trySend(NetworkStatus.Available(network)) override fun onAvailable(network: Network) {
} trySend(NetworkStatus.Available(network))
}
override fun onLost(network: Network) { override fun onLost(network: Network) {
trySend(NetworkStatus.Unavailable(network)) trySend(NetworkStatus.Unavailable(network))
} }
override fun onCapabilitiesChanged( override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
network: Network, trySend(
networkCapabilities: NetworkCapabilities NetworkStatus.CapabilitiesChanged(
) { network,
trySend( networkCapabilities,
NetworkStatus.CapabilitiesChanged( ),
network, )
networkCapabilities, }
), }
) }
}
}
}
else -> { else -> {
object : ConnectivityManager.NetworkCallback() { object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
trySend(NetworkStatus.Available(network)) trySend(NetworkStatus.Available(network))
} }
override fun onLost(network: Network) { override fun onLost(network: Network) {
trySend(NetworkStatus.Unavailable(network)) trySend(NetworkStatus.Unavailable(network))
} }
override fun onCapabilitiesChanged( override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
network: Network, trySend(
networkCapabilities: NetworkCapabilities NetworkStatus.CapabilitiesChanged(
) { network,
trySend( networkCapabilities,
NetworkStatus.CapabilitiesChanged( ),
network, )
networkCapabilities, }
), }
) }
} }
} val request =
} NetworkRequest.Builder()
} .addTransportType(networkCapability)
val request = .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
NetworkRequest.Builder() .addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
.addTransportType(networkCapability) .build()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) connectivityManager.registerNetworkCallback(request, networkStatusCallback)
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
.build()
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) } awaitClose { connectivityManager.unregisterNetworkCallback(networkStatusCallback) }
} }
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? { override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
var ssid: String? = getWifiNameFromCapabilities(networkCapabilities) var ssid: String? = getWifiNameFromCapabilities(networkCapabilities)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
val info = wifiManager.connectionInfo val info = wifiManager.connectionInfo
if (info.supplicantState === SupplicantState.COMPLETED) { if (info.supplicantState === SupplicantState.COMPLETED) {
ssid = info.ssid ssid = info.ssid
} }
} }
return ssid?.trim('"') return ssid?.trim('"')
} }
companion object { companion object {
private fun getWifiNameFromCapabilities(networkCapabilities: NetworkCapabilities): String? { private fun getWifiNameFromCapabilities(networkCapabilities: NetworkCapabilities): String? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val info: WifiInfo val info: WifiInfo
if (networkCapabilities.transportInfo is WifiInfo) { if (networkCapabilities.transportInfo is WifiInfo) {
info = networkCapabilities.transportInfo as WifiInfo info = networkCapabilities.transportInfo as WifiInfo
return info.ssid return info.ssid
} }
} }
return null return null
} }
} }
} }
inline fun <Result> Flow<NetworkStatus>.map( inline fun <Result> Flow<NetworkStatus>.map(
crossinline onUnavailable: suspend (network: Network) -> Result, crossinline onUnavailable: suspend (network: Network) -> Result,
crossinline onAvailable: suspend (network: Network) -> Result, crossinline onAvailable: suspend (network: Network) -> Result,
crossinline onCapabilitiesChanged: crossinline onCapabilitiesChanged:
suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result suspend (network: Network, networkCapabilities: NetworkCapabilities) -> Result,
): Flow<Result> = map { status -> ): Flow<Result> = map { status ->
when (status) { when (status) {
is NetworkStatus.Unavailable -> onUnavailable(status.network) is NetworkStatus.Unavailable -> onUnavailable(status.network)
is NetworkStatus.Available -> onAvailable(status.network) is NetworkStatus.Available -> onAvailable(status.network)
is NetworkStatus.CapabilitiesChanged -> is NetworkStatus.CapabilitiesChanged ->
onCapabilitiesChanged( onCapabilitiesChanged(
status.network, status.network,
status.networkCapabilities, status.networkCapabilities,
) )
} }
} }
@@ -5,5 +5,9 @@ import android.net.NetworkCapabilities
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
class EthernetService @Inject constructor(@ApplicationContext context: Context) : class EthernetService
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET) @Inject
constructor(
@ApplicationContext context: Context,
) :
BaseNetworkService<EthernetService>(context, NetworkCapabilities.TRANSPORT_ETHERNET)
@@ -5,5 +5,9 @@ import android.net.NetworkCapabilities
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
class MobileDataService @Inject constructor(@ApplicationContext context: Context) : class MobileDataService
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR) @Inject
constructor(
@ApplicationContext context: Context,
) :
BaseNetworkService<MobileDataService>(context, NetworkCapabilities.TRANSPORT_CELLULAR)
@@ -4,7 +4,7 @@ import android.net.NetworkCapabilities
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface NetworkService<T> { interface NetworkService<T> {
fun getNetworkName(networkCapabilities: NetworkCapabilities): String? fun getNetworkName(networkCapabilities: NetworkCapabilities): String?
val networkStatus: Flow<NetworkStatus> val networkStatus: Flow<NetworkStatus>
} }
@@ -4,10 +4,10 @@ import android.net.Network
import android.net.NetworkCapabilities import android.net.NetworkCapabilities
sealed class NetworkStatus { sealed class NetworkStatus {
class Available(val network: Network) : NetworkStatus() class Available(val network: Network) : NetworkStatus()
class Unavailable(val network: Network) : NetworkStatus() class Unavailable(val network: Network) : NetworkStatus()
class CapabilitiesChanged(val network: Network, val networkCapabilities: NetworkCapabilities) : class CapabilitiesChanged(val network: Network, val networkCapabilities: NetworkCapabilities) :
NetworkStatus() NetworkStatus()
} }
@@ -5,5 +5,9 @@ import android.net.NetworkCapabilities
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
class WifiService @Inject constructor(@ApplicationContext context: Context) : class WifiService
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI) @Inject
constructor(
@ApplicationContext context: Context,
) :
BaseNetworkService<WifiService>(context, NetworkCapabilities.TRANSPORT_WIFI)
@@ -5,18 +5,18 @@ import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
interface NotificationService { interface NotificationService {
fun createNotification( fun createNotification(
channelId: String, channelId: String,
channelName: String, channelName: String,
title: String = "", title: String = "",
action: PendingIntent? = null, action: PendingIntent? = null,
actionText: String? = null, 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, vibration: Boolean = false,
onGoing: Boolean = true, onGoing: Boolean = true,
lights: Boolean = true, lights: Boolean = true,
onlyAlertOnce: Boolean = true, onlyAlertOnce: Boolean = true,
): Notification ): Notification
} }
@@ -9,93 +9,97 @@ import android.content.Intent
import android.graphics.Color import android.graphics.Color
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.MainActivity import com.zaneschepke.wireguardautotunnel.ui.SplashActivity
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
class WireGuardNotification @Inject constructor(@ApplicationContext private val context: Context) : class WireGuardNotification
NotificationService { @Inject
private val notificationManager = constructor(
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @ApplicationContext private val context: Context,
) :
NotificationService {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val watcherBuilder: NotificationCompat.Builder = private val watcherBuilder: NotificationCompat.Builder =
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
context.getString(R.string.watcher_channel_id), context.getString(R.string.watcher_channel_id),
) )
private val tunnelBuilder: NotificationCompat.Builder = private val tunnelBuilder: NotificationCompat.Builder =
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
context.getString(R.string.vpn_channel_id), context.getString(R.string.vpn_channel_id),
) )
override fun createNotification( override fun createNotification(
channelId: String, channelId: String,
channelName: String, channelName: String,
title: String, title: String,
action: PendingIntent?, action: PendingIntent?,
actionText: String?, actionText: String?,
description: String, description: String,
showTimestamp: Boolean, showTimestamp: Boolean,
importance: Int, importance: Int,
vibration: Boolean, vibration: Boolean,
onGoing: Boolean, onGoing: Boolean,
lights: Boolean, lights: Boolean,
onlyAlertOnce: Boolean, onlyAlertOnce: Boolean,
): Notification { ): Notification {
val channel = val channel =
NotificationChannel( NotificationChannel(
channelId, channelId,
channelName, channelName,
importance, importance,
) )
.let { .let {
it.description = title it.description = title
it.enableLights(lights) it.enableLights(lights)
it.lightColor = Color.RED it.lightColor = Color.RED
it.enableVibration(vibration) it.enableVibration(vibration)
it.vibrationPattern = longArrayOf(100, 200, 300) it.vibrationPattern = longArrayOf(100, 200, 300)
it it
} }
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(channel)
val pendingIntent: PendingIntent = val pendingIntent: PendingIntent =
Intent(context, MainActivity::class.java).let { notificationIntent -> Intent(context, SplashActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity( PendingIntent.getActivity(
context, context,
0, 0,
notificationIntent, notificationIntent,
PendingIntent.FLAG_IMMUTABLE, PendingIntent.FLAG_IMMUTABLE,
) )
} }
val builder = val builder =
when (channelId) { when (channelId) {
context.getString(R.string.watcher_channel_id) -> watcherBuilder context.getString(R.string.watcher_channel_id) -> watcherBuilder
context.getString(R.string.vpn_channel_id) -> tunnelBuilder context.getString(R.string.vpn_channel_id) -> tunnelBuilder
else -> { else -> {
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
channelId, channelId,
) )
} }
} }
return builder.let { return builder.let {
if (action != null && actionText != null) { if (action != null && actionText != null) {
it.addAction( it.addAction(
NotificationCompat.Action.Builder(0, actionText, action).build(), NotificationCompat.Action.Builder(0, actionText, action).build(),
) )
it.setAutoCancel(true) it.setAutoCancel(true)
} }
it.setContentTitle(title) it.setContentTitle(title)
.setContentText(description) .setContentText(description)
.setOnlyAlertOnce(onlyAlertOnce) .setOnlyAlertOnce(onlyAlertOnce)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setOngoing(onGoing) .setOngoing(onGoing)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.setShowWhen(showTimestamp) .setShowWhen(showTimestamp)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.build() .build()
} }
} }
} }
@@ -2,74 +2,80 @@ package com.zaneschepke.wireguardautotunnel.service.shortcut
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.Action import com.zaneschepke.wireguardautotunnel.service.foreground.Action
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.service.foreground.AutoTunnelService
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import com.zaneschepke.wireguardautotunnel.util.extensions.stopTunnelBackground
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint @AndroidEntryPoint
class ShortcutsActivity : ComponentActivity() { class ShortcutsActivity : ComponentActivity() {
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject @Inject
lateinit var appDataRepository: AppDataRepository lateinit var tunnelService: Provider<TunnelService>
@Inject @Inject
lateinit var serviceManager: ServiceManager @ApplicationScope
lateinit var applicationScope: CoroutineScope
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
WireGuardAutoTunnel.applicationScope.launch(Dispatchers.IO) { applicationScope.launch {
val settings = appDataRepository.settings.getSettings() val settings = appDataRepository.settings.getSettings()
if (settings.isShortcutsEnabled) { if (settings.isShortcutsEnabled) {
when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) { when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) {
WireGuardTunnelService::class.java.simpleName -> { LEGACY_TUNNEL_SERVICE_NAME, TunnelService::class.java.simpleName -> {
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY) val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
val tunnelConfig = tunnelName?.let { Timber.d("Tunnel name extra: $tunnelName")
appDataRepository.tunnels.getAll().firstOrNull { val tunnelConfig = tunnelName?.let {
it.name == tunnelName appDataRepository.tunnels.getAll()
} .firstOrNull { it.name == tunnelName }
} } ?: appDataRepository.getStartTunnelConfig()
when (intent.action) { Timber.d("Shortcut action on name: ${tunnelConfig?.name}")
Action.START.name -> serviceManager.startVpnServiceForeground( tunnelConfig?.let {
this@ShortcutsActivity, tunnelConfig?.id, isManualStart = true, when (intent.action) {
) Action.START.name -> this@ShortcutsActivity.startTunnelBackground(it.id)
Action.STOP.name -> this@ShortcutsActivity.stopTunnelBackground(it.id)
else -> Unit
}
}
}
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
when (intent.action) {
Action.START.name ->
appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = false,
),
)
Action.STOP.name ->
appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = true,
),
)
}
}
}
}
}
finish()
}
Action.STOP.name -> serviceManager.stopVpnServiceForeground( companion object {
this@ShortcutsActivity, const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService"
isManualStop = true, const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService"
) const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
} const val CLASS_NAME_EXTRA_KEY = "className"
} }
WireGuardConnectivityWatcherService::class.java.simpleName -> {
when (intent.action) {
Action.START.name -> appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = false,
),
)
Action.STOP.name -> appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = true,
),
)
}
}
}
}
}
finish()
}
companion object {
const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
const val CLASS_NAME_EXTRA_KEY = "className"
}
} }
@@ -1,111 +1,149 @@
package com.zaneschepke.wireguardautotunnel.service.tile package com.zaneschepke.wireguardautotunnel.service.tile
import android.content.Intent
import android.os.Build import android.os.Build
import android.os.IBinder
import android.service.quicksettings.Tile import android.service.quicksettings.Tile
import android.service.quicksettings.TileService import android.service.quicksettings.TileService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class AutoTunnelControlTile : TileService() { class AutoTunnelControlTile : TileService(), LifecycleOwner {
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject @Inject
lateinit var appDataRepository: AppDataRepository lateinit var serviceManager: ServiceManager
@Inject @Inject
lateinit var serviceManager: ServiceManager @ApplicationScope
lateinit var applicationScope: CoroutineScope
private val scope = CoroutineScope(Dispatchers.IO) private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
private var manualStartConfig: TunnelConfig? = null /* This works around an annoying unsolved frameworks bug some people are hitting. */
override fun onBind(intent: Intent): IBinder? {
var ret: IBinder? = null
try {
ret = super.onBind(intent)
} catch (e: Throwable) {
Timber.e("Failed to bind to AutoTunnelTile")
}
return ret
}
override fun onStartListening() { override fun onCreate() {
super.onStartListening() super.onCreate()
scope.launch { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
appDataRepository.settings.getSettingsFlow().collectLatest {
when (it.isAutoTunnelEnabled) {
true -> {
if (it.isAutoTunnelPaused) {
setInactive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.paused))
} else {
setActive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.active))
}
}
false -> { applicationScope.launch {
setTileDescription(this@AutoTunnelControlTile.getString(R.string.disabled)) appDataRepository.settings.getSettingsFlow().collect {
setUnavailable() kotlin.runCatching {
} when (it.isAutoTunnelEnabled) {
} true -> {
} if (it.isAutoTunnelPaused) {
} setInactive()
} setTileDescription(this@AutoTunnelControlTile.getString(R.string.paused))
} else {
setActive()
setTileDescription(this@AutoTunnelControlTile.getString(R.string.active))
}
}
override fun onTileAdded() { false -> {
super.onTileAdded() setTileDescription(this@AutoTunnelControlTile.getString(R.string.disabled))
onStartListening() setUnavailable()
} }
}
}.onFailure {
Timber.e(it)
}
}
}
}
override fun onDestroy() { override fun onStopListening() {
super.onDestroy() lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
scope.cancel() }
}
override fun onTileRemoved() { override fun onDestroy() {
super.onTileRemoved() super.onDestroy()
scope.cancel() lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
} }
override fun onClick() { override fun onStartListening() {
super.onClick() super.onStartListening()
unlockAndRun { lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
scope.launch { }
try {
appDataRepository.toggleWatcherServicePause()
} catch (e: Exception) {
Timber.e(e.message)
} finally {
cancel()
}
}
}
}
private fun setActive() { override fun onClick() {
qsTile.state = Tile.STATE_ACTIVE super.onClick()
qsTile.updateTile() unlockAndRun {
} lifecycleScope.launch {
kotlin.runCatching {
val settings = appDataRepository.settings.getSettings()
if (settings.isAutoTunnelPaused) {
return@launch appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = false,
),
)
}
appDataRepository.settings.save(
settings.copy(
isAutoTunnelPaused = true,
),
)
}
}
}
}
private fun setInactive() { private fun setActive() {
qsTile.state = Tile.STATE_INACTIVE kotlin.runCatching {
qsTile.updateTile() qsTile.state = Tile.STATE_ACTIVE
} qsTile.updateTile()
}
}
private fun setUnavailable() { private fun setInactive() {
manualStartConfig = null kotlin.runCatching {
qsTile.state = Tile.STATE_UNAVAILABLE qsTile.state = Tile.STATE_INACTIVE
qsTile.updateTile() qsTile.updateTile()
} }
}
private fun setTileDescription(description: String) { private fun setUnavailable() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { kotlin.runCatching {
qsTile.subtitle = description qsTile.state = Tile.STATE_UNAVAILABLE
} qsTile.updateTile()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { }
qsTile.stateDescription = description }
}
qsTile.updateTile() private fun setTileDescription(description: String) {
} kotlin.runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
qsTile.subtitle = description
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
qsTile.stateDescription = description
}
qsTile.updateTile()
}
}
override val lifecycle: Lifecycle
get() = lifecycleRegistry
} }
@@ -3,123 +3,127 @@ package com.zaneschepke.wireguardautotunnel.service.tile
import android.os.Build import android.os.Build
import android.service.quicksettings.Tile import android.service.quicksettings.Tile
import android.service.quicksettings.TileService import android.service.quicksettings.TileService
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import com.zaneschepke.wireguardautotunnel.util.extensions.stopTunnelBackground
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint @AndroidEntryPoint
class TunnelControlTile : TileService() { class TunnelControlTile : TileService(), LifecycleOwner {
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject @Inject
lateinit var appDataRepository: AppDataRepository lateinit var tunnelService: Provider<TunnelService>
@Inject @Inject
lateinit var vpnService: VpnService @ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
lateinit var serviceManager: ServiceManager
private val scope = CoroutineScope(Dispatchers.IO) override fun onCreate() {
super.onCreate()
Timber.d("onCreate for tile service")
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
private var manualStartConfig: TunnelConfig? = null override fun onStopListening() {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
override fun onStartListening() { override fun onDestroy() {
super.onStartListening() super.onDestroy()
Timber.d("On start listening called") lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
scope.launch { }
vpnService.vpnState.collect { it ->
when (it.status) {
TunnelState.UP -> {
setActive()
it.tunnelConfig?.name?.let { name -> setTileDescription(name) }
}
TunnelState.DOWN -> { override fun onStartListening() {
setInactive() super.onStartListening()
val config = appDataRepository.getStartTunnelConfig()?.also { config -> lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
manualStartConfig = config lifecycleScope.launch {
} ?: appDataRepository.getPrimaryOrFirstTunnel() if (appDataRepository.tunnels.getAll().isEmpty()) return@launch setUnavailable()
config?.let { updateTileState()
setTileDescription(it.name) }
} ?: setUnavailable() }
}
else -> setInactive()
}
}
}
}
override fun onDestroy() { private suspend fun updateTileState() {
super.onDestroy() val lastActive = appDataRepository.getStartTunnelConfig()
scope.cancel() lastActive?.let {
} updateTile(it)
}
}
override fun onTileRemoved() { override fun onClick() {
super.onTileRemoved() super.onClick()
scope.cancel() unlockAndRun {
} Timber.d("Click")
lifecycleScope.launch {
val context = this@TunnelControlTile
val lastActive = appDataRepository.getStartTunnelConfig()
lastActive?.let { tunnel ->
if (tunnel.isActive) return@launch context.stopTunnelBackground(tunnel.id)
context.startTunnelBackground(tunnel.id)
}
}
}
}
override fun onTileAdded() { private fun setActive() {
super.onTileAdded() kotlin.runCatching {
onStartListening() qsTile.state = Tile.STATE_ACTIVE
} qsTile.updateTile()
}
}
override fun onClick() { private fun setInactive() {
super.onClick() kotlin.runCatching {
unlockAndRun { qsTile.state = Tile.STATE_INACTIVE
scope.launch { qsTile.updateTile()
try { }
if (vpnService.getState() == TunnelState.UP) { }
serviceManager.stopVpnServiceForeground(
this@TunnelControlTile,
isManualStop = true,
)
} else {
serviceManager.startVpnServiceForeground(
this@TunnelControlTile, manualStartConfig?.id, isManualStart = true,
)
}
} catch (e: Exception) {
Timber.e(e.message)
} finally {
cancel()
}
}
}
}
private fun setActive() { private fun setUnavailable() {
qsTile.state = Tile.STATE_ACTIVE kotlin.runCatching {
qsTile.updateTile() qsTile.state = Tile.STATE_UNAVAILABLE
} setTileDescription("")
qsTile.updateTile()
}
}
private fun setInactive() { private fun setTileDescription(description: String) {
qsTile.state = Tile.STATE_INACTIVE kotlin.runCatching {
qsTile.updateTile() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
} qsTile.subtitle = description
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
qsTile.stateDescription = description
}
qsTile.updateTile()
}
}
private fun setUnavailable() { private fun updateTile(tunnelConfig: TunnelConfig?) {
manualStartConfig = null kotlin.runCatching {
qsTile.state = Tile.STATE_UNAVAILABLE tunnelConfig?.let {
qsTile.updateTile() setTileDescription(it.name)
} if (it.isActive) return setActive()
setInactive()
}
}
}
private fun setTileDescription(description: String) { override val lifecycle: Lifecycle
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { get() = lifecycleRegistry
qsTile.subtitle = description
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
qsTile.stateDescription = description
}
qsTile.updateTile()
}
} }
@@ -0,0 +1,46 @@
package com.zaneschepke.wireguardautotunnel.service.tunnel
import android.content.Intent
import android.os.IBinder
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint
class AlwaysOnVpnService : LifecycleService() {
@Inject
lateinit var tunnelService: Provider<TunnelService>
@Inject
lateinit var appDataRepository: AppDataRepository
override fun onBind(intent: Intent): IBinder? {
super.onBind(intent)
// We don't provide binding, so return null
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null || intent.component == null || intent.component!!.packageName != packageName) {
Timber.i("Always-on VPN requested started")
lifecycleScope.launch {
val settings = appDataRepository.settings.getSettings()
if (settings.isAlwaysOnVpnEnabled) {
val tunnel = appDataRepository.getPrimaryOrFirstTunnel()
tunnel?.let {
tunnelService.get().startTunnel(it)
}
} else {
Timber.w("Always-on VPN is not enabled in app settings")
}
}
}
return super.onStartCommand(intent, flags, startId)
}
}
@@ -1,16 +1,17 @@
package com.zaneschepke.wireguardautotunnel.service.tunnel package com.zaneschepke.wireguardautotunnel.service.tunnel
enum class HandshakeStatus { enum class HandshakeStatus {
HEALTHY, HEALTHY,
STALE, STALE,
UNKNOWN, UNKNOWN,
NOT_STARTED; NOT_STARTED,
;
companion object { companion object {
private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 180 private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 180
const val STATUS_CHANGE_TIME_BUFFER = 30 const val STATUS_CHANGE_TIME_BUFFER = 30
const val STALE_TIME_LIMIT_SEC = const val STALE_TIME_LIMIT_SEC =
WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC + STATUS_CHANGE_TIME_BUFFER WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC + STATUS_CHANGE_TIME_BUFFER
const val NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC = 30 const val NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC = 30
} }
} }
@@ -0,0 +1,20 @@
package com.zaneschepke.wireguardautotunnel.service.tunnel
import com.wireguard.android.backend.Tunnel
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import kotlinx.coroutines.flow.StateFlow
interface TunnelService : Tunnel, org.amnezia.awg.backend.Tunnel {
suspend fun startTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState>
val vpnState: StateFlow<VpnState>
suspend fun runningTunnelNames(): Set<String>
suspend fun getState(): TunnelState
fun cancelStatsJob()
fun startStatsJob()
}
@@ -3,40 +3,42 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel
import com.wireguard.android.backend.Tunnel import com.wireguard.android.backend.Tunnel
enum class TunnelState { enum class TunnelState {
UP, UP,
DOWN, DOWN,
TOGGLE; TOGGLE,
;
fun toWgState() : Tunnel.State { fun toWgState(): Tunnel.State {
return when(this) { return when (this) {
UP -> Tunnel.State.UP UP -> Tunnel.State.UP
DOWN -> Tunnel.State.DOWN DOWN -> Tunnel.State.DOWN
TOGGLE -> Tunnel.State.TOGGLE TOGGLE -> Tunnel.State.TOGGLE
} }
} }
fun toAmState() : org.amnezia.awg.backend.Tunnel.State { fun toAmState(): org.amnezia.awg.backend.Tunnel.State {
return when(this) { return when (this) {
UP -> org.amnezia.awg.backend.Tunnel.State.UP UP -> org.amnezia.awg.backend.Tunnel.State.UP
DOWN -> org.amnezia.awg.backend.Tunnel.State.DOWN DOWN -> org.amnezia.awg.backend.Tunnel.State.DOWN
TOGGLE -> org.amnezia.awg.backend.Tunnel.State.TOGGLE TOGGLE -> org.amnezia.awg.backend.Tunnel.State.TOGGLE
} }
} }
companion object { companion object {
fun from(state: Tunnel.State) : TunnelState { fun from(state: Tunnel.State): TunnelState {
return when(state) { return when (state) {
Tunnel.State.DOWN -> DOWN Tunnel.State.DOWN -> DOWN
Tunnel.State.TOGGLE -> TOGGLE Tunnel.State.TOGGLE -> TOGGLE
Tunnel.State.UP -> UP Tunnel.State.UP -> UP
} }
} }
fun from(state: org.amnezia.awg.backend.Tunnel.State) : TunnelState {
return when(state) { fun from(state: org.amnezia.awg.backend.Tunnel.State): TunnelState {
org.amnezia.awg.backend.Tunnel.State.DOWN -> DOWN return when (state) {
org.amnezia.awg.backend.Tunnel.State.TOGGLE -> TOGGLE org.amnezia.awg.backend.Tunnel.State.DOWN -> DOWN
org.amnezia.awg.backend.Tunnel.State.UP -> UP org.amnezia.awg.backend.Tunnel.State.TOGGLE -> TOGGLE
} org.amnezia.awg.backend.Tunnel.State.UP -> UP
} }
} }
}
} }
@@ -1,15 +0,0 @@
package com.zaneschepke.wireguardautotunnel.service.tunnel
import com.wireguard.android.backend.Tunnel
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import kotlinx.coroutines.flow.StateFlow
interface VpnService : Tunnel, org.amnezia.awg.backend.Tunnel {
suspend fun startTunnel(tunnelConfig: TunnelConfig? = null): TunnelState
suspend fun stopTunnel()
val vpnState: StateFlow<VpnState>
fun getState(): TunnelState
}
@@ -4,7 +4,7 @@ import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
data class VpnState( data class VpnState(
val status: TunnelState = TunnelState.DOWN, val status: TunnelState = TunnelState.DOWN,
val tunnelConfig: TunnelConfig? = null, val tunnelConfig: TunnelConfig? = null,
val statistics: TunnelStatistics? = null val statistics: TunnelStatistics? = null,
) )
@@ -1,193 +1,202 @@
package com.zaneschepke.wireguardautotunnel.service.tunnel package com.zaneschepke.wireguardautotunnel.service.tunnel
import com.wireguard.android.backend.Backend import com.wireguard.android.backend.Backend
import com.wireguard.android.backend.BackendException
import com.wireguard.android.backend.Tunnel.State import com.wireguard.android.backend.Tunnel.State
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.module.Kernel import com.zaneschepke.wireguardautotunnel.module.Kernel
import com.zaneschepke.wireguardautotunnel.module.Userspace import com.zaneschepke.wireguardautotunnel.module.Userspace
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.AmneziaStatistics
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.WireGuardStatistics
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import kotlinx.coroutines.CancellationException import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
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 javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider
class WireGuardTunnel class WireGuardTunnel
@Inject @Inject
constructor( constructor(
private val userspaceAmneziaBackend : org.amnezia.awg.backend.Backend, private val amneziaBackend: Provider<org.amnezia.awg.backend.Backend>,
@Userspace private val userspaceBackend: Backend, @Userspace private val userspaceBackend: Provider<Backend>,
@Kernel private val kernelBackend: Backend, @Kernel private val kernelBackend: Provider<Backend>,
private val appDataRepository: AppDataRepository, private val appDataRepository: AppDataRepository,
) : VpnService { @ApplicationScope private val applicationScope: CoroutineScope,
private val _vpnState = MutableStateFlow(VpnState()) @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
override val vpnState: StateFlow<VpnState> = _vpnState.asStateFlow() ) : TunnelService {
private val _vpnState = MutableStateFlow(VpnState())
override val vpnState: StateFlow<VpnState> = _vpnState.asStateFlow()
private val scope = CoroutineScope(Dispatchers.IO) override suspend fun runningTunnelNames(): Set<String> {
return when (val backend = backend()) {
is Backend -> backend.runningTunnelNames
is org.amnezia.awg.backend.Backend -> backend.runningTunnelNames
else -> emptySet()
}
}
private var statsJob: Job? = null private var statsJob: Job? = null
private var backend: Backend = userspaceBackend private suspend fun setState(tunnelConfig: TunnelConfig, tunnelState: TunnelState): Result<TunnelState> {
return runCatching {
when (val backend = backend()) {
is Backend -> backend.setState(this, tunnelState.toWgState(), TunnelConfig.configFromWgQuick(tunnelConfig.wgQuick)).let { TunnelState.from(it) }
is org.amnezia.awg.backend.Backend -> {
val config = if (tunnelConfig.amQuick.isBlank()) {
TunnelConfig.configFromAmQuick(
tunnelConfig.wgQuick,
)
} else {
TunnelConfig.configFromAmQuick(tunnelConfig.amQuick)
}
backend.setState(this, tunnelState.toAmState(), config).let {
TunnelState.from(it)
}
}
else -> throw NotImplementedError()
}
}.onFailure {
Timber.e(it)
}
}
private var backendIsWgUserspace = true private suspend fun backend(): Any {
val settings = appDataRepository.settings.getSettings()
if (settings.isKernelEnabled) return kernelBackend.get()
if (settings.isAmneziaEnabled) return amneziaBackend.get()
return userspaceBackend.get()
}
private var backendIsAmneziaUserspace = false override suspend fun startTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
return withContext(ioDispatcher) {
if (_vpnState.value.status == TunnelState.UP) vpnState.value.tunnelConfig?.let { stopTunnel(it) }
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
appDataRepository.appState.setLastActiveTunnelId(tunnelConfig.id)
emitTunnelConfig(tunnelConfig)
setState(tunnelConfig, TunnelState.UP).onSuccess {
emitTunnelState(it)
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
}.onFailure {
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
}
}
}
init { override suspend fun stopTunnel(tunnelConfig: TunnelConfig): Result<TunnelState> {
scope.launch { return withContext(ioDispatcher) {
appDataRepository.settings.getSettingsFlow().collect { appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
if (it.isKernelEnabled && (backendIsWgUserspace || backendIsAmneziaUserspace)) { setState(tunnelConfig, TunnelState.DOWN).onSuccess {
Timber.d("Setting kernel backend") emitTunnelState(it)
backend = kernelBackend resetBackendStatistics()
backendIsWgUserspace = false WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
backendIsAmneziaUserspace = false }.onFailure {
} else if (!it.isKernelEnabled && !it.isAmneziaEnabled && !backendIsWgUserspace) { Timber.e(it)
Timber.d("Setting WireGuard userspace backend") appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
backend = userspaceBackend WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
backendIsWgUserspace = true }
backendIsAmneziaUserspace = false }
} else if (it.isAmneziaEnabled && !backendIsAmneziaUserspace) { }
Timber.d("Setting Amnezia userspace backend")
backendIsAmneziaUserspace = true
backendIsWgUserspace = false
}
}
}
}
private fun setState(tunnelConfig: TunnelConfig?, tunnelState: TunnelState) : TunnelState { private fun emitTunnelState(state: TunnelState) {
return if(backendIsAmneziaUserspace) { _vpnState.tryEmit(
Timber.i("Using Amnezia backend") _vpnState.value.copy(
val config = tunnelConfig?.let { status = state,
if(it.amQuick != "") TunnelConfig.configFromAmQuick(it.amQuick) else { ),
Timber.w("Using backwards compatible wg config, amnezia specific config not found.") )
TunnelConfig.configFromAmQuick(it.wgQuick) }
}
}
val state = userspaceAmneziaBackend.setState(this, tunnelState.toAmState(), config)
TunnelState.from(state)
} else {
Timber.i("Using Wg backend")
val wgConfig = tunnelConfig?.let { TunnelConfig.configFromWgQuick(it.wgQuick) }
val state = backend.setState(
this,
tunnelState.toWgState(),
wgConfig,
)
TunnelState.from(state)
}
}
override suspend fun startTunnel(tunnelConfig: TunnelConfig?): TunnelState { private fun emitBackendStatistics(statistics: TunnelStatistics) {
return try { _vpnState.tryEmit(
//TODO we need better error handling here _vpnState.value.copy(
val config = tunnelConfig ?: appDataRepository.getPrimaryOrFirstTunnel() statistics = statistics,
if (config != null) { ),
emitTunnelConfig(config) )
setState(config, TunnelState.UP) }
} else throw Exception("No tunnels")
} catch (e: BackendException) {
Timber.e("Failed to start tunnel with error: ${e.message}")
TunnelState.from(State.DOWN)
}
}
private fun emitTunnelState(state : TunnelState) { private fun emitTunnelConfig(tunnelConfig: TunnelConfig?) {
_vpnState.tryEmit( _vpnState.tryEmit(
_vpnState.value.copy( _vpnState.value.copy(
status = state, tunnelConfig = tunnelConfig,
), ),
) )
} }
private fun emitBackendStatistics(statistics: TunnelStatistics) { private fun resetBackendStatistics() {
_vpnState.tryEmit( _vpnState.tryEmit(
_vpnState.value.copy( _vpnState.value.copy(
statistics = statistics, statistics = null,
), ),
) )
} }
private suspend fun emitTunnelConfig(tunnelConfig: TunnelConfig?) { override suspend fun getState(): TunnelState {
_vpnState.emit( return when (val backend = backend()) {
_vpnState.value.copy( is Backend -> backend.getState(this).let { TunnelState.from(it) }
tunnelConfig = tunnelConfig, is org.amnezia.awg.backend.Backend -> backend.getState(this).let { TunnelState.from(it) }
), else -> TunnelState.DOWN
) }
} }
private fun resetVpnState() { override fun cancelStatsJob() {
_vpnState.tryEmit(VpnState()) statsJob?.cancel()
} }
override suspend fun stopTunnel() { override fun startStatsJob() {
try { statsJob = startTunnelStatisticsJob()
if (getState() == TunnelState.UP) { }
val state = setState(null, TunnelState.DOWN)
resetVpnState()
emitTunnelState(state)
}
} catch (e: BackendException) {
Timber.e("Failed to stop wireguard tunnel with error: ${e.message}")
} catch (e: org.amnezia.awg.backend.BackendException) {
Timber.e("Failed to stop amnezia tunnel with error: ${e.message}")
}
}
override fun getState(): TunnelState { override fun getName(): String {
return if(backendIsAmneziaUserspace) TunnelState.from(userspaceAmneziaBackend.getState(this)) return _vpnState.value.tunnelConfig?.name ?: ""
else TunnelState.from(backend.getState(this)) }
}
override fun getName(): String { override fun onStateChange(newState: Tunnel.State) {
return _vpnState.value.tunnelConfig?.name ?: "" handleStateChange(TunnelState.from(newState))
} }
private fun handleStateChange(state: TunnelState) {
emitTunnelState(state)
WireGuardAutoTunnel.instance.requestTunnelTileServiceStateUpdate()
when (state) {
TunnelState.UP -> startStatsJob()
else -> cancelStatsJob()
}
}
override fun onStateChange(newState: Tunnel.State) { private fun startTunnelStatisticsJob() = applicationScope.launch(ioDispatcher) {
handleStateChange(TunnelState.from(newState)) val backend = backend()
} while (true) {
when (backend) {
is Backend -> emitBackendStatistics(
WireGuardStatistics(backend.getStatistics(this@WireGuardTunnel)),
)
is org.amnezia.awg.backend.Backend -> {
emitBackendStatistics(
AmneziaStatistics(
backend.getStatistics(this@WireGuardTunnel),
),
)
}
}
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
}
}
private fun handleStateChange(state: TunnelState) { override fun onStateChange(state: State) {
val tunnel = this handleStateChange(TunnelState.from(state))
emitTunnelState(state) }
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
if (state == TunnelState.UP) {
statsJob =
scope.launch {
while (true) {
if(backendIsAmneziaUserspace) {
emitBackendStatistics(AmneziaStatistics(userspaceAmneziaBackend.getStatistics(tunnel)))
} else {
emitBackendStatistics(WireGuardStatistics(backend.getStatistics(tunnel)))
}
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
}
}
}
if (state == TunnelState.DOWN) {
try {
statsJob?.cancel()
} catch (e : CancellationException) {
Timber.i("Stats job cancelled")
}
}
}
override fun onStateChange(state: State) {
handleStateChange(TunnelState.from(state))
}
} }
@@ -4,31 +4,31 @@ import org.amnezia.awg.backend.Statistics
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
class AmneziaStatistics(private val statistics: Statistics) : TunnelStatistics() { class AmneziaStatistics(private val statistics: Statistics) : TunnelStatistics() {
override fun peerStats(peer: Key): PeerStats? { override fun peerStats(peer: Key): PeerStats? {
val key = Key.fromBase64(peer.toBase64()) val key = Key.fromBase64(peer.toBase64())
val stats = statistics.peer(key) val stats = statistics.peer(key)
return stats?.let { return stats?.let {
PeerStats( PeerStats(
rxBytes = stats.rxBytes, rxBytes = stats.rxBytes,
txBytes = stats.txBytes, txBytes = stats.txBytes,
latestHandshakeEpochMillis = stats.latestHandshakeEpochMillis latestHandshakeEpochMillis = stats.latestHandshakeEpochMillis,
) )
} }
} }
override fun isTunnelStale(): Boolean { override fun isTunnelStale(): Boolean {
return statistics.isStale return statistics.isStale
} }
override fun getPeers(): Array<Key> { override fun getPeers(): Array<Key> {
return statistics.peers() return statistics.peers()
} }
override fun rx(): Long { override fun rx(): Long {
return statistics.totalRx() return statistics.totalRx()
} }
override fun tx(): Long { override fun tx(): Long {
return statistics.totalTx() return statistics.totalTx()
} }
} }
@@ -2,17 +2,17 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel.statistics
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
abstract class TunnelStatistics { abstract class TunnelStatistics {
@JvmRecord @JvmRecord
data class PeerStats(val rxBytes: Long, val txBytes: Long, val latestHandshakeEpochMillis: Long) data class PeerStats(val rxBytes: Long, val txBytes: Long, val latestHandshakeEpochMillis: Long)
abstract fun peerStats(peer: Key): PeerStats? abstract fun peerStats(peer: Key): PeerStats?
abstract fun isTunnelStale() : Boolean abstract fun isTunnelStale(): Boolean
abstract fun getPeers(): Array<Key> abstract fun getPeers(): Array<Key>
abstract fun rx() : Long abstract fun rx(): Long
abstract fun tx() : Long abstract fun tx(): Long
} }
@@ -3,34 +3,34 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel.statistics
import com.wireguard.android.backend.Statistics import com.wireguard.android.backend.Statistics
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
class WireGuardStatistics(private val statistics: Statistics) : TunnelStatistics() { class WireGuardStatistics(private val statistics: Statistics) : TunnelStatistics() {
override fun peerStats(peer: Key): PeerStats? { override fun peerStats(peer: Key): PeerStats? {
val key = com.wireguard.crypto.Key.fromBase64(peer.toBase64()) val key = com.wireguard.crypto.Key.fromBase64(peer.toBase64())
val peerStats = statistics.peer(key) val peerStats = statistics.peer(key)
return peerStats?.let { return peerStats?.let {
PeerStats( PeerStats(
txBytes = peerStats.txBytes, txBytes = peerStats.txBytes,
rxBytes = peerStats.rxBytes, rxBytes = peerStats.rxBytes,
latestHandshakeEpochMillis = peerStats.latestHandshakeEpochMillis latestHandshakeEpochMillis = peerStats.latestHandshakeEpochMillis,
) )
} }
} }
override fun isTunnelStale(): Boolean { override fun isTunnelStale(): Boolean {
return statistics.isStale return statistics.isStale
} }
override fun getPeers(): Array<Key> { override fun getPeers(): Array<Key> {
return statistics.peers().map { return statistics.peers().map {
Key.fromBase64(it.toBase64()) Key.fromBase64(it.toBase64())
}.toTypedArray() }.toTypedArray()
} }
override fun rx(): Long { override fun rx(): Long {
return statistics.totalRx() return statistics.totalRx()
} }
override fun tx(): Long { override fun tx(): Long {
return statistics.totalTx() return statistics.totalTx()
} }
} }
@@ -1,9 +1,8 @@
package com.zaneschepke.wireguardautotunnel.ui package com.zaneschepke.wireguardautotunnel.ui
data class AppUiState( data class AppUiState(
val snackbarMessage: String = "", val snackbarMessage: String = "",
val snackbarMessageConsumed: Boolean = true, val snackbarMessageConsumed: Boolean = true,
val vpnPermissionAccepted: Boolean = false, val notificationPermissionAccepted: Boolean = false,
val notificationPermissionAccepted: Boolean = false, val requestPermissions: Boolean = false,
val requestPermissions: Boolean = false
) )
@@ -1,159 +1,53 @@
package com.zaneschepke.wireguardautotunnel.ui package com.zaneschepke.wireguardautotunnel.ui
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.wireguard.android.backend.GoBackend import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.logcatter.Logcatter
import com.zaneschepke.logcatter.model.LogMessage
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.FileUtils
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
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.launch import kotlinx.coroutines.launch
import timber.log.Timber import xyz.teamgravity.pin_lock_compose.PinManager
import java.time.Instant
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
class AppViewModel class AppViewModel
@Inject @Inject
constructor() : ViewModel() { constructor(
private val appDataRepository: AppDataRepository,
) : ViewModel() {
val vpnIntent: Intent? = GoBackend.VpnService.prepare(WireGuardAutoTunnel.instance) private val _appUiState =
MutableStateFlow(
AppUiState(),
)
val appUiState = _appUiState.asStateFlow()
private val _appUiState = MutableStateFlow( fun showSnackbarMessage(message: String) {
AppUiState( _appUiState.update {
vpnPermissionAccepted = vpnIntent == null, it.copy(
), snackbarMessage = message,
) snackbarMessageConsumed = false,
val appUiState = _appUiState.asStateFlow() )
}
}
fun snackbarMessageConsumed() {
_appUiState.update {
it.copy(
snackbarMessage = "",
snackbarMessageConsumed = true,
)
}
}
fun isRequiredPermissionGranted(): Boolean { fun onPinLockDisabled() = viewModelScope.launch {
val allAccepted = PinManager.clearPin()
(_appUiState.value.vpnPermissionAccepted && _appUiState.value.vpnPermissionAccepted) appDataRepository.appState.setPinLockEnabled(false)
if (!allAccepted) requestPermissions() }
return allAccepted
}
private fun requestPermissions() { fun onPinLockEnabled() = viewModelScope.launch {
_appUiState.update { appDataRepository.appState.setPinLockEnabled(true)
it.copy( }
requestPermissions = true
)
}
}
fun permissionsRequested() {
_appUiState.update {
it.copy(
requestPermissions = false
)
}
}
fun openWebPage(url: String, context : Context) {
try {
val webpage: Uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, webpage).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Timber.e(e)
showSnackbarMessage(context.getString(R.string.no_browser_detected))
}
}
fun onVpnPermissionAccepted() {
_appUiState.update {
it.copy(
vpnPermissionAccepted = true
)
}
}
fun launchEmail(context: Context) {
try {
val intent =
Intent(Intent.ACTION_SENDTO).apply {
type = Constants.EMAIL_MIME_TYPE
putExtra(Intent.EXTRA_EMAIL, arrayOf(context.getString(R.string.my_email)))
putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.email_subject))
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(
Intent.createChooser(intent, context.getString(R.string.email_chooser)).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
},
)
} catch (e: ActivityNotFoundException) {
Timber.e(e)
showSnackbarMessage(context.getString(R.string.no_email_detected))
}
}
fun showSnackbarMessage(message: String) {
_appUiState.update {
it.copy(
snackbarMessage = message,
snackbarMessageConsumed = false,
)
}
}
fun snackbarMessageConsumed() {
_appUiState.update {
it.copy(
snackbarMessage = "",
snackbarMessageConsumed = true,
)
}
}
val logs = mutableStateListOf<LogMessage>()
fun readLogCatOutput() =
viewModelScope.launch(viewModelScope.coroutineContext + Dispatchers.IO) {
launch {
Logcatter.logs(callback = {
logs.add(it)
if (logs.size > Constants.LOG_BUFFER_SIZE) {
logs.removeRange(0, (logs.size - Constants.LOG_BUFFER_SIZE).toInt())
}
})
}
}
fun clearLogs() {
logs.clear()
Logcatter.clear()
}
fun saveLogsToFile(context: Context) {
val fileName = "${Constants.BASE_LOG_FILE_NAME}-${Instant.now().epochSecond}.txt"
val content = logs.joinToString(separator = "\n")
FileUtils.saveFileToDownloads(context.applicationContext, content, fileName)
Toast.makeText(context, context.getString(R.string.logs_saved), Toast.LENGTH_SHORT)
.show()
}
fun setNotificationPermissionAccepted(accepted: Boolean) {
_appUiState.update {
it.copy(
notificationPermissionAccepted = accepted,
)
}
}
} }
@@ -1,5 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui
import com.journeyapps.barcodescanner.CaptureActivity
class CaptureActivityPortrait : CaptureActivity()
@@ -1,14 +1,13 @@
package com.zaneschepke.wireguardautotunnel.ui package com.zaneschepke.wireguardautotunnel.ui
import android.Manifest
import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.activity.SystemBarStyle import androidx.activity.SystemBarStyle
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.focusable import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@@ -19,6 +18,7 @@ import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Surface
import androidx.compose.material3.surfaceColorAtElevation import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
@@ -38,14 +38,8 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument import androidx.navigation.navArgument
import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
import com.google.accompanist.permissions.isGranted import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.google.accompanist.permissions.rememberPermissionState
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar import com.zaneschepke.wireguardautotunnel.ui.common.prompt.CustomSnackBar
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen
@@ -57,233 +51,188 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.StringValue
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import xyz.teamgravity.pin_lock_compose.PinManager
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@Inject
lateinit var appStateRepository: AppStateRepository
@Inject @Inject
lateinit var dataStoreManager: DataStoreManager lateinit var tunnelService: TunnelService
@Inject override fun onCreate(savedInstanceState: Bundle?) {
lateinit var settingsRepository: SettingsRepository super.onCreate(savedInstanceState)
@Inject val isPinLockEnabled = intent.extras?.getBoolean(SplashActivity.IS_PIN_LOCK_ENABLED_KEY)
lateinit var serviceManager: ServiceManager
@OptIn( enableEdgeToEdge(navigationBarStyle = SystemBarStyle.dark(Color.Transparent.toArgb()))
ExperimentalPermissionsApi::class,
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge(navigationBarStyle = SystemBarStyle.dark(Color.Transparent.toArgb())) setContent {
val appViewModel = hiltViewModel<AppViewModel>()
val appUiState by appViewModel.appUiState.collectAsStateWithLifecycle()
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
// load preferences into memory and init data val snackbarHostState = remember { SnackbarHostState() }
lifecycleScope.launch {
dataStoreManager.init()
WireGuardAutoTunnel.requestTunnelTileServiceStateUpdate()
val settings = settingsRepository.getSettings()
if (settings.isAutoTunnelEnabled) {
serviceManager.startWatcherService(application.applicationContext)
}
}
setContent {
val appViewModel = hiltViewModel<AppViewModel>()
val appUiState by appViewModel.appUiState.collectAsStateWithLifecycle()
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val notificationPermissionState = fun showSnackBarMessage(message: StringValue) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) lifecycleScope.launch(Dispatchers.Main) {
rememberPermissionState(Manifest.permission.POST_NOTIFICATIONS) else null val result =
snackbarHostState.showSnackbar(
message = message.asString(this@MainActivity),
duration = SnackbarDuration.Short,
)
when (result) {
SnackbarResult.ActionPerformed,
SnackbarResult.Dismissed,
-> {
snackbarHostState.currentSnackbarData?.dismiss()
}
}
}
}
val snackbarHostState = remember { SnackbarHostState() } WireguardAutoTunnelTheme {
LaunchedEffect(appUiState.snackbarMessageConsumed) {
if (!appUiState.snackbarMessageConsumed) {
showSnackBarMessage(StringValue.DynamicString(appUiState.snackbarMessage))
appViewModel.snackbarMessageConsumed()
}
}
val vpnActivityResultState = val focusRequester = remember { FocusRequester() }
rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult(),
onResult = {
val accepted = (it.resultCode == RESULT_OK)
if (accepted) {
appViewModel.onVpnPermissionAccepted()
}
},
)
fun showSnackBarMessage(message: StringValue) { Scaffold(
lifecycleScope.launch(Dispatchers.Main) { snackbarHost = {
val result = SnackbarHost(snackbarHostState) { snackbarData: SnackbarData ->
snackbarHostState.showSnackbar( CustomSnackBar(
message = message.asString(this@MainActivity), snackbarData.visuals.message,
duration = SnackbarDuration.Short, isRtl = false,
) containerColor =
when (result) { MaterialTheme.colorScheme.surfaceColorAtElevation(
SnackbarResult.ActionPerformed, 2.dp,
SnackbarResult.Dismissed -> { ),
snackbarHostState.currentSnackbarData?.dismiss() )
} }
} },
} containerColor = MaterialTheme.colorScheme.background,
} modifier =
Modifier
.focusable()
.focusProperties {
when (navBackStackEntry?.destination?.route) {
Screen.Lock.route -> Unit
else -> up = focusRequester
}
},
bottomBar = {
BottomNavBar(
navController,
listOf(
Screen.Main.navItem,
Screen.Settings.navItem,
Screen.Support.navItem,
),
)
},
) { padding ->
Surface(modifier = Modifier.fillMaxSize().padding(padding)) {
NavHost(
navController,
enterTransition = { fadeIn(tween(Constants.TRANSITION_ANIMATION_TIME)) },
exitTransition = { fadeOut(tween(Constants.TRANSITION_ANIMATION_TIME)) },
startDestination = (if (isPinLockEnabled == true) Screen.Lock.route else Screen.Main.route),
) {
composable(
Screen.Main.route,
) {
MainScreen(
focusRequester = focusRequester,
appViewModel = appViewModel,
navController = navController,
)
}
composable(
Screen.Settings.route,
) {
SettingsScreen(
appViewModel = appViewModel,
navController = navController,
focusRequester = focusRequester,
)
}
composable(
Screen.Support.route,
) {
SupportScreen(
focusRequester = focusRequester,
navController = navController,
)
}
composable(Screen.Support.Logs.route) {
LogsScreen()
}
composable(
"${Screen.Config.route}/{id}?configType={configType}",
arguments =
listOf(
navArgument("id") {
type = NavType.StringType
defaultValue = "0"
},
navArgument("configType") {
type = NavType.StringType
defaultValue = ConfigType.WIREGUARD.name
},
),
) {
val id = it.arguments?.getString("id")
val configType =
ConfigType.valueOf(
it.arguments?.getString("configType") ?: ConfigType.WIREGUARD.name,
)
if (!id.isNullOrBlank()) {
ConfigScreen(
navController = navController,
tunnelId = id,
appViewModel = appViewModel,
focusRequester = focusRequester,
configType = configType,
)
}
}
composable("${Screen.Option.route}/{id}") {
val id = it.arguments?.getString("id")
if (!id.isNullOrBlank()) {
OptionsScreen(
navController = navController,
tunnelId = id,
appViewModel = appViewModel,
focusRequester = focusRequester,
)
}
}
composable(Screen.Lock.route) {
PinLockScreen(
navController = navController,
appViewModel = appViewModel,
)
}
}
}
}
}
}
}
LaunchedEffect(appUiState.requestPermissions) { override fun onDestroy() {
if (appUiState.requestPermissions) { super.onDestroy()
appViewModel.permissionsRequested() tunnelService.cancelStatsJob()
if (notificationPermissionState != null && !notificationPermissionState.status.isGranted }
) {
showSnackBarMessage(StringValue.StringResource(R.string.notification_permission_required))
return@LaunchedEffect notificationPermissionState.launchPermissionRequest()
}
if (!appUiState.vpnPermissionAccepted) {
return@LaunchedEffect appViewModel.vpnIntent?.let {
vpnActivityResultState.launch(
it
)
}!!
}
}
}
WireguardAutoTunnelTheme {
LaunchedEffect(Unit) {
appViewModel.setNotificationPermissionAccepted(
notificationPermissionState?.status?.isGranted ?: true,
)
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) appViewModel.readLogCatOutput()
}
LaunchedEffect(appUiState.snackbarMessageConsumed) {
if (!appUiState.snackbarMessageConsumed) {
showSnackBarMessage(StringValue.DynamicString(appUiState.snackbarMessage))
appViewModel.snackbarMessageConsumed()
}
}
val focusRequester = remember { FocusRequester() }
Scaffold(
snackbarHost = {
SnackbarHost(snackbarHostState) { snackbarData: SnackbarData ->
CustomSnackBar(
snackbarData.visuals.message,
isRtl = false,
containerColor =
MaterialTheme.colorScheme.surfaceColorAtElevation(
2.dp,
),
)
}
},
//TODO refactor
modifier = Modifier
.focusable()
.focusProperties {
when (navBackStackEntry?.destination?.route) {
Screen.Lock.route -> Unit
else -> up = focusRequester
}
},
bottomBar = {
BottomNavBar(
navController,
listOf(
Screen.Main.navItem,
Screen.Settings.navItem,
Screen.Support.navItem,
),
)
},
) { padding ->
NavHost(
navController,
startDestination =
(if (PinManager.pinExists()) Screen.Lock.route else Screen.Main.route),
modifier =
Modifier
.padding(padding)
.fillMaxSize(),
) {
composable(
Screen.Main.route,
) {
MainScreen(
focusRequester = focusRequester,
appViewModel = appViewModel,
navController = navController,
)
}
composable(
Screen.Settings.route,
) {
SettingsScreen(
appViewModel = appViewModel,
navController = navController,
focusRequester = focusRequester,
)
}
composable(
Screen.Support.route,
) {
SupportScreen(
focusRequester = focusRequester,
appViewModel = appViewModel,
navController = navController,
)
}
composable(Screen.Support.Logs.route) {
LogsScreen(appViewModel)
}
//TODO fix navigation for amnezia
composable("${Screen.Config.route}/{id}?configType={configType}", arguments =
listOf(
navArgument("id") {
type = NavType.StringType
defaultValue = "0"
},
navArgument("configType") {
type = NavType.StringType
defaultValue = ConfigType.WIREGUARD.name
}
)
) {
val id = it.arguments?.getString("id")
val configType = ConfigType.valueOf( it.arguments?.getString("configType") ?: ConfigType.WIREGUARD.name)
if (!id.isNullOrBlank()) {
ConfigScreen(
navController = navController,
tunnelId = id,
appViewModel = appViewModel,
focusRequester = focusRequester,
configType = configType
)
}
}
composable("${Screen.Option.route}/{id}") {
val id = it.arguments?.getString("id")
if (!id.isNullOrBlank()) {
OptionsScreen(
navController = navController,
tunnelId = id,
appViewModel = appViewModel,
focusRequester = focusRequester,
)
}
}
composable(Screen.Lock.route) {
PinLockScreen(
navController = navController,
appViewModel = appViewModel,
)
}
}
}
}
}
}
} }
@@ -9,37 +9,38 @@ import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
sealed class Screen(val route: String) { sealed class Screen(val route: String) {
data object Main : Screen("main") { data object Main : Screen("main") {
val navItem = val navItem =
BottomNavItem( BottomNavItem(
name = WireGuardAutoTunnel.instance.getString(R.string.tunnels), name = WireGuardAutoTunnel.instance.getString(R.string.tunnels),
route = route, route = route,
icon = Icons.Rounded.Home, icon = Icons.Rounded.Home,
) )
} }
data object Settings : Screen("settings") { data object Settings : Screen("settings") {
val navItem = val navItem =
BottomNavItem( BottomNavItem(
name = WireGuardAutoTunnel.instance.getString(R.string.settings), name = WireGuardAutoTunnel.instance.getString(R.string.settings),
route = route, route = route,
icon = Icons.Rounded.Settings, icon = Icons.Rounded.Settings,
) )
} }
data object Support : Screen("support") { data object Support : Screen("support") {
val navItem = val navItem =
BottomNavItem( BottomNavItem(
name = WireGuardAutoTunnel.instance.getString(R.string.support), name = WireGuardAutoTunnel.instance.getString(R.string.support),
route = route, route = route,
icon = Icons.Rounded.QuestionMark, icon = Icons.Rounded.QuestionMark,
) )
data object Logs : Screen("support/logs") data object Logs : Screen("support/logs")
} }
data object Config : Screen("config") data object Config : Screen("config")
data object Lock : Screen("lock")
data object Option : Screen("option") data object Lock : Screen("lock")
data object Option : Screen("option")
} }
@@ -0,0 +1,75 @@
package com.zaneschepke.wireguardautotunnel.ui
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import xyz.teamgravity.pin_lock_compose.PinManager
import javax.inject.Inject
import javax.inject.Provider
@SuppressLint("CustomSplashScreen")
@AndroidEntryPoint
class SplashActivity : ComponentActivity() {
@Inject
lateinit var appStateRepository: AppStateRepository
@Inject
lateinit var appDataRepository: AppDataRepository
@Inject
lateinit var tunnelService: Provider<TunnelService>
@Inject
lateinit var serviceManager: ServiceManager
override fun onCreate(savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { true }
}
super.onCreate(savedInstanceState)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val pinLockEnabled = appStateRepository.isPinLockEnabled()
if (pinLockEnabled) {
PinManager.initialize(WireGuardAutoTunnel.instance)
}
val settings = appDataRepository.settings.getSettings()
if (settings.isAutoTunnelEnabled) serviceManager.startWatcherService(application.applicationContext)
if (tunnelService.get().getState() == TunnelState.UP) tunnelService.get().startStatsJob()
val tunnels = appDataRepository.tunnels.getActive()
if (tunnels.isNotEmpty() && tunnelService.get().getState() == TunnelState.DOWN) tunnelService.get().startTunnel(tunnels.first())
requestTunnelTileServiceStateUpdate()
requestAutoTunnelTileServiceUpdate()
val intent =
Intent(this@SplashActivity, MainActivity::class.java).apply {
putExtra(IS_PIN_LOCK_ENABLED_KEY, pinLockEnabled)
}
startActivity(intent)
finish()
}
}
}
companion object {
const val IS_PIN_LOCK_ENABLED_KEY = "is_pin_lock_enabled"
}
}
@@ -12,31 +12,25 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
@Composable @Composable
fun ClickableIconButton( fun ClickableIconButton(onClick: () -> Unit, onIconClick: () -> Unit, text: String, icon: ImageVector, enabled: Boolean) {
onClick: () -> Unit, TextButton(
onIconClick: () -> Unit, onClick = onClick,
text: String, enabled = enabled,
icon: ImageVector, ) {
enabled: Boolean Text(text, Modifier.weight(1f, false))
) { Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
TextButton( Icon(
onClick = onClick, imageVector = icon,
enabled = enabled, contentDescription = icon.name,
) { modifier =
Text(text, Modifier.weight(1f, false)) Modifier
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing)) .size(ButtonDefaults.IconSize)
Icon( .weight(1f, false)
imageVector = icon, .clickable {
contentDescription = icon.name, if (enabled) {
modifier = onIconClick()
Modifier }
.size(ButtonDefaults.IconSize) },
.weight(1f, false) )
.clickable { }
if (enabled) {
onIconClick()
}
},
)
}
} }
@@ -15,78 +15,85 @@ 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.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics import com.zaneschepke.wireguardautotunnel.service.tunnel.statistics.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.util.NumberUtils import com.zaneschepke.wireguardautotunnel.util.NumberUtils
import com.zaneschepke.wireguardautotunnel.util.toThreeDecimalPlaceString import com.zaneschepke.wireguardautotunnel.util.extensions.toThreeDecimalPlaceString
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun RowListItem( fun RowListItem(
icon: @Composable () -> Unit, icon: @Composable () -> Unit,
text: String, text: String,
onHold: () -> Unit, onHold: () -> Unit,
onClick: () -> Unit, onClick: () -> Unit,
rowButton: @Composable () -> Unit, rowButton: @Composable () -> Unit,
expanded: Boolean, expanded: Boolean,
statistics: TunnelStatistics? statistics: TunnelStatistics?,
focusRequester: FocusRequester,
) { ) {
Box( Box(
modifier = modifier =
Modifier Modifier
.animateContentSize() .focusRequester(focusRequester)
.clip(RoundedCornerShape(30.dp)) .animateContentSize()
.combinedClickable( .clip(RoundedCornerShape(30.dp))
onClick = { onClick() }, .combinedClickable(
onLongClick = { onHold() }, onClick = { onClick() },
), onLongClick = { onHold() },
) { ),
Column { ) {
Row( Column {
modifier = Modifier Row(
.fillMaxWidth() modifier =
.padding(horizontal = 15.dp, vertical = 5.dp), Modifier
verticalAlignment = Alignment.CenterVertically, .fillMaxWidth()
horizontalArrangement = Arrangement.SpaceBetween, .padding(horizontal = 15.dp, vertical = 5.dp),
) { verticalAlignment = Alignment.CenterVertically,
Row( horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically, ) {
) { Row(
icon() verticalAlignment = Alignment.CenterVertically,
Text(text) modifier = Modifier.fillMaxWidth(13 / 20f),
} ) {
rowButton() icon()
} Text(text, maxLines = 1, overflow = TextOverflow.Ellipsis)
if (expanded) { }
statistics?.getPeers()?.forEach { rowButton()
Row( }
modifier = if (expanded) {
Modifier statistics?.getPeers()?.forEach {
.fillMaxWidth() Row(
.padding(end = 10.dp, bottom = 10.dp, start = 10.dp), modifier =
verticalAlignment = Alignment.CenterVertically, Modifier
horizontalArrangement = Arrangement.SpaceEvenly, .fillMaxWidth()
) { .padding(end = 10.dp, bottom = 10.dp, start = 10.dp),
//TODO change these to string resources verticalAlignment = Alignment.CenterVertically,
val handshakeEpoch = statistics.peerStats(it)!!.latestHandshakeEpochMillis horizontalArrangement = Arrangement.SpaceEvenly,
val peerTx = statistics.peerStats(it)!!.txBytes ) {
val peerRx = statistics.peerStats(it)!!.rxBytes // TODO change these to string resources
val peerId = it.toBase64().subSequence(0, 3).toString() + "***" val handshakeEpoch = statistics.peerStats(it)!!.latestHandshakeEpochMillis
val handshakeSec = val peerTx = statistics.peerStats(it)!!.txBytes
NumberUtils.getSecondsBetweenTimestampAndNow(handshakeEpoch) val peerRx = statistics.peerStats(it)!!.rxBytes
val handshake = val peerId = it.toBase64().subSequence(0, 3).toString() + "***"
if (handshakeSec == null) "never" else "$handshakeSec secs ago" val handshakeSec =
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString() NumberUtils.getSecondsBetweenTimestampAndNow(handshakeEpoch)
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString() val handshake =
val fontSize = 9.sp if (handshakeSec == null) "never" else "$handshakeSec secs ago"
Text("peer: $peerId", fontSize = fontSize) val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
Text("handshake: $handshake", fontSize = fontSize) val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
Text("tx: $peerTxMB MB", fontSize = fontSize) val fontSize = 9.sp
Text("rx: $peerRxMB MB", fontSize = fontSize) Text("peer: $peerId", fontSize = fontSize)
} Text("handshake: $handshake", fontSize = fontSize)
} Text("tx: $peerTxMB MB", fontSize = fontSize)
} Text("rx: $peerRxMB MB", fontSize = fontSize)
} }
} }
}
}
}
} }
@@ -26,55 +26,57 @@ import com.zaneschepke.wireguardautotunnel.R
@Composable @Composable
fun SearchBar(onQuery: (queryString: String) -> Unit) { fun SearchBar(onQuery: (queryString: String) -> Unit) {
// Immediately update and keep track of query from text field changes. // Immediately update and keep track of query from text field changes.
var query: String by rememberSaveable { mutableStateOf("") } var query: String by rememberSaveable { mutableStateOf("") }
var showClearIcon by rememberSaveable { mutableStateOf(false) } var showClearIcon by rememberSaveable { mutableStateOf(false) }
if (query.isEmpty()) { if (query.isEmpty()) {
showClearIcon = false showClearIcon = false
} else if (query.isNotEmpty()) { } else if (query.isNotEmpty()) {
showClearIcon = true showClearIcon = true
} }
TextField( TextField(
value = query, value = query,
onValueChange = { onQueryChanged -> onValueChange = { onQueryChanged ->
// If user makes changes to text, immediately updated it. // If user makes changes to text, immediately updated it.
query = onQueryChanged query = onQueryChanged
onQuery(onQueryChanged) onQuery(onQueryChanged)
}, },
leadingIcon = { leadingIcon = {
Icon( val icon = Icons.Rounded.Search
imageVector = Icons.Rounded.Search, Icon(
tint = MaterialTheme.colorScheme.onBackground, imageVector = icon,
contentDescription = stringResource(id = R.string.search_icon), tint = MaterialTheme.colorScheme.onBackground,
) contentDescription = icon.name,
}, )
trailingIcon = { },
if (showClearIcon) { trailingIcon = {
IconButton(onClick = { query = "" }) { if (showClearIcon) {
Icon( IconButton(onClick = { query = "" }) {
imageVector = Icons.Rounded.Clear, val icon = Icons.Rounded.Clear
tint = MaterialTheme.colorScheme.onBackground, Icon(
contentDescription = stringResource(id = R.string.clear_icon), imageVector = icon,
) tint = MaterialTheme.colorScheme.onBackground,
} contentDescription = icon.name,
} )
}, }
maxLines = 1, }
colors = },
TextFieldDefaults.colors( maxLines = 1,
focusedContainerColor = Color.Transparent, colors =
unfocusedContainerColor = Color.Transparent, TextFieldDefaults.colors(
disabledContainerColor = Color.Transparent, focusedContainerColor = Color.Transparent,
), unfocusedContainerColor = Color.Transparent,
placeholder = { Text(text = stringResource(R.string.hint_search_packages)) }, disabledContainerColor = Color.Transparent,
textStyle = MaterialTheme.typography.bodySmall, ),
singleLine = true, placeholder = { Text(text = stringResource(R.string.hint_search_packages)) },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text), textStyle = MaterialTheme.typography.bodySmall,
modifier = singleLine = true,
Modifier keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
.fillMaxWidth() modifier =
.background(color = MaterialTheme.colorScheme.background, shape = RectangleShape), Modifier
) .fillMaxWidth()
.background(color = MaterialTheme.colorScheme.background, shape = RectangleShape),
)
} }
@@ -11,26 +11,26 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
@Composable @Composable
fun ConfigurationTextBox( fun ConfigurationTextBox(
value: String, value: String,
hint: String, hint: String,
onValueChange: (String) -> Unit, onValueChange: (String) -> Unit,
keyboardActions: KeyboardActions, keyboardActions: KeyboardActions,
label: String, label: String,
modifier: Modifier modifier: Modifier,
) { ) {
OutlinedTextField( OutlinedTextField(
modifier = modifier, modifier = modifier,
value = value, value = value,
singleLine = true, singleLine = true,
onValueChange = { onValueChange(it) }, onValueChange = { onValueChange(it) },
label = { Text(label) }, label = { Text(label) },
maxLines = 1, maxLines = 1,
placeholder = { Text(hint) }, placeholder = { Text(hint) },
keyboardOptions = keyboardOptions =
KeyboardOptions( KeyboardOptions(
capitalization = KeyboardCapitalization.None, capitalization = KeyboardCapitalization.None,
imeAction = ImeAction.Done, imeAction = ImeAction.Done,
), ),
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
) )
} }
@@ -13,32 +13,31 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
@Composable @Composable
fun ConfigurationToggle( fun ConfigurationToggle(label: String, enabled: Boolean, checked: Boolean, padding: Dp, onCheckChanged: () -> Unit, modifier: Modifier = Modifier) {
label: String, Row(
enabled: Boolean, modifier =
checked: Boolean, Modifier
padding: Dp, .fillMaxWidth()
onCheckChanged: () -> Unit, .padding(padding),
modifier: Modifier = Modifier verticalAlignment = Alignment.CenterVertically,
) { horizontalArrangement = Arrangement.SpaceBetween,
Row( ) {
modifier = Modifier Text(
.fillMaxWidth() label,
.padding(padding), textAlign = TextAlign.Start,
verticalAlignment = Alignment.CenterVertically, modifier =
horizontalArrangement = Arrangement.SpaceBetween, Modifier
) { .weight(
Text(label, textAlign = TextAlign.Start, modifier = Modifier weight = 1.0f,
.weight( fill = false,
weight = 1.0f, ),
fill = false, softWrap = true,
), )
softWrap = true) Switch(
Switch( modifier = modifier,
modifier = modifier, enabled = enabled,
enabled = enabled, checked = checked,
checked = checked, onCheckedChange = { onCheckChanged() },
onCheckedChange = { onCheckChanged() }, )
) }
}
} }
@@ -0,0 +1,37 @@
package com.zaneschepke.wireguardautotunnel.ui.common.dialog
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.zaneschepke.wireguardautotunnel.R
@Composable
fun InfoDialog(
onAttest: () -> Unit,
onDismiss: () -> Unit,
title: @Composable () -> Unit,
body: @Composable () -> Unit,
confirmText: @Composable () -> Unit,
) {
AlertDialog(
onDismissRequest = { onDismiss() },
confirmButton = {
TextButton(
onClick = {
onAttest()
},
) {
confirmText()
}
},
dismissButton = {
TextButton(onClick = { onDismiss() }) {
Text(text = stringResource(R.string.cancel))
}
},
title = { title() },
text = { body() },
)
}
@@ -0,0 +1,53 @@
package com.zaneschepke.wireguardautotunnel.ui.common.functions
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import com.zaneschepke.wireguardautotunnel.util.Constants
@Composable
fun rememberFileImportLauncherForResult(onNoFileExplorer: () -> Unit, onData: (data: Uri) -> Unit): ManagedActivityResultLauncher<String, Uri?> {
return rememberLauncherForActivityResult(
object : ActivityResultContracts.GetContent() {
override fun createIntent(context: Context, input: String): Intent {
val intent = super.createIntent(context, input)
/* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than
* what we can do, so detect this and throw an exception that we can catch later. */
val activitiesToResolveIntent =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.queryIntentActivities(
intent,
PackageManager.ResolveInfoFlags.of(
PackageManager.MATCH_DEFAULT_ONLY.toLong(),
),
)
} else {
context.packageManager.queryIntentActivities(
intent,
PackageManager.MATCH_DEFAULT_ONLY,
)
}
if (
activitiesToResolveIntent.all {
val name = it.activityInfo.packageName
name.startsWith(Constants.GOOGLE_TV_EXPLORER_STUB) ||
name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB)
}
) {
onNoFileExplorer()
}
return intent
}
},
) { data ->
if (data == null) return@rememberLauncherForActivityResult
onData(data)
}
}
@@ -10,47 +10,55 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import com.zaneschepke.wireguardautotunnel.ui.Screen
@Composable @Composable
fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavItem>) { fun BottomNavBar(navController: NavController, bottomNavItems: List<BottomNavItem>) {
val backStackEntry = navController.currentBackStackEntryAsState() var showBottomBar by rememberSaveable { mutableStateOf(true) }
val navBackStackEntry by navController.currentBackStackEntryAsState()
var showBottomBar by rememberSaveable { mutableStateOf(true) } showBottomBar = bottomNavItems.firstOrNull { navBackStackEntry?.destination?.route?.contains(it.route) == true } != null
val navBackStackEntry by navController.currentBackStackEntryAsState()
//TODO find a better way to hide nav bar if (showBottomBar) {
showBottomBar = when (navBackStackEntry?.destination?.route) { NavigationBar(
Screen.Lock.route -> false containerColor = MaterialTheme.colorScheme.surface,
else -> true ) {
} bottomNavItems.forEach { item ->
val selected = navBackStackEntry?.destination?.route?.contains(item.route) == true
NavigationBar( NavigationBarItem(
containerColor = if (!showBottomBar) Color.Transparent else MaterialTheme.colorScheme.background, selected = selected,
) { onClick = {
if (showBottomBar) bottomNavItems.forEach { item -> if (navBackStackEntry?.destination?.route == item.route) return@NavigationBarItem
val selected = item.route == backStackEntry.value?.destination?.route navController.navigate(item.route) {
// Pop up to the start destination of the graph to
NavigationBarItem( // avoid building up a large stack of destinations
selected = selected, // on the back stack as users select items
onClick = { navController.navigate(item.route) }, popUpTo(navController.graph.findStartDestination().id) {
label = { saveState = true
Text( }
text = item.name, // Avoid multiple copies of the same destination when
fontWeight = FontWeight.SemiBold, // reselecting the same item
) launchSingleTop = true
}, }
icon = { },
Icon( label = {
imageVector = item.icon, Text(
contentDescription = "${item.name} Icon", text = item.name,
) fontWeight = FontWeight.SemiBold,
}, )
) },
} icon = {
} Icon(
imageVector = item.icon,
contentDescription = "${item.name} Icon",
)
},
)
}
}
}
} }
@@ -3,7 +3,7 @@ package com.zaneschepke.wireguardautotunnel.ui.common.navigation
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
data class BottomNavItem( data class BottomNavItem(
val name: String, val name: String,
val route: String, val route: String,
val icon: ImageVector, val icon: ImageVector,
) )
@@ -12,78 +12,77 @@ import androidx.fragment.app.FragmentActivity
@Composable @Composable
fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) { fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) {
val context = LocalContext.current val context = LocalContext.current
val biometricManager = BiometricManager.from(context) val biometricManager = BiometricManager.from(context)
val bio = biometricManager.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL) val bio = biometricManager.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
val isBiometricAvailable = remember { val isBiometricAvailable =
when (bio) { remember {
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> { when (bio) {
onError("Biometrics not available") BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
false onError("Biometrics not available")
} false
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> { BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
onError("Biometrics not created") onError("Biometrics not created")
false false
} }
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> { BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
onError("Biometric hardware not found") onError("Biometric hardware not found")
false false
} }
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> { BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
onError("Biometric security update required") onError("Biometric security update required")
false false
} }
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> { BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
onError("Biometrics not supported") onError("Biometrics not supported")
false false
} }
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> { BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
onError("Biometrics status unknown") onError("Biometrics status unknown")
false false
} }
BiometricManager.BIOMETRIC_SUCCESS -> true BiometricManager.BIOMETRIC_SUCCESS -> true
else -> false else -> false
} }
} }
if (isBiometricAvailable) { if (isBiometricAvailable) {
val executor = remember { ContextCompat.getMainExecutor(context) } val executor = remember { ContextCompat.getMainExecutor(context) }
val promptInfo = val promptInfo =
BiometricPrompt.PromptInfo.Builder() BiometricPrompt.PromptInfo.Builder()
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL) .setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
.setTitle("Biometric Authentication") .setTitle("Biometric Authentication")
.setSubtitle("Log in using your biometric credential") .setSubtitle("Log in using your biometric credential")
.build() .build()
val biometricPrompt = val biometricPrompt =
BiometricPrompt( BiometricPrompt(
context as FragmentActivity, context as FragmentActivity,
executor, executor,
object : BiometricPrompt.AuthenticationCallback() { object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
onFailure() onFailure()
} }
override fun onAuthenticationSucceeded( override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
result: BiometricPrompt.AuthenticationResult super.onAuthenticationSucceeded(result)
) { onSuccess()
super.onAuthenticationSucceeded(result) }
onSuccess()
}
override fun onAuthenticationFailed() { override fun onAuthenticationFailed() {
super.onAuthenticationFailed() super.onAuthenticationFailed()
onFailure() onFailure()
} }
}, },
) )
biometricPrompt.authenticate(promptInfo) biometricPrompt.authenticate(promptInfo)
} }
} }
@@ -19,46 +19,45 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
@Composable @Composable
fun CustomSnackBar( fun CustomSnackBar(message: String, isRtl: Boolean = true, containerColor: Color = MaterialTheme.colorScheme.surface) {
message: String, val context = LocalContext.current
isRtl: Boolean = true, Snackbar(
containerColor: Color = MaterialTheme.colorScheme.surface containerColor = containerColor,
) { modifier =
Snackbar( Modifier
containerColor = containerColor, .fillMaxWidth(
modifier = if (context.isRunningOnTv()) 1 / 3f else 2 / 3f,
Modifier )
.fillMaxWidth( .padding(bottom = 100.dp),
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) 1 / 3f else 2 / 3f, shape = RoundedCornerShape(16.dp),
) ) {
.padding(bottom = 100.dp), CompositionLocalProvider(
shape = RoundedCornerShape(16.dp), LocalLayoutDirection provides if (isRtl) LayoutDirection.Rtl else LayoutDirection.Ltr,
) { ) {
CompositionLocalProvider( Row(
LocalLayoutDirection provides if (isRtl) LayoutDirection.Rtl else LayoutDirection.Ltr, modifier =
) { Modifier
Row( .width(IntrinsicSize.Max)
modifier = Modifier .height(IntrinsicSize.Min),
.width(IntrinsicSize.Max) verticalAlignment = Alignment.CenterVertically,
.height(IntrinsicSize.Min), horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically, ) {
horizontalArrangement = Arrangement.Start, val icon = Icons.Rounded.Info
) { Icon(
val icon = Icons.Rounded.Info icon,
Icon( contentDescription = icon.name,
icon, tint = MaterialTheme.colorScheme.onSurface,
contentDescription = icon.name, modifier = Modifier.padding(end = 10.dp),
tint = Color.White, )
modifier = Modifier.padding(end = 10.dp), Text(message, color = MaterialTheme.colorScheme.onSurface, modifier = Modifier.padding(end = 5.dp))
) }
Text(message, color = Color.White, modifier = Modifier.padding(end = 5.dp)) }
} }
}
}
} }
@@ -13,14 +13,15 @@ import androidx.compose.ui.unit.dp
@Composable @Composable
fun LoadingScreen() { fun LoadingScreen() {
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Top, verticalArrangement = Arrangement.Top,
modifier = Modifier modifier =
.fillMaxSize() Modifier
.focusable() .fillMaxSize()
.padding(), .focusable()
) { .padding(),
Column(modifier = Modifier.padding(120.dp)) { CircularProgressIndicator() } ) {
} Column(modifier = Modifier.padding(120.dp)) { CircularProgressIndicator() }
}
} }
@@ -13,13 +13,14 @@ import androidx.compose.ui.unit.dp
@Composable @Composable
fun LogTypeLabel(color: Color, content: @Composable () -> Unit) { fun LogTypeLabel(color: Color, content: @Composable () -> Unit) {
Box( Box(
modifier = Modifier modifier =
.size(20.dp) Modifier
.clip(RoundedCornerShape(2.dp)) .size(20.dp)
.background(color), .clip(RoundedCornerShape(2.dp))
contentAlignment = Alignment.Center, .background(color),
) { contentAlignment = Alignment.Center,
content() ) {
} content()
}
} }
@@ -13,10 +13,10 @@ import androidx.compose.ui.unit.sp
@Composable @Composable
fun SectionTitle(title: String, padding: Dp) { fun SectionTitle(title: String, padding: Dp) {
Text( Text(
title, title,
textAlign = TextAlign.Start, textAlign = TextAlign.Start,
style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.ExtraBold), style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.ExtraBold),
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp), modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp),
) )
} }

Some files were not shown because too many files have changed in this diff Show More