Commit 06df4227 authored by Mygod's avatar Mygod

Be more informative about import errors

parent 902dfb3e
......@@ -25,6 +25,7 @@ import android.util.LongSparseArray
import com.github.shadowsocks.Core
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.forEachTry
import com.github.shadowsocks.utils.printLog
import com.google.gson.JsonParser
import com.google.gson.stream.JsonReader
......@@ -61,8 +62,7 @@ object ProfileManager {
profiles?.values?.singleOrNull { it.id == DataStore.profileId }
} else Core.currentProfile?.first
val lazyClear = lazy { clear() }
var result: Exception? = null
for (json in jsons) try {
jsons.asIterable().forEachTry { json ->
Profile.parseJson(jsonParser.parse(JsonReader(json.bufferedReader()).apply {
isLenient = true
}), feature) {
......@@ -76,10 +76,7 @@ object ProfileManager {
}
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? {
if (profiles == null) return null
......
......@@ -45,6 +45,19 @@ import java.net.HttpURLConnection
import java.net.InetAddress
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
private val parseNumericAddress by lazy @SuppressLint("DiscouragedPrivateApi") {
......
......@@ -40,9 +40,7 @@ import androidx.core.util.forEach
import com.crashlytics.android.Crashlytics
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.utils.datas
import com.github.shadowsocks.utils.openBitmap
import com.github.shadowsocks.utils.printLog
import com.github.shadowsocks.utils.*
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.samples.vision.barcodereader.BarcodeCapture
import com.google.android.gms.samples.vision.barcodereader.BarcodeGraphic
......@@ -142,8 +140,9 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever {
when (requestCode) {
REQUEST_IMPORT, REQUEST_IMPORT_OR_FINISH -> if (resultCode == Activity.RESULT_OK) {
val feature = Core.currentProfile?.first
try {
var success = false
for (uri in data!!.datas) try {
data!!.datas.forEachTry { uri ->
detector.detect(Frame.Builder().setBitmap(contentResolver.openBitmap(uri)).build())
.forEach { _, barcode ->
Profile.findAllUrls(barcode.rawValue, feature).forEach {
......@@ -151,11 +150,12 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever {
success = true
}
}
} catch (e: Exception) {
printLog(e)
}
Toast.makeText(this, if (success) R.string.action_import_msg else R.string.action_import_err,
Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
Toast.makeText(this, e.readableMessage, Toast.LENGTH_LONG).show()
}
onSupportNavigateUp()
} else if (requestCode == REQUEST_IMPORT_OR_FINISH) onSupportNavigateUp()
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