Minor fixes and cleanups

This commit is contained in:
topjohnwu
2019-07-22 01:49:21 -07:00
parent 6fb032b3c2
commit 094c3d559a
12 changed files with 47 additions and 86 deletions
@@ -6,13 +6,13 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Environment
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.annotation.RequiresPermission
import androidx.core.app.NotificationCompat
import com.topjohnwu.magisk.ClassMap
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.entity.internal.Configuration.*
import com.topjohnwu.magisk.model.entity.internal.Configuration.Flash.Secondary
@@ -31,7 +31,7 @@ import kotlin.random.Random.Default.nextInt
open class DownloadService : RemoteFileService() {
private val context get() = this
private val String.downloadsFile get() = Config.downloadsFile()?.let { File(it, this) }
private val String.downloadsFile get() = File(Config.downloadDirectory, this)
private val File.type
get() = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension)
@@ -40,7 +40,6 @@ open class DownloadService : RemoteFileService() {
override fun onFinished(file: File, subject: DownloadSubject) = when (subject) {
is Magisk -> onFinishedInternal(file, subject)
is Module -> onFinishedInternal(file, subject)
else -> Unit
}
private fun onFinishedInternal(
@@ -71,7 +70,6 @@ open class DownloadService : RemoteFileService() {
) = when (subject) {
is Magisk -> addActionsInternal(file, subject)
is Module -> addActionsInternal(file, subject)
else -> this
}
private fun NotificationCompat.Builder.addActionsInternal(
@@ -109,13 +107,7 @@ open class DownloadService : RemoteFileService() {
// ---
private fun moveToDownloads(file: File) {
val destination = file.name.downloadsFile ?: let {
Utils.toast(
getString(R.string.download_file_folder_error),
Toast.LENGTH_LONG
)
return
}
val destination = file.name.downloadsFile
if (file != destination) {
destination.deleteRecursively()
@@ -125,32 +117,18 @@ open class DownloadService : RemoteFileService() {
Utils.toast(
getString(
R.string.internal_storage,
"/" + destination.toRelativeString(Const.EXTERNAL_PATH.parentFile)
"/" + destination.toRelativeString(Environment.getExternalStorageDirectory())
),
Toast.LENGTH_LONG
)
}
private fun fileIntent(fileName: String): Intent {
val file = fileName.downloadsFile ?: let {
Utils.toast(
getString(R.string.download_file_folder_error),
Toast.LENGTH_LONG
)
return Intent()
}
return fileIntent(file)
return fileIntent(fileName.downloadsFile)
}
private fun fileParentIntent(fileName: String): Intent {
val file = fileName.downloadsFile?.parentFile ?: let {
Utils.toast(
getString(R.string.download_file_folder_error),
Toast.LENGTH_LONG
)
return Intent()
}
return fileIntent(file)
return fileIntent(fileName.downloadsFile.parentFile!!)
}
private fun fileIntent(file: File): Intent {
@@ -8,7 +8,8 @@ import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.FileRepository
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.*
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Magisk
import com.topjohnwu.magisk.model.entity.internal.DownloadSubject.Module
import com.topjohnwu.magisk.utils.ProgInputStream
import com.topjohnwu.magisk.utils.cachedFile
import com.topjohnwu.magisk.utils.firstMap
@@ -30,8 +31,7 @@ abstract class RemoteFileService : NotificationService() {
private val supportedFolders
get() = listOfNotNull(
cacheDir,
Config.downloadsFile(),
Const.EXTERNAL_PATH
Config.downloadDirectory
)
override val defaultNotification: NotificationCompat.Builder
@@ -46,31 +46,27 @@ abstract class RemoteFileService : NotificationService() {
// ---
private fun startInternal(subject: DownloadSubject): Single<File> = search(subject)
private fun start(subject: DownloadSubject) = search(subject)
.onErrorResumeNext(download(subject))
.doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.fileName) } }
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess {
runCatching { onFinished(it, subject) }.onFailure { Timber.e(it) }
finish(it, subject)
}
private fun start(subject: DownloadSubject) = startInternal(subject).subscribeK()
}.subscribeK()
private fun search(subject: DownloadSubject) = Single.fromCallable {
if (!Config.isDownloadCacheEnabled) {
throw IllegalStateException("The download cache is disabled")
}
val file = supportedFolders.firstMap { it.find(subject.fileName) }
if (subject is Magisk) {
if (!ShellUtils.checkSum("MD5", file, subject.magisk.hash)) {
throw IllegalStateException("The given file doesn't match the hash")
supportedFolders.firstMap { it.find(subject.fileName) }.also {
if (subject is Magisk) {
if (!ShellUtils.checkSum("MD5", it, subject.magisk.hash)) {
throw IllegalStateException("The given file doesn't match the hash")
}
}
}
file
}
private fun download(subject: DownloadSubject) = repo.downloadFile(subject.url)
@@ -107,8 +103,6 @@ abstract class RemoteFileService : NotificationService() {
}
private fun finish(file: File, subject: DownloadSubject) = finishWork(subject.hashCode()) {
if (subject is Installer) return@finishWork null
it.addActions(file, subject)
.setContentText(getString(R.string.download_complete))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
@@ -14,10 +14,6 @@ sealed class Configuration : Parcelable {
@Parcelize
object Secondary : Flash()
companion object {
operator fun invoke(): Flash = Primary
}
}
@Parcelize
@@ -1,12 +0,0 @@
package com.topjohnwu.magisk.model.entity.internal
import com.skoumal.teanity.util.KObservableField
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.model.observer.Observer
class DownloadDialogData(initialValue: String) {
val text = KObservableField(initialValue)
val path = Observer(text) { Config.downloadsFile(text.value)?.absolutePath.orEmpty() }
}
@@ -1,8 +1,6 @@
package com.topjohnwu.magisk.model.entity.internal
import android.os.Parcelable
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.Info
import com.topjohnwu.magisk.model.entity.MagiskJson
import com.topjohnwu.magisk.model.entity.Repo
@@ -39,10 +37,4 @@ sealed class DownloadSubject : Parcelable {
}
@Parcelize
object Installer : DownloadSubject() {
override val fileName: String get() = "module_installer(${BuildConfig.VERSION_CODE}).sh"
override val url: String get() = Const.Url.MODULE_INSTALLER
}
}