Fix stub app loading on older Android versions

This commit is contained in:
topjohnwu
2022-06-05 01:09:30 -07:00
parent a3381da7ed
commit 9016e6727d
6 changed files with 54 additions and 40 deletions

View File

@@ -4,6 +4,7 @@ package com.topjohnwu.magisk.core
import android.content.ComponentName
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.res.AssetManager
import android.content.res.Configuration
@@ -13,16 +14,12 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.StubApk
import com.topjohnwu.magisk.core.di.AppContext
import com.topjohnwu.magisk.core.utils.syncLocale
import com.topjohnwu.magisk.ktx.unwrap
lateinit var AppApkPath: String
fun Resources.addAssetPath(path: String) = StubApk.addAssetPath(this, path)
fun Context.patch(): Context {
resources.patch()
return this
}
fun Resources.patch(): Resources {
if (isRunningAsStub)
addAssetPath(AppApkPath)
@@ -30,6 +27,21 @@ fun Resources.patch(): Resources {
return this
}
fun Context.patch(): Context {
unwrap().resources.patch()
return this
}
// Wrapping is only necessary for ContextThemeWrapper to support configuration overrides
fun Context.wrap(): Context {
patch()
return object : ContextWrapper(this) {
override fun createConfigurationContext(config: Configuration): Context {
return super.createConfigurationContext(config).wrap()
}
}
}
fun createNewResources(): Resources {
val asset = AssetManager::class.java.newInstance()
val config = Configuration(AppContext.resources.configuration)

View File

@@ -5,7 +5,6 @@ import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
@@ -18,9 +17,9 @@ import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.isRunningAsStub
import com.topjohnwu.magisk.core.patch
import com.topjohnwu.magisk.core.utils.RequestInstall
import com.topjohnwu.magisk.core.utils.UninstallPackage
import com.topjohnwu.magisk.core.wrap
import com.topjohnwu.magisk.ktx.reflectField
import com.topjohnwu.magisk.utils.Utils
import java.util.concurrent.CountDownLatch
@@ -56,11 +55,7 @@ abstract class BaseActivity : AppCompatActivity() {
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base.patch())
}
override fun createConfigurationContext(config: Configuration): Context {
return super.createConfigurationContext(config).patch()
super.attachBaseContext(base.wrap())
}
override fun onCreate(savedInstanceState: Bundle?) {