From 4e55bea9e275f94fa42779a711147ccfc671a2c6 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 13 May 2021 00:52:13 +0800 Subject: [PATCH] Refine supported detection --- .../be/mygod/vpnhotspot/net/TetherOffloadManager.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt index 470d8c8f..73e9f576 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt @@ -16,8 +16,17 @@ import timber.log.Timber */ object TetherOffloadManager { val supported by lazy { - Build.VERSION.SDK_INT >= 27 || Settings.Global::class.java.getDeclaredField("TETHER_OFFLOAD_DISABLED") - .get(null).also { if (it != TETHER_OFFLOAD_DISABLED) Timber.w(Exception("Unknown field $it")) } != null + Build.VERSION.SDK_INT >= 27 || try { + Settings.Global::class.java.getDeclaredField("TETHER_OFFLOAD_DISABLED").get(null).let { + require(it == TETHER_OFFLOAD_DISABLED) { "Unknown field $it" } + } + true + } catch (_: NoSuchFieldException) { + false + } catch (e: Exception) { + Timber.w(e) + false + } } private const val TETHER_OFFLOAD_DISABLED = "tether_offload_disabled" val enabled get() = Settings.Global.getInt(app.contentResolver, TETHER_OFFLOAD_DISABLED, 0) == 0