-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamic shortcuts and package search
Add dynamic on and off shortcuts for each new tunnel added. Shortcuts will toggle auto tunneling to allow manual override. Add package search for split tunneling. Add tunnel toggling from tile even while app is closed and auto tunneling is disabled. Refactor and code cleanup. Closes #23 Closes #21
- Loading branch information
1 parent
eeccc71
commit 20cc2c0
Showing
18 changed files
with
346 additions
and
201 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
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
82 changes: 82 additions & 0 deletions
82
app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceManager.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,82 @@ | ||
package com.zaneschepke.wireguardautotunnel.service.foreground | ||
|
||
import android.app.ActivityManager | ||
import android.app.Application | ||
import android.app.Service | ||
import android.content.Context | ||
import android.content.Context.ACTIVITY_SERVICE | ||
import android.content.Intent | ||
import com.google.firebase.crashlytics.ktx.crashlytics | ||
import com.google.firebase.ktx.Firebase | ||
import com.zaneschepke.wireguardautotunnel.R | ||
|
||
object ServiceManager { | ||
@Suppress("DEPRECATION") | ||
private // Deprecated for third party Services. | ||
fun <T> Context.isServiceRunning(service: Class<T>) = | ||
(getSystemService(ACTIVITY_SERVICE) as ActivityManager) | ||
.getRunningServices(Integer.MAX_VALUE) | ||
.any { it.service.className == service.name } | ||
|
||
fun <T : Service> getServiceState(context: Context, cls : Class<T>): ServiceState { | ||
val isServiceRunning = context.isServiceRunning(cls) | ||
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) { | ||
if (getServiceState(context, cls) == ServiceState.STOPPED && action == Action.STOP) return | ||
if (getServiceState(context, cls) == ServiceState.STARTED && action == Action.START) return | ||
val intent = Intent(context, cls).also { | ||
it.action = action.name | ||
extras?.forEach {(k, v) -> | ||
it.putExtra(k, v) | ||
} | ||
} | ||
intent.component?.javaClass | ||
try { | ||
when(action) { | ||
Action.START -> context.startForegroundService(intent) | ||
Action.STOP -> context.startService(intent) | ||
} | ||
} catch (e : Exception) { | ||
e.message?.let { Firebase.crashlytics.log(it) } | ||
} | ||
} | ||
|
||
fun startVpnService(context : Context, tunnelConfig : String) { | ||
actionOnService( | ||
Action.START, | ||
context, | ||
WireGuardTunnelService::class.java, | ||
mapOf(context.getString(R.string.tunnel_extras_key) to tunnelConfig)) | ||
} | ||
fun stopVpnService(context : Context) { | ||
actionOnService( | ||
Action.STOP, | ||
context, | ||
WireGuardTunnelService::class.java | ||
) | ||
} | ||
|
||
fun startWatcherService(context : Context, tunnelConfig : String) { | ||
actionOnService( | ||
Action.START, context, | ||
WireGuardConnectivityWatcherService::class.java, mapOf(context. | ||
getString(R.string.tunnel_extras_key) to | ||
tunnelConfig)) | ||
} | ||
|
||
fun stopWatcherService(context : Context) { | ||
actionOnService( | ||
Action.STOP, 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) | ||
} | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt
This file was deleted.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/ShortcutsActivity.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,28 @@ | ||
package com.zaneschepke.wireguardautotunnel.service.shortcut | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.zaneschepke.wireguardautotunnel.R | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.Action | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager | ||
import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardTunnelService | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class ShortcutsActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
if(intent.getStringExtra(ShortcutsManager.CLASS_NAME_EXTRA_KEY) | ||
.equals(WireGuardTunnelService::class.java.name)) { | ||
intent.getStringExtra(getString(R.string.tunnel_extras_key))?.let { | ||
ServiceManager.toggleWatcherService(this, it) | ||
} | ||
when(intent.action){ | ||
Action.STOP.name -> ServiceManager.stopVpnService(this) | ||
Action.START.name -> intent.getStringExtra(getString(R.string.tunnel_extras_key)) | ||
?.let { ServiceManager.startVpnService(this, it) } | ||
} | ||
} | ||
finish() | ||
} | ||
} |
Oops, something went wrong.