Minor adjustments

This commit is contained in:
topjohnwu 2022-02-13 20:16:23 -08:00
parent 668e549208
commit e36596470c
4 changed files with 27 additions and 15 deletions

View File

@ -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;

View File

@ -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
}

View File

@ -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)
}

View File

@ -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())