Cosmetic changes

This commit is contained in:
topjohnwu 2019-08-23 03:05:41 +08:00
parent 85d9bd9106
commit a6126c5eda
3 changed files with 55 additions and 67 deletions

View File

@ -30,82 +30,71 @@ open class DownloadService : RemoteFileService() {
.getMimeTypeFromExtension(extension)
?: "resource/folder"
override fun onFinished(file: File, subject: DownloadSubject, id: Int) = when (subject) {
is Magisk -> onFinishedInternal(file, subject, id)
is Module -> onFinishedInternal(file, subject, id)
is Manager -> onFinishedInternal(file, subject, id)
override fun onFinished(subject: DownloadSubject, id: Int) = when (subject) {
is Magisk -> onFinishedInternal(subject, id)
is Module -> onFinishedInternal(subject, id)
is Manager -> onFinishedInternal(subject, id)
}
private fun onFinishedInternal(
file: File,
subject: Magisk,
id: Int
) = when (val conf = subject.configuration) {
Uninstall -> FlashActivity.uninstall(this, file, id)
is Patch -> FlashActivity.patch(this, file, conf.fileUri, id)
is Flash -> FlashActivity.flash(this, file, conf is Secondary, id)
Uninstall -> FlashActivity.uninstall(this, subject.file, id)
is Patch -> FlashActivity.patch(this, subject.file, conf.fileUri, id)
is Flash -> FlashActivity.flash(this, subject.file, conf is Secondary, id)
else -> Unit
}
private fun onFinishedInternal(
file: File,
subject: Module,
id: Int
) = when (subject.configuration) {
is Flash -> FlashActivity.install(this, file, id)
is Flash -> FlashActivity.install(this, subject.file, id)
else -> Unit
}
private fun onFinishedInternal(
file: File,
subject: Manager,
id: Int
) {
remove(id)
when (subject.configuration) {
is APK.Upgrade -> APKInstall.install(this, file)
is APK.Upgrade -> APKInstall.install(this, subject.file)
else -> Unit
}
}
// ---
override fun NotificationCompat.Builder.addActions(
file: File,
subject: DownloadSubject
) = when (subject) {
is Magisk -> addActionsInternal(file, subject)
is Module -> addActionsInternal(file, subject)
is Manager -> addActionsInternal(file, subject)
override fun NotificationCompat.Builder.addActions(subject: DownloadSubject)
= when (subject) {
is Magisk -> addActionsInternal(subject)
is Module -> addActionsInternal(subject)
is Manager -> addActionsInternal(subject)
}
private fun NotificationCompat.Builder.addActionsInternal(
file: File,
subject: Magisk
) = when (val conf = subject.configuration) {
private fun NotificationCompat.Builder.addActionsInternal(subject: Magisk)
= when (val conf = subject.configuration) {
Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!))
.addAction(0, R.string.download_open_self, fileIntent(subject.file))
Uninstall -> setContentIntent(FlashActivity.uninstallIntent(context, file))
is Flash -> setContentIntent(FlashActivity.flashIntent(context, file, conf is Secondary))
is Patch -> setContentIntent(FlashActivity.patchIntent(context, file, conf.fileUri))
Uninstall -> setContentIntent(FlashActivity.uninstallIntent(context, subject.file))
is Flash -> setContentIntent(FlashActivity.flashIntent(context, subject.file, conf is Secondary))
is Patch -> setContentIntent(FlashActivity.patchIntent(context, subject.file, conf.fileUri))
else -> this
}
private fun NotificationCompat.Builder.addActionsInternal(
file: File,
subject: Module
) = when (subject.configuration) {
private fun NotificationCompat.Builder.addActionsInternal(subject: Module)
= when (subject.configuration) {
Download -> addAction(0, R.string.download_open_parent, fileIntent(subject.file.parentFile!!))
.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
}
private fun NotificationCompat.Builder.addActionsInternal(
file: File,
subject: Manager
) = when (subject.configuration) {
APK.Upgrade -> setContentIntent(APKInstall.installIntent(context, file))
private fun NotificationCompat.Builder.addActionsInternal(subject: Manager)
= when (subject.configuration) {
APK.Upgrade -> setContentIntent(APKInstall.installIntent(context, subject.file))
else -> this
}

View File

@ -60,8 +60,8 @@ private fun RemoteFileService.restore(apk: File, id: Int): File {
return apk
}
fun RemoteFileService.handleAPK(apk: File, subject: DownloadSubject.Manager)
fun RemoteFileService.handleAPK(subject: DownloadSubject.Manager)
= when (subject.configuration) {
is Upgrade -> patchPackage(apk, subject.hashCode())
is Restore -> restore(apk, subject.hashCode())
is Upgrade -> patchPackage(subject.file, subject.hashCode())
is Restore -> restore(subject.file, subject.hashCode())
}

View File

@ -16,7 +16,7 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.*
import com.topjohnwu.magisk.utils.ProgressInputStream
import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.superuser.ShellUtils
import io.reactivex.Single
import io.reactivex.Completable
import okhttp3.ResponseBody
import org.koin.android.ext.android.inject
import timber.log.Timber
@ -34,9 +34,7 @@ abstract class RemoteFileService : NotificationService() {
)
override val defaultNotification: NotificationCompat.Builder
get() = Notifications
.progress(this, "")
.setContentText(getString(R.string.download_local))
get() = Notifications.progress(this, "")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.getParcelableExtra<DownloadSubject>(ARG_URL)?.let { start(it) }
@ -46,7 +44,7 @@ abstract class RemoteFileService : NotificationService() {
// ---
private fun start(subject: DownloadSubject) = search(subject)
.onErrorResumeNext(download(subject))
.onErrorResumeNext { download(subject) }
.doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } }
.subscribeK(onError = {
Timber.e(it)
@ -56,13 +54,13 @@ abstract class RemoteFileService : NotificationService() {
.setOngoing(false)
}
}) {
val newId = finishNotify(it, subject)
val newId = finishNotify(subject)
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) {
throw IllegalStateException("The download cache is disabled")
}
@ -78,23 +76,22 @@ abstract class RemoteFileService : NotificationService() {
private fun download(subject: DownloadSubject) = service.fetchFile(subject.url)
.map { it.toStream(subject.hashCode()) }
.flatMap { stream ->
.flatMapCompletable { stream ->
when (subject) {
is Module -> service.fetchInstaller()
.map { stream.toModule(subject.file, it.byteStream()); subject.file }
else -> Single.fromCallable { stream.writeTo(subject.file); subject.file }
}
}.map {
when (subject) {
is Manager -> handleAPK(it, subject)
else -> it
.doOnSuccess { stream.toModule(subject.file, it.byteStream()) }
.ignoreElement()
else -> Completable.fromAction { stream.writeTo(subject.file) }
}
}.doOnComplete {
if (subject is Manager)
handleAPK(subject)
}
// ---
private fun File.find(name: String) = list().orEmpty()
.firstOrNull { it == name }
private fun File.find(name: String) = list()
?.firstOrNull { it == name }
?.let { File(this, it) }
private fun ResponseBody.toStream(id: Int): InputStream {
@ -104,15 +101,19 @@ abstract class RemoteFileService : NotificationService() {
return ProgressInputStream(byteStream()) {
val progress = it / 1_000_000f
update(id) { notification ->
notification
.setProgress(maxRaw.toInt(), it.toInt(), false)
.setContentText(getString(R.string.download_progress, progress, max))
if (maxRaw > 0) {
notification
.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()) {
it.addActions(file, subject)
private fun finishNotify(subject: DownloadSubject) = finishNotify(subject.hashCode()) {
it.addActions(subject)
.setContentText(getString(R.string.download_complete))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setProgress(0, 0, false)
@ -124,12 +125,10 @@ abstract class RemoteFileService : NotificationService() {
@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(
file: File,
subject: DownloadSubject
): NotificationCompat.Builder
protected abstract fun NotificationCompat.Builder.addActions(subject: DownloadSubject)
: NotificationCompat.Builder
companion object {
const val ARG_URL = "arg_url"