Fix Handler constructor deprecation

This commit is contained in:
Mygod
2020-05-29 12:15:36 +08:00
parent 89a02bde69
commit 44df94dae2
3 changed files with 13 additions and 12 deletions

View File

@@ -4,7 +4,6 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
import android.os.Handler
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.IpNeighbour import be.mygod.vpnhotspot.net.IpNeighbour
@@ -51,7 +50,6 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
private val dispatcher = newSingleThreadContext("LocalOnlyHotspotService") private val dispatcher = newSingleThreadContext("LocalOnlyHotspotService")
override val coroutineContext = dispatcher + Job() override val coroutineContext = dispatcher + Job()
private var routingManager: RoutingManager? = null private var routingManager: RoutingManager? = null
private val handler = Handler()
@RequiresApi(28) @RequiresApi(28)
private var timeoutMonitor: TetherTimeoutMonitor? = null private var timeoutMonitor: TetherTimeoutMonitor? = null
private var receiverRegistered = false private var receiverRegistered = false
@@ -85,7 +83,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
this@LocalOnlyHotspotService.reservation = reservation this@LocalOnlyHotspotService.reservation = reservation
if (!receiverRegistered) { if (!receiverRegistered) {
if (Build.VERSION.SDK_INT >= 28) timeoutMonitor = TetherTimeoutMonitor( 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)) registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
receiverRegistered = true receiverRegistered = true
} }

View File

@@ -9,7 +9,6 @@ import android.content.res.Configuration
import android.net.wifi.WpsInfo import android.net.wifi.WpsInfo
import android.net.wifi.p2p.* import android.net.wifi.p2p.*
import android.os.Build import android.os.Build
import android.os.Handler
import android.os.Looper import android.os.Looper
import android.provider.Settings import android.provider.Settings
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
@@ -137,7 +136,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
private val p2pManager get() = RepeaterService.p2pManager!! private val p2pManager get() = RepeaterService.p2pManager!!
private var channel: WifiP2pManager.Channel? = null private var channel: WifiP2pManager.Channel? = null
private val binder = Binder() private val binder = Binder()
private val handler = Handler()
@RequiresApi(28) @RequiresApi(28)
private var timeoutMonitor: TetherTimeoutMonitor? = null private var timeoutMonitor: TetherTimeoutMonitor? = null
private var receiverRegistered = false private var receiverRegistered = false
@@ -219,7 +217,10 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
if (!safeMode) setOperatingChannel() if (!safeMode) setOperatingChannel()
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
Timber.w(e) 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 * startService Step 3
*/ */
private fun doStartLocked(group: WifiP2pGroup) { 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 binder.group = group
if (persistNextGroup) { if (persistNextGroup) {
networkName = group.networkName networkName = group.networkName
@@ -389,8 +390,9 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
private fun startFailure(msg: CharSequence, group: WifiP2pGroup? = null, showWifiEnable: Boolean = false) { private fun startFailure(msg: CharSequence, group: WifiP2pGroup? = null, showWifiEnable: Boolean = false) {
SmartSnackbar.make(msg).apply { SmartSnackbar.make(msg).apply {
if (showWifiEnable) action(R.string.repeater_p2p_unavailable_enable) { if (showWifiEnable) action(R.string.repeater_p2p_unavailable_enable) {
if (Build.VERSION.SDK_INT >= 29) it.context.startActivity(Intent(Settings.Panel.ACTION_WIFI)) if (Build.VERSION.SDK_INT < 29) @Suppress("DEPRECATION") {
else @Suppress("DEPRECATION") app.wifi.isWifiEnabled = true app.wifi.isWifiEnabled = true
} else it.context.startActivity(Intent(Settings.Panel.ACTION_WIFI))
} }
}.show() }.show()
showNotification() showNotification()
@@ -430,7 +432,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
} }
override fun onDestroy() { override fun onDestroy() {
handler.removeCallbacksAndMessages(null)
if (status != Status.IDLE) binder.shutdown() if (status != Status.IDLE) binder.shutdown()
launch { // force clean to prevent leakage launch { // force clean to prevent leakage
cleanLocked() cleanLocked()

View File

@@ -6,6 +6,7 @@ import android.content.res.Resources
import android.database.ContentObserver import android.database.ContentObserver
import android.os.BatteryManager import android.os.BatteryManager
import android.os.Handler import android.os.Handler
import android.os.Looper
import android.provider.Settings import android.provider.Settings
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
@@ -16,8 +17,9 @@ import be.mygod.vpnhotspot.util.intentFilter
import timber.log.Timber import timber.log.Timber
@RequiresApi(28) @RequiresApi(28)
class TetherTimeoutMonitor(private val context: Context, private val handler: Handler, class TetherTimeoutMonitor(private val context: Context, private val onTimeout: () -> Unit,
private val onTimeout: () -> Unit) : ContentObserver(handler), AutoCloseable { private val handler: Handler = Handler(Looper.getMainLooper())) :
ContentObserver(handler), AutoCloseable {
/** /**
* config_wifi_framework_soft_ap_timeout_delay was introduced in Android 9. * config_wifi_framework_soft_ap_timeout_delay was introduced in Android 9.
* *