Fix and optimize APK installations

This commit is contained in:
topjohnwu 2018-12-07 21:42:53 -05:00
parent 1fae89cbb6
commit 14ac37e8a5
2 changed files with 23 additions and 15 deletions

View File

@ -12,11 +12,12 @@ import com.topjohnwu.magisk.components.Notifications;
import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
import com.topjohnwu.utils.JarMap; import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK; import com.topjohnwu.utils.SignAPK;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer; import java.nio.CharBuffer;
@ -97,23 +98,22 @@ public class PatchAPK {
MagiskManager mm = Data.MM(); MagiskManager mm = Data.MM();
// Generate a new app with random package name // Generate a new app with random package name
SuFile repack = new SuFile("/data/local/tmp/repack.apk"); File repack = new File(mm.getFilesDir(), "patched.apk");
String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length()); String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
try { try {
JarMap apk = new JarMap(mm.getPackageCodePath()); JarMap apk = new JarMap(mm.getPackageCodePath());
if (!patchPackageID(apk, BuildConfig.APPLICATION_ID, pkg)) if (!patch(apk, pkg))
return false; return false;
SignAPK.sign(apk, new SuFileOutputStream(repack)); SignAPK.sign(apk, new BufferedOutputStream(new FileOutputStream(repack)));
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
// Install the application // Install the application
repack.setReadable(true, false);
if (!ShellUtils.fastCmdResult("pm install " + repack)) if (!ShellUtils.fastCmdResult("pm install " + repack))
return false; return false;;
repack.delete();
mm.mDB.setStrings(Const.Key.SU_MANAGER, pkg); mm.mDB.setStrings(Const.Key.SU_MANAGER, pkg);
Data.exportPrefs(); Data.exportPrefs();
@ -122,13 +122,13 @@ public class PatchAPK {
return true; return true;
} }
public static boolean patchPackageID(JarMap apk, String from, String to) { public static boolean patch(JarMap apk, String pkg) {
try { try {
JarEntry je = apk.getJarEntry(Const.ANDROID_MANIFEST); JarEntry je = apk.getJarEntry(Const.ANDROID_MANIFEST);
byte xml[] = apk.getRawData(je); byte xml[] = apk.getRawData(je);
if (!findAndPatch(xml, from, to) || if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) ||
!findAndPatch(xml, from + ".provider", to + ".provider") || !findAndPatch(xml, BuildConfig.APPLICATION_ID + ".provider", pkg + ".provider") ||
!findAndPatch(xml, R.string.app_name, R.string.re_app_name)) !findAndPatch(xml, R.string.app_name, R.string.re_app_name))
return false; return false;

View File

@ -81,14 +81,14 @@ public class DlInstallManager {
patched = new File(apk.getParent(), "patched.apk"); patched = new File(apk.getParent(), "patched.apk");
try { try {
JarMap jarMap = new JarMap(apk); JarMap jarMap = new JarMap(apk);
PatchAPK.patchPackageID(jarMap, BuildConfig.APPLICATION_ID, mm.getPackageName()); PatchAPK.patch(jarMap, mm.getPackageName());
SignAPK.sign(jarMap, new BufferedOutputStream(new FileOutputStream(patched))); SignAPK.sign(jarMap, new BufferedOutputStream(new FileOutputStream(patched)));
} catch (Exception e) { } catch (Exception e) {
return; return;
} }
} }
progress.dismiss();
APKInstall.install(mm, patched); APKInstall.install(mm, patched);
progress.dismiss();
} }
} }
@ -96,10 +96,18 @@ public class DlInstallManager {
@Override @Override
public void onDownloadComplete(File apk, ProgressNotification progress) { public void onDownloadComplete(File apk, ProgressNotification progress) {
progress.dismiss(); MagiskManager mm = Data.MM();
progress.getNotification()
.setProgress(0, 0, true)
.setContentTitle(mm.getString(R.string.restore_img_msg))
.setContentText("");
progress.update();
Data.exportPrefs(); Data.exportPrefs();
// Make it world readable
apk.setReadable(true, false);
if (ShellUtils.fastCmdResult("pm install " + apk)) if (ShellUtils.fastCmdResult("pm install " + apk))
RootUtils.rmAndLaunch(Data.MM().getPackageName(), BuildConfig.APPLICATION_ID); RootUtils.rmAndLaunch(mm.getPackageName(), BuildConfig.APPLICATION_ID);
progress.dismiss();
} }
} }
} }