Skip to content

Commit

Permalink
Add Android Request Interception
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmassel committed Oct 16, 2024
1 parent c31879b commit ac9d15f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Demo-Android/Gutenberg/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.webkit)
implementation(platform(libs.okhttp.bom))
implementation(libs.okhttp)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.wordpress.gutenberg

import android.webkit.WebResourceRequest

public interface GutenbergRequestInterceptor {
fun interceptRequest(request: WebResourceRequest): WebResourceRequest
}

class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor {
override fun interceptRequest(request: WebResourceRequest): WebResourceRequest {
return request
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.webkit.WebViewAssetLoader
import androidx.webkit.WebViewAssetLoader.AssetsPathHandler
import okhttp3.Headers.Companion.toHeaders
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okio.IOException
import org.json.JSONObject
import java.lang.ref.WeakReference

Expand Down Expand Up @@ -48,10 +53,15 @@ class GutenbergView : WebView {
var editorDidBecomeAvailable: ((GutenbergView) -> Unit)? = null
var filePathCallback: ValueCallback<Array<Uri?>?>? = null
val pickImageRequestCode = 1

var requestInterceptor: GutenbergRequestInterceptor = DefaultGutenbergRequestInterceptor()

private var onFileChooserRequested: WeakReference<((Intent, Int) -> Unit)?>? = null
private var contentChangeListener: WeakReference<ContentChangeListener>? = null
private var editorDidBecomeAvailableListener: EditorAvailableListener? = null

private val httpClient = OkHttpClient()

fun setContentChangeListener(listener: ContentChangeListener) {
contentChangeListener = WeakReference(listener)
}
Expand Down Expand Up @@ -103,10 +113,34 @@ class GutenbergView : WebView {
hasSetEditorConfig = true
}

return if (request?.url != null) {
assetLoader.shouldInterceptRequest(request.url)
if (request?.url == null) {
return super.shouldInterceptRequest(view, request)
} else if(request.url.host?.contains("appassets.androidplatform.net") == true) {
return assetLoader.shouldInterceptRequest(request.url)
} else {
super.shouldInterceptRequest(view, request)
val modifiedRequest = requestInterceptor.interceptRequest(request)

try {
val okHttpRequest = Request.Builder()
.url(modifiedRequest.url!!.toString())
.headers(modifiedRequest.requestHeaders.toHeaders())
.build()

val response: Response = httpClient.newCall(okHttpRequest).execute()

val body = if(response.body != null) { response.body!! } else { return null }
val contentType = if(body.contentType() != null) { body.contentType() } else { return null }

return WebResourceResponse(
contentType.toString(),
response.header("content-encoding", null),
body.byteStream()
)
} catch (e: IOException) {
// We don't need to handle this ourselves, just tell the WebView that
// we weren't able to fetch the resource
return null;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Demo-Android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ appcompat = "1.7.0"
material = "1.12.0"
activity = "1.9.0"
constraintlayout = "2.1.4"
okhttp = "4.12.0"
webkit = "1.11.0"

[libraries]
Expand All @@ -21,6 +22,8 @@ material = { group = "com.google.android.material", name = "material", version.r
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
androidx-webkit = { group = "androidx.webkit", name = "webkit", version.ref = "webkit" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttp-bom = { module = "com.squareup.okhttp3:okhttp-bom", version.ref = "okhttp" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit ac9d15f

Please sign in to comment.