diff --git a/app/shared/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java b/app/shared/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java index 6e4729be5..b82199286 100644 --- a/app/shared/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java +++ b/app/shared/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java @@ -56,8 +56,11 @@ public final class APKInstall { } public interface Session { + // @WorkerThread OutputStream openStream(Context context) throws IOException; + // @WorkerThread void install(Context context, File apk) throws IOException; + // @WorkerThread @Nullable Intent waitIntent(); } @@ -106,11 +109,10 @@ public final class APKInstall { context.getApplicationContext().unregisterReceiver(this); } - // @WorkerThread @Nullable @Override public Intent waitIntent() { try { - //noinspection ResultOfMethodCallIgnored + // noinspection ResultOfMethodCallIgnored latch.await(5, TimeUnit.SECONDS); } catch (Exception ignored) {} return userAction; 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 1e202bc41..890328022 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 @@ -13,12 +13,14 @@ import com.topjohnwu.magisk.R import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.* import com.topjohnwu.magisk.core.tasks.HideAPK +import com.topjohnwu.magisk.core.utils.MediaStoreUtils import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream import com.topjohnwu.magisk.ktx.copyAndClose import com.topjohnwu.magisk.ktx.forEach import com.topjohnwu.magisk.ktx.withStreams import com.topjohnwu.magisk.ktx.writeTo import com.topjohnwu.magisk.utils.APKInstall +import com.topjohnwu.magisk.view.Notifications import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -57,12 +59,13 @@ class DownloadService : NotificationService() { is Subject.Module -> handleModule(stream, subject.file) } val activity = ActivityTracker.foreground - if (activity != null && subject.autoStart) { + if (activity != null && subject.autoLaunch) { remove(subject.notifyId) subject.pendingIntent(activity)?.send() } else { notifyFinish(subject) } + subject.postDownload?.invoke() if (!hasNotifications) stopSelf() } catch (e: Exception) { @@ -79,7 +82,8 @@ class DownloadService : NotificationService() { private suspend fun handleApp(stream: InputStream, subject: Subject.App) { fun writeTee(output: OutputStream) { - val external = subject.externalFile.outputStream() + val uri = MediaStoreUtils.getFile("${subject.title}.apk").uri + val external = uri.outputStream() stream.copyAndClose(TeeOutputStream(external, output)) } @@ -120,8 +124,9 @@ class DownloadService : NotificationService() { // Relaunch the process if we are foreground StubApk.restartProcess(it) } ?: run { - // Or else simply kill the current process - Runtime.getRuntime().exit(0) + // Or else kill the current process after posting notification + subject.intent = Notifications.selfLaunchIntent(this) + subject.postDownload = { Runtime.getRuntime().exit(0) } } return } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt index 001abdd2a..a43a9382a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt @@ -32,7 +32,8 @@ sealed class Subject : Parcelable { abstract val file: Uri abstract val title: String abstract val notifyId: Int - open val autoStart: Boolean get() = true + open val autoLaunch: Boolean get() = true + open val postDownload: (() -> Unit)? get() = null abstract fun pendingIntent(context: Context): PendingIntent? @@ -44,7 +45,7 @@ sealed class Subject : Parcelable { ) : Subject() { override val url: String get() = module.zipUrl override val title: String get() = module.downloadFilename - override val autoStart: Boolean get() = action == Action.Flash + override val autoLaunch: Boolean get() = action == Action.Flash @IgnoredOnParcel override val file by lazy { @@ -69,11 +70,11 @@ sealed class Subject : Parcelable { cachedFile("manager.apk") } + @IgnoredOnParcel + override var postDownload: (() -> Unit)? = null + @IgnoredOnParcel var intent: Intent? = null - - val externalFile get() = MediaStoreUtils.getFile("$title.apk").uri - override fun pendingIntent(context: Context) = intent?.toPending(context) } diff --git a/app/src/main/java/com/topjohnwu/magisk/view/Notifications.kt b/app/src/main/java/com/topjohnwu/magisk/view/Notifications.kt index b253f4bd1..b7195c722 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/Notifications.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/Notifications.kt @@ -43,13 +43,17 @@ object Notifications { } } + fun selfLaunchIntent(context: Context): Intent { + val pm = context.packageManager + val intent = pm.getLaunchIntentForPackage(context.packageName)!! + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) + return intent + } + @SuppressLint("InlinedApi") fun updateDone(context: Context) { - val pm = context.packageManager - val intent = pm.getLaunchIntentForPackage(context.packageName) ?: return - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - val pending = PendingIntent.getActivity(context, 0, intent, flag) + val pending = PendingIntent.getActivity(context, 0, selfLaunchIntent(context), flag) val builder = if (SDK_INT >= 26) { Notification.Builder(context, UPDATED_CHANNEL) .setSmallIcon(context.getBitmap(R.drawable.ic_magisk_outline).toIcon())