Fix Magisk Manager hiding after using WorkManager

This commit is contained in:
topjohnwu 2018-12-31 14:55:03 +08:00
parent 570ecd9987
commit 039be65a89
3 changed files with 32 additions and 33 deletions

View File

@ -72,8 +72,7 @@ public class DownloadApp {
progress.update(); progress.update();
patched = new File(apk.getParent(), "patched.apk"); patched = new File(apk.getParent(), "patched.apk");
try { try {
JarMap jarMap = new JarMap(apk); JarMap jarMap = PatchAPK.patch(apk.getPath(), app.getPackageName());
PatchAPK.patch(jarMap, app.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;

View File

@ -23,10 +23,11 @@ import java.nio.ByteOrder;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class PatchAPK { public class PatchAPK {
@ -42,7 +43,7 @@ public class PatchAPK {
builder.append(prefix); builder.append(prefix);
length -= prefix.length(); length -= prefix.length();
SecureRandom random = new SecureRandom(); SecureRandom random = new SecureRandom();
char next, prev = '.'; char next, prev = prefix.charAt(prefix.length() - 1);
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
if (prev == '.' || i == length - 1) { if (prev == '.' || i == length - 1) {
next = ALPHA.charAt(random.nextInt(ALPHA.length())); next = ALPHA.charAt(random.nextInt(ALPHA.length()));
@ -55,30 +56,30 @@ public class PatchAPK {
return builder.toString(); return builder.toString();
} }
private static boolean findAndPatch(byte xml[], String a, String b) { private static boolean findAndPatch(byte xml[], String from, String to) {
if (a.length() != b.length()) if (from.length() != to.length())
return false; return false;
char[] from = a.toCharArray(), to = b.toCharArray();
CharBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(); CharBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
int offset = -1; List<Integer> offList = new ArrayList<>();
for (int i = 0; i < buf.length() - from.length; ++i) { for (int i = 0; i < buf.length() - from.length(); ++i) {
boolean match = true; boolean match = true;
for (int j = 0; j < from.length; ++j) { for (int j = 0; j < from.length(); ++j) {
if (buf.get(i + j) != from[j]) { if (buf.get(i + j) != from.charAt(j)) {
match = false; match = false;
break; break;
} }
} }
// Make sure it is null terminated if (match) {
if (match && buf.get(i + from.length) == '\0') { offList.add(i);
offset = i; i += from.length();
break;
} }
} }
if (offset < 0) if (offList.isEmpty())
return false; return false;
buf.position(offset); for (int off : offList) {
buf.position(off);
buf.put(to); buf.put(to);
}
return true; return true;
} }
@ -102,8 +103,8 @@ public class PatchAPK {
String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length()); String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
try { try {
JarMap apk = new JarMap(app.getPackageCodePath()); JarMap apk;
if (!patch(apk, pkg)) if ((apk = patch(app.getPackageCodePath(), pkg)) == null)
return false; return false;
SignAPK.sign(apk, new BufferedOutputStream(new FileOutputStream(repack))); SignAPK.sign(apk, new BufferedOutputStream(new FileOutputStream(repack)));
} catch (Exception e) { } catch (Exception e) {
@ -113,7 +114,7 @@ public class PatchAPK {
// Install the application // Install the application
repack.setReadable(true, false); repack.setReadable(true, false);
if (!ShellUtils.fastCmdResult("pm install " + repack)) if (!ShellUtils.fastCmdResult("pm install " + repack))
return false;; return false;
app.mDB.setStrings(Const.Key.SU_MANAGER, pkg); app.mDB.setStrings(Const.Key.SU_MANAGER, pkg);
Data.exportPrefs(); Data.exportPrefs();
@ -122,23 +123,23 @@ public class PatchAPK {
return true; return true;
} }
public static boolean patch(JarMap apk, String pkg) { public static JarMap patch(String apk, String pkg) {
try { try {
JarEntry je = apk.getJarEntry(Const.ANDROID_MANIFEST); JarMap jar = new JarMap(apk);
byte xml[] = apk.getRawData(je); JarEntry je = jar.getJarEntry(Const.ANDROID_MANIFEST);
byte xml[] = jar.getRawData(je);
if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) || if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) ||
!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 null;
// Write in changes // Write in changes
apk.getOutputStream(je).write(xml); jar.getOutputStream(je).write(xml);
return jar;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return null;
} }
return true;
} }
public static void hideManager() { public static void hideManager() {
@ -146,10 +147,9 @@ public class PatchAPK {
App app = App.self; App app = App.self;
NotificationCompat.Builder progress = NotificationCompat.Builder progress =
Notifications.progress(app.getString(R.string.hide_manager_title)); Notifications.progress(app.getString(R.string.hide_manager_title));
NotificationManagerCompat mgr = NotificationManagerCompat.from(app); Notifications.mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
boolean b = patchAndHide(); boolean b = patchAndHide();
mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID); Notifications.mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID);
if (!b) Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG); if (!b) Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
}); });
} }

View File

@ -18,7 +18,7 @@
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
android:authorities="com.topjohnwu.magisk.provider" android:authorities="${applicationId}.provider"
android:exported="false" android:exported="false"
android:grantUriPermissions="true"> android:grantUriPermissions="true">
<meta-data <meta-data