From 26a4b5eccdca2d90577b339571b32bcb83b84592 Mon Sep 17 00:00:00 2001 From: Yuku on yuku8 Date: Mon, 27 Jul 2015 11:52:50 +0800 Subject: [PATCH 01/22] Handle invalid int in footnote and xref tags --- .../alkitab/base/widget/VerseRenderer.java | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/Alkitab/src/main/java/yuku/alkitab/base/widget/VerseRenderer.java b/Alkitab/src/main/java/yuku/alkitab/base/widget/VerseRenderer.java index 17dc18b79..8ebf933f2 100644 --- a/Alkitab/src/main/java/yuku/alkitab/base/widget/VerseRenderer.java +++ b/Alkitab/src/main/java/yuku/alkitab/base/widget/VerseRenderer.java @@ -2,6 +2,8 @@ import android.graphics.Paint.FontMetricsInt; import android.graphics.Typeface; +import android.os.Handler; +import android.os.Looper; import android.support.annotation.Nullable; import android.text.SpannableStringBuilder; import android.text.TextPaint; @@ -12,7 +14,9 @@ import android.text.style.MetricAffectingSpan; import android.text.style.StyleSpan; import android.widget.TextView; +import android.widget.Toast; import yuku.afw.storage.Preferences; +import yuku.alkitab.base.App; import yuku.alkitab.base.S; import yuku.alkitab.base.U; import yuku.alkitab.base.util.Appearances; @@ -317,21 +321,42 @@ static void processSpecialTag(final SpannableStringBuilder sb, final StringBuild if (tag.length() >= 2) { // Footnote if (tag.charAt(0) == 'f') { - final int field = Integer.parseInt(tag.substring(1)); - appendSuperscriptNumber(sb, field); - if (inlineLinkSpanFactory != null) { - sb.setSpan(inlineLinkSpanFactory.create(VerseInlineLinkSpan.Type.footnote, ari << 8 | field), sb_len, sb.length(), 0); + try { + final int field = Integer.parseInt(tag.substring(1)); + appendSuperscriptNumber(sb, field); + if (inlineLinkSpanFactory != null) { + sb.setSpan(inlineLinkSpanFactory.create(VerseInlineLinkSpan.Type.footnote, ari << 8 | field), sb_len, sb.length(), 0); + } + } catch (NumberFormatException e) { + reportInvalidSpecialTag("Invalid footnote tag at ari 0x" + Integer.toHexString(ari) + ": " + tag); } } else if (tag.charAt(0) == 'x') { - sb.append(XREF_MARK); // star mark - final int field = Integer.parseInt(tag.substring(1)); - if (inlineLinkSpanFactory != null) { - sb.setSpan(inlineLinkSpanFactory.create(VerseInlineLinkSpan.Type.xref, ari << 8 | field), sb_len, sb.length(), 0); + try { + final int field = Integer.parseInt(tag.substring(1)); + sb.append(XREF_MARK); // star mark + if (inlineLinkSpanFactory != null) { + sb.setSpan(inlineLinkSpanFactory.create(VerseInlineLinkSpan.Type.xref, ari << 8 | field), sb_len, sb.length(), 0); + } + } catch (NumberFormatException e) { + reportInvalidSpecialTag("Invalid xref tag at ari 0x" + Integer.toHexString(ari) + ": " + tag); } } } } + static Toast invalidSpecialTagToast; + + static void reportInvalidSpecialTag(final String msg) { + new Handler(Looper.getMainLooper()).post(() -> { + if (invalidSpecialTagToast == null) { + invalidSpecialTagToast = Toast.makeText(App.context, msg, Toast.LENGTH_SHORT); + } else { + invalidSpecialTagToast.setText(msg); + } + invalidSpecialTagToast.show(); + }); + } + public static void appendSuperscriptNumber(final SpannableStringBuilder sb, final int field) { if (field >= 0 && field < 10) { sb.append(superscriptDigits[field]); From e14d2d766feace7406f1e13b3bacef8fa52e8872 Mon Sep 17 00:00:00 2001 From: Yuku on yuku8 Date: Mon, 27 Jul 2015 11:57:23 +0800 Subject: [PATCH 02/22] Do not crash if the target xref is not available. --- .../src/main/java/yuku/alkitab/base/dialog/XrefDialog.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java b/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java index 868d466af..c75c05d7b 100644 --- a/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java +++ b/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java @@ -169,7 +169,12 @@ void showVerses(int linkPos, String encodedTarget) { class Verses extends SingleChapterVerses { @Override public String getVerse(int verse_0) { - return displayedVerseTexts.get(verse_0); + final String res = displayedVerseTexts.get(verse_0); + // prevent crash if the target xref is not available + if (res == null) { + return getString(R.string.generic_verse_not_available_in_this_version); + } + return res; } @Override public int getVerseCount() { From 1e9d21e345c0443cedc14a9729f044bff14b448d Mon Sep 17 00:00:00 2001 From: Yuku on yuku8 Date: Mon, 27 Jul 2015 12:13:45 +0800 Subject: [PATCH 03/22] Prevent crash when loading nonexisting xref. --- .../yuku/alkitab/base/dialog/XrefDialog.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java b/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java index c75c05d7b..63cc1cb13 100644 --- a/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java +++ b/Alkitab/src/main/java/yuku/alkitab/base/dialog/XrefDialog.java @@ -13,6 +13,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import com.afollestad.materialdialogs.MaterialDialog; import yuku.afw.D; import yuku.afw.V; import yuku.alkitab.debug.R; @@ -32,11 +33,12 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; public class XrefDialog extends BaseDialog { public static final String TAG = XrefDialog.class.getSimpleName(); - private static final String EXTRA_arif = "arif"; //$NON-NLS-1$ + private static final String EXTRA_arif = "arif"; public interface XrefDialogListener { void onVerseSelected(XrefDialog dialog, int arif_source, int ari_target); @@ -87,7 +89,7 @@ public static XrefDialog newInstance(int arif) { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View res = inflater.inflate(R.layout.dialog_xref, null); + View res = inflater.inflate(R.layout.dialog_xref, container, false); tXrefText = V.get(res, R.id.tXrefText); versesView = V.get(res, R.id.versesView); @@ -97,11 +99,28 @@ public static XrefDialog newInstance(int arif) { versesView.setVerseSelectionMode(VerseSelectionMode.singleClick); versesView.setSelectedVersesListener(versesView_selectedVerses); tXrefText.setMovementMethod(LinkMovementMethod.getInstance()); - renderXrefText(); - + + if (xrefEntry != null) { + renderXrefText(); + } else { + new MaterialDialog.Builder(getActivity()) + .content(String.format(Locale.US, "Error: xref at arif 0x%08x couldn't be loaded", arif_source)) + .positiveText(R.string.ok) + .show(); + } + return res; } - + + @Override + public void onActivityCreated(final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + if (xrefEntry == null) { + dismiss(); + } + } + void renderXrefText() { final SpannableStringBuilder sb = new SpannableStringBuilder(); sb.append(VerseRenderer.XREF_MARK); From 9e10bd4e1cfcb5e46add0dba655521c5276a8112 Mon Sep 17 00:00:00 2001 From: Yuku on yuku8 Date: Mon, 27 Jul 2015 12:14:56 +0800 Subject: [PATCH 04/22] Project files --- .idea/codeStyleSettings.xml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml index 8edb84464..1eb339ccd 100644 --- a/.idea/codeStyleSettings.xml +++ b/.idea/codeStyleSettings.xml @@ -23,6 +23,34 @@ + +