Preserve hidden when upgrade

This commit is contained in:
topjohnwu
2018-06-12 05:32:35 +08:00
parent c231e88a5d
commit a949641342
11 changed files with 173 additions and 130 deletions

View File

@@ -23,8 +23,8 @@ import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.components.ExpandableView;
import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.utils.ISafetyNetHelper;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.ISafetyNetHelper;
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.ShowUI;
import com.topjohnwu.magisk.utils.Topic;

View File

@@ -3,8 +3,8 @@ package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.ISafetyNetHelper;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.ISafetyNetHelper;
import com.topjohnwu.magisk.utils.WebService;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
@@ -13,7 +13,6 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

View File

@@ -7,17 +7,15 @@ import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.PatchAPK;
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.ZipUtils;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
import com.topjohnwu.utils.JarMap;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.SecureRandom;
import java.util.jar.JarEntry;
public class HideManager extends ParallelTask<Void, Void, Boolean> {
@@ -48,51 +46,6 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
return builder.toString();
}
private int findOffset(byte buf[], byte pattern[]) {
int offset = -1;
for (int i = 0; i < buf.length - pattern.length; ++i) {
boolean match = true;
for (int j = 0; j < pattern.length; ++j) {
if (buf[i + j] != pattern[j]) {
match = false;
break;
}
}
if (match) {
offset = i;
break;
}
}
return offset;
}
/* It seems that AAPT sometimes generate another type of string format */
private boolean fallbackPatch(byte xml[], String from, String to) {
byte[] target = new byte[from.length() * 2 + 2];
for (int i = 0; i < from.length(); ++i) {
target[i * 2] = (byte) from.charAt(i);
}
int offset = findOffset(xml, target);
if (offset < 0)
return false;
byte[] dest = new byte[target.length - 2];
for (int i = 0; i < to.length(); ++i) {
dest[i * 2] = (byte) to.charAt(i);
}
System.arraycopy(dest, 0, xml, offset, dest.length);
return true;
}
private boolean findAndPatch(byte xml[], String from, String to) {
byte target[] = (from + '\0').getBytes();
int offset = findOffset(xml, target);
if (offset < 0)
return fallbackPatch(xml, from, to);
System.arraycopy(to.getBytes(), 0, xml, offset, to.length());
return true;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(),
@@ -104,28 +57,17 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... voids) {
MagiskManager mm = MagiskManager.get();
// Generate a new unhide app with random package name
// Generate a new app with random package name
SuFile repack = new SuFile("/data/local/tmp/repack.apk", true);
String pkg = genPackageName("com.", Const.ORIG_PKG_NAME.length());
try {
// Read whole APK into memory
JarMap apk = new JarMap(new FileInputStream(mm.getPackageCodePath()));
JarEntry je = new JarEntry(Const.ANDROID_MANIFEST);
byte xml[] = apk.getRawData(je);
if (!findAndPatch(xml, Const.ORIG_PKG_NAME, pkg))
if (!PatchAPK.patchPackageID(
mm.getPackageCodePath(),
new SuFileOutputStream(repack),
Const.ORIG_PKG_NAME, pkg))
return false;
if (!findAndPatch(xml, Const.ORIG_PKG_NAME + ".provider", pkg + ".provider"))
return false;
// Write in changes
apk.getOutputStream(je).write(xml);
// Sign the APK
ZipUtils.signZip(apk, new SuFileOutputStream(repack));
} catch (Exception e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
return false;
}

View File

@@ -0,0 +1,48 @@
package com.topjohnwu.magisk.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.PatchAPK;
import com.topjohnwu.magisk.utils.Utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ManagerUpdate extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Utils.dlAndReceive(
context, new PatchedInstall(),
intent.getStringExtra(Const.Key.INTENT_SET_LINK),
intent.getStringExtra(Const.Key.INTENT_SET_FILENAME)
);
}
private static class PatchedInstall extends ManagerInstall {
@Override
public void onDownloadDone(Context context, Uri uri) {
if (!context.getPackageName().equals(Const.ORIG_PKG_NAME)) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
String o = uri.getPath();
String p = o.substring(0, o.lastIndexOf('.')) + "-patched.apk";
try {
PatchAPK.patchPackageID(o, new BufferedOutputStream(new FileOutputStream(p)),
Const.ORIG_PKG_NAME, context.getPackageName());
} catch (FileNotFoundException ignored) { }
super.onDownloadDone(context, Uri.fromFile(new File(p)));
});
} else {
super.onDownloadDone(context, uri);
}
}
}
}

View File

@@ -0,0 +1,77 @@
package com.topjohnwu.magisk.utils;
import com.topjohnwu.utils.JarMap;
import java.io.OutputStream;
import java.util.jar.JarEntry;
public class PatchAPK {
private static int findOffset(byte buf[], byte pattern[]) {
int offset = -1;
for (int i = 0; i < buf.length - pattern.length; ++i) {
boolean match = true;
for (int j = 0; j < pattern.length; ++j) {
if (buf[i + j] != pattern[j]) {
match = false;
break;
}
}
if (match) {
offset = i;
break;
}
}
return offset;
}
/* It seems that AAPT sometimes generate another type of string format */
private static boolean fallbackPatch(byte xml[], String from, String to) {
byte[] target = new byte[from.length() * 2 + 2];
for (int i = 0; i < from.length(); ++i) {
target[i * 2] = (byte) from.charAt(i);
}
int offset = findOffset(xml, target);
if (offset < 0)
return false;
byte[] dest = new byte[target.length - 2];
for (int i = 0; i < to.length(); ++i) {
dest[i * 2] = (byte) to.charAt(i);
}
System.arraycopy(dest, 0, xml, offset, dest.length);
return true;
}
private static boolean findAndPatch(byte xml[], String from, String to) {
byte target[] = (from + '\0').getBytes();
int offset = findOffset(xml, target);
if (offset < 0)
return fallbackPatch(xml, from, to);
System.arraycopy(to.getBytes(), 0, xml, offset, to.length());
return true;
}
public static boolean patchPackageID(String fileName, OutputStream out, String from, String to) {
try {
JarMap apk = new JarMap(fileName);
JarEntry je = apk.getJarEntry(Const.ANDROID_MANIFEST);
byte xml[] = apk.getRawData(je);
if (!findAndPatch(xml, from, to))
return false;
if (!findAndPatch(xml, from + ".provider", to + ".provider"))
return false;
// Write in changes
apk.getOutputStream(je).write(xml);
// Sign the APK
ZipUtils.signZip(apk, out);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}

View File

@@ -113,7 +113,7 @@ public class ShowUI {
.setPositiveButton(R.string.yes, (d, i) -> {
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
public void onDownloadDone(Context context, Uri uri) {
new InstallMagisk(activity, uri).exec();
}
}, mm.magiskLink, filename);
@@ -167,7 +167,7 @@ public class ShowUI {
activity,
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@@ -185,7 +185,7 @@ public class ShowUI {
case 0:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
public void onDownloadDone(Context context, Uri uri) {
SnackbarMaker.showUri(activity, uri);
}
};
@@ -196,7 +196,7 @@ public class ShowUI {
return;
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri)
.putExtra(Const.Key.FLASH_SET_BOOT, boot)
@@ -221,7 +221,7 @@ public class ShowUI {
return;
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri)
.putExtra(Const.Key.FLASH_SET_BOOT, boot)