From 936ad1aa209f52c16fb7788dc39fd59034934427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=AE=AB=E9=9B=AA=E7=8F=8A?= Date: Sun, 13 Feb 2022 18:30:09 +0800 Subject: [PATCH] Handle download fail Co-authored-by: topjohnwu --- .../magisk/core/download/DownloadService.kt | 40 +++++++++++-------- .../java/com/topjohnwu/magisk/DynLoad.java | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt index 3b111f6cc..441018af4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadService.kt @@ -29,6 +29,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.launch import timber.log.Timber import java.io.File +import java.io.IOException import java.io.InputStream import java.io.OutputStream import java.util.zip.ZipEntry @@ -84,23 +85,30 @@ class DownloadService : NotificationService() { if (isRunningAsStub) { val apk = subject.file.toFile() val id = subject.notifyId - write(StubApk.update(this).outputStream()) - if (Info.stub!!.version < subject.stub.versionCode) { - // Also upgrade stub - update(id) { - it.setProgress(0, 0, true) - .setContentTitle(getString(R.string.hide_app_title)) - .setContentText("") + try { + write(StubApk.update(this).outputStream()) + if (Info.stub!!.version < subject.stub.versionCode) { + // Also upgrade stub + update(id) { + it.setProgress(0, 0, true) + .setContentTitle(getString(R.string.hide_app_title)) + .setContentText("") + } + service.fetchFile(subject.stub.link).byteStream().writeTo(apk) + val patched = File(apk.parent, "patched.apk") + val label = applicationInfo.nonLocalizedLabel + if (!HideAPK.patch(this, apk, patched, packageName, label)) { + throw IOException("HideAPK patch error") + } + apk.delete() + patched.renameTo(apk) + } else { + val clz = Info.stub!!.classToComponent["PHOENIX"]!! + PhoenixActivity.rebirth(this, clz) + return } - service.fetchFile(subject.stub.link).byteStream().writeTo(apk) - val patched = File(apk.parent, "patched.apk") - HideAPK.patch(this, apk, patched, packageName, applicationInfo.nonLocalizedLabel) - apk.delete() - patched.renameTo(apk) - } else { - val clz = Info.stub!!.classToComponent["PHOENIX"]!! - PhoenixActivity.rebirth(this, clz) - return + } catch (e: Exception) { + StubApk.update(this).delete() } } val receiver = APKInstall.register(this, null, null) diff --git a/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java b/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java index b4effa1cf..1e116a80e 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java +++ b/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java @@ -125,7 +125,7 @@ public class DynLoad { PackageInfo pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), 0); try { return newApp(pkgInfo.applicationInfo); - } catch (ReflectiveOperationException e) { + } catch (ReflectiveOperationException | NullPointerException e) { Log.e(DynLoad.class.getSimpleName(), "", e); apk.delete(); }