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

Commit c34a9dde authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add underline to urls in AnnotatedStringResource

To match other urls in Settings.

Also simplify the footer content for TogglePermissionAppList.

Fix: 294008469
Test: unit test
Change-Id: I540bea8c85ea8aa6434819aad485bfa7570886cb
parent ebec011d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -24,4 +24,6 @@
    <string name="single_line_summary_preference_title" translatable="false">Preference (singleLineSummary = true)</string>
    <!-- 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>
    <!-- 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>
+6 −1
Original line number 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");
 * 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.stateOf
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.PreferenceModel
import com.android.settingslib.spa.widget.scaffold.RegularScaffold
import com.android.settingslib.spa.widget.ui.AnnotatedText
import com.android.settingslib.spa.widget.ui.Footer

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

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

import android.content.res.Resources
import android.graphics.Typeface
import android.text.Spanned
import android.text.style.StyleSpan
import android.text.style.URLSpan
import androidx.annotation.StringRes
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
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.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
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
fun annotatedStringResource(@StringRes id: Int, urlSpanColor: Color): AnnotatedString {
    LocalConfiguration.current
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
    val resources = LocalContext.current.resources
    val density = LocalDensity.current
    val urlSpanColor = MaterialTheme.colorScheme.primary
    return remember(id) {
        val text = resources.getText(id)
        spannableStringToAnnotatedString(text, density, urlSpanColor)
        spannableStringToAnnotatedString(text, urlSpanColor)
    }
}

private fun spannableStringToAnnotatedString(text: CharSequence, density: Density, urlSpanColor: Color): AnnotatedString {
    return if (text is Spanned) {
        with(density) {
private fun spannableStringToAnnotatedString(text: CharSequence, urlSpanColor: Color) =
    if (text is Spanned) {
        buildAnnotatedString {
            append((text.toString()))
                text.getSpans(0, text.length, Any::class.java).forEach {
                    val start = text.getSpanStart(it)
                    val end = text.getSpanEnd(it)
                    when (it) {
                        is StyleSpan ->
                            when (it.style) {
            for (span in text.getSpans(0, text.length, Any::class.java)) {
                val start = text.getSpanStart(span)
                val end = text.getSpanEnd(span)
                when (span) {
                    is StyleSpan -> addStyleSpan(span, start, end)
                    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(
                                        SpanStyle(
                                                fontWeight = FontWeight.Normal,
                                                fontStyle = FontStyle.Normal
                                        ),
            SpanStyle(fontWeight = FontWeight.Normal, fontStyle = FontStyle.Normal),
            start,
                                        end
            end,
        )

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

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

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

private fun AnnotatedString.Builder.addUrlSpan(
    urlSpan: URLSpan,
    urlSpanColor: Color,
    start: Int,
    end: Int,
) {
    addStyle(
                                    SpanStyle(
                                            color = urlSpanColor,
                                    ),
                                    start,
                                    end
                            )
                            if (!it.url.isNullOrEmpty()) {
                                addStringAnnotation(
                                        URLSPAN_TAG,
                                        it.url,
        SpanStyle(color = urlSpanColor, textDecoration = TextDecoration.Underline),
        start,
                                        end
        end,
    )
                            }
                        }
                        else -> addStyle(SpanStyle(), start, end)
                    }
                }
            }
        }
    } else {
        AnnotatedString(text.toString())
    if (!urlSpan.url.isNullOrEmpty()) {
        addStringAnnotation(URL_SPAN_TAG, urlSpan.url, start, end)
    }
}
+42 −0
Original line number 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 Diff line number Diff line
@@ -26,5 +26,7 @@
        other {There are # songs found in {place}.}
    }</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>
Loading