Moar refactoring

This commit is contained in:
topjohnwu 2020-08-19 03:27:12 -07:00
parent 1fcf35ebeb
commit 7c319f5fc3
22 changed files with 89 additions and 120 deletions

View File

@ -15,11 +15,7 @@ import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.events.BackPressEvent
import com.topjohnwu.magisk.events.PermissionEvent
import com.topjohnwu.magisk.events.SnackbarEvent
import com.topjohnwu.magisk.events.ViewActionEvent
import com.topjohnwu.magisk.model.navigation.NavigationWrapper
import com.topjohnwu.magisk.events.*
import com.topjohnwu.magisk.utils.ObservableHost
import com.topjohnwu.magisk.utils.set
import kotlinx.coroutines.Job
@ -111,7 +107,7 @@ abstract class BaseViewModel(
}
fun NavDirections.publish() {
_viewEvents.postValue(NavigationWrapper(this))
_viewEvents.postValue(NavigationEvent(this))
}
}

View File

@ -82,4 +82,10 @@ object Const {
const val UNINSTALL = "uninstall"
}
object Nav {
const val SETTINGS = "settings"
const val HIDE = "hide"
const val MODULES = "modules"
const val SUPERUSER = "superuser"
}
}

View File

@ -3,13 +3,13 @@ package com.topjohnwu.magisk.core
import android.content.ContextWrapper
import android.content.Intent
import com.topjohnwu.magisk.core.base.BaseReceiver
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.model.ManagerJson
import com.topjohnwu.magisk.core.su.SuCallbackHandler
import com.topjohnwu.magisk.ktx.reboot
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.view.Shortcuts
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.GlobalScope

View File

@ -2,12 +2,14 @@ package com.topjohnwu.magisk.core
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.ktx.get
import com.topjohnwu.magisk.model.navigation.Navigation
import com.topjohnwu.magisk.ui.MainActivity
import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.magisk.view.Shortcuts
import com.topjohnwu.superuser.Shell
@ -58,11 +60,21 @@ open class SplashActivity : Activity() {
get<GithubRawServices>()
DONE = true
Navigation.start(intent, this)
val section = if (intent.action == ACTION_APPLICATION_PREFERENCES) Const.Nav.SETTINGS
else intent.getStringExtra(Const.Key.OPEN_SECTION)
intent<MainActivity>()
.putExtra(Const.Key.OPEN_SECTION, section)
.also { startActivity(it) }
finish()
}
companion object {
private val ACTION_APPLICATION_PREFERENCES get() =
if (Build.VERSION.SDK_INT >= 24) Intent.ACTION_APPLICATION_PREFERENCES
else "???"
var DONE = false
}

View File

@ -1,4 +1,4 @@
package com.topjohnwu.magisk.model.internal
package com.topjohnwu.magisk.core.download
import android.net.Uri
import android.os.Parcelable

View File

@ -8,15 +8,14 @@ import android.content.Intent
import android.os.Build
import android.webkit.MimeTypeMap
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.download.Configuration.*
import com.topjohnwu.magisk.core.download.Configuration.Flash.Secondary
import com.topjohnwu.magisk.core.download.DownloadSubject.*
import com.topjohnwu.magisk.core.intent
import com.topjohnwu.magisk.core.tasks.EnvFixTask
import com.topjohnwu.magisk.ktx.chooser
import com.topjohnwu.magisk.ktx.exists
import com.topjohnwu.magisk.ktx.provide
import com.topjohnwu.magisk.model.internal.Configuration.*
import com.topjohnwu.magisk.model.internal.Configuration.Flash.Secondary
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.model.internal.DownloadSubject.*
import com.topjohnwu.magisk.ui.flash.FlashFragment
import com.topjohnwu.magisk.utils.APKInstall
import org.koin.core.get

View File

@ -1,4 +1,4 @@
package com.topjohnwu.magisk.model.internal
package com.topjohnwu.magisk.core.download
import android.content.Context
import android.os.Parcelable

View File

@ -6,13 +6,12 @@ import com.topjohnwu.magisk.ProcessPhoenix
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration.APK.Restore
import com.topjohnwu.magisk.core.download.Configuration.APK.Upgrade
import com.topjohnwu.magisk.core.intent
import com.topjohnwu.magisk.core.isRunningAsStub
import com.topjohnwu.magisk.core.utils.PatchAPK
import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.magisk.model.internal.Configuration.APK.Restore
import com.topjohnwu.magisk.model.internal.Configuration.APK.Upgrade
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.superuser.Shell
import java.io.File

View File

@ -6,13 +6,12 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.ForegroundTracker
import com.topjohnwu.magisk.core.download.DownloadSubject.Magisk
import com.topjohnwu.magisk.core.download.DownloadSubject.Module
import com.topjohnwu.magisk.core.utils.ProgressInputStream
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.ktx.checkSum
import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.model.internal.DownloadSubject.Magisk
import com.topjohnwu.magisk.model.internal.DownloadSubject.Module
import com.topjohnwu.magisk.view.Notifications
import kotlinx.coroutines.launch
import okhttp3.ResponseBody

View File

@ -3,10 +3,8 @@ package com.topjohnwu.magisk.events
import android.app.Activity
import android.content.Context
import android.content.Intent
import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.ContextExecutor
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.arch.ViewEventWithScope
import androidx.navigation.NavDirections
import com.topjohnwu.magisk.arch.*
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.view.MarkDownWindow
@ -74,3 +72,13 @@ class RequestFileEvent : ViewEvent(), ActivityExecutor {
?.data
}
}
class NavigationEvent(
private val directions: NavDirections
) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) {
(activity as? BaseUIActivity<*, *>)?.apply {
directions.navigate()
}
}
}

View File

@ -6,9 +6,9 @@ import android.content.Intent
import android.content.IntentFilter
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.download.Configuration.EnvFix
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.model.internal.Configuration.EnvFix
import com.topjohnwu.magisk.model.internal.DownloadSubject.Magisk
import com.topjohnwu.magisk.core.download.DownloadSubject.Magisk
import com.topjohnwu.magisk.view.MagiskDialog
class EnvFixDialog : DialogEvent() {

View File

@ -2,10 +2,10 @@ package com.topjohnwu.magisk.events.dialog
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.ktx.res
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.view.MagiskDialog
import com.topjohnwu.magisk.view.MarkDownWindow
import kotlinx.coroutines.Dispatchers

View File

@ -1,10 +1,10 @@
package com.topjohnwu.magisk.events.dialog
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.view.MagiskDialog
class ModuleInstallDialog(private val item: Repo) : DialogEvent() {

View File

@ -3,9 +3,9 @@ package com.topjohnwu.magisk.events.dialog
import android.widget.Toast
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.view.MagiskDialog
import com.topjohnwu.superuser.Shell

View File

@ -1,30 +0,0 @@
package com.topjohnwu.magisk.model.navigation
import android.content.Context
import android.content.Intent
import android.os.Build
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.intent
import com.topjohnwu.magisk.ui.MainActivity
object Navigation {
fun start(launchIntent: Intent, context: Context) {
context.intent<MainActivity>()
.putExtra(
Const.Key.OPEN_SECTION, launchIntent.getStringExtra(
Const.Key.OPEN_SECTION))
.putExtra(
Const.Key.OPEN_SETTINGS,
launchIntent.action == ACTION_APPLICATION_PREFERENCES
)
.also { context.startActivity(it) }
}
private val ACTION_APPLICATION_PREFERENCES
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent.ACTION_APPLICATION_PREFERENCES
} else {
"cannot be null, cannot be empty"
}
}

View File

@ -1,18 +0,0 @@
package com.topjohnwu.magisk.model.navigation
import androidx.navigation.NavDirections
import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.base.BaseActivity
class NavigationWrapper(
private val directions: NavDirections
) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) {
if (activity !is BaseUIActivity<*, *>) return
activity.apply {
directions.navigate()
}
}
}

View File

@ -44,8 +44,7 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
binding.mainNavigation.setPadding(0)
}
protected var isRoot = true
private set
private var isRootFragment = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -61,8 +60,8 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
navigation?.addOnDestinationChangedListener { controller, destination, arguments ->
isRoot = when (destination.id) {
navigation?.addOnDestinationChangedListener { _, destination, _ ->
isRootFragment = when (destination.id) {
R.id.homeFragment,
R.id.modulesFragment,
R.id.superuserFragment,
@ -70,8 +69,8 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
else -> false
}
setDisplayHomeAsUpEnabled(!isRoot)
requestNavigationHidden(!isRoot)
setDisplayHomeAsUpEnabled(!isRootFragment)
requestNavigationHidden(!isRootFragment)
binding.mainNavigation.menu.forEach {
if (it.itemId == destination.id) {
@ -89,13 +88,7 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
behavior = HideBottomViewOnScrollBehavior<MaterialCardView>()
}
binding.mainNavigation.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.homeFragment -> MainDirections.actionHomeFragment()
R.id.modulesFragment -> MainDirections.actionModuleFragment()
R.id.superuserFragment -> MainDirections.actionSuperuserFragment()
R.id.logFragment -> MainDirections.actionLogFragment()
else -> throw NotImplementedError("Id ${it.itemId} is not defined as selectable")
}.navigate()
getScreen(it.itemId)?.navigate()
true
}
binding.mainNavigation.setOnNavigationItemReselectedListener {
@ -104,16 +97,11 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
binding.mainNavigation.viewTreeObserver.addOnGlobalLayoutListener(navObserver)
when {
intent.hasExtra(Const.Key.OPEN_SECTION) ->
getScreen(intent.getStringExtra(Const.Key.OPEN_SECTION))?.navigate()
intent.getBooleanExtra(Const.Key.OPEN_SETTINGS, false) ->
HomeFragmentDirections.actionHomeFragmentToSettingsFragment().navigate()
}
if (intent.hasExtra(Const.Key.OPEN_SECTION))
getScreen(intent.getStringExtra(Const.Key.OPEN_SECTION))?.navigate()
if (savedInstanceState != null) {
if (!isRoot) {
if (!isRootFragment) {
requestNavigationHidden()
}
}
@ -171,11 +159,21 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
private fun getScreen(name: String?): NavDirections? {
return when (name) {
"superuser" -> HomeFragmentDirections.actionSuperuserFragment()
"magiskhide" -> HomeFragmentDirections.actionHideFragment()
"modules" -> HomeFragmentDirections.actionModuleFragment()
null -> null
else -> TODO("Implement screen shortcut \"$name\"")
Const.Nav.SUPERUSER -> HomeFragmentDirections.actionSuperuserFragment()
Const.Nav.HIDE -> HomeFragmentDirections.actionHideFragment()
Const.Nav.MODULES -> HomeFragmentDirections.actionModuleFragment()
Const.Nav.SETTINGS -> HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
else -> null
}
}
private fun getScreen(id: Int): NavDirections? {
return when (id) {
R.id.homeFragment -> MainDirections.actionHomeFragment()
R.id.modulesFragment -> MainDirections.actionModuleFragment()
R.id.superuserFragment -> MainDirections.actionSuperuserFragment()
R.id.logFragment -> MainDirections.actionLogFragment()
else -> null
}
}

View File

@ -12,6 +12,7 @@ import com.topjohnwu.magisk.arch.itemBindingOf
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.core.download.DownloadSubject.Manager
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.ManagerJson
@ -23,7 +24,6 @@ import com.topjohnwu.magisk.events.dialog.UninstallDialog
import com.topjohnwu.magisk.ktx.await
import com.topjohnwu.magisk.ktx.packageName
import com.topjohnwu.magisk.ktx.res
import com.topjohnwu.magisk.model.internal.DownloadSubject.Manager
import com.topjohnwu.magisk.utils.set
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.launch

View File

@ -8,13 +8,13 @@ import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.events.RequestFileEvent
import com.topjohnwu.magisk.events.dialog.SecondSlotWarningDialog
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.set
import com.topjohnwu.superuser.Shell

View File

@ -8,6 +8,7 @@ import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.*
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.model.module.Module
import com.topjohnwu.magisk.core.tasks.RepoUpdater
@ -19,7 +20,6 @@ import com.topjohnwu.magisk.events.OpenChangelogEvent
import com.topjohnwu.magisk.events.dialog.ModuleInstallDialog
import com.topjohnwu.magisk.ktx.addOnListChangedCallback
import com.topjohnwu.magisk.ktx.reboot
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.utils.EndlessRecyclerScrollListener
import com.topjohnwu.magisk.utils.set
import kotlinx.coroutines.Dispatchers

View File

@ -12,13 +12,13 @@ import com.topjohnwu.magisk.arch.diffListOf
import com.topjohnwu.magisk.arch.itemBindingOf
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Configuration
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.DownloadSubject
import com.topjohnwu.magisk.core.utils.PatchAPK
import com.topjohnwu.magisk.data.database.RepoDao
import com.topjohnwu.magisk.events.RecreateEvent
import com.topjohnwu.magisk.events.dialog.BiometricDialog
import com.topjohnwu.magisk.model.internal.Configuration
import com.topjohnwu.magisk.model.internal.DownloadSubject
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.launch

View File

@ -42,11 +42,11 @@ object Shortcuts {
if (Utils.showSuperUser()) {
shortCuts.add(
ShortcutInfo.Builder(context, "superuser")
ShortcutInfo.Builder(context, Const.Nav.SUPERUSER)
.setShortLabel(context.getString(R.string.superuser))
.setIntent(
Intent(intent)
.putExtra(Const.Key.OPEN_SECTION, "superuser")
.putExtra(Const.Key.OPEN_SECTION, Const.Nav.SUPERUSER)
.setAction(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
)
@ -57,11 +57,11 @@ object Shortcuts {
}
if (Info.env.magiskHide) {
shortCuts.add(
ShortcutInfo.Builder(context, "magiskhide")
ShortcutInfo.Builder(context, Const.Nav.HIDE)
.setShortLabel(context.getString(R.string.magiskhide))
.setIntent(
Intent(intent)
.putExtra(Const.Key.OPEN_SECTION, "magiskhide")
.putExtra(Const.Key.OPEN_SECTION, Const.Nav.HIDE)
.setAction(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
)
@ -72,11 +72,11 @@ object Shortcuts {
}
if (Info.env.isActive) {
shortCuts.add(
ShortcutInfo.Builder(context, "modules")
ShortcutInfo.Builder(context, Const.Nav.MODULES)
.setShortLabel(context.getString(R.string.modules))
.setIntent(
Intent(intent)
.putExtra(Const.Key.OPEN_SECTION, "modules")
.putExtra(Const.Key.OPEN_SECTION, Const.Nav.MODULES)
.setAction(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
)