mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-06 07:20:47 -05:00
Stop using platform provided DownloadManager
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.androidnetworking.AndroidNetworking;
|
||||
import com.androidnetworking.error.ANError;
|
||||
import com.androidnetworking.interfaces.DownloadListener;
|
||||
import com.topjohnwu.magisk.Const;
|
||||
import com.topjohnwu.magisk.Data;
|
||||
import com.topjohnwu.magisk.MagiskManager;
|
||||
import com.topjohnwu.magisk.asyncs.PatchAPK;
|
||||
import com.topjohnwu.magisk.components.NotificationProgress;
|
||||
import com.topjohnwu.superuser.ShellUtils;
|
||||
import com.topjohnwu.utils.JarMap;
|
||||
import com.topjohnwu.utils.SignAPK;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
public class DlInstallManager {
|
||||
|
||||
public static void upgrade(String name) {
|
||||
dlInstall(name, new PatchPackageName());
|
||||
}
|
||||
|
||||
public static void restore() {
|
||||
String name = Utils.fmt("MagiskManager v%s(%d)",
|
||||
Data.remoteManagerVersionString, Data.remoteManagerVersionCode);
|
||||
dlInstall(name, new RestoreManager());
|
||||
}
|
||||
|
||||
public static void dlInstall(String name, ManagerDownloadListener listener) {
|
||||
MagiskManager mm = Data.MM();
|
||||
File apk = new File(mm.getFilesDir(), "manager.apk");
|
||||
NotificationProgress progress = new NotificationProgress(name);
|
||||
listener.setInstances(apk, progress);
|
||||
AndroidNetworking
|
||||
.download(Data.managerLink, apk.getParent(), apk.getName())
|
||||
.setExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
|
||||
.build()
|
||||
.setDownloadProgressListener(progress)
|
||||
.startDownload(listener);
|
||||
}
|
||||
|
||||
public abstract static class ManagerDownloadListener implements DownloadListener {
|
||||
private File apk;
|
||||
private NotificationProgress progress;
|
||||
|
||||
private void setInstances(File apk, NotificationProgress progress) {
|
||||
this.apk = apk;
|
||||
this.progress = progress;
|
||||
}
|
||||
|
||||
public abstract void onDownloadComplete(File apk, NotificationProgress progress);
|
||||
|
||||
@Override
|
||||
public final void onDownloadComplete() {
|
||||
onDownloadComplete(apk, progress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(ANError anError) {}
|
||||
}
|
||||
|
||||
private static class PatchPackageName extends ManagerDownloadListener {
|
||||
|
||||
@Override
|
||||
public void onDownloadComplete(File apk, NotificationProgress progress) {
|
||||
File patched = apk;
|
||||
MagiskManager mm = Data.MM();
|
||||
if (!mm.getPackageName().equals(Const.ORIG_PKG_NAME)) {
|
||||
progress.getBuilder()
|
||||
.setProgress(0, 0, true)
|
||||
.setContentText("Patching APK");
|
||||
progress.update();
|
||||
patched = new File(apk.getParent(), "patched.apk");
|
||||
try {
|
||||
JarMap jarMap = new JarMap(apk);
|
||||
PatchAPK.patchPackageID(jarMap, Const.ORIG_PKG_NAME, mm.getPackageName());
|
||||
SignAPK.sign(jarMap, new BufferedOutputStream(new FileOutputStream(patched)));
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
progress.defaultDone();
|
||||
APKInstall.install(mm, patched);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RestoreManager extends ManagerDownloadListener {
|
||||
|
||||
@Override
|
||||
public void onDownloadComplete(File apk, NotificationProgress progress) {
|
||||
progress.defaultDone();
|
||||
Data.exportPrefs();
|
||||
if (ShellUtils.fastCmdResult("pm install " + apk))
|
||||
RootUtils.rmAndLaunch(Data.MM().getPackageName(), Const.ORIG_PKG_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-14
@@ -1,8 +1,6 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.topjohnwu.magisk.Const;
|
||||
@@ -14,9 +12,10 @@ import com.topjohnwu.magisk.receivers.ManagerUpdate;
|
||||
import com.topjohnwu.magisk.receivers.RebootReceiver;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
import androidx.core.app.TaskStackBuilder;
|
||||
|
||||
public class NotificationMgr {
|
||||
public class Notifications {
|
||||
|
||||
public static void magiskUpdate() {
|
||||
MagiskManager mm = Data.MM();
|
||||
@@ -37,19 +36,18 @@ public class NotificationMgr {
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
|
||||
NotificationManagerCompat mgr = NotificationManagerCompat.from(mm);
|
||||
mgr.notify(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
|
||||
}
|
||||
|
||||
public static void managerUpdate() {
|
||||
MagiskManager mm = Data.MM();
|
||||
String filename = Utils.fmt("MagiskManager-v%s(%d).apk",
|
||||
String name = Utils.fmt("MagiskManager v%s(%d)",
|
||||
Data.remoteManagerVersionString, Data.remoteManagerVersionCode);
|
||||
|
||||
Intent intent = new Intent(mm, Data.classMap.get(ManagerUpdate.class));
|
||||
intent.putExtra(Const.Key.INTENT_SET_LINK, Data.managerLink);
|
||||
intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename);
|
||||
intent.putExtra(Const.Key.INTENT_SET_NAME, name);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
|
||||
Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
@@ -61,9 +59,8 @@ public class NotificationMgr {
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent);
|
||||
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(Const.ID.APK_UPDATE_NOTIFICATION_ID, builder.build());
|
||||
NotificationManagerCompat mgr = NotificationManagerCompat.from(mm);
|
||||
mgr.notify(Const.ID.APK_UPDATE_NOTIFICATION_ID, builder.build());
|
||||
}
|
||||
|
||||
public static void dtboPatched() {
|
||||
@@ -80,8 +77,16 @@ public class NotificationMgr {
|
||||
.setVibrate(new long[]{0, 100, 100, 100})
|
||||
.addAction(R.drawable.ic_refresh, mm.getString(R.string.reboot), pendingIntent);
|
||||
|
||||
NotificationManager notificationManager =
|
||||
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build());
|
||||
NotificationManagerCompat mgr = NotificationManagerCompat.from(mm);
|
||||
mgr.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build());
|
||||
}
|
||||
|
||||
public static NotificationCompat.Builder progress(String title) {
|
||||
MagiskManager mm = Data.MM();
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
|
||||
builder.setSmallIcon(R.drawable.ic_magisk_outline)
|
||||
.setContentTitle(title)
|
||||
.setProgress(0, 0, true);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import android.os.AsyncTask;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.androidnetworking.AndroidNetworking;
|
||||
import com.topjohnwu.magisk.Const;
|
||||
import com.topjohnwu.magisk.Data;
|
||||
import com.topjohnwu.magisk.MagiskManager;
|
||||
@@ -150,4 +151,9 @@ public class Utils {
|
||||
return Shell.rootAccess() && (Const.USER_ID == 0 ||
|
||||
Data.multiuserState != Const.Value.MULTIUSER_MODE_OWNER_MANAGED);
|
||||
}
|
||||
|
||||
public static String dlString(String url) {
|
||||
String s = (String) AndroidNetworking.get(url).build().executeForString().getResult();
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user