2020-12-29 01:44:02 -08:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
|
|
|
import android.app.AppComponentFactory;
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.content.ContextWrapper;
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.net.Uri;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.os.Build;
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2021-01-26 03:40:25 -08:00
|
|
|
import java.lang.reflect.Field;
|
2020-12-29 01:44:02 -08:00
|
|
|
|
|
|
|
public class InjectAPK {
|
|
|
|
|
2021-01-26 03:40:25 -08:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2020-12-29 01:44:02 -08:00
|
|
|
static Application setup(Context context) {
|
2021-01-26 03:40:25 -08:00
|
|
|
// Get ContextImpl
|
|
|
|
while (context instanceof ContextWrapper) {
|
|
|
|
context = ((ContextWrapper) context).getBaseContext();
|
|
|
|
}
|
|
|
|
|
2020-12-29 01:44:02 -08:00
|
|
|
File apk = DynAPK.current(context);
|
|
|
|
File update = DynAPK.update(context);
|
|
|
|
if (update.exists())
|
|
|
|
update.renameTo(apk);
|
2021-01-26 03:40:25 -08:00
|
|
|
Application result = null;
|
2020-12-29 01:44:02 -08:00
|
|
|
if (!apk.exists()) {
|
|
|
|
// Try copying APK
|
|
|
|
Uri uri = new Uri.Builder().scheme("content")
|
|
|
|
.authority("com.topjohnwu.magisk.provider")
|
|
|
|
.encodedPath("apk_file").build();
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2021-01-26 03:40:25 -08:00
|
|
|
try (InputStream src = resolver.openInputStream(uri)) {
|
|
|
|
if (src != null) {
|
2020-12-29 01:44:02 -08:00
|
|
|
try (OutputStream out = new FileOutputStream(apk)) {
|
|
|
|
byte[] buf = new byte[4096];
|
2021-01-26 03:40:25 -08:00
|
|
|
for (int read; (read = src.read(buf)) >= 0;) {
|
2020-12-29 01:44:02 -08:00
|
|
|
out.write(buf, 0, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 03:40:25 -08:00
|
|
|
} catch (Exception ignored) {}
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
|
|
|
if (apk.exists()) {
|
2021-01-26 03:40:25 -08:00
|
|
|
ClassLoader cl = new InjectedClassLoader(apk);
|
2020-12-29 01:44:02 -08:00
|
|
|
try {
|
2021-01-26 03:40:25 -08:00
|
|
|
// Create the receiver Application
|
|
|
|
Object app = cl.loadClass(Mapping.get("APP")).getConstructor(Object.class)
|
2020-12-29 01:44:02 -08:00
|
|
|
.newInstance(DynAPK.pack(dynData()));
|
|
|
|
|
2021-01-26 03:40:25 -08:00
|
|
|
// Create the receiver component factory
|
|
|
|
Object factory = null;
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
factory = cl.loadClass(Mapping.get("ACF")).newInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
setClassLoader(context, cl);
|
|
|
|
|
|
|
|
// Finally, set variables
|
|
|
|
result = (Application) app;
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
DelegateComponentFactory.INSTANCE.loader = cl;
|
|
|
|
DelegateComponentFactory.INSTANCE.receiver = (AppComponentFactory) factory;
|
|
|
|
}
|
2020-12-29 01:44:02 -08:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(InjectAPK.class.getSimpleName(), "", e);
|
|
|
|
apk.delete();
|
|
|
|
}
|
2021-01-26 03:40:25 -08:00
|
|
|
} else {
|
|
|
|
ClassLoader cl = new RedirectClassLoader();
|
|
|
|
try {
|
|
|
|
setClassLoader(context, cl);
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
DelegateComponentFactory.INSTANCE.loader = cl;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Should never happen
|
|
|
|
Log.e(InjectAPK.class.getSimpleName(), "", e);
|
|
|
|
}
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
2021-01-26 03:40:25 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace LoadedApk mClassLoader
|
|
|
|
private static void setClassLoader(Context impl, ClassLoader cl)
|
|
|
|
throws NoSuchFieldException, IllegalAccessException {
|
|
|
|
Field mInfo = impl.getClass().getDeclaredField("mPackageInfo");
|
|
|
|
mInfo.setAccessible(true);
|
|
|
|
Object loadedApk = mInfo.get(impl);
|
|
|
|
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
|
|
|
|
mcl.setAccessible(true);
|
|
|
|
mcl.set(loadedApk, cl);
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static DynAPK.Data dynData() {
|
|
|
|
DynAPK.Data data = new DynAPK.Data();
|
|
|
|
data.version = BuildConfig.STUB_VERSION;
|
2021-01-22 20:45:37 -08:00
|
|
|
data.classToComponent = Mapping.inverseMap;
|
2020-12-29 01:44:02 -08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|