mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aeb4a13389 | |||
| f0ec661223 | |||
| ffa7a207fb | |||
| 515e91d191 | |||
| 16e65aec9f | |||
| ff87bee3b4 | |||
| 9739d35eda | |||
| 11ad494fbb | |||
| 90b006acc5 | |||
| eb7b39c379 | |||
| 0a17593310 | |||
| c0e58125dd | |||
| 3791261f91 | |||
| d1e61be3ae | |||
| afd4fb127f | |||
| e0cce8fba4 | |||
| b70ecbdfff | |||
| 513d08998b | |||
| 79583e0e61 | |||
| 75790ec6d5 | |||
| a1941b7229 | |||
| 37bae82700 | |||
| 77cd328a71 | |||
| 5a1430706b | |||
| 321730536d | |||
| 2912238f27 | |||
| bc7daacd13 | |||
| c8f2cfd758 | |||
| ca3f3fd439 | |||
| 235170508b |
@@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: Create a report to help us improve
|
||||||
|
title: "[BUG] - Problem with app"
|
||||||
|
labels: bug
|
||||||
|
assignees: zaneschepke
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
A clear and concise description of what the bug is.
|
||||||
|
|
||||||
|
**Smartphone (please complete the following information):**
|
||||||
|
- Device: [e.g. iPhone6]
|
||||||
|
- Android Version: [e.g. iOS8.1]
|
||||||
|
- App Version [e.g. 22]
|
||||||
|
|
||||||
|
**To Reproduce**
|
||||||
|
Steps to reproduce the behavior:
|
||||||
|
1. Go to '...'
|
||||||
|
2. Click on '....'
|
||||||
|
3. Scroll down to '....'
|
||||||
|
4. See error
|
||||||
|
|
||||||
|
**Expected behavior**
|
||||||
|
A clear and concise description of what you expected to happen.
|
||||||
|
|
||||||
|
**Screenshots (Only if necessary)**
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context about the problem here.
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
name: Feature request
|
||||||
|
about: Suggest an idea for this project
|
||||||
|
title: "[FEATURE] - New feature request"
|
||||||
|
labels: enhancement
|
||||||
|
assignees: zaneschepke
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Is your feature request related to a problem? Please describe.**
|
||||||
|
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
|
||||||
|
**Describe the solution you'd like**
|
||||||
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
|
**Describe alternatives you've considered**
|
||||||
|
A clear and concise description of any alternative solutions or features you've considered.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context or screenshots about the feature request here.
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# Support
|
||||||
|
|
||||||
|
If you are experiencing issues with the app, the following resources are available to help you.
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>
|
||||||
|
See the app docs site <a href="https://zaneschepke.com/wgtunnel-docs/overview.html">here</a> (work in progress).
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Chat with me and our community on Discord <a href="https://discord.gg/rbRRNh6H7V">here</a>, or open an issue on GitHub <a href="https://github.com/zaneschepke/wgtunnel/issues/new/choose">here</a>.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Send me an email <a href="mailto:zanecschepke@gmail.com">here</a>.
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
Thank you for using WG Tunnel.
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
# name of the workflow
|
||||||
|
name: Android CI Tag Deployment
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*.*.*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build Signed APK
|
||||||
|
# change to macos because of hilt issues on ubuntu in gradle 8.3
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
env:
|
||||||
|
KEY_STORE_PATH: ${{ secrets.KEY_STORE_PATH }}
|
||||||
|
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||||
|
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||||
|
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Set up JDK 17
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
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: 'android_keystore.jks'
|
||||||
|
fileDir: ${{ github.workspace }}/app/keystore/
|
||||||
|
encodedString: ${{ secrets.KEYSTORE }}
|
||||||
|
|
||||||
|
- 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
|
||||||
|
|
||||||
|
# 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@v3.1.2
|
||||||
|
with:
|
||||||
|
name: wgtunnel
|
||||||
|
path: ${{ steps.apk-path.outputs.path }}
|
||||||
|
- name: Download APK from build
|
||||||
|
uses: actions/download-artifact@v1
|
||||||
|
with:
|
||||||
|
name: wgtunnel
|
||||||
|
- name: Create Release with Fastlane changelog notes
|
||||||
|
id: create_release
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
# fix hardcode changelog file name
|
||||||
|
body_path: ${{ github.workspace }}/fastlane/metadata/android/en-US/changelogs/33000.txt
|
||||||
|
tag_name: ${{ github.ref_name }}
|
||||||
|
name: Release ${{ github.ref_name }}
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
|
files: ${{ github.workspace }}/${{ steps.apk-path.outputs.path }}
|
||||||
|
- 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)
|
||||||
|
|
||||||
@@ -69,3 +69,5 @@ lint/tmp/
|
|||||||
# App Specific cases
|
# App Specific cases
|
||||||
app/release/output.json
|
app/release/output.json
|
||||||
.idea/codeStyles/
|
.idea/codeStyles/
|
||||||
|
# where we keep our signing secrets locally
|
||||||
|
app/signing.properties
|
||||||
|
|||||||
@@ -2,61 +2,64 @@
|
|||||||
WG Tunnel
|
WG Tunnel
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<span align="center">
|
<div align="center">
|
||||||
|
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
[](https://discord.gg/rbRRNh6H7V)
|
[](https://discord.gg/rbRRNh6H7V)
|
||||||
|
|
||||||
</span>
|
</div>
|
||||||
|
|
||||||
<span align="center">
|
<div align="center">
|
||||||
|
|
||||||
|
|
||||||
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
[](https://play.google.com/store/apps/details?id=com.zaneschepke.wireguardautotunnel)
|
||||||
[](https://www.amazon.com/gp/product/B0CFGGL7WK)
|
[](https://www.amazon.com/gp/product/B0CFGGL7WK)
|
||||||
|
[](https://f-droid.org/packages/com.zaneschepke.wireguardautotunnel/)
|
||||||
|
|
||||||
</span>
|
|
||||||
|
|
||||||
<span align="center">
|
</div>
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
[](https://ko-fi.com/N4N8NMJN2)
|
[](https://ko-fi.com/N4N8NMJN2)
|
||||||
|
|
||||||
</span>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<span align="left">
|
<div align="left">
|
||||||
|
|
||||||
This is an alternative Android Application for [WireGuard](https://www.wireguard.com/) with added features. Built using the [wireguard-android](https://github.com/WireGuard/wireguard-android) library and [Jetpack Compose](https://developer.android.com/jetpack/compose), this application was inspired by the official [WireGuard Android](https://github.com/WireGuard/wireguard-android) app.
|
This is an alternative Android Application for [WireGuard](https://www.wireguard.com/) with added features. Built using the [wireguard-android](https://github.com/WireGuard/wireguard-android) library and [Jetpack Compose](https://developer.android.com/jetpack/compose), this application was inspired by the official [WireGuard Android](https://github.com/WireGuard/wireguard-android) app.
|
||||||
|
|
||||||
</span>
|
</div>
|
||||||
|
|
||||||
<span align="center">
|
<div align="center">
|
||||||
|
|
||||||
## Screenshots
|
## Screenshots
|
||||||
|
|
||||||
<p float="center">
|
<p float="center">
|
||||||
<img label="Main" style="padding-right:25px" src="asset/main_screen.png" width="200" />
|
<img label="Main" style="padding-right:25px" src="asset/main_screen.png" width="200" />
|
||||||
<img label="Config" style="padding-left:25px" src="./asset/config_screen.png" width="200" />
|
<img label="Config" style="padding-left:25px" src="asset/config_screen.png" width="200" />
|
||||||
<img label="Settings" style="padding-left:25px" src="asset/settings_screen.png" width="200" />
|
<img label="Settings" style="padding-left:25px" src="asset/settings_screen.png" width="200" />
|
||||||
<img label="Support" style="padding-left:25px" src="asset/support_screen.png" width="200" />
|
<img label="Support" style="padding-left:25px" src="asset/support_screen.png" width="200" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<span align="left">
|
<div align="left">
|
||||||
|
|
||||||
## Inspiration
|
## Inspiration
|
||||||
|
|
||||||
The inspiration for this app came from the inconvenience of constantly having to turn VPN off and on while on different networks. With there being no free solution to this problem, this app was created to meet that need.
|
The original inspiration for this app came from the inconvenience of having to manually turn VPN off and on while on different networks. This app was created to offer a free solution to this problem.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* Add tunnels via .conf file
|
* Add tunnels via .conf file, zip, manual entry, or QR code
|
||||||
* Auto connect to VPN based on Wi-Fi SSID
|
* Auto connect to VPN based on Wi-Fi SSID
|
||||||
* Split tunneling by application with search
|
* Split tunneling by application with search
|
||||||
* Always-on VPN for Android support
|
* Always-on VPN for Android support
|
||||||
* Quick tile support for vpn toggling
|
* Export tunnels to zip
|
||||||
* Dynamic shortcuts support for automation integration
|
* Quick tile support for VPN toggling
|
||||||
* Configurable Trusted Network list
|
* Static shortcuts support for primary tunnel for automation integration
|
||||||
* Optional auto connect on mobile data
|
* Intent automation support for all tunnels
|
||||||
|
* Optional auto connect on mobile data, ethernet
|
||||||
* Automatic service restart after reboot
|
* Automatic service restart after reboot
|
||||||
* Service will stay running in background after app has been closed
|
* Service will stay running in background after app has been closed
|
||||||
|
|
||||||
@@ -66,7 +69,7 @@ The inspiration for this app came from the inconvenience of constantly having to
|
|||||||
```
|
```
|
||||||
$ git clone https://github.com/zaneschepke/wgtunnel
|
$ git clone https://github.com/zaneschepke/wgtunnel
|
||||||
$ cd wgtunnel
|
$ cd wgtunnel
|
||||||
$ ./gradlew assembleRelease
|
$ ./gradlew assembleDebug
|
||||||
```
|
```
|
||||||
|
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
+100
-27
@@ -1,3 +1,5 @@
|
|||||||
|
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)
|
||||||
@@ -7,17 +9,24 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "com.zaneschepke.wireguardautotunnel"
|
namespace = Constants.APP_ID
|
||||||
compileSdk = 34
|
compileSdk = Constants.TARGET_SDK
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "com.zaneschepke.wireguardautotunnel"
|
applicationId = Constants.APP_ID
|
||||||
minSdk = 26
|
minSdk = Constants.MIN_SDK
|
||||||
targetSdk = 34
|
compileSdk = Constants.COMPILE_SDK
|
||||||
versionCode = 30002
|
targetSdk = Constants.TARGET_SDK
|
||||||
versionName = "3.0.2"
|
versionCode = Constants.VERSION_CODE
|
||||||
|
versionName = Constants.VERSION_NAME
|
||||||
|
|
||||||
multiDexEnabled = true
|
ksp {
|
||||||
|
arg("room.schemaLocation", "$projectDir/schemas")
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
|
||||||
|
}
|
||||||
|
|
||||||
resourceConfigurations.addAll(listOf("en"))
|
resourceConfigurations.addAll(listOf("en"))
|
||||||
|
|
||||||
@@ -27,7 +36,56 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signingConfigs {
|
||||||
|
create(Constants.RELEASE) {
|
||||||
|
val properties =
|
||||||
|
Properties().apply {
|
||||||
|
// created local file for signing details
|
||||||
|
try {
|
||||||
|
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 build
|
||||||
|
storeFile = file(
|
||||||
|
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 {
|
buildTypes {
|
||||||
|
// don't strip
|
||||||
|
packaging.jniLibs.keepDebugSymbols.addAll(
|
||||||
|
listOf("libwg-go.so", "libwg-quick.so", "libwg.so")
|
||||||
|
)
|
||||||
|
|
||||||
|
applicationVariants.all {
|
||||||
|
val variant = this
|
||||||
|
variant.outputs
|
||||||
|
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
|
||||||
|
.forEach { output ->
|
||||||
|
val outputFileName =
|
||||||
|
"${Constants.APP_NAME}-${variant.flavorName}-${variant.buildType.name}-${variant.versionName}.apk"
|
||||||
|
output.outputFileName = outputFileName
|
||||||
|
}
|
||||||
|
}
|
||||||
release {
|
release {
|
||||||
isDebuggable = false
|
isDebuggable = false
|
||||||
isMinifyEnabled = true
|
isMinifyEnabled = true
|
||||||
@@ -36,20 +94,21 @@ android {
|
|||||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||||
"proguard-rules.pro"
|
"proguard-rules.pro"
|
||||||
)
|
)
|
||||||
|
signingConfig = signingConfigs.getByName(Constants.RELEASE)
|
||||||
}
|
}
|
||||||
debug {
|
debug {
|
||||||
isDebuggable = true
|
isDebuggable = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
flavorDimensions.add("type")
|
flavorDimensions.add(Constants.TYPE)
|
||||||
productFlavors {
|
productFlavors {
|
||||||
create("fdroid") {
|
create("fdroid") {
|
||||||
dimension = "type"
|
dimension = Constants.TYPE
|
||||||
|
proguardFile("fdroid-rules.pro")
|
||||||
}
|
}
|
||||||
create("general") {
|
create("general") {
|
||||||
dimension = "type"
|
dimension = Constants.TYPE
|
||||||
if (BuildHelper.isReleaseBuild(gradle) && BuildHelper.isGeneralFlavor(gradle))
|
if (BuildHelper.isReleaseBuild(gradle) && BuildHelper.isGeneralFlavor(gradle)) {
|
||||||
{
|
|
||||||
apply(plugin = "com.google.gms.google-services")
|
apply(plugin = "com.google.gms.google-services")
|
||||||
apply(plugin = "com.google.firebase.crashlytics")
|
apply(plugin = "com.google.firebase.crashlytics")
|
||||||
}
|
}
|
||||||
@@ -58,14 +117,14 @@ android {
|
|||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
isCoreLibraryDesugaringEnabled = true
|
||||||
}
|
}
|
||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = "17"
|
jvmTarget = Constants.JVM_TARGET
|
||||||
}
|
}
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
compose = true
|
compose = true
|
||||||
buildConfig = true
|
buildConfig = true
|
||||||
|
|
||||||
}
|
}
|
||||||
composeOptions {
|
composeOptions {
|
||||||
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
|
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
|
||||||
@@ -81,6 +140,8 @@ val generalImplementation by configurations
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.lifecycle.runtime.ktx)
|
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||||
|
// optional - helpers for implementing LifecycleOwner in a 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)
|
||||||
@@ -89,19 +150,22 @@ dependencies {
|
|||||||
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)
|
||||||
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)
|
||||||
debugImplementation(libs.androidx.compose.ui.tooling)
|
debugImplementation(libs.androidx.compose.ui.tooling)
|
||||||
debugImplementation(libs.androidx.compose.manifest)
|
debugImplementation(libs.androidx.compose.manifest)
|
||||||
|
|
||||||
//wg
|
// wg
|
||||||
implementation(libs.tunnel)
|
implementation(libs.tunnel)
|
||||||
|
coreLibraryDesugaring(libs.desugar.jdk.libs)
|
||||||
|
|
||||||
//logging
|
// logging
|
||||||
implementation(libs.timber)
|
implementation(libs.timber)
|
||||||
|
|
||||||
// compose navigation
|
// compose navigation
|
||||||
@@ -112,32 +176,41 @@ dependencies {
|
|||||||
implementation(libs.hilt.android)
|
implementation(libs.hilt.android)
|
||||||
ksp(libs.hilt.android.compiler)
|
ksp(libs.hilt.android.compiler)
|
||||||
|
|
||||||
//accompanist
|
// accompanist
|
||||||
implementation(libs.accompanist.systemuicontroller)
|
implementation(libs.accompanist.systemuicontroller)
|
||||||
implementation(libs.accompanist.permissions)
|
implementation(libs.accompanist.permissions)
|
||||||
implementation(libs.accompanist.flowlayout)
|
implementation(libs.accompanist.flowlayout)
|
||||||
implementation(libs.accompanist.navigation.animation)
|
|
||||||
implementation(libs.accompanist.drawablepainter)
|
implementation(libs.accompanist.drawablepainter)
|
||||||
|
|
||||||
//room
|
// storage
|
||||||
implementation(libs.androidx.room.runtime)
|
implementation(libs.androidx.room.runtime)
|
||||||
ksp(libs.androidx.room.compiler)
|
ksp(libs.androidx.room.compiler)
|
||||||
implementation(libs.androidx.room.ktx)
|
implementation(libs.androidx.room.ktx)
|
||||||
|
implementation(libs.androidx.datastore.preferences)
|
||||||
|
|
||||||
//lifecycle
|
// lifecycle
|
||||||
implementation(libs.lifecycle.runtime.compose)
|
implementation(libs.lifecycle.runtime.compose)
|
||||||
|
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||||
|
implementation(libs.androidx.lifecycle.process)
|
||||||
|
|
||||||
//icons
|
// icons
|
||||||
implementation(libs.material.icons.extended)
|
implementation(libs.material.icons.extended)
|
||||||
//serialization
|
// serialization
|
||||||
implementation(libs.kotlinx.serialization.json)
|
implementation(libs.kotlinx.serialization.json)
|
||||||
|
|
||||||
//firebase crashlytics
|
// firebase crashlytics
|
||||||
generalImplementation(platform(libs.firebase.bom))
|
generalImplementation(platform(libs.firebase.bom))
|
||||||
generalImplementation(libs.google.firebase.crashlytics.ktx)
|
generalImplementation(libs.google.firebase.crashlytics.ktx)
|
||||||
generalImplementation(libs.google.firebase.analytics.ktx)
|
generalImplementation(libs.google.firebase.analytics.ktx)
|
||||||
|
|
||||||
//barcode scanning
|
// barcode scanning
|
||||||
implementation(libs.zxing.android.embedded)
|
implementation(libs.zxing.android.embedded)
|
||||||
implementation(libs.zxing.core)
|
implementation(libs.zxing.core)
|
||||||
}
|
|
||||||
|
// bio
|
||||||
|
implementation(libs.androidx.biometric.ktx)
|
||||||
|
|
||||||
|
// shortcuts
|
||||||
|
implementation(libs.androidx.core)
|
||||||
|
implementation(libs.androidx.core.google.shortcuts)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
-dontwarn com.google.errorprone.annotations.**
|
||||||
|
|
||||||
|
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
|
||||||
|
<fields>;
|
||||||
|
}
|
||||||
Vendored
+6
-1
@@ -18,4 +18,9 @@
|
|||||||
|
|
||||||
# If you keep the line number information, uncomment this to
|
# If you keep the line number information, uncomment this to
|
||||||
# hide the original source file name.
|
# hide the original source file name.
|
||||||
#-renamesourcefileattribute SourceFile
|
#-renamesourcefileattribute SourceFile
|
||||||
|
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
|
||||||
|
<fields>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 1,
|
||||||
|
"identityHash": "ba86153e6fb0b823197b987239b03e64",
|
||||||
|
"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, `default_tunnel` TEXT, `is_always_on_vpn_enabled` INTEGER NOT NULL, `is_tunnel_on_ethernet_enabled` INTEGER NOT NULL)",
|
||||||
|
"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": "defaultTunnel",
|
||||||
|
"columnName": "default_tunnel",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isAlwaysOnVpnEnabled",
|
||||||
|
"columnName": "is_always_on_vpn_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isTunnelOnEthernetEnabled",
|
||||||
|
"columnName": "is_tunnel_on_ethernet_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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)",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, 'ba86153e6fb0b823197b987239b03e64')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 2,
|
||||||
|
"identityHash": "65b1c9efff61712231fa64d1f19f3915",
|
||||||
|
"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, `default_tunnel` TEXT, `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_battery_saver_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": "defaultTunnel",
|
||||||
|
"columnName": "default_tunnel",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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": "isBatterySaverEnabled",
|
||||||
|
"columnName": "is_battery_saver_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)",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, '65b1c9efff61712231fa64d1f19f3915')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 3,
|
||||||
|
"identityHash": "6b30daba29bb95f8ddc4d26206329d4f",
|
||||||
|
"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, `default_tunnel` TEXT, `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_battery_saver_enabled` INTEGER NOT NULL DEFAULT false, `is_tunnel_on_wifi_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": "defaultTunnel",
|
||||||
|
"columnName": "default_tunnel",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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": "isBatterySaverEnabled",
|
||||||
|
"columnName": "is_battery_saver_enabled",
|
||||||
|
"affinity": "INTEGER",
|
||||||
|
"notNull": true,
|
||||||
|
"defaultValue": "false"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldPath": "isTunnelOnWifiEnabled",
|
||||||
|
"columnName": "is_tunnel_on_wifi_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)",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, '6b30daba29bb95f8ddc4d26206329d4f')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 4,
|
||||||
|
"identityHash": "aee55639422df8dadfe74c3bad204477",
|
||||||
|
"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, `default_tunnel` TEXT, `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_battery_saver_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)",
|
||||||
|
"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": "defaultTunnel",
|
||||||
|
"columnName": "default_tunnel",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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": "isBatterySaverEnabled",
|
||||||
|
"columnName": "is_battery_saver_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"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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)",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, 'aee55639422df8dadfe74c3bad204477')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
{
|
||||||
|
"formatVersion": 1,
|
||||||
|
"database": {
|
||||||
|
"version": 5,
|
||||||
|
"identityHash": "bc15003a44746e18b9c260ec49737089",
|
||||||
|
"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, `default_tunnel` TEXT, `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_battery_saver_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)",
|
||||||
|
"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": "defaultTunnel",
|
||||||
|
"columnName": "default_tunnel",
|
||||||
|
"affinity": "TEXT",
|
||||||
|
"notNull": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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": "isBatterySaverEnabled",
|
||||||
|
"columnName": "is_battery_saver_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"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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)",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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, 'bc15003a44746e18b9c260ec49737089')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
SIGNING_STORE_PASSWORD=
|
||||||
|
SIGNING_KEY_ALIAS=
|
||||||
|
SIGNING_KEY_PASSWORD=
|
||||||
|
KEY_STORE_PATH=/
|
||||||
+3
-5
@@ -1,13 +1,11 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
import org.junit.Assert.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instrumented test, which will execute on an Android device.
|
* Instrumented test, which will execute on an Android device.
|
||||||
*
|
*
|
||||||
@@ -21,4 +19,4 @@ class ExampleInstrumentedTest {
|
|||||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||||
assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName)
|
assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
|
import androidx.room.testing.MigrationTestHelper
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class MigrationTest {
|
||||||
|
private val dbName = "migration-test"
|
||||||
|
|
||||||
|
@get:Rule
|
||||||
|
val helper: MigrationTestHelper = MigrationTestHelper(
|
||||||
|
InstrumentationRegistry.getInstrumentation(),
|
||||||
|
AppDatabase::class.java
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun migrate4To5() {
|
||||||
|
helper.createDatabase(dbName, 4).apply {
|
||||||
|
// Database has schema version 1. Insert some data using SQL queries.
|
||||||
|
// You can't use DAO classes because they expect the latest schema.
|
||||||
|
execSQL(
|
||||||
|
"INSERT INTO Settings (is_tunnel_enabled," +
|
||||||
|
"is_tunnel_on_mobile_data_enabled," +
|
||||||
|
"trusted_network_ssids," +
|
||||||
|
"default_tunnel," +
|
||||||
|
"is_always_on_vpn_enabled," +
|
||||||
|
"is_tunnel_on_ethernet_enabled," +
|
||||||
|
"is_shortcuts_enabled," +
|
||||||
|
"is_battery_saver_enabled," +
|
||||||
|
"is_tunnel_on_wifi_enabled," +
|
||||||
|
"is_kernel_enabled," +
|
||||||
|
"is_restore_on_boot_enabled," +
|
||||||
|
"is_multi_tunnel_enabled)" +
|
||||||
|
" VALUES " +
|
||||||
|
"('false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'[trustedSSID1,trustedSSID2]'," +
|
||||||
|
"'defaultTunnel'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false'," +
|
||||||
|
"'false')"
|
||||||
|
)
|
||||||
|
execSQL(
|
||||||
|
"INSERT INTO TunnelConfig (name, wg_quick)" +
|
||||||
|
" VALUES ('hello', 'hello')"
|
||||||
|
)
|
||||||
|
// Prepare for the next version.
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-open the database with version 2 and provide
|
||||||
|
// MIGRATION_1_2 as the migration process.
|
||||||
|
helper.runMigrationsAndValidate(dbName, 5, true)
|
||||||
|
// MigrationTestHelper automatically verifies the schema changes,
|
||||||
|
// but you need to validate that the data was migrated properly.
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,10 @@
|
|||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="32" />
|
android:maxSdkVersion="32" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="32"
|
||||||
|
tools:ignore="ScopedStorage" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
|
||||||
android:maxSdkVersion="30"
|
android:maxSdkVersion="30"
|
||||||
tools:ignore="LeanbackUsesWifi" />
|
tools:ignore="LeanbackUsesWifi" />
|
||||||
@@ -13,8 +17,11 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||||
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
|
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
|
||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
|
<!--foreground service exempt android 14-->
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING"/>
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED"/>
|
||||||
|
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
|
||||||
|
|
||||||
<!--foreground service permissions-->
|
<!--foreground service permissions-->
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
@@ -38,6 +45,7 @@
|
|||||||
</queries>
|
</queries>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
android:enableOnBackInvokedCallback="true"
|
||||||
android:name=".WireGuardAutoTunnel"
|
android:name=".WireGuardAutoTunnel"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
android:fullBackupContent="@xml/backup_rules"
|
android:fullBackupContent="@xml/backup_rules"
|
||||||
@@ -47,7 +55,7 @@
|
|||||||
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.WireguardAutoTunnel"
|
||||||
tools:targetApi="31">
|
tools:targetApi="tiramisu">
|
||||||
<activity
|
<activity
|
||||||
android:name=".ui.MainActivity"
|
android:name=".ui.MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
@@ -58,6 +66,8 @@
|
|||||||
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
|
||||||
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
|
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
<meta-data android:name="android.app.shortcuts"
|
||||||
|
android:resource="@xml/shortcuts" />
|
||||||
</activity>
|
</activity>
|
||||||
<activity
|
<activity
|
||||||
android:name=".ui.CaptureActivityPortrait"
|
android:name=".ui.CaptureActivityPortrait"
|
||||||
@@ -67,12 +77,15 @@
|
|||||||
android:windowSoftInputMode="stateAlwaysHidden" />
|
android:windowSoftInputMode="stateAlwaysHidden" />
|
||||||
<activity
|
<activity
|
||||||
android:finishOnTaskLaunch="true"
|
android:finishOnTaskLaunch="true"
|
||||||
|
android:enabled="true"
|
||||||
|
android:exported="true"
|
||||||
android:theme="@android:style/Theme.NoDisplay"
|
android:theme="@android:style/Theme.NoDisplay"
|
||||||
android:name=".service.shortcut.ShortcutsActivity"/>
|
android:name=".service.shortcut.ShortcutsActivity"/>
|
||||||
<service
|
<service
|
||||||
android:name=".service.foreground.ForegroundService"
|
android:name=".service.foreground.ForegroundService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:foregroundServiceType="remoteMessaging"
|
android:foregroundServiceType="systemExempted|specialUse"
|
||||||
|
tools:node="merge"
|
||||||
android:exported="false">
|
android:exported="false">
|
||||||
</service>
|
</service>
|
||||||
<service
|
<service
|
||||||
@@ -94,27 +107,32 @@
|
|||||||
android:permission="android.permission.BIND_VPN_SERVICE"
|
android:permission="android.permission.BIND_VPN_SERVICE"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:persistent="true"
|
android:persistent="true"
|
||||||
android:foregroundServiceType="remoteMessaging"
|
android:foregroundServiceType="systemExempted|specialUse"
|
||||||
|
tools:node="merge"
|
||||||
android:exported="false">
|
android:exported="false">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.net.VpnService"/>
|
<action android:name="android.net.VpnService"/>
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
<meta-data android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
|
<meta-data android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
|
||||||
android:value="true"/>
|
android:value="true" />
|
||||||
</service>
|
</service>
|
||||||
<service
|
<service
|
||||||
android:name=".service.foreground.WireGuardConnectivityWatcherService"
|
android:name=".service.foreground.WireGuardConnectivityWatcherService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:stopWithTask="false"
|
android:stopWithTask="false"
|
||||||
android:persistent="true"
|
android:persistent="true"
|
||||||
android:foregroundServiceType="location"
|
android:foregroundServiceType="systemExempted|specialUse"
|
||||||
android:permission=""
|
tools:node="merge"
|
||||||
android:exported="false">
|
android:exported="false">
|
||||||
</service>
|
</service>
|
||||||
<receiver android:enabled="true" android:name=".receiver.BootReceiver"
|
<receiver android:enabled="true" android:name=".receiver.BootReceiver"
|
||||||
android:exported="true">
|
android:exported="false">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||||
|
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
|
||||||
|
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
|
||||||
|
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
<receiver android:exported="false" android:name=".receiver.NotificationActionReceiver"/>
|
<receiver android:exported="false" android:name=".receiver.NotificationActionReceiver"/>
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
|
||||||
|
|
||||||
object Constants {
|
|
||||||
const val VPN_CONNECTIVITY_CHECK_INTERVAL = 3000L
|
|
||||||
const val VPN_STATISTIC_CHECK_INTERVAL = 10000L
|
|
||||||
const val SNACKBAR_DELAY = 3000L
|
|
||||||
const val TOGGLE_TUNNEL_DELAY = 500L
|
|
||||||
const val FADE_IN_ANIMATION_DURATION = 1000
|
|
||||||
const val SLIDE_IN_ANIMATION_DURATION = 500
|
|
||||||
const val SLIDE_IN_TRANSITION_OFFSET = 1000
|
|
||||||
const val VALID_FILE_EXTENSION = ".conf"
|
|
||||||
const val URI_CONTENT_SCHEME = "content"
|
|
||||||
const val URI_PACKAGE_SCHEME = "package"
|
|
||||||
const val ALLOWED_FILE_TYPES = "*/*"
|
|
||||||
}
|
|
||||||
@@ -1,24 +1,64 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel
|
package com.zaneschepke.wireguardautotunnel
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.ComponentName
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
|
import android.service.quicksettings.TileService
|
||||||
|
import androidx.lifecycle.ProcessLifecycleOwner
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tile.TunnelControlTile
|
||||||
import dagger.hilt.android.HiltAndroidApp
|
import dagger.hilt.android.HiltAndroidApp
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.io.IOException
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltAndroidApp
|
@HiltAndroidApp
|
||||||
class WireGuardAutoTunnel : Application() {
|
class WireGuardAutoTunnel : Application() {
|
||||||
|
@Inject
|
||||||
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var dataStoreManager: DataStoreManager
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
if(BuildConfig.DEBUG) {
|
instance = this
|
||||||
Timber.plant(Timber.DebugTree())
|
if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree())
|
||||||
|
initSettings()
|
||||||
|
with(ProcessLifecycleOwner.get()) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
try {
|
||||||
|
// load preferences into memory
|
||||||
|
dataStoreManager.init()
|
||||||
|
requestTileServiceStateUpdate()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Timber.e("Failed to load preferences")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initSettings() {
|
||||||
|
with(ProcessLifecycleOwner.get()) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (settingsRepository.getAll().isEmpty()) {
|
||||||
|
settingsRepository.save(Settings())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun isRunningOnAndroidTv(context : Context) : Boolean {
|
lateinit var instance: WireGuardAutoTunnel private set
|
||||||
return context.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
|
fun isRunningOnAndroidTv(): Boolean {
|
||||||
|
return instance.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
|
||||||
|
}
|
||||||
|
fun requestTileServiceStateUpdate() {
|
||||||
|
TileService.requestListeningState(instance, ComponentName(instance, TunnelControlTile::class.java))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data
|
||||||
|
|
||||||
|
import androidx.room.AutoMigration
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import androidx.room.TypeConverters
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
|
||||||
|
@Database(
|
||||||
|
entities = [Settings::class, TunnelConfig::class],
|
||||||
|
version = 5,
|
||||||
|
autoMigrations = [
|
||||||
|
AutoMigration(from = 1, to = 2), AutoMigration(from = 2, to = 3), AutoMigration(
|
||||||
|
from = 3,
|
||||||
|
to = 4
|
||||||
|
),AutoMigration(
|
||||||
|
from = 4,
|
||||||
|
to = 5
|
||||||
|
)
|
||||||
|
],
|
||||||
|
exportSchema = true
|
||||||
|
)
|
||||||
|
@TypeConverters(DatabaseListConverters::class)
|
||||||
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
abstract fun settingDao(): SettingsDao
|
||||||
|
|
||||||
|
abstract fun tunnelConfigDoa(): TunnelConfigDao
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data
|
||||||
|
|
||||||
|
import androidx.room.TypeConverter
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
|
class DatabaseListConverters {
|
||||||
|
@TypeConverter
|
||||||
|
fun listToString(value: MutableList<String>): String {
|
||||||
|
return Json.encodeToString(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun stringToList(value: String): MutableList<String> {
|
||||||
|
if (value.isEmpty()) return mutableListOf()
|
||||||
|
return try {
|
||||||
|
Json.decodeFromString<MutableList<String>>(value)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
val list = value.split(",").toMutableList()
|
||||||
|
val json = listToString(list)
|
||||||
|
Json.decodeFromString<MutableList<String>>(json)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
-5
@@ -1,16 +1,15 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository
|
package com.zaneschepke.wireguardautotunnel.data
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Delete
|
import androidx.room.Delete
|
||||||
import androidx.room.Insert
|
import androidx.room.Insert
|
||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.Settings
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface SettingsDoa {
|
interface SettingsDao {
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun save(t: Settings)
|
suspend fun save(t: Settings)
|
||||||
|
|
||||||
@@ -23,6 +22,9 @@ interface SettingsDoa {
|
|||||||
@Query("SELECT * FROM settings")
|
@Query("SELECT * FROM settings")
|
||||||
suspend fun getAll(): List<Settings>
|
suspend fun getAll(): List<Settings>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM settings LIMIT 1")
|
||||||
|
fun getSettingsFlow(): Flow<Settings>
|
||||||
|
|
||||||
@Query("SELECT * FROM settings")
|
@Query("SELECT * FROM settings")
|
||||||
fun getAllFlow(): Flow<MutableList<Settings>>
|
fun getAllFlow(): Flow<MutableList<Settings>>
|
||||||
|
|
||||||
@@ -31,4 +33,4 @@ interface SettingsDoa {
|
|||||||
|
|
||||||
@Query("SELECT COUNT('id') FROM settings")
|
@Query("SELECT COUNT('id') FROM settings")
|
||||||
suspend fun count(): Long
|
suspend fun count(): Long
|
||||||
}
|
}
|
||||||
+4
-5
@@ -1,16 +1,15 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository
|
package com.zaneschepke.wireguardautotunnel.data
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Delete
|
import androidx.room.Delete
|
||||||
import androidx.room.Insert
|
import androidx.room.Insert
|
||||||
import androidx.room.OnConflictStrategy
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
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)
|
||||||
|
|
||||||
@@ -31,4 +30,4 @@ interface TunnelConfigDao{
|
|||||||
|
|
||||||
@Query("SELECT * FROM tunnelconfig")
|
@Query("SELECT * FROM tunnelconfig")
|
||||||
fun getAllFlow(): Flow<MutableList<TunnelConfig>>
|
fun getAllFlow(): Flow<MutableList<TunnelConfig>>
|
||||||
}
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.datastore
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.datastore.preferences.core.Preferences
|
||||||
|
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||||
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
|
class DataStoreManager(private val context: Context) {
|
||||||
|
companion object {
|
||||||
|
val LOCATION_DISCLOSURE_SHOWN = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
|
||||||
|
}
|
||||||
|
|
||||||
|
// preferences
|
||||||
|
private val preferencesKey = "preferences"
|
||||||
|
private val Context.dataStore by preferencesDataStore(
|
||||||
|
name = preferencesKey
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun init() {
|
||||||
|
context.dataStore.data.first()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> saveToDataStore(key: Preferences.Key<T>, value: T) =
|
||||||
|
context.dataStore.edit {
|
||||||
|
it[key] = value
|
||||||
|
}
|
||||||
|
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map {
|
||||||
|
it[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> getFromStore(key: Preferences.Key<T>) = context.dataStore.data.first { it.contains(key) }[key]
|
||||||
|
|
||||||
|
val locationDisclosureFlow: Flow<Boolean?> = context.dataStore.data.map {
|
||||||
|
it[LOCATION_DISCLOSURE_SHOWN]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
data class Settings(
|
||||||
|
@PrimaryKey(autoGenerate = true) val id: Int = 0,
|
||||||
|
@ColumnInfo(name = "is_tunnel_enabled") var isAutoTunnelEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled") var isTunnelOnMobileDataEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(name = "trusted_network_ssids") var trustedNetworkSSIDs: MutableList<String> = mutableListOf(),
|
||||||
|
@ColumnInfo(name = "default_tunnel") var defaultTunnel: String? = null,
|
||||||
|
@ColumnInfo(name = "is_always_on_vpn_enabled") var isAlwaysOnVpnEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled") var isTunnelOnEthernetEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_shortcuts_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isShortcutsEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_battery_saver_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isBatterySaverEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_tunnel_on_wifi_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isTunnelOnWifiEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_kernel_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isKernelEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_restore_on_boot_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isRestoreOnBootEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_multi_tunnel_enabled",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isMultiTunnelEnabled: Boolean = false,
|
||||||
|
@ColumnInfo(
|
||||||
|
name = "is_auto_tunnel_paused",
|
||||||
|
defaultValue = "false"
|
||||||
|
) var isAutoTunnelPaused: Boolean = false,
|
||||||
|
) {
|
||||||
|
fun isTunnelConfigDefault(tunnelConfig: TunnelConfig): Boolean {
|
||||||
|
return if (defaultTunnel != null) {
|
||||||
|
val defaultConfig = TunnelConfig.from(defaultTunnel!!)
|
||||||
|
(tunnelConfig.id == defaultConfig.id)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.Index
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
import com.wireguard.config.Config
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
@Entity(indices = [Index(value = ["name"], unique = true)])
|
||||||
|
@Serializable
|
||||||
|
data class TunnelConfig(
|
||||||
|
@PrimaryKey(autoGenerate = true) val id: Int = 0,
|
||||||
|
@ColumnInfo(name = "name") var name: String,
|
||||||
|
@ColumnInfo(name = "wg_quick") var wgQuick: String
|
||||||
|
) {
|
||||||
|
override fun toString(): String {
|
||||||
|
return Json.encodeToString(serializer(), this)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun from(string: String): TunnelConfig {
|
||||||
|
return Json.decodeFromString<TunnelConfig>(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun configFromQuick(wgQuick: String): Config {
|
||||||
|
val inputStream: InputStream = wgQuick.byteInputStream()
|
||||||
|
val reader = inputStream.bufferedReader(Charsets.UTF_8)
|
||||||
|
return Config.parse(reader)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
interface SettingsRepository {
|
||||||
|
suspend fun save(settings : Settings)
|
||||||
|
fun getSettingsFlow() : Flow<Settings>
|
||||||
|
|
||||||
|
suspend fun getSettings() : Settings
|
||||||
|
suspend fun getAll() : List<Settings>
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.SettingsDao
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
class SettingsRepositoryImpl(private val settingsDoa: SettingsDao) : SettingsRepository {
|
||||||
|
|
||||||
|
override suspend fun save(settings: Settings) {
|
||||||
|
settingsDoa.save(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getSettingsFlow(): Flow<Settings> {
|
||||||
|
return settingsDoa.getSettingsFlow()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getSettings(): Settings {
|
||||||
|
return settingsDoa.getAll().firstOrNull() ?: Settings()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getAll(): List<Settings> {
|
||||||
|
return settingsDoa.getAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
interface TunnelConfigRepository {
|
||||||
|
|
||||||
|
fun getTunnelConfigsFlow() : Flow<TunnelConfigs>
|
||||||
|
suspend fun getAll() : TunnelConfigs
|
||||||
|
suspend fun save(tunnelConfig: TunnelConfig)
|
||||||
|
suspend fun delete(tunnelConfig: TunnelConfig)
|
||||||
|
suspend fun count() : Int
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.data.repository
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
class TunnelConfigRepositoryImpl(private val tunnelConfigDao: TunnelConfigDao) : TunnelConfigRepository {
|
||||||
|
override fun getTunnelConfigsFlow(): Flow<TunnelConfigs> {
|
||||||
|
return tunnelConfigDao.getAllFlow()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getAll(): TunnelConfigs {
|
||||||
|
return tunnelConfigDao.getAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun save(tunnelConfig: TunnelConfig) {
|
||||||
|
tunnelConfigDao.save(tunnelConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun delete(tunnelConfig: TunnelConfig) {
|
||||||
|
tunnelConfigDao.delete(tunnelConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Int {
|
||||||
|
return tunnelConfigDao.count().toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ package com.zaneschepke.wireguardautotunnel.module
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.AppDatabase
|
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -16,10 +16,15 @@ import javax.inject.Singleton
|
|||||||
class DatabaseModule {
|
class DatabaseModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideDatabase(@ApplicationContext context : Context) : AppDatabase {
|
fun provideDatabase(
|
||||||
|
@ApplicationContext context: Context
|
||||||
|
): AppDatabase {
|
||||||
return Room.databaseBuilder(
|
return Room.databaseBuilder(
|
||||||
context,
|
context,
|
||||||
AppDatabase::class.java, context.getString(R.string.db_name)
|
AppDatabase::class.java,
|
||||||
).build()
|
context.getString(R.string.db_name)
|
||||||
|
)
|
||||||
|
.fallbackToDestructiveMigration()
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
|
import javax.inject.Qualifier
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class Kernel
|
||||||
@@ -1,27 +1,51 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.module
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.AppDatabase
|
import android.content.Context
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.AppDatabase
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.SettingsDao
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.TunnelConfigDao
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepositoryImpl
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepositoryImpl
|
||||||
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.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
class RepositoryModule {
|
class RepositoryModule {
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideSettingsRepository(appDatabase: AppDatabase) : SettingsDoa {
|
fun provideSettingsDoa(appDatabase: AppDatabase): SettingsDao {
|
||||||
return appDatabase.settingDao()
|
return appDatabase.settingDao()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideTunnelConfigRepository(appDatabase: AppDatabase) : TunnelConfigDao {
|
fun provideTunnelConfigDoa(appDatabase: AppDatabase): TunnelConfigDao {
|
||||||
return appDatabase.tunnelConfigDoa()
|
return appDatabase.tunnelConfigDoa()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao): TunnelConfigRepository {
|
||||||
|
return TunnelConfigRepositoryImpl(tunnelConfigDao)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun provideSettingsRepository(settingsDao: SettingsDao): SettingsRepository {
|
||||||
|
return SettingsRepositoryImpl(settingsDao)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
fun providePreferencesDataStore(@ApplicationContext context: Context): DataStoreManager {
|
||||||
|
return DataStoreManager(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,20 +15,19 @@ import dagger.hilt.android.scopes.ServiceScoped
|
|||||||
@Module
|
@Module
|
||||||
@InstallIn(ServiceComponent::class)
|
@InstallIn(ServiceComponent::class)
|
||||||
abstract class ServiceModule {
|
abstract class ServiceModule {
|
||||||
|
@Binds
|
||||||
|
@ServiceScoped
|
||||||
|
abstract fun provideNotificationService(wireGuardNotification: WireGuardNotification): NotificationService
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@ServiceScoped
|
||||||
abstract fun provideNotificationService(wireGuardNotification: WireGuardNotification) : NotificationService
|
abstract fun provideWifiService(wifiService: WifiService): NetworkService<WifiService>
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@ServiceScoped
|
||||||
abstract fun provideWifiService(wifiService: WifiService) : NetworkService<WifiService>
|
abstract fun provideMobileDataService(mobileDataService: MobileDataService): NetworkService<MobileDataService>
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
@ServiceScoped
|
@ServiceScoped
|
||||||
abstract fun provideMobileDataService(mobileDataService : MobileDataService) : NetworkService<MobileDataService>
|
abstract fun provideEthernetService(ethernetService: EthernetService): NetworkService<EthernetService>
|
||||||
|
}
|
||||||
@Binds
|
|
||||||
@ServiceScoped
|
|
||||||
abstract fun provideEthernetService(ethernetService: EthernetService) : NetworkService<EthernetService>
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ 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.WgQuickBackend
|
||||||
|
import com.wireguard.android.util.RootShell
|
||||||
|
import com.wireguard.android.util.ToolsInstaller
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
@@ -15,17 +19,40 @@ import javax.inject.Singleton
|
|||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
class TunnelModule {
|
class TunnelModule {
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideRootShell(
|
||||||
|
@ApplicationContext context: Context
|
||||||
|
): RootShell {
|
||||||
|
return RootShell(context)
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideBackend(@ApplicationContext context : Context) : Backend {
|
@Userspace
|
||||||
|
fun provideUserspaceBackend(
|
||||||
|
@ApplicationContext context: Context
|
||||||
|
): Backend {
|
||||||
return GoBackend(context)
|
return GoBackend(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideVpnService(backend: Backend) : VpnService {
|
@Kernel
|
||||||
return WireGuardTunnel(backend)
|
fun provideKernelBackend(
|
||||||
|
@ApplicationContext context: Context,
|
||||||
|
rootShell: RootShell
|
||||||
|
): Backend {
|
||||||
|
return WgQuickBackend(context, rootShell, ToolsInstaller(context, rootShell))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideVpnService(
|
||||||
|
@Userspace userspaceBackend: Backend,
|
||||||
|
@Kernel kernelBackend: Backend,
|
||||||
|
settingsRepository : SettingsRepository
|
||||||
|
): VpnService {
|
||||||
|
return WireGuardTunnel(userspaceBackend, kernelBackend, settingsRepository)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.module
|
||||||
|
|
||||||
|
import javax.inject.Qualifier
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
annotation class Userspace
|
||||||
@@ -3,36 +3,23 @@ package com.zaneschepke.wireguardautotunnel.receiver
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.goAsync
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.cancel
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class BootReceiver : BroadcastReceiver() {
|
class BootReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsRepo : SettingsDoa
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
override fun onReceive(context: Context?, intent: Intent?) = goAsync {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
if (Intent.ACTION_BOOT_COMPLETED != intent?.action) return@goAsync
|
||||||
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
|
if(settingsRepository.getSettings().isAutoTunnelEnabled) {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
ServiceManager.startWatcherServiceForeground(context!!)
|
||||||
try {
|
|
||||||
val settings = settingsRepo.getAll()
|
|
||||||
if (settings.isNotEmpty()) {
|
|
||||||
val setting = settings.first()
|
|
||||||
if (setting.isAutoTunnelEnabled && setting.defaultTunnel != null) {
|
|
||||||
ServiceManager.startWatcherService(context, setting.defaultTunnel!!)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|||||||
+18
-22
@@ -3,37 +3,33 @@ package com.zaneschepke.wireguardautotunnel.receiver
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
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 dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class NotificationActionReceiver : BroadcastReceiver() {
|
class NotificationActionReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsRepo : SettingsDoa
|
lateinit var settingsRepository: SettingsRepository
|
||||||
override fun onReceive(context: Context, intent: Intent?) {
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
override fun onReceive(
|
||||||
try {
|
context: Context,
|
||||||
val settings = settingsRepo.getAll()
|
intent: Intent?
|
||||||
if (settings.isNotEmpty()) {
|
) = goAsync {
|
||||||
val setting = settings.first()
|
try {
|
||||||
if (setting.defaultTunnel != null) {
|
val settings = settingsRepository.getSettings()
|
||||||
ServiceManager.stopVpnService(context)
|
if (settings.defaultTunnel != null) {
|
||||||
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
ServiceManager.stopVpnService(context)
|
||||||
ServiceManager.startVpnService(context, setting.defaultTunnel.toString())
|
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
||||||
}
|
ServiceManager.startVpnService(context, settings.defaultTunnel.toString())
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cancel()
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository
|
|
||||||
|
|
||||||
import androidx.room.Database
|
|
||||||
import androidx.room.RoomDatabase
|
|
||||||
import androidx.room.TypeConverters
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.Settings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
|
|
||||||
@Database(entities = [Settings::class, TunnelConfig::class], version = 1, exportSchema = false)
|
|
||||||
@TypeConverters(DatabaseListConverters::class)
|
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
|
||||||
abstract fun settingDao(): SettingsDoa
|
|
||||||
abstract fun tunnelConfigDoa() : TunnelConfigDao
|
|
||||||
}
|
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository
|
|
||||||
|
|
||||||
import androidx.room.TypeConverter
|
|
||||||
|
|
||||||
class DatabaseListConverters {
|
|
||||||
@TypeConverter
|
|
||||||
fun listToString(value: MutableList<String>): String {
|
|
||||||
return value.joinToString(",")
|
|
||||||
}
|
|
||||||
@TypeConverter
|
|
||||||
fun <T> stringToList(value: String): MutableList<String> {
|
|
||||||
if(value.isEmpty()) return mutableListOf()
|
|
||||||
return value.split(",").toMutableList()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository.model
|
|
||||||
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
data class Settings(
|
|
||||||
@PrimaryKey(autoGenerate = true) val id : Int = 0,
|
|
||||||
@ColumnInfo(name = "is_tunnel_enabled") var isAutoTunnelEnabled : Boolean = false,
|
|
||||||
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled") var isTunnelOnMobileDataEnabled : Boolean = false,
|
|
||||||
@ColumnInfo(name = "trusted_network_ssids") var trustedNetworkSSIDs : MutableList<String> = mutableListOf(),
|
|
||||||
@ColumnInfo(name = "default_tunnel") var defaultTunnel : String? = null,
|
|
||||||
@ColumnInfo(name = "is_always_on_vpn_enabled") var isAlwaysOnVpnEnabled : Boolean = false,
|
|
||||||
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled") var isTunnelOnEthernetEnabled : Boolean = false,
|
|
||||||
)
|
|
||||||
-85
@@ -1,85 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.repository.model
|
|
||||||
|
|
||||||
import androidx.room.ColumnInfo
|
|
||||||
import androidx.room.Entity
|
|
||||||
import androidx.room.Index
|
|
||||||
import androidx.room.PrimaryKey
|
|
||||||
import com.wireguard.config.Config
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import java.io.InputStream
|
|
||||||
|
|
||||||
@Entity(indices = [Index(value = ["name"], unique = true)])
|
|
||||||
@Serializable
|
|
||||||
data class TunnelConfig(
|
|
||||||
@PrimaryKey(autoGenerate = true) val id : Int = 0,
|
|
||||||
@ColumnInfo(name = "name") var name : String,
|
|
||||||
@ColumnInfo(name = "wg_quick") var wgQuick : String,
|
|
||||||
){
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return Json.encodeToString(serializer(), this)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val INCLUDED_APPLICATIONS = "IncludedApplications = "
|
|
||||||
private const val EXCLUDED_APPLICATIONS = "ExcludedApplications = "
|
|
||||||
private const val INTERFACE = "[Interface]"
|
|
||||||
private const val NEWLINE_CHAR = "\n"
|
|
||||||
private const val APP_CONFIG_SEPARATOR = ", "
|
|
||||||
|
|
||||||
private fun addApplicationsToConfig(appConfig : String, wgQuick : String) : String {
|
|
||||||
val configList = wgQuick.split(NEWLINE_CHAR).toMutableList()
|
|
||||||
val interfaceIndex = configList.indexOf(INTERFACE)
|
|
||||||
configList.add(interfaceIndex + 1, appConfig)
|
|
||||||
return configList.joinToString(NEWLINE_CHAR)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun clearAllApplicationsFromConfig(wgQuick : String) : String {
|
|
||||||
val configList = wgQuick.split(NEWLINE_CHAR).toMutableList()
|
|
||||||
val itr = configList.iterator()
|
|
||||||
while (itr.hasNext()) {
|
|
||||||
val next = itr.next()
|
|
||||||
if(next.contains(INCLUDED_APPLICATIONS) || next.contains(EXCLUDED_APPLICATIONS)) {
|
|
||||||
itr.remove()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return configList.joinToString(NEWLINE_CHAR)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun setExcludedApplicationsOnQuick(packages : List<String>, wgQuick: String) : String {
|
|
||||||
if(packages.isEmpty()) {
|
|
||||||
return wgQuick
|
|
||||||
}
|
|
||||||
val clearedWgQuick = clearAllApplicationsFromConfig(wgQuick)
|
|
||||||
val excludeConfig = buildExcludedApplicationsString(packages)
|
|
||||||
return addApplicationsToConfig(excludeConfig, clearedWgQuick)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setIncludedApplicationsOnQuick(packages : List<String>, wgQuick: String) : String {
|
|
||||||
if(packages.isEmpty()) {
|
|
||||||
return wgQuick
|
|
||||||
}
|
|
||||||
val clearedWgQuick = clearAllApplicationsFromConfig(wgQuick)
|
|
||||||
val includeConfig = buildIncludedApplicationsString(packages)
|
|
||||||
return addApplicationsToConfig(includeConfig, clearedWgQuick)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildExcludedApplicationsString(packages : List<String>) : String {
|
|
||||||
return EXCLUDED_APPLICATIONS + packages.joinToString(APP_CONFIG_SEPARATOR)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildIncludedApplicationsString(packages : List<String>) : String {
|
|
||||||
return INCLUDED_APPLICATIONS + packages.joinToString(APP_CONFIG_SEPARATOR)
|
|
||||||
}
|
|
||||||
fun from(string : String) : TunnelConfig {
|
|
||||||
return Json.decodeFromString<TunnelConfig>(string)
|
|
||||||
}
|
|
||||||
fun configFromQuick(wgQuick: String): Config {
|
|
||||||
val inputStream: InputStream = wgQuick.byteInputStream()
|
|
||||||
val reader = inputStream.bufferedReader(Charsets.UTF_8)
|
|
||||||
return Config.parse(reader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,4 +4,4 @@ enum class Action {
|
|||||||
START,
|
START,
|
||||||
START_FOREGROUND,
|
START_FOREGROUND,
|
||||||
STOP
|
STOP
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-9
@@ -1,22 +1,26 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.foreground
|
package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||||
|
|
||||||
import android.app.Service
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
|
import androidx.lifecycle.LifecycleService
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
|
open class ForegroundService : LifecycleService() {
|
||||||
open class ForegroundService : Service() {
|
|
||||||
|
|
||||||
private var isServiceStarted = false
|
private var isServiceStarted = false
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
|
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)
|
||||||
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
|
||||||
@@ -39,19 +43,18 @@ open class ForegroundService : Service() {
|
|||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
Timber.d("The service has been destroyed")
|
Timber.d("The service has been destroyed")
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun startService(extras : Bundle?) {
|
protected open fun startService(extras: Bundle?) {
|
||||||
if (isServiceStarted) return
|
if (isServiceStarted) return
|
||||||
Timber.d("Starting ${this.javaClass.simpleName}")
|
Timber.d("Starting ${this.javaClass.simpleName}")
|
||||||
isServiceStarted = true
|
isServiceStarted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun stopService(extras : Bundle?) {
|
protected open fun stopService(extras: Bundle?) {
|
||||||
Timber.d("Stopping ${this.javaClass.simpleName}")
|
Timber.d("Stopping ${this.javaClass.simpleName}")
|
||||||
try {
|
try {
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||||
@@ -61,4 +64,4 @@ open class ForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
isServiceStarted = false
|
isServiceStarted = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-45
@@ -16,44 +16,60 @@ object ServiceManager {
|
|||||||
.getRunningServices(Integer.MAX_VALUE)
|
.getRunningServices(Integer.MAX_VALUE)
|
||||||
.any { it.service.className == service.name }
|
.any { it.service.className == service.name }
|
||||||
|
|
||||||
fun <T : Service> getServiceState(context: Context, cls : Class<T>): ServiceState {
|
fun <T : Service> getServiceState(
|
||||||
|
context: Context,
|
||||||
|
cls: Class<T>
|
||||||
|
): ServiceState {
|
||||||
val isServiceRunning = context.isServiceRunning(cls)
|
val isServiceRunning = context.isServiceRunning(cls)
|
||||||
return if(isServiceRunning) ServiceState.STARTED else ServiceState.STOPPED
|
return if (isServiceRunning) ServiceState.STARTED else ServiceState.STOPPED
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Service> actionOnService(action: Action, context: Context, cls : Class<T>, extras : Map<String,String>? = null) {
|
private fun <T : Service> actionOnService(
|
||||||
|
action: Action,
|
||||||
|
context: Context,
|
||||||
|
cls: Class<T>,
|
||||||
|
extras: Map<String, String>? = null
|
||||||
|
) {
|
||||||
if (getServiceState(context, cls) == ServiceState.STOPPED && action == Action.STOP) return
|
if (getServiceState(context, cls) == ServiceState.STOPPED && action == Action.STOP) return
|
||||||
if (getServiceState(context, cls) == ServiceState.STARTED && action == Action.START) return
|
if (getServiceState(context, cls) == ServiceState.STARTED && action == Action.START) return
|
||||||
val intent = Intent(context, cls).also {
|
val intent =
|
||||||
it.action = action.name
|
Intent(context, cls).also {
|
||||||
extras?.forEach {(k, v) ->
|
it.action = action.name
|
||||||
it.putExtra(k, v)
|
extras?.forEach { (k, v) ->
|
||||||
|
it.putExtra(k, v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
intent.component?.javaClass
|
intent.component?.javaClass
|
||||||
try {
|
try {
|
||||||
when(action) {
|
when (action) {
|
||||||
Action.START_FOREGROUND -> {
|
Action.START_FOREGROUND -> {
|
||||||
context.startForegroundService(intent)
|
context.startForegroundService(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
Action.START -> {
|
Action.START -> {
|
||||||
context.startService(intent)
|
context.startService(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
Action.STOP -> context.startService(intent)
|
Action.STOP -> context.startService(intent)
|
||||||
}
|
}
|
||||||
} catch (e : Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e.message)
|
Timber.e(e.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startVpnService(context : Context, tunnelConfig : String) {
|
fun startVpnService(
|
||||||
|
context: Context,
|
||||||
|
tunnelConfig: String
|
||||||
|
) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.START,
|
Action.START,
|
||||||
context,
|
context,
|
||||||
WireGuardTunnelService::class.java,
|
WireGuardTunnelService::class.java,
|
||||||
mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig))
|
mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
fun stopVpnService(context : Context) {
|
|
||||||
|
fun stopVpnService(context: Context) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.STOP,
|
Action.STOP,
|
||||||
context,
|
context,
|
||||||
@@ -61,49 +77,43 @@ object ServiceManager {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startVpnServiceForeground(context : Context, tunnelConfig : String) {
|
fun startVpnServiceForeground(
|
||||||
|
context: Context,
|
||||||
|
tunnelConfig: String
|
||||||
|
) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.START_FOREGROUND,
|
Action.START_FOREGROUND,
|
||||||
context,
|
context,
|
||||||
WireGuardTunnelService::class.java,
|
WireGuardTunnelService::class.java,
|
||||||
mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig))
|
mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startWatcherServiceForeground(context : Context, tunnelConfig : String) {
|
fun startWatcherServiceForeground(
|
||||||
|
context: Context,
|
||||||
|
) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.START, context,
|
Action.START_FOREGROUND,
|
||||||
WireGuardConnectivityWatcherService::class.java, mapOf(context.
|
context,
|
||||||
getString(R.string.tunnel_extras_key) to
|
WireGuardConnectivityWatcherService::class.java
|
||||||
tunnelConfig))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startWatcherService(context : Context, tunnelConfig : String) {
|
fun startWatcherService(
|
||||||
|
context: Context
|
||||||
|
) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.START, context,
|
Action.START,
|
||||||
WireGuardConnectivityWatcherService::class.java, mapOf(context.
|
context,
|
||||||
getString(R.string.tunnel_extras_key) to
|
WireGuardConnectivityWatcherService::class.java
|
||||||
tunnelConfig))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopWatcherService(context : Context) {
|
fun stopWatcherService(context: Context) {
|
||||||
actionOnService(
|
actionOnService(
|
||||||
Action.STOP, context,
|
Action.STOP,
|
||||||
WireGuardConnectivityWatcherService::class.java)
|
context,
|
||||||
|
WireGuardConnectivityWatcherService::class.java
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fun toggleWatcherService(context: Context, tunnelConfig : String) {
|
|
||||||
when(getServiceState( context,
|
|
||||||
WireGuardConnectivityWatcherService::class.java,)) {
|
|
||||||
ServiceState.STARTED -> stopWatcherService(context)
|
|
||||||
ServiceState.STOPPED -> startWatcherService(context, tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toggleWatcherServiceForeground(context: Context, tunnelConfig : String) {
|
|
||||||
when(getServiceState( context,
|
|
||||||
WireGuardConnectivityWatcherService::class.java,)) {
|
|
||||||
ServiceState.STARTED -> stopWatcherService(context)
|
|
||||||
ServiceState.STOPPED -> startWatcherServiceForeground(context, tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+1
-1
@@ -3,4 +3,4 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
|||||||
enum class ServiceState {
|
enum class ServiceState {
|
||||||
STARTED,
|
STARTED,
|
||||||
STOPPED,
|
STOPPED,
|
||||||
}
|
}
|
||||||
|
|||||||
+301
-179
@@ -7,11 +7,12 @@ import android.content.Intent
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
|
import androidx.core.app.ServiceCompat
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.Settings
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
import com.zaneschepke.wireguardautotunnel.service.network.EthernetService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
import com.zaneschepke.wireguardautotunnel.service.network.MobileDataService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
import com.zaneschepke.wireguardautotunnel.service.network.NetworkService
|
||||||
@@ -19,227 +20,348 @@ import com.zaneschepke.wireguardautotunnel.service.network.NetworkStatus
|
|||||||
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
import com.zaneschepke.wireguardautotunnel.service.network.WifiService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
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.collectLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class WireGuardConnectivityWatcherService : ForegroundService() {
|
class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
|
private val foregroundId = 122
|
||||||
|
|
||||||
private val foregroundId = 122;
|
@Inject lateinit var wifiService: NetworkService<WifiService>
|
||||||
|
|
||||||
@Inject
|
@Inject lateinit var mobileDataService: NetworkService<MobileDataService>
|
||||||
lateinit var wifiService : NetworkService<WifiService>
|
|
||||||
|
|
||||||
@Inject
|
@Inject lateinit var ethernetService: NetworkService<EthernetService>
|
||||||
lateinit var mobileDataService : NetworkService<MobileDataService>
|
|
||||||
|
|
||||||
@Inject
|
@Inject lateinit var settingsRepository: SettingsRepository
|
||||||
lateinit var ethernetService: NetworkService<EthernetService>
|
|
||||||
|
|
||||||
@Inject
|
@Inject lateinit var notificationService: NotificationService
|
||||||
lateinit var settingsRepo: SettingsDoa
|
|
||||||
|
|
||||||
@Inject
|
@Inject lateinit var vpnService: VpnService
|
||||||
lateinit var notificationService : NotificationService
|
|
||||||
|
|
||||||
@Inject
|
private val networkEventsFlow = MutableStateFlow(WatcherState())
|
||||||
lateinit var vpnService : VpnService
|
data class WatcherState(
|
||||||
|
val isWifiConnected: Boolean = false,
|
||||||
|
val isVpnConnected : Boolean = false,
|
||||||
|
val isEthernetConnected: Boolean = false,
|
||||||
|
val isMobileDataConnected: Boolean = false,
|
||||||
|
val currentNetworkSSID: String = "",
|
||||||
|
val settings: Settings = Settings()
|
||||||
|
)
|
||||||
|
|
||||||
private var isWifiConnected = false;
|
private lateinit var watcherJob: Job
|
||||||
private var isEthernetConnected = false;
|
|
||||||
private var isMobileDataConnected = false;
|
|
||||||
private var currentNetworkSSID = "";
|
|
||||||
|
|
||||||
private lateinit var watcherJob : Job;
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
private lateinit var setting : Settings
|
private val tag = this.javaClass.name
|
||||||
private lateinit var tunnelConfig: String
|
|
||||||
|
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
override fun onCreate() {
|
||||||
private val tag = this.javaClass.name;
|
super.onCreate()
|
||||||
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
|
try {
|
||||||
override fun onCreate() {
|
if(settingsRepository.getSettings().isAutoTunnelPaused) {
|
||||||
super.onCreate()
|
launchWatcherPausedNotification()
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
} else launchWatcherNotification()
|
||||||
launchWatcherNotification()
|
} catch (e: Exception) {
|
||||||
}
|
Timber.e("Failed to start watcher service, not enough permissions")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun startService(extras: Bundle?) {
|
override fun startService(extras: Bundle?) {
|
||||||
super.startService(extras)
|
super.startService(extras)
|
||||||
launchWatcherNotification()
|
try {
|
||||||
val tunnelId = extras?.getString(getString(R.string.tunnel_extras_key))
|
// we need this lock so our service gets not affected by Doze Mode
|
||||||
if (tunnelId != null) {
|
lifecycleScope.launch { initWakeLock() }
|
||||||
this.tunnelConfig = tunnelId
|
cancelWatcherJob()
|
||||||
}
|
startWatcherJob()
|
||||||
// we need this lock so our service gets not affected by Doze Mode
|
} catch (e: Exception) {
|
||||||
initWakeLock()
|
Timber.e("Failed to launch watcher service, no permissions")
|
||||||
cancelWatcherJob()
|
|
||||||
if(this::tunnelConfig.isInitialized) {
|
|
||||||
startWatcherJob()
|
|
||||||
} else {
|
|
||||||
stopService(extras)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun stopService(extras: Bundle?) {
|
override fun stopService(extras: Bundle?) {
|
||||||
super.stopService(extras)
|
super.stopService(extras)
|
||||||
wakeLock?.let {
|
wakeLock?.let {
|
||||||
if (it.isHeld) {
|
if (it.isHeld) {
|
||||||
it.release()
|
it.release()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
cancelWatcherJob()
|
|
||||||
stopSelf()
|
|
||||||
}
|
}
|
||||||
|
cancelWatcherJob()
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
private fun launchWatcherNotification() {
|
private fun launchWatcherNotification(description: String = getString(R.string.watcher_notification_text_active)) {
|
||||||
val notification = notificationService.createNotification(
|
val notification =
|
||||||
|
notificationService.createNotification(
|
||||||
channelId = getString(R.string.watcher_channel_id),
|
channelId = getString(R.string.watcher_channel_id),
|
||||||
channelName = getString(R.string.watcher_channel_name),
|
channelName = getString(R.string.watcher_channel_name),
|
||||||
description = getString(R.string.watcher_notification_text))
|
title = getString(R.string.auto_tunnel_title),
|
||||||
super.startForeground(foregroundId, notification)
|
description = description)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this, foregroundId, notification, Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchWatcherPausedNotification() {
|
||||||
|
launchWatcherNotification(getString(R.string.watcher_notification_text_paused))
|
||||||
}
|
}
|
||||||
|
|
||||||
//try to start task again if killed
|
// TODO could this be restarting service in a bad state?
|
||||||
override fun onTaskRemoved(rootIntent: Intent) {
|
// try to start task again if killed
|
||||||
Timber.d("Task Removed called")
|
override fun onTaskRemoved(rootIntent: Intent) {
|
||||||
val restartServiceIntent = Intent(rootIntent)
|
Timber.d("Task Removed called")
|
||||||
val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent,
|
val restartServiceIntent = Intent(rootIntent)
|
||||||
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE);
|
val restartServicePendingIntent: PendingIntent =
|
||||||
applicationContext.getSystemService(Context.ALARM_SERVICE);
|
PendingIntent.getService(
|
||||||
val alarmService: AlarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager;
|
this,
|
||||||
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent);
|
1,
|
||||||
}
|
restartServiceIntent,
|
||||||
|
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
|
||||||
|
applicationContext.getSystemService(Context.ALARM_SERVICE)
|
||||||
|
val alarmService: AlarmManager =
|
||||||
|
applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||||
|
alarmService.set(
|
||||||
|
AlarmManager.ELAPSED_REALTIME,
|
||||||
|
SystemClock.elapsedRealtime() + 1000,
|
||||||
|
restartServicePendingIntent)
|
||||||
|
}
|
||||||
|
|
||||||
private fun initWakeLock() {
|
private suspend fun initWakeLock() {
|
||||||
wakeLock =
|
val isBatterySaverOn =
|
||||||
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
withContext(lifecycleScope.coroutineContext) {
|
||||||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
|
settingsRepository.getSettings().isBatterySaverEnabled
|
||||||
acquire()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun cancelWatcherJob() {
|
|
||||||
if(this::watcherJob.isInitialized) {
|
|
||||||
watcherJob.cancel()
|
|
||||||
}
|
}
|
||||||
}
|
wakeLock =
|
||||||
|
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||||
|
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
|
||||||
|
if (isBatterySaverOn) {
|
||||||
|
Timber.d("Initiating wakelock with timeout")
|
||||||
|
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
|
||||||
|
} else {
|
||||||
|
Timber.d("Initiating wakelock with zero timeout")
|
||||||
|
acquire(Constants.DEFAULT_WATCHER_WAKE_LOCK_TIMEOUT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun startWatcherJob() {
|
private fun cancelWatcherJob() {
|
||||||
watcherJob = CoroutineScope(Dispatchers.IO).launch {
|
if (this::watcherJob.isInitialized) {
|
||||||
val settings = settingsRepo.getAll();
|
watcherJob.cancel()
|
||||||
if(settings.isNotEmpty()) {
|
}
|
||||||
setting = settings[0]
|
}
|
||||||
|
|
||||||
|
private fun startWatcherJob() {
|
||||||
|
watcherJob =
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
val setting = settingsRepository.getSettings()
|
||||||
|
launch {
|
||||||
|
Timber.d("Starting wifi watcher")
|
||||||
|
watchForWifiConnectivityChanges()
|
||||||
|
}
|
||||||
|
if (setting.isTunnelOnMobileDataEnabled) {
|
||||||
|
launch {
|
||||||
|
Timber.d("Starting mobile data watcher")
|
||||||
|
watchForMobileDataConnectivityChanges()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (setting.isTunnelOnEthernetEnabled) {
|
||||||
|
launch {
|
||||||
|
Timber.d("Starting ethernet data watcher")
|
||||||
|
watchForEthernetConnectivityChanges()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
launch {
|
||||||
|
Timber.d("Starting vpn state watcher")
|
||||||
|
watchForVpnConnectivityChanges()
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
watchForWifiConnectivityChanges()
|
Timber.d("Starting settings watcher")
|
||||||
|
watchForSettingsChanges()
|
||||||
}
|
}
|
||||||
if(setting.isTunnelOnMobileDataEnabled) {
|
launch {
|
||||||
launch {
|
Timber.d("Starting management watcher")
|
||||||
watchForMobileDataConnectivityChanges()
|
manageVpn()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForMobileDataConnectivityChanges() {
|
||||||
|
mobileDataService.networkStatus.collect {
|
||||||
|
when (it) {
|
||||||
|
is NetworkStatus.Available -> {
|
||||||
|
Timber.d("Gained Mobile data connection")
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isMobileDataConnected = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isMobileDataConnected = true
|
||||||
|
)
|
||||||
|
Timber.d("Mobile data capabilities changed")
|
||||||
|
}
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isMobileDataConnected = false
|
||||||
|
)
|
||||||
|
Timber.d("Lost mobile data connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private suspend fun watchForSettingsChanges() {
|
||||||
|
settingsRepository.getSettingsFlow().collect {
|
||||||
|
if(networkEventsFlow.value.settings.isAutoTunnelPaused != it.isAutoTunnelPaused) {
|
||||||
|
when(it.isAutoTunnelPaused) {
|
||||||
|
true -> launchWatcherPausedNotification()
|
||||||
|
false -> launchWatcherNotification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(setting.isTunnelOnEthernetEnabled) {
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
launch {
|
settings = it
|
||||||
watchForEthernetConnectivityChanges()
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
|
||||||
manageVpn()
|
private suspend fun watchForVpnConnectivityChanges() {
|
||||||
|
vpnService.vpnState.collect {
|
||||||
|
when(it.status) {
|
||||||
|
Tunnel.State.DOWN -> networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isVpnConnected = false
|
||||||
|
)
|
||||||
|
Tunnel.State.UP -> networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isVpnConnected = true
|
||||||
|
)
|
||||||
|
else -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun watchForMobileDataConnectivityChanges() {
|
private suspend fun watchForEthernetConnectivityChanges() {
|
||||||
mobileDataService.networkStatus.collect {
|
ethernetService.networkStatus.collect {
|
||||||
when(it) {
|
when (it) {
|
||||||
is NetworkStatus.Available -> {
|
is NetworkStatus.Available -> {
|
||||||
Timber.d("Gained Mobile data connection")
|
Timber.d("Gained Ethernet connection")
|
||||||
isMobileDataConnected = true
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isEthernetConnected = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
Timber.d("Ethernet capabilities changed")
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isEthernetConnected = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isEthernetConnected = false
|
||||||
|
)
|
||||||
|
Timber.d("Lost Ethernet connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun watchForWifiConnectivityChanges() {
|
||||||
|
wifiService.networkStatus.collect {
|
||||||
|
when (it) {
|
||||||
|
is NetworkStatus.Available -> {
|
||||||
|
Timber.d("Gained Wi-Fi connection")
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isWifiConnected = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> {
|
||||||
|
Timber.d("Wifi capabilities changed")
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isWifiConnected = true
|
||||||
|
)
|
||||||
|
val ssid = wifiService.getNetworkName(it.networkCapabilities) ?: ""
|
||||||
|
Timber.d("Detected SSID: $ssid")
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
currentNetworkSSID = ssid
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is NetworkStatus.Unavailable -> {
|
||||||
|
networkEventsFlow.value = networkEventsFlow.value.copy(
|
||||||
|
isWifiConnected = false
|
||||||
|
)
|
||||||
|
Timber.d("Lost Wi-Fi connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO clean this up
|
||||||
|
private suspend fun manageVpn() {
|
||||||
|
networkEventsFlow.collectLatest {
|
||||||
|
Timber.i("New watcher state: $it")
|
||||||
|
if (!it.settings.isAutoTunnelPaused && it.settings.defaultTunnel != null) {
|
||||||
|
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
||||||
|
when {
|
||||||
|
((it.isEthernetConnected &&
|
||||||
|
it.settings.isTunnelOnEthernetEnabled &&
|
||||||
|
!it.isVpnConnected)) -> {
|
||||||
|
ServiceManager.startVpnService(this, it.settings.defaultTunnel!!)
|
||||||
|
Timber.i("Condition 1 met")
|
||||||
}
|
}
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
(!it.isEthernetConnected &&
|
||||||
isMobileDataConnected = true
|
it.settings.isTunnelOnMobileDataEnabled &&
|
||||||
Timber.d("Mobile data capabilities changed")
|
!it.isWifiConnected &&
|
||||||
|
it.isMobileDataConnected &&
|
||||||
|
!it.isVpnConnected) -> {
|
||||||
|
ServiceManager.startVpnService(this, it.settings.defaultTunnel!!)
|
||||||
|
Timber.i("Condition 2 met")
|
||||||
}
|
}
|
||||||
is NetworkStatus.Unavailable -> {
|
(!it.isEthernetConnected &&
|
||||||
isMobileDataConnected = false
|
!it.settings.isTunnelOnMobileDataEnabled &&
|
||||||
Timber.d("Lost mobile data connection")
|
!it.isWifiConnected &&
|
||||||
|
it.isVpnConnected) -> {
|
||||||
|
ServiceManager.stopVpnService(this)
|
||||||
|
Timber.i("Condition 3 met")
|
||||||
|
}
|
||||||
|
(!it.isEthernetConnected &&
|
||||||
|
it.isWifiConnected &&
|
||||||
|
!it.settings.trustedNetworkSSIDs.contains(it.currentNetworkSSID) &&
|
||||||
|
it.settings.isTunnelOnWifiEnabled &&
|
||||||
|
(!it.isVpnConnected)) -> {
|
||||||
|
ServiceManager.startVpnService(this, it.settings.defaultTunnel!!)
|
||||||
|
Timber.i("Condition 4 met")
|
||||||
|
}
|
||||||
|
(!it.isEthernetConnected &&
|
||||||
|
(it.isWifiConnected && it.settings.trustedNetworkSSIDs.contains(it.currentNetworkSSID)) &&
|
||||||
|
(it.isVpnConnected)) -> {
|
||||||
|
ServiceManager.stopVpnService(this)
|
||||||
|
Timber.i("Condition 5 met")
|
||||||
|
}
|
||||||
|
(!it.isEthernetConnected &&
|
||||||
|
(it.isWifiConnected &&
|
||||||
|
!it.settings.isTunnelOnWifiEnabled &&
|
||||||
|
(it.isVpnConnected))) -> {
|
||||||
|
ServiceManager.stopVpnService(this)
|
||||||
|
Timber.i("Condition 6 met")
|
||||||
|
}
|
||||||
|
(!it.isEthernetConnected &&
|
||||||
|
!it.isWifiConnected &&
|
||||||
|
!it.isMobileDataConnected &&
|
||||||
|
(it.isVpnConnected)) -> {
|
||||||
|
ServiceManager.stopVpnService(this)
|
||||||
|
Timber.i("Condition 7 met")
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
Timber.i("No condition met")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private suspend fun watchForEthernetConnectivityChanges() {
|
}
|
||||||
ethernetService.networkStatus.collect {
|
|
||||||
when (it) {
|
|
||||||
is NetworkStatus.Available -> {
|
|
||||||
Timber.d("Gained Ethernet connection")
|
|
||||||
isEthernetConnected = true
|
|
||||||
}
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.d("Ethernet capabilities changed")
|
|
||||||
isEthernetConnected = true
|
|
||||||
}
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
isEthernetConnected = false
|
|
||||||
Timber.d("Lost Ethernet connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun watchForWifiConnectivityChanges() {
|
|
||||||
wifiService.networkStatus.collect {
|
|
||||||
when (it) {
|
|
||||||
is NetworkStatus.Available -> {
|
|
||||||
Timber.d("Gained Wi-Fi connection")
|
|
||||||
isWifiConnected = true
|
|
||||||
}
|
|
||||||
is NetworkStatus.CapabilitiesChanged -> {
|
|
||||||
Timber.d("Wifi capabilities changed")
|
|
||||||
isWifiConnected = true
|
|
||||||
currentNetworkSSID = wifiService.getNetworkName(it.networkCapabilities) ?: "";
|
|
||||||
}
|
|
||||||
is NetworkStatus.Unavailable -> {
|
|
||||||
isWifiConnected = false
|
|
||||||
Timber.d("Lost Wi-Fi connection")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun manageVpn() {
|
|
||||||
while(true) {
|
|
||||||
if(isEthernetConnected && setting.isTunnelOnEthernetEnabled && vpnService.getState() == Tunnel.State.DOWN) {
|
|
||||||
ServiceManager.startVpnService(this, tunnelConfig)
|
|
||||||
}
|
|
||||||
if(!isEthernetConnected && setting.isTunnelOnMobileDataEnabled &&
|
|
||||||
!isWifiConnected &&
|
|
||||||
isMobileDataConnected
|
|
||||||
&& vpnService.getState() == Tunnel.State.DOWN) {
|
|
||||||
ServiceManager.startVpnService(this, tunnelConfig)
|
|
||||||
} else if(!isEthernetConnected && !setting.isTunnelOnMobileDataEnabled &&
|
|
||||||
!isWifiConnected &&
|
|
||||||
vpnService.getState() == Tunnel.State.UP) {
|
|
||||||
ServiceManager.stopVpnService(this)
|
|
||||||
} else if(!isEthernetConnected && isWifiConnected &&
|
|
||||||
!setting.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
|
||||||
(vpnService.getState() != Tunnel.State.UP)) {
|
|
||||||
ServiceManager.startVpnService(this, tunnelConfig)
|
|
||||||
} else if(!isEthernetConnected && (isWifiConnected &&
|
|
||||||
setting.trustedNetworkSSIDs.contains(currentNetworkSSID)) &&
|
|
||||||
(vpnService.getState() == Tunnel.State.UP)) {
|
|
||||||
ServiceManager.stopVpnService(this)
|
|
||||||
}
|
|
||||||
delay(Constants.VPN_CONNECTIVITY_CHECK_INTERVAL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+123
-101
@@ -3,157 +3,179 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
|||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.core.app.ServiceCompat
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
|
import com.zaneschepke.wireguardautotunnel.receiver.NotificationActionReceiver
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.handshakeStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.mapPeerStats
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
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 WireGuardTunnelService : ForegroundService() {
|
class WireGuardTunnelService : ForegroundService() {
|
||||||
|
private val foregroundId = 123
|
||||||
private val foregroundId = 123;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var vpnService : VpnService
|
lateinit var vpnService: VpnService
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsRepo: SettingsDoa
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var notificationService : NotificationService
|
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
||||||
|
|
||||||
private lateinit var job : Job
|
@Inject
|
||||||
|
lateinit var notificationService: NotificationService
|
||||||
|
|
||||||
private var tunnelName : String = ""
|
private lateinit var job: Job
|
||||||
|
|
||||||
|
private var tunnelName: String = ""
|
||||||
|
private var didShowConnected = false
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
launchVpnStartingNotification()
|
if(tunnelConfigRepository.getAll().isNotEmpty()) {
|
||||||
|
launchVpnNotification()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startService(extras : Bundle?) {
|
override fun startService(extras: Bundle?) {
|
||||||
super.startService(extras)
|
super.startService(extras)
|
||||||
launchVpnStartingNotification()
|
|
||||||
val tunnelConfigString = extras?.getString(getString(R.string.tunnel_extras_key))
|
|
||||||
cancelJob()
|
cancelJob()
|
||||||
job = CoroutineScope(Dispatchers.IO).launch {
|
val tunnelConfigString = extras?.getString(getString(R.string.tunnel_extras_key))
|
||||||
if(tunnelConfigString != null) {
|
val tunnelConfig = tunnelConfigString?.let {
|
||||||
try {
|
TunnelConfig.from(it)
|
||||||
val tunnelConfig = TunnelConfig.from(tunnelConfigString)
|
}
|
||||||
tunnelName = tunnelConfig.name
|
tunnelName = tunnelConfig?.name ?: ""
|
||||||
vpnService.startTunnel(tunnelConfig)
|
job = lifecycleScope.launch(Dispatchers.IO) {
|
||||||
} catch (e : Exception) {
|
launch {
|
||||||
Timber.e("Problem starting tunnel: ${e.message}")
|
if (tunnelConfig != null) {
|
||||||
stopService(extras)
|
try {
|
||||||
|
tunnelName = tunnelConfig.name
|
||||||
|
vpnService.startTunnel(tunnelConfig)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e("Problem starting tunnel: ${e.message}")
|
||||||
|
stopService(extras)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timber.d("Tunnel config null, starting default tunnel or first")
|
||||||
|
val settings = settingsRepository.getSettings()
|
||||||
|
val tunnels = tunnelConfigRepository.getAll()
|
||||||
|
if (settings.isAlwaysOnVpnEnabled) {
|
||||||
|
val tunnel = if(settings.defaultTunnel != null) {
|
||||||
|
TunnelConfig.from(settings.defaultTunnel!!)
|
||||||
|
} else if(tunnels.isNotEmpty()) {
|
||||||
|
tunnels.first()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
if(tunnel != null) {
|
||||||
|
tunnelName = tunnel.name
|
||||||
|
vpnService.startTunnel(tunnel)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
//TODO add failed to connect notification
|
||||||
Timber.d("Tunnel config null, starting default tunnel")
|
launch {
|
||||||
val settings = settingsRepo.getAll();
|
vpnService.vpnState.collect { state ->
|
||||||
if(settings.isNotEmpty()) {
|
state.statistics
|
||||||
val setting = settings[0]
|
?.mapPeerStats()
|
||||||
if(setting.defaultTunnel != null && setting.isAlwaysOnVpnEnabled) {
|
?.map { it.value?.handshakeStatus() }
|
||||||
val tunnelConfig = TunnelConfig.from(setting.defaultTunnel!!)
|
.let { statuses ->
|
||||||
tunnelName = tunnelConfig.name
|
when {
|
||||||
vpnService.startTunnel(tunnelConfig)
|
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> {
|
||||||
|
if(!didShowConnected){
|
||||||
|
delay(Constants.VPN_CONNECTED_NOTIFICATION_DELAY)
|
||||||
|
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 -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
CoroutineScope(job).launch {
|
|
||||||
var didShowConnected = false
|
|
||||||
var didShowFailedHandshakeNotification = false
|
|
||||||
vpnService.handshakeStatus.collect {
|
|
||||||
when(it) {
|
|
||||||
HandshakeStatus.NOT_STARTED -> {
|
|
||||||
}
|
|
||||||
HandshakeStatus.NEVER_CONNECTED -> {
|
|
||||||
if(!didShowFailedHandshakeNotification) {
|
|
||||||
launchVpnConnectionFailedNotification(getString(R.string.initial_connection_failure_message))
|
|
||||||
didShowFailedHandshakeNotification = true
|
|
||||||
didShowConnected = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HandshakeStatus.HEALTHY -> {
|
|
||||||
if(!didShowConnected) {
|
|
||||||
launchVpnConnectedNotification()
|
|
||||||
didShowConnected = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HandshakeStatus.UNHEALTHY -> {
|
|
||||||
if(!didShowFailedHandshakeNotification) {
|
|
||||||
launchVpnConnectionFailedNotification(getString(R.string.lost_connection_failure_message))
|
|
||||||
didShowFailedHandshakeNotification = true
|
|
||||||
didShowConnected = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stopService(extras : Bundle?) {
|
override fun stopService(extras: Bundle?) {
|
||||||
super.stopService(extras)
|
super.stopService(extras)
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
vpnService.stopTunnel()
|
vpnService.stopTunnel()
|
||||||
|
didShowConnected = false
|
||||||
}
|
}
|
||||||
cancelJob()
|
cancelJob()
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun launchVpnConnectedNotification() {
|
private fun launchVpnNotification(title : String = getString(R.string.vpn_starting),description : String = getString(R.string.attempt_connection)) {
|
||||||
val notification = notificationService.createNotification(
|
val notification =
|
||||||
channelId = getString(R.string.vpn_channel_id),
|
notificationService.createNotification(
|
||||||
channelName = getString(R.string.vpn_channel_name),
|
channelId = getString(R.string.vpn_channel_id),
|
||||||
title = getString(R.string.tunnel_start_title),
|
channelName = getString(R.string.vpn_channel_name),
|
||||||
onGoing = false,
|
title = title,
|
||||||
showTimestamp = true,
|
onGoing = false,
|
||||||
description = "${getString(R.string.tunnel_start_text)} $tunnelName"
|
vibration = false,
|
||||||
|
showTimestamp = true,
|
||||||
|
description = description
|
||||||
|
)
|
||||||
|
ServiceCompat.startForeground(
|
||||||
|
this,
|
||||||
|
foregroundId,
|
||||||
|
notification,
|
||||||
|
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID
|
||||||
)
|
)
|
||||||
super.startForeground(foregroundId, notification)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun launchVpnStartingNotification() {
|
private fun launchVpnConnectionFailedNotification(message: String) {
|
||||||
val notification = notificationService.createNotification(
|
val notification =
|
||||||
channelId = getString(R.string.vpn_channel_id),
|
notificationService.createNotification(
|
||||||
channelName = getString(R.string.vpn_channel_name),
|
channelId = getString(R.string.vpn_channel_id),
|
||||||
title = getString(R.string.vpn_starting),
|
channelName = getString(R.string.vpn_channel_name),
|
||||||
onGoing = false,
|
action =
|
||||||
showTimestamp = true,
|
PendingIntent.getBroadcast(
|
||||||
description = getString(R.string.attempt_connection)
|
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
|
||||||
)
|
)
|
||||||
super.startForeground(foregroundId, notification)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
|
||||||
showTimestamp = true,
|
|
||||||
description = message
|
|
||||||
)
|
|
||||||
super.startForeground(foregroundId, notification)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun cancelJob() {
|
private fun cancelJob() {
|
||||||
if(this::job.isInitialized) {
|
if (this::job.isInitialized) {
|
||||||
job.cancel()
|
job.cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+78
-59
@@ -14,69 +14,82 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
import kotlinx.coroutines.flow.callbackFlow
|
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, networkCapability : Int) : NetworkService<T> {
|
val context: Context,
|
||||||
|
networkCapability: Int
|
||||||
|
) : 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 = when (Build.VERSION.SDK_INT) {
|
callbackFlow {
|
||||||
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
val networkStatusCallback =
|
||||||
object : ConnectivityManager.NetworkCallback(
|
when (Build.VERSION.SDK_INT) {
|
||||||
FLAG_INCLUDE_LOCATION_INFO
|
in Build.VERSION_CODES.S..Int.MAX_VALUE -> {
|
||||||
) {
|
object : ConnectivityManager.NetworkCallback(
|
||||||
override fun onAvailable(network: Network) {
|
FLAG_INCLUDE_LOCATION_INFO
|
||||||
trySend(NetworkStatus.Available(network))
|
) {
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
trySend(NetworkStatus.Unavailable(network))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCapabilitiesChanged(
|
||||||
|
network: Network,
|
||||||
|
networkCapabilities: NetworkCapabilities
|
||||||
|
) {
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
else -> {
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
object : ConnectivityManager.NetworkCallback() {
|
||||||
}
|
override fun onAvailable(network: Network) {
|
||||||
|
trySend(NetworkStatus.Available(network))
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(
|
override fun onLost(network: Network) {
|
||||||
network: Network,
|
trySend(NetworkStatus.Unavailable(network))
|
||||||
networkCapabilities: NetworkCapabilities
|
}
|
||||||
) {
|
|
||||||
trySend(NetworkStatus.CapabilitiesChanged(network, networkCapabilities))
|
override fun onCapabilitiesChanged(
|
||||||
|
network: Network,
|
||||||
|
networkCapabilities: NetworkCapabilities
|
||||||
|
) {
|
||||||
|
trySend(
|
||||||
|
NetworkStatus.CapabilitiesChanged(
|
||||||
|
network,
|
||||||
|
networkCapabilities
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
val request =
|
||||||
|
NetworkRequest.Builder()
|
||||||
|
.addTransportType(networkCapability)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||||
|
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
.build()
|
||||||
|
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
||||||
|
|
||||||
else -> {
|
awaitClose {
|
||||||
object : ConnectivityManager.NetworkCallback() {
|
connectivityManager.unregisterNetworkCallback(networkStatusCallback)
|
||||||
|
|
||||||
override fun onAvailable(network: Network) {
|
|
||||||
trySend(NetworkStatus.Available(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLost(network: Network) {
|
|
||||||
trySend(NetworkStatus.Unavailable(network))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCapabilitiesChanged(
|
|
||||||
network: Network,
|
|
||||||
networkCapabilities: NetworkCapabilities
|
|
||||||
) {
|
|
||||||
trySend(NetworkStatus.CapabilitiesChanged(network, networkCapabilities))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val request = NetworkRequest.Builder()
|
|
||||||
.addTransportType(networkCapability)
|
|
||||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
||||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
||||||
.build()
|
|
||||||
connectivityManager.registerNetworkCallback(request, networkStatusCallback)
|
|
||||||
|
|
||||||
awaitClose {
|
|
||||||
connectivityManager.unregisterNetworkCallback(networkStatusCallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
override fun getNetworkName(networkCapabilities: NetworkCapabilities): String? {
|
||||||
var ssid: String? = getWifiNameFromCapabilities(networkCapabilities)
|
var ssid: String? = getWifiNameFromCapabilities(networkCapabilities)
|
||||||
@@ -89,7 +102,6 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(val context: Contex
|
|||||||
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) {
|
||||||
@@ -105,13 +117,20 @@ abstract class BaseNetworkService<T : BaseNetworkService<T>>(val context: Contex
|
|||||||
}
|
}
|
||||||
|
|
||||||
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: suspend (network : Network, networkCapabilities : NetworkCapabilities) -> Result,
|
crossinline onCapabilitiesChanged: suspend (
|
||||||
): Flow<Result> = map { status ->
|
network: Network,
|
||||||
when (status) {
|
networkCapabilities: NetworkCapabilities
|
||||||
is NetworkStatus.Unavailable -> onUnavailable(status.network)
|
) -> Result
|
||||||
is NetworkStatus.Available -> onAvailable(status.network)
|
): Flow<Result> =
|
||||||
is NetworkStatus.CapabilitiesChanged -> onCapabilitiesChanged(status.network, status.networkCapabilities)
|
map { status ->
|
||||||
|
when (status) {
|
||||||
|
is NetworkStatus.Unavailable -> onUnavailable(status.network)
|
||||||
|
is NetworkStatus.Available -> onAvailable(status.network)
|
||||||
|
is NetworkStatus.CapabilitiesChanged -> onCapabilitiesChanged(
|
||||||
|
status.network,
|
||||||
|
status.networkCapabilities
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
+6
-3
@@ -5,6 +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)
|
||||||
|
|||||||
+6
-3
@@ -5,6 +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)
|
||||||
|
|||||||
+3
-3
@@ -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>
|
||||||
|
}
|
||||||
|
|||||||
+6
-3
@@ -4,7 +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 CapabilitiesChanged(val network : Network, val networkCapabilities : NetworkCapabilities) : NetworkStatus()
|
class Unavailable(val network: Network) : NetworkStatus()
|
||||||
|
|
||||||
|
class CapabilitiesChanged(val network: Network, val networkCapabilities: NetworkCapabilities) :
|
||||||
|
NetworkStatus()
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -5,6 +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)
|
||||||
|
|||||||
+6
-4
@@ -3,6 +3,7 @@ package com.zaneschepke.wireguardautotunnel.service.notification
|
|||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
|
import androidx.core.app.NotificationCompat
|
||||||
|
|
||||||
interface NotificationService {
|
interface NotificationService {
|
||||||
fun createNotification(
|
fun createNotification(
|
||||||
@@ -12,10 +13,11 @@ interface NotificationService {
|
|||||||
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 = true,
|
vibration: Boolean = false,
|
||||||
onGoing: Boolean = true,
|
onGoing: Boolean = true,
|
||||||
lights: Boolean = true
|
lights: Boolean = true,
|
||||||
|
onlyAlertOnce: Boolean = true,
|
||||||
): Notification
|
): Notification
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-29
@@ -7,14 +7,29 @@ import android.app.PendingIntent
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
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.MainActivity
|
||||||
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) : NotificationService {
|
class WireGuardNotification
|
||||||
|
@Inject
|
||||||
|
constructor(
|
||||||
|
@ApplicationContext private val context: Context
|
||||||
|
) : NotificationService {
|
||||||
|
private val notificationManager =
|
||||||
|
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
|
||||||
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
|
private val watcherBuilder: NotificationCompat.Builder =
|
||||||
|
NotificationCompat.Builder(
|
||||||
|
context,
|
||||||
|
context.getString(R.string.watcher_channel_id)
|
||||||
|
)
|
||||||
|
private val tunnelBuilder: NotificationCompat.Builder = NotificationCompat.Builder(
|
||||||
|
context,
|
||||||
|
context.getString(R.string.vpn_channel_id)
|
||||||
|
)
|
||||||
|
|
||||||
override fun createNotification(
|
override fun createNotification(
|
||||||
channelId: String,
|
channelId: String,
|
||||||
@@ -27,20 +42,22 @@ class WireGuardNotification @Inject constructor(@ApplicationContext private val
|
|||||||
importance: Int,
|
importance: Int,
|
||||||
vibration: Boolean,
|
vibration: Boolean,
|
||||||
onGoing: Boolean,
|
onGoing: Boolean,
|
||||||
lights: Boolean
|
lights: Boolean,
|
||||||
|
onlyAlertOnce: Boolean,
|
||||||
): Notification {
|
): Notification {
|
||||||
val channel = NotificationChannel(
|
val channel =
|
||||||
channelId,
|
NotificationChannel(
|
||||||
channelName,
|
channelId,
|
||||||
importance
|
channelName,
|
||||||
).let {
|
importance
|
||||||
it.description = title
|
).let {
|
||||||
it.enableLights(lights)
|
it.description = title
|
||||||
it.lightColor = Color.RED
|
it.enableLights(lights)
|
||||||
it.enableVibration(vibration)
|
it.lightColor = Color.RED
|
||||||
it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
|
it.enableVibration(vibration)
|
||||||
it
|
it.vibrationPattern = longArrayOf(100,200,300)
|
||||||
}
|
it
|
||||||
|
}
|
||||||
notificationManager.createNotificationChannel(channel)
|
notificationManager.createNotificationChannel(channel)
|
||||||
val pendingIntent: PendingIntent =
|
val pendingIntent: PendingIntent =
|
||||||
Intent(context, MainActivity::class.java).let { notificationIntent ->
|
Intent(context, MainActivity::class.java).let { notificationIntent ->
|
||||||
@@ -52,26 +69,34 @@ class WireGuardNotification @Inject constructor(@ApplicationContext private val
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val builder: Notification.Builder =
|
val builder = when(channelId) {
|
||||||
Notification.Builder(
|
context.getString(R.string.watcher_channel_id) -> watcherBuilder
|
||||||
context,
|
context.getString(R.string.vpn_channel_id) -> tunnelBuilder
|
||||||
channelId
|
else -> {
|
||||||
)
|
NotificationCompat.Builder(
|
||||||
return builder.let {
|
context,
|
||||||
if(action != null && actionText != null) {
|
channelId
|
||||||
//TODO find a not deprecated way to do this
|
)
|
||||||
it.addAction(
|
|
||||||
Notification.Action.Builder(0, actionText, action)
|
|
||||||
.build())
|
|
||||||
it.setAutoCancel(true)
|
|
||||||
}
|
}
|
||||||
it.setContentTitle(title)
|
}
|
||||||
|
|
||||||
|
return builder.let {
|
||||||
|
if (action != null && actionText != null) {
|
||||||
|
it.addAction(
|
||||||
|
NotificationCompat.Action.Builder(0, actionText, action)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
it.setAutoCancel(true)
|
||||||
|
}
|
||||||
|
it.setContentTitle(title)
|
||||||
.setContentText(description)
|
.setContentText(description)
|
||||||
|
.setOnlyAlertOnce(onlyAlertOnce)
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(pendingIntent)
|
||||||
.setOngoing(onGoing)
|
.setOngoing(onGoing)
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
.setShowWhen(showTimestamp)
|
.setShowWhen(showTimestamp)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher_foreground)
|
.setSmallIcon(R.mipmap.ic_launcher_foreground)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-28
@@ -1,52 +1,84 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import android.view.View
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import androidx.activity.ComponentActivity
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
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.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class ShortcutsActivity : AppCompatActivity() {
|
class ShortcutsActivity : ComponentActivity() {
|
||||||
|
@Inject
|
||||||
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsRepo : SettingsDoa
|
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
||||||
|
|
||||||
private val scope = CoroutineScope(Dispatchers.Main);
|
private suspend fun toggleWatcherServicePause() {
|
||||||
|
val settings = settingsRepository.getSettings()
|
||||||
private fun attemptWatcherServiceToggle(tunnelConfig : String) {
|
if (settings.isAutoTunnelEnabled) {
|
||||||
scope.launch {
|
val pauseAutoTunnel = !settings.isAutoTunnelPaused
|
||||||
val settings = settingsRepo.getAll()
|
settingsRepository.save(settings.copy(
|
||||||
if (settings.isNotEmpty()) {
|
isAutoTunnelPaused = pauseAutoTunnel
|
||||||
val setting = settings.first()
|
))
|
||||||
if(setting.isAutoTunnelEnabled) {
|
|
||||||
ServiceManager.toggleWatcherServiceForeground(this@ShortcutsActivity, tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
if(intent.getStringExtra(ShortcutsManager.CLASS_NAME_EXTRA_KEY)
|
setContentView(View(this))
|
||||||
.equals(WireGuardTunnelService::class.java.name)) {
|
if (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)
|
||||||
|
.equals(WireGuardTunnelService::class.java.simpleName)
|
||||||
intent.getStringExtra(getString(R.string.tunnel_extras_key))?.let {
|
) {
|
||||||
attemptWatcherServiceToggle(it)
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
}
|
val settings = settingsRepository.getSettings()
|
||||||
when(intent.action){
|
if (settings.isShortcutsEnabled) {
|
||||||
Action.STOP.name -> ServiceManager.stopVpnService(this)
|
try {
|
||||||
Action.START.name -> intent.getStringExtra(getString(R.string.tunnel_extras_key))
|
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
|
||||||
?.let { ServiceManager.startVpnService(this, it) }
|
val tunnelConfig =
|
||||||
|
if (tunnelName != null) {
|
||||||
|
tunnelConfigRepository.getAll().firstOrNull { it.name == tunnelName }
|
||||||
|
} else {
|
||||||
|
if (settings.defaultTunnel == null) {
|
||||||
|
tunnelConfigRepository.getAll().first()
|
||||||
|
} else {
|
||||||
|
TunnelConfig.from(settings.defaultTunnel!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tunnelConfig ?: return@launch
|
||||||
|
toggleWatcherServicePause()
|
||||||
|
when (intent.action) {
|
||||||
|
Action.STOP.name -> ServiceManager.stopVpnService(
|
||||||
|
this@ShortcutsActivity
|
||||||
|
)
|
||||||
|
Action.START.name -> ServiceManager.startVpnService(
|
||||||
|
this@ShortcutsActivity,
|
||||||
|
tunnelConfig.toString()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e.message)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
companion object {
|
||||||
|
const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
|
||||||
|
const val CLASS_NAME_EXTRA_KEY = "className"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
-75
@@ -1,75 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.shortcut
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import androidx.core.content.pm.ShortcutInfoCompat
|
|
||||||
import androidx.core.content.pm.ShortcutManagerCompat
|
|
||||||
import androidx.core.graphics.drawable.IconCompat
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.Action
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
|
|
||||||
object ShortcutsManager {
|
|
||||||
|
|
||||||
private const val SHORT_LABEL_MAX_SIZE = 10;
|
|
||||||
private const val LONG_LABEL_MAX_SIZE = 25;
|
|
||||||
private const val APPEND_ON = " On";
|
|
||||||
private const val APPEND_OFF = " Off"
|
|
||||||
const val CLASS_NAME_EXTRA_KEY = "className"
|
|
||||||
|
|
||||||
private fun createAndPushShortcut(context : Context, intent : Intent, id : String, shortLabel : String,
|
|
||||||
longLabel : String, drawable : Int ) {
|
|
||||||
val shortcut = ShortcutInfoCompat.Builder(context, id)
|
|
||||||
.setShortLabel(shortLabel)
|
|
||||||
.setLongLabel(longLabel)
|
|
||||||
.setIcon(IconCompat.createWithResource(context, drawable))
|
|
||||||
.setIntent(intent)
|
|
||||||
.build()
|
|
||||||
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createTunnelShortcuts(context : Context, tunnelConfig : TunnelConfig) {
|
|
||||||
createAndPushShortcut(context,
|
|
||||||
createTunnelOnIntent(context, mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig.toString())),
|
|
||||||
tunnelConfig.id.toString() + APPEND_ON,
|
|
||||||
tunnelConfig.name.take((SHORT_LABEL_MAX_SIZE - APPEND_ON.length)) + APPEND_ON,
|
|
||||||
tunnelConfig.name.take((LONG_LABEL_MAX_SIZE - APPEND_ON.length)) + APPEND_ON,
|
|
||||||
R.drawable.vpn_on
|
|
||||||
)
|
|
||||||
createAndPushShortcut(context,
|
|
||||||
createTunnelOffIntent(context, mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig.toString())),
|
|
||||||
tunnelConfig.id.toString() + APPEND_OFF,
|
|
||||||
tunnelConfig.name.take((SHORT_LABEL_MAX_SIZE - APPEND_OFF.length)) + APPEND_OFF,
|
|
||||||
tunnelConfig.name.take((LONG_LABEL_MAX_SIZE - APPEND_OFF.length)) + APPEND_OFF,
|
|
||||||
R.drawable.vpn_off
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeTunnelShortcuts(context : Context, tunnelConfig : TunnelConfig?) {
|
|
||||||
if(tunnelConfig != null) {
|
|
||||||
ShortcutManagerCompat.removeDynamicShortcuts(context, listOf(tunnelConfig.id.toString() + APPEND_ON,
|
|
||||||
tunnelConfig.id.toString() + APPEND_OFF ))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createTunnelOnIntent(context: Context, extras : Map<String,String>) : Intent {
|
|
||||||
return Intent(context, ShortcutsActivity::class.java).also {
|
|
||||||
it.action = Action.START.name
|
|
||||||
it.putExtra(CLASS_NAME_EXTRA_KEY, WireGuardTunnelService::class.java.name)
|
|
||||||
extras.forEach {(k, v) ->
|
|
||||||
it.putExtra(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createTunnelOffIntent(context : Context, extras : Map<String,String>) : Intent {
|
|
||||||
return Intent(context, ShortcutsActivity::class.java).also {
|
|
||||||
it.action = Action.STOP.name
|
|
||||||
it.putExtra(CLASS_NAME_EXTRA_KEY, WireGuardTunnelService::class.java.name)
|
|
||||||
extras.forEach {(k, v) ->
|
|
||||||
it.putExtra(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+68
-84
@@ -4,55 +4,68 @@ 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 com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
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.VpnService
|
||||||
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.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.cancel
|
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
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class TunnelControlTile : TileService() {
|
class TunnelControlTile() : TileService() {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsRepo : SettingsDoa
|
lateinit var tunnelConfigRepository: TunnelConfigRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var configRepo : TunnelConfigDao
|
lateinit var settingsRepository: SettingsRepository
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var vpnService : VpnService
|
lateinit var vpnService: VpnService
|
||||||
|
|
||||||
private val scope = CoroutineScope(Dispatchers.Main);
|
private val scope = CoroutineScope(Dispatchers.IO)
|
||||||
|
|
||||||
private lateinit var job : Job
|
private var tunnelName : String? = null
|
||||||
|
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
job = scope.launch {
|
|
||||||
updateTileState()
|
|
||||||
}
|
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
}
|
Timber.d("On start listening called")
|
||||||
|
|
||||||
override fun onTileAdded() {
|
|
||||||
super.onTileAdded()
|
|
||||||
qsTile.contentDescription = this.resources.getString(R.string.toggle_vpn)
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
updateTileState();
|
vpnService.vpnState.collect {
|
||||||
|
when(it.status) {
|
||||||
|
Tunnel.State.UP -> setActive()
|
||||||
|
Tunnel.State.DOWN -> setInactive()
|
||||||
|
else -> setInactive()
|
||||||
|
}
|
||||||
|
val tunnels = tunnelConfigRepository.getAll()
|
||||||
|
if(tunnels.isEmpty()) {
|
||||||
|
setUnavailable()
|
||||||
|
return@collect
|
||||||
|
}
|
||||||
|
tunnelName = it.name.ifBlank {
|
||||||
|
val settings = settingsRepository.getSettings()
|
||||||
|
if (settings.defaultTunnel != null) {
|
||||||
|
TunnelConfig.from(settings.defaultTunnel!!).name
|
||||||
|
} else tunnels.firstOrNull()?.name
|
||||||
|
}
|
||||||
|
setTileDescription(tunnelName ?: "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
scope.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onTileRemoved() {
|
override fun onTileRemoved() {
|
||||||
super.onTileRemoved()
|
super.onTileRemoved()
|
||||||
cancelJob()
|
scope.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
@@ -60,16 +73,17 @@ class TunnelControlTile : TileService() {
|
|||||||
unlockAndRun {
|
unlockAndRun {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
val tunnel = determineTileTunnel();
|
val tunnelConfig = tunnelConfigRepository.getAll().first { it.name == tunnelName }
|
||||||
if(tunnel != null) {
|
toggleWatcherServicePause()
|
||||||
attemptWatcherServiceToggle(tunnel.toString())
|
if (vpnService.getState() == Tunnel.State.UP) {
|
||||||
if(vpnService.getState() == Tunnel.State.UP) {
|
ServiceManager.stopVpnService(this@TunnelControlTile)
|
||||||
ServiceManager.stopVpnService(this@TunnelControlTile)
|
} else {
|
||||||
} else {
|
ServiceManager.startVpnServiceForeground(
|
||||||
ServiceManager.startVpnServiceForeground(this@TunnelControlTile, tunnel.toString())
|
this@TunnelControlTile,
|
||||||
}
|
tunnelConfig.toString()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch (e : Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e.message)
|
Timber.e(e.message)
|
||||||
} finally {
|
} finally {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -78,70 +92,40 @@ class TunnelControlTile : TileService() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun determineTileTunnel() : TunnelConfig? {
|
private fun toggleWatcherServicePause() {
|
||||||
var tunnelConfig : TunnelConfig? = null;
|
|
||||||
val settings = settingsRepo.getAll()
|
|
||||||
if (settings.isNotEmpty()) {
|
|
||||||
val setting = settings.first()
|
|
||||||
tunnelConfig = if (setting.defaultTunnel != null) {
|
|
||||||
TunnelConfig.from(setting.defaultTunnel!!);
|
|
||||||
} else {
|
|
||||||
val configs = configRepo.getAll();
|
|
||||||
val config = if(configs.isNotEmpty()) {
|
|
||||||
configs.first();
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
config
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tunnelConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun attemptWatcherServiceToggle(tunnelConfig : String) {
|
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val settings = settingsRepo.getAll()
|
val settings = settingsRepository.getSettings()
|
||||||
if (settings.isNotEmpty()) {
|
if (settings.isAutoTunnelEnabled) {
|
||||||
val setting = settings.first()
|
val pauseAutoTunnel = !settings.isAutoTunnelPaused
|
||||||
if(setting.isAutoTunnelEnabled) {
|
settingsRepository.save(settings.copy(
|
||||||
ServiceManager.toggleWatcherServiceForeground(this@TunnelControlTile, tunnelConfig)
|
isAutoTunnelPaused = pauseAutoTunnel
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateTileState() {
|
private fun setActive() {
|
||||||
vpnService.state.collect {
|
qsTile.state = Tile.STATE_ACTIVE
|
||||||
when(it) {
|
qsTile.updateTile()
|
||||||
Tunnel.State.UP -> {
|
|
||||||
qsTile.state = Tile.STATE_ACTIVE
|
|
||||||
}
|
|
||||||
Tunnel.State.DOWN -> {
|
|
||||||
qsTile.state = Tile.STATE_INACTIVE;
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
qsTile.state = Tile.STATE_UNAVAILABLE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val config = determineTileTunnel();
|
|
||||||
setTileDescription(config?.name ?: this.resources.getString(R.string.no_tunnel_available))
|
|
||||||
qsTile.updateTile()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setTileDescription(description : String) {
|
private fun setInactive() {
|
||||||
|
qsTile.state = Tile.STATE_INACTIVE
|
||||||
|
qsTile.updateTile()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setUnavailable() {
|
||||||
|
qsTile.state = Tile.STATE_UNAVAILABLE
|
||||||
|
qsTile.updateTile()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setTileDescription(description: String) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
qsTile.subtitle = description
|
qsTile.subtitle = description
|
||||||
}
|
}
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
qsTile.stateDescription = description;
|
qsTile.stateDescription = description
|
||||||
}
|
}
|
||||||
|
qsTile.updateTile()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private fun cancelJob() {
|
|
||||||
if(this::job.isInitialized) {
|
|
||||||
job.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+8
-6
@@ -2,13 +2,15 @@ package com.zaneschepke.wireguardautotunnel.service.tunnel
|
|||||||
|
|
||||||
enum class HandshakeStatus {
|
enum class HandshakeStatus {
|
||||||
HEALTHY,
|
HEALTHY,
|
||||||
UNHEALTHY,
|
STALE,
|
||||||
NEVER_CONNECTED,
|
UNKNOWN,
|
||||||
NOT_STARTED;
|
NOT_STARTED
|
||||||
|
;
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 120
|
private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 180
|
||||||
const val UNHEALTHY_TIME_LIMIT_SEC = WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC + 60
|
const val STATUS_CHANGE_TIME_BUFFER = 30
|
||||||
|
const val STALE_TIME_LIMIT_SEC = 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
import com.wireguard.android.backend.Statistics
|
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.wireguard.crypto.Key
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
|
||||||
|
|
||||||
interface VpnService : Tunnel {
|
interface VpnService : Tunnel {
|
||||||
suspend fun startTunnel(tunnelConfig : TunnelConfig) : Tunnel.State
|
suspend fun startTunnel(tunnelConfig: TunnelConfig): Tunnel.State
|
||||||
|
|
||||||
suspend fun stopTunnel()
|
suspend fun stopTunnel()
|
||||||
val state : SharedFlow<Tunnel.State>
|
|
||||||
val tunnelName : SharedFlow<String>
|
val vpnState: StateFlow<VpnState>
|
||||||
val statistics : SharedFlow<Statistics>
|
|
||||||
val lastHandshake : SharedFlow<Map<Key,Long>>
|
fun getState(): Tunnel.State
|
||||||
val handshakeStatus : SharedFlow<HandshakeStatus>
|
}
|
||||||
fun getState() : Tunnel.State
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.service.tunnel
|
||||||
|
|
||||||
|
import com.wireguard.android.backend.Statistics
|
||||||
|
import com.wireguard.android.backend.Tunnel
|
||||||
|
|
||||||
|
data class VpnState(
|
||||||
|
val status : Tunnel.State = Tunnel.State.DOWN,
|
||||||
|
val name : String = "",
|
||||||
|
val statistics : Statistics? = null
|
||||||
|
)
|
||||||
+103
-85
@@ -3,129 +3,147 @@ 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.BackendException
|
||||||
import com.wireguard.android.backend.Statistics
|
import com.wireguard.android.backend.Statistics
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel.State
|
||||||
import com.wireguard.crypto.Key
|
import com.wireguard.config.Config
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Kernel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.module.Userspace
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.channels.BufferOverflow
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class WireGuardTunnel
|
||||||
class WireGuardTunnel @Inject constructor(private val backend : Backend,
|
@Inject
|
||||||
|
constructor(
|
||||||
|
@Userspace private val userspaceBackend: Backend,
|
||||||
|
@Kernel private val kernelBackend: Backend,
|
||||||
|
private val settingsRepository: SettingsRepository
|
||||||
) : VpnService {
|
) : VpnService {
|
||||||
|
private val _vpnState = MutableStateFlow(VpnState())
|
||||||
|
override val vpnState: StateFlow<VpnState> = _vpnState.asStateFlow()
|
||||||
|
|
||||||
private val _tunnelName = MutableStateFlow("")
|
private val scope = CoroutineScope(Dispatchers.IO)
|
||||||
override val tunnelName get() = _tunnelName.asStateFlow()
|
|
||||||
|
|
||||||
private val _state = MutableSharedFlow<Tunnel.State>(
|
private lateinit var statsJob: Job
|
||||||
onBufferOverflow = BufferOverflow.DROP_OLDEST,
|
|
||||||
replay = 1)
|
|
||||||
|
|
||||||
private val _handshakeStatus = MutableSharedFlow<HandshakeStatus>(replay = 1,
|
private var config: Config? = null
|
||||||
onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
|
||||||
override val state get() = _state.asSharedFlow()
|
|
||||||
|
|
||||||
private val _statistics = MutableSharedFlow<Statistics>(replay = 1)
|
private var backend: Backend = userspaceBackend
|
||||||
override val statistics get() = _statistics.asSharedFlow()
|
|
||||||
|
|
||||||
private val _lastHandshake = MutableSharedFlow<Map<Key, Long>>(replay = 1)
|
private var backendIsUserspace = true
|
||||||
override val lastHandshake get() = _lastHandshake.asSharedFlow()
|
|
||||||
|
|
||||||
override val handshakeStatus: SharedFlow<HandshakeStatus>
|
init {
|
||||||
get() = _handshakeStatus.asSharedFlow()
|
scope.launch {
|
||||||
|
settingsRepository.getSettingsFlow().collect {
|
||||||
private lateinit var statsJob : Job
|
if (it.isKernelEnabled && backendIsUserspace) {
|
||||||
|
Timber.d("Setting kernel backend")
|
||||||
|
backend = kernelBackend
|
||||||
override suspend fun startTunnel(tunnelConfig: TunnelConfig) : Tunnel.State{
|
backendIsUserspace = false
|
||||||
return try {
|
} else if (!it.isKernelEnabled && !backendIsUserspace) {
|
||||||
if(getState() == Tunnel.State.UP && _tunnelName.value != tunnelConfig.name) {
|
Timber.d("Setting userspace backend")
|
||||||
stopTunnel()
|
backend = userspaceBackend
|
||||||
|
backendIsUserspace = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_tunnelName.emit(tunnelConfig.name)
|
}
|
||||||
val config = TunnelConfig.configFromQuick(tunnelConfig.wgQuick)
|
}
|
||||||
val state = backend.setState(
|
|
||||||
this, Tunnel.State.UP, config)
|
override suspend fun startTunnel(tunnelConfig: TunnelConfig): State {
|
||||||
_state.emit(state)
|
return try {
|
||||||
state;
|
stopTunnelOnConfigChange(tunnelConfig)
|
||||||
} catch (e : Exception) {
|
emitTunnelName(tunnelConfig.name)
|
||||||
|
config = TunnelConfig.configFromQuick(tunnelConfig.wgQuick)
|
||||||
|
val state =
|
||||||
|
backend.setState(
|
||||||
|
this,
|
||||||
|
State.UP,
|
||||||
|
config
|
||||||
|
)
|
||||||
|
emitTunnelState(state)
|
||||||
|
state
|
||||||
|
} catch (e: Exception) {
|
||||||
Timber.e("Failed to start tunnel with error: ${e.message}")
|
Timber.e("Failed to start tunnel with error: ${e.message}")
|
||||||
Tunnel.State.DOWN
|
State.DOWN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun emitTunnelState(state: State) {
|
||||||
|
_vpnState.tryEmit(
|
||||||
|
_vpnState.value.copy(
|
||||||
|
status = state
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun emitBackendStatistics(statistics: Statistics) {
|
||||||
|
_vpnState.tryEmit(
|
||||||
|
_vpnState.value.copy(
|
||||||
|
statistics = statistics
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun emitTunnelName(name: String) {
|
||||||
|
_vpnState.emit(
|
||||||
|
_vpnState.value.copy(
|
||||||
|
name = name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun stopTunnelOnConfigChange(tunnelConfig: TunnelConfig) {
|
||||||
|
if (getState() == State.UP && _vpnState.value.name != tunnelConfig.name) {
|
||||||
|
stopTunnel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getName(): String {
|
override fun getName(): String {
|
||||||
return _tunnelName.value
|
return _vpnState.value.name
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun stopTunnel() {
|
override suspend fun stopTunnel() {
|
||||||
try {
|
try {
|
||||||
if(getState() == Tunnel.State.UP) {
|
if (getState() == State.UP) {
|
||||||
val state = backend.setState(this, Tunnel.State.DOWN, null)
|
val state = backend.setState(this, State.DOWN, null)
|
||||||
_state.emit(state)
|
emitTunnelState(state)
|
||||||
}
|
}
|
||||||
} catch (e : BackendException) {
|
} catch (e: BackendException) {
|
||||||
Timber.e("Failed to stop tunnel with error: ${e.message}")
|
Timber.e("Failed to stop tunnel with error: ${e.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getState(): Tunnel.State {
|
override fun getState(): State {
|
||||||
return backend.getState(this)
|
return backend.getState(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStateChange(state : Tunnel.State) {
|
override fun onStateChange(state: State) {
|
||||||
val tunnel = this;
|
val tunnel = this
|
||||||
_state.tryEmit(state)
|
emitTunnelState(state)
|
||||||
if(state == Tunnel.State.UP) {
|
WireGuardAutoTunnel.requestTileServiceStateUpdate()
|
||||||
statsJob = CoroutineScope(Dispatchers.IO).launch {
|
if (state == State.UP) {
|
||||||
val handshakeMap = HashMap<Key, Long>()
|
statsJob =
|
||||||
var neverHadHandshakeCounter = 0
|
scope.launch {
|
||||||
while (true) {
|
while (true) {
|
||||||
val statistics = backend.getStatistics(tunnel)
|
val statistics = backend.getStatistics(tunnel)
|
||||||
_statistics.emit(statistics)
|
emitBackendStatistics(statistics)
|
||||||
statistics.peers().forEach {
|
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
|
||||||
val handshakeEpoch = statistics.peer(it)?.latestHandshakeEpochMillis ?: 0L
|
|
||||||
handshakeMap[it] = handshakeEpoch
|
|
||||||
if(handshakeEpoch == 0L) {
|
|
||||||
if(neverHadHandshakeCounter >= HandshakeStatus.NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC) {
|
|
||||||
_handshakeStatus.emit(HandshakeStatus.NEVER_CONNECTED)
|
|
||||||
} else {
|
|
||||||
_handshakeStatus.emit(HandshakeStatus.NOT_STARTED)
|
|
||||||
}
|
|
||||||
if(neverHadHandshakeCounter <= HandshakeStatus.NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC) {
|
|
||||||
neverHadHandshakeCounter += 10
|
|
||||||
}
|
|
||||||
return@forEach
|
|
||||||
}
|
|
||||||
if(NumberUtils.getSecondsBetweenTimestampAndNow(handshakeEpoch) >= HandshakeStatus.UNHEALTHY_TIME_LIMIT_SEC) {
|
|
||||||
_handshakeStatus.emit(HandshakeStatus.UNHEALTHY)
|
|
||||||
} else {
|
|
||||||
_handshakeStatus.emit(HandshakeStatus.HEALTHY)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_lastHandshake.emit(handshakeMap)
|
|
||||||
delay(Constants.VPN_STATISTIC_CHECK_INTERVAL)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(state == Tunnel.State.DOWN) {
|
if (state == State.DOWN) {
|
||||||
if(this::statsJob.isInitialized) {
|
if (this::statsJob.isInitialized) {
|
||||||
statsJob.cancel()
|
statsJob.cancel()
|
||||||
}
|
}
|
||||||
_handshakeStatus.tryEmit(HandshakeStatus.NOT_STARTED)
|
|
||||||
_lastHandshake.tryEmit(emptyMap())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.SettingsDao
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class ActivityViewModel @Inject constructor(
|
||||||
|
private val settingsRepo: SettingsDao,
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,4 +2,4 @@ package com.zaneschepke.wireguardautotunnel.ui
|
|||||||
|
|
||||||
import com.journeyapps.barcodescanner.CaptureActivity
|
import com.journeyapps.barcodescanner.CaptureActivity
|
||||||
|
|
||||||
class CaptureActivityPortrait : CaptureActivity()
|
class CaptureActivityPortrait : CaptureActivity()
|
||||||
|
|||||||
@@ -6,18 +6,19 @@ import android.net.Uri
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.view.KeyEvent
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.animation.ExperimentalAnimationApi
|
import androidx.compose.foundation.focusable
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.animation.fadeIn
|
|
||||||
import androidx.compose.animation.slideInHorizontally
|
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.SnackbarData
|
||||||
|
import androidx.compose.material3.SnackbarDuration
|
||||||
import androidx.compose.material3.SnackbarHost
|
import androidx.compose.material3.SnackbarHost
|
||||||
import androidx.compose.material3.SnackbarHostState
|
import androidx.compose.material3.SnackbarHostState
|
||||||
|
import androidx.compose.material3.SnackbarResult
|
||||||
|
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
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
@@ -25,41 +26,43 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.compose.ui.input.key.onKeyEvent
|
import androidx.compose.ui.focus.focusProperties
|
||||||
import com.google.accompanist.navigation.animation.AnimatedNavHost
|
import androidx.compose.ui.unit.dp
|
||||||
import com.google.accompanist.navigation.animation.composable
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
|
import androidx.navigation.compose.NavHost
|
||||||
|
import androidx.navigation.compose.composable
|
||||||
|
import androidx.navigation.compose.rememberNavController
|
||||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||||
import com.google.accompanist.permissions.isGranted
|
import com.google.accompanist.permissions.isGranted
|
||||||
import com.google.accompanist.permissions.rememberPermissionState
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
import com.wireguard.android.backend.GoBackend
|
import com.wireguard.android.backend.GoBackend
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.PermissionRequestFailedScreen
|
import com.zaneschepke.wireguardautotunnel.ui.common.PermissionRequestFailedScreen
|
||||||
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.screens.config.ConfigScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.config.ConfigScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.detail.DetailScreen
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
|
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.theme.TransparentSystemBars
|
import com.zaneschepke.wireguardautotunnel.ui.theme.TransparentSystemBars
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
|
||||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelException
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import timber.log.Timber
|
import kotlinx.coroutines.Dispatchers
|
||||||
import java.lang.IllegalStateException
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
@OptIn(
|
||||||
@OptIn(ExperimentalAnimationApi::class,
|
|
||||||
ExperimentalPermissionsApi::class
|
ExperimentalPermissionsApi::class
|
||||||
)
|
)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContent {
|
setContent {
|
||||||
val navController = rememberAnimatedNavController()
|
// val activityViewModel = hiltViewModel<ActivityViewModel>()
|
||||||
val focusRequester = remember { FocusRequester() }
|
|
||||||
|
val navController = rememberNavController()
|
||||||
|
val focusRequester = remember { FocusRequester()}
|
||||||
|
|
||||||
WireguardAutoTunnelTheme {
|
WireguardAutoTunnelTheme {
|
||||||
TransparentSystemBars()
|
TransparentSystemBars()
|
||||||
@@ -76,46 +79,62 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var vpnIntent by remember { mutableStateOf(GoBackend.VpnService.prepare(this)) }
|
var vpnIntent by remember { mutableStateOf(GoBackend.VpnService.prepare(this)) }
|
||||||
val vpnActivityResultState = rememberLauncherForActivityResult(
|
val vpnActivityResultState =
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
rememberLauncherForActivityResult(
|
||||||
onResult = {
|
ActivityResultContracts.StartActivityForResult(),
|
||||||
val accepted = (it.resultCode == RESULT_OK)
|
onResult = {
|
||||||
if (accepted) {
|
val accepted = (it.resultCode == RESULT_OK)
|
||||||
vpnIntent = null
|
if (accepted) {
|
||||||
|
vpnIntent = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
LaunchedEffect(vpnIntent) {
|
LaunchedEffect(vpnIntent) {
|
||||||
if (vpnIntent != null) {
|
if (vpnIntent != null) {
|
||||||
vpnActivityResultState.launch(vpnIntent)
|
vpnActivityResultState.launch(vpnIntent)
|
||||||
} else requestNotificationPermission()
|
} else {
|
||||||
|
requestNotificationPermission()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Scaffold(snackbarHost = { SnackbarHost(snackbarHostState)},
|
fun showSnackBarMessage(message: String) {
|
||||||
modifier = Modifier.onKeyEvent {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
if (it.nativeKeyEvent.action == KeyEvent.ACTION_UP) {
|
val result = snackbarHostState.showSnackbar(
|
||||||
when (it.nativeKeyEvent.keyCode) {
|
message = message,
|
||||||
KeyEvent.KEYCODE_DPAD_UP -> {
|
actionLabel = applicationContext.getString(R.string.okay),
|
||||||
try {
|
duration = SnackbarDuration.Short
|
||||||
focusRequester.requestFocus()
|
)
|
||||||
} catch(e : IllegalStateException) {
|
when (result) {
|
||||||
Timber.e("No D-Pad focus request modifier added to element on screen")
|
SnackbarResult.ActionPerformed, SnackbarResult.Dismissed -> {
|
||||||
}
|
snackbarHostState.currentSnackbarData?.dismiss()
|
||||||
false
|
|
||||||
} else -> {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
false
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Scaffold(
|
||||||
|
snackbarHost = {
|
||||||
|
SnackbarHost(snackbarHostState) { snackbarData: SnackbarData ->
|
||||||
|
CustomSnackBar(
|
||||||
|
snackbarData.visuals.message,
|
||||||
|
isRtl = false,
|
||||||
|
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
|
||||||
|
2.dp
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
bottomBar = if (vpnIntent == null && notificationPermissionState.status.isGranted) {
|
modifier = Modifier.focusable().focusProperties { up = focusRequester },
|
||||||
{ BottomNavBar(navController, Routes.navItems) }
|
bottomBar =
|
||||||
|
if (vpnIntent == null && notificationPermissionState.status.isGranted) {
|
||||||
|
{ BottomNavBar(navController, listOf(
|
||||||
|
Screen.Main.navItem,
|
||||||
|
Screen.Settings.navItem,
|
||||||
|
Screen.Support.navItem)) }
|
||||||
} else {
|
} else {
|
||||||
{}
|
{}
|
||||||
},
|
}
|
||||||
)
|
) { padding ->
|
||||||
{ padding ->
|
|
||||||
if (vpnIntent != null) {
|
if (vpnIntent != null) {
|
||||||
PermissionRequestFailedScreen(
|
PermissionRequestFailedScreen(
|
||||||
padding = padding,
|
padding = padding,
|
||||||
@@ -132,7 +151,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val intentSettings =
|
val intentSettings =
|
||||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||||
intentSettings.data =
|
intentSettings.data =
|
||||||
Uri.fromParts(Constants.URI_PACKAGE_SCHEME, this.packageName, null)
|
Uri.fromParts(
|
||||||
|
Constants.URI_PACKAGE_SCHEME,
|
||||||
|
this.packageName,
|
||||||
|
null
|
||||||
|
)
|
||||||
startActivity(intentSettings)
|
startActivity(intentSettings)
|
||||||
},
|
},
|
||||||
message = getString(R.string.notification_permission_required),
|
message = getString(R.string.notification_permission_required),
|
||||||
@@ -140,68 +163,39 @@ class MainActivity : AppCompatActivity() {
|
|||||||
)
|
)
|
||||||
return@Scaffold
|
return@Scaffold
|
||||||
}
|
}
|
||||||
AnimatedNavHost(navController, startDestination = Routes.Main.name) {
|
NavHost(navController, startDestination = Screen.Main.route) {
|
||||||
composable(Routes.Main.name, enterTransition = {
|
composable(
|
||||||
when (initialState.destination.route) {
|
Screen.Main.route,
|
||||||
Routes.Settings.name, Routes.Support.name ->
|
) {
|
||||||
slideInHorizontally(
|
MainScreen(padding = padding, focusRequester = focusRequester, showSnackbarMessage = { message ->
|
||||||
initialOffsetX = { -Constants.SLIDE_IN_TRANSITION_OFFSET },
|
showSnackBarMessage(message)
|
||||||
animationSpec = tween(Constants.SLIDE_IN_ANIMATION_DURATION)
|
}, navController = navController)
|
||||||
)
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
fadeIn(animationSpec = tween(Constants.FADE_IN_ANIMATION_DURATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
MainScreen(padding = padding, snackbarHostState = snackbarHostState, navController = navController)
|
|
||||||
}
|
}
|
||||||
composable(Routes.Settings.name, enterTransition = {
|
composable(Screen.Settings.route,
|
||||||
when (initialState.destination.route) {
|
) {
|
||||||
Routes.Main.name ->
|
SettingsScreen(padding = padding, showSnackbarMessage = { message ->
|
||||||
slideInHorizontally(
|
showSnackBarMessage(message)
|
||||||
initialOffsetX = { Constants.SLIDE_IN_TRANSITION_OFFSET },
|
}, focusRequester = focusRequester)
|
||||||
animationSpec = tween(Constants.SLIDE_IN_ANIMATION_DURATION)
|
}
|
||||||
)
|
composable(Screen.Support.route,
|
||||||
|
) {
|
||||||
Routes.Support.name -> {
|
SupportScreen(padding = padding, focusRequester = focusRequester,
|
||||||
slideInHorizontally(
|
showSnackbarMessage = { message ->
|
||||||
initialOffsetX = { -Constants.SLIDE_IN_TRANSITION_OFFSET },
|
showSnackBarMessage(message)
|
||||||
animationSpec = tween(Constants.SLIDE_IN_ANIMATION_DURATION)
|
})
|
||||||
)
|
}
|
||||||
}
|
composable("${Screen.Config.route}/{id}") {
|
||||||
|
|
||||||
else -> {
|
|
||||||
fadeIn(animationSpec = tween(Constants.FADE_IN_ANIMATION_DURATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) { SettingsScreen(padding = padding, snackbarHostState = snackbarHostState, navController = navController, focusRequester = focusRequester) }
|
|
||||||
composable(Routes.Support.name, enterTransition = {
|
|
||||||
when (initialState.destination.route) {
|
|
||||||
Routes.Settings.name, Routes.Main.name ->
|
|
||||||
slideInHorizontally(
|
|
||||||
initialOffsetX = { Constants.SLIDE_IN_ANIMATION_DURATION },
|
|
||||||
animationSpec = tween(Constants.SLIDE_IN_ANIMATION_DURATION)
|
|
||||||
)
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
fadeIn(animationSpec = tween(Constants.FADE_IN_ANIMATION_DURATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) { SupportScreen(padding = padding, focusRequester) }
|
|
||||||
composable("${Routes.Config.name}/{id}", enterTransition = {
|
|
||||||
fadeIn(animationSpec = tween(Constants.FADE_IN_ANIMATION_DURATION))
|
|
||||||
}) {
|
|
||||||
val id = it.arguments?.getString("id")
|
val id = it.arguments?.getString("id")
|
||||||
if(!id.isNullOrBlank()) {
|
if (!id.isNullOrBlank()) {
|
||||||
ConfigScreen(padding = padding, navController = navController, id = id, focusRequester = focusRequester)}
|
//https://dagger.dev/hilt/view-model#assisted-injection
|
||||||
}
|
ConfigScreen(
|
||||||
composable("${Routes.Detail.name}/{id}", enterTransition = {
|
navController = navController,
|
||||||
fadeIn(animationSpec = tween(Constants.FADE_IN_ANIMATION_DURATION))
|
id = id,
|
||||||
}) {
|
showSnackbarMessage = { message ->
|
||||||
val id = it.arguments?.getString("id")
|
showSnackBarMessage(message)
|
||||||
if(!id.isNullOrBlank()) {
|
},
|
||||||
DetailScreen(padding = padding, id = id)
|
focusRequester = focusRequester
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
|
||||||
|
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.rounded.Home
|
|
||||||
import androidx.compose.material.icons.rounded.QuestionMark
|
|
||||||
import androidx.compose.material.icons.rounded.Settings
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
|
||||||
|
|
||||||
enum class Routes {
|
|
||||||
Main,
|
|
||||||
Settings,
|
|
||||||
Support,
|
|
||||||
Config,
|
|
||||||
Detail;
|
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
val navItems = listOf(
|
|
||||||
BottomNavItem(
|
|
||||||
name = "Tunnels",
|
|
||||||
route = Main.name,
|
|
||||||
icon = Icons.Rounded.Home,
|
|
||||||
),
|
|
||||||
BottomNavItem(
|
|
||||||
name = "Settings",
|
|
||||||
route = Settings.name,
|
|
||||||
icon = Icons.Rounded.Settings,
|
|
||||||
),
|
|
||||||
BottomNavItem(
|
|
||||||
name = "Support",
|
|
||||||
route = Support.name,
|
|
||||||
icon = Icons.Rounded.QuestionMark,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Home
|
||||||
|
import androidx.compose.material.icons.rounded.QuestionMark
|
||||||
|
import androidx.compose.material.icons.rounded.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem
|
||||||
|
|
||||||
|
sealed class Screen(val route : String) {
|
||||||
|
data object Main: Screen("main") {
|
||||||
|
val navItem = BottomNavItem(
|
||||||
|
name = "Tunnels",
|
||||||
|
route = route,
|
||||||
|
icon = Icons.Rounded.Home
|
||||||
|
)
|
||||||
|
}
|
||||||
|
data object Settings: Screen("settings") {
|
||||||
|
val navItem = BottomNavItem(
|
||||||
|
name = "Settings",
|
||||||
|
route = route,
|
||||||
|
icon = Icons.Rounded.Settings
|
||||||
|
)
|
||||||
|
}
|
||||||
|
data object Support: Screen("support") {
|
||||||
|
val navItem = BottomNavItem(
|
||||||
|
name = "Support",
|
||||||
|
route = route,
|
||||||
|
icon = Icons.Rounded.QuestionMark
|
||||||
|
)
|
||||||
|
}
|
||||||
|
data object Config : Screen("config")
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui
|
|
||||||
|
|
||||||
data class ViewState(
|
|
||||||
val showSnackbarMessage : Boolean = false,
|
|
||||||
val snackbarMessage : String = "",
|
|
||||||
val snackbarActionText : String = "",
|
|
||||||
val onSnackbarActionClick : () -> Unit = {},
|
|
||||||
val isLoading : Boolean = false
|
|
||||||
)
|
|
||||||
+18
-8
@@ -3,29 +3,39 @@ package com.zaneschepke.wireguardautotunnel.ui.common
|
|||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.material3.Button
|
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ClickableIconButton(onIconClick : () -> Unit, text : String, icon : ImageVector, enabled : Boolean) {
|
fun ClickableIconButton(
|
||||||
Button(onClick = {},
|
onClick: () -> Unit,
|
||||||
|
onIconClick: () -> Unit,
|
||||||
|
text: String,
|
||||||
|
icon: ImageVector,
|
||||||
|
enabled: Boolean
|
||||||
|
) {
|
||||||
|
TextButton(
|
||||||
|
onClick = onClick,
|
||||||
enabled = enabled
|
enabled = enabled
|
||||||
) {
|
) {
|
||||||
Text(text)
|
Text(text, Modifier.weight(1f, false))
|
||||||
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = icon,
|
imageVector = icon,
|
||||||
contentDescription = "Delete",
|
contentDescription = stringResource(R.string.delete),
|
||||||
modifier = Modifier.size(ButtonDefaults.IconSize).clickable {
|
modifier =
|
||||||
if(enabled) {
|
Modifier.size(ButtonDefaults.IconSize).weight(1f, false).clickable {
|
||||||
|
if (enabled) {
|
||||||
onIconClick()
|
onIconClick()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-5
@@ -16,13 +16,21 @@ import androidx.compose.ui.unit.dp
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PermissionRequestFailedScreen(padding : PaddingValues, onRequestAgain : () -> Unit, message : String, buttonText : String ) {
|
fun PermissionRequestFailedScreen(
|
||||||
|
padding: PaddingValues,
|
||||||
|
onRequestAgain: () -> Unit,
|
||||||
|
message: String,
|
||||||
|
buttonText: String
|
||||||
|
) {
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
Column(horizontalAlignment = Alignment.CenterHorizontally,
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
verticalArrangement = Arrangement.Center,
|
verticalArrangement = Arrangement.Center,
|
||||||
modifier = Modifier
|
modifier =
|
||||||
|
Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(padding)) {
|
.padding(padding)
|
||||||
|
) {
|
||||||
Text(message, textAlign = TextAlign.Center, modifier = Modifier.padding(15.dp))
|
Text(message, textAlign = TextAlign.Center, modifier = Modifier.padding(15.dp))
|
||||||
Button(onClick = {
|
Button(onClick = {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
@@ -32,4 +40,4 @@ fun PermissionRequestFailedScreen(padding : PaddingValues, onRequestAgain : () -
|
|||||||
Text(buttonText)
|
Text(buttonText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,42 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.common
|
package com.zaneschepke.wireguardautotunnel.ui.common
|
||||||
|
|
||||||
|
import androidx.compose.animation.animateContentSize
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.combinedClickable
|
import androidx.compose.foundation.combinedClickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.unit.sp
|
||||||
|
import com.wireguard.android.backend.Statistics
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.toThreeDecimalPlaceString
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun RowListItem(leadingIcon : ImageVector? = null, leadingIconColor : Color = Color.Gray, text : String, onHold : () -> Unit, onClick: () -> Unit, rowButton : @Composable() () -> Unit ) {
|
fun RowListItem(
|
||||||
|
icon: @Composable () -> Unit,
|
||||||
|
text: String,
|
||||||
|
onHold: () -> Unit,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
rowButton: @Composable () -> Unit,
|
||||||
|
expanded: Boolean,
|
||||||
|
statistics: Statistics?
|
||||||
|
) {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.animateContentSize()
|
||||||
|
.clip(RoundedCornerShape(30.dp))
|
||||||
.combinedClickable(
|
.combinedClickable(
|
||||||
onClick = {
|
onClick = {
|
||||||
onClick()
|
onClick()
|
||||||
@@ -31,25 +46,52 @@ fun RowListItem(leadingIcon : ImageVector? = null, leadingIconColor : Color = Co
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Row(
|
Column {
|
||||||
modifier = Modifier
|
Row(
|
||||||
.fillMaxWidth()
|
modifier =
|
||||||
.padding(14.dp),
|
Modifier
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
.fillMaxWidth()
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
.padding(horizontal = 15.dp, vertical = 5.dp),
|
||||||
) {
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
Row(verticalAlignment = Alignment.CenterVertically,) {
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
if(leadingIcon != null) {
|
) {
|
||||||
Icon(
|
Row(
|
||||||
leadingIcon, "status",
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
tint = leadingIconColor,
|
modifier = Modifier.fillMaxWidth(.60f)
|
||||||
modifier = Modifier.padding(end = 10.dp).size(15.dp)
|
) {
|
||||||
)
|
icon()
|
||||||
|
Text(text)
|
||||||
|
}
|
||||||
|
rowButton()
|
||||||
|
}
|
||||||
|
if (expanded) {
|
||||||
|
statistics?.peers()?.forEach {
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(end = 10.dp, bottom = 10.dp, start = 10.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceEvenly
|
||||||
|
) {
|
||||||
|
val handshakeEpoch = statistics.peer(it)!!.latestHandshakeEpochMillis
|
||||||
|
val peerTx = statistics.peer(it)!!.txBytes
|
||||||
|
val peerRx = statistics.peer(it)!!.rxBytes
|
||||||
|
val peerId = it.toBase64().subSequence(0, 3).toString() + "***"
|
||||||
|
val handshakeSec =
|
||||||
|
NumberUtils.getSecondsBetweenTimestampAndNow(handshakeEpoch)
|
||||||
|
val handshake =
|
||||||
|
if (handshakeSec == null) "never" else "$handshakeSec secs ago"
|
||||||
|
val peerTxMB = NumberUtils.bytesToMB(peerTx).toThreeDecimalPlaceString()
|
||||||
|
val peerRxMB = NumberUtils.bytesToMB(peerRx).toThreeDecimalPlaceString()
|
||||||
|
val fontSize = 9.sp
|
||||||
|
Text("peer: $peerId", fontSize = fontSize)
|
||||||
|
Text("handshake: $handshake", fontSize = fontSize)
|
||||||
|
Text("tx: $peerTxMB MB", fontSize = fontSize)
|
||||||
|
Text("rx: $peerRxMB MB", fontSize = fontSize)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Text(text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rowButton()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,7 @@ import androidx.compose.ui.text.input.KeyboardType
|
|||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SearchBar(
|
fun SearchBar(onQuery: (queryString: String) -> Unit) {
|
||||||
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) }
|
||||||
@@ -64,17 +62,19 @@ fun SearchBar(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
maxLines = 1,
|
maxLines = 1,
|
||||||
colors = TextFieldDefaults.colors(
|
colors =
|
||||||
|
TextFieldDefaults.colors(
|
||||||
focusedContainerColor = Color.Transparent,
|
focusedContainerColor = Color.Transparent,
|
||||||
unfocusedContainerColor = Color.Transparent,
|
unfocusedContainerColor = Color.Transparent,
|
||||||
disabledContainerColor = Color.Transparent,
|
disabledContainerColor = Color.Transparent
|
||||||
),
|
),
|
||||||
placeholder = { Text(text = stringResource(R.string.hint_search_packages)) },
|
placeholder = { Text(text = stringResource(R.string.hint_search_packages)) },
|
||||||
textStyle = MaterialTheme.typography.bodySmall,
|
textStyle = MaterialTheme.typography.bodySmall,
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
|
||||||
modifier = Modifier
|
modifier =
|
||||||
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.background(color = MaterialTheme.colorScheme.background, shape = RectangleShape)
|
.background(color = MaterialTheme.colorScheme.background, shape = RectangleShape)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||||
|
|
||||||
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ConfigurationTextBox(
|
||||||
|
value: String,
|
||||||
|
hint: String,
|
||||||
|
onValueChange: (String) -> Unit,
|
||||||
|
keyboardActions: KeyboardActions,
|
||||||
|
label: String,
|
||||||
|
modifier: Modifier
|
||||||
|
) {
|
||||||
|
OutlinedTextField(
|
||||||
|
modifier = modifier,
|
||||||
|
value = value,
|
||||||
|
singleLine = true,
|
||||||
|
onValueChange = {
|
||||||
|
onValueChange(it)
|
||||||
|
},
|
||||||
|
label = { Text(label) },
|
||||||
|
maxLines = 1,
|
||||||
|
placeholder = {
|
||||||
|
Text(hint)
|
||||||
|
},
|
||||||
|
keyboardOptions =
|
||||||
|
KeyboardOptions(
|
||||||
|
capitalization = KeyboardCapitalization.None,
|
||||||
|
imeAction = ImeAction.Done
|
||||||
|
),
|
||||||
|
keyboardActions = keyboardActions
|
||||||
|
)
|
||||||
|
}
|
||||||
+41
@@ -0,0 +1,41 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.config
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.Switch
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ConfigurationToggle(
|
||||||
|
label: String,
|
||||||
|
enabled: Boolean,
|
||||||
|
checked: Boolean,
|
||||||
|
padding: Dp,
|
||||||
|
onCheckChanged: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(padding),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
|
) {
|
||||||
|
Text(label)
|
||||||
|
Switch(
|
||||||
|
modifier = modifier,
|
||||||
|
enabled = enabled,
|
||||||
|
checked = checked,
|
||||||
|
onCheckedChange = {
|
||||||
|
onCheckChanged()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-6
@@ -11,12 +11,14 @@ import androidx.navigation.NavController
|
|||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun BottomNavBar(navController : NavController, bottomNavItems : List<BottomNavItem>) {
|
fun BottomNavBar(
|
||||||
|
navController: NavController,
|
||||||
|
bottomNavItems: List<BottomNavItem>
|
||||||
|
) {
|
||||||
val backStackEntry = navController.currentBackStackEntryAsState()
|
val backStackEntry = navController.currentBackStackEntryAsState()
|
||||||
|
|
||||||
NavigationBar(
|
NavigationBar(
|
||||||
containerColor = MaterialTheme.colorScheme.background,
|
containerColor = MaterialTheme.colorScheme.background
|
||||||
) {
|
) {
|
||||||
bottomNavItems.forEach { item ->
|
bottomNavItems.forEach { item ->
|
||||||
val selected = item.route == backStackEntry.value?.destination?.route
|
val selected = item.route == backStackEntry.value?.destination?.route
|
||||||
@@ -27,16 +29,16 @@ fun BottomNavBar(navController : NavController, bottomNavItems : List<BottomNavI
|
|||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = item.name,
|
text = item.name,
|
||||||
fontWeight = FontWeight.SemiBold,
|
fontWeight = FontWeight.SemiBold
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
icon = {
|
icon = {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = item.icon,
|
imageVector = item.icon,
|
||||||
contentDescription = "${item.name} Icon",
|
contentDescription = "${item.name} Icon"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+97
@@ -0,0 +1,97 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
|
||||||
|
|
||||||
|
import androidx.biometric.BiometricManager
|
||||||
|
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
|
||||||
|
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
|
||||||
|
import androidx.biometric.BiometricPrompt
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AuthorizationPrompt(
|
||||||
|
onSuccess: () -> Unit,
|
||||||
|
onFailure: () -> Unit,
|
||||||
|
onError: (String) -> Unit
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val biometricManager = BiometricManager.from(context)
|
||||||
|
val bio = biometricManager.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
||||||
|
val isBiometricAvailable =
|
||||||
|
remember {
|
||||||
|
when (bio) {
|
||||||
|
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
|
||||||
|
onError("Biometrics not available")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
|
||||||
|
onError("Biometrics not created")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
|
||||||
|
onError("Biometric hardware not found")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
|
||||||
|
onError("Biometric security update required")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
|
||||||
|
onError("Biometrics not supported")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
|
||||||
|
onError("Biometrics status unknown")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
BiometricManager.BIOMETRIC_SUCCESS -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isBiometricAvailable) {
|
||||||
|
val executor = remember { ContextCompat.getMainExecutor(context) }
|
||||||
|
|
||||||
|
val promptInfo =
|
||||||
|
BiometricPrompt.PromptInfo.Builder()
|
||||||
|
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
|
||||||
|
.setTitle("Biometric Authentication")
|
||||||
|
.setSubtitle("Log in using your biometric credential")
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val biometricPrompt =
|
||||||
|
BiometricPrompt(
|
||||||
|
context as FragmentActivity,
|
||||||
|
executor,
|
||||||
|
object : BiometricPrompt.AuthenticationCallback() {
|
||||||
|
override fun onAuthenticationError(
|
||||||
|
errorCode: Int,
|
||||||
|
errString: CharSequence
|
||||||
|
) {
|
||||||
|
super.onAuthenticationError(errorCode, errString)
|
||||||
|
onFailure()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationSucceeded(
|
||||||
|
result: BiometricPrompt.AuthenticationResult
|
||||||
|
) {
|
||||||
|
super.onAuthenticationSucceeded(result)
|
||||||
|
onSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
super.onAuthenticationFailed()
|
||||||
|
onFailure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
biometricPrompt.authenticate(promptInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.Info
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Snackbar
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.LayoutDirection
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun CustomSnackBar(
|
||||||
|
message: String,
|
||||||
|
isRtl: Boolean = true,
|
||||||
|
containerColor: Color = MaterialTheme.colorScheme.surface
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
Snackbar(
|
||||||
|
containerColor = containerColor,
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxWidth(
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) 1 / 3f else 2 / 3f
|
||||||
|
).padding(bottom = 100.dp),
|
||||||
|
shape = RoundedCornerShape(16.dp)
|
||||||
|
) {
|
||||||
|
CompositionLocalProvider(
|
||||||
|
LocalLayoutDirection provides
|
||||||
|
if (isRtl) LayoutDirection.Rtl else LayoutDirection.Ltr
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.width(IntrinsicSize.Max).height(IntrinsicSize.Min),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.Start
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Info,
|
||||||
|
contentDescription = stringResource(R.string.info),
|
||||||
|
tint = Color.White,
|
||||||
|
modifier = Modifier.padding(end = 10.dp)
|
||||||
|
)
|
||||||
|
Text(message, color = Color.White, modifier = Modifier.padding(end = 5.dp))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.screen
|
||||||
|
|
||||||
|
import androidx.compose.foundation.focusable
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun LoadingScreen() {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.fillMaxSize().focusable().padding()) {
|
||||||
|
Column(modifier = Modifier.padding(120.dp)) { CircularProgressIndicator() }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.common.text
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.text.TextStyle
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.unit.sp
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SectionTitle(
|
||||||
|
title: String,
|
||||||
|
padding: Dp
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
title,
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
style = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.ExtraBold),
|
||||||
|
modifier = Modifier.padding(padding, bottom = 5.dp, top = 5.dp)
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.models
|
||||||
|
|
||||||
|
import com.wireguard.config.Interface
|
||||||
|
|
||||||
|
data class InterfaceProxy(
|
||||||
|
var privateKey: String = "",
|
||||||
|
var publicKey: String = "",
|
||||||
|
var addresses: String = "",
|
||||||
|
var dnsServers: String = "",
|
||||||
|
var listenPort: String = "",
|
||||||
|
var mtu: String = ""
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun from(i: Interface): InterfaceProxy {
|
||||||
|
return InterfaceProxy(
|
||||||
|
publicKey = i.keyPair.publicKey.toBase64().trim(),
|
||||||
|
privateKey = i.keyPair.privateKey.toBase64().trim(),
|
||||||
|
addresses = i.addresses.joinToString(", ").trim(),
|
||||||
|
dnsServers = i.dnsServers.joinToString(", ").replace("/", "").trim(),
|
||||||
|
listenPort = if (i.listenPort.isPresent) {
|
||||||
|
i.listenPort.get().toString()
|
||||||
|
.trim()
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
mtu = if (i.mtu.isPresent) i.mtu.get().toString().trim() else ""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.models
|
||||||
|
|
||||||
|
import com.wireguard.config.Peer
|
||||||
|
|
||||||
|
data class PeerProxy(
|
||||||
|
var publicKey: String = "",
|
||||||
|
var preSharedKey: String = "",
|
||||||
|
var persistentKeepalive: String = "",
|
||||||
|
var endpoint: String = "",
|
||||||
|
var allowedIps: String = IPV4_WILDCARD.joinToString(", ").trim()
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun from(peer: Peer): PeerProxy {
|
||||||
|
return PeerProxy(
|
||||||
|
publicKey = peer.publicKey.toBase64(),
|
||||||
|
preSharedKey = if (peer.preSharedKey.isPresent) {
|
||||||
|
peer.preSharedKey.get().toBase64()
|
||||||
|
.trim()
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
persistentKeepalive = if (peer.persistentKeepalive.isPresent) {
|
||||||
|
peer.persistentKeepalive.get()
|
||||||
|
.toString().trim()
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
endpoint = if (peer.endpoint.isPresent) {
|
||||||
|
peer.endpoint.get().toString()
|
||||||
|
.trim()
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
allowedIps = peer.allowedIps.joinToString(", ").trim()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val IPV4_PUBLIC_NETWORKS =
|
||||||
|
setOf(
|
||||||
|
"0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3",
|
||||||
|
"64.0.0.0/2", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/6", "172.0.0.0/12",
|
||||||
|
"172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7",
|
||||||
|
"176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16",
|
||||||
|
"192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10",
|
||||||
|
"193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4"
|
||||||
|
)
|
||||||
|
val IPV4_WILDCARD = setOf("0.0.0.0/0")
|
||||||
|
}
|
||||||
|
}
|
||||||
+481
-173
@@ -1,242 +1,550 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||||
|
|
||||||
import android.widget.Toast
|
import android.annotation.SuppressLint
|
||||||
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.focusGroup
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.rounded.Android
|
import androidx.compose.material.icons.rounded.Android
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material.icons.rounded.ContentCopy
|
||||||
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
|
import androidx.compose.material.icons.rounded.Refresh
|
||||||
|
import androidx.compose.material.icons.rounded.Save
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
import androidx.compose.material3.Checkbox
|
import androidx.compose.material3.Checkbox
|
||||||
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
import androidx.compose.material3.FabPosition
|
||||||
|
import androidx.compose.material3.FloatingActionButton
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Switch
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.compose.ui.focus.focusRequester
|
import androidx.compose.ui.focus.focusRequester
|
||||||
|
import androidx.compose.ui.focus.onFocusChanged
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.platform.ClipboardManager
|
||||||
|
import androidx.compose.ui.platform.LocalClipboardManager
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalFocusManager
|
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.text.AnnotatedString
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||||
|
import androidx.compose.ui.text.input.VisualTransformation
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import com.google.accompanist.drawablepainter.DrawablePainter
|
import com.google.accompanist.drawablepainter.DrawablePainter
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Routes
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
import com.zaneschepke.wireguardautotunnel.ui.common.SearchBar
|
||||||
import kotlinx.coroutines.launch
|
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
@OptIn(ExperimentalComposeUiApi::class)
|
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||||
|
@OptIn(
|
||||||
|
ExperimentalComposeUiApi::class,
|
||||||
|
ExperimentalMaterial3Api::class,
|
||||||
|
ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun ConfigScreen(
|
fun ConfigScreen(
|
||||||
viewModel: ConfigViewModel = hiltViewModel(),
|
viewModel: ConfigViewModel = hiltViewModel(),
|
||||||
padding: PaddingValues,
|
|
||||||
focusRequester: FocusRequester,
|
focusRequester: FocusRequester,
|
||||||
navController: NavController,
|
navController: NavController,
|
||||||
id : String
|
showSnackbarMessage: (String) -> Unit,
|
||||||
|
id: String
|
||||||
) {
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
||||||
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
var showApplicationsDialog by remember { mutableStateOf(false) }
|
||||||
|
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||||
|
var isAuthenticated by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val context = LocalContext.current
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val focusManager = LocalFocusManager.current
|
|
||||||
|
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
|
||||||
val scope = rememberCoroutineScope()
|
|
||||||
val tunnel by viewModel.tunnel.collectAsStateWithLifecycle(null)
|
|
||||||
val tunnelName = viewModel.tunnelName.collectAsStateWithLifecycle()
|
|
||||||
val packages by viewModel.packages.collectAsStateWithLifecycle()
|
|
||||||
val checkedPackages by viewModel.checkedPackages.collectAsStateWithLifecycle()
|
|
||||||
val include by viewModel.include.collectAsStateWithLifecycle()
|
|
||||||
val allApplications by viewModel.allApplications.collectAsStateWithLifecycle()
|
|
||||||
val sortedPackages = remember(packages) {
|
|
||||||
packages.sortedBy { viewModel.getPackageLabel(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
viewModel.emitScreenData(id)
|
viewModel.init(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tunnel != null) {
|
LaunchedEffect(uiState.loading) {
|
||||||
LazyColumn(
|
if(!uiState.loading && WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
horizontalAlignment = Alignment.Start,
|
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||||
verticalArrangement = Arrangement.Top,
|
focusRequester.requestFocus()
|
||||||
modifier = Modifier
|
}
|
||||||
.fillMaxSize()
|
}
|
||||||
.padding(padding)
|
|
||||||
) {
|
if (uiState.loading) {
|
||||||
item {
|
LoadingScreen()
|
||||||
Row(
|
return
|
||||||
modifier = Modifier
|
}
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
val keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() })
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
val keyboardOptions =
|
||||||
) {
|
KeyboardOptions(imeAction = ImeAction.Done)
|
||||||
OutlinedTextField(
|
|
||||||
modifier = Modifier.focusRequester(focusRequester),
|
val fillMaxHeight = .85f
|
||||||
value = tunnelName.value,
|
val fillMaxWidth = .85f
|
||||||
onValueChange = {
|
val screenPadding = 5.dp
|
||||||
viewModel.onTunnelNameChange(it)
|
|
||||||
},
|
val applicationButtonText = {
|
||||||
label = { Text(stringResource(id = R.string.tunnel_name)) },
|
"Tunneling apps: " +
|
||||||
maxLines = 1,
|
if (uiState.isAllApplicationsEnabled) {
|
||||||
keyboardOptions = KeyboardOptions(
|
"all"
|
||||||
capitalization = KeyboardCapitalization.None,
|
} else {
|
||||||
imeAction = ImeAction.Done
|
"${uiState.checkedPackageNames.size} " + (if (uiState.include) "included" else "excluded")
|
||||||
),
|
}
|
||||||
keyboardActions = KeyboardActions(
|
}
|
||||||
onDone = {
|
|
||||||
focusManager.clearFocus()
|
if (showAuthPrompt) {
|
||||||
keyboardController?.hide()
|
AuthorizationPrompt(
|
||||||
viewModel.onTunnelNameChange(tunnelName.value)
|
onSuccess = {
|
||||||
}
|
showAuthPrompt = false
|
||||||
),
|
isAuthenticated = true
|
||||||
)
|
},
|
||||||
}
|
onError = { error ->
|
||||||
}
|
showAuthPrompt = false
|
||||||
item {
|
showSnackbarMessage(Event.Error.AuthenticationFailed.message)
|
||||||
Row(
|
},
|
||||||
modifier = Modifier
|
onFailure = {
|
||||||
.fillMaxWidth()
|
showAuthPrompt = false
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
showSnackbarMessage(Event.Error.AuthorizationFailed.message)
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
})
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
}
|
||||||
) {
|
|
||||||
|
if (showApplicationsDialog) {
|
||||||
|
val sortedPackages =
|
||||||
|
remember(uiState.packages) { uiState.packages.sortedBy { viewModel.getPackageLabel(it) } }
|
||||||
|
AlertDialog(onDismissRequest = { showApplicationsDialog = false }) {
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxWidth()
|
||||||
|
.fillMaxHeight(if (uiState.isAllApplicationsEnabled) 1 / 5f else 4 / 5f)) {
|
||||||
|
Column(modifier = Modifier.fillMaxWidth()) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 7.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
Text(stringResource(id = R.string.tunnel_all))
|
Text(stringResource(id = R.string.tunnel_all))
|
||||||
Switch(
|
Switch(
|
||||||
checked = allApplications,
|
checked = uiState.isAllApplicationsEnabled,
|
||||||
onCheckedChange = {
|
onCheckedChange = { viewModel.onAllApplicationsChange(it) })
|
||||||
viewModel.onAllApplicationsChange(!allApplications)
|
}
|
||||||
}
|
if (!uiState.isAllApplicationsEnabled) {
|
||||||
)
|
Row(
|
||||||
}
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 7.dp),
|
||||||
}
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
if (!allApplications) {
|
horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
item {
|
Row(
|
||||||
Row(
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
modifier = Modifier
|
horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
.fillMaxWidth()
|
Text(stringResource(id = R.string.include))
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
Checkbox(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
checked = uiState.include,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
onCheckedChange = { viewModel.onIncludeChange(!uiState.include) })
|
||||||
) {
|
}
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
) {
|
Text(stringResource(id = R.string.exclude))
|
||||||
Text(stringResource(id = R.string.include))
|
Checkbox(
|
||||||
Checkbox(
|
checked = !uiState.include,
|
||||||
checked = include,
|
onCheckedChange = { viewModel.onIncludeChange(!uiState.include) })
|
||||||
onCheckedChange = {
|
}
|
||||||
viewModel.onIncludeChange(!include)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Text(stringResource(id = R.string.exclude))
|
|
||||||
Checkbox(
|
|
||||||
checked = !include,
|
|
||||||
onCheckedChange = {
|
|
||||||
viewModel.onIncludeChange(!include)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
item {
|
Row(
|
||||||
Row(
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 7.dp),
|
||||||
modifier = Modifier
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
.fillMaxWidth()
|
horizontalArrangement = Arrangement.SpaceBetween) {
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
SearchBar(viewModel::emitQueriedPackages)
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween) {
|
|
||||||
SearchBar(viewModel::emitQueriedPackages);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
items(sortedPackages, key = { it.packageName }) { pack ->
|
Spacer(Modifier.padding(5.dp))
|
||||||
|
LazyColumn(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.fillMaxHeight(4 / 5f)) {
|
||||||
|
items(sortedPackages, key = { it.packageName }) { pack ->
|
||||||
Row(
|
Row(
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
) {
|
modifier = Modifier.fillMaxSize().padding(5.dp)) {
|
||||||
Row(
|
Row(modifier = Modifier.fillMaxWidth(fillMaxWidth)) {
|
||||||
horizontalArrangement = Arrangement.Center,
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier.padding(5.dp)
|
|
||||||
) {
|
|
||||||
val drawable =
|
val drawable =
|
||||||
pack.applicationInfo?.loadIcon(context.packageManager)
|
pack.applicationInfo?.loadIcon(context.packageManager)
|
||||||
if (drawable != null) {
|
if (drawable != null) {
|
||||||
Image(
|
Image(
|
||||||
painter = DrawablePainter(drawable),
|
painter = DrawablePainter(drawable),
|
||||||
stringResource(id = R.string.icon),
|
stringResource(id = R.string.icon),
|
||||||
modifier = Modifier.size(50.dp, 50.dp)
|
modifier = Modifier.size(50.dp, 50.dp))
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Icon(
|
Icon(
|
||||||
Icons.Rounded.Android,
|
Icons.Rounded.Android,
|
||||||
stringResource(id = R.string.edit),
|
stringResource(id = R.string.edit),
|
||||||
modifier = Modifier.size(50.dp, 50.dp)
|
modifier = Modifier.size(50.dp, 50.dp))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Text(
|
Text(
|
||||||
viewModel.getPackageLabel(pack), modifier = Modifier.padding(5.dp)
|
viewModel.getPackageLabel(pack),
|
||||||
)
|
modifier = Modifier.padding(5.dp))
|
||||||
|
}
|
||||||
|
Checkbox(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
checked =
|
||||||
|
(uiState.checkedPackageNames.contains(pack.packageName)),
|
||||||
|
onCheckedChange = {
|
||||||
|
if (it) {
|
||||||
|
viewModel.onAddCheckedPackage(pack.packageName)
|
||||||
|
} else {
|
||||||
|
viewModel.onRemoveCheckedPackage(pack.packageName)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Checkbox(
|
}
|
||||||
checked = (checkedPackages.contains(pack.packageName)),
|
|
||||||
onCheckedChange = {
|
|
||||||
if (it) viewModel.onAddCheckedPackage(pack.packageName) else viewModel.onRemoveCheckedPackage(
|
|
||||||
pack.packageName
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
item {
|
Row(
|
||||||
Row(
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.Center,
|
modifier = Modifier.fillMaxSize().padding(top = 5.dp),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
horizontalArrangement = Arrangement.Center) {
|
||||||
modifier = Modifier.fillMaxWidth()
|
TextButton(onClick = { showApplicationsDialog = false }) {
|
||||||
) {
|
Text(stringResource(R.string.done))
|
||||||
Button(onClick = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onSaveAllChanges()
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
context.resources.getString(R.string.config_changes_saved),
|
|
||||||
Toast.LENGTH_LONG
|
|
||||||
).show()
|
|
||||||
navController.navigate(Routes.Main.name)
|
|
||||||
}
|
|
||||||
}, Modifier.padding(25.dp)) {
|
|
||||||
Text(stringResource(id = R.string.save_changes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Scaffold(
|
||||||
|
floatingActionButtonPosition = FabPosition.End,
|
||||||
|
floatingActionButton = {
|
||||||
|
val secondaryColor = MaterialTheme.colorScheme.secondary
|
||||||
|
val hoverColor = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)
|
||||||
|
var fobColor by remember { mutableStateOf(secondaryColor) }
|
||||||
|
FloatingActionButton(
|
||||||
|
modifier =
|
||||||
|
Modifier.padding(bottom = 90.dp).onFocusChanged {
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
fobColor = if (it.isFocused) hoverColor else secondaryColor
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClick = {
|
||||||
|
viewModel.onSaveAllChanges().let {
|
||||||
|
when (it) {
|
||||||
|
is Result.Success -> {
|
||||||
|
showSnackbarMessage(it.data.message)
|
||||||
|
navController.navigate(Screen.Main.route)
|
||||||
|
}
|
||||||
|
is Result.Error -> showSnackbarMessage(it.error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
containerColor = fobColor,
|
||||||
|
shape = RoundedCornerShape(16.dp)) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Rounded.Save,
|
||||||
|
contentDescription = stringResource(id = R.string.save_changes),
|
||||||
|
tint = Color.DarkGray)
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Column {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier =
|
||||||
|
Modifier.verticalScroll(rememberScrollState()).weight(1f, true).fillMaxSize()) {
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier =
|
||||||
|
(if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Modifier.fillMaxHeight(fillMaxHeight).fillMaxWidth(fillMaxWidth)
|
||||||
|
} else {
|
||||||
|
Modifier.fillMaxWidth(fillMaxWidth)
|
||||||
|
})
|
||||||
|
.padding(top = 50.dp, bottom = 10.dp)) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.padding(15.dp).focusGroup()) {
|
||||||
|
SectionTitle(
|
||||||
|
stringResource(R.string.interface_), padding = screenPadding)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.tunnelName,
|
||||||
|
onValueChange = { value -> viewModel.onTunnelNameChange(value) },
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.name),
|
||||||
|
hint = stringResource(R.string.tunnel_name).lowercase(),
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.focusRequester(focusRequester))
|
||||||
|
OutlinedTextField(
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxWidth().clickable {
|
||||||
|
showAuthPrompt = true
|
||||||
|
},
|
||||||
|
value = uiState.interfaceProxy.privateKey,
|
||||||
|
visualTransformation =
|
||||||
|
if ((id == Constants.MANUAL_TUNNEL_CONFIG_ID) ||
|
||||||
|
isAuthenticated)
|
||||||
|
VisualTransformation.None
|
||||||
|
else PasswordVisualTransformation(),
|
||||||
|
enabled =
|
||||||
|
(id == Constants.MANUAL_TUNNEL_CONFIG_ID) || isAuthenticated,
|
||||||
|
onValueChange = { value -> viewModel.onPrivateKeyChange(value) },
|
||||||
|
trailingIcon = {
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||||
|
onClick = { viewModel.generateKeyPair() }) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Refresh,
|
||||||
|
stringResource(R.string.rotate_keys),
|
||||||
|
tint = Color.White)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
label = { Text(stringResource(R.string.private_key)) },
|
||||||
|
singleLine = true,
|
||||||
|
placeholder = { Text(stringResource(R.string.base64_key)) },
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions)
|
||||||
|
OutlinedTextField(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.focusRequester(FocusRequester.Default),
|
||||||
|
value = uiState.interfaceProxy.publicKey,
|
||||||
|
enabled = false,
|
||||||
|
onValueChange = {},
|
||||||
|
trailingIcon = {
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusRequester(FocusRequester.Default),
|
||||||
|
onClick = {
|
||||||
|
clipboardManager.setText(
|
||||||
|
AnnotatedString(uiState.interfaceProxy.publicKey))
|
||||||
|
}) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.ContentCopy,
|
||||||
|
stringResource(R.string.copy_public_key),
|
||||||
|
tint = Color.White)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
label = { Text(stringResource(R.string.public_key)) },
|
||||||
|
singleLine = true,
|
||||||
|
placeholder = { Text(stringResource(R.string.base64_key)) },
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions)
|
||||||
|
Row(modifier = Modifier.fillMaxWidth()) {
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.interfaceProxy.addresses,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onAddressesChanged(value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.addresses),
|
||||||
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth(3 / 5f)
|
||||||
|
.padding(end = 5.dp))
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.interfaceProxy.listenPort,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onListenPortChanged(value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.listen_port),
|
||||||
|
hint = stringResource(R.string.random),
|
||||||
|
modifier = Modifier.width(IntrinsicSize.Min))
|
||||||
|
}
|
||||||
|
Row(modifier = Modifier.fillMaxWidth()) {
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.interfaceProxy.dnsServers,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onDnsServersChanged(value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.dns_servers),
|
||||||
|
hint = stringResource(R.string.comma_separated_list),
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth(3 / 5f)
|
||||||
|
.padding(end = 5.dp))
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = uiState.interfaceProxy.mtu,
|
||||||
|
onValueChange = { value -> viewModel.onMtuChanged(value) },
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.mtu),
|
||||||
|
hint = stringResource(R.string.auto),
|
||||||
|
modifier = Modifier.width(IntrinsicSize.Min))
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxSize().padding(top = 5.dp),
|
||||||
|
horizontalArrangement = Arrangement.Center) {
|
||||||
|
TextButton(onClick = { showApplicationsDialog = true }) {
|
||||||
|
Text(applicationButtonText())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uiState.proxyPeers.forEachIndexed { index, peer ->
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier =
|
||||||
|
(if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Modifier.fillMaxHeight(fillMaxHeight).fillMaxWidth(fillMaxWidth)
|
||||||
|
} else {
|
||||||
|
Modifier.fillMaxWidth(fillMaxWidth)
|
||||||
|
})
|
||||||
|
.padding(top = 10.dp, bottom = 10.dp)) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier =
|
||||||
|
Modifier.padding(horizontal = 15.dp).padding(bottom = 10.dp)) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 5.dp)) {
|
||||||
|
SectionTitle(
|
||||||
|
stringResource(R.string.peer), padding = screenPadding)
|
||||||
|
IconButton(onClick = { viewModel.onDeletePeer(index) }) {
|
||||||
|
Icon(Icons.Rounded.Delete, stringResource(R.string.delete))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = peer.publicKey,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onPeerPublicKeyChange(index, value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.public_key),
|
||||||
|
hint = stringResource(R.string.base64_key),
|
||||||
|
modifier = Modifier.fillMaxWidth())
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = peer.preSharedKey,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onPreSharedKeyChange(index, value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.preshared_key),
|
||||||
|
hint = stringResource(R.string.optional),
|
||||||
|
modifier = Modifier.fillMaxWidth())
|
||||||
|
OutlinedTextField(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
value = peer.persistentKeepalive,
|
||||||
|
enabled = true,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onPersistentKeepaliveChanged(index, value)
|
||||||
|
},
|
||||||
|
trailingIcon = {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.seconds),
|
||||||
|
modifier = Modifier.padding(end = 10.dp))
|
||||||
|
},
|
||||||
|
label = { Text(stringResource(R.string.persistent_keepalive)) },
|
||||||
|
singleLine = true,
|
||||||
|
placeholder = {
|
||||||
|
Text(stringResource(R.string.optional_no_recommend))
|
||||||
|
},
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions)
|
||||||
|
ConfigurationTextBox(
|
||||||
|
value = peer.endpoint,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onEndpointChange(index, value)
|
||||||
|
},
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
label = stringResource(R.string.endpoint),
|
||||||
|
hint = stringResource(R.string.endpoint).lowercase(),
|
||||||
|
modifier = Modifier.fillMaxWidth())
|
||||||
|
OutlinedTextField(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
value = peer.allowedIps,
|
||||||
|
enabled = true,
|
||||||
|
onValueChange = { value ->
|
||||||
|
viewModel.onAllowedIpsChange(index, value)
|
||||||
|
},
|
||||||
|
label = { Text(stringResource(R.string.allowed_ips)) },
|
||||||
|
singleLine = true,
|
||||||
|
placeholder = {
|
||||||
|
Text(stringResource(R.string.comma_separated_list))
|
||||||
|
},
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceEvenly,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxSize().padding(bottom = 140.dp)) {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.Center) {
|
||||||
|
TextButton(onClick = { viewModel.addEmptyPeer() }) {
|
||||||
|
Text(stringResource(R.string.add_peer))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Spacer(modifier = Modifier.weight(.17f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.config
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.models.InterfaceProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.models.PeerProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Packages
|
||||||
|
|
||||||
|
data class ConfigUiState(
|
||||||
|
val proxyPeers: List<PeerProxy> = arrayListOf(PeerProxy()),
|
||||||
|
val interfaceProxy: InterfaceProxy = InterfaceProxy(),
|
||||||
|
val packages: Packages = emptyList(),
|
||||||
|
val checkedPackageNames: List<String> = emptyList(),
|
||||||
|
val include: Boolean = true,
|
||||||
|
val isAllApplicationsEnabled : Boolean = false,
|
||||||
|
val loading: Boolean = true,
|
||||||
|
val tunnel: TunnelConfig? = null,
|
||||||
|
val tunnelName: String = ""
|
||||||
|
)
|
||||||
+279
-214
@@ -5,243 +5,308 @@ import android.app.Application
|
|||||||
import android.content.pm.PackageInfo
|
import android.content.pm.PackageInfo
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
|
||||||
import androidx.compose.runtime.toMutableStateList
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.wireguard.config.Interface
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
import com.wireguard.config.Peer
|
||||||
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsManager
|
import com.wireguard.crypto.Key
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.wireguard.crypto.KeyPair
|
||||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelException
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.models.InterfaceProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.models.PeerProxy
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.removeAt
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.update
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
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.first
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class ConfigViewModel @Inject constructor(private val application : Application,
|
class ConfigViewModel
|
||||||
private val tunnelRepo : TunnelConfigDao,
|
@Inject
|
||||||
private val settingsRepo : SettingsDoa) : ViewModel() {
|
constructor(
|
||||||
|
private val application: Application,
|
||||||
|
private val tunnelConfigRepository: TunnelConfigRepository,
|
||||||
|
private val settingsRepository: SettingsRepository,
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _tunnel = MutableStateFlow<TunnelConfig?>(null)
|
private val packageManager = application.packageManager
|
||||||
private val _tunnelName = MutableStateFlow("")
|
|
||||||
val tunnelName get() = _tunnelName.asStateFlow()
|
|
||||||
val tunnel get() = _tunnel.asStateFlow()
|
|
||||||
private val _packages = MutableStateFlow(emptyList<PackageInfo>())
|
|
||||||
val packages get() = _packages.asStateFlow()
|
|
||||||
private val packageManager = application.packageManager
|
|
||||||
|
|
||||||
private val _checkedPackages = MutableStateFlow(mutableStateListOf<String>())
|
private val _uiState = MutableStateFlow(ConfigUiState())
|
||||||
val checkedPackages get() = _checkedPackages.asStateFlow()
|
val uiState = _uiState.asStateFlow()
|
||||||
private val _include = MutableStateFlow(true)
|
|
||||||
val include get() = _include.asStateFlow()
|
|
||||||
|
|
||||||
private val _allApplications = MutableStateFlow(true)
|
fun init(tunnelId : String) = viewModelScope.launch(Dispatchers.IO) {
|
||||||
val allApplications get() = _allApplications.asStateFlow()
|
val packages = getQueriedPackages("")
|
||||||
|
val state = if(tunnelId != Constants.MANUAL_TUNNEL_CONFIG_ID) {
|
||||||
fun emitScreenData(id : String) {
|
val tunnelConfig =
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
tunnelConfigRepository.getAll().firstOrNull { it.id.toString() == tunnelId }
|
||||||
val tunnelConfig = getTunnelConfigById(id);
|
|
||||||
emitTunnelConfig(tunnelConfig);
|
|
||||||
emitTunnelConfigName(tunnelConfig?.name)
|
|
||||||
emitQueriedPackages("")
|
|
||||||
emitCurrentPackageConfigurations(id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getTunnelConfigById(id : String) : TunnelConfig? {
|
|
||||||
return try {
|
|
||||||
tunnelRepo.getById(id.toLong())
|
|
||||||
} catch (e : Exception) {
|
|
||||||
Timber.e(e.message)
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitTunnelConfig(tunnelConfig: TunnelConfig?) {
|
|
||||||
if(tunnelConfig != null) {
|
|
||||||
_tunnel.emit(tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitTunnelConfigName(name : String?) {
|
|
||||||
if(name != null) {
|
|
||||||
_tunnelName.emit(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelNameChange(name : String) {
|
|
||||||
_tunnelName.value = name
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onIncludeChange(include : Boolean) {
|
|
||||||
_include.value = include
|
|
||||||
}
|
|
||||||
fun onAddCheckedPackage(packageName : String) {
|
|
||||||
_checkedPackages.value.add(packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onAllApplicationsChange(allApplications : Boolean) {
|
|
||||||
_allApplications.value = allApplications
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onRemoveCheckedPackage(packageName : String) {
|
|
||||||
_checkedPackages.value.remove(packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitSplitTunnelConfiguration(config : Config) {
|
|
||||||
val excludedApps = config.`interface`.excludedApplications
|
|
||||||
val includedApps = config.`interface`.includedApplications
|
|
||||||
if (excludedApps.isNotEmpty() || includedApps.isNotEmpty()) {
|
|
||||||
emitTunnelAllApplicationsDisabled()
|
|
||||||
determineAppInclusionState(excludedApps, includedApps)
|
|
||||||
} else {
|
|
||||||
emitTunnelAllApplicationsEnabled()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun determineAppInclusionState(excludedApps : Set<String>, includedApps : Set<String>) {
|
|
||||||
if (excludedApps.isEmpty()) {
|
|
||||||
emitIncludedAppsExist()
|
|
||||||
emitCheckedApps(includedApps)
|
|
||||||
} else {
|
|
||||||
emitExcludedAppsExist()
|
|
||||||
emitCheckedApps(excludedApps)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitIncludedAppsExist() {
|
|
||||||
_include.emit(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitExcludedAppsExist() {
|
|
||||||
_include.emit(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitCheckedApps(apps : Set<String>) {
|
|
||||||
_checkedPackages.emit(apps.toMutableStateList())
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitTunnelAllApplicationsEnabled() {
|
|
||||||
_allApplications.emit(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun emitTunnelAllApplicationsDisabled() {
|
|
||||||
_allApplications.emit(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun emitCurrentPackageConfigurations(id : String) {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
val tunnelConfig = getTunnelConfigById(id)
|
|
||||||
if (tunnelConfig != null) {
|
if (tunnelConfig != null) {
|
||||||
val config = TunnelConfig.configFromQuick(tunnelConfig.wgQuick)
|
val config = TunnelConfig.configFromQuick(tunnelConfig.wgQuick)
|
||||||
emitSplitTunnelConfiguration(config)
|
val proxyPeers = config.peers.map { PeerProxy.from(it) }
|
||||||
}
|
val proxyInterface = InterfaceProxy.from(config.`interface`)
|
||||||
}
|
var include = true
|
||||||
}
|
var isAllApplicationsEnabled = false
|
||||||
|
val checkedPackages =
|
||||||
fun emitQueriedPackages(query : String) {
|
if (config.`interface`.includedApplications.isNotEmpty()) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
config.`interface`.includedApplications
|
||||||
val packages = getAllInternetCapablePackages().filter {
|
} else if (config.`interface`.excludedApplications.isNotEmpty()) {
|
||||||
getPackageLabel(it).lowercase().contains(query.lowercase())
|
include = false
|
||||||
}
|
config.`interface`.excludedApplications
|
||||||
_packages.emit(packages)
|
} else {
|
||||||
}
|
isAllApplicationsEnabled = true
|
||||||
}
|
emptySet()
|
||||||
|
}
|
||||||
fun getPackageLabel(packageInfo : PackageInfo) : String {
|
ConfigUiState(
|
||||||
return packageInfo.applicationInfo.loadLabel(application.packageManager).toString()
|
proxyPeers,
|
||||||
}
|
proxyInterface,
|
||||||
|
packages,
|
||||||
|
checkedPackages.toList(),
|
||||||
private fun getAllInternetCapablePackages() : List<PackageInfo> {
|
include,
|
||||||
return getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET))
|
isAllApplicationsEnabled,
|
||||||
}
|
false,
|
||||||
|
tunnelConfig,
|
||||||
private fun getPackagesHoldingPermissions(permissions: Array<String>): List<PackageInfo> {
|
tunnelConfig.name)
|
||||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
packageManager.getPackagesHoldingPermissions(permissions, PackageManager.PackageInfoFlags.of(0L))
|
|
||||||
} else {
|
|
||||||
packageManager.getPackagesHoldingPermissions(permissions, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun removeTunnelShortcuts(tunnelConfig: TunnelConfig?) {
|
|
||||||
if(tunnelConfig != null) {
|
|
||||||
ShortcutsManager.removeTunnelShortcuts(application, tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isAllApplicationsEnabled() : Boolean {
|
|
||||||
return _allApplications.value
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isIncludeApplicationsEnabled() : Boolean {
|
|
||||||
return _include.value
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateQuickStringWithSelectedPackages() : String {
|
|
||||||
var wgQuick = _tunnel.value?.wgQuick
|
|
||||||
if(wgQuick != null) {
|
|
||||||
wgQuick = if(isAllApplicationsEnabled()) {
|
|
||||||
TunnelConfig.clearAllApplicationsFromConfig(wgQuick)
|
|
||||||
} else if(isIncludeApplicationsEnabled()) {
|
|
||||||
TunnelConfig.setIncludedApplicationsOnQuick(_checkedPackages.value, wgQuick)
|
|
||||||
} else {
|
} else {
|
||||||
TunnelConfig.setExcludedApplicationsOnQuick(_checkedPackages.value, wgQuick)
|
ConfigUiState(loading = false, packages = packages)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw WgTunnelException("Wg quick string is null")
|
ConfigUiState(loading = false, packages = packages)
|
||||||
}
|
}
|
||||||
return wgQuick;
|
_uiState.value = state
|
||||||
}
|
}
|
||||||
|
fun onTunnelNameChange(name: String) {
|
||||||
|
_uiState.value = _uiState.value.copy(tunnelName = name)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun saveConfig(tunnelConfig: TunnelConfig) {
|
fun onIncludeChange(include: Boolean) {
|
||||||
tunnelRepo.save(tunnelConfig)
|
_uiState.value = _uiState.value.copy(include = include)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onAddCheckedPackage(packageName: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(checkedPackageNames = _uiState.value.checkedPackageNames + packageName)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onAllApplicationsChange(isAllApplicationsEnabled: Boolean) {
|
||||||
|
_uiState.value = _uiState.value.copy(isAllApplicationsEnabled = isAllApplicationsEnabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onRemoveCheckedPackage(packageName: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(checkedPackageNames = _uiState.value.checkedPackageNames - packageName)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getQueriedPackages(query: String): List<PackageInfo> {
|
||||||
|
return getAllInternetCapablePackages().filter {
|
||||||
|
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||||
}
|
}
|
||||||
private suspend fun updateTunnelConfig(tunnelConfig: TunnelConfig?) {
|
}
|
||||||
if(tunnelConfig != null) {
|
|
||||||
saveConfig(tunnelConfig)
|
fun getPackageLabel(packageInfo: PackageInfo): String {
|
||||||
addTunnelShortcuts(tunnelConfig)
|
return packageInfo.applicationInfo.loadLabel(application.packageManager).toString()
|
||||||
updateSettingsDefaultTunnel(tunnelConfig)
|
}
|
||||||
|
|
||||||
|
private fun getAllInternetCapablePackages(): List<PackageInfo> {
|
||||||
|
return getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getPackagesHoldingPermissions(permissions: Array<String>): List<PackageInfo> {
|
||||||
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
packageManager.getPackagesHoldingPermissions(
|
||||||
|
permissions, PackageManager.PackageInfoFlags.of(0L))
|
||||||
|
} else {
|
||||||
|
packageManager.getPackagesHoldingPermissions(permissions, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isAllApplicationsEnabled(): Boolean {
|
||||||
|
return _uiState.value.isAllApplicationsEnabled
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveConfig(tunnelConfig: TunnelConfig) =
|
||||||
|
viewModelScope.launch {
|
||||||
|
tunnelConfigRepository.save(tunnelConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateTunnelConfig(tunnelConfig: TunnelConfig?) =
|
||||||
|
viewModelScope.launch {
|
||||||
|
if (tunnelConfig != null) {
|
||||||
|
saveConfig(tunnelConfig).join()
|
||||||
|
WireGuardAutoTunnel.requestTileServiceStateUpdate()
|
||||||
|
updateSettingsDefaultTunnel(tunnelConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateSettingsDefaultTunnel(tunnelConfig: TunnelConfig) {
|
private suspend fun updateSettingsDefaultTunnel(tunnelConfig: TunnelConfig) {
|
||||||
val settings = settingsRepo.getAll()
|
val settings = settingsRepository.getSettingsFlow().first()
|
||||||
if(settings.isNotEmpty()) {
|
if (settings.defaultTunnel != null) {
|
||||||
val setting = settings[0]
|
if (tunnelConfig.id == TunnelConfig.from(settings.defaultTunnel!!).id) {
|
||||||
if(setting.defaultTunnel != null) {
|
settingsRepository.save(settings.copy(defaultTunnel = tunnelConfig.toString()))
|
||||||
if(tunnelConfig.id == TunnelConfig.from(setting.defaultTunnel!!).id) {
|
}
|
||||||
settingsRepo.save(setting.copy(
|
}
|
||||||
defaultTunnel = tunnelConfig.toString()
|
}
|
||||||
))
|
|
||||||
}
|
private fun buildPeerListFromProxyPeers(): List<Peer> {
|
||||||
}
|
return _uiState.value.proxyPeers.map {
|
||||||
|
val builder = Peer.Builder()
|
||||||
|
if (it.allowedIps.isNotEmpty()) builder.parseAllowedIPs(it.allowedIps.trim())
|
||||||
|
if (it.publicKey.isNotEmpty()) builder.parsePublicKey(it.publicKey.trim())
|
||||||
|
if (it.preSharedKey.isNotEmpty()) builder.parsePreSharedKey(it.preSharedKey.trim())
|
||||||
|
if (it.endpoint.isNotEmpty()) builder.parseEndpoint(it.endpoint.trim())
|
||||||
|
if (it.persistentKeepalive.isNotEmpty()) {
|
||||||
|
builder.parsePersistentKeepalive(it.persistentKeepalive.trim())
|
||||||
|
}
|
||||||
|
builder.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun emptyCheckedPackagesList() {
|
||||||
|
_uiState.value = _uiState.value.copy(checkedPackageNames = emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildInterfaceListFromProxyInterface(): Interface {
|
||||||
|
val builder = Interface.Builder()
|
||||||
|
builder.parsePrivateKey(_uiState.value.interfaceProxy.privateKey.trim())
|
||||||
|
builder.parseAddresses(_uiState.value.interfaceProxy.addresses.trim())
|
||||||
|
builder.parseDnsServers(_uiState.value.interfaceProxy.dnsServers.trim())
|
||||||
|
if (_uiState.value.interfaceProxy.mtu.isNotEmpty())
|
||||||
|
builder.parseMtu(_uiState.value.interfaceProxy.mtu.trim())
|
||||||
|
if (_uiState.value.interfaceProxy.listenPort.isNotEmpty()) {
|
||||||
|
builder.parseListenPort(_uiState.value.interfaceProxy.listenPort.trim())
|
||||||
|
}
|
||||||
|
if (isAllApplicationsEnabled()) emptyCheckedPackagesList()
|
||||||
|
if (_uiState.value.include) builder.includeApplications(_uiState.value.checkedPackageNames)
|
||||||
|
if (!_uiState.value.include) builder.excludeApplications(_uiState.value.checkedPackageNames)
|
||||||
|
return builder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onSaveAllChanges(): Result<Event> {
|
||||||
|
return try {
|
||||||
|
val peerList = buildPeerListFromProxyPeers()
|
||||||
|
val wgInterface = buildInterfaceListFromProxyInterface()
|
||||||
|
val config = Config.Builder().addPeers(peerList).setInterface(wgInterface).build()
|
||||||
|
val tunnelConfig =
|
||||||
|
_uiState.value.tunnel?.copy(
|
||||||
|
name = _uiState.value.tunnelName, wgQuick = config.toWgQuickString())
|
||||||
|
updateTunnelConfig(tunnelConfig)
|
||||||
|
Result.Success(Event.Message.ConfigSaved)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.Error(Event.Error.Exception(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPeerPublicKeyChange(index: Int, value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
proxyPeers =
|
||||||
|
_uiState.value.proxyPeers.update(
|
||||||
|
index, _uiState.value.proxyPeers[index].copy(publicKey = value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPreSharedKeyChange(index: Int, value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
proxyPeers =
|
||||||
|
_uiState.value.proxyPeers.update(
|
||||||
|
index, _uiState.value.proxyPeers[index].copy(preSharedKey = value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onEndpointChange(index: Int, value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
proxyPeers =
|
||||||
|
_uiState.value.proxyPeers.update(
|
||||||
|
index, _uiState.value.proxyPeers[index].copy(endpoint = value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onAllowedIpsChange(index: Int, value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
proxyPeers =
|
||||||
|
_uiState.value.proxyPeers.update(
|
||||||
|
index, _uiState.value.proxyPeers[index].copy(allowedIps = value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPersistentKeepaliveChanged(index: Int, value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
proxyPeers =
|
||||||
|
_uiState.value.proxyPeers.update(
|
||||||
|
index, _uiState.value.proxyPeers[index].copy(persistentKeepalive = value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDeletePeer(index: Int) {
|
||||||
|
_uiState.value = _uiState.value.copy(
|
||||||
|
proxyPeers = _uiState.value.proxyPeers.removeAt(index)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addEmptyPeer() {
|
||||||
|
_uiState.value = _uiState.value.copy(proxyPeers = _uiState.value.proxyPeers + PeerProxy())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun generateKeyPair() {
|
||||||
|
val keyPair = KeyPair()
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(
|
||||||
|
interfaceProxy =
|
||||||
|
_uiState.value.interfaceProxy.copy(
|
||||||
|
privateKey = keyPair.privateKey.toBase64(),
|
||||||
|
publicKey = keyPair.publicKey.toBase64()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onAddressesChanged(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(addresses = value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onListenPortChanged(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(listenPort = value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDnsServersChanged(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(dnsServers = value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onMtuChanged(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(mtu = value))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onInterfacePublicKeyChange(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(publicKey = value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPrivateKeyChange(value: String) {
|
||||||
|
_uiState.value =
|
||||||
|
_uiState.value.copy(interfaceProxy = _uiState.value.interfaceProxy.copy(privateKey = value))
|
||||||
|
if (NumberUtils.isValidKey(value)) {
|
||||||
|
val pair = KeyPair(Key.fromBase64(value))
|
||||||
|
onInterfacePublicKeyChange(pair.publicKey.toBase64())
|
||||||
|
} else {
|
||||||
|
onInterfacePublicKeyChange("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun emitQueriedPackages(query: String) {
|
||||||
|
val packages =
|
||||||
|
getAllInternetCapablePackages().filter {
|
||||||
|
getPackageLabel(it).lowercase().contains(query.lowercase())
|
||||||
}
|
}
|
||||||
}
|
_uiState.value = _uiState.value.copy(packages = packages)
|
||||||
|
}
|
||||||
private fun addTunnelShortcuts(tunnelConfig: TunnelConfig) {
|
}
|
||||||
ShortcutsManager.createTunnelShortcuts(application, tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun onSaveAllChanges() {
|
|
||||||
try {
|
|
||||||
removeTunnelShortcuts(_tunnel.value)
|
|
||||||
val wgQuick = updateQuickStringWithSelectedPackages()
|
|
||||||
val tunnelConfig = _tunnel.value?.copy(
|
|
||||||
name = _tunnelName.value,
|
|
||||||
wgQuick = wgQuick
|
|
||||||
)
|
|
||||||
updateTunnelConfig(tunnelConfig)
|
|
||||||
} catch (e : Exception) {
|
|
||||||
Timber.e(e.message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
-143
@@ -1,143 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.detail
|
|
||||||
|
|
||||||
import androidx.compose.foundation.clickable
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
|
||||||
import androidx.compose.foundation.verticalScroll
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.ClipboardManager
|
|
||||||
import androidx.compose.ui.platform.LocalClipboardManager
|
|
||||||
import androidx.compose.ui.res.stringResource
|
|
||||||
import androidx.compose.ui.text.AnnotatedString
|
|
||||||
import androidx.compose.ui.text.font.FontStyle
|
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.compose.ui.unit.sp
|
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
|
||||||
import java.time.Duration
|
|
||||||
import java.time.Instant
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun DetailScreen(
|
|
||||||
viewModel: DetailViewModel = hiltViewModel(),
|
|
||||||
padding: PaddingValues,
|
|
||||||
id : String
|
|
||||||
) {
|
|
||||||
|
|
||||||
val clipboardManager: ClipboardManager = LocalClipboardManager.current
|
|
||||||
val tunnelStats by viewModel.tunnelStats.collectAsStateWithLifecycle(null)
|
|
||||||
val tunnel by viewModel.tunnel.collectAsStateWithLifecycle(null)
|
|
||||||
val tunnelName by viewModel.tunnelName.collectAsStateWithLifecycle()
|
|
||||||
val lastHandshake by viewModel.lastHandshake.collectAsStateWithLifecycle(emptyMap())
|
|
||||||
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
viewModel.emitConfig(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
if(null != tunnel) {
|
|
||||||
val interfaceKey = tunnel?.`interface`?.keyPair?.publicKey?.toBase64().toString()
|
|
||||||
val addresses = tunnel?.`interface`?.addresses!!.joinToString()
|
|
||||||
val dnsServers = tunnel?.`interface`?.dnsServers!!.joinToString()
|
|
||||||
val optionalMtu = tunnel?.`interface`?.mtu
|
|
||||||
val mtu = if(optionalMtu?.isPresent == true) optionalMtu.get().toString() else stringResource(
|
|
||||||
id = R.string.none
|
|
||||||
)
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.verticalScroll(rememberScrollState())
|
|
||||||
.padding(padding)
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(horizontal = 20.dp, vertical = 7.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Column {
|
|
||||||
Text(stringResource(R.string.config_interface), fontWeight = FontWeight.Bold, fontSize = 20.sp)
|
|
||||||
Text(stringResource(R.string.name), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = tunnelName, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(tunnelName))
|
|
||||||
})
|
|
||||||
Text(stringResource(R.string.public_key), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = interfaceKey, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(interfaceKey))
|
|
||||||
})
|
|
||||||
Text(stringResource(R.string.addresses), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = addresses, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(addresses))
|
|
||||||
})
|
|
||||||
Text(stringResource(R.string.dns_servers), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = dnsServers, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(dnsServers))
|
|
||||||
})
|
|
||||||
Text(stringResource(R.string.mtu), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = mtu, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(mtu))
|
|
||||||
})
|
|
||||||
Box(modifier = Modifier.padding(10.dp))
|
|
||||||
tunnel?.peers?.forEach{
|
|
||||||
val peerKey = it.publicKey.toBase64().toString()
|
|
||||||
val allowedIps = it.allowedIps.joinToString()
|
|
||||||
val endpoint = if(it.endpoint.isPresent) it.endpoint.get().toString() else stringResource(
|
|
||||||
id = R.string.none
|
|
||||||
)
|
|
||||||
Text(stringResource(R.string.peer), fontWeight = FontWeight.Bold, fontSize = 20.sp)
|
|
||||||
Text(stringResource(R.string.public_key), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = peerKey, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(peerKey))
|
|
||||||
})
|
|
||||||
Text(stringResource(id = R.string.allowed_ips), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = allowedIps, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(allowedIps))
|
|
||||||
})
|
|
||||||
Text(stringResource(R.string.endpoint), fontStyle = FontStyle.Italic)
|
|
||||||
Text(text = endpoint, modifier = Modifier.clickable {
|
|
||||||
clipboardManager.setText(AnnotatedString(endpoint))
|
|
||||||
})
|
|
||||||
if (tunnelStats != null) {
|
|
||||||
val totalRx = tunnelStats?.totalRx() ?: 0
|
|
||||||
val totalTx = tunnelStats?.totalTx() ?: 0
|
|
||||||
if((totalRx + totalTx != 0L)) {
|
|
||||||
val rxKB = NumberUtils.bytesToKB(tunnelStats!!.totalRx())
|
|
||||||
val txKB = NumberUtils.bytesToKB(tunnelStats!!.totalTx())
|
|
||||||
Text(stringResource(R.string.transfer), fontStyle = FontStyle.Italic)
|
|
||||||
Text("rx: ${NumberUtils.formatDecimalTwoPlaces(rxKB)} KB tx: ${NumberUtils.formatDecimalTwoPlaces(txKB)} KB")
|
|
||||||
Text(stringResource(R.string.last_handshake), fontStyle = FontStyle.Italic)
|
|
||||||
val handshakeEpoch = lastHandshake[it.publicKey]
|
|
||||||
if(handshakeEpoch != null) {
|
|
||||||
if(handshakeEpoch == 0L) {
|
|
||||||
Text(stringResource(id = R.string.never))
|
|
||||||
} else {
|
|
||||||
val time = Instant.ofEpochMilli(handshakeEpoch)
|
|
||||||
Text("${Duration.between(time, Instant.now()).seconds} seconds ago")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-45
@@ -1,45 +0,0 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.detail
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.wireguard.config.Config
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@HiltViewModel
|
|
||||||
class DetailViewModel @Inject constructor(private val tunnelRepo : TunnelConfigDao, private val vpnService : VpnService
|
|
||||||
) : ViewModel() {
|
|
||||||
|
|
||||||
private val _tunnel = MutableStateFlow<Config?>(null)
|
|
||||||
val tunnel get() = _tunnel.asStateFlow()
|
|
||||||
|
|
||||||
private val _tunnelName = MutableStateFlow("")
|
|
||||||
val tunnelName = _tunnelName.asStateFlow()
|
|
||||||
val tunnelStats get() = vpnService.statistics
|
|
||||||
val lastHandshake get() = vpnService.lastHandshake
|
|
||||||
|
|
||||||
private suspend fun getTunnelConfigById(id: String): TunnelConfig? {
|
|
||||||
return try {
|
|
||||||
tunnelRepo.getById(id.toLong())
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e.message)
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fun emitConfig(id: String) {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
val tunnelConfig = getTunnelConfigById(id)
|
|
||||||
if(tunnelConfig != null) {
|
|
||||||
_tunnel.emit(TunnelConfig.configFromQuick(tunnelConfig.wgQuick))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+443
-267
@@ -1,33 +1,49 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
|
import androidx.compose.animation.fadeIn
|
||||||
|
import androidx.compose.animation.fadeOut
|
||||||
import androidx.compose.animation.slideInVertically
|
import androidx.compose.animation.slideInVertically
|
||||||
import androidx.compose.animation.slideOutVertically
|
import androidx.compose.animation.slideOutVertically
|
||||||
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.focusable
|
import androidx.compose.foundation.focusable
|
||||||
|
import androidx.compose.foundation.gestures.ScrollableDefaults
|
||||||
import androidx.compose.foundation.gestures.detectTapGestures
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.requiredWidth
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
|
import androidx.compose.foundation.overscroll
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.Create
|
||||||
import androidx.compose.material.icons.filled.FileOpen
|
import androidx.compose.material.icons.filled.FileOpen
|
||||||
import androidx.compose.material.icons.filled.QrCode
|
import androidx.compose.material.icons.filled.QrCode
|
||||||
import androidx.compose.material.icons.rounded.Add
|
import androidx.compose.material.icons.rounded.Add
|
||||||
|
import androidx.compose.material.icons.rounded.Bolt
|
||||||
import androidx.compose.material.icons.rounded.Circle
|
import androidx.compose.material.icons.rounded.Circle
|
||||||
import androidx.compose.material.icons.rounded.Delete
|
import androidx.compose.material.icons.rounded.Delete
|
||||||
import androidx.compose.material.icons.rounded.Edit
|
import androidx.compose.material.icons.rounded.Edit
|
||||||
import androidx.compose.material.icons.rounded.Info
|
import androidx.compose.material.icons.rounded.Info
|
||||||
|
import androidx.compose.material.icons.rounded.Star
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
import androidx.compose.material3.Divider
|
import androidx.compose.material3.Divider
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.FabPosition
|
import androidx.compose.material3.FabPosition
|
||||||
@@ -35,16 +51,18 @@ import androidx.compose.material3.FloatingActionButton
|
|||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.IconButton
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.MaterialTheme.typography
|
||||||
import androidx.compose.material3.ModalBottomSheet
|
import androidx.compose.material3.ModalBottomSheet
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.SnackbarDuration
|
|
||||||
import androidx.compose.material3.SnackbarHostState
|
|
||||||
import androidx.compose.material3.SnackbarResult
|
|
||||||
import androidx.compose.material3.Switch
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.material3.TopAppBar
|
||||||
import androidx.compose.material3.rememberModalBottomSheetState
|
import androidx.compose.material3.rememberModalBottomSheetState
|
||||||
|
import androidx.compose.material3.surfaceColorAtElevation
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
@@ -55,13 +73,11 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
import androidx.compose.ui.focus.focusRequester
|
import androidx.compose.ui.focus.focusRequester
|
||||||
import androidx.compose.ui.geometry.Offset
|
import androidx.compose.ui.focus.onFocusChanged
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
|
||||||
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
|
|
||||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
|
||||||
import androidx.compose.ui.input.pointer.pointerInput
|
import androidx.compose.ui.input.pointer.pointerInput
|
||||||
|
import androidx.compose.ui.platform.LocalConfiguration
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
@@ -73,314 +89,474 @@ import androidx.navigation.NavController
|
|||||||
import com.journeyapps.barcodescanner.ScanContract
|
import com.journeyapps.barcodescanner.ScanContract
|
||||||
import com.journeyapps.barcodescanner.ScanOptions
|
import com.journeyapps.barcodescanner.ScanOptions
|
||||||
import com.wireguard.android.backend.Tunnel
|
import com.wireguard.android.backend.Tunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.HandshakeStatus
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.CaptureActivityPortrait
|
import com.zaneschepke.wireguardautotunnel.ui.CaptureActivityPortrait
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Routes
|
import com.zaneschepke.wireguardautotunnel.ui.Screen
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.RowListItem
|
import com.zaneschepke.wireguardautotunnel.ui.common.RowListItem
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.brickRed
|
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.theme.corn
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.theme.mint
|
import com.zaneschepke.wireguardautotunnel.ui.theme.mint
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.handshakeStatus
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.mapPeerStats
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun MainScreen(
|
fun MainScreen(
|
||||||
viewModel: MainViewModel = hiltViewModel(), padding: PaddingValues,
|
viewModel: MainViewModel = hiltViewModel(),
|
||||||
snackbarHostState: SnackbarHostState, navController: NavController
|
padding: PaddingValues,
|
||||||
|
focusRequester: FocusRequester,
|
||||||
|
showSnackbarMessage: (String) -> Unit,
|
||||||
|
navController: NavController
|
||||||
) {
|
) {
|
||||||
|
val haptic = LocalHapticFeedback.current
|
||||||
|
val context = LocalContext.current
|
||||||
|
val isVisible = rememberSaveable { mutableStateOf(true) }
|
||||||
|
val scope = rememberCoroutineScope { Dispatchers.IO }
|
||||||
|
|
||||||
val haptic = LocalHapticFeedback.current
|
val sheetState = rememberModalBottomSheetState()
|
||||||
val context = LocalContext.current
|
var showBottomSheet by remember { mutableStateOf(false) }
|
||||||
val isVisible = rememberSaveable { mutableStateOf(true) }
|
|
||||||
val scope = rememberCoroutineScope()
|
|
||||||
|
|
||||||
val sheetState = rememberModalBottomSheetState()
|
var showPrimaryChangeAlertDialog by remember { mutableStateOf(false) }
|
||||||
var showBottomSheet by remember { mutableStateOf(false) }
|
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
||||||
val tunnels by viewModel.tunnels.collectAsStateWithLifecycle(mutableListOf())
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val handshakeStatus by viewModel.handshakeStatus.collectAsStateWithLifecycle(HandshakeStatus.NOT_STARTED)
|
|
||||||
val viewState = viewModel.viewState.collectAsStateWithLifecycle()
|
|
||||||
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
|
|
||||||
val state by viewModel.state.collectAsStateWithLifecycle(Tunnel.State.DOWN)
|
|
||||||
val tunnelName by viewModel.tunnelName.collectAsStateWithLifecycle("")
|
|
||||||
|
|
||||||
|
LaunchedEffect(uiState.loading) {
|
||||||
|
if (!uiState.loading && WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
delay(Constants.FOCUS_REQUEST_DELAY)
|
||||||
|
focusRequester.requestFocus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Nested scroll for control FAB
|
if (uiState.loading) {
|
||||||
val nestedScrollConnection = remember {
|
LoadingScreen()
|
||||||
object : NestedScrollConnection {
|
return
|
||||||
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
|
}
|
||||||
// Hide FAB
|
|
||||||
if (available.y < -1) {
|
val tunnelFileImportResultLauncher =
|
||||||
isVisible.value = false
|
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)
|
||||||
|
}) {
|
||||||
|
showSnackbarMessage(Event.Error.FileExplorerRequired.message)
|
||||||
|
}
|
||||||
|
return intent
|
||||||
|
}
|
||||||
|
}) { data ->
|
||||||
|
if (data == null) return@rememberLauncherForActivityResult
|
||||||
|
scope.launch {
|
||||||
|
viewModel.onTunnelFileSelected(data).let {
|
||||||
|
when (it) {
|
||||||
|
is Result.Error -> showSnackbarMessage(it.error.message)
|
||||||
|
is Result.Success -> {}
|
||||||
}
|
}
|
||||||
// Show FAB
|
}
|
||||||
if (available.y > 1) {
|
}
|
||||||
isVisible.value = true
|
}
|
||||||
|
val scanLauncher =
|
||||||
|
rememberLauncherForActivityResult(
|
||||||
|
contract = ScanContract(),
|
||||||
|
onResult = {
|
||||||
|
if (it.contents != null) {
|
||||||
|
scope.launch {
|
||||||
|
viewModel.onTunnelQrResult(it.contents).let { result ->
|
||||||
|
when (result) {
|
||||||
|
is Result.Success -> {}
|
||||||
|
is Result.Error -> showSnackbarMessage(result.error.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Offset.Zero
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(viewState.value) {
|
AnimatedVisibility(showPrimaryChangeAlertDialog) {
|
||||||
if (viewState.value.showSnackbarMessage) {
|
AlertDialog(
|
||||||
val result = snackbarHostState.showSnackbar(
|
onDismissRequest = { showPrimaryChangeAlertDialog = false },
|
||||||
message = viewState.value.snackbarMessage,
|
confirmButton = {
|
||||||
actionLabel = viewState.value.snackbarActionText,
|
TextButton(
|
||||||
duration = SnackbarDuration.Long,
|
onClick = {
|
||||||
)
|
viewModel.onDefaultTunnelChange(selectedTunnel)
|
||||||
when (result) {
|
showPrimaryChangeAlertDialog = false
|
||||||
SnackbarResult.ActionPerformed -> viewState.value.onSnackbarActionClick
|
|
||||||
SnackbarResult.Dismissed -> viewState.value.onSnackbarActionClick
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val pickFileLauncher = rememberLauncherForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult()
|
|
||||||
) { result ->
|
|
||||||
result.data?.data?.let { viewModel.onTunnelFileSelected(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
val scanLauncher = rememberLauncherForActivityResult(
|
|
||||||
contract = ScanContract(),
|
|
||||||
onResult = {
|
|
||||||
try {
|
|
||||||
viewModel.onTunnelQrResult(it.contents)
|
|
||||||
} catch (e : Exception) {
|
|
||||||
viewModel.showSnackBarMessage(context.getString(R.string.qr_result_failed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
modifier = Modifier.pointerInput(Unit) {
|
|
||||||
detectTapGestures(onTap = {
|
|
||||||
selectedTunnel = null
|
selectedTunnel = null
|
||||||
})
|
}) {
|
||||||
|
Text(text = stringResource(R.string.okay))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
floatingActionButtonPosition = FabPosition.End,
|
dismissButton = {
|
||||||
floatingActionButton = {
|
TextButton(onClick = { showPrimaryChangeAlertDialog = false }) {
|
||||||
AnimatedVisibility(
|
Text(text = stringResource(R.string.cancel))
|
||||||
visible = isVisible.value,
|
}
|
||||||
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
},
|
||||||
exit = slideOutVertically(targetOffsetY = { it * 2 }),
|
title = { Text(text = stringResource(R.string.primary_tunnel_change)) },
|
||||||
) {
|
text = { Text(text = stringResource(R.string.primary_tunnel_change_question)) })
|
||||||
FloatingActionButton(
|
}
|
||||||
modifier = Modifier.padding(bottom = 90.dp),
|
|
||||||
onClick = {
|
fun onTunnelToggle(checked: Boolean, tunnel: TunnelConfig) {
|
||||||
showBottomSheet = true
|
if (checked) viewModel.onTunnelStart(tunnel) else viewModel.onTunnelStop()
|
||||||
},
|
}
|
||||||
containerColor = MaterialTheme.colorScheme.secondary,
|
|
||||||
shape = RoundedCornerShape(16.dp),
|
Scaffold(
|
||||||
) {
|
modifier =
|
||||||
|
Modifier.pointerInput(Unit) {
|
||||||
|
detectTapGestures(
|
||||||
|
onTap = {
|
||||||
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) selectedTunnel = null
|
||||||
|
})
|
||||||
|
},
|
||||||
|
floatingActionButtonPosition = FabPosition.End,
|
||||||
|
topBar = {
|
||||||
|
if (uiState.settings.isAutoTunnelEnabled)
|
||||||
|
TopAppBar(
|
||||||
|
title = {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.requiredWidth(LocalConfiguration.current.screenWidthDp.dp).padding(end = 5.dp)) {
|
||||||
|
Row {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Bolt,
|
||||||
|
stringResource(id = R.string.auto),
|
||||||
|
modifier = Modifier.size(25.dp),
|
||||||
|
tint = if(uiState.settings.isAutoTunnelPaused) Color.Gray else mint)
|
||||||
|
Text(
|
||||||
|
"Auto-tunneling: ${if(uiState.settings.isAutoTunnelPaused) "paused" else "active" }",
|
||||||
|
style = typography.bodyLarge,
|
||||||
|
modifier = Modifier.padding(start = 10.dp))
|
||||||
|
}
|
||||||
|
if(uiState.settings.isAutoTunnelPaused) TextButton(
|
||||||
|
onClick = { viewModel.resumeAutoTunneling() },
|
||||||
|
modifier = Modifier.padding(end = 10.dp)) {
|
||||||
|
Text("Resume")
|
||||||
|
} else TextButton(
|
||||||
|
onClick = { viewModel.pauseAutoTunneling() },
|
||||||
|
modifier = Modifier.padding(end = 10.dp)) {
|
||||||
|
Text("Pause")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
},
|
||||||
|
floatingActionButton = {
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = isVisible.value,
|
||||||
|
enter = slideInVertically(initialOffsetY = { it * 2 }),
|
||||||
|
exit = slideOutVertically(targetOffsetY = { it * 2 })) {
|
||||||
|
val secondaryColor = MaterialTheme.colorScheme.secondary
|
||||||
|
val hoverColor = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)
|
||||||
|
var fobColor by remember { mutableStateOf(secondaryColor) }
|
||||||
|
FloatingActionButton(
|
||||||
|
modifier =
|
||||||
|
(if (WireGuardAutoTunnel.isRunningOnAndroidTv() &&
|
||||||
|
uiState.tunnels.isEmpty())
|
||||||
|
Modifier.focusRequester(focusRequester)
|
||||||
|
else Modifier)
|
||||||
|
.padding(bottom = 90.dp)
|
||||||
|
.onFocusChanged {
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
fobColor = if (it.isFocused) hoverColor else secondaryColor
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onClick = { showBottomSheet = true },
|
||||||
|
containerColor = fobColor,
|
||||||
|
shape = RoundedCornerShape(16.dp)) {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = Icons.Rounded.Add,
|
imageVector = Icons.Rounded.Add,
|
||||||
contentDescription = stringResource(id = R.string.add_tunnel),
|
contentDescription = stringResource(id = R.string.add_tunnel),
|
||||||
tint = Color.DarkGray,
|
tint = Color.DarkGray)
|
||||||
)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}) { innerPadding ->
|
||||||
) {
|
AnimatedVisibility(uiState.tunnels.isEmpty(), exit = fadeOut(), enter = fadeIn()) {
|
||||||
if (tunnels.isEmpty()) {
|
Column(
|
||||||
Column(
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
verticalArrangement = Arrangement.Center,
|
||||||
verticalArrangement = Arrangement.Center,
|
modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(padding)
|
|
||||||
) {
|
|
||||||
Text(text = stringResource(R.string.no_tunnels), fontStyle = FontStyle.Italic)
|
Text(text = stringResource(R.string.no_tunnels), fontStyle = FontStyle.Italic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (showBottomSheet) {
|
if (showBottomSheet) {
|
||||||
ModalBottomSheet(
|
ModalBottomSheet(
|
||||||
onDismissRequest = {
|
onDismissRequest = { showBottomSheet = false }, sheetState = sheetState) {
|
||||||
showBottomSheet = false
|
|
||||||
},
|
|
||||||
sheetState = sheetState
|
|
||||||
) {
|
|
||||||
// Sheet content
|
// Sheet content
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.fillMaxWidth()
|
Modifier.fillMaxWidth()
|
||||||
.clickable {
|
.clickable {
|
||||||
showBottomSheet = false
|
showBottomSheet = false
|
||||||
val fileSelectionIntent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
tunnelFileImportResultLauncher.launch(Constants.ALLOWED_FILE_TYPES)
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
|
||||||
type = Constants.ALLOWED_FILE_TYPES
|
|
||||||
}
|
}
|
||||||
if (fileSelectionIntent.resolveActivity(context.packageManager) != null) {
|
.padding(10.dp)) {
|
||||||
pickFileLauncher.launch(fileSelectionIntent)
|
Icon(
|
||||||
} else {
|
Icons.Filled.FileOpen,
|
||||||
viewModel.showSnackBarMessage(context.getString(R.string.no_file_app))
|
contentDescription = stringResource(id = R.string.open_file),
|
||||||
}
|
modifier = Modifier.padding(10.dp))
|
||||||
}
|
Text(
|
||||||
.padding(10.dp)
|
stringResource(id = R.string.add_tunnels_text),
|
||||||
) {
|
modifier = Modifier.padding(10.dp))
|
||||||
Icon(
|
}
|
||||||
Icons.Filled.FileOpen,
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
contentDescription = stringResource(id = R.string.open_file),
|
Divider()
|
||||||
modifier = Modifier.padding(10.dp)
|
Row(
|
||||||
)
|
modifier =
|
||||||
Text(
|
Modifier.fillMaxWidth()
|
||||||
stringResource(id = R.string.add_from_file),
|
.clickable {
|
||||||
modifier = Modifier.padding(10.dp)
|
scope.launch {
|
||||||
)
|
showBottomSheet = false
|
||||||
|
val scanOptions = ScanOptions()
|
||||||
|
scanOptions.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
||||||
|
scanOptions.setOrientationLocked(true)
|
||||||
|
scanOptions.setPrompt(context.getString(R.string.scanning_qr))
|
||||||
|
scanOptions.setBeepEnabled(false)
|
||||||
|
scanOptions.captureActivity = CaptureActivityPortrait::class.java
|
||||||
|
scanLauncher.launch(scanOptions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(10.dp)) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.QrCode,
|
||||||
|
contentDescription = stringResource(id = R.string.qr_scan),
|
||||||
|
modifier = Modifier.padding(10.dp))
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.add_from_qr),
|
||||||
|
modifier = Modifier.padding(10.dp))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Divider()
|
Divider()
|
||||||
Row(modifier = Modifier
|
Row(
|
||||||
.fillMaxWidth()
|
modifier =
|
||||||
.clickable {
|
Modifier.fillMaxWidth()
|
||||||
scope.launch {
|
.clickable {
|
||||||
showBottomSheet = false
|
showBottomSheet = false
|
||||||
val scanOptions = ScanOptions()
|
navController.navigate(
|
||||||
scanOptions.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
"${Screen.Config.route}/${Constants.MANUAL_TUNNEL_CONFIG_ID}")
|
||||||
scanOptions.setOrientationLocked(true)
|
}
|
||||||
scanOptions.setPrompt(context.getString(R.string.scanning_qr))
|
.padding(10.dp)) {
|
||||||
scanOptions.setBeepEnabled(false)
|
Icon(
|
||||||
scanOptions.captureActivity = CaptureActivityPortrait::class.java
|
Icons.Filled.Create,
|
||||||
scanLauncher.launch(scanOptions)
|
contentDescription = stringResource(id = R.string.create_import),
|
||||||
}
|
modifier = Modifier.padding(10.dp))
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.create_import),
|
||||||
|
modifier = Modifier.padding(10.dp))
|
||||||
}
|
}
|
||||||
.padding(10.dp)
|
}
|
||||||
) {
|
|
||||||
Icon(
|
|
||||||
Icons.Filled.QrCode,
|
|
||||||
contentDescription = stringResource(id = R.string.qr_scan),
|
|
||||||
modifier = Modifier.padding(10.dp)
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.add_from_qr),
|
|
||||||
modifier = Modifier.padding(10.dp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Column(
|
|
||||||
|
LazyColumn(
|
||||||
horizontalAlignment = Alignment.Start,
|
horizontalAlignment = Alignment.Start,
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
modifier = Modifier
|
modifier =
|
||||||
.fillMaxSize()
|
Modifier.fillMaxWidth().fillMaxHeight(.90f).overscroll(ScrollableDefaults.overscrollEffect()).padding(innerPadding),
|
||||||
.padding(padding)
|
state = rememberLazyListState(0, uiState.tunnels.count()),
|
||||||
) {
|
userScrollEnabled = true,
|
||||||
|
reverseLayout = true,
|
||||||
LazyColumn(
|
flingBehavior = ScrollableDefaults.flingBehavior()) {
|
||||||
modifier = Modifier
|
items(uiState.tunnels,
|
||||||
.fillMaxSize()
|
key = { tunnel -> tunnel.id }) { tunnel ->
|
||||||
.nestedScroll(nestedScrollConnection),
|
val leadingIconColor =
|
||||||
) {
|
(if (uiState.vpnState.name == tunnel.name &&
|
||||||
items(tunnels, key = { tunnel -> tunnel.id }) {tunnel ->
|
uiState.vpnState.status == Tunnel.State.UP) {
|
||||||
val focusRequester = remember { FocusRequester() }
|
uiState.vpnState.statistics
|
||||||
RowListItem(leadingIcon = Icons.Rounded.Circle,
|
?.mapPeerStats()
|
||||||
leadingIconColor = if (tunnelName == tunnel.name) when (handshakeStatus) {
|
?.map { it.value?.handshakeStatus() }
|
||||||
HandshakeStatus.HEALTHY -> mint
|
.let { statuses ->
|
||||||
HandshakeStatus.UNHEALTHY -> brickRed
|
when {
|
||||||
HandshakeStatus.NOT_STARTED -> Color.Gray
|
statuses?.all { it == HandshakeStatus.HEALTHY } == true -> mint
|
||||||
HandshakeStatus.NEVER_CONNECTED -> brickRed
|
statuses?.any { it == HandshakeStatus.STALE } == true -> corn
|
||||||
} else Color.Gray,
|
statuses?.all { it == HandshakeStatus.NOT_STARTED } == true ->
|
||||||
|
Color.Gray
|
||||||
|
else -> {
|
||||||
|
Color.Gray
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Color.Gray
|
||||||
|
})
|
||||||
|
val expanded = remember { mutableStateOf(false) }
|
||||||
|
RowListItem(
|
||||||
|
icon = {
|
||||||
|
if (uiState.settings.isTunnelConfigDefault(tunnel)) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Star,
|
||||||
|
stringResource(R.string.status),
|
||||||
|
tint = leadingIconColor,
|
||||||
|
modifier = Modifier.padding(end = 10.dp).size(20.dp))
|
||||||
|
} else {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Circle,
|
||||||
|
stringResource(R.string.status),
|
||||||
|
tint = leadingIconColor,
|
||||||
|
modifier = Modifier.padding(end = 15.dp).size(15.dp))
|
||||||
|
}
|
||||||
|
},
|
||||||
text = tunnel.name,
|
text = tunnel.name,
|
||||||
onHold = {
|
onHold = {
|
||||||
if (state == Tunnel.State.UP && tunnel.name == tunnelName) {
|
if ((uiState.vpnState.status == Tunnel.State.UP) &&
|
||||||
scope.launch {
|
(tunnel.name == uiState.vpnState.name)) {
|
||||||
viewModel.showSnackBarMessage(context.resources.getString(R.string.turn_off_tunnel))
|
showSnackbarMessage(Event.Message.TunnelOffAction.message)
|
||||||
}
|
return@RowListItem
|
||||||
return@RowListItem
|
}
|
||||||
}
|
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||||
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
|
selectedTunnel = tunnel
|
||||||
selectedTunnel = tunnel
|
|
||||||
},
|
},
|
||||||
onClick = {
|
onClick = {
|
||||||
if (!WireGuardAutoTunnel.isRunningOnAndroidTv(context)) {
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
navController.navigate("${Routes.Detail.name}/${tunnel.id}")
|
if (uiState.vpnState.status == Tunnel.State.UP &&
|
||||||
} else {
|
(uiState.vpnState.name == tunnel.name)) {
|
||||||
focusRequester.requestFocus()
|
expanded.value = !expanded.value
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
selectedTunnel = tunnel
|
||||||
|
focusRequester.requestFocus()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
statistics = uiState.vpnState.statistics,
|
||||||
|
expanded = expanded.value,
|
||||||
rowButton = {
|
rowButton = {
|
||||||
if (tunnel.id == selectedTunnel?.id) {
|
if (tunnel.id == selectedTunnel?.id &&
|
||||||
Row {
|
!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
IconButton(onClick = {
|
Row {
|
||||||
navController.navigate("${Routes.Config.name}/${selectedTunnel?.id}")
|
if (!uiState.settings.isTunnelConfigDefault(tunnel)) {
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (uiState.settings.isAutoTunnelEnabled && !uiState.settings.isAutoTunnelPaused) {
|
||||||
|
showSnackbarMessage(
|
||||||
|
Event.Message.AutoTunnelOffAction.message)
|
||||||
|
} else {
|
||||||
|
showPrimaryChangeAlertDialog = true
|
||||||
|
}
|
||||||
}) {
|
}) {
|
||||||
Icon(Icons.Rounded.Edit, stringResource(id = R.string.edit))
|
Icon(
|
||||||
|
Icons.Rounded.Star,
|
||||||
|
stringResource(id = R.string.set_primary))
|
||||||
}
|
}
|
||||||
IconButton(
|
}
|
||||||
modifier = Modifier.focusable(),
|
IconButton(
|
||||||
onClick = { viewModel.onDelete(tunnel) }) {
|
onClick = {
|
||||||
Icon(
|
if (uiState.settings.isAutoTunnelEnabled && uiState.settings.isTunnelConfigDefault(tunnel)
|
||||||
Icons.Rounded.Delete,
|
&& !uiState.settings.isAutoTunnelPaused) {
|
||||||
stringResource(id = R.string.delete)
|
showSnackbarMessage(
|
||||||
)
|
Event.Message.AutoTunnelOffAction.message)
|
||||||
}
|
} else navController.navigate(
|
||||||
}
|
"${Screen.Config.route}/${selectedTunnel?.id}")
|
||||||
} else {
|
}) {
|
||||||
if (WireGuardAutoTunnel.isRunningOnAndroidTv(context)) {
|
Icon(Icons.Rounded.Edit, stringResource(id = R.string.edit))
|
||||||
Row {
|
}
|
||||||
IconButton(
|
IconButton(
|
||||||
modifier = Modifier.focusRequester(focusRequester),
|
modifier = Modifier.focusable(),
|
||||||
onClick = {
|
onClick = { viewModel.onDelete(tunnel) }) {
|
||||||
navController.navigate("${Routes.Detail.name}/${tunnel.id}")
|
Icon(Icons.Rounded.Delete, stringResource(id = R.string.delete))
|
||||||
}) {
|
}
|
||||||
Icon(Icons.Rounded.Info, "Info")
|
|
||||||
}
|
|
||||||
IconButton(onClick = {
|
|
||||||
if (state == Tunnel.State.UP && tunnel.name == tunnelName)
|
|
||||||
scope.launch {
|
|
||||||
viewModel.showSnackBarMessage(
|
|
||||||
context.resources.getString(
|
|
||||||
R.string.turn_off_tunnel
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
navController.navigate("${Routes.Config.name}/${tunnel.id}")
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.Edit,
|
|
||||||
stringResource(id = R.string.edit)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
IconButton(onClick = {
|
|
||||||
if (state == Tunnel.State.UP && tunnel.name == tunnelName)
|
|
||||||
scope.launch {
|
|
||||||
viewModel.showSnackBarMessage(
|
|
||||||
context.resources.getString(
|
|
||||||
R.string.turn_off_tunnel
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
viewModel.onDelete(tunnel)
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
Icon(
|
|
||||||
Icons.Rounded.Delete,
|
|
||||||
stringResource(id = R.string.delete)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Switch(
|
|
||||||
checked = (state == Tunnel.State.UP && tunnel.name == tunnelName),
|
|
||||||
onCheckedChange = { checked ->
|
|
||||||
if (checked) viewModel.onTunnelStart(tunnel) else viewModel.onTunnelStop()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Switch(
|
|
||||||
checked = (state == Tunnel.State.UP && tunnel.name == tunnelName),
|
|
||||||
onCheckedChange = { checked ->
|
|
||||||
if (checked) viewModel.onTunnelStart(tunnel) else viewModel.onTunnelStop()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
val checked by remember {
|
||||||
|
derivedStateOf {
|
||||||
|
(uiState.vpnState.status == Tunnel.State.UP &&
|
||||||
|
tunnel.name == uiState.vpnState.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!checked) expanded.value = false
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun TunnelSwitch() =
|
||||||
|
Switch(
|
||||||
|
modifier = Modifier.focusRequester(focusRequester),
|
||||||
|
checked = checked,
|
||||||
|
onCheckedChange = { checked ->
|
||||||
|
if (!checked) expanded.value = false
|
||||||
|
onTunnelToggle(checked, tunnel)
|
||||||
|
})
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Row {
|
||||||
|
if (!uiState.settings.isTunnelConfigDefault(tunnel)) {
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (uiState.settings.isAutoTunnelEnabled) {
|
||||||
|
showSnackbarMessage(
|
||||||
|
Event.Message.AutoTunnelOffAction.message)
|
||||||
|
} else {
|
||||||
|
selectedTunnel = tunnel
|
||||||
|
showPrimaryChangeAlertDialog = true
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Star,
|
||||||
|
stringResource(id = R.string.set_primary))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
modifier = Modifier.focusRequester(focusRequester),
|
||||||
|
onClick = {
|
||||||
|
if (uiState.vpnState.status == Tunnel.State.UP &&
|
||||||
|
(uiState.vpnState.name == tunnel.name)) {
|
||||||
|
expanded.value = !expanded.value
|
||||||
|
} else {
|
||||||
|
showSnackbarMessage(Event.Message.TunnelOnAction.message)
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Icon(Icons.Rounded.Info, stringResource(R.string.info))
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (uiState.vpnState.status == Tunnel.State.UP &&
|
||||||
|
tunnel.name == uiState.vpnState.name) {
|
||||||
|
showSnackbarMessage(Event.Message.TunnelOffAction.message)
|
||||||
|
} else {
|
||||||
|
navController.navigate(
|
||||||
|
"${Screen.Config.route}/${tunnel.id}")
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Icon(Icons.Rounded.Edit, stringResource(id = R.string.edit))
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
onClick = {
|
||||||
|
if (uiState.vpnState.status == Tunnel.State.UP &&
|
||||||
|
tunnel.name == uiState.vpnState.name) {
|
||||||
|
showSnackbarMessage(Event.Message.TunnelOffAction.message)
|
||||||
|
} else {
|
||||||
|
viewModel.onDelete(tunnel)
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.Delete,
|
||||||
|
stringResource(id = R.string.delete))
|
||||||
|
}
|
||||||
|
TunnelSwitch()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TunnelSwitch()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.main
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.TunnelConfigs
|
||||||
|
|
||||||
|
data class MainUiState(
|
||||||
|
val settings : Settings = Settings(),
|
||||||
|
val tunnels : TunnelConfigs = emptyList(),
|
||||||
|
val vpnState: VpnState = VpnState(),
|
||||||
|
val loading : Boolean = true
|
||||||
|
)
|
||||||
+217
-225
@@ -7,263 +7,255 @@ import android.net.Uri
|
|||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.wireguard.config.BadConfigException
|
|
||||||
import com.wireguard.config.Config
|
import com.wireguard.config.Config
|
||||||
import com.zaneschepke.wireguardautotunnel.Constants
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.Settings
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceState
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceState
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService
|
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService
|
||||||
import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutsManager
|
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.ViewState
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.WireGuardTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
|
||||||
import com.zaneschepke.wireguardautotunnel.util.WgTunnelException
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
import java.util.zip.ZipInputStream
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class MainViewModel @Inject constructor(private val application : Application,
|
class MainViewModel
|
||||||
private val tunnelRepo : TunnelConfigDao,
|
@Inject
|
||||||
private val settingsRepo : SettingsDoa,
|
constructor(
|
||||||
private val vpnService: VpnService
|
private val application: Application,
|
||||||
|
private val tunnelConfigRepository: TunnelConfigRepository,
|
||||||
|
private val settingsRepository: SettingsRepository,
|
||||||
|
private val vpnService: VpnService
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _viewState = MutableStateFlow(ViewState())
|
val uiState =
|
||||||
val viewState get() = _viewState.asStateFlow()
|
combine(
|
||||||
val tunnels get() = tunnelRepo.getAllFlow()
|
settingsRepository.getSettingsFlow(),
|
||||||
val state get() = vpnService.state
|
tunnelConfigRepository.getTunnelConfigsFlow(),
|
||||||
|
vpnService.vpnState,
|
||||||
|
) { settings, tunnels, vpnState ->
|
||||||
|
validateWatcherServiceState(settings)
|
||||||
|
MainUiState(settings, tunnels, vpnState, false)
|
||||||
|
}
|
||||||
|
.stateIn(
|
||||||
|
viewModelScope,
|
||||||
|
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||||
|
MainUiState())
|
||||||
|
|
||||||
val handshakeStatus get() = vpnService.handshakeStatus
|
private fun validateWatcherServiceState(settings: Settings) = viewModelScope.launch(Dispatchers.IO) {
|
||||||
val tunnelName get() = vpnService.tunnelName
|
val watcherState =
|
||||||
private val _settings = MutableStateFlow(Settings())
|
ServiceManager.getServiceState(
|
||||||
val settings get() = _settings.asStateFlow()
|
application.applicationContext, WireGuardConnectivityWatcherService::class.java)
|
||||||
|
if (settings.isAutoTunnelEnabled &&
|
||||||
|
watcherState == ServiceState.STOPPED) {
|
||||||
|
ServiceManager.startWatcherService(application.applicationContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
private fun stopWatcherService() = viewModelScope.launch(Dispatchers.IO) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
ServiceManager.stopWatcherService(application.applicationContext)
|
||||||
settingsRepo.getAllFlow().filter { it.isNotEmpty() }.collect {
|
}
|
||||||
val settings = it.first()
|
fun onDelete(tunnel: TunnelConfig) {
|
||||||
validateWatcherServiceState(settings)
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
_settings.emit(settings)
|
if (tunnelConfigRepository.count() == 1) {
|
||||||
}
|
stopWatcherService()
|
||||||
|
val settings = settingsRepository.getSettings()
|
||||||
|
settings.defaultTunnel = null
|
||||||
|
settings.isAutoTunnelEnabled = false
|
||||||
|
settings.isAlwaysOnVpnEnabled = false
|
||||||
|
saveSettings(settings)
|
||||||
|
}
|
||||||
|
tunnelConfigRepository.delete(tunnel)
|
||||||
|
WireGuardAutoTunnel.requestTileServiceStateUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onTunnelStart(tunnelConfig: TunnelConfig) = viewModelScope.launch(Dispatchers.IO) {
|
||||||
|
stopActiveTunnel().await()
|
||||||
|
startTunnel(tunnelConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startTunnel(tunnelConfig: TunnelConfig) = viewModelScope.launch(Dispatchers.IO) {
|
||||||
|
ServiceManager.startVpnService(application.applicationContext, tunnelConfig.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopActiveTunnel() =
|
||||||
|
viewModelScope.async(Dispatchers.IO) {
|
||||||
|
if (ServiceManager.getServiceState(
|
||||||
|
application.applicationContext, WireGuardTunnelService::class.java) ==
|
||||||
|
ServiceState.STARTED) {
|
||||||
|
onTunnelStop()
|
||||||
|
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun validateWatcherServiceState(settings: Settings) {
|
fun onTunnelStop() = viewModelScope.launch(Dispatchers.IO) {
|
||||||
val watcherState = ServiceManager.getServiceState(application.applicationContext, WireGuardConnectivityWatcherService::class.java)
|
ServiceManager.stopVpnService(application.applicationContext)
|
||||||
if(settings.isAutoTunnelEnabled && watcherState == ServiceState.STOPPED && settings.defaultTunnel != null) {
|
}
|
||||||
ServiceManager.startWatcherService(application.applicationContext, settings.defaultTunnel!!)
|
|
||||||
|
private fun validateConfigString(config: String) {
|
||||||
|
TunnelConfig.configFromQuick(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun onTunnelQrResult(result: String) : Result<Unit> {
|
||||||
|
return try {
|
||||||
|
validateConfigString(result)
|
||||||
|
val tunnelConfig =
|
||||||
|
TunnelConfig(name = NumberUtils.generateRandomTunnelName(), wgQuick = result)
|
||||||
|
addTunnel(tunnelConfig)
|
||||||
|
Result.Success(Unit)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.Error(Event.Error.InvalidQrCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun saveTunnelConfigFromStream(stream: InputStream, fileName: String) {
|
||||||
|
val bufferReader = stream.bufferedReader(charset = Charsets.UTF_8)
|
||||||
|
val config = Config.parse(bufferReader)
|
||||||
|
val tunnelName = getNameFromFileName(fileName)
|
||||||
|
addTunnel(TunnelConfig(name = tunnelName, wgQuick = config.toWgQuickString()))
|
||||||
|
withContext(Dispatchers.IO) { stream.close() }
|
||||||
|
}
|
||||||
|
|
||||||
fun onDelete(tunnel : TunnelConfig) {
|
private fun getInputStreamFromUri(uri: Uri): InputStream? {
|
||||||
viewModelScope.launch {
|
return application.applicationContext.contentResolver.openInputStream(uri)
|
||||||
if(tunnelRepo.count() == 1L) {
|
}
|
||||||
ServiceManager.stopWatcherService(application.applicationContext)
|
|
||||||
val settings = settingsRepo.getAll()
|
|
||||||
if(settings.isNotEmpty()) {
|
|
||||||
val setting = settings[0]
|
|
||||||
setting.defaultTunnel = null
|
|
||||||
setting.isAutoTunnelEnabled = false
|
|
||||||
setting.isAlwaysOnVpnEnabled = false
|
|
||||||
settingsRepo.save(setting)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tunnelRepo.delete(tunnel)
|
|
||||||
ShortcutsManager.removeTunnelShortcuts(application.applicationContext, tunnel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelStart(tunnelConfig : TunnelConfig) {
|
suspend fun onTunnelFileSelected(uri: Uri) : Result<Unit> {
|
||||||
viewModelScope.launch {
|
|
||||||
stopActiveTunnel()
|
|
||||||
startTunnel(tunnelConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun startTunnel(tunnelConfig: TunnelConfig) {
|
|
||||||
ServiceManager.startVpnService(application.applicationContext, tunnelConfig.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun stopActiveTunnel() {
|
|
||||||
if(ServiceManager.getServiceState(application.applicationContext,
|
|
||||||
WireGuardTunnelService::class.java, ) == ServiceState.STARTED) {
|
|
||||||
onTunnelStop()
|
|
||||||
delay(Constants.TOGGLE_TUNNEL_DELAY)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelStop() {
|
|
||||||
ServiceManager.stopVpnService(application.applicationContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun validateConfigString(config : String) {
|
|
||||||
if(!config.contains(application.getString(R.string.config_validation))) {
|
|
||||||
throw WgTunnelException(application.getString(R.string.config_validation))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelQrResult(result : String) {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
try {
|
|
||||||
validateConfigString(result)
|
|
||||||
val tunnelConfig = TunnelConfig(name = NumberUtils.generateRandomTunnelName(), wgQuick = result)
|
|
||||||
addTunnel(tunnelConfig)
|
|
||||||
} catch (e : WgTunnelException) {
|
|
||||||
showSnackBarMessage(e.message ?: application.getString(R.string.unknown_error_message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun validateFileExtension(fileName : String) {
|
|
||||||
val extension = getFileExtensionFromFileName(fileName)
|
|
||||||
if(extension != Constants.VALID_FILE_EXTENSION) {
|
|
||||||
throw WgTunnelException(application.getString(R.string.file_extension_message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveTunnelConfigFromStream(stream : InputStream, fileName : String) {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
val bufferReader = stream.bufferedReader(charset = Charsets.UTF_8)
|
|
||||||
val config = Config.parse(bufferReader)
|
|
||||||
val tunnelName = getNameFromFileName(fileName)
|
|
||||||
addTunnel(TunnelConfig(name = tunnelName, wgQuick = config.toWgQuickString()))
|
|
||||||
stream.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getInputStreamFromUri(uri: Uri): InputStream {
|
|
||||||
return application.applicationContext.contentResolver.openInputStream(uri)
|
|
||||||
?: throw WgTunnelException(application.getString(R.string.stream_failed))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onTunnelFileSelected(uri : Uri) {
|
|
||||||
try {
|
try {
|
||||||
val fileName = getFileName(application.applicationContext, uri)
|
if(isValidUriContentScheme(uri)){
|
||||||
validateFileExtension(fileName)
|
val fileName = getFileName(application.applicationContext, uri)
|
||||||
val stream = getInputStreamFromUri(uri)
|
when (getFileExtensionFromFileName(fileName)) {
|
||||||
saveTunnelConfigFromStream(stream, fileName)
|
Constants.CONF_FILE_EXTENSION -> saveTunnelFromConfUri(fileName, uri).let {
|
||||||
} catch (e : Exception) {
|
when(it) {
|
||||||
showExceptionMessage(e)
|
is Result.Error -> return Result.Error(Event.Error.FileReadFailed)
|
||||||
}
|
is Result.Success -> return it
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showExceptionMessage(e : Exception) {
|
|
||||||
when(e) {
|
|
||||||
is BadConfigException -> {
|
|
||||||
showSnackBarMessage(application.getString(R.string.bad_config))
|
|
||||||
}
|
|
||||||
is WgTunnelException -> {
|
|
||||||
showSnackBarMessage(e.message ?: application.getString(R.string.unknown_error_message))
|
|
||||||
}
|
|
||||||
else -> showSnackBarMessage(application.getString(R.string.unknown_error_message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun addTunnel(tunnelConfig: TunnelConfig) {
|
|
||||||
saveTunnel(tunnelConfig)
|
|
||||||
createTunnelAppShortcuts(tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun saveTunnel(tunnelConfig : TunnelConfig) {
|
|
||||||
tunnelRepo.save(tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createTunnelAppShortcuts(tunnelConfig: TunnelConfig) {
|
|
||||||
ShortcutsManager.createTunnelShortcuts(application.applicationContext, tunnelConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getFileNameByCursor(context: Context, uri: Uri) : String {
|
|
||||||
val cursor = context.contentResolver.query(uri, null, null, null, null)
|
|
||||||
if(cursor != null) {
|
|
||||||
cursor.use {
|
|
||||||
return getDisplayNameByCursor(it)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw WgTunnelException("Failed to initialize cursor")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getDisplayNameColumnIndex(cursor: Cursor) : Int {
|
|
||||||
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
|
||||||
if(columnIndex == -1) {
|
|
||||||
throw WgTunnelException("Cursor out of bounds")
|
|
||||||
}
|
|
||||||
return columnIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getDisplayNameByCursor(cursor: Cursor) : String {
|
|
||||||
if(cursor.moveToFirst()) {
|
|
||||||
val index = getDisplayNameColumnIndex(cursor)
|
|
||||||
return cursor.getString(index)
|
|
||||||
} else {
|
|
||||||
throw WgTunnelException("Cursor failed to move to first")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun validateUriContentScheme(uri : Uri) {
|
|
||||||
if (uri.scheme != Constants.URI_CONTENT_SCHEME) {
|
|
||||||
throw WgTunnelException(application.getString(R.string.file_extension_message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun getFileName(context: Context, uri: Uri): String {
|
|
||||||
validateUriContentScheme(uri)
|
|
||||||
return try {
|
|
||||||
getFileNameByCursor(context, uri)
|
|
||||||
} catch (_: Exception) {
|
|
||||||
NumberUtils.generateRandomTunnelName()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showSnackBarMessage(message : String) {
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
|
||||||
_viewState.emit(_viewState.value.copy(
|
|
||||||
showSnackbarMessage = true,
|
|
||||||
snackbarMessage = message,
|
|
||||||
snackbarActionText = application.getString(R.string.okay),
|
|
||||||
onSnackbarActionClick = {
|
|
||||||
viewModelScope.launch {
|
|
||||||
dismissSnackBar()
|
|
||||||
}
|
}
|
||||||
|
Constants.ZIP_FILE_EXTENSION -> saveTunnelsFromZipUri(uri)
|
||||||
|
else -> return Result.Error(Event.Error.InvalidFileExtension)
|
||||||
}
|
}
|
||||||
))
|
return Result.Success(Unit)
|
||||||
delay(Constants.SNACKBAR_DELAY)
|
} else {
|
||||||
dismissSnackBar()
|
return Result.Error(Event.Error.InvalidFileExtension)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
return Result.Error(Event.Error.FileReadFailed)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun saveTunnelsFromZipUri(uri: Uri) {
|
||||||
|
ZipInputStream(getInputStreamFromUri(uri)).use { zip ->
|
||||||
|
generateSequence { zip.nextEntry }
|
||||||
|
.filterNot {
|
||||||
|
it.isDirectory || getFileExtensionFromFileName(it.name) != Constants.CONF_FILE_EXTENSION
|
||||||
|
}
|
||||||
|
.forEach {
|
||||||
|
val name = getNameFromFileName(it.name)
|
||||||
|
val config = Config.parse(zip)
|
||||||
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
|
addTunnel(TunnelConfig(name = name, wgQuick = config.toWgQuickString()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun saveTunnelFromConfUri(name: String, uri: Uri) : Result<Unit> {
|
||||||
|
val stream = getInputStreamFromUri(uri)
|
||||||
|
return if(stream != null) {
|
||||||
|
saveTunnelConfigFromStream(stream, name)
|
||||||
|
Result.Success(Unit)
|
||||||
|
} else {
|
||||||
|
Result.Error(Event.Error.FileReadFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun addTunnel(tunnelConfig: TunnelConfig) {
|
||||||
|
saveTunnel(tunnelConfig)
|
||||||
|
WireGuardAutoTunnel.requestTileServiceStateUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pauseAutoTunneling() = viewModelScope.launch {
|
||||||
|
settingsRepository.save(uiState.value.settings.copy(isAutoTunnelPaused = true))
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun dismissSnackBar() {
|
fun resumeAutoTunneling() = viewModelScope.launch {
|
||||||
_viewState.emit(_viewState.value.copy(
|
settingsRepository.save(uiState.value.settings.copy(isAutoTunnelPaused = false))
|
||||||
showSnackbarMessage = false
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getNameFromFileName(fileName : String) : String {
|
private suspend fun saveTunnel(tunnelConfig: TunnelConfig) {
|
||||||
return fileName.substring(0 , fileName.lastIndexOf('.') )
|
tunnelConfigRepository.save(tunnelConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getFileExtensionFromFileName(fileName : String) : String {
|
private fun getFileNameByCursor(context: Context, uri: Uri): String? {
|
||||||
return try {
|
context.contentResolver.query(uri, null, null, null, null)?.use {
|
||||||
fileName.substring(fileName.lastIndexOf('.'))
|
return getDisplayNameByCursor(it)
|
||||||
} catch (e : Exception) {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getDisplayNameColumnIndex(cursor: Cursor): Int? {
|
||||||
|
val columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||||
|
return if (columnIndex != -1) {
|
||||||
|
return columnIndex
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getDisplayNameByCursor(cursor: Cursor): String? {
|
||||||
|
return if (cursor.moveToFirst()) {
|
||||||
|
val index = getDisplayNameColumnIndex(cursor)
|
||||||
|
if (index != null) {
|
||||||
|
cursor.getString(index)
|
||||||
|
} else null
|
||||||
|
} else null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isValidUriContentScheme(uri: Uri): Boolean {
|
||||||
|
return uri.scheme == Constants.URI_CONTENT_SCHEME
|
||||||
|
}
|
||||||
|
private fun getFileName(context: Context, uri: Uri): String {
|
||||||
|
return getFileNameByCursor(context, uri) ?: NumberUtils.generateRandomTunnelName()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getNameFromFileName(fileName: String): String {
|
||||||
|
return fileName.substring(0, fileName.lastIndexOf('.'))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getFileExtensionFromFileName(fileName: String): String {
|
||||||
|
return try {
|
||||||
|
fileName.substring(fileName.lastIndexOf('.'))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveSettings(settings: Settings) =
|
||||||
|
viewModelScope.launch(Dispatchers.IO) { settingsRepository.save(settings) }
|
||||||
|
|
||||||
|
fun onDefaultTunnelChange(selectedTunnel: TunnelConfig?) = viewModelScope.launch {
|
||||||
|
if (selectedTunnel != null) {
|
||||||
|
saveSettings(uiState.value.settings.copy(defaultTunnel = selectedTunnel.toString())).join()
|
||||||
|
WireGuardAutoTunnel.requestTileServiceStateUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+426
-352
@@ -4,21 +4,25 @@ import android.Manifest
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.Settings
|
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
||||||
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
import androidx.compose.foundation.layout.FlowRow
|
import androidx.compose.foundation.layout.FlowRow
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxHeight
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
@@ -26,22 +30,16 @@ import androidx.compose.material.icons.Icons
|
|||||||
import androidx.compose.material.icons.filled.Close
|
import androidx.compose.material.icons.filled.Close
|
||||||
import androidx.compose.material.icons.outlined.Add
|
import androidx.compose.material.icons.outlined.Add
|
||||||
import androidx.compose.material.icons.rounded.LocationOff
|
import androidx.compose.material.icons.rounded.LocationOff
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.AlertDialog
|
||||||
import androidx.compose.material3.DropdownMenuItem
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.ExposedDropdownMenuBox
|
|
||||||
import androidx.compose.material3.ExposedDropdownMenuDefaults
|
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
import androidx.compose.material3.SnackbarDuration
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.SnackbarHostState
|
|
||||||
import androidx.compose.material3.SnackbarResult
|
|
||||||
import androidx.compose.material3.Switch
|
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.material3.TextField
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.SideEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
@@ -50,6 +48,7 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
|
import androidx.compose.ui.focus.focusProperties
|
||||||
import androidx.compose.ui.focus.focusRequester
|
import androidx.compose.ui.focus.focusRequester
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
@@ -63,372 +62,447 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.navigation.NavController
|
|
||||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||||
import com.google.accompanist.permissions.isGranted
|
import com.google.accompanist.permissions.isGranted
|
||||||
import com.google.accompanist.permissions.rememberPermissionState
|
import com.google.accompanist.permissions.rememberPermissionState
|
||||||
|
import com.wireguard.android.backend.Tunnel
|
||||||
|
import com.wireguard.android.backend.WgQuickBackend
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.Routes
|
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton
|
import com.zaneschepke.wireguardautotunnel.ui.common.ClickableIconButton
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationToggle
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.text.SectionTitle
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.FileUtils
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class,
|
@OptIn(
|
||||||
ExperimentalLayoutApi::class
|
ExperimentalPermissionsApi::class,
|
||||||
)
|
ExperimentalLayoutApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun SettingsScreen(
|
fun SettingsScreen(
|
||||||
viewModel: SettingsViewModel = hiltViewModel(),
|
viewModel: SettingsViewModel = hiltViewModel(),
|
||||||
padding: PaddingValues,
|
padding: PaddingValues,
|
||||||
navController: NavController,
|
showSnackbarMessage: (String) -> Unit,
|
||||||
focusRequester: FocusRequester,
|
focusRequester: FocusRequester
|
||||||
snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }
|
|
||||||
) {
|
) {
|
||||||
|
val scope = rememberCoroutineScope { Dispatchers.IO }
|
||||||
|
val context = LocalContext.current
|
||||||
|
val focusManager = LocalFocusManager.current
|
||||||
|
val scrollState = rememberScrollState()
|
||||||
|
val interactionSource = remember { MutableInteractionSource() }
|
||||||
|
|
||||||
val scope = rememberCoroutineScope()
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val context = LocalContext.current
|
|
||||||
val focusManager = LocalFocusManager.current
|
|
||||||
val interactionSource = remember { MutableInteractionSource() }
|
|
||||||
|
|
||||||
var expanded by remember { mutableStateOf(false) }
|
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
val viewState by viewModel.viewState.collectAsStateWithLifecycle()
|
var currentText by remember { mutableStateOf("") }
|
||||||
val settings by viewModel.settings.collectAsStateWithLifecycle()
|
var isBackgroundLocationGranted by remember { mutableStateOf(true) }
|
||||||
val trustedSSIDs by viewModel.trustedSSIDs.collectAsStateWithLifecycle()
|
var showLocationServicesAlertDialog by remember { mutableStateOf(false) }
|
||||||
val tunnels by viewModel.tunnels.collectAsStateWithLifecycle(mutableListOf())
|
var didExportFiles by remember { mutableStateOf(false) }
|
||||||
val fineLocationState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
|
var showAuthPrompt by remember { mutableStateOf(false) }
|
||||||
var currentText by remember { mutableStateOf("") }
|
val focusRequester2 = remember { FocusRequester() }
|
||||||
val scrollState = rememberScrollState()
|
|
||||||
var isLocationServicesEnabled by remember { mutableStateOf(viewModel.checkLocationServicesEnabled())}
|
|
||||||
|
|
||||||
LaunchedEffect(viewState) {
|
val screenPadding = 5.dp
|
||||||
if (viewState.showSnackbarMessage) {
|
val fillMaxWidth = .85f
|
||||||
val result = snackbarHostState.showSnackbar(
|
|
||||||
message = viewState.snackbarMessage,
|
if (uiState.loading) {
|
||||||
actionLabel = viewState.snackbarActionText,
|
LoadingScreen()
|
||||||
duration = SnackbarDuration.Long,
|
return
|
||||||
)
|
}
|
||||||
when (result) {
|
|
||||||
SnackbarResult.ActionPerformed -> viewState.onSnackbarActionClick
|
fun exportAllConfigs() {
|
||||||
SnackbarResult.Dismissed -> viewState.onSnackbarActionClick
|
try {
|
||||||
}
|
val files = uiState.tunnels.map { File(context.cacheDir, "${it.name}.conf") }
|
||||||
}
|
files.forEachIndexed { index, file ->
|
||||||
|
file.outputStream().use { it.write(uiState.tunnels[index].wgQuick.toByteArray()) }
|
||||||
|
}
|
||||||
|
FileUtils.saveFilesToZip(context, files)
|
||||||
|
didExportFiles = true
|
||||||
|
showSnackbarMessage(Event.Message.ConfigsExported.message)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showSnackbarMessage(Event.Error.Exception(e).message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun saveTrustedSSID() {
|
fun saveTrustedSSID() {
|
||||||
if (currentText.isNotEmpty()) {
|
if (currentText.isNotEmpty()) {
|
||||||
scope.launch {
|
viewModel.onSaveTrustedSSID(currentText).let {
|
||||||
viewModel.onSaveTrustedSSID(currentText)
|
when(it) {
|
||||||
currentText = ""
|
is Result.Success -> currentText = ""
|
||||||
}
|
is Result.Error -> showSnackbarMessage(it.error.message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun openSettings() {
|
fun openSettings() {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
val intentSettings =
|
val intentSettings = Intent(ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
intentSettings.data = Uri.fromParts("package", context.packageName, null)
|
||||||
intentSettings.data =
|
context.startActivity(intentSettings)
|
||||||
Uri.fromParts("package", context.packageName, null)
|
|
||||||
context.startActivity(intentSettings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
val backgroundLocationState =
|
|
||||||
rememberPermissionState(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
|
|
||||||
if(!backgroundLocationState.status.isGranted) {
|
|
||||||
Column(horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.verticalScroll(scrollState)
|
|
||||||
.padding(padding)) {
|
|
||||||
Icon(Icons.Rounded.LocationOff, contentDescription = stringResource(id = R.string.map), modifier = Modifier
|
|
||||||
.padding(30.dp)
|
|
||||||
.size(128.dp))
|
|
||||||
Text(stringResource(R.string.prominent_background_location_title), textAlign = TextAlign.Center, modifier = Modifier.padding(30.dp), fontSize = 20.sp)
|
|
||||||
Text(stringResource(R.string.prominent_background_location_message), textAlign = TextAlign.Center, modifier = Modifier.padding(30.dp), fontSize = 15.sp)
|
|
||||||
Row(
|
|
||||||
modifier = if(WireGuardAutoTunnel.isRunningOnAndroidTv(context)) Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(10.dp) else Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(30.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceEvenly
|
|
||||||
) {
|
|
||||||
Button(onClick = {
|
|
||||||
navController.navigate(Routes.Main.name)
|
|
||||||
}) {
|
|
||||||
Text(stringResource(id = R.string.no_thanks))
|
|
||||||
}
|
|
||||||
Button(modifier = Modifier.focusRequester(focusRequester), onClick = {
|
|
||||||
openSettings()
|
|
||||||
}) {
|
|
||||||
Text(stringResource(id = R.string.turn_on))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!fineLocationState.status.isGranted) {
|
fun checkFineLocationGranted() {
|
||||||
Column(
|
isBackgroundLocationGranted =
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
if (!fineLocationState.status.isGranted) {
|
||||||
verticalArrangement = Arrangement.Center,
|
false
|
||||||
modifier = Modifier
|
} else {
|
||||||
.fillMaxSize()
|
viewModel.setLocationDisclosureShown()
|
||||||
.padding(padding)
|
true
|
||||||
) {
|
}
|
||||||
Text(
|
}
|
||||||
stringResource(id = R.string.precise_location_message),
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
modifier = Modifier.padding(15.dp),
|
|
||||||
fontStyle = FontStyle.Italic
|
|
||||||
)
|
|
||||||
Button(modifier = Modifier.focusRequester(focusRequester),onClick = {
|
|
||||||
fineLocationState.launchPermissionRequest()
|
|
||||||
}) {
|
|
||||||
Text(stringResource(id = R.string.request))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
return
|
if(WireGuardAutoTunnel.isRunningOnAndroidTv() && Build.VERSION.SDK_INT == Build.VERSION_CODES.Q){
|
||||||
}
|
checkFineLocationGranted()
|
||||||
|
} else {
|
||||||
|
val backgroundLocationState =
|
||||||
|
rememberPermissionState(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
|
||||||
|
isBackgroundLocationGranted =
|
||||||
|
if (!backgroundLocationState.status.isGranted) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
SideEffect { viewModel.setLocationDisclosureShown() }
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (tunnels.isEmpty()) {
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
|
||||||
Column(
|
checkFineLocationGranted()
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
}
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
modifier = Modifier
|
AnimatedVisibility(showLocationServicesAlertDialog) {
|
||||||
.fillMaxSize()
|
AlertDialog(
|
||||||
.padding(padding)
|
onDismissRequest = { showLocationServicesAlertDialog = false },
|
||||||
) {
|
confirmButton = {
|
||||||
Text(
|
TextButton(
|
||||||
stringResource(R.string.one_tunnel_required),
|
onClick = {
|
||||||
textAlign = TextAlign.Center,
|
showLocationServicesAlertDialog = false
|
||||||
modifier = Modifier.padding(15.dp),
|
|
||||||
fontStyle = FontStyle.Italic
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if(!isLocationServicesEnabled && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
|
||||||
verticalArrangement = Arrangement.Center,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(padding)
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.location_services_not_detected),
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
modifier = Modifier.padding(15.dp),
|
|
||||||
fontStyle = FontStyle.Italic
|
|
||||||
)
|
|
||||||
Button(modifier = Modifier.focusRequester(focusRequester), onClick = {
|
|
||||||
val locationServicesEnabled = viewModel.checkLocationServicesEnabled()
|
|
||||||
isLocationServicesEnabled = locationServicesEnabled
|
|
||||||
if(!locationServicesEnabled) {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.showSnackBarMessage(context.getString(R.string.detecting_location_services_disabled))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
Text(stringResource(id = R.string.check_again))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val screenPadding = if(WireGuardAutoTunnel.isRunningOnAndroidTv(context)) 5.dp else 15.dp
|
|
||||||
Column(
|
|
||||||
horizontalAlignment = Alignment.Start,
|
|
||||||
verticalArrangement = Arrangement.Top,
|
|
||||||
modifier = if(WireGuardAutoTunnel.isRunningOnAndroidTv(context)) Modifier
|
|
||||||
.fillMaxHeight(.85f)
|
|
||||||
.fillMaxWidth()
|
|
||||||
.verticalScroll(scrollState)
|
|
||||||
.clickable(indication = null, interactionSource = interactionSource) {
|
|
||||||
focusManager.clearFocus()
|
|
||||||
}
|
|
||||||
.padding(padding) else Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.verticalScroll(scrollState)
|
|
||||||
.clickable(indication = null, interactionSource = interactionSource) {
|
|
||||||
focusManager.clearFocus()
|
|
||||||
}
|
|
||||||
.padding(padding)
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(screenPadding),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Text(stringResource(R.string.enable_auto_tunnel))
|
|
||||||
Switch(
|
|
||||||
modifier = Modifier.focusRequester(focusRequester),
|
|
||||||
enabled = !settings.isAlwaysOnVpnEnabled,
|
|
||||||
checked = settings.isAutoTunnelEnabled,
|
|
||||||
onCheckedChange = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.toggleAutoTunnel()
|
viewModel.toggleAutoTunnel()
|
||||||
}
|
}) {
|
||||||
}
|
Text(text = stringResource(R.string.okay))
|
||||||
)
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
stringResource(id = R.string.select_tunnel),
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
modifier = Modifier.padding(screenPadding, bottom = 5.dp, top = 5.dp)
|
|
||||||
)
|
|
||||||
ExposedDropdownMenuBox(
|
|
||||||
expanded = expanded,
|
|
||||||
onExpandedChange = {
|
|
||||||
if(!(settings.isAutoTunnelEnabled || settings.isAlwaysOnVpnEnabled)) {
|
|
||||||
expanded = !expanded }},
|
|
||||||
modifier = Modifier.padding(start = 15.dp, top = 5.dp, bottom = 10.dp).clickable {
|
|
||||||
expanded = !expanded
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
TextField(
|
|
||||||
enabled = !(settings.isAutoTunnelEnabled || settings.isAlwaysOnVpnEnabled),
|
|
||||||
value = settings.defaultTunnel?.let {
|
|
||||||
TunnelConfig.from(it).name }
|
|
||||||
?: "",
|
|
||||||
readOnly = true,
|
|
||||||
modifier = Modifier.menuAnchor(),
|
|
||||||
label = { Text(stringResource(R.string.tunnels)) },
|
|
||||||
onValueChange = { },
|
|
||||||
trailingIcon = {
|
|
||||||
ExposedDropdownMenuDefaults.TrailingIcon(
|
|
||||||
expanded = expanded
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
ExposedDropdownMenu(
|
|
||||||
expanded = expanded,
|
|
||||||
onDismissRequest = {
|
|
||||||
expanded = false
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
tunnels.forEach() { tunnel ->
|
|
||||||
DropdownMenuItem(
|
|
||||||
onClick = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onDefaultTunnelSelected(tunnel)
|
|
||||||
}
|
|
||||||
expanded = false
|
|
||||||
},
|
|
||||||
text = { Text(text = tunnel.name) }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.trusted_ssid),
|
|
||||||
textAlign = TextAlign.Center,
|
|
||||||
modifier = Modifier.padding(screenPadding, bottom = 5.dp, top = 5.dp)
|
|
||||||
)
|
|
||||||
FlowRow(
|
|
||||||
modifier = Modifier.padding(screenPadding),
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
|
||||||
verticalArrangement = Arrangement.SpaceEvenly
|
|
||||||
) {
|
|
||||||
trustedSSIDs.forEach { ssid ->
|
|
||||||
ClickableIconButton(onIconClick = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onDeleteTrustedSSID(ssid)
|
|
||||||
}
|
|
||||||
}, text = ssid, icon = Icons.Filled.Close, enabled = !(settings.isAutoTunnelEnabled || settings.isAlwaysOnVpnEnabled))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
OutlinedTextField(
|
|
||||||
enabled = !(settings.isAutoTunnelEnabled || settings.isAlwaysOnVpnEnabled),
|
|
||||||
value = currentText,
|
|
||||||
onValueChange = { currentText = it },
|
|
||||||
label = { Text(stringResource(R.string.add_trusted_ssid)) },
|
|
||||||
modifier = Modifier.padding(start = screenPadding, top = 5.dp),
|
|
||||||
maxLines = 1,
|
|
||||||
keyboardOptions = KeyboardOptions(
|
|
||||||
capitalization = KeyboardCapitalization.None,
|
|
||||||
imeAction = ImeAction.Done
|
|
||||||
),
|
|
||||||
keyboardActions = KeyboardActions(
|
|
||||||
onDone = {
|
|
||||||
saveTrustedSSID()
|
|
||||||
}
|
|
||||||
),
|
|
||||||
trailingIcon = {
|
|
||||||
IconButton(onClick = { saveTrustedSSID() }) {
|
|
||||||
Icon(
|
|
||||||
imageVector = Icons.Outlined.Add,
|
|
||||||
contentDescription = if (currentText == "") stringResource(id = R.string.trusted_ssid_empty_description) else stringResource(
|
|
||||||
id = R.string.trusted_ssid_value_description
|
|
||||||
),
|
|
||||||
tint = if(currentText == "") Color.Transparent else Color.Green
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
dismissButton = {
|
||||||
Row(
|
TextButton(onClick = { showLocationServicesAlertDialog = false }) {
|
||||||
modifier = Modifier
|
Text(text = stringResource(R.string.cancel))
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(screenPadding),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Text(stringResource(R.string.tunnel_mobile_data))
|
|
||||||
Switch(
|
|
||||||
enabled = !(settings.isAutoTunnelEnabled || settings.isAlwaysOnVpnEnabled),
|
|
||||||
checked = settings.isTunnelOnMobileDataEnabled,
|
|
||||||
onCheckedChange = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onToggleTunnelOnMobileData()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
},
|
||||||
}
|
title = { Text(text = stringResource(R.string.location_services_not_detected)) },
|
||||||
Row(
|
text = { Text(text = stringResource(R.string.location_services_missing_message)) })
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(screenPadding),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Text("Tunnel on Ethernet")
|
|
||||||
Switch(
|
|
||||||
enabled = !settings.isAutoTunnelEnabled,
|
|
||||||
checked = settings.isTunnelOnEthernetEnabled,
|
|
||||||
onCheckedChange = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onToggleTunnelOnEthernet()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(screenPadding),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
|
||||||
) {
|
|
||||||
Text(stringResource(R.string.always_on_vpn_support))
|
|
||||||
Switch(
|
|
||||||
enabled = !settings.isAutoTunnelEnabled,
|
|
||||||
checked = settings.isAlwaysOnVpnEnabled,
|
|
||||||
onCheckedChange = {
|
|
||||||
scope.launch {
|
|
||||||
viewModel.onToggleAlwaysOnVPN()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!uiState.isLocationDisclosureShown) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.fillMaxSize().verticalScroll(scrollState).padding(padding)) {
|
||||||
|
Icon(
|
||||||
|
Icons.Rounded.LocationOff,
|
||||||
|
contentDescription = stringResource(id = R.string.map),
|
||||||
|
modifier = Modifier.padding(30.dp).size(128.dp))
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.prominent_background_location_title),
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
modifier = Modifier.padding(30.dp),
|
||||||
|
fontSize = 20.sp)
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.prominent_background_location_message),
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
modifier = Modifier.padding(30.dp),
|
||||||
|
fontSize = 15.sp)
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Modifier.fillMaxWidth().padding(10.dp)
|
||||||
|
} else {
|
||||||
|
Modifier.fillMaxWidth().padding(30.dp)
|
||||||
|
},
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceEvenly) {
|
||||||
|
TextButton(onClick = { viewModel.setLocationDisclosureShown() }) {
|
||||||
|
Text(stringResource(id = R.string.no_thanks))
|
||||||
|
}
|
||||||
|
TextButton(
|
||||||
|
modifier = Modifier.focusRequester(focusRequester),
|
||||||
|
onClick = {
|
||||||
|
openSettings()
|
||||||
|
viewModel.setLocationDisclosureShown()
|
||||||
|
}) {
|
||||||
|
Text(stringResource(id = R.string.turn_on))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(showAuthPrompt) {
|
||||||
|
AuthorizationPrompt(
|
||||||
|
onSuccess = {
|
||||||
|
showAuthPrompt = false
|
||||||
|
exportAllConfigs()
|
||||||
|
},
|
||||||
|
onError = { _ ->
|
||||||
|
showAuthPrompt = false
|
||||||
|
showSnackbarMessage(Event.Error.AuthenticationFailed.message)
|
||||||
|
},
|
||||||
|
onFailure = {
|
||||||
|
showAuthPrompt = false
|
||||||
|
showSnackbarMessage(Event.Error.AuthorizationFailed.message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.tunnels.isEmpty() && uiState.isLocationDisclosureShown) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
modifier = Modifier.fillMaxSize().padding(padding)) {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.one_tunnel_required),
|
||||||
|
textAlign = TextAlign.Center,
|
||||||
|
modifier = Modifier.padding(15.dp),
|
||||||
|
fontStyle = FontStyle.Italic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (uiState.isLocationDisclosureShown && uiState.tunnels.isNotEmpty()) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxSize().verticalScroll(scrollState).clickable(
|
||||||
|
indication = null, interactionSource = interactionSource) {
|
||||||
|
focusManager.clearFocus()
|
||||||
|
}) {
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier =
|
||||||
|
(if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Modifier.height(IntrinsicSize.Min)
|
||||||
|
.fillMaxWidth(fillMaxWidth)
|
||||||
|
.padding(top = 10.dp)
|
||||||
|
} else {
|
||||||
|
Modifier.fillMaxWidth(fillMaxWidth).padding(top = 60.dp)
|
||||||
|
})
|
||||||
|
.padding(bottom = 10.dp)) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.padding(15.dp)) {
|
||||||
|
SectionTitle(
|
||||||
|
title = stringResource(id = R.string.auto_tunneling),
|
||||||
|
padding = screenPadding)
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(id = R.string.tunnel_on_wifi),
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled),
|
||||||
|
checked = uiState.settings.isTunnelOnWifiEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleTunnelOnWifi() },
|
||||||
|
modifier = if(uiState.settings.isAutoTunnelEnabled) Modifier else Modifier.focusRequester(focusRequester).focusProperties { down = focusRequester2 })
|
||||||
|
AnimatedVisibility(visible = uiState.settings.isTunnelOnWifiEnabled) {
|
||||||
|
Column {
|
||||||
|
FlowRow(
|
||||||
|
modifier = Modifier.padding(screenPadding).fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(5.dp)) {
|
||||||
|
uiState.settings.trustedNetworkSSIDs.forEach { ssid ->
|
||||||
|
ClickableIconButton(
|
||||||
|
onClick = { if(WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
viewModel.onDeleteTrustedSSID(ssid)
|
||||||
|
focusRequester2.requestFocus()
|
||||||
|
}},
|
||||||
|
onIconClick = { viewModel.onDeleteTrustedSSID(ssid) },
|
||||||
|
text = ssid,
|
||||||
|
icon = Icons.Filled.Close,
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled))
|
||||||
|
}
|
||||||
|
if (uiState.settings.trustedNetworkSSIDs.isEmpty()) {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.none),
|
||||||
|
fontStyle = FontStyle.Italic,
|
||||||
|
color = Color.Gray)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OutlinedTextField(
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled),
|
||||||
|
value = currentText,
|
||||||
|
onValueChange = { currentText = it },
|
||||||
|
label = { Text(stringResource(R.string.add_trusted_ssid)) },
|
||||||
|
modifier =
|
||||||
|
Modifier.padding(
|
||||||
|
start = screenPadding, top = 5.dp, bottom = 10.dp)
|
||||||
|
.focusRequester(focusRequester2)
|
||||||
|
,
|
||||||
|
maxLines = 1,
|
||||||
|
keyboardOptions =
|
||||||
|
KeyboardOptions(
|
||||||
|
capitalization = KeyboardCapitalization.None,
|
||||||
|
imeAction = ImeAction.Done),
|
||||||
|
keyboardActions = KeyboardActions(onDone = { saveTrustedSSID() }),
|
||||||
|
trailingIcon = {
|
||||||
|
if (currentText != "") {
|
||||||
|
IconButton(onClick = { saveTrustedSSID() }) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Outlined.Add,
|
||||||
|
contentDescription =
|
||||||
|
if (currentText == "") {
|
||||||
|
stringResource(
|
||||||
|
id = R.string.trusted_ssid_empty_description)
|
||||||
|
} else {
|
||||||
|
stringResource(
|
||||||
|
id = R.string.trusted_ssid_value_description)
|
||||||
|
},
|
||||||
|
tint = MaterialTheme.colorScheme.primary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(R.string.tunnel_mobile_data),
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled),
|
||||||
|
checked = uiState.settings.isTunnelOnMobileDataEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleTunnelOnMobileData() })
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(id = R.string.tunnel_on_ethernet),
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled),
|
||||||
|
checked = uiState.settings.isTunnelOnEthernetEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleTunnelOnEthernet() })
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(R.string.battery_saver),
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled),
|
||||||
|
checked = uiState.settings.isBatterySaverEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleBatterySaver() })
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = (if(!uiState.settings.isAutoTunnelEnabled) Modifier else Modifier.focusRequester(focusRequester))
|
||||||
|
.fillMaxSize().padding(top = 5.dp),
|
||||||
|
horizontalArrangement = Arrangement.Center) {
|
||||||
|
TextButton(
|
||||||
|
enabled = !uiState.settings.isAlwaysOnVpnEnabled,
|
||||||
|
onClick = {
|
||||||
|
if (uiState.settings.isTunnelOnWifiEnabled && !uiState.settings.isAutoTunnelEnabled) {
|
||||||
|
when(false) {
|
||||||
|
isBackgroundLocationGranted ->
|
||||||
|
showSnackbarMessage(Event.Error.BackgroundLocationRequired.message)
|
||||||
|
fineLocationState.status.isGranted ->
|
||||||
|
showSnackbarMessage(Event.Error.PreciseLocationRequired.message)
|
||||||
|
viewModel.isLocationEnabled(context) ->
|
||||||
|
showLocationServicesAlertDialog = true
|
||||||
|
else -> {
|
||||||
|
viewModel.toggleAutoTunnel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
viewModel.toggleAutoTunnel()
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
val autoTunnelButtonText =
|
||||||
|
if (uiState.settings.isAutoTunnelEnabled) {
|
||||||
|
stringResource(R.string.disable_auto_tunnel)
|
||||||
|
} else {
|
||||||
|
stringResource(id = R.string.enable_auto_tunnel)
|
||||||
|
}
|
||||||
|
Text(autoTunnelButtonText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (WgQuickBackend.hasKernelSupport()) {
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier = Modifier.fillMaxWidth(fillMaxWidth).padding(vertical = 10.dp)) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.padding(15.dp)) {
|
||||||
|
SectionTitle(
|
||||||
|
title = stringResource(id = R.string.kernel), padding = screenPadding)
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(R.string.use_kernel),
|
||||||
|
enabled =
|
||||||
|
!(uiState.settings.isAutoTunnelEnabled ||
|
||||||
|
uiState.settings.isAlwaysOnVpnEnabled ||
|
||||||
|
(uiState.vpnState.status == Tunnel.State.UP)),
|
||||||
|
checked = uiState.settings.isKernelEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleKernelMode().let {
|
||||||
|
when(it) {
|
||||||
|
is Result.Error -> showSnackbarMessage(it.error.message)
|
||||||
|
is Result.Success -> {}
|
||||||
|
}
|
||||||
|
} })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Surface(
|
||||||
|
tonalElevation = 2.dp,
|
||||||
|
shadowElevation = 2.dp,
|
||||||
|
shape = RoundedCornerShape(12.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surface,
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxWidth(fillMaxWidth)
|
||||||
|
.padding(vertical = 10.dp)
|
||||||
|
.padding(bottom = 140.dp)) {
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.Start,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier = Modifier.padding(15.dp)) {
|
||||||
|
SectionTitle(
|
||||||
|
title = stringResource(id = R.string.other), padding = screenPadding)
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(R.string.always_on_vpn_support),
|
||||||
|
enabled = !uiState.settings.isAutoTunnelEnabled,
|
||||||
|
checked = uiState.settings.isAlwaysOnVpnEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleAlwaysOnVPN() })
|
||||||
|
ConfigurationToggle(
|
||||||
|
stringResource(R.string.enabled_app_shortcuts),
|
||||||
|
enabled = true,
|
||||||
|
checked = uiState.settings.isShortcutsEnabled,
|
||||||
|
padding = screenPadding,
|
||||||
|
onCheckChanged = { viewModel.onToggleShortcutsEnabled() })
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxSize().padding(top = 5.dp),
|
||||||
|
horizontalArrangement = Arrangement.Center) {
|
||||||
|
TextButton(
|
||||||
|
enabled = !didExportFiles, onClick = { showAuthPrompt = true }) {
|
||||||
|
Text(stringResource(R.string.export_configs))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
|
Spacer(modifier = Modifier.weight(.17f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.settings
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
|
||||||
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnState
|
||||||
|
|
||||||
|
data class SettingsUiState(
|
||||||
|
val settings : Settings = Settings(),
|
||||||
|
val tunnels : List<TunnelConfig> = emptyList(),
|
||||||
|
val vpnState: VpnState = VpnState(),
|
||||||
|
val isLocationDisclosureShown : Boolean = true,
|
||||||
|
val loading : Boolean = true
|
||||||
|
)
|
||||||
+136
-105
@@ -3,140 +3,171 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.settings
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.location.LocationManager
|
import android.location.LocationManager
|
||||||
|
import androidx.core.location.LocationManagerCompat
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.wireguard.android.util.RootShell
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.SettingsDoa
|
import com.zaneschepke.wireguardautotunnel.data.datastore.DataStoreManager
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.TunnelConfigDao
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.Settings
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.repository.model.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.repository.TunnelConfigRepository
|
||||||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
|
||||||
import com.zaneschepke.wireguardautotunnel.ui.ViewState
|
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Result
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
|
||||||
import kotlinx.coroutines.flow.filter
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class SettingsViewModel @Inject constructor(private val application : Application,
|
class SettingsViewModel
|
||||||
private val tunnelRepo : TunnelConfigDao, private val settingsRepo : SettingsDoa
|
@Inject
|
||||||
|
constructor(
|
||||||
|
private val application: Application,
|
||||||
|
private val tunnelConfigRepository: TunnelConfigRepository,
|
||||||
|
private val settingsRepository: SettingsRepository,
|
||||||
|
private val dataStoreManager: DataStoreManager,
|
||||||
|
private val rootShell: RootShell,
|
||||||
|
private val vpnService: VpnService
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _trustedSSIDs = MutableStateFlow(emptyList<String>())
|
val uiState = combine(
|
||||||
val trustedSSIDs = _trustedSSIDs.asStateFlow()
|
settingsRepository.getSettingsFlow(),
|
||||||
private val _settings = MutableStateFlow(Settings())
|
tunnelConfigRepository.getTunnelConfigsFlow(),
|
||||||
val settings get() = _settings.asStateFlow()
|
vpnService.vpnState,
|
||||||
val tunnels get() = tunnelRepo.getAllFlow()
|
dataStoreManager.locationDisclosureFlow,
|
||||||
private val _viewState = MutableStateFlow(ViewState())
|
){ settings, tunnels, tunnelState, locationDisclosure ->
|
||||||
val viewState get() = _viewState.asStateFlow()
|
SettingsUiState(settings, tunnels, tunnelState, locationDisclosure
|
||||||
|
?: false, false)
|
||||||
|
}.stateIn(viewModelScope,
|
||||||
|
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT), SettingsUiState())
|
||||||
|
|
||||||
init {
|
fun onSaveTrustedSSID(ssid: String) : Result<Unit>{
|
||||||
checkLocationServicesEnabled()
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
settingsRepo.getAllFlow().filter { it.isNotEmpty() }.collect {
|
|
||||||
val settings = it.first()
|
|
||||||
_settings.emit(settings)
|
|
||||||
_trustedSSIDs.emit(settings.trustedNetworkSSIDs.toList())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun onSaveTrustedSSID(ssid: String) {
|
|
||||||
val trimmed = ssid.trim()
|
val trimmed = ssid.trim()
|
||||||
if (!_settings.value.trustedNetworkSSIDs.contains(trimmed)) {
|
return if (!uiState.value.settings.trustedNetworkSSIDs.contains(trimmed)) {
|
||||||
_settings.value.trustedNetworkSSIDs.add(trimmed)
|
uiState.value.settings.trustedNetworkSSIDs.add(trimmed)
|
||||||
settingsRepo.save(_settings.value)
|
saveSettings(uiState.value.settings)
|
||||||
|
Result.Success(Unit)
|
||||||
} else {
|
} else {
|
||||||
showSnackBarMessage("SSID already exists.")
|
Result.Error(Event.Error.SsidConflict)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onDefaultTunnelSelected(tunnelConfig: TunnelConfig) {
|
fun setLocationDisclosureShown() = viewModelScope.launch {
|
||||||
settingsRepo.save(_settings.value.copy(
|
dataStoreManager.saveToDataStore(DataStoreManager.LOCATION_DISCLOSURE_SHOWN, true)
|
||||||
defaultTunnel = tunnelConfig.toString()
|
}
|
||||||
|
|
||||||
|
fun onToggleTunnelOnMobileData() {
|
||||||
|
saveSettings(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isTunnelOnMobileDataEnabled = !uiState.value.settings.isTunnelOnMobileDataEnabled
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDeleteTrustedSSID(ssid: String) {
|
||||||
|
saveSettings(uiState.value.settings.copy(
|
||||||
|
trustedNetworkSSIDs = (uiState.value.settings.trustedNetworkSSIDs - ssid).toMutableList()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onToggleTunnelOnMobileData() {
|
private suspend fun getDefaultTunnelOrFirst() : String {
|
||||||
settingsRepo.save(_settings.value.copy(
|
return uiState.value.settings.defaultTunnel ?: tunnelConfigRepository.getAll().first().toString()
|
||||||
isTunnelOnMobileDataEnabled = !_settings.value.isTunnelOnMobileDataEnabled
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onDeleteTrustedSSID(ssid: String) {
|
fun toggleAutoTunnel() = viewModelScope.launch {
|
||||||
_settings.value.trustedNetworkSSIDs.remove(ssid)
|
val isAutoTunnelEnabled = uiState.value.settings.isAutoTunnelEnabled
|
||||||
settingsRepo.save(_settings.value)
|
var isAutoTunnelPaused = uiState.value.settings.isAutoTunnelPaused
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun toggleAutoTunnel() {
|
if (isAutoTunnelEnabled) {
|
||||||
if(_settings.value.defaultTunnel.isNullOrEmpty() && !_settings.value.isAutoTunnelEnabled) {
|
|
||||||
showSnackBarMessage(application.getString(R.string.select_tunnel_message))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if(_settings.value.isAutoTunnelEnabled) {
|
|
||||||
ServiceManager.stopWatcherService(application)
|
ServiceManager.stopWatcherService(application)
|
||||||
} else {
|
} else {
|
||||||
if(_settings.value.defaultTunnel != null) {
|
ServiceManager.startWatcherService(application)
|
||||||
val defaultTunnel = _settings.value.defaultTunnel
|
isAutoTunnelPaused = false
|
||||||
ServiceManager.startWatcherService(application, defaultTunnel!!)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
settingsRepo.save(_settings.value.copy(
|
saveSettings(
|
||||||
isAutoTunnelEnabled = !_settings.value.isAutoTunnelEnabled
|
uiState.value.settings.copy(
|
||||||
))
|
isAutoTunnelEnabled = !isAutoTunnelEnabled,
|
||||||
}
|
isAutoTunnelPaused = isAutoTunnelPaused,
|
||||||
|
defaultTunnel = getDefaultTunnelOrFirst()
|
||||||
suspend fun showSnackBarMessage(message : String) {
|
|
||||||
_viewState.emit(_viewState.value.copy(
|
|
||||||
showSnackbarMessage = true,
|
|
||||||
snackbarMessage = message,
|
|
||||||
snackbarActionText = "Okay",
|
|
||||||
onSnackbarActionClick = {
|
|
||||||
viewModelScope.launch {
|
|
||||||
dismissSnackBar()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun dismissSnackBar() {
|
|
||||||
_viewState.emit(_viewState.value.copy(
|
|
||||||
showSnackbarMessage = false
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun onToggleAlwaysOnVPN() {
|
|
||||||
if(_settings.value.defaultTunnel != null) {
|
|
||||||
_settings.emit(
|
|
||||||
_settings.value.copy(isAlwaysOnVpnEnabled = !_settings.value.isAlwaysOnVpnEnabled)
|
|
||||||
)
|
)
|
||||||
settingsRepo.save(_settings.value)
|
)
|
||||||
} else {
|
|
||||||
showSnackBarMessage(application.getString(R.string.select_tunnel_message))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onToggleTunnelOnEthernet() {
|
|
||||||
if(_settings.value.defaultTunnel != null) {
|
fun onToggleAlwaysOnVPN() = viewModelScope.launch {
|
||||||
_settings.emit(
|
val updatedSettings = uiState.value.settings.copy(
|
||||||
_settings.value.copy(isTunnelOnEthernetEnabled = !_settings.value.isTunnelOnEthernetEnabled)
|
isAlwaysOnVpnEnabled = !uiState.value.settings.isAlwaysOnVpnEnabled,
|
||||||
|
defaultTunnel = getDefaultTunnelOrFirst()
|
||||||
)
|
)
|
||||||
settingsRepo.save(_settings.value)
|
saveSettings(updatedSettings)
|
||||||
} else {
|
|
||||||
showSnackBarMessage(application.getString(R.string.select_tunnel_message))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkLocationServicesEnabled() : Boolean {
|
private fun saveSettings(settings: Settings) = viewModelScope.launch {
|
||||||
val locationManager =
|
settingsRepository.save(settings)
|
||||||
application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
||||||
return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
fun onToggleTunnelOnEthernet() {
|
||||||
|
saveSettings(uiState.value.settings.copy(
|
||||||
|
isTunnelOnEthernetEnabled = !uiState.value.settings.isTunnelOnEthernetEnabled
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isLocationEnabled(context: Context): Boolean {
|
||||||
|
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
|
return LocationManagerCompat.isLocationEnabled(locationManager)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleShortcutsEnabled() {
|
||||||
|
saveSettings(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isShortcutsEnabled = !uiState.value.settings.isShortcutsEnabled
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleBatterySaver() {
|
||||||
|
saveSettings(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isBatterySaverEnabled = !uiState.value.settings.isBatterySaverEnabled
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveKernelMode(on: Boolean) {
|
||||||
|
saveSettings(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isKernelEnabled = on
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleTunnelOnWifi() {
|
||||||
|
saveSettings(
|
||||||
|
uiState.value.settings.copy(
|
||||||
|
isTunnelOnWifiEnabled = !uiState.value.settings.isTunnelOnWifiEnabled
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onToggleKernelMode() : Result<Unit> {
|
||||||
|
if (!uiState.value.settings.isKernelEnabled) {
|
||||||
|
try {
|
||||||
|
rootShell.start()
|
||||||
|
Timber.d("Root shell accepted!")
|
||||||
|
saveKernelMode(on = true)
|
||||||
|
} catch (e: RootShell.RootShellException) {
|
||||||
|
saveKernelMode(on = false)
|
||||||
|
return Result.Error(Event.Error.RootDenied)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
saveKernelMode(on = false)
|
||||||
|
}
|
||||||
|
return Result.Success(Unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+185
-40
@@ -1,26 +1,36 @@
|
|||||||
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.Intent.createChooser
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.focusable
|
import androidx.compose.foundation.focusable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.rounded.ArrowForward
|
||||||
|
import androidx.compose.material.icons.rounded.Book
|
||||||
|
import androidx.compose.material.icons.rounded.Mail
|
||||||
|
import androidx.compose.material3.Divider
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.FocusRequester
|
||||||
@@ -30,54 +40,189 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.res.vectorResource
|
import androidx.compose.ui.res.vectorResource
|
||||||
import androidx.compose.ui.text.TextStyle
|
import androidx.compose.ui.text.TextStyle
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.text.style.TextDecoration
|
import androidx.compose.ui.text.style.TextDecoration
|
||||||
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 androidx.core.content.ContextCompat.startActivity
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.R
|
import com.zaneschepke.wireguardautotunnel.R
|
||||||
|
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
|
||||||
|
import com.zaneschepke.wireguardautotunnel.ui.common.screen.LoadingScreen
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Event
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SupportScreen(padding : PaddingValues, focusRequester: FocusRequester) {
|
fun SupportScreen(
|
||||||
|
viewModel: SupportViewModel = hiltViewModel(),
|
||||||
|
padding: PaddingValues,
|
||||||
|
showSnackbarMessage: (String) -> Unit,
|
||||||
|
focusRequester: FocusRequester
|
||||||
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val fillMaxWidth = .85f
|
||||||
|
|
||||||
val context = LocalContext.current
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
fun openWebPage(url: String) {
|
fun openWebPage(url: String) {
|
||||||
val webpage: Uri = Uri.parse(url)
|
try {
|
||||||
val intent = Intent(Intent.ACTION_VIEW, webpage)
|
val webpage: Uri = Uri.parse(url)
|
||||||
context.startActivity(intent)
|
val intent = Intent(Intent.ACTION_VIEW, webpage)
|
||||||
}
|
context.startActivity(intent)
|
||||||
|
} catch (e : Exception) {
|
||||||
|
showSnackbarMessage(Event.Error.Exception(e).message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Column(horizontalAlignment = Alignment.CenterHorizontally,
|
fun launchEmail() {
|
||||||
verticalArrangement = Arrangement.Top,
|
try {
|
||||||
modifier = Modifier
|
val intent =
|
||||||
.fillMaxSize()
|
Intent(Intent.ACTION_SEND).apply {
|
||||||
|
type = Constants.EMAIL_MIME_TYPE
|
||||||
|
putExtra(Intent.EXTRA_EMAIL, context.getString(R.string.my_email))
|
||||||
|
putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.email_subject))
|
||||||
|
}
|
||||||
|
startActivity(context, createChooser(intent, context.getString(R.string.email_chooser)), null)
|
||||||
|
} catch (e : Exception) {
|
||||||
|
showSnackbarMessage(Event.Error.Exception(e).message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.loading) {
|
||||||
|
LoadingScreen()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Column(
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
modifier =
|
||||||
|
Modifier.fillMaxSize()
|
||||||
.verticalScroll(rememberScrollState())
|
.verticalScroll(rememberScrollState())
|
||||||
.focusable()
|
.focusable()
|
||||||
.padding(padding)) {
|
.padding(padding)) {
|
||||||
Text(stringResource(R.string.support_text), textAlign = TextAlign.Center, modifier = Modifier.padding(30.dp), fontSize = 15.sp)
|
Surface(
|
||||||
Row(
|
tonalElevation = 2.dp,
|
||||||
modifier = Modifier
|
shadowElevation = 2.dp,
|
||||||
.fillMaxWidth()
|
shape = RoundedCornerShape(12.dp),
|
||||||
.padding(14.dp),
|
color = MaterialTheme.colorScheme.surface,
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
modifier =
|
||||||
horizontalArrangement = Arrangement.SpaceEvenly
|
(if (WireGuardAutoTunnel.isRunningOnAndroidTv()) {
|
||||||
) {
|
Modifier.height(IntrinsicSize.Min)
|
||||||
IconButton(onClick = {
|
.fillMaxWidth(fillMaxWidth)
|
||||||
openWebPage(context.resources.getString(R.string.discord_url))
|
.padding(top = 10.dp)
|
||||||
}) {
|
} else {
|
||||||
Icon(imageVector = ImageVector.vectorResource(R.drawable.discord), "Discord")
|
Modifier.fillMaxWidth(fillMaxWidth).padding(top = 20.dp)
|
||||||
|
})
|
||||||
|
.padding(bottom = 25.dp)) {
|
||||||
|
Column(modifier = Modifier.padding(20.dp)) {
|
||||||
|
Text(
|
||||||
|
stringResource(R.string.thank_you),
|
||||||
|
textAlign = TextAlign.Start,
|
||||||
|
fontWeight = FontWeight.Bold,
|
||||||
|
modifier = Modifier.padding(bottom = 20.dp),
|
||||||
|
fontSize = 16.sp)
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.support_help_text),
|
||||||
|
textAlign = TextAlign.Start,
|
||||||
|
fontSize = 16.sp,
|
||||||
|
modifier = Modifier.padding(bottom = 20.dp))
|
||||||
|
TextButton(
|
||||||
|
onClick = { openWebPage(context.resources.getString(R.string.docs_url)) },
|
||||||
|
modifier = Modifier.padding(vertical = 5.dp).focusRequester(focusRequester)) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth()) {
|
||||||
|
Row {
|
||||||
|
Icon(Icons.Rounded.Book, stringResource(id = R.string.docs))
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.docs_description),
|
||||||
|
textAlign = TextAlign.Justify,
|
||||||
|
modifier = Modifier.padding(start = 10.dp))
|
||||||
|
}
|
||||||
|
Icon(Icons.Rounded.ArrowForward, stringResource(id = R.string.go))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Divider(color = MaterialTheme.colorScheme.onBackground, thickness = 0.5.dp)
|
||||||
|
TextButton(
|
||||||
|
onClick = { openWebPage(context.resources.getString(R.string.discord_url)) },
|
||||||
|
modifier = Modifier.padding(vertical = 5.dp)) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth()) {
|
||||||
|
Row {
|
||||||
|
Icon(
|
||||||
|
imageVector = ImageVector.vectorResource(R.drawable.discord),
|
||||||
|
stringResource(id = R.string.discord),
|
||||||
|
Modifier.size(25.dp))
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.discord_description),
|
||||||
|
textAlign = TextAlign.Justify,
|
||||||
|
modifier = Modifier.padding(start = 10.dp))
|
||||||
|
}
|
||||||
|
Icon(Icons.Rounded.ArrowForward, stringResource(id = R.string.go))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Divider(color = MaterialTheme.colorScheme.onBackground, thickness = 0.5.dp)
|
||||||
|
TextButton(
|
||||||
|
onClick = { openWebPage(context.resources.getString(R.string.github_url)) },
|
||||||
|
modifier = Modifier.padding(vertical = 5.dp)) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth()) {
|
||||||
|
Row {
|
||||||
|
Icon(
|
||||||
|
imageVector = ImageVector.vectorResource(R.drawable.github),
|
||||||
|
stringResource(id = R.string.github),
|
||||||
|
Modifier.size(25.dp))
|
||||||
|
Text(
|
||||||
|
"Open an issue",
|
||||||
|
textAlign = TextAlign.Justify,
|
||||||
|
modifier = Modifier.padding(start = 10.dp))
|
||||||
|
}
|
||||||
|
Icon(Icons.Rounded.ArrowForward, stringResource(id = R.string.go))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Divider(color = MaterialTheme.colorScheme.onBackground, thickness = 0.5.dp)
|
||||||
|
TextButton(
|
||||||
|
onClick = { launchEmail() }, modifier = Modifier.padding(vertical = 5.dp)) {
|
||||||
|
Row(
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.fillMaxWidth()) {
|
||||||
|
Row {
|
||||||
|
Icon(Icons.Rounded.Mail, stringResource(id = R.string.email))
|
||||||
|
Text(
|
||||||
|
stringResource(id = R.string.email_description),
|
||||||
|
textAlign = TextAlign.Justify,
|
||||||
|
modifier = Modifier.padding(start = 10.dp))
|
||||||
|
}
|
||||||
|
Icon(Icons.Rounded.ArrowForward, stringResource(id = R.string.go))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
IconButton(modifier = Modifier.focusRequester(focusRequester),onClick = {
|
}
|
||||||
openWebPage(context.resources.getString(R.string.github_url))
|
Spacer(modifier = Modifier.weight(1f))
|
||||||
}) {
|
Text(
|
||||||
Icon(imageVector = ImageVector.vectorResource(R.drawable.github), "Github")
|
stringResource(id = R.string.privacy_policy),
|
||||||
}
|
style = TextStyle(textDecoration = TextDecoration.Underline),
|
||||||
}
|
fontSize = 16.sp,
|
||||||
Spacer(modifier = Modifier.weight(1f))
|
modifier =
|
||||||
Text(stringResource(id = R.string.privacy_policy), style = TextStyle(textDecoration = TextDecoration.Underline),
|
Modifier.clickable {
|
||||||
modifier = Modifier.clickable {
|
openWebPage(context.resources.getString(R.string.privacy_policy_url))
|
||||||
openWebPage(context.resources.getString(R.string.privacy_policy_url))
|
})
|
||||||
})
|
Row(
|
||||||
Text("App version: ${com.zaneschepke.wireguardautotunnel.BuildConfig.VERSION_NAME}", Modifier.padding(25.dp))
|
horizontalArrangement = Arrangement.spacedBy(25.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
modifier = Modifier.padding(25.dp)) {
|
||||||
|
Text("Version: ${BuildConfig.VERSION_NAME}", modifier = Modifier.focusable())
|
||||||
|
Text("Mode: ${if (uiState.settings.isKernelEnabled) "Kernel" else "Userspace" }")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
||||||
|
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.model.Settings
|
||||||
|
|
||||||
|
data class SupportUiState(
|
||||||
|
val settings : Settings = Settings(),
|
||||||
|
val loading : Boolean = true
|
||||||
|
)
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
package com.zaneschepke.wireguardautotunnel.ui.screens.support
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
|
||||||
|
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class SupportViewModel @Inject constructor(
|
||||||
|
private val settingsRepository: SettingsRepository
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
|
val uiState = settingsRepository.getSettingsFlow().map {
|
||||||
|
SupportUiState(it, false)
|
||||||
|
}.stateIn(
|
||||||
|
viewModelScope,
|
||||||
|
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
|
||||||
|
SupportUiState()
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -11,7 +11,8 @@ val Purple40 = Color(0xFF6650a4)
|
|||||||
val PurpleGrey40 = Color(0xFF625b71)
|
val PurpleGrey40 = Color(0xFF625b71)
|
||||||
val Pink40 = Color(0xFFFFFFFF)
|
val Pink40 = Color(0xFFFFFFFF)
|
||||||
|
|
||||||
//status colors
|
// status colors
|
||||||
val brickRed = Color(0xFFCE4257)
|
val brickRed = Color(0xFFCE4257)
|
||||||
|
val corn = Color(0xFFFBEC5D)
|
||||||
val pinkRed = Color(0xFFEF476F)
|
val pinkRed = Color(0xFFEF476F)
|
||||||
val mint = Color(0xFF52B788)
|
val mint = Color(0xFF52B788)
|
||||||
|
|||||||
@@ -15,51 +15,52 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.platform.LocalView
|
import androidx.compose.ui.platform.LocalView
|
||||||
import androidx.core.view.WindowCompat
|
import androidx.core.view.WindowCompat
|
||||||
|
|
||||||
private val DarkColorScheme = darkColorScheme(
|
private val DarkColorScheme =
|
||||||
//primary = Purple80,
|
darkColorScheme(
|
||||||
primary = virdigris,
|
// primary = Purple80,
|
||||||
secondary = virdigris,
|
primary = virdigris,
|
||||||
// secondary = PurpleGrey80,
|
secondary = virdigris,
|
||||||
tertiary = virdigris
|
// secondary = PurpleGrey80,
|
||||||
//tertiary = Pink80
|
tertiary = virdigris
|
||||||
)
|
// tertiary = Pink80
|
||||||
|
)
|
||||||
|
|
||||||
private val LightColorScheme = lightColorScheme(
|
private val LightColorScheme =
|
||||||
primary = Purple40,
|
lightColorScheme(
|
||||||
secondary = PurpleGrey40,
|
primary = Purple40,
|
||||||
tertiary = Pink40
|
secondary = PurpleGrey40,
|
||||||
|
tertiary = Pink40
|
||||||
/* Other default colors to override
|
/* Other default colors to override
|
||||||
background = Color(0xFFFFFBFE),
|
background = Color(0xFFFFFBFE),
|
||||||
surface = Color(0xFFFFFBFE),
|
surface = Color(0xFFFFFBFE),
|
||||||
onPrimary = Color.White,
|
onPrimary = Color.White,
|
||||||
onSecondary = Color.White,
|
onSecondary = Color.White,
|
||||||
onTertiary = Color.White,
|
onTertiary = Color.White,
|
||||||
onBackground = Color(0xFF1C1B1F),
|
onBackground = Color(0xFF1C1B1F),
|
||||||
onSurface = Color(0xFF1C1B1F),
|
onSurface = Color(0xFF1C1B1F),
|
||||||
*/
|
*/
|
||||||
)
|
)
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun WireguardAutoTunnelTheme(
|
fun WireguardAutoTunnelTheme(
|
||||||
//force dark theme
|
// force dark theme
|
||||||
darkTheme : Boolean = true,
|
darkTheme: Boolean = true,
|
||||||
//darkTheme: Boolean = isSystemInDarkTheme(),
|
// darkTheme: Boolean = isSystemInDarkTheme(),
|
||||||
// Dynamic color is available on Android 12+
|
// Dynamic color is available on Android 12+
|
||||||
//turning off dynamic color for now
|
// turning off dynamic color for now
|
||||||
dynamicColor: Boolean = false,
|
dynamicColor: Boolean = false,
|
||||||
content: @Composable () -> Unit
|
content: @Composable () -> Unit
|
||||||
) {
|
) {
|
||||||
|
val colorScheme =
|
||||||
|
when {
|
||||||
|
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||||
|
val context = LocalContext.current
|
||||||
|
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
||||||
|
}
|
||||||
|
|
||||||
val colorScheme = when {
|
darkTheme -> DarkColorScheme
|
||||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
else -> LightColorScheme
|
||||||
val context = LocalContext.current
|
|
||||||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
darkTheme -> DarkColorScheme
|
|
||||||
else -> LightColorScheme
|
|
||||||
}
|
|
||||||
val view = LocalView.current
|
val view = LocalView.current
|
||||||
if (!view.isInEditMode) {
|
if (!view.isInEditMode) {
|
||||||
SideEffect {
|
SideEffect {
|
||||||
@@ -77,4 +78,4 @@ fun WireguardAutoTunnelTheme(
|
|||||||
typography = Typography,
|
typography = Typography,
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user