diff --git a/detekt.yml b/detekt.yml index fdfc1d2e..3c511e56 100644 --- a/detekt.yml +++ b/detekt.yml @@ -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 diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt index f864c3cf..bac3c38d 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -370,7 +370,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U loop@ while (true) { val command = try { input.readParcelable(RootServer::class.java.classLoader) - } catch (e: EOFException) { + } catch (_: EOFException) { break } val callback = counter diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/ServerCommands.kt b/mobile/src/main/java/be/mygod/librootkotlinx/ServerCommands.kt index afbe7d6b..14b1797e 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/ServerCommands.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/ServerCommands.kt @@ -40,7 +40,7 @@ interface RootCommandChannel : 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 diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/Utils.kt b/mobile/src/main/java/be/mygod/librootkotlinx/Utils.kt index f500a617..f5f11b12 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/Utils.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/Utils.kt @@ -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.* diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index 1baa6f11..1c7be430 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -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 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index e844e289..19d7d30d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -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() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt index ecb7ab0e..d2e2656c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -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 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index ae35f31d..197abcbb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -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) + } } /** diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt index 4ede8cc5..a9887af6 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpMonitor.kt @@ -115,7 +115,7 @@ abstract class IpMonitor { this)) } } - } catch (e: CancellationException) { + } catch (_: CancellationException) { } catch (e: Exception) { Timber.w(e) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt index 2ee6c9f2..0fb8be86 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt @@ -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() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApDialogFragment.kt index e7d199ef..aab24036 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApDialogFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApDialogFragment.kt @@ -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 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt index fed46b49..a5b11fe3 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt @@ -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 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/root/MiscCommands.kt b/mobile/src/main/java/be/mygod/vpnhotspot/root/MiscCommands.kt index d907198e..2d772236 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/root/MiscCommands.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/root/MiscCommands.kt @@ -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} diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/root/RepeaterCommands.kt b/mobile/src/main/java/be/mygod/vpnhotspot/root/RepeaterCommands.kt index cc949fc3..da474f3b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/root/RepeaterCommands.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/root/RepeaterCommands.kt @@ -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") } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/root/RoutingCommands.kt b/mobile/src/main/java/be/mygod/vpnhotspot/root/RoutingCommands.kt index cab9118a..c90ad964 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/root/RoutingCommands.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/root/RoutingCommands.kt @@ -40,7 +40,7 @@ object RoutingCommands { fun check(command: List, out: Boolean = this.out.isNotEmpty(), err: Boolean = this.err.isNotEmpty()) = message(command, out, err)?.let { msg -> - throw UnexpectedOutputException(msg, this) + throw UnexpectedOutputException(msg, this) } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 753cd68d..dca37f17 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -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 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 ByteArray.toParcelable() = useParcel { p -> - p.unmarshall(this, 0, size) - p.setDataPosition(0) - p.readParcelable(T::class.java.classLoader) -} - fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String? = null) { if (!manager.isStateSaved) show(manager, tag) }