Refine code style

This commit is contained in:
Mygod
2020-06-21 06:06:06 +08:00
parent ad218d7ec6
commit 2ebe0e4962
16 changed files with 35 additions and 72 deletions

View File

@@ -137,18 +137,7 @@ exceptions:
ThrowingNewInstanceOfSameException: ThrowingNewInstanceOfSameException:
active: true active: true
TooGenericExceptionCaught: TooGenericExceptionCaught:
active: true active: false
excludes: ['**/test/**', '**/androidTest/**', '**/*.Test.kt', '**/*.Spec.kt', '**/*.Spek.kt']
exceptionNames:
- ArrayIndexOutOfBoundsException
- Error
- Exception
- IllegalMonitorStateException
- NullPointerException
- IndexOutOfBoundsException
- RuntimeException
- Throwable
allowedExceptionNameRegex: '^(_|(ignore|expected).*)'
TooGenericExceptionThrown: TooGenericExceptionThrown:
active: true active: true
exceptionNames: exceptionNames:
@@ -345,8 +334,7 @@ performance:
active: true active: true
excludes: ['**/test/**', '**/androidTest/**', '**/*.Test.kt', '**/*.Spec.kt', '**/*.Spek.kt'] excludes: ['**/test/**', '**/androidTest/**', '**/*.Test.kt', '**/*.Spec.kt', '**/*.Spek.kt']
SpreadOperator: SpreadOperator:
active: true active: false
excludes: ['**/test/**', '**/androidTest/**', '**/*.Test.kt', '**/*.Spec.kt', '**/*.Spek.kt']
UnnecessaryTemporaryInstantiation: UnnecessaryTemporaryInstantiation:
active: true active: true

View File

@@ -370,7 +370,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
loop@ while (true) { loop@ while (true) {
val command = try { val command = try {
input.readParcelable<Parcelable>(RootServer::class.java.classLoader) input.readParcelable<Parcelable>(RootServer::class.java.classLoader)
} catch (e: EOFException) { } catch (_: EOFException) {
break break
} }
val callback = counter val callback = counter

View File

@@ -40,7 +40,7 @@ interface RootCommandChannel<T : Parcelable?> : Parcelable {
@Parcelize @Parcelize
internal class ChannelClosed(val index: Long) : RootCommandOneWay { internal class ChannelClosed(val index: Long) : RootCommandOneWay {
override suspend fun execute() = throw IllegalStateException("Internal implementation") override suspend fun execute() = error("Internal implementation")
} }
@Parcelize @Parcelize

View File

@@ -1,7 +1,6 @@
package be.mygod.librootkotlinx package be.mygod.librootkotlinx
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.IBinder
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import android.util.* import android.util.*

View File

@@ -3,7 +3,6 @@ package be.mygod.vpnhotspot
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application import android.app.Application
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
@@ -38,10 +37,6 @@ class App : Application() {
lateinit var app: App lateinit var app: App
} }
public override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
}
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
app = this app = this

View File

@@ -5,7 +5,6 @@ import android.annotation.TargetApi
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
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
@@ -14,6 +13,7 @@ import android.provider.Settings
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.content.edit import androidx.core.content.edit
import be.mygod.librootkotlinx.useParcel
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor
@@ -73,7 +73,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
get() = app.pref.getString(KEY_PASSPHRASE, null) get() = app.pref.getString(KEY_PASSPHRASE, null)
set(value) = app.pref.edit { putString(KEY_PASSPHRASE, value) } set(value) = app.pref.edit { putString(KEY_PASSPHRASE, value) }
var operatingBand: Int var operatingBand: Int
@SuppressLint("InlinedApi") get() = app.pref.getInt(KEY_OPERATING_BAND, WifiP2pConfig.GROUP_OWNER_BAND_AUTO) @SuppressLint("InlinedApi")
get() = app.pref.getInt(KEY_OPERATING_BAND, WifiP2pConfig.GROUP_OWNER_BAND_AUTO)
set(value) = app.pref.edit { putInt(KEY_OPERATING_BAND, value) } set(value) = app.pref.edit { putInt(KEY_OPERATING_BAND, value) }
var operatingChannel: Int var operatingChannel: Int
get() { get() {

View File

@@ -11,7 +11,6 @@ import android.service.quicksettings.Tile
import android.widget.Toast import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import be.mygod.vpnhotspot.App
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.TetheringService import be.mygod.vpnhotspot.TetheringService
import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetherType

View File

@@ -365,17 +365,19 @@ object TetheringManager {
else stopTetheringLegacy(Services.connectivity, type) else stopTetheringLegacy(Services.connectivity, type)
} }
@RequiresApi(24) @RequiresApi(24)
fun stopTethering(type: Int, callback: (Exception) -> Unit) = try { fun stopTethering(type: Int, callback: (Exception) -> Unit) {
stopTethering(type) try {
} catch (e: InvocationTargetException) { stopTethering(type)
if (e.targetException is SecurityException) GlobalScope.launch(Dispatchers.Unconfined) { } catch (e: InvocationTargetException) {
try { if (e.targetException is SecurityException) GlobalScope.launch(Dispatchers.Unconfined) {
RootManager.use { it.execute(StopTethering(type)) } try {
} catch (eRoot: Exception) { RootManager.use { it.execute(StopTethering(type)) }
eRoot.addSuppressed(e) } catch (eRoot: Exception) {
callback(eRoot) eRoot.addSuppressed(e)
} callback(eRoot)
} else callback(e) }
} else callback(e)
}
} }
/** /**

View File

@@ -115,7 +115,7 @@ abstract class IpMonitor {
this)) this))
} }
} }
} catch (e: CancellationException) { } catch (_: CancellationException) {
} catch (e: Exception) { } catch (e: Exception) {
Timber.w(e) Timber.w(e)
} }

View File

@@ -18,7 +18,6 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup? = null) {
private const val PERSISTENT_MAC = "p2p_device_persistent_mac_addr=" private const val PERSISTENT_MAC = "p2p_device_persistent_mac_addr="
private val networkParser = private val networkParser =
"^(bssid=(([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})|psk=(ext:|\"(.*)\"|[0-9a-fA-F]{64}\$)?)".toRegex() "^(bssid=(([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})|psk=(ext:|\"(.*)\"|[0-9a-fA-F]{64}\$)?)".toRegex()
private val whitespaceMatcher = "\\s+".toRegex()
} }
private class NetworkBlock : ArrayList<String>() { private class NetworkBlock : ArrayList<String>() {

View File

@@ -17,13 +17,17 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.core.view.isGone import androidx.core.view.isGone
import be.mygod.librootkotlinx.toByteArray
import be.mygod.librootkotlinx.toParcelable
import be.mygod.vpnhotspot.AlertDialogFragment import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.RepeaterService import be.mygod.vpnhotspot.RepeaterService
import be.mygod.vpnhotspot.databinding.DialogWifiApBinding import be.mygod.vpnhotspot.databinding.DialogWifiApBinding
import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.util.* import be.mygod.vpnhotspot.util.QRCodeDialog
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.util.showAllowingStateLoss
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize

View File

@@ -2,7 +2,7 @@ package be.mygod.vpnhotspot.room
import android.text.TextUtils import android.text.TextUtils
import androidx.room.TypeConverter import androidx.room.TypeConverter
import be.mygod.vpnhotspot.util.useParcel import be.mygod.librootkotlinx.useParcel
import timber.log.Timber import timber.log.Timber
import java.net.InetAddress import java.net.InetAddress

View File

@@ -4,7 +4,6 @@ import android.content.Context
import android.os.Build import android.os.Build
import android.os.Parcelable import android.os.Parcelable
import android.os.RemoteException import android.os.RemoteException
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import be.mygod.librootkotlinx.* import be.mygod.librootkotlinx.*
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
@@ -26,14 +25,12 @@ class Dump(val path: String, val cacheDir: File = app.deviceStorage.codeCacheDir
val process = ProcessBuilder("sh").redirectErrorStream(true).start() val process = ProcessBuilder("sh").redirectErrorStream(true).start()
process.outputStream.bufferedWriter().use { commands -> process.outputStream.bufferedWriter().use { commands ->
// https://android.googlesource.com/platform/external/iptables/+/android-7.0.0_r1/iptables/Android.mk#34 // https://android.googlesource.com/platform/external/iptables/+/android-7.0.0_r1/iptables/Android.mk#34
val iptablesSave = if (Build.VERSION.SDK_INT >= 24) "iptables-save" else val iptablesSave = if (Build.VERSION.SDK_INT < 24) File(cacheDir, "iptables-save").absolutePath.also {
File(cacheDir, "iptables-save").absolutePath.also { commands.appendln("ln -sf /system/bin/iptables $it")
commands.appendln("ln -sf /system/bin/iptables $it") } else "iptables-save"
} val ip6tablesSave = if (Build.VERSION.SDK_INT < 24) File(cacheDir, "ip6tables-save").absolutePath.also {
val ip6tablesSave = if (Build.VERSION.SDK_INT >= 24) "ip6tables-save" else commands.appendln("ln -sf /system/bin/ip6tables $it")
File(cacheDir, "ip6tables-save").absolutePath.also { } else "ip6tables-save"
commands.appendln("ln -sf /system/bin/ip6tables $it")
}
commands.appendln(""" commands.appendln("""
|echo dumpsys ${Context.WIFI_P2P_SERVICE} |echo dumpsys ${Context.WIFI_P2P_SERVICE}
|dumpsys ${Context.WIFI_P2P_SERVICE} |dumpsys ${Context.WIFI_P2P_SERVICE}

View File

@@ -68,7 +68,7 @@ object RepeaterCommands {
override suspend fun execute(): WriteP2pConfig { override suspend fun execute(): WriteP2pConfig {
test(CONF_PATH_TREBLE)?.let { return WriteP2pConfig(it, false) } test(CONF_PATH_TREBLE)?.let { return WriteP2pConfig(it, false) }
test(CONF_PATH_LEGACY)?.let { return WriteP2pConfig(it, true) } test(CONF_PATH_LEGACY)?.let { return WriteP2pConfig(it, true) }
throw IllegalStateException("p2p config file not found") error("p2p config file not found")
} }
} }

View File

@@ -40,7 +40,7 @@ object RoutingCommands {
fun check(command: List<String>, out: Boolean = this.out.isNotEmpty(), fun check(command: List<String>, out: Boolean = this.out.isNotEmpty(),
err: Boolean = this.err.isNotEmpty()) = message(command, out, err)?.let { msg -> err: Boolean = this.err.isNotEmpty()) = message(command, out, err)?.let { msg ->
throw UnexpectedOutputException(msg, this) throw UnexpectedOutputException(msg, this)
} }
} }

View File

@@ -5,8 +5,6 @@ import android.annotation.TargetApi
import android.content.* import android.content.*
import android.net.InetAddresses import android.net.InetAddresses
import android.os.Build import android.os.Build
import android.os.Parcel
import android.os.Parcelable
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
@@ -51,25 +49,6 @@ fun Context.ensureReceiverUnregistered(receiver: BroadcastReceiver) {
} catch (_: IllegalArgumentException) { } } catch (_: IllegalArgumentException) { }
} }
@SuppressLint("Recycle")
fun <T> useParcel(block: (Parcel) -> T) = Parcel.obtain().run {
try {
block(this)
} finally {
recycle()
}
}
fun Parcelable.toByteArray(parcelableFlags: Int = 0) = useParcel { p ->
p.writeParcelable(this, parcelableFlags)
p.marshall()
}
inline fun <reified T : Parcelable> ByteArray.toParcelable() = useParcel { p ->
p.unmarshall(this, 0, size)
p.setDataPosition(0)
p.readParcelable<T>(T::class.java.classLoader)
}
fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String? = null) { fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String? = null) {
if (!manager.isStateSaved) show(manager, tag) if (!manager.isStateSaved) show(manager, tag)
} }