From 7ddc82d5cd97f45f6c4149809ad60047891f60c0 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 13 Dec 2020 18:45:04 -0500 Subject: [PATCH] Fix toast BadTokenException in OS 7.1.2 Apparently recent changes with ViewModel affect the internal of Activity which lead to toast throwing BadTokenException in OS 7.1.2. The error is not easily catchable. This library use reflection to override a key function in WindowManager to catch the error. I have audit the code of the library. See https://github.com/PureWriter/ToastCompat for more details --- V2rayNG/app/build.gradle | 1 + V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/V2rayNG/app/build.gradle b/V2rayNG/app/build.gradle index 2aa50fffa..2a4086bbc 100644 --- a/V2rayNG/app/build.gradle +++ b/V2rayNG/app/build.gradle @@ -96,6 +96,7 @@ dependencies { implementation 'me.dm7.barcodescanner:core:1.9.8' implementation 'me.dm7.barcodescanner:zxing:1.9.8' implementation 'com.github.jorgecastilloprz:fabprogresscircle:1.01@aar' + implementation 'me.drakeet.support:toastcompat:1.1.0' implementation(name: 'libv2ray', ext: 'aar') //implementation(name: 'tun2socks', ext: 'aar') diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt index aa49b89ca..73a400cd6 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt @@ -5,6 +5,7 @@ import android.os.Build import android.widget.Toast import com.v2ray.ang.AngApplication import me.dozen.dpreference.DPreference +import me.drakeet.support.toast.ToastCompat import org.json.JSONObject import java.net.URLConnection @@ -22,13 +23,13 @@ val Context.v2RayApplication: AngApplication val Context.defaultDPreference: DPreference get() = v2RayApplication.defaultDPreference -inline fun Context.toast(message: Int): Toast = Toast +inline fun Context.toast(message: Int): Toast = ToastCompat .makeText(this, message, Toast.LENGTH_SHORT) .apply { show() } -inline fun Context.toast(message: CharSequence): Toast = Toast +inline fun Context.toast(message: CharSequence): Toast = ToastCompat .makeText(this, message, Toast.LENGTH_SHORT) .apply { show()