Commit 06df4227 authored by Mygod's avatar Mygod

Be more informative about import errors

parent 902dfb3e
...@@ -25,6 +25,7 @@ import android.util.LongSparseArray ...@@ -25,6 +25,7 @@ import android.util.LongSparseArray
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.forEachTry
import com.github.shadowsocks.utils.printLog import com.github.shadowsocks.utils.printLog
import com.google.gson.JsonParser import com.google.gson.JsonParser
import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonReader
...@@ -61,8 +62,7 @@ object ProfileManager { ...@@ -61,8 +62,7 @@ object ProfileManager {
profiles?.values?.singleOrNull { it.id == DataStore.profileId } profiles?.values?.singleOrNull { it.id == DataStore.profileId }
} else Core.currentProfile?.first } else Core.currentProfile?.first
val lazyClear = lazy { clear() } val lazyClear = lazy { clear() }
var result: Exception? = null jsons.asIterable().forEachTry { json ->
for (json in jsons) try {
Profile.parseJson(jsonParser.parse(JsonReader(json.bufferedReader()).apply { Profile.parseJson(jsonParser.parse(JsonReader(json.bufferedReader()).apply {
isLenient = true isLenient = true
}), feature) { }), feature) {
...@@ -76,10 +76,7 @@ object ProfileManager { ...@@ -76,10 +76,7 @@ object ProfileManager {
} }
createProfile(it) createProfile(it)
} }
} catch (e: Exception) {
if (result == null) result = e else result.addSuppressed(e)
} }
if (result != null) throw result
} }
fun serializeToJson(profiles: List<Profile>? = getAllProfiles()): JSONArray? { fun serializeToJson(profiles: List<Profile>? = getAllProfiles()): JSONArray? {
if (profiles == null) return null if (profiles == null) return null
......
...@@ -45,6 +45,19 @@ import java.net.HttpURLConnection ...@@ -45,6 +45,19 @@ import java.net.HttpURLConnection
import java.net.InetAddress import java.net.InetAddress
import kotlin.coroutines.resume import kotlin.coroutines.resume
fun <T> Iterable<T>.forEachTry(action: (T) -> Unit) {
var result: Exception? = null
for (element in this) try {
action(element)
} catch (e: Exception) {
if (result == null) result = e else result.addSuppressed(e)
}
if (result != null) {
result.printStackTrace()
throw result
}
}
val Throwable.readableMessage get() = localizedMessage ?: javaClass.name val Throwable.readableMessage get() = localizedMessage ?: javaClass.name
private val parseNumericAddress by lazy @SuppressLint("DiscouragedPrivateApi") { private val parseNumericAddress by lazy @SuppressLint("DiscouragedPrivateApi") {
......
...@@ -40,9 +40,7 @@ import androidx.core.util.forEach ...@@ -40,9 +40,7 @@ import androidx.core.util.forEach
import com.crashlytics.android.Crashlytics import com.crashlytics.android.Crashlytics
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.utils.datas import com.github.shadowsocks.utils.*
import com.github.shadowsocks.utils.openBitmap
import com.github.shadowsocks.utils.printLog
import com.google.android.gms.common.GoogleApiAvailability import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.samples.vision.barcodereader.BarcodeCapture import com.google.android.gms.samples.vision.barcodereader.BarcodeCapture
import com.google.android.gms.samples.vision.barcodereader.BarcodeGraphic import com.google.android.gms.samples.vision.barcodereader.BarcodeGraphic
...@@ -142,20 +140,22 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever { ...@@ -142,20 +140,22 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever {
when (requestCode) { when (requestCode) {
REQUEST_IMPORT, REQUEST_IMPORT_OR_FINISH -> if (resultCode == Activity.RESULT_OK) { REQUEST_IMPORT, REQUEST_IMPORT_OR_FINISH -> if (resultCode == Activity.RESULT_OK) {
val feature = Core.currentProfile?.first val feature = Core.currentProfile?.first
var success = false try {
for (uri in data!!.datas) try { var success = false
detector.detect(Frame.Builder().setBitmap(contentResolver.openBitmap(uri)).build()) data!!.datas.forEachTry { uri ->
.forEach { _, barcode -> detector.detect(Frame.Builder().setBitmap(contentResolver.openBitmap(uri)).build())
Profile.findAllUrls(barcode.rawValue, feature).forEach { .forEach { _, barcode ->
ProfileManager.createProfile(it) Profile.findAllUrls(barcode.rawValue, feature).forEach {
success = true ProfileManager.createProfile(it)
success = true
}
} }
} }
Toast.makeText(this, if (success) R.string.action_import_msg else R.string.action_import_err,
Toast.LENGTH_SHORT).show()
} catch (e: Exception) { } catch (e: Exception) {
printLog(e) Toast.makeText(this, e.readableMessage, Toast.LENGTH_LONG).show()
} }
Toast.makeText(this, if (success) R.string.action_import_msg else R.string.action_import_err,
Toast.LENGTH_SHORT).show()
onSupportNavigateUp() onSupportNavigateUp()
} else if (requestCode == REQUEST_IMPORT_OR_FINISH) onSupportNavigateUp() } else if (requestCode == REQUEST_IMPORT_OR_FINISH) onSupportNavigateUp()
else -> super.onActivityResult(requestCode, resultCode, data) else -> super.onActivityResult(requestCode, resultCode, data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment