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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,7 +5,6 @@ import android.annotation.TargetApi
import android.app.Service
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.net.wifi.WpsInfo
import android.net.wifi.p2p.*
import android.os.Build
@@ -14,6 +13,7 @@ import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.core.content.edit
import be.mygod.librootkotlinx.useParcel
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor
@@ -73,7 +73,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
get() = app.pref.getString(KEY_PASSPHRASE, null)
set(value) = app.pref.edit { putString(KEY_PASSPHRASE, value) }
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) }
var operatingChannel: Int
get() {

View File

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

View File

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

View File

@@ -115,7 +115,7 @@ abstract class IpMonitor {
this))
}
}
} catch (e: CancellationException) {
} catch (_: CancellationException) {
} catch (e: Exception) {
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 val networkParser =
"^(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>() {

View File

@@ -17,13 +17,17 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.view.isGone
import be.mygod.librootkotlinx.toByteArray
import be.mygod.librootkotlinx.toParcelable
import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.RepeaterService
import be.mygod.vpnhotspot.databinding.DialogWifiApBinding
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 kotlinx.android.parcel.Parcelize

View File

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

View File

@@ -4,7 +4,6 @@ import android.content.Context
import android.os.Build
import android.os.Parcelable
import android.os.RemoteException
import android.util.Log
import androidx.annotation.RequiresApi
import be.mygod.librootkotlinx.*
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()
process.outputStream.bufferedWriter().use { commands ->
// 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
File(cacheDir, "iptables-save").absolutePath.also {
commands.appendln("ln -sf /system/bin/iptables $it")
}
val ip6tablesSave = if (Build.VERSION.SDK_INT >= 24) "ip6tables-save" else
File(cacheDir, "ip6tables-save").absolutePath.also {
commands.appendln("ln -sf /system/bin/ip6tables $it")
}
val iptablesSave = if (Build.VERSION.SDK_INT < 24) File(cacheDir, "iptables-save").absolutePath.also {
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 {
commands.appendln("ln -sf /system/bin/ip6tables $it")
} else "ip6tables-save"
commands.appendln("""
|echo dumpsys ${Context.WIFI_P2P_SERVICE}
|dumpsys ${Context.WIFI_P2P_SERVICE}

View File

@@ -68,7 +68,7 @@ object RepeaterCommands {
override suspend fun execute(): WriteP2pConfig {
test(CONF_PATH_TREBLE)?.let { return WriteP2pConfig(it, false) }
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(),
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.net.InetAddresses
import android.os.Build
import android.os.Parcel
import android.os.Parcelable
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
@@ -51,25 +49,6 @@ fun Context.ensureReceiverUnregistered(receiver: BroadcastReceiver) {
} 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) {
if (!manager.isStateSaved) show(manager, tag)
}