From 2d9fd074244081ec931e7832acefc736e365d0c3 Mon Sep 17 00:00:00 2001 From: Chuan-Zheng Lee Date: Sun, 31 Oct 2021 12:57:38 +1300 Subject: [PATCH] Fix crashes caused by null field extraction This happened in 1.3.1 to incomingStyleName in Debating #showDialogToConfirmImport also change "(unknown)" to "[name unknown]" --- .../debatekeeper/DebateFormatDownloadManager.java | 6 ++++++ .../czlee/debatekeeper/DebatingTimerFragment.java | 13 +++++++++---- .../czlee/debatekeeper/FormatChooserFragment.java | 6 ++++-- .../debateformat/DebateFormatFieldExtractor.java | 6 ++++++ app/src/main/res/values/debating_timer.xml | 2 +- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/net/czlee/debatekeeper/DebateFormatDownloadManager.java b/app/src/main/java/net/czlee/debatekeeper/DebateFormatDownloadManager.java index a2daa763..1c0c2ca9 100644 --- a/app/src/main/java/net/czlee/debatekeeper/DebateFormatDownloadManager.java +++ b/app/src/main/java/net/czlee/debatekeeper/DebateFormatDownloadManager.java @@ -134,6 +134,12 @@ void checkForExistingFile(FormatXmlFilesManager filesManager, DebateFormatFieldE return; } + if (versionStr == null) { + Log.e(TAG, "No version found in " + this.filename); + this.state = DownloadState.UPDATE_AVAILABLE; + return; + } + int existingVersion; try { existingVersion = Integer.parseInt(versionStr); diff --git a/app/src/main/java/net/czlee/debatekeeper/DebatingTimerFragment.java b/app/src/main/java/net/czlee/debatekeeper/DebatingTimerFragment.java index 843ff04d..769f428f 100644 --- a/app/src/main/java/net/czlee/debatekeeper/DebatingTimerFragment.java +++ b/app/src/main/java/net/czlee/debatekeeper/DebatingTimerFragment.java @@ -1876,17 +1876,21 @@ private void showDialogToConfirmImport() { FormatXmlFilesManager filesManager = new FormatXmlFilesManager(context); boolean exists = filesManager.exists(incomingFilename); - String incomingStyleName, existingStyleName; + String incomingStyleName = null, existingStyleName = null; try { incomingStyleName = nameExtractor.getFieldValue(is); is.close(); - } catch (IOException | SAXException e) { + } catch (IOException e) { showSnackbar(Snackbar.LENGTH_LONG, R.string.importDebateFormat_snackbar_error_generic); return; + } catch (SAXException e) { + Log.e(TAG, "showDialogToConfirmImport: error parsing incoming file"); + // continue with unknown file name } + if (incomingStyleName == null) + incomingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName); - existingStyleName = null; if (exists) { // If there's an existing file, grab its style name and prompt to replace. (We don't // give an option not to replace. @@ -1895,9 +1899,10 @@ private void showDialogToConfirmImport() { existingStyleName = nameExtractor.getFieldValue(existingIs); existingIs.close(); } catch (IOException | SAXException e) { - existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName); Log.e(TAG, "showDialogToConfirmImport: error parsing existing file"); } + if (existingStyleName == null) + existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName); } else { // If it wasn't found, check if the style name happens to match any other file, since we diff --git a/app/src/main/java/net/czlee/debatekeeper/FormatChooserFragment.java b/app/src/main/java/net/czlee/debatekeeper/FormatChooserFragment.java index 48d7e974..5f6d0c3c 100644 --- a/app/src/main/java/net/czlee/debatekeeper/FormatChooserFragment.java +++ b/app/src/main/java/net/czlee/debatekeeper/FormatChooserFragment.java @@ -671,7 +671,7 @@ private void importIncomingFilePrompt(@Nullable Uri uri) { if (!filename.endsWith(".xml")) filename = filename + ".xml"; - String existingStyleName; + String existingStyleName = null; if (mFilesManager.exists(filename)) { DebateFormatFieldExtractor nameExtractor = new DebateFormatFieldExtractor(context, R.string.xml2elemName_name); @@ -681,8 +681,10 @@ private void importIncomingFilePrompt(@Nullable Uri uri) { } catch (IOException | SAXException e) { Log.e(TAG, "Couldn't open existing file, even though one exists"); e.printStackTrace(); - existingStyleName = "???"; } + if (existingStyleName == null) + existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName); + DialogFragment fragment = ConfirmOverwriteDialogFragment.newInstance(uri, filename, existingStyleName); fragment.show(getChildFragmentManager(), DIALOG_TAG_CONFIRM_OVERWRITE + filename); diff --git a/app/src/main/java/net/czlee/debatekeeper/debateformat/DebateFormatFieldExtractor.java b/app/src/main/java/net/czlee/debatekeeper/debateformat/DebateFormatFieldExtractor.java index cc734e61..df4fde18 100644 --- a/app/src/main/java/net/czlee/debatekeeper/debateformat/DebateFormatFieldExtractor.java +++ b/app/src/main/java/net/czlee/debatekeeper/debateformat/DebateFormatFieldExtractor.java @@ -20,9 +20,12 @@ import android.content.res.Resources; import android.util.Xml; +import androidx.annotation.Nullable; + import net.czlee.debatekeeper.R; import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -63,7 +66,10 @@ public DebateFormatFieldExtractor(Context context, int fieldNameResId) { * @param is an {@link InputStream} * @return the name of the style, e.g. "British Parliamentary", or null if the file is not a * valid debate format XML file. + * @throws IOException if thrown by {@link Xml#parse(InputStream, Xml.Encoding, ContentHandler)} + * @throws SAXException if thrown by {@link Xml#parse(InputStream, Xml.Encoding, ContentHandler)} */ + @Nullable public String getFieldValue(InputStream is) throws IOException, SAXException { mFieldValue = null; diff --git a/app/src/main/res/values/debating_timer.xml b/app/src/main/res/values/debating_timer.xml index 1ee7e85f..45cb94ce 100644 --- a/app/src/main/res/values/debating_timer.xml +++ b/app/src/main/res/values/debating_timer.xml @@ -71,7 +71,7 @@ This will overwrite the existing custom style \"%1$s\", because its file has the same name. If you don\'t want to lose %1$s, cancel and then import using a different file name. - (unknown) + [name unknown] Import Cancel Replace debate style?