From 7cf7817ceafc1a3f733fe533ec61b2b7244a9945 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Sat, 21 Dec 2024 23:28:50 -0500 Subject: [PATCH] fix: switch to dynamic shortcuts closes #365 closes #508 --- app/src/main/AndroidManifest.xml | 3 - .../wireguardautotunnel/module/AppModule.kt | 8 +++ .../autotunnel/AutoTunnelService.kt | 2 +- .../service/network/WifiService.kt | 12 ++-- .../shortcut/DynamicShortcutManager.kt | 72 +++++++++++++++++++ .../service/shortcut/ShortcutManager.kt | 6 ++ .../wireguardautotunnel/ui/MainActivity.kt | 8 +++ app/src/main/res/xml/shortcuts.xml | 68 ------------------ 8 files changed, 101 insertions(+), 78 deletions(-) create mode 100644 app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/DynamicShortcutManager.kt create mode 100644 app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/ShortcutManager.kt delete mode 100644 app/src/main/res/xml/shortcuts.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6bd670..5503c46 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -79,9 +79,6 @@ - + @AppShell private val rootShell: Provider, ) : NetworkService { val mutex = Mutex() - private var ssid : String? = null - private var available : Boolean = false + private var ssid: String? = null + private var available: Boolean = false private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager @@ -105,12 +105,12 @@ constructor( Timber.e(it) emit(NetworkStatus.Unavailable()) }.transform { - when(it) { + when (it) { is NetworkStatus.Available -> mutex.withLock { available = true } is NetworkStatus.CapabilitiesChanged -> mutex.withLock { - if(available) { + if (available) { available = false Timber.d("Getting SSID from capabilities") ssid = getNetworkName(it.networkCapabilities) @@ -122,7 +122,7 @@ constructor( } private suspend fun getNetworkName(networkCapabilities: NetworkCapabilities): String? { - if(settingsRepository.getSettings().isWifiNameByShellEnabled) return rootShell.get().getCurrentWifiName() + if (settingsRepository.getSettings().isWifiNameByShellEnabled) return rootShell.get().getCurrentWifiName() var ssid = networkCapabilities.getWifiName() if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) { val wifiManager = diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/DynamicShortcutManager.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/DynamicShortcutManager.kt new file mode 100644 index 0000000..aa91025 --- /dev/null +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/DynamicShortcutManager.kt @@ -0,0 +1,72 @@ +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 + +class DynamicShortcutManager(private val context: Context) : ShortcutManager { + override fun addShortcuts() { + ShortcutManagerCompat.setDynamicShortcuts(context, createShortcuts()) + } + + override fun removeShortcuts() { + ShortcutManagerCompat.removeDynamicShortcuts(context, createShortcuts().map { it.id }) + } + + private fun createShortcuts(): List { + return listOf( + buildShortcut( + context.getString(R.string.vpn_off), + context.getString(R.string.vpn_off), + context.getString(R.string.vpn_off), + intent = Intent(context, ShortcutsActivity::class.java).apply { + putExtra("className", "WireGuardTunnelService") + action = ShortcutsActivity.Action.STOP.name + }, + shortcutIcon = R.drawable.vpn_off, + ), + buildShortcut( + context.getString(R.string.vpn_on), + context.getString(R.string.vpn_on), + context.getString(R.string.vpn_on), + intent = Intent(context, ShortcutsActivity::class.java).apply { + putExtra("className", "WireGuardTunnelService") + action = ShortcutsActivity.Action.START.name + }, + shortcutIcon = R.drawable.vpn_on, + ), + buildShortcut( + context.getString(R.string.start_auto), + context.getString(R.string.start_auto), + context.getString(R.string.start_auto), + intent = Intent(context, ShortcutsActivity::class.java).apply { + putExtra("className", "WireGuardConnectivityWatcherService") + action = ShortcutsActivity.Action.START.name + }, + shortcutIcon = R.drawable.auto_play, + ), + buildShortcut( + context.getString(R.string.stop_auto), + context.getString(R.string.stop_auto), + context.getString(R.string.stop_auto), + intent = Intent(context, ShortcutsActivity::class.java).apply { + putExtra("className", "WireGuardConnectivityWatcherService") + action = ShortcutsActivity.Action.STOP.name + }, + shortcutIcon = R.drawable.auto_pause, + ), + ) + } + + private fun buildShortcut(id: String, shortLabel: String, longLabel: String, intent: Intent, shortcutIcon: Int): ShortcutInfoCompat { + return ShortcutInfoCompat.Builder(context, id) + .setShortLabel(shortLabel) + .setLongLabel(longLabel) + .setIntent(intent) + .setIcon(IconCompat.createWithResource(context, shortcutIcon)) + .build() + } +} diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/ShortcutManager.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/ShortcutManager.kt new file mode 100644 index 0000000..fc3884c --- /dev/null +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/shortcut/ShortcutManager.kt @@ -0,0 +1,6 @@ +package com.zaneschepke.wireguardautotunnel.service.shortcut + +interface ShortcutManager { + fun addShortcuts() + fun removeShortcuts() +} diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt index 376d75f..45fca6a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt @@ -40,6 +40,7 @@ import androidx.navigation.compose.rememberNavController import androidx.navigation.toRoute import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository +import com.zaneschepke.wireguardautotunnel.service.shortcut.ShortcutManager import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavBar import com.zaneschepke.wireguardautotunnel.ui.common.navigation.BottomNavItem @@ -78,6 +79,9 @@ class MainActivity : AppCompatActivity() { @Inject lateinit var tunnelService: TunnelService + @Inject + lateinit var shortcutManager: ShortcutManager + @OptIn(ExperimentalLayoutApi::class) override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge( @@ -119,6 +123,10 @@ class MainActivity : AppCompatActivity() { LaunchedEffect(isAutoTunnelEnabled) { this@MainActivity.requestAutoTunnelTileServiceUpdate() } + LaunchedEffect(isShortcutsEnabled) { + if (!isShortcutsEnabled) return@LaunchedEffect shortcutManager.removeShortcuts() + shortcutManager.addShortcuts() + } } CompositionLocalProvider(LocalNavController provides navController) { diff --git a/app/src/main/res/xml/shortcuts.xml b/app/src/main/res/xml/shortcuts.xml deleted file mode 100644 index bfd59e9..0000000 --- a/app/src/main/res/xml/shortcuts.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - -