Yokan / rebuild
Course 1 8624561

BridgeEnvironment.kt

vendor/yokan-ext/src/androidMain/kotlin/com/kodjodevf/m_extension_server/BridgeEnvironment.kt

implementation78 lines
1package com.kodjodevf.m_extension_server23import android.app.Application4import android.content.Context5import android.content.SharedPreferences6import android.content.pm.PackageManager7import androidx.preference.PreferenceManager8import eu.kanade.tachiyomi.AppModule9import eu.kanade.tachiyomi.network.NetworkHelper10import uy.kohesive.injekt.Injekt11import uy.kohesive.injekt.api.get1213/** Minimal host environment retained by the Mihon compatibility bridge. */14class MExtensionServerPlugin internal constructor(15    val applicationContext: Context,16)1718/**19 * The Application handed to extensions. Every SharedPreferences name (source_<id>, default20 * preferences) is prefixed with the owning profile's [preferencesPrefix]; "" keeps legacy names.21 */22private class ProfileBridgeApplication(base: Application) : Application() {23    @Volatile24    var preferencesPrefix = ""2526    init {27        attachBaseContext(base)28    }2930    override fun getApplicationContext(): Context = this3132    override fun getSharedPreferences(name: String?, mode: Int): SharedPreferences =33        super.getSharedPreferences(name?.let { preferencesPrefix + it }, mode)34}3536var instance: MExtensionServerPlugin? = null37    private set38var pm: PackageManager? = null39    private set40var preferenceManager: PreferenceManager? = null41    private set4243private var bridgeApplication: ProfileBridgeApplication? = null44private var bridgeOwner: Any? = null4546/**47 * Claims the process-wide bridge for [owner]. Extensions share one Injekt graph, so only one48 * profile may run the bridge at a time; switching profile clears the shared network state.49 */50@Synchronized51fun acquireBridgeEnvironment(owner: Any, context: Context, preferencesPrefix: String) {52    check(bridgeOwner == null || bridgeOwner === owner) {53        "The Mihon APK bridge is running for another profile; stop it there before starting it here"54    }55    val app = bridgeApplication ?: run {56        val base = context.applicationContext as? Application57            ?: throw IllegalArgumentException("Android bridge requires an Application context")58        ProfileBridgeApplication(base).also { created ->59            bridgeApplication = created60            instance = MExtensionServerPlugin(created)61            pm = created.packageManager62            preferenceManager = PreferenceManager(created)63            Injekt.importModule(AppModule(created))64        }65    }66    if (app.preferencesPrefix != preferencesPrefix) {67        app.preferencesPrefix = preferencesPrefix68        val network = Injekt.get<NetworkHelper>()69        network.cookieJar.clear()70        network.client.cache?.evictAll()71    }72    bridgeOwner = owner73}7475@Synchronized76fun releaseBridgeEnvironment(owner: Any) {77    if (bridgeOwner === owner) bridgeOwner = null78}79

Search chapters, reference and source filenames.