Deprecate thread helper

This commit is contained in:
Mygod
2019-01-26 01:33:35 +08:00
parent 59713a1eb5
commit 074252ad1c
2 changed files with 5 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ import android.system.OsConstants
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.DebugHelper import be.mygod.vpnhotspot.DebugHelper
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.util.thread
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
@@ -13,6 +12,7 @@ import java.io.InterruptedIOException
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
abstract class IpMonitor : Runnable { abstract class IpMonitor : Runnable {
companion object { companion object {
@@ -41,7 +41,7 @@ abstract class IpMonitor : Runnable {
return return
} }
monitor = process monitor = process
val err = thread("${javaClass.simpleName}-error") { val err = thread(name = "${javaClass.simpleName}-error") {
try { try {
process.errorStream.bufferedReader().forEachLine { Timber.e(it) } process.errorStream.bufferedReader().forEachLine { Timber.e(it) }
} catch (_: InterruptedIOException) { } catch (e: IOException) { } catch (_: InterruptedIOException) { } catch (e: IOException) {
@@ -65,7 +65,7 @@ abstract class IpMonitor : Runnable {
} }
init { init {
thread("${javaClass.simpleName}-input") { thread(name = "${javaClass.simpleName}-input") {
val mode = Mode.valueOf(app.pref.getString(KEY, Mode.Poll.toString()) ?: "") val mode = Mode.valueOf(app.pref.getString(KEY, Mode.Poll.toString()) ?: "")
if (mode != Mode.Poll) { if (mode != Mode.Poll) {
if (mode != Mode.MonitorRoot) { if (mode != Mode.MonitorRoot) {
@@ -83,14 +83,14 @@ abstract class IpMonitor : Runnable {
} }
} }
fun flush() = thread("${javaClass.simpleName}-flush") { run() } fun flush() = thread(name = "${javaClass.simpleName}-flush") { run() }
override fun run() { override fun run() {
val process = ProcessBuilder("ip", monitoredObject) val process = ProcessBuilder("ip", monitoredObject)
.redirectErrorStream(true) .redirectErrorStream(true)
.start() .start()
process.waitFor() process.waitFor()
thread("${javaClass.simpleName}-flush-error") { thread(name = "${javaClass.simpleName}-flush-error") {
val err = process.errorStream.bufferedReader().readText() val err = process.errorStream.bufferedReader().readText()
if (err.isNotBlank()) { if (err.isNotBlank()) {
Timber.e(err) Timber.e(err)

View File

@@ -7,9 +7,6 @@ import android.widget.ImageView
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.databinding.BindingAdapter import androidx.databinding.BindingAdapter
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
import java.net.InetAddress import java.net.InetAddress
import java.net.NetworkInterface import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
@@ -62,20 +59,6 @@ private val parseNumericAddress by lazy {
} }
fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress
/**
* Wrapper for kotlin.concurrent.thread that silences uncaught exceptions.
*/
fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = false,
contextClassLoader: ClassLoader? = null, priority: Int = -1, block: () -> Unit): Thread {
val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block)
thread.setUncaughtExceptionHandler { _, e ->
SmartSnackbar.make(R.string.noisy_su_failure).show()
Timber.w(e)
}
if (start) thread.start()
return thread
}
fun Context.stopAndUnbind(connection: ServiceConnection) { fun Context.stopAndUnbind(connection: ServiceConnection) {
connection.onServiceDisconnected(null) connection.onServiceDisconnected(null)
unbindService(connection) unbindService(connection)