Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LinkUtil.java « util « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c4b495d85cb393233f32f9b9b77d119ee11c535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package it.niedermann.nextcloud.deck.util;

import android.content.res.Resources;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

import it.niedermann.nextcloud.deck.R;

public final class LinkUtil {
    private LinkUtil() {
    }

    /**
     * Creates a {@link Spanned} from a HTML string on all SDK versions.
     *
     * @param source Source string with HTML markup
     * @return Spannable for using in a {@link TextView}
     * @see Html#fromHtml(String)
     * @see Html#fromHtml(String, int)
     */
    private static Spanned fromHtml(String source) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
        } else {
            return Html.fromHtml(source);
        }
    }

    /**
     * Fills a {@link TextView} with HTML content and activates links in that {@link TextView}.
     *
     * @param view      The {@link TextView} which should be filled.
     * @param stringIds The string resource containing HTML tags (escaped by <code>&lt;</code>)
     */
    public static void setHtml(TextView view, String... stringIds) {

        StringBuilder sb = new StringBuilder();
        for (String arg : stringIds) {
            sb.append(arg);
        }

        view.setText(LinkUtil.fromHtml(sb.toString()));
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public static String concatenateResources(Resources resources, int... stringIds) {
        StringBuilder sb = new StringBuilder();
        for (int arg : stringIds) {
            sb.append(resources.getString(arg));
        }
        return sb.toString();
    }

    public static String makeLink(Resources resources, String linkURL, String linkText) {
        return new StringBuilder()
                .append(resources.getString(R.string.anchor_start))
                .append(linkURL)
                .append(resources.getString(R.string.anchor_middle))
                .append(linkText)
                .append(resources.getString(R.string.anchor_end))
                .toString();
    }

    public static String makeLink(Resources resources, int linkURL, int linkText) {
        return makeLink(resources, resources.getString(linkURL), resources.getString(linkText));
    }
}