Commit e3659647 authored by topjohnwu's avatar topjohnwu

Minor adjustments

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