Added internal download pseudo broadcasts

This commit is contained in:
Viktor De Pasquale 2019-10-20 12:40:26 +02:00
parent c954a4f7bc
commit 6d03798314
3 changed files with 26 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import com.topjohnwu.magisk.utils.RxBus
import org.koin.core.qualifier.named
@ -23,6 +24,7 @@ val applicationModule = module {
single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) }
single { ActivityTracker() }
factory { get<ActivityTracker>().foreground ?: NullActivity }
single { LocalBroadcastManager.getInstance(get()) }
}
private fun createDEContext(context: Context): Context {

View File

@ -3,6 +3,8 @@ package com.topjohnwu.magisk.model.download
import android.app.Activity
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.di.NullActivity
@ -17,6 +19,7 @@ import com.topjohnwu.superuser.ShellUtils
import io.reactivex.Completable
import okhttp3.ResponseBody
import org.koin.android.ext.android.inject
import org.koin.core.KoinComponent
import timber.log.Timber
import java.io.InputStream
@ -62,7 +65,7 @@ abstract class RemoteFileService : NotificationService() {
}
private fun download(subject: DownloadSubject) = service.fetchFile(subject.url)
.map { it.toStream(subject.hashCode()) }
.map { it.toStream(subject.hashCode(), subject) }
.flatMapCompletable { stream ->
when (subject) {
is Module -> service.fetchInstaller()
@ -75,7 +78,7 @@ abstract class RemoteFileService : NotificationService() {
handleAPK(subject)
}
private fun ResponseBody.toStream(id: Int): InputStream {
private fun ResponseBody.toStream(id: Int, subject: DownloadSubject): InputStream {
val maxRaw = contentLength()
val max = maxRaw / 1_000_000f
@ -83,6 +86,7 @@ abstract class RemoteFileService : NotificationService() {
val progress = it / 1_000_000f
update(id) { notification ->
if (maxRaw > 0) {
send(progress / max, subject)
notification
.setProgress(maxRaw.toInt(), it.toInt(), false)
.setContentText("%.2f / %.2f MB".format(progress, max))
@ -94,6 +98,7 @@ abstract class RemoteFileService : NotificationService() {
}
private fun finishNotify(subject: DownloadSubject) = finishNotify(subject.hashCode()) {
send(1f, subject)
it.addActions(subject)
.setContentText(getString(R.string.download_complete))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
@ -111,8 +116,15 @@ abstract class RemoteFileService : NotificationService() {
protected abstract fun NotificationCompat.Builder.addActions(subject: DownloadSubject)
: NotificationCompat.Builder
companion object {
companion object : KoinComponent {
const val ARG_URL = "arg_url"
private val internalProgressBroadcast = MutableLiveData<Pair<Float, DownloadSubject>>()
val progressBroadcast: LiveData<Pair<Float, DownloadSubject>> get() = internalProgressBroadcast
fun send(progress: Float, subject: DownloadSubject) {
internalProgressBroadcast.postValue(progress to subject)
}
}
}

View File

@ -59,7 +59,7 @@ sealed class DownloadSubject : Parcelable {
val magisk: MagiskJson = Info.remote.magisk
@Parcelize
protected data class Flash(
data class Flash(
override val configuration: Configuration
) : Magisk() {
override val url: String get() = magisk.link
@ -72,7 +72,7 @@ sealed class DownloadSubject : Parcelable {
}
@Parcelize
protected class Uninstall : Magisk() {
class Uninstall : Magisk() {
override val configuration: Configuration get() = Configuration.Uninstall
override val url: String get() = Info.remote.uninstaller.link
@ -83,7 +83,7 @@ sealed class DownloadSubject : Parcelable {
}
@Parcelize
protected class Download : Magisk() {
class Download : Magisk() {
override val configuration: Configuration get() = Configuration.Download
override val url: String get() = magisk.link