Skip to content

Commit

Permalink
Merge pull request #3880 from kiwix/Fixes#3878
Browse files Browse the repository at this point in the history
Fixed ServiceWorker Initialization Issue
  • Loading branch information
kelson42 authored Jun 15, 2024
2 parents ab5fb35 + 4c3af12 commit 9f94965
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/org/kiwix/kiwixmobile/core/CoreApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ abstract class CoreApp : Application() {
.build()
AndroidThreeTen.init(this)
coreComponent.inject(this)
serviceWorkerInitialiser.init(this)
downloadMonitor.init()
nightModeConfig.init()
fileLogger.writeLogFile(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package org.kiwix.kiwixmobile.core

import android.app.ActivityManager
import android.content.Context
import android.os.Process
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import androidx.webkit.ServiceWorkerClientCompat
Expand All @@ -26,14 +29,23 @@ import androidx.webkit.WebViewFeature
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import javax.inject.Inject

class ServiceWorkerInitialiser @Inject constructor(zimReaderContainer: ZimReaderContainer) {
init {
if (WebViewFeature.isFeatureSupported(WebViewFeature.SERVICE_WORKER_BASIC_USAGE)) {
class ServiceWorkerInitialiser @Inject constructor(val zimReaderContainer: ZimReaderContainer) {
fun init(context: Context) {
if (isMainProcess(context) &&
WebViewFeature.isFeatureSupported(WebViewFeature.SERVICE_WORKER_BASIC_USAGE)
) {
ServiceWorkerControllerCompat.getInstance()
.setServiceWorkerClient(object : ServiceWorkerClientCompat() {
override fun shouldInterceptRequest(request: WebResourceRequest): WebResourceResponse? =
zimReaderContainer.load(request.url.toString(), request.requestHeaders)
})
}
}

private fun isMainProcess(context: Context): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
return activityManager.runningAppProcesses.find {
it.pid == Process.myPid()
}?.processName == context.packageName
}
}

0 comments on commit 9f94965

Please sign in to comment.