From 0f140b408c4673cba002563118ac68895cd5ecf8 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sat, 16 Nov 2019 19:13:40 +0100 Subject: [PATCH] Added installing external modules from storage --- .../model/entity/recycler/ModuleRvItem.kt | 7 ++++ .../events/InstallExternalModuleEvent.kt | 38 +++++++++++++++++++ .../magisk/redesign/module/ModuleFragment.kt | 7 ++++ .../magisk/redesign/module/ModuleViewModel.kt | 8 +++- .../res/drawable/ic_module_storage_md2.xml | 10 +++++ .../main/res/layout/item_module_download.xml | 30 +++++++++++++++ app/src/main/res/values/strings_md2.xml | 1 + 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt create mode 100644 app/src/main/res/drawable/ic_module_storage_md2.xml create mode 100644 app/src/main/res/layout/item_module_download.xml diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt index cfd5a4003..ac8ec6f12 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/ModuleRvItem.kt @@ -81,6 +81,13 @@ class RepoRvItem(val item: Repo) : ComparableRvItem() { override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id } +object InstallModule : ComparableRvItem() { + override val layoutRes = R.layout.item_module_download + + override fun contentSameAs(other: InstallModule) = this == other + override fun itemSameAs(other: InstallModule) = this === other +} + class SectionTitle( val title: Int, val button: Int = 0, diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt new file mode 100644 index 000000000..02d2f3819 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/InstallExternalModuleEvent.kt @@ -0,0 +1,38 @@ +package com.topjohnwu.magisk.model.events + +import android.app.Activity +import android.content.Context +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import com.topjohnwu.magisk.Const +import com.topjohnwu.magisk.base.BaseActivity +import com.topjohnwu.magisk.intent +import com.topjohnwu.magisk.ui.flash.FlashActivity + +class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor { + + override fun invoke(activity: AppCompatActivity) { + activity as BaseActivity<*, *> + activity.withExternalRW { + onSuccess { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "application/zip" + activity.startActivityForResult(intent, Const.ID.FETCH_ZIP) + } + } + } + + companion object { + + fun onActivityResult(context: Context, requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) { + // Get the URI of the selected file + val intent = context.intent(FlashActivity::class.java) + intent.setData(data.data).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP) + context.startActivity(intent) + } + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleFragment.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleFragment.kt index bd7cd6945..09bcdeb44 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleFragment.kt @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.redesign.module +import android.content.Intent import android.graphics.Insets import android.os.Bundle import android.view.Menu @@ -9,6 +10,7 @@ import android.view.View import androidx.recyclerview.widget.StaggeredGridLayoutManager import com.topjohnwu.magisk.R import com.topjohnwu.magisk.databinding.FragmentModuleMd2Binding +import com.topjohnwu.magisk.model.events.InstallExternalModuleEvent import com.topjohnwu.magisk.redesign.MainActivity import com.topjohnwu.magisk.redesign.ReselectionTarget import com.topjohnwu.magisk.redesign.compat.CompatFragment @@ -27,6 +29,11 @@ class ModuleFragment : CompatFragment override fun consumeSystemWindowInsets(insets: Insets) = insets + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + InstallExternalModuleEvent.onActivityResult(requireContext(), requestCode, resultCode, data) + } + override fun onStart() { super.onStart() setHasOptionsMenu(true) diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleViewModel.kt index 934ede1aa..728150575 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/module/ModuleViewModel.kt @@ -15,9 +15,11 @@ import com.topjohnwu.magisk.model.download.RemoteFileService import com.topjohnwu.magisk.model.entity.internal.DownloadSubject import com.topjohnwu.magisk.model.entity.module.Module import com.topjohnwu.magisk.model.entity.module.Repo +import com.topjohnwu.magisk.model.entity.recycler.InstallModule import com.topjohnwu.magisk.model.entity.recycler.ModuleItem import com.topjohnwu.magisk.model.entity.recycler.RepoItem import com.topjohnwu.magisk.model.entity.recycler.SectionTitle +import com.topjohnwu.magisk.model.events.InstallExternalModuleEvent import com.topjohnwu.magisk.model.events.dialog.ModuleInstallDialog import com.topjohnwu.magisk.redesign.compat.CompatViewModel import com.topjohnwu.magisk.redesign.compat.Queryable @@ -219,10 +221,12 @@ class ModuleViewModel( fun download(item: RepoItem) = ModuleInstallDialog(item.item).publish() fun sectionPressed(item: SectionTitle) = when (item) { - sectionActive -> reboot() + sectionActive -> reboot() //TODO add reboot picker, regular reboot is not always preferred else -> Unit } + fun installPressed() = InstallExternalModuleEvent().publish() + // --- /** Callable only from worker thread because of expensive list filtering */ @@ -230,7 +234,7 @@ class ModuleViewModel( private fun build( active: List = itemsInstalled, remote: List = itemsRemote - ) = active.prependIfNotEmpty { sectionActive } + + ) = (active + InstallModule).prependIfNotEmpty { sectionActive } + remote.prependIfNotEmpty { sectionRemote } private fun List.prependIfNotEmpty(item: () -> T) = diff --git a/app/src/main/res/drawable/ic_module_storage_md2.xml b/app/src/main/res/drawable/ic_module_storage_md2.xml new file mode 100644 index 000000000..9505cddde --- /dev/null +++ b/app/src/main/res/drawable/ic_module_storage_md2.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_module_download.xml b/app/src/main/res/layout/item_module_download.xml new file mode 100644 index 000000000..6773a1a93 --- /dev/null +++ b/app/src/main/res/layout/item_module_download.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings_md2.xml b/app/src/main/res/values/strings_md2.xml index a37dd2ed1..1a9fa8ab5 100644 --- a/app/src/main/res/values/strings_md2.xml +++ b/app/src/main/res/values/strings_md2.xml @@ -80,6 +80,7 @@ Remote Remove Restore + Install from storage Toggles logging Toggles “toast” notifications