Commit a6126c5e authored by topjohnwu's avatar topjohnwu

Cosmetic changes

parent 85d9bd91
...@@ -30,82 +30,71 @@ open class DownloadService : RemoteFileService() { ...@@ -30,82 +30,71 @@ open class DownloadService : RemoteFileService() {
.getMimeTypeFromExtension(extension) .getMimeTypeFromExtension(extension)
?: "resource/folder" ?: "resource/folder"
override fun onFinished(file: File, subject: DownloadSubject, id: Int) = when (subject) { override fun onFinished(subject: DownloadSubject, id: Int) = when (subject) {
is Magisk -> onFinishedInternal(file, subject, id) is Magisk -> onFinishedInternal(subject, id)
is Module -> onFinishedInternal(file, subject, id) is Module -> onFinishedInternal(subject, id)
is Manager -> onFinishedInternal(file, subject, id) is Manager -> onFinishedInternal(subject, id)
} }
private fun onFinishedInternal( private fun onFinishedInternal(
file: File,
subject: Magisk, subject: Magisk,
id: Int id: Int
) = when (val conf = subject.configuration) { ) = when (val conf = subject.configuration) {
Uninstall -> FlashActivity.uninstall(this, file, id) Uninstall -> FlashActivity.uninstall(this, subject.file, id)
is Patch -> FlashActivity.patch(this, file, conf.fileUri, id) is Patch -> FlashActivity.patch(this, subject.file, conf.fileUri, id)
is Flash -> FlashActivity.flash(this, file, conf is Secondary, id) is Flash -> FlashActivity.flash(this, subject.file, conf is Secondary, id)
else -> Unit else -> Unit
} }
private fun onFinishedInternal( private fun onFinishedInternal(
file: File,
subject: Module, subject: Module,
id: Int id: Int
) = when (subject.configuration) { ) = when (subject.configuration) {
is Flash -> FlashActivity.install(this, file, id) is Flash -> FlashActivity.install(this, subject.file, id)
else -> Unit else -> Unit
} }
private fun onFinishedInternal( private fun onFinishedInternal(
file: File,
subject: Manager, subject: Manager,
id: Int id: Int
) { ) {
remove(id) remove(id)
when (subject.configuration) { when (subject.configuration) {
is APK.Upgrade -> APKInstall.install(this, file) is APK.Upgrade -> APKInstall.install(this, subject.file)
else -> Unit else -> Unit
} }
} }
// --- // ---
override fun NotificationCompat.Builder.addActions( override fun NotificationCompat.Builder.addActions(subject: DownloadSubject)
file: File, = when (subject) {
subject: DownloadSubject is Magisk -> addActionsInternal(subject)
) = when (subject) { is Module -> addActionsInternal(subject)
is Magisk -> addActionsInternal(file, subject) is Manager -> addActionsInternal(subject)
is Module -> addActionsInternal(file, subject)
is Manager -> addActionsInternal(file, subject)
} }
private fun NotificationCompat.Builder.addActionsInternal( private fun NotificationCompat.Builder.addActionsInternal(subject: Magisk)
file: File, = when (val conf = subject.configuration) {
subject: Magisk
) = when (val conf = subject.configuration) {
Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!)) Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!))
.addAction(0, R.string.download_open_self, fileIntent(subject.file)) .addAction(0, R.string.download_open_self, fileIntent(subject.file))
Uninstall -> setContentIntent(FlashActivity.uninstallIntent(context, file)) Uninstall -> setContentIntent(FlashActivity.uninstallIntent(context, subject.file))
is Flash -> setContentIntent(FlashActivity.flashIntent(context, file, conf is Secondary)) is Flash -> setContentIntent(FlashActivity.flashIntent(context, subject.file, conf is Secondary))
is Patch -> setContentIntent(FlashActivity.patchIntent(context, file, conf.fileUri)) is Patch -> setContentIntent(FlashActivity.patchIntent(context, subject.file, conf.fileUri))
else -> this else -> this
} }
private fun NotificationCompat.Builder.addActionsInternal( private fun NotificationCompat.Builder.addActionsInternal(subject: Module)
file: File, = when (subject.configuration) {
subject: Module
) = when (subject.configuration) {
Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!)) Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!))
.addAction(0, R.string.download_open_self, fileIntent(subject.file)) .addAction(0, R.string.download_open_self, fileIntent(subject.file))
is Flash -> setContentIntent(FlashActivity.installIntent(context, file)) is Flash -> setContentIntent(FlashActivity.installIntent(context, subject.file))
else -> this else -> this
} }
private fun NotificationCompat.Builder.addActionsInternal( private fun NotificationCompat.Builder.addActionsInternal(subject: Manager)
file: File, = when (subject.configuration) {
subject: Manager APK.Upgrade -> setContentIntent(APKInstall.installIntent(context, subject.file))
) = when (subject.configuration) {
APK.Upgrade -> setContentIntent(APKInstall.installIntent(context, file))
else -> this else -> this
} }
......
...@@ -60,8 +60,8 @@ private fun RemoteFileService.restore(apk: File, id: Int): File { ...@@ -60,8 +60,8 @@ private fun RemoteFileService.restore(apk: File, id: Int): File {
return apk return apk
} }
fun RemoteFileService.handleAPK(apk: File, subject: DownloadSubject.Manager) fun RemoteFileService.handleAPK(subject: DownloadSubject.Manager)
= when (subject.configuration) { = when (subject.configuration) {
is Upgrade -> patchPackage(apk, subject.hashCode()) is Upgrade -> patchPackage(subject.file, subject.hashCode())
is Restore -> restore(apk, subject.hashCode()) is Restore -> restore(subject.file, subject.hashCode())
} }
...@@ -16,7 +16,7 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.* ...@@ -16,7 +16,7 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.*
import com.topjohnwu.magisk.utils.ProgressInputStream import com.topjohnwu.magisk.utils.ProgressInputStream
import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.superuser.ShellUtils import com.topjohnwu.superuser.ShellUtils
import io.reactivex.Single import io.reactivex.Completable
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
import timber.log.Timber import timber.log.Timber
...@@ -34,9 +34,7 @@ abstract class RemoteFileService : NotificationService() { ...@@ -34,9 +34,7 @@ abstract class RemoteFileService : NotificationService() {
) )
override val defaultNotification: NotificationCompat.Builder override val defaultNotification: NotificationCompat.Builder
get() = Notifications get() = Notifications.progress(this, "")
.progress(this, "")
.setContentText(getString(R.string.download_local))
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.getParcelableExtra<DownloadSubject>(ARG_URL)?.let { start(it) } intent?.getParcelableExtra<DownloadSubject>(ARG_URL)?.let { start(it) }
...@@ -46,7 +44,7 @@ abstract class RemoteFileService : NotificationService() { ...@@ -46,7 +44,7 @@ abstract class RemoteFileService : NotificationService() {
// --- // ---
private fun start(subject: DownloadSubject) = search(subject) private fun start(subject: DownloadSubject) = search(subject)
.onErrorResumeNext(download(subject)) .onErrorResumeNext { download(subject) }
.doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } } .doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } }
.subscribeK(onError = { .subscribeK(onError = {
Timber.e(it) Timber.e(it)
...@@ -56,13 +54,13 @@ abstract class RemoteFileService : NotificationService() { ...@@ -56,13 +54,13 @@ abstract class RemoteFileService : NotificationService() {
.setOngoing(false) .setOngoing(false)
} }
}) { }) {
val newId = finishNotify(it, subject) val newId = finishNotify(subject)
if (get<Activity>() !is NullActivity) { if (get<Activity>() !is NullActivity) {
onFinished(it, subject, newId) onFinished(subject, newId)
} }
} }
private fun search(subject: DownloadSubject) = Single.fromCallable { private fun search(subject: DownloadSubject) = Completable.fromAction {
if (!Config.isDownloadCacheEnabled || subject is Manager) { if (!Config.isDownloadCacheEnabled || subject is Manager) {
throw IllegalStateException("The download cache is disabled") throw IllegalStateException("The download cache is disabled")
} }
...@@ -78,23 +76,22 @@ abstract class RemoteFileService : NotificationService() { ...@@ -78,23 +76,22 @@ abstract class RemoteFileService : NotificationService() {
private fun download(subject: DownloadSubject) = service.fetchFile(subject.url) private fun download(subject: DownloadSubject) = service.fetchFile(subject.url)
.map { it.toStream(subject.hashCode()) } .map { it.toStream(subject.hashCode()) }
.flatMap { stream -> .flatMapCompletable { stream ->
when (subject) { when (subject) {
is Module -> service.fetchInstaller() is Module -> service.fetchInstaller()
.map { stream.toModule(subject.file, it.byteStream()); subject.file } .doOnSuccess { stream.toModule(subject.file, it.byteStream()) }
else -> Single.fromCallable { stream.writeTo(subject.file); subject.file } .ignoreElement()
} else -> Completable.fromAction { stream.writeTo(subject.file) }
}.map {
when (subject) {
is Manager -> handleAPK(it, subject)
else -> it
} }
}.doOnComplete {
if (subject is Manager)
handleAPK(subject)
} }
// --- // ---
private fun File.find(name: String) = list().orEmpty() private fun File.find(name: String) = list()
.firstOrNull { it == name } ?.firstOrNull { it == name }
?.let { File(this, it) } ?.let { File(this, it) }
private fun ResponseBody.toStream(id: Int): InputStream { private fun ResponseBody.toStream(id: Int): InputStream {
...@@ -104,15 +101,19 @@ abstract class RemoteFileService : NotificationService() { ...@@ -104,15 +101,19 @@ abstract class RemoteFileService : NotificationService() {
return ProgressInputStream(byteStream()) { return ProgressInputStream(byteStream()) {
val progress = it / 1_000_000f val progress = it / 1_000_000f
update(id) { notification -> update(id) { notification ->
notification if (maxRaw > 0) {
.setProgress(maxRaw.toInt(), it.toInt(), false) notification
.setContentText(getString(R.string.download_progress, progress, max)) .setProgress(maxRaw.toInt(), it.toInt(), false)
.setContentText("%.2f / %.2f MB".format(progress, max))
} else {
notification.setContentText("%.2f MB / ??".format(progress))
}
} }
} }
} }
private fun finishNotify(file: File, subject: DownloadSubject) = finishNotify(subject.hashCode()) { private fun finishNotify(subject: DownloadSubject) = finishNotify(subject.hashCode()) {
it.addActions(file, subject) it.addActions(subject)
.setContentText(getString(R.string.download_complete)) .setContentText(getString(R.string.download_complete))
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setProgress(0, 0, false) .setProgress(0, 0, false)
...@@ -124,12 +125,10 @@ abstract class RemoteFileService : NotificationService() { ...@@ -124,12 +125,10 @@ abstract class RemoteFileService : NotificationService() {
@Throws(Throwable::class) @Throws(Throwable::class)
protected abstract fun onFinished(file: File, subject: DownloadSubject, id: Int) protected abstract fun onFinished(subject: DownloadSubject, id: Int)
protected abstract fun NotificationCompat.Builder.addActions( protected abstract fun NotificationCompat.Builder.addActions(subject: DownloadSubject)
file: File, : NotificationCompat.Builder
subject: DownloadSubject
): NotificationCompat.Builder
companion object { companion object {
const val ARG_URL = "arg_url" const val ARG_URL = "arg_url"
......
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