haerzig.core.activities.CopyrightNoticesActivity.kt Maven / Gradle / Ivy
package haerzig.core.activities
import android.content.res.Configuration
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.HtmlCompat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import haerzig.core.CopyrightNotice
import haerzig.core.EXTRA_COPYRIGHTS
import haerzig.core.R
import haerzig.core.themes.AppTheme
import haerzig.core.views.TopAppBar
class CopyrightNoticesActivity : ComponentActivity() {
@Preview(
uiMode = Configuration.UI_MODE_NIGHT_YES,
name = "DefaultPreviewDark"
)
@Preview(
uiMode = Configuration.UI_MODE_NIGHT_NO,
name = "DefaultPreviewLight"
)
@Composable
fun Preview() {
AppTheme {
Surface {
ContentView()
}
}
}
private lateinit var mContent: String
private var copyrightNotices = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
copyrightNotices.addAll(Gson().fromJson(savedInstanceState?.getString(EXTRA_COPYRIGHTS) ?: "", object : TypeToken>() {}.type))
val b = StringBuilder()
b.append(
"" +
""
)
for (copyrightNotice in copyrightNotices) {
var text = copyrightNotice.getText(this)
// Reflow text, i.e. remove line breaks unless a paragraph
// - "\" at line endings allows preserving line breaks
// - The " *" trims spaces at line beginnings
text = text.replace("\r\n", "\n")
text = text.replace("([^\\n\\\\])\\n *([^\\n])".toRegex(), "$1 $2")
text = text.replace("\\\n", "\n")
// Make links clickable
text = text.replace("<(http.*)>".toRegex(), "$1")
// Convert line breaks to
text = text.replace("\\n".toRegex(), "
")
// Replace/add copyright symbols
text = text.replace("©", "©")
text = text.replace("(C)", "©")
text = text.replace("(c)", "©")
// Remove double dot when owner ends with "Inc."
text = text.replace("..", ".")
b.append("").append(copyrightNotice.title).append("
")
.append(" (")
.append(copyrightNotice.url).append(")
")
.append("").append(text).append("
")
}
b.append("")
mContent = b.toString()
setContent {
AppTheme {
Surface {
ContentView()
}
}
}
}
@Composable
fun ContentView() {
Scaffold(
topBar = { TopAppBar(title = stringResource(R.string.copyright_notices), onGoBack = { finish() }) }
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(start = 10.dp, bottom = it.calculateBottomPadding(), top = it.calculateTopPadding())
) {
AndroidView(factory = { context ->
TextView(context).apply {
text = HtmlCompat.fromHtml(mContent, HtmlCompat.FROM_HTML_MODE_LEGACY)
movementMethod = LinkMovementMethod.getInstance()
}
})
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy