diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index a58da05e..9f40b2c2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -30,6 +30,8 @@ import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager.localOnlyTetheredIfaces import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor +import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat +import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat.Companion.toCompat import be.mygod.vpnhotspot.net.wifi.WifiApDialogFragment import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.root.RootManager @@ -201,11 +203,16 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick apConfigurationRunning = true viewLifecycleOwner.lifecycleScope.launchWhenCreated { try { - WifiApManager.configuration + if (Build.VERSION.SDK_INT < 30) { + WifiApManager.configurationLegacy?.toCompat() ?: SoftApConfigurationCompat() + } else WifiApManager.configuration.toCompat() } catch (e: InvocationTargetException) { if (e.targetException !is SecurityException) Timber.w(e) try { - RootManager.use { it.execute(WifiApCommands.GetConfiguration()) } + if (Build.VERSION.SDK_INT < 30) { + RootManager.use { it.execute(WifiApCommands.GetConfigurationLegacy()) }?.toCompat() + ?: SoftApConfigurationCompat() + } else RootManager.use { it.execute(WifiApCommands.GetConfiguration()) }.toCompat() } catch (_: CancellationException) { null } catch (eRoot: Exception) { @@ -239,7 +246,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick if (which == DialogInterface.BUTTON_POSITIVE) viewLifecycleOwner.lifecycleScope.launchWhenCreated { val configuration = ret!!.configuration @Suppress("DEPRECATION") - if (Build.VERSION.SDK_INT >= 28 && Build.VERSION.SDK_INT < 30 && + if (Build.VERSION.SDK_INT in 28 until 30 && configuration.isAutoShutdownEnabled != TetherTimeoutMonitor.enabled) try { TetherTimeoutMonitor.setEnabled(configuration.isAutoShutdownEnabled) } catch (e: Exception) { @@ -247,10 +254,18 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick SmartSnackbar.make(e).show() } val success = try { - WifiApManager.setConfiguration(configuration) + if (Build.VERSION.SDK_INT < 30) { + WifiApManager.setConfiguration(configuration.toWifiConfiguration()) + } else WifiApManager.setConfiguration(configuration.toPlatform()) } catch (e: InvocationTargetException) { try { - RootManager.use { it.execute(WifiApCommands.SetConfiguration(configuration)) } + if (Build.VERSION.SDK_INT < 30) { + val wc = configuration.toWifiConfiguration() + RootManager.use { it.execute(WifiApCommands.SetConfigurationLegacy(wc)) } + } else { + val platform = configuration.toPlatform() + RootManager.use { it.execute(WifiApCommands.SetConfiguration(platform)) } + } } catch (_: CancellationException) { } catch (eRoot: Exception) { eRoot.addSuppressed(e) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt index 6fd1804c..df21ef02 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt @@ -164,13 +164,19 @@ object WifiApManager { /** * Requires NETWORK_SETTINGS permission (or root) on API 30+, and OVERRIDE_WIFI_CONFIG on API 29-. */ - val configuration get() = if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") { - (getWifiApConfiguration(Services.wifi) as android.net.wifi.WifiConfiguration?)?.toCompat() - ?: SoftApConfigurationCompat() - } else (getSoftApConfiguration(Services.wifi) as SoftApConfiguration).toCompat() - fun setConfiguration(value: SoftApConfigurationCompat) = (if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") { - setWifiApConfiguration(Services.wifi, value.toWifiConfiguration()) - } else setSoftApConfiguration(Services.wifi, value.toPlatform())) as Boolean + @Deprecated("Use configuration instead", ReplaceWith("configuration")) + @Suppress("DEPRECATION") + val configurationLegacy get() = getWifiApConfiguration(Services.wifi) as android.net.wifi.WifiConfiguration? + /** + * Requires NETWORK_SETTINGS permission (or root). + */ + @get:RequiresApi(30) + val configuration get() = getSoftApConfiguration(Services.wifi) as SoftApConfiguration + @Deprecated("Use SoftApConfiguration instead") + @Suppress("DEPRECATION") + fun setConfiguration(value: android.net.wifi.WifiConfiguration?) = + setWifiApConfiguration(Services.wifi, value) as Boolean + fun setConfiguration(value: SoftApConfiguration) = setSoftApConfiguration(Services.wifi, value) as Boolean @RequiresApi(28) interface SoftApCallbackCompat { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/root/WifiApCommands.kt b/mobile/src/main/java/be/mygod/vpnhotspot/root/WifiApCommands.kt index e7db4c6d..5359ada3 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/root/WifiApCommands.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/root/WifiApCommands.kt @@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.root import android.annotation.TargetApi import android.content.ClipData +import android.net.wifi.SoftApConfiguration import android.os.Build import android.os.Parcelable import androidx.annotation.RequiresApi @@ -10,7 +11,6 @@ import be.mygod.librootkotlinx.RootCommand import be.mygod.librootkotlinx.RootCommandChannel import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.R -import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiClient import be.mygod.vpnhotspot.widget.SmartSnackbar @@ -167,12 +167,28 @@ object WifiApCommands { } @Parcelize - class GetConfiguration : RootCommand { + @Deprecated("Use GetConfiguration instead", ReplaceWith("GetConfiguration")) + @Suppress("DEPRECATION") + class GetConfigurationLegacy : RootCommand { + override suspend fun execute() = WifiApManager.configurationLegacy + } + @Parcelize + @RequiresApi(30) + class GetConfiguration : RootCommand { override suspend fun execute() = WifiApManager.configuration } @Parcelize - data class SetConfiguration(val configuration: SoftApConfigurationCompat) : RootCommand { + @Deprecated("Use SetConfiguration instead", ReplaceWith("SetConfiguration")) + @Suppress("DEPRECATION") + data class SetConfigurationLegacy( + val configuration: android.net.wifi.WifiConfiguration?, + ) : RootCommand { + override suspend fun execute() = ParcelableBoolean(WifiApManager.setConfiguration(configuration)) + } + @Parcelize + @RequiresApi(30) + data class SetConfiguration(val configuration: SoftApConfiguration) : RootCommand { override suspend fun execute() = ParcelableBoolean(WifiApManager.setConfiguration(configuration)) } }