Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42d2ac89 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Add underline to urls in AnnotatedStringResource" into main

parents 1733e375 c34a9dde
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -24,4 +24,6 @@
    <string name="single_line_summary_preference_title" translatable="false">Preference (singleLineSummary = true)</string>
    <string name="single_line_summary_preference_title" translatable="false">Preference (singleLineSummary = true)</string>
    <!-- Summary for single line summary preference. [DO NOT TRANSLATE] -->
    <!-- Summary for single line summary preference. [DO NOT TRANSLATE] -->
    <string name="single_line_summary_preference_summary" translatable="false">A very long summary to show case a preference which only shows a single line summary.</string>
    <string name="single_line_summary_preference_summary" translatable="false">A very long summary to show case a preference which only shows a single line summary.</string>
    <!-- Footer text with two links. [DO NOT TRANSLATE] -->
    <string name="footer_with_two_links" translatable="false">Annotated string with <a href="https://www.android.com/">link 1</a> and <a href="https://source.android.com/">link 2</a>.</string>
</resources>
</resources>
+6 −1
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright (C) 2022 The Android Open Source Project
 * Copyright (C) 2023 The Android Open Source Project
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -28,9 +28,11 @@ import com.android.settingslib.spa.framework.common.createSettingsPage
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.gallery.R
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spa.widget.scaffold.RegularScaffold
import com.android.settingslib.spa.widget.scaffold.RegularScaffold
import com.android.settingslib.spa.widget.ui.AnnotatedText
import com.android.settingslib.spa.widget.ui.Footer
import com.android.settingslib.spa.widget.ui.Footer


private const val TITLE = "Sample Footer"
private const val TITLE = "Sample Footer"
@@ -78,6 +80,9 @@ object FooterPageProvider : SettingsPageProvider {
                entry.UiLayout()
                entry.UiLayout()
            }
            }
            Footer(footerText = "Footer text always at the end of page.")
            Footer(footerText = "Footer text always at the end of page.")
            Footer {
                AnnotatedText(R.string.footer_with_two_links)
            }
        }
        }
    }
    }
}
}
+60 −72
Original line number Original line Diff line number Diff line
@@ -16,105 +16,93 @@


package com.android.settingslib.spa.framework.util
package com.android.settingslib.spa.framework.util


import android.content.res.Resources
import android.graphics.Typeface
import android.graphics.Typeface
import android.text.Spanned
import android.text.Spanned
import android.text.style.StyleSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.text.style.URLSpan
import androidx.annotation.StringRes
import androidx.annotation.StringRes
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Density
import androidx.compose.ui.text.style.TextDecoration


const val URLSPAN_TAG = "URLSPAN_TAG"
const val URL_SPAN_TAG = "URL_SPAN_TAG"


@Composable
@Composable
fun annotatedStringResource(@StringRes id: Int, urlSpanColor: Color): AnnotatedString {
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
    LocalConfiguration.current
    val resources = LocalContext.current.resources
    val resources = LocalContext.current.resources
    val density = LocalDensity.current
    val urlSpanColor = MaterialTheme.colorScheme.primary
    return remember(id) {
    return remember(id) {
        val text = resources.getText(id)
        val text = resources.getText(id)
        spannableStringToAnnotatedString(text, density, urlSpanColor)
        spannableStringToAnnotatedString(text, urlSpanColor)
    }
    }
}
}


private fun spannableStringToAnnotatedString(text: CharSequence, density: Density, urlSpanColor: Color): AnnotatedString {
private fun spannableStringToAnnotatedString(text: CharSequence, urlSpanColor: Color) =
    return if (text is Spanned) {
    if (text is Spanned) {
        with(density) {
        buildAnnotatedString {
        buildAnnotatedString {
            append((text.toString()))
            append((text.toString()))
                text.getSpans(0, text.length, Any::class.java).forEach {
            for (span in text.getSpans(0, text.length, Any::class.java)) {
                    val start = text.getSpanStart(it)
                val start = text.getSpanStart(span)
                    val end = text.getSpanEnd(it)
                val end = text.getSpanEnd(span)
                    when (it) {
                when (span) {
                        is StyleSpan ->
                    is StyleSpan -> addStyleSpan(span, start, end)
                            when (it.style) {
                    is URLSpan -> addUrlSpan(span, urlSpanColor, start, end)
                    else -> addStyle(SpanStyle(), start, end)
                }
            }
        }
    } else {
        AnnotatedString(text.toString())
    }

private fun AnnotatedString.Builder.addStyleSpan(styleSpan: StyleSpan, start: Int, end: Int) {
    when (styleSpan.style) {
        Typeface.NORMAL -> addStyle(
        Typeface.NORMAL -> addStyle(
                                        SpanStyle(
            SpanStyle(fontWeight = FontWeight.Normal, fontStyle = FontStyle.Normal),
                                                fontWeight = FontWeight.Normal,
                                                fontStyle = FontStyle.Normal
                                        ),
            start,
            start,
                                        end
            end,
        )
        )

        Typeface.BOLD -> addStyle(
        Typeface.BOLD -> addStyle(
                                        SpanStyle(
            SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Normal),
                                                fontWeight = FontWeight.Bold,
                                                fontStyle = FontStyle.Normal
                                        ),
            start,
            start,
                                        end
            end,
        )
        )

        Typeface.ITALIC -> addStyle(
        Typeface.ITALIC -> addStyle(
                                        SpanStyle(
            SpanStyle(fontWeight = FontWeight.Normal, fontStyle = FontStyle.Italic),
                                                fontWeight = FontWeight.Normal,
                                                fontStyle = FontStyle.Italic
                                        ),
            start,
            start,
                                        end
            end,
        )
        )

        Typeface.BOLD_ITALIC -> addStyle(
        Typeface.BOLD_ITALIC -> addStyle(
                                        SpanStyle(
            SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic),
                                                fontWeight = FontWeight.Bold,
                                                fontStyle = FontStyle.Italic
                                        ),
            start,
            start,
                                        end
            end,
        )
        )
    }
    }
                        is URLSpan -> {
}

private fun AnnotatedString.Builder.addUrlSpan(
    urlSpan: URLSpan,
    urlSpanColor: Color,
    start: Int,
    end: Int,
) {
    addStyle(
    addStyle(
                                    SpanStyle(
        SpanStyle(color = urlSpanColor, textDecoration = TextDecoration.Underline),
                                            color = urlSpanColor,
                                    ),
                                    start,
                                    end
                            )
                            if (!it.url.isNullOrEmpty()) {
                                addStringAnnotation(
                                        URLSPAN_TAG,
                                        it.url,
        start,
        start,
                                        end
        end,
    )
    )
                            }
    if (!urlSpan.url.isNullOrEmpty()) {
                        }
        addStringAnnotation(URL_SPAN_TAG, urlSpan.url, start, end)
                        else -> addStyle(SpanStyle(), start, end)
                    }
                }
            }
        }
    } else {
        AnnotatedString(text.toString())
    }
    }
}
}
+42 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2023 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.settingslib.spa.widget.ui

import androidx.annotation.StringRes
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalUriHandler
import com.android.settingslib.spa.framework.util.URL_SPAN_TAG
import com.android.settingslib.spa.framework.util.annotatedStringResource

@Composable
fun AnnotatedText(@StringRes id: Int) {
    val uriHandler = LocalUriHandler.current
    val annotatedString = annotatedStringResource(id)
    ClickableText(
        text = annotatedString,
        style = MaterialTheme.typography.bodyMedium.copy(
            color = MaterialTheme.colorScheme.onSurfaceVariant,
        ),
    ) { offset ->
        // Gets the url at the clicked position.
        annotatedString.getStringAnnotations(URL_SPAN_TAG, offset, offset)
            .firstOrNull()
            ?.let { uriHandler.openUri(it.item) }
    }
}
+3 −1
Original line number Original line Diff line number Diff line
@@ -26,5 +26,7 @@
        other {There are # songs found in {place}.}
        other {There are # songs found in {place}.}
    }</string>
    }</string>


    <string name="test_annotated_string_resource">Annotated string with <b>bold</b> and <a href="https://www.google.com/">link</a>.</string>
    <string name="test_annotated_string_resource">Annotated string with <b>bold</b> and <a href="https://www.android.com/">link</a>.</string>

    <string name="test_link"><a href="https://www.android.com/">link</a></string>
</resources>
</resources>
Loading