From 44df94dae2f51956d663cefb0d6b86052a3d3bff Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 29 May 2020 12:15:36 +0800 Subject: [PATCH] Fix Handler constructor deprecation --- .../mygod/vpnhotspot/LocalOnlyHotspotService.kt | 4 +--- .../java/be/mygod/vpnhotspot/RepeaterService.kt | 15 ++++++++------- .../net/monitor/TetherTimeoutMonitor.kt | 6 ++++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt index b90f419d..35e0e2d2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt @@ -4,7 +4,6 @@ import android.content.Intent import android.content.IntentFilter import android.net.wifi.WifiManager import android.os.Build -import android.os.Handler import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.IpNeighbour @@ -51,7 +50,6 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope { private val dispatcher = newSingleThreadContext("LocalOnlyHotspotService") override val coroutineContext = dispatcher + Job() private var routingManager: RoutingManager? = null - private val handler = Handler() @RequiresApi(28) private var timeoutMonitor: TetherTimeoutMonitor? = null private var receiverRegistered = false @@ -85,7 +83,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope { this@LocalOnlyHotspotService.reservation = reservation if (!receiverRegistered) { if (Build.VERSION.SDK_INT >= 28) timeoutMonitor = TetherTimeoutMonitor( - this@LocalOnlyHotspotService, handler, reservation::close) + this@LocalOnlyHotspotService, reservation::close) registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) receiverRegistered = true } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 1e00fe81..94c25b6f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -9,7 +9,6 @@ import android.content.res.Configuration import android.net.wifi.WpsInfo import android.net.wifi.p2p.* import android.os.Build -import android.os.Handler import android.os.Looper import android.provider.Settings import androidx.annotation.RequiresApi @@ -137,7 +136,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene private val p2pManager get() = RepeaterService.p2pManager!! private var channel: WifiP2pManager.Channel? = null private val binder = Binder() - private val handler = Handler() @RequiresApi(28) private var timeoutMonitor: TetherTimeoutMonitor? = null private var receiverRegistered = false @@ -219,7 +217,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene if (!safeMode) setOperatingChannel() } catch (e: RuntimeException) { Timber.w(e) - handler.postDelayed(this::onChannelDisconnected, 1000) + launch(Dispatchers.Main) { + delay(1000) + onChannelDisconnected() + } } } @@ -370,7 +371,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene * startService Step 3 */ private fun doStartLocked(group: WifiP2pGroup) { - if (Build.VERSION.SDK_INT >= 28) timeoutMonitor = TetherTimeoutMonitor(this, handler, binder::shutdown) + if (Build.VERSION.SDK_INT >= 28) timeoutMonitor = TetherTimeoutMonitor(this, binder::shutdown) binder.group = group if (persistNextGroup) { networkName = group.networkName @@ -389,8 +390,9 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene private fun startFailure(msg: CharSequence, group: WifiP2pGroup? = null, showWifiEnable: Boolean = false) { SmartSnackbar.make(msg).apply { if (showWifiEnable) action(R.string.repeater_p2p_unavailable_enable) { - if (Build.VERSION.SDK_INT >= 29) it.context.startActivity(Intent(Settings.Panel.ACTION_WIFI)) - else @Suppress("DEPRECATION") app.wifi.isWifiEnabled = true + if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") { + app.wifi.isWifiEnabled = true + } else it.context.startActivity(Intent(Settings.Panel.ACTION_WIFI)) } }.show() showNotification() @@ -430,7 +432,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene } override fun onDestroy() { - handler.removeCallbacksAndMessages(null) if (status != Status.IDLE) binder.shutdown() launch { // force clean to prevent leakage cleanLocked() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt index 073eb363..786f1044 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt @@ -6,6 +6,7 @@ import android.content.res.Resources import android.database.ContentObserver import android.os.BatteryManager import android.os.Handler +import android.os.Looper import android.provider.Settings import androidx.annotation.RequiresApi import androidx.core.os.postDelayed @@ -16,8 +17,9 @@ import be.mygod.vpnhotspot.util.intentFilter import timber.log.Timber @RequiresApi(28) -class TetherTimeoutMonitor(private val context: Context, private val handler: Handler, - private val onTimeout: () -> Unit) : ContentObserver(handler), AutoCloseable { +class TetherTimeoutMonitor(private val context: Context, private val onTimeout: () -> Unit, + private val handler: Handler = Handler(Looper.getMainLooper())) : + ContentObserver(handler), AutoCloseable { /** * config_wifi_framework_soft_ap_timeout_delay was introduced in Android 9. *