summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/accessibility/floatingmenu/AnnotationLinkSpan.java
blob: 0d7551ff66e950a9135380f16d32f7a82763cca9 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.accessibility.floatingmenu;

import android.text.Annotation;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.Optional;

/**
 * A span that turns the text wrapped by annotation tag into the clickable link text.
 */
public class AnnotationLinkSpan extends ClickableSpan {
    private final Optional<View.OnClickListener> mClickListener;

    private AnnotationLinkSpan(View.OnClickListener listener) {
        mClickListener = Optional.ofNullable(listener);
    }

    @Override
    public void onClick(View view) {
        mClickListener.ifPresent(listener -> listener.onClick(view));
    }

    /**
     * Makes the text has the link with the click action. In addition, the span will match first
     * LinkInfo and attach into the text.
     *
     * @param text the text wrapped by annotation tag
     * @param linkInfos used to attach the click action into the corresponding span
     * @return the text attached with the span
     */
    public static CharSequence linkify(CharSequence text, LinkInfo... linkInfos) {
        final SpannableString msg = new SpannableString(text);
        final Annotation[] spans =
                msg.getSpans(/* queryStart= */ 0, msg.length(), Annotation.class);
        final SpannableStringBuilder builder = new SpannableStringBuilder(msg);

        Arrays.asList(spans).forEach(annotationTag -> {
            final String key = annotationTag.getValue();
            final Optional<LinkInfo> linkInfo =
                    Arrays.asList(linkInfos).stream().filter(
                            info -> info.mAnnotation.isPresent()
                                    && info.mAnnotation.get().equals(key)).findFirst();

            linkInfo.flatMap(info -> info.mListener).ifPresent(listener -> {
                final AnnotationLinkSpan span = new AnnotationLinkSpan(listener);
                builder.setSpan(span,
                        msg.getSpanStart(annotationTag),
                        msg.getSpanEnd(annotationTag),
                        msg.getSpanFlags(span));
            });
        });

        return builder;
    }

    /**
     * Data class to store the annotation and the click action.
     */
    public static class LinkInfo {
        public static final String DEFAULT_ANNOTATION = "link";
        private final Optional<String> mAnnotation;
        private final Optional<View.OnClickListener> mListener;

        public LinkInfo(@NonNull String annotation, View.OnClickListener listener) {
            mAnnotation = Optional.of(annotation);
            mListener = Optional.ofNullable(listener);
        }
    }
}