From a910c8ccd8fa53c7e4b00db47798513362bd7a69 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 16 Oct 2019 05:07:29 -0400 Subject: [PATCH] Support stub APK upgrades --- .../magisk/model/download/DownloadService.kt | 14 ++++++++++++-- .../magisk/model/download/RemoteFileService.kt | 3 ++- .../com/topjohnwu/magisk/utils/DynAPK.java | 18 ++++++++++++++---- .../topjohnwu/magisk/DelegateApplication.java | 4 +++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/DownloadService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/DownloadService.kt index ae39a7fb9..70adc04e3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/DownloadService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/DownloadService.kt @@ -7,6 +7,7 @@ import android.content.Intent import android.os.Build import android.webkit.MimeTypeMap import androidx.core.app.NotificationCompat +import com.topjohnwu.magisk.ProcessPhoenix import com.topjohnwu.magisk.R import com.topjohnwu.magisk.extensions.chooser import com.topjohnwu.magisk.extensions.exists @@ -18,6 +19,8 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.* import com.topjohnwu.magisk.ui.flash.FlashActivity import com.topjohnwu.magisk.utils.APKInstall +import com.topjohnwu.magisk.utils.DynAPK +import com.topjohnwu.magisk.utils.isRunningAsStub import org.koin.core.get import java.io.File import kotlin.random.Random.Default.nextInt @@ -62,8 +65,15 @@ open class DownloadService : RemoteFileService() { ) { remove(id) when (subject.configuration) { - is APK.Upgrade -> APKInstall.install(this, subject.file) - else -> Unit + is APK.Upgrade -> { + if (isRunningAsStub) { + subject.file.renameTo(DynAPK.update(this)) + ProcessPhoenix.triggerRebirth(this) + } else { + APKInstall.install(this, subject.file) + } + } + is APK.Restore -> Unit } } diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt index 1c5fea005..ceb35cc85 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt @@ -12,6 +12,7 @@ import com.topjohnwu.magisk.extensions.writeTo import com.topjohnwu.magisk.model.entity.internal.DownloadSubject import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.* import com.topjohnwu.magisk.utils.ProgressInputStream +import com.topjohnwu.magisk.utils.isRunningAsStub import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.superuser.ShellUtils import io.reactivex.Completable @@ -71,7 +72,7 @@ abstract class RemoteFileService : NotificationService() { else -> Completable.fromAction { stream.writeTo(subject.file) } } }.doOnComplete { - if (subject is Manager) + if (!isRunningAsStub && subject is Manager) handleAPK(subject) } diff --git a/shared/src/main/java/com/topjohnwu/magisk/utils/DynAPK.java b/shared/src/main/java/com/topjohnwu/magisk/utils/DynAPK.java index 4c1d170c5..f75d855a7 100644 --- a/shared/src/main/java/com/topjohnwu/magisk/utils/DynAPK.java +++ b/shared/src/main/java/com/topjohnwu/magisk/utils/DynAPK.java @@ -8,9 +8,19 @@ public class DynAPK { private static File dynDir; - public static File current(Context context) { - if (dynDir == null) - dynDir = new File(context.getFilesDir().getParent(), "dyn"); - return new File(dynDir, "current.apk"); + private static File getDynDir(Context c) { + if (dynDir == null) { + dynDir = new File(c.getFilesDir().getParent(), "dyn"); + dynDir.mkdir(); + } + return dynDir; + } + + public static File current(Context c) { + return new File(getDynDir(c), "current.apk"); + } + + public static File update(Context c) { + return new File(getDynDir(c), "update.apk"); } } diff --git a/stub/src/main/java/com/topjohnwu/magisk/DelegateApplication.java b/stub/src/main/java/com/topjohnwu/magisk/DelegateApplication.java index 83ef6879c..c6a46f00e 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/DelegateApplication.java +++ b/stub/src/main/java/com/topjohnwu/magisk/DelegateApplication.java @@ -37,7 +37,9 @@ public class DelegateApplication extends Application { // If 9.0+, try to dynamically load the APK DelegateComponentFactory factory = (DelegateComponentFactory) this.factory; MANAGER_APK = DynAPK.current(this); - MANAGER_APK.getParentFile().mkdir(); + File update = DynAPK.update(this); + if (update.exists()) + update.renameTo(MANAGER_APK); if (MANAGER_APK.exists()) { ClassLoader cl = new DynamicClassLoader(MANAGER_APK, factory.loader); try {