96 lines
3.0 KiB
Java
Raw Normal View History

2017-08-22 03:01:54 +08:00
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.app.ProgressDialog;
2017-08-22 03:01:54 +08:00
import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-11-06 04:41:23 +08:00
import com.topjohnwu.magisk.utils.Const;
2018-06-12 05:32:35 +08:00
import com.topjohnwu.magisk.utils.PatchAPK;
2018-06-02 22:00:52 +08:00
import com.topjohnwu.magisk.utils.RootUtils;
2018-01-21 06:07:24 +08:00
import com.topjohnwu.superuser.Shell;
2018-02-12 23:07:35 +08:00
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
2017-08-22 03:01:54 +08:00
2018-06-12 05:32:35 +08:00
import java.io.FileNotFoundException;
2017-10-15 23:54:34 +08:00
import java.security.SecureRandom;
2017-08-22 03:01:54 +08:00
public class HideManager extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public HideManager(Activity activity) {
super(activity);
}
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-08-22 03:01:54 +08:00
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(),
getActivity().getString(R.string.hide_manager_toast),
getActivity().getString(R.string.hide_manager_toast2));
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
2018-06-12 05:32:35 +08:00
// Generate a new app with random package name
SuFile repack = new SuFile("/data/local/tmp/repack.apk", true);
2017-11-06 04:41:23 +08:00
String pkg = genPackageName("com.", Const.ORIG_PKG_NAME.length());
2017-10-04 22:27:14 +08:00
try {
2018-06-12 05:32:35 +08:00
if (!PatchAPK.patchPackageID(
mm.getPackageCodePath(),
new SuFileOutputStream(repack),
Const.ORIG_PKG_NAME, pkg))
2017-10-04 22:27:14 +08:00
return false;
2018-06-12 05:32:35 +08:00
} catch (FileNotFoundException e) {
2017-10-04 22:27:14 +08:00
return false;
}
2017-08-22 03:01:54 +08:00
// Install the application
2018-02-13 03:27:27 +08:00
if (!ShellUtils.fastCmdResult(Shell.getShell(), "pm install " + repack))
2018-02-12 23:07:35 +08:00
return false;
2017-10-21 22:54:47 +08:00
repack.delete();
2017-08-22 03:01:54 +08:00
2018-04-06 01:54:09 +08:00
mm.mDB.setStrings(Const.Key.SU_REQUESTER, pkg);
2018-06-02 22:00:52 +08:00
mm.dumpPrefs();
RootUtils.uninstallPkg(Const.ORIG_PKG_NAME);
2017-08-22 03:01:54 +08:00
return true;
}
@Override
protected void onPostExecute(Boolean b) {
dialog.dismiss();
2017-08-22 03:01:54 +08:00
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);
}
}