-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quick settings tile for easy tunnel toggling and auto-tunnel override. Fix bug on AndroidTV D-pad tunnel control for multiple tunnels. Closes #18 , Closes #20
- Loading branch information
1 parent
f513297
commit 0e64bbb
Showing
7 changed files
with
210 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
app/src/main/java/com/zaneschepke/wireguardautotunnel/service/TunnelControlTile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package com.zaneschepke.wireguardautotunnel.service | ||
|
||
import android.os.Build | ||
import android.service.quicksettings.Tile | ||
import android.service.quicksettings.TileService | ||
import com.wireguard.android.backend.Tunnel | ||
import com.zaneschepke.wireguardautotunnel.R | ||
import com.zaneschepke.wireguardautotunnel.repository.Repository | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.Action | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceTracker | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectivityWatcherService | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService | ||
import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService | ||
import com.zaneschepke.wireguardautotunnel.service.tunnel.model.Settings | ||
import com.zaneschepke.wireguardautotunnel.service.tunnel.model.TunnelConfig | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class TunnelControlTile : TileService() { | ||
|
||
@Inject | ||
lateinit var settingsRepo : Repository<Settings> | ||
|
||
@Inject | ||
lateinit var configRepo : Repository<TunnelConfig> | ||
|
||
@Inject | ||
lateinit var vpnService : VpnService | ||
|
||
private val scope = CoroutineScope(Dispatchers.Main); | ||
|
||
private lateinit var job : Job | ||
|
||
override fun onStartListening() { | ||
if (!this::job.isInitialized) { | ||
job = scope.launch { | ||
updateTileState() | ||
} | ||
} | ||
Timber.d("On start listening") | ||
super.onStartListening() | ||
} | ||
|
||
override fun onTileAdded() { | ||
super.onTileAdded() | ||
qsTile.contentDescription = "Toggle VPN" | ||
scope.launch { | ||
updateTileState(); | ||
} | ||
} | ||
|
||
override fun onTileRemoved() { | ||
super.onTileRemoved() | ||
cancelJob() | ||
} | ||
|
||
override fun onClick() { | ||
unlockAndRun { | ||
scope.launch { | ||
try { | ||
if(vpnService.getState() == Tunnel.State.UP) { | ||
stopTunnel(); | ||
return@launch | ||
} | ||
val settings = settingsRepo.getAll() | ||
if (!settings.isNullOrEmpty()) { | ||
val setting = settings.first() | ||
if (setting.defaultTunnel != null) { | ||
startTunnel(setting.defaultTunnel!!) | ||
} else { | ||
val config = configRepo.getAll()?.first(); | ||
if(config != null) { | ||
startTunnel(config.toString()); | ||
} | ||
} | ||
} | ||
} finally { | ||
cancel() | ||
} | ||
} | ||
super.onClick() | ||
} | ||
} | ||
|
||
private fun stopTunnel() { | ||
ServiceTracker.actionOnService( | ||
Action.STOP, this@TunnelControlTile, | ||
WireGuardConnectivityWatcherService::class.java) | ||
ServiceTracker.actionOnService( | ||
Action.STOP, this@TunnelControlTile, | ||
WireGuardTunnelService::class.java) | ||
} | ||
|
||
private fun startTunnel(tunnelConfig : String) { | ||
ServiceTracker.actionOnService( | ||
Action.START, this.applicationContext, | ||
WireGuardTunnelService::class.java, | ||
mapOf(this.applicationContext.resources. | ||
getString(R.string.tunnel_extras_key) to | ||
tunnelConfig)) | ||
} | ||
|
||
private suspend fun updateTileState() { | ||
vpnService.state.collect { | ||
when(it) { | ||
Tunnel.State.UP -> { | ||
setTileOn() | ||
} | ||
Tunnel.State.DOWN -> { | ||
setTileOff() | ||
} | ||
else -> { | ||
qsTile.state = Tile.STATE_UNAVAILABLE | ||
} | ||
} | ||
qsTile.updateTile() | ||
} | ||
} | ||
|
||
private fun setTileOff() { | ||
qsTile.state = Tile.STATE_INACTIVE; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
qsTile.subtitle = "Off" | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
qsTile.stateDescription = "VPN Off"; | ||
} | ||
} | ||
|
||
private fun setTileOn() { | ||
qsTile.state = Tile.STATE_ACTIVE; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
qsTile.subtitle = "On" | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
qsTile.stateDescription = "VPN On"; | ||
} | ||
} | ||
private fun cancelJob() { | ||
if(this::job.isInitialized) { | ||
job.cancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12 5.16,-1.26 9,-6.45 9,-12V5l-9,-4z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters