116 lines
3.8 KiB
Java
Raw Normal View History

2017-08-22 03:01:54 +08:00
package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import android.content.pm.PackageManager;
2017-08-27 01:04:55 +08:00
import android.os.Environment;
2017-08-22 03:01:54 +08:00
import android.widget.Toast;
import com.topjohnwu.jarsigner.JarMap;
2017-08-22 03:01:54 +08:00
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.Policy;
2017-08-22 03:01:54 +08:00
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File;
import java.util.List;
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 UNHIDE_APK = "unhide.apk";
private static final String ANDROID_MANIFEST = "AndroidManifest.xml";
private static final byte[] UNHIDE_PKG_NAME = "com.topjohnwu.unhide\0".getBytes();
2017-08-22 03:01:54 +08:00
public HideManager(Context context) {
super(context);
}
@Override
protected void onPreExecute() {
getMagiskManager().toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT);
}
@Override
protected Boolean doInBackground(Void... voids) {
2017-09-03 15:35:14 +08:00
MagiskManager mm = getMagiskManager();
if (mm == null)
2017-08-22 03:01:54 +08:00
return false;
// Generate a new unhide app with random package name
2017-08-27 01:04:55 +08:00
File unhideAPK = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", "unhide.apk");
unhideAPK.getParentFile().mkdirs();
2017-10-04 22:27:14 +08:00
String pkg;
try {
JarMap asset = new JarMap(mm.getAssets().open(UNHIDE_APK));
JarEntry je = new JarEntry(ANDROID_MANIFEST);
byte xml[] = asset.getRawData(je);
int offset = -1;
// Linear search pattern offset
for (int i = 0; i < xml.length - UNHIDE_PKG_NAME.length; ++i) {
boolean match = true;
for (int j = 0; j < UNHIDE_PKG_NAME.length; ++j) {
if (xml[i + j] != UNHIDE_PKG_NAME[j]) {
match = false;
break;
}
}
if (match) {
offset = i;
break;
}
}
if (offset < 0)
return false;
// Patch binary XML with new package name
pkg = Utils.genPackageName("com.", UNHIDE_PKG_NAME.length - 1);
System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length());
asset.getOutputStream(je).write(xml);
// Sign the APK
ZipUtils.signZip(mm, asset, unhideAPK, false);
} catch (Exception e) {
e.printStackTrace();
return false;
}
2017-08-22 03:01:54 +08:00
// Install the application
List<String> ret = getShell().su("pm install " + unhideAPK + ">/dev/null && echo true || echo false");
unhideAPK.delete();
if (!Utils.isValidShellResponse(ret) || !Boolean.parseBoolean(ret.get(0)))
return false;
try {
// Allow the application to gain root by default
2017-09-03 15:35:14 +08:00
PackageManager pm = mm.getPackageManager();
2017-08-22 03:01:54 +08:00
int uid = pm.getApplicationInfo(pkg, 0).uid;
Policy policy = new Policy(uid, pm);
policy.policy = Policy.ALLOW;
policy.notification = false;
policy.logging = false;
2017-09-03 15:35:14 +08:00
mm.suDB.addPolicy(policy);
2017-08-22 03:01:54 +08:00
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
// Hide myself!
2017-09-03 15:35:14 +08:00
getShell().su_raw("pm hide " + mm.getPackageName());
2017-08-22 03:01:54 +08:00
return true;
}
@Override
protected void onPostExecute(Boolean b) {
2017-09-03 15:35:14 +08:00
MagiskManager mm = getMagiskManager();
if (mm == null)
2017-08-22 03:01:54 +08:00
return;
if (!b) {
2017-09-03 15:35:14 +08:00
mm.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
2017-08-22 03:01:54 +08:00
}
super.onPostExecute(b);
}
}