From 881d3b5221356291873c9ab5747fcaf8b8cdb618 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 11 Jul 2024 16:28:56 -0700 Subject: [PATCH] App migration minor refactoring --- .../com/topjohnwu/magisk/ui/SplashActivity.kt | 4 ++-- .../magisk/ui/settings/SettingsItems.kt | 4 ++-- .../magisk/ui/settings/SettingsViewModel.kt | 6 +++--- .../java/com/topjohnwu/magisk/core/AppContext.kt | 8 ++++---- .../magisk/core/download/DownloadEngine.kt | 4 ++-- .../core/tasks/{HideAPK.kt => AppMigration.kt} | 16 ++++++++-------- 6 files changed, 21 insertions(+), 21 deletions(-) rename app/core/src/main/java/com/topjohnwu/magisk/core/tasks/{HideAPK.kt => AppMigration.kt} (96%) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt index 4fe1dc366..6ebb05716 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt @@ -14,7 +14,7 @@ import com.topjohnwu.magisk.core.base.relaunch import com.topjohnwu.magisk.core.initializeOnSplashScreen import com.topjohnwu.magisk.core.isRunningAsStub import com.topjohnwu.magisk.core.ktx.toast -import com.topjohnwu.magisk.core.tasks.HideAPK +import com.topjohnwu.magisk.core.tasks.AppMigration import com.topjohnwu.magisk.ui.theme.Theme import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.superuser.Shell @@ -91,7 +91,7 @@ abstract class SplashActivity : NavigationActivity createHosts() DenyListConfig -> SettingsFragmentDirections.actionSettingsFragmentToDenyFragment().navigate() UpdateChannel -> openUrlIfNecessary(view) - is Hide -> viewModelScope.launch { HideAPK.hide(view.activity, item.value) } - Restore -> viewModelScope.launch { HideAPK.restore(view.activity) } + is Hide -> viewModelScope.launch { AppMigration.hide(view.activity, item.value) } + Restore -> viewModelScope.launch { AppMigration.restore(view.activity) } Zygisk -> if (Zygisk.mismatch) SnackbarEvent(R.string.reboot_apply_change).publish() else -> Unit } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt index ff84f2dc4..0965c2540 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/AppContext.kt @@ -22,7 +22,7 @@ import com.topjohnwu.magisk.core.base.UntrackedActivity import com.topjohnwu.magisk.core.base.launchPackage import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.ktx.writeTo -import com.topjohnwu.magisk.core.tasks.HideAPK +import com.topjohnwu.magisk.core.tasks.AppMigration import com.topjohnwu.magisk.core.utils.LocaleSetting import com.topjohnwu.magisk.core.utils.NetworkObserver import com.topjohnwu.magisk.core.utils.ProcessLifecycle @@ -178,15 +178,15 @@ where T : ComponentActivity, T : IActivityExtension { // Version mismatch Info.stub!!.version != BuildConfig.STUB_VERSION || // Not properly patched - intent.component!!.className.contains(HideAPK.PLACEHOLDER)) + intent.component!!.className.contains(AppMigration.PLACEHOLDER)) ) { withPermission(REQUEST_INSTALL_PACKAGES) { granted -> if (granted) { - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch { val apk = File(cacheDir, "stub.apk") try { assets.open("stub.apk").writeTo(apk) - HideAPK.upgrade(this@initializeOnSplashScreen, apk)?.let { + AppMigration.upgradeStub(this@initializeOnSplashScreen, apk)?.let { startActivity(it) } } catch (e: IOException) { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt index 161b86724..41523b125 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt @@ -33,7 +33,7 @@ import com.topjohnwu.magisk.core.ktx.forEach import com.topjohnwu.magisk.core.ktx.set import com.topjohnwu.magisk.core.ktx.withStreams import com.topjohnwu.magisk.core.ktx.writeTo -import com.topjohnwu.magisk.core.tasks.HideAPK +import com.topjohnwu.magisk.core.tasks.AppMigration import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.core.utils.ProgressInputStream import com.topjohnwu.magisk.utils.APKInstall @@ -292,7 +292,7 @@ class DownloadEngine( zf.close() // Patch and install - subject.intent = HideAPK.upgrade(context, apk) + subject.intent = AppMigration.upgradeStub(context, apk) ?: throw IOException("HideAPK patch error") apk.delete() } catch (e: Exception) { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/AppMigration.kt similarity index 96% rename from app/core/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt rename to app/core/src/main/java/com/topjohnwu/magisk/core/tasks/AppMigration.kt index d3bca3986..57c2ee522 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/AppMigration.kt @@ -6,7 +6,6 @@ import android.content.Context import android.content.Intent import android.os.Build import android.widget.Toast -import androidx.annotation.WorkerThread import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.BuildConfig.APP_PACKAGE_NAME import com.topjohnwu.magisk.core.Config @@ -33,7 +32,7 @@ import java.io.OutputStream import java.security.SecureRandom import kotlin.random.asKotlinRandom -object HideAPK { +object AppMigration { private const val ALPHA = "abcdefghijklmnopqrstuvwxyz" private const val ALPHADOTS = "$ALPHA....." @@ -258,16 +257,17 @@ object HideAPK { if (!success) onFailure.run() } - @WorkerThread - fun upgrade(context: Context, apk: File): Intent? { + suspend fun upgradeStub(context: Context, apk: File): Intent? { val label = context.applicationInfo.nonLocalizedLabel val pkg = context.packageName val session = APKInstall.startSession(context) - session.openStream(context).use { - if (!patch(context, apk, it, pkg, label)) { - return null + return withContext(Dispatchers.IO) { + session.openStream(context).use { + if (!patch(context, apk, it, pkg, label)) { + return@withContext null + } } + session.waitIntent() } - return session.waitIntent() } }