From 05753976c7aff6af58543bc636158d87304acbdd Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 19 May 2023 15:51:20 -0400 Subject: [PATCH] Some fixes for setting platform ap config --- .../vpnhotspot/manage/TetheringFragment.kt | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) 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 a7b3668f..9d2c1019 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -238,36 +238,51 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick AlertDialogFragment.setResultListener(this) { which, ret -> if (which == DialogInterface.BUTTON_POSITIVE) GlobalScope.launch { val configuration = ret!!.configuration - @Suppress("DEPRECATION") - if (Build.VERSION.SDK_INT < 30 && - configuration.isAutoShutdownEnabled != TetherTimeoutMonitor.enabled) try { - TetherTimeoutMonitor.setEnabled(configuration.isAutoShutdownEnabled) - } catch (e: Exception) { - Timber.w(e) - SmartSnackbar.make(e).show() - } - val success = try { - if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") { - WifiApManager.setConfiguration(configuration.toWifiConfiguration()) - } else WifiApManager.setConfiguration(configuration.toPlatform()) - } catch (e: InvocationTargetException) { + if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") { + if (configuration.isAutoShutdownEnabled != TetherTimeoutMonitor.enabled) try { + TetherTimeoutMonitor.setEnabled(configuration.isAutoShutdownEnabled) + } catch (e: Exception) { + Timber.w(e) + SmartSnackbar.make(e).show() + } + val wc = configuration.toWifiConfiguration() try { - if (Build.VERSION.SDK_INT < 30) @Suppress("DEPRECATION") { - val wc = configuration.toWifiConfiguration() - RootManager.use { it.execute(WifiApCommands.SetConfigurationLegacy(wc)) } - } else { - val platform = configuration.toPlatform() - RootManager.use { it.execute(WifiApCommands.SetConfiguration(platform)) } + if (WifiApManager.setConfiguration(wc)) return@launch + } catch (e: InvocationTargetException) { + try { + if (RootManager.use { it.execute(WifiApCommands.SetConfigurationLegacy(wc)) } + .value) return@launch + } catch (e: CancellationException) { + return@launch SmartSnackbar.make(e).show() + } catch (eRoot: Exception) { + eRoot.addSuppressed(e) + Timber.w(eRoot) + return@launch SmartSnackbar.make(eRoot).show() + } + } + } else { + val platform = try { + configuration.toPlatform() + } catch (e: InvocationTargetException) { + Timber.w(e) + return@launch SmartSnackbar.make(e).show() + } + try { + if (WifiApManager.setConfiguration(platform)) return@launch + } catch (e: InvocationTargetException) { + try { + if (RootManager.use { it.execute(WifiApCommands.SetConfiguration(platform)) } + .value) return@launch + } catch (e: CancellationException) { + return@launch SmartSnackbar.make(e).show() + } catch (eRoot: Exception) { + eRoot.addSuppressed(e) + Timber.w(eRoot) + return@launch SmartSnackbar.make(eRoot).show() } - } catch (_: CancellationException) { - } catch (eRoot: Exception) { - eRoot.addSuppressed(e) - Timber.w(eRoot) - SmartSnackbar.make(eRoot).show() - null } } - if (success == false) SmartSnackbar.make(R.string.configuration_rejected).show() + SmartSnackbar.make(R.string.configuration_rejected).show() } } binding = FragmentTetheringBinding.inflate(inflater, container, false)