refactor: adopt androidx.core KTX helpers (Bitmap/Uri/SharedPreferences)

This commit is contained in:
davotoula
2026-07-11 17:00:40 +01:00
parent 56e4b1ec7d
commit 0053edf2dd
18 changed files with 63 additions and 29 deletions
@@ -54,6 +54,8 @@ import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.core.graphics.Insets
import androidx.core.graphics.scale
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.webkit.JavaScriptReplyProxy
@@ -456,7 +458,7 @@ class NappletBrowserActivity : ComponentActivity() {
runCatching {
val scaled =
if (icon.width > ICON_MAX_PX || icon.height > ICON_MAX_PX) {
Bitmap.createScaledBitmap(icon, ICON_MAX_PX, ICON_MAX_PX, true)
icon.scale(ICON_MAX_PX, ICON_MAX_PX)
} else {
icon
}
@@ -597,7 +599,7 @@ class NappletBrowserActivity : ComponentActivity() {
// Key the persisted choice on the host actually displayed (which may differ from startUrl after
// in-page navigation), so the preference sticks to the right site.
val liveUrl = if (this::webView.isInitialized) webView.url ?: startUrl else startUrl
val host = runCatching { Uri.parse(liveUrl).host }.getOrNull()?.takeIf { it.isNotBlank() } ?: return
val host = runCatching { liveUrl.toUri().host }.getOrNull()?.takeIf { it.isNotBlank() } ?: return
val msg =
Message.obtain(null, NappletIpc.MSG_SET_WEB_TOR).apply {
data =
@@ -613,7 +615,7 @@ class NappletBrowserActivity : ComponentActivity() {
private var title: String = ""
private fun barTitle(): String = title.ifBlank { runCatching { Uri.parse(startUrl).host }.getOrNull() ?: getString(CommonsR.string.napplet_untitled) }
private fun barTitle(): String = title.ifBlank { runCatching { startUrl.toUri().host }.getOrNull() ?: getString(CommonsR.string.napplet_untitled) }
/**
* The top pull-down sheet: a small grabber at the top edge (out of the corner where a site shows its
@@ -644,7 +646,7 @@ class NappletBrowserActivity : ComponentActivity() {
*/
private fun openPermissions() {
val liveUrl = if (this::webView.isInitialized) webView.url ?: startUrl else startUrl
val uri = runCatching { Uri.parse(liveUrl) }.getOrNull() ?: return
val uri = runCatching { liveUrl.toUri() }.getOrNull() ?: return
val scheme = uri.scheme?.takeIf { it.isNotBlank() } ?: return
val host = uri.host?.takeIf { it.isNotBlank() } ?: return
val origin = "$scheme://$host" + if (uri.port > 0) ":${uri.port}" else ""
@@ -774,6 +776,6 @@ class NappletBrowserActivity : ComponentActivity() {
.putExtra(EXTRA_THEME, theme)
.putExtra(EXTRA_IS_FAVORITE, isFavorite)
// Distinct task identity per URL for documentLaunchMode=intoExisting.
.setData(Uri.parse(url))
.setData(url.toUri())
}
}
@@ -45,6 +45,7 @@ import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.RequiresApi
import androidx.core.graphics.createBitmap
import androidx.privacysandbox.ui.provider.toCoreLibInfo
import androidx.webkit.JavaScriptReplyProxy
import androidx.webkit.WebMessageCompat
@@ -206,7 +207,7 @@ class NappletBrowserService : Service() {
val outW = (boxW * zoom).toInt().coerceAtLeast(1)
val outH = (boxH * zoom).toInt().coerceAtLeast(1)
val t0 = SystemClock.elapsedRealtimeNanos()
val bitmap = Bitmap.createBitmap(outW, outH, Bitmap.Config.ARGB_8888)
val bitmap = createBitmap(outW, outH)
val canvas = Canvas(bitmap)
canvas.drawColor(tab.bgColor)
// Map the source rect (centered on cx,cy in view px) into the zoomed output bitmap.
@@ -45,6 +45,7 @@ import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.annotation.RequiresApi
import androidx.core.graphics.createBitmap
import androidx.privacysandbox.ui.provider.toCoreLibInfo
import androidx.webkit.JavaScriptReplyProxy
import androidx.webkit.ProxyConfig
@@ -238,7 +239,7 @@ class NappletHostService : Service() {
val outW = (boxW * zoom).toInt().coerceAtLeast(1)
val outH = (boxH * zoom).toInt().coerceAtLeast(1)
val t0 = SystemClock.elapsedRealtimeNanos()
val bitmap = Bitmap.createBitmap(outW, outH, Bitmap.Config.ARGB_8888)
val bitmap = createBitmap(outW, outH)
val canvas = Canvas(bitmap)
canvas.drawColor(tab.bgColor)
canvas.scale(zoom, zoom)