2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui;
20 import android.app.AlertDialog;
21 import android.content.Context;
22 import android.text.SpannableStringBuilder;
23 import android.text.Spanned;
24 import android.text.TextUtils;
25 import android.text.method.LinkMovementMethod;
26 import android.text.style.URLSpan;
27 import android.widget.TextView;
29 import androidx.annotation.ArrayRes;
30 import androidx.annotation.StringRes;
32 import net.ktnx.mobileledger.R;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 public class HelpDialog {
38 private final static Pattern MARKDOWN_LINK_PATTERN =
39 Pattern.compile("\\[([^\\[]+)]\\(([^)]*)\\)");
40 public static void show(Context context, @StringRes int title, @ArrayRes int content) {
41 AlertDialog.Builder adb = new AlertDialog.Builder(context);
43 String message = TextUtils.join("\n\n", context.getResources()
44 .getStringArray(content));
46 SpannableStringBuilder richTextMessage = new SpannableStringBuilder();
48 Matcher m = MARKDOWN_LINK_PATTERN.matcher(message);
50 richTextMessage.append(message.substring(0, m.start()));
51 String linkText = m.group(1);
52 assert linkText != null;
53 String linkURL = m.group(2);
54 assert linkURL != null;
56 if (linkText.isEmpty())
59 int spanStart = richTextMessage.length();
60 richTextMessage.append(linkText);
61 richTextMessage.setSpan(new URLSpan(linkURL), spanStart,
62 spanStart + linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
63 URLSpan linkSpan = new URLSpan(linkText);
65 message = message.substring(m.end());
68 richTextMessage.append(message);
72 adb.setMessage(richTextMessage);
73 adb.setPositiveButton(R.string.close_button, (dialog, buttonId) -> dialog.dismiss());
74 final AlertDialog dialog = adb.create();
76 ((TextView) dialog.findViewById(android.R.id.message)).setMovementMethod(
77 LinkMovementMethod.getInstance());