From 317e2121c245208468deb0417d50764eba775f0b Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 23 Feb 2019 23:00:14 +0800 Subject: [PATCH] Refine error messages in general --- .../main/java/be/mygod/vpnhotspot/manage/TetherManager.kt | 7 ++++--- .../be/mygod/vpnhotspot/manage/TetheringTileService.kt | 5 +++-- mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt | 2 ++ .../main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt | 7 ++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt index 47feb647..0c16d91d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -21,6 +21,7 @@ import be.mygod.vpnhotspot.databinding.ListitemInterfaceBinding import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.wifi.WifiApManager +import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.widget.SmartSnackbar import timber.log.Timber import java.io.IOException @@ -57,7 +58,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), if (started) manager.stop() else manager.start() } catch (e: IOException) { Timber.w(e) - Toast.makeText(mainActivity, e.localizedMessage, Toast.LENGTH_LONG).show() + Toast.makeText(mainActivity, e.readableMessage, Toast.LENGTH_LONG).show() ManageBar.start(itemView.context) } catch (e: InvocationTargetException) { if (e.targetException !is SecurityException) Timber.w(e) @@ -65,7 +66,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), while (cause != null) { cause = cause.cause if (cause != null && cause !is InvocationTargetException) { - Toast.makeText(mainActivity, cause.localizedMessage, Toast.LENGTH_LONG).show() + Toast.makeText(mainActivity, cause.readableMessage, Toast.LENGTH_LONG).show() ManageBar.start(itemView.context) break } @@ -120,7 +121,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), } } catch (e: InvocationTargetException) { if (Build.VERSION.SDK_INT !in 24..25 || e.cause !is SecurityException) Timber.w(e) else Timber.d(e) - e.localizedMessage + e.readableMessage } } data.notifyChange() 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 724cbbf0..03ada374 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -14,6 +14,7 @@ import be.mygod.vpnhotspot.TetheringService import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.wifi.WifiApManager +import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.stopAndUnbind import be.mygod.vpnhotspot.widget.SmartSnackbar import timber.log.Timber @@ -74,14 +75,14 @@ sealed class TetheringTileService : TetherListeningTileService(), TetheringManag func() } catch (e: IOException) { Timber.w(e) - Toast.makeText(this, e.localizedMessage, Toast.LENGTH_LONG).show() + Toast.makeText(this, e.readableMessage, Toast.LENGTH_LONG).show() } catch (e: InvocationTargetException) { if (e.targetException !is SecurityException) Timber.w(e) var cause: Throwable? = e while (cause != null) { cause = cause.cause if (cause != null && cause !is InvocationTargetException) { - Toast.makeText(this, cause.localizedMessage, Toast.LENGTH_LONG).show() + Toast.makeText(this, cause.readableMessage, Toast.LENGTH_LONG).show() break } } 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 a136236a..4292536d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -19,6 +19,8 @@ import java.net.InetAddress import java.net.NetworkInterface import java.net.SocketException +val Throwable.readableMessage get() = localizedMessage ?: javaClass.name + /** * This is a hack: we wrap longs around in 1 billion and such. Hopefully every language counts in base 10 and this works * marvelously for everybody. diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt b/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt index 4faad0ed..d10fc6c9 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt @@ -9,6 +9,7 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.OnLifecycleEvent import be.mygod.vpnhotspot.App.Companion.app +import be.mygod.vpnhotspot.util.readableMessage import com.google.android.material.snackbar.Snackbar import com.topjohnwu.superuser.NoShellException @@ -18,17 +19,17 @@ sealed class SmartSnackbar { private var holder: View? = null fun make(@StringRes text: Int): SmartSnackbar = make(app.getText(text)) - fun make(text: CharSequence? = ""): SmartSnackbar { + fun make(text: CharSequence = ""): SmartSnackbar { val holder = holder return if (holder == null) @SuppressLint("ShowToast") { if (Looper.myLooper() == null) Looper.prepare() ToastWrapper(Toast.makeText(app, text, Toast.LENGTH_LONG)) - } else SnackbarWrapper(Snackbar.make(holder, text ?: null.toString(), Snackbar.LENGTH_LONG)) + } else SnackbarWrapper(Snackbar.make(holder, text, Snackbar.LENGTH_LONG)) } fun make(e: Throwable) = make(when (e) { is NoShellException -> e.cause ?: e else -> e - }.localizedMessage) + }.readableMessage) } class Register(lifecycle: Lifecycle, private val view: View) : LifecycleObserver {