From 8f0ea5925af0f5be6925fbc0c2442a183a24c344 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 13 Feb 2022 02:58:55 -0800 Subject: [PATCH] Relaunch process without second process --- .../com/topjohnwu/magisk/PhoenixActivity.java | 37 ------------------- .../java/com/topjohnwu/magisk/StubApk.java | 10 +++++ .../magisk/core/download/DownloadService.kt | 10 +++-- buildSrc/src/main/java/Codegen.kt | 13 +------ .../com/topjohnwu/magisk/ClassLoaders.java | 8 +--- .../topjohnwu/magisk/DownloadActivity.java | 2 +- 6 files changed, 20 insertions(+), 60 deletions(-) delete mode 100644 app/shared/src/main/java/com/topjohnwu/magisk/PhoenixActivity.java diff --git a/app/shared/src/main/java/com/topjohnwu/magisk/PhoenixActivity.java b/app/shared/src/main/java/com/topjohnwu/magisk/PhoenixActivity.java deleted file mode 100644 index 68c6f8ff6..000000000 --- a/app/shared/src/main/java/com/topjohnwu/magisk/PhoenixActivity.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.topjohnwu.magisk; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.os.Process; - -import io.michaelrocks.paranoid.Obfuscate; - -// Inspired by https://github.com/JakeWharton/ProcessPhoenix - -@Obfuscate -public class PhoenixActivity extends Activity { - - private static final String PID_KEY = "pid"; - - public static void rebirth(Context context, String clzName) { - var intent = new Intent(); - intent.setComponent(new ComponentName(context, clzName)); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(PID_KEY, Process.myPid()); - context.startActivity(intent); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Process.killProcess(getIntent().getIntExtra(PID_KEY, -1)); - var intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - startActivity(intent); - finish(); - Runtime.getRuntime().exit(0); - } -} diff --git a/app/shared/src/main/java/com/topjohnwu/magisk/StubApk.java b/app/shared/src/main/java/com/topjohnwu/magisk/StubApk.java index d442c3889..70543637b 100644 --- a/app/shared/src/main/java/com/topjohnwu/magisk/StubApk.java +++ b/app/shared/src/main/java/com/topjohnwu/magisk/StubApk.java @@ -2,7 +2,9 @@ package com.topjohnwu.magisk; import static android.os.Build.VERSION.SDK_INT; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; @@ -56,6 +58,14 @@ public class StubApk { } catch (Exception ignored) {} } + public static void restartProcess(Activity activity) { + Intent intent = activity.getPackageManager() + .getLaunchIntentForPackage(activity.getPackageName()); + activity.finishAffinity(); + activity.startActivity(intent); + Runtime.getRuntime().exit(0); + } + public static class Data { // Indices of the object array private static final int STUB_VERSION = 0; diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt index 441018af4..f57778234 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt @@ -9,7 +9,6 @@ import android.net.Uri import android.os.Build import androidx.core.net.toFile import androidx.lifecycle.LifecycleOwner -import com.topjohnwu.magisk.PhoenixActivity import com.topjohnwu.magisk.R import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.ActivityTracker @@ -103,8 +102,13 @@ class DownloadService : NotificationService() { apk.delete() patched.renameTo(apk) } else { - val clz = Info.stub!!.classToComponent["PHOENIX"]!! - PhoenixActivity.rebirth(this, clz) + ActivityTracker.foreground?.let { + // Relaunch the process if we are foreground + StubApk.restartProcess(it) + } ?: run { + // Or else simply kill the current process + Runtime.getRuntime().exit(0) + } return } } catch (e: Exception) { diff --git a/buildSrc/src/main/java/Codegen.kt b/buildSrc/src/main/java/Codegen.kt index eba892731..df32b2ff6 100644 --- a/buildSrc/src/main/java/Codegen.kt +++ b/buildSrc/src/main/java/Codegen.kt @@ -104,17 +104,6 @@ fun genStubManifest(srcDir: File, outDir: File): String { true )) - cmpList.add(Component( - "PHOENIX", - "PhoenixActivity", - """ - |""".ind(2), - true - )) - cmpList.add(Component( "com.topjohnwu.magisk.core.Provider", "dummy.DummyProvider", @@ -260,7 +249,7 @@ fun genStubManifest(srcDir: File, outDir: File): String { } } if (gen.xml.isNotEmpty()) { - cmps.add(gen.xml.format(name, names.random(kRANDOM))) + cmps.add(gen.xml.format(name)) } } diff --git a/stub/src/main/java/com/topjohnwu/magisk/ClassLoaders.java b/stub/src/main/java/com/topjohnwu/magisk/ClassLoaders.java index ab7f7c824..e92e3680b 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/ClassLoaders.java +++ b/stub/src/main/java/com/topjohnwu/magisk/ClassLoaders.java @@ -8,19 +8,13 @@ import java.io.File; // mapping when loading from platform (via LoadedApk.mClassLoader) class InjectedClassLoader extends ClassLoader { - private static final String PKEY = "PHOENIX"; - static final String PHOENIX = Mapping.inverseMap.get(PKEY); - InjectedClassLoader(File apk) { super(new DynamicClassLoader(apk)); } @Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - String n = Mapping.get(name); - if (n.equals(PKEY)) - return PhoenixActivity.class; - return super.loadClass(n, resolve); + return super.loadClass(Mapping.get(name), resolve); } } diff --git a/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java b/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java index d4400c99a..b305536f1 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java +++ b/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java @@ -109,7 +109,7 @@ public class DownloadActivity extends Activity { dialog = ProgressDialog.show(themed, getString(dling), getString(dling) + " " + APP_NAME, true); Runnable onSuccess = () -> { dialog.dismiss(); - PhoenixActivity.rebirth(this, InjectedClassLoader.PHOENIX); + StubApk.restartProcess(this); finish(); }; // Download and upgrade the app