show an error when something goes wrong at svg parsing

This commit is contained in:
tibbi
2017-04-09 10:53:46 +02:00
parent 9ff5876a19
commit 8147db004b
3 changed files with 24 additions and 17 deletions

View File

@@ -1,6 +1,8 @@
package com.simplemobiletools.draw package com.simplemobiletools.draw
import android.app.Activity
import android.graphics.Path import android.graphics.Path
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.actions.Action import com.simplemobiletools.draw.actions.Action
import com.simplemobiletools.draw.actions.Line import com.simplemobiletools.draw.actions.Line
import com.simplemobiletools.draw.actions.Move import com.simplemobiletools.draw.actions.Move
@@ -23,9 +25,10 @@ class MyPath : Path(), Serializable {
} }
} }
fun readObject(pathData: String) { fun readObject(pathData: String, activity: Activity) {
val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray() val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
var i = 0 var i = 0
try {
while (i < tokens.size) { while (i < tokens.size) {
when (tokens[i][0]) { when (tokens[i][0]) {
'M' -> addAction(Move(tokens[i])) 'M' -> addAction(Move(tokens[i]))
@@ -43,6 +46,9 @@ class MyPath : Path(), Serializable {
} }
++i ++i
} }
} catch (e: Exception) {
activity.toast(R.string.unknown_error_occurred)
}
} }
override fun reset() { override fun reset() {

View File

@@ -1,5 +1,6 @@
package com.simplemobiletools.draw package com.simplemobiletools.draw
import android.app.Activity
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.sax.RootElement import android.sax.RootElement
@@ -45,7 +46,7 @@ object Svg {
} }
} }
fun loadSvg(file: File, canvas: MyCanvas) { fun loadSvg(activity: Activity, file: File, canvas: MyCanvas) {
val svg = parseSvg(file) val svg = parseSvg(file)
canvas.clearCanvas() canvas.clearCanvas()
@@ -53,7 +54,7 @@ object Svg {
svg.paths.forEach { svg.paths.forEach {
val path = MyPath() val path = MyPath()
path.readObject(it.data) path.readObject(it.data, activity)
val options = PaintOptions(it.color, it.strokeWidth) val options = PaintOptions(it.color, it.strokeWidth)
canvas.addPath(path, options) canvas.addPath(path, options)

View File

@@ -110,7 +110,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private fun openFile() { private fun openFile() {
FilePickerDialog(this, curPath) { FilePickerDialog(this, curPath) {
if (it.endsWith(".svg")) { if (it.endsWith(".svg")) {
Svg.loadSvg(File(it), my_canvas) Svg.loadSvg(this, File(it), my_canvas)
} else if (File(it).isImageSlow()) { } else if (File(it).isImageSlow()) {
} else { } else {