2017-08-22 03:01:54 +08:00
|
|
|
package com.topjohnwu.magisk.asyncs;
|
|
|
|
|
2017-08-27 01:04:55 +08:00
|
|
|
import android.os.Environment;
|
2017-08-22 03:01:54 +08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-10-07 20:31:49 +08:00
|
|
|
import com.topjohnwu.jarsigner.JarMap;
|
2017-08-22 03:01:54 +08:00
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
|
|
|
import com.topjohnwu.magisk.R;
|
2017-10-21 22:54:47 +08:00
|
|
|
import com.topjohnwu.magisk.database.SuDatabaseHelper;
|
2017-10-15 23:02:44 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2017-08-22 03:01:54 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
import com.topjohnwu.magisk.utils.ZipUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
2017-10-21 22:54:47 +08:00
|
|
|
import java.io.FileInputStream;
|
2017-10-15 23:54:34 +08:00
|
|
|
import java.security.SecureRandom;
|
2017-08-22 03:01:54 +08:00
|
|
|
import java.util.List;
|
2017-10-21 22:54:47 +08:00
|
|
|
import java.util.Locale;
|
2017-10-04 22:27:14 +08:00
|
|
|
import java.util.jar.JarEntry;
|
2017-08-22 03:01:54 +08:00
|
|
|
|
|
|
|
public class HideManager extends ParallelTask<Void, Void, Boolean> {
|
|
|
|
|
2017-10-04 22:27:14 +08:00
|
|
|
private static final String ANDROID_MANIFEST = "AndroidManifest.xml";
|
|
|
|
|
2017-10-15 23:54:34 +08:00
|
|
|
private String genPackageName(String prefix, int length) {
|
|
|
|
StringBuilder builder = new StringBuilder(length);
|
|
|
|
builder.append(prefix);
|
|
|
|
length -= prefix.length();
|
|
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
String base = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
String alpha = base + base.toUpperCase();
|
|
|
|
String full = alpha + "0123456789..........";
|
|
|
|
char next, prev = '\0';
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if (prev == '.' || i == length - 1 || i == 0) {
|
|
|
|
next = alpha.charAt(random.nextInt(alpha.length()));
|
|
|
|
} else {
|
|
|
|
next = full.charAt(random.nextInt(full.length()));
|
|
|
|
}
|
|
|
|
builder.append(next);
|
|
|
|
prev = next;
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2017-10-21 22:54:47 +08:00
|
|
|
private boolean findAndPatch(byte xml[], String from, String to) {
|
|
|
|
int offset = -1;
|
|
|
|
byte target[] = (from + '\0').getBytes();
|
|
|
|
for (int i = 0; i < xml.length - target.length; ++i) {
|
|
|
|
boolean match = true;
|
|
|
|
for (int j = 0; j < target.length; ++j) {
|
|
|
|
if (xml[i + j] != target[j]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
offset = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
System.arraycopy(to.getBytes(), 0, xml, offset, to.length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:01:54 +08:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2017-10-16 00:54:48 +08:00
|
|
|
MagiskManager.toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT);
|
2017-10-21 22:54:47 +08:00
|
|
|
MagiskManager.toast(R.string.hide_manager_toast2, Toast.LENGTH_LONG);
|
2017-08-22 03:01:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... voids) {
|
2017-10-16 00:54:48 +08:00
|
|
|
MagiskManager mm = MagiskManager.get();
|
2017-08-22 03:01:54 +08:00
|
|
|
|
|
|
|
// Generate a new unhide app with random package name
|
2017-10-21 22:54:47 +08:00
|
|
|
File repack = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", "repack.apk");
|
|
|
|
repack.getParentFile().mkdirs();
|
|
|
|
String pkg = genPackageName("com.", MagiskManager.ORIG_PKG_NAME.length());
|
2017-10-04 22:27:14 +08:00
|
|
|
|
|
|
|
try {
|
2017-10-21 22:54:47 +08:00
|
|
|
// Read whole APK into memory
|
|
|
|
JarMap apk = new JarMap(new FileInputStream(mm.getPackageCodePath()));
|
2017-10-04 22:27:14 +08:00
|
|
|
JarEntry je = new JarEntry(ANDROID_MANIFEST);
|
2017-10-21 22:54:47 +08:00
|
|
|
byte xml[] = apk.getRawData(je);
|
|
|
|
|
|
|
|
if (!findAndPatch(xml, MagiskManager.ORIG_PKG_NAME, pkg))
|
|
|
|
return false;
|
|
|
|
if (!findAndPatch(xml, MagiskManager.ORIG_PKG_NAME + ".provider", pkg + ".provider"))
|
2017-10-04 22:27:14 +08:00
|
|
|
return false;
|
|
|
|
|
2017-10-21 22:54:47 +08:00
|
|
|
// Write in changes
|
|
|
|
apk.getOutputStream(je).write(xml);
|
2017-10-04 22:27:14 +08:00
|
|
|
|
|
|
|
// Sign the APK
|
2017-10-21 22:54:47 +08:00
|
|
|
ZipUtils.signZip(apk, repack, false);
|
2017-10-04 22:27:14 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-22 03:01:54 +08:00
|
|
|
|
|
|
|
// Install the application
|
2017-10-21 22:54:47 +08:00
|
|
|
|
|
|
|
List<String> ret = Shell.su(String.format(Locale.US,
|
|
|
|
"pm install --user %d %s >/dev/null && echo true || echo false",
|
|
|
|
mm.userId, repack));
|
|
|
|
repack.delete();
|
2017-08-22 03:01:54 +08:00
|
|
|
if (!Utils.isValidShellResponse(ret) || !Boolean.parseBoolean(ret.get(0)))
|
|
|
|
return false;
|
|
|
|
|
2017-10-21 22:54:47 +08:00
|
|
|
mm.suDB.setStrings(SuDatabaseHelper.REQUESTER, pkg);
|
|
|
|
Shell.su_raw(String.format(Locale.US, "pm uninstall --user %d %s", mm.userId, mm.getPackageName()));
|
2017-08-22 03:01:54 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean b) {
|
|
|
|
if (!b) {
|
2017-10-16 00:54:48 +08:00
|
|
|
MagiskManager.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
|
2017-08-22 03:01:54 +08:00
|
|
|
}
|
|
|
|
super.onPostExecute(b);
|
|
|
|
}
|
|
|
|
}
|