From 9d5efea66ea789977f639ee4ddc5e9e59a5620c4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 24 Jan 2021 05:14:46 -0800 Subject: [PATCH] Remove ManagerJson Everything is now Magisk --- .../magisk/core/UpdateCheckService.kt | 15 ++---- .../topjohnwu/magisk/core/download/Subject.kt | 8 +-- .../topjohnwu/magisk/core/model/UpdateInfo.kt | 11 +--- .../topjohnwu/magisk/core/tasks/HideAPK.kt | 2 +- .../magisk/data/repository/NetworkService.kt | 8 +-- .../com/topjohnwu/magisk/events/ViewEvents.kt | 16 +++--- .../events/dialog/ManagerInstallDialog.kt | 31 ++++------- .../magisk/events/dialog/MarkDownDialog.kt | 48 +++++++++++++++++ .../magisk/ui/module/ModuleViewModel.kt | 6 +-- .../magisk/ui/settings/SettingsItems.kt | 2 +- .../topjohnwu/magisk/view/MarkDownWindow.kt | 54 ------------------- 11 files changed, 84 insertions(+), 117 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/events/dialog/MarkDownDialog.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/view/MarkDownWindow.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/core/UpdateCheckService.kt b/app/src/main/java/com/topjohnwu/magisk/core/UpdateCheckService.kt index bae7b7630..3abf5d5d8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/UpdateCheckService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/UpdateCheckService.kt @@ -1,13 +1,11 @@ package com.topjohnwu.magisk.core +import android.annotation.SuppressLint import android.content.Context import androidx.work.* import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.view.Notifications -import com.topjohnwu.superuser.Shell -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import org.koin.core.KoinComponent import org.koin.core.inject import java.util.concurrent.TimeUnit @@ -18,20 +16,15 @@ class UpdateCheckService(context: Context, workerParams: WorkerParameters) private val svc: NetworkService by inject() override suspend fun doWork(): Result { - // Make sure shell initializer was ran - withContext(Dispatchers.IO) { - Shell.getShell() - } - return svc.fetchUpdate()?.let { - if (BuildConfig.VERSION_CODE < it.app.versionCode) + return svc.fetchUpdate()?.run { + if (Info.env.isActive && BuildConfig.VERSION_CODE < magisk.versionCode) Notifications.managerUpdate(applicationContext) - else if (Info.env.isActive && Info.env.magiskVersionCode < it.magisk.versionCode) - Notifications.magiskUpdate(applicationContext) Result.success() } ?: Result.failure() } companion object { + @SuppressLint("NewApi") fun schedule(context: Context) { if (Config.checkUpdate) { val constraints = Constraints.Builder() diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt index ad88d8913..b0e022a69 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/Subject.kt @@ -5,7 +5,7 @@ import android.net.Uri import android.os.Parcelable import androidx.core.net.toUri import com.topjohnwu.magisk.core.Info -import com.topjohnwu.magisk.core.model.ManagerJson +import com.topjohnwu.magisk.core.model.MagiskJson import com.topjohnwu.magisk.core.model.StubJson import com.topjohnwu.magisk.core.model.module.OnlineModule import com.topjohnwu.magisk.core.utils.MediaStoreUtils @@ -39,12 +39,12 @@ sealed class Subject : Parcelable { @Parcelize class Manager( - private val app: ManagerJson = Info.remote.app, + private val json: MagiskJson = Info.remote.magisk, val stub: StubJson = Info.remote.stub ) : Subject() { override val action get() = Action.Download - override val title: String get() = "MagiskManager-${app.version}(${app.versionCode})" - override val url: String get() = app.link + override val title: String get() = "Magisk-${json.version}(${json.versionCode})" + override val url: String get() = json.link @IgnoredOnParcel override val file by lazy { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt index d4ff44455..017b513a1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/UpdateInfo.kt @@ -6,11 +6,11 @@ import kotlinx.parcelize.Parcelize @JsonClass(generateAdapter = true) data class UpdateInfo( - val app: ManagerJson = ManagerJson(), val magisk: MagiskJson = MagiskJson(), val stub: StubJson = StubJson() ) +@Parcelize @JsonClass(generateAdapter = true) data class MagiskJson( val version: String = "", @@ -18,15 +18,6 @@ data class MagiskJson( val link: String = "", val note: String = "", val md5: String = "" -) - -@Parcelize -@JsonClass(generateAdapter = true) -data class ManagerJson( - val version: String = "", - val versionCode: Int = -1, - val link: String = "", - val note: String = "" ) : Parcelable @Parcelize diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt index 597e988e7..7f7f23cf6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/HideAPK.kt @@ -148,7 +148,7 @@ object HideAPK { } else { File(context.cacheDir, "manager.apk").also { apk -> try { - svc.fetchFile(Info.remote.app.link).byteStream().writeTo(apk) + svc.fetchFile(Info.remote.magisk.link).byteStream().writeTo(apk) } catch (e: IOException) { Timber.e(e) return false diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt index c04e8736c..ab95f4775 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt @@ -8,7 +8,9 @@ import com.topjohnwu.magisk.core.Config.Value.DEFAULT_CHANNEL import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Info -import com.topjohnwu.magisk.core.model.* +import com.topjohnwu.magisk.core.model.MagiskJson +import com.topjohnwu.magisk.core.model.StubJson +import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.data.network.* import retrofit2.HttpException import timber.log.Timber @@ -47,14 +49,12 @@ class NetworkService( val info = jsd.fetchCanaryUpdate(sha) fun genCDNUrl(name: String) = "${Const.Url.JS_DELIVR_URL}${MAGISK_FILES}@${sha}/${name}" - fun ManagerJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note)) fun MagiskJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note)) fun StubJson.updateCopy() = copy(link = genCDNUrl(link)) return info.copy( - app = info.app.updateCopy(), magisk = info.magisk.updateCopy(), - stub = info.stub.updateCopy(), + stub = info.stub.updateCopy() ) } diff --git a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt index 2a6165cd3..50c6f3b17 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt @@ -14,20 +14,22 @@ import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.base.ActivityResultCallback import com.topjohnwu.magisk.core.base.BaseActivity import com.topjohnwu.magisk.core.model.module.OnlineModule +import com.topjohnwu.magisk.events.dialog.MarkDownDialog import com.topjohnwu.magisk.utils.Utils -import com.topjohnwu.magisk.view.MarkDownWindow +import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.Shortcuts -import kotlinx.coroutines.launch class ViewActionEvent(val action: BaseActivity.() -> Unit) : ViewEvent(), ActivityExecutor { override fun invoke(activity: BaseUIActivity<*, *>) = action(activity) } -class OpenReadmeEvent(val item: OnlineModule) : ViewEventWithScope(), ContextExecutor { - override fun invoke(context: Context) { - scope.launch { - MarkDownWindow.show(context, null, item::notes) - } +class OpenReadmeEvent(private val item: OnlineModule) : MarkDownDialog() { + override suspend fun getMarkdownText() = item.notes() + override fun build(dialog: MagiskDialog) { + super.build(dialog) + dialog.applyButton(MagiskDialog.ButtonType.NEGATIVE) { + titleRes = android.R.string.cancel + }.cancellable(true) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt b/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt index 00498522e..78a2c053f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/dialog/ManagerInstallDialog.kt @@ -4,37 +4,26 @@ import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.DownloadService import com.topjohnwu.magisk.core.download.Subject -import com.topjohnwu.magisk.ktx.res +import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.view.MagiskDialog -import com.topjohnwu.magisk.view.MarkDownWindow -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +import org.koin.core.inject -class ManagerInstallDialog : DialogEvent() { +class ManagerInstallDialog : MarkDownDialog() { + + private val svc: NetworkService by inject() + + override suspend fun getMarkdownText() = svc.fetchString(Info.remote.magisk.note) override fun build(dialog: MagiskDialog) { + super.build(dialog) with(dialog) { - val subject = Subject.Manager() - - applyTitle(R.string.repo_install_title.res(R.string.app_name.res())) - applyMessage(R.string.repo_install_msg.res(subject.title)) - setCancelable(true) - applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = R.string.install - onClick { DownloadService.start(context, subject) } + onClick { DownloadService.start(context, Subject.Manager()) } } - - if (Info.remote.app.note.isEmpty()) return applyButton(MagiskDialog.ButtonType.NEGATIVE) { - titleRes = R.string.app_changelog - onClick { - GlobalScope.launch(Dispatchers.Main.immediate) { - MarkDownWindow.show(context, null, Info.remote.app.note) - } - } + titleRes = android.R.string.cancel } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/events/dialog/MarkDownDialog.kt b/app/src/main/java/com/topjohnwu/magisk/events/dialog/MarkDownDialog.kt new file mode 100644 index 000000000..2891d3b39 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/events/dialog/MarkDownDialog.kt @@ -0,0 +1,48 @@ +package com.topjohnwu.magisk.events.dialog + +import android.view.LayoutInflater +import android.widget.TextView +import androidx.annotation.CallSuper +import androidx.lifecycle.lifecycleScope +import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.core.base.BaseActivity +import com.topjohnwu.magisk.ktx.coroutineScope +import com.topjohnwu.magisk.view.MagiskDialog +import io.noties.markwon.Markwon +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import org.koin.core.KoinComponent +import org.koin.core.inject +import timber.log.Timber +import kotlin.coroutines.cancellation.CancellationException + +abstract class MarkDownDialog : DialogEvent(), KoinComponent { + + private val markwon: Markwon by inject() + + abstract suspend fun getMarkdownText(): String + + @CallSuper + override fun build(dialog: MagiskDialog) { + with(dialog) { + val view = LayoutInflater.from(context).inflate(R.layout.markdown_window_md2, null) + applyView(view) + (ownerActivity as BaseActivity).lifecycleScope.launch { + val tv = view.findViewById(R.id.md_txt) + tv.coroutineScope = CoroutineScope(coroutineContext) + withContext(Dispatchers.IO) { + try { + markwon.setMarkdown(tv, getMarkdownText()) + } catch (e: Exception) { + if (e is CancellationException) + throw e + Timber.e(e) + tv.post { tv.setText(R.string.download_file_error) } + } + } + } + } + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt index 103bf5a8d..a4d38cf87 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/module/ModuleViewModel.kt @@ -313,10 +313,8 @@ class ModuleViewModel( fun infoPressed(item: ModuleItem) { item.repo?.also { - if (isConnected.get()) - OpenReadmeEvent(it).publish() - else - SnackbarEvent(R.string.no_connection).publish() + if (isConnected.get()) OpenReadmeEvent(it).publish() + else SnackbarEvent(R.string.no_connection).publish() } ?: return } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt index 07a8b18ed..8ea449d28 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt @@ -109,7 +109,7 @@ object Restore : BaseSettingsItem.Blank() { override val title = R.string.settings_restore_manager_title.asTransitive() override val description = R.string.settings_restore_manager_summary.asTransitive() override fun refresh() { - isEnabled = Info.remote.app.versionCode > 0 + isEnabled = Info.remote.magisk.versionCode > 0 } } diff --git a/app/src/main/java/com/topjohnwu/magisk/view/MarkDownWindow.kt b/app/src/main/java/com/topjohnwu/magisk/view/MarkDownWindow.kt deleted file mode 100644 index 743e24458..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/view/MarkDownWindow.kt +++ /dev/null @@ -1,54 +0,0 @@ -package com.topjohnwu.magisk.view - -import android.content.Context -import android.view.LayoutInflater -import android.widget.TextView -import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.data.repository.NetworkService -import com.topjohnwu.magisk.ktx.coroutineScope -import io.noties.markwon.Markwon -import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.koin.core.KoinComponent -import org.koin.core.inject -import timber.log.Timber -import kotlin.coroutines.coroutineContext - -object MarkDownWindow : KoinComponent { - - private val svc: NetworkService by inject() - private val markwon: Markwon by inject() - - suspend fun show(activity: Context, title: String?, url: String) { - show(activity, title) { - svc.fetchString(url) - } - } - - suspend fun show(activity: Context, title: String?, input: suspend () -> String) { - val view = LayoutInflater.from(activity).inflate(R.layout.markdown_window_md2, null) - - MagiskDialog(activity) - .applyTitle(title ?: "") - .applyView(view) - .applyButton(MagiskDialog.ButtonType.NEGATIVE) { - titleRes = android.R.string.cancel - } - .reveal() - - val tv = view.findViewById(R.id.md_txt) - tv.coroutineScope = CoroutineScope(coroutineContext) - withContext(Dispatchers.IO) { - try { - markwon.setMarkdown(tv, input()) - } catch (e: Exception) { - if (e is CancellationException) - throw e - Timber.e(e) - tv.post { tv.setText(R.string.download_file_error) } - } - } - } -}