manager: Usse WebViewAssetLoader instead of file uri. refer: https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader

androidCompileSdkVersion changed to meet webview library requirements
This commit is contained in:
weishu 2024-02-23 16:12:53 +08:00
parent a829707b16
commit a99c69f9b4
4 changed files with 27 additions and 9 deletions

View File

@ -114,4 +114,5 @@ dependencies {
implementation(libs.sheet.compose.dialogs.input)
implementation(libs.markdown)
implementation(libs.androidx.webkit)
}

View File

@ -8,6 +8,8 @@ import android.os.Looper
import android.text.TextUtils
import android.view.Window
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.widget.Toast
import androidx.compose.foundation.layout.fillMaxSize
@ -21,6 +23,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.webkit.WebViewAssetLoader
import com.google.accompanist.web.AccompanistWebViewClient
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewState
import com.ramcosta.composedestinations.annotation.Destination
@ -31,6 +35,7 @@ import me.weishu.kernelsu.ui.util.createRootShell
import me.weishu.kernelsu.ui.util.serveModule
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.util.concurrent.CompletableFuture
@SuppressLint("SetJavaScriptEnabled")
@ -53,23 +58,37 @@ fun WebScreen(navigator: DestinationsNavigator, moduleId: String, moduleName: St
}
Scaffold { innerPadding ->
val webRoot = File(context.dataDir, "webroot")
val webViewAssetLoader = WebViewAssetLoader.Builder()
.addPathHandler("/", WebViewAssetLoader.InternalStoragePathHandler(context, webRoot))
.build()
val webViewClient = object : AccompanistWebViewClient() {
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
return webViewAssetLoader.shouldInterceptRequest(request.url)
}
}
WebView(
state = rememberWebViewState(url = "file:///data/data/me.weishu.kernelsu/webroot/index.html"),
state = rememberWebViewState(url = "https://appassets.androidplatform.net/index.html"),
Modifier
.fillMaxSize()
.padding(innerPadding),
client = webViewClient,
factory = { context ->
WebView(context).apply {
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.allowFileAccess = true
settings.allowFileAccess = false
addJavascriptInterface(WebViewInterface(context, this), "ksu")
}
})
}
}
class WebViewInterface(val context: Context, val webView: WebView) {
class WebViewInterface(val context: Context, private val webView: WebView) {
companion object {
var isHideSystemUI: Boolean = false
@ -109,10 +128,6 @@ class WebViewInterface(val context: Context, val webView: WebView) {
options: String?,
callbackFunc: String
) {
val opts = if (options == null) JSONObject() else {
JSONObject(options)
}
val finalCommand = StringBuilder()
processOptions(finalCommand, options)
finalCommand.append(cmd)

View File

@ -34,8 +34,8 @@ cmaker {
val androidMinSdkVersion = 26
val androidTargetSdkVersion = 33
val androidCompileSdkVersion = 33
val androidBuildToolsVersion = "33.0.2"
val androidCompileSdkVersion = 34
val androidBuildToolsVersion = "34.0.0"
val androidCompileNdkVersion = "25.2.9519653"
val androidSourceCompatibility = JavaVersion.VERSION_17
val androidTargetCompatibility = JavaVersion.VERSION_17

View File

@ -10,6 +10,7 @@ compose-destination = "1.9.42-beta"
libsu = "5.2.1"
sheets-compose-dialogs = "1.2.0"
markdown = "4.6.2"
webkit = "1.10.0"
[plugins]
agp-app = { id = "com.android.application", version.ref = "agp" }
@ -36,6 +37,7 @@ androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecyc
androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycle" }
androidx-webkit = { module = "androidx.webkit:webkit", version.ref = "webkit" }
com-google-accompanist-drawablepainter = { group = "com.google.accompanist", name = "accompanist-drawablepainter", version.ref = "accompanist" }
com-google-accompanist-navigation-animation = { group = "com.google.accompanist", name = "accompanist-navigation-animation", version.ref = "accompanist" }
com-google-accompanist-systemuicontroller = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" }