From 8147db004b2fbdd3c18f4c19489308013563b702 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 9 Apr 2017 10:53:46 +0200 Subject: [PATCH] show an error when something goes wrong at svg parsing --- .../com/simplemobiletools/draw/MyPath.kt | 34 +++++++++++-------- .../kotlin/com/simplemobiletools/draw/Svg.kt | 5 +-- .../draw/activities/MainActivity.kt | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt b/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt index 6b04c7b..df2c37e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt @@ -1,6 +1,8 @@ package com.simplemobiletools.draw +import android.app.Activity import android.graphics.Path +import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.draw.actions.Action import com.simplemobiletools.draw.actions.Line import com.simplemobiletools.draw.actions.Move @@ -23,25 +25,29 @@ class MyPath : Path(), Serializable { } } - fun readObject(pathData: String) { + fun readObject(pathData: String, activity: Activity) { val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray() var i = 0 - while (i < tokens.size) { - when (tokens[i][0]) { - 'M' -> addAction(Move(tokens[i])) - 'L' -> addAction(Line(tokens[i])) - 'Q' -> { - // Quad actions are of the following form: - // "Qx1,y1 x2,y2" - // Since we split the tokens by whitespace, we need to join them again - if (i + 1 >= tokens.size) - throw InvalidParameterException("Error parsing the data for a Quad.") + try { + while (i < tokens.size) { + when (tokens[i][0]) { + 'M' -> addAction(Move(tokens[i])) + 'L' -> addAction(Line(tokens[i])) + 'Q' -> { + // Quad actions are of the following form: + // "Qx1,y1 x2,y2" + // Since we split the tokens by whitespace, we need to join them again + if (i + 1 >= tokens.size) + throw InvalidParameterException("Error parsing the data for a Quad.") - addAction(Quad(tokens[i] + " " + tokens[i + 1])) - ++i + addAction(Quad(tokens[i] + " " + tokens[i + 1])) + ++i + } } + ++i } - ++i + } catch (e: Exception) { + activity.toast(R.string.unknown_error_occurred) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt b/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt index 96c3e11..abd137b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.draw +import android.app.Activity import android.graphics.Color import android.graphics.drawable.ColorDrawable 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) canvas.clearCanvas() @@ -53,7 +54,7 @@ object Svg { svg.paths.forEach { val path = MyPath() - path.readObject(it.data) + path.readObject(it.data, activity) val options = PaintOptions(it.color, it.strokeWidth) canvas.addPath(path, options) diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt index 2ad961a..9620484 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt @@ -110,7 +110,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { private fun openFile() { FilePickerDialog(this, curPath) { if (it.endsWith(".svg")) { - Svg.loadSvg(File(it), my_canvas) + Svg.loadSvg(this, File(it), my_canvas) } else if (File(it).isImageSlow()) { } else {