2020-01-13 00:43:09 +08:00
|
|
|
package com.topjohnwu.magisk.ui
|
2019-10-02 19:42:38 +02:00
|
|
|
|
2019-10-03 18:41:04 +02:00
|
|
|
import android.os.Bundle
|
2019-10-18 17:04:41 +02:00
|
|
|
import android.view.MenuItem
|
2020-01-04 13:30:21 +01:00
|
|
|
import android.view.View
|
2019-10-10 17:34:06 +02:00
|
|
|
import android.view.ViewTreeObserver
|
2020-01-12 15:00:49 +08:00
|
|
|
import android.view.WindowManager
|
2019-10-16 16:08:07 +02:00
|
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
2020-01-05 16:05:22 +08:00
|
|
|
import androidx.core.graphics.Insets
|
2020-03-17 17:28:09 +01:00
|
|
|
import androidx.core.view.forEach
|
2019-10-10 17:34:06 +02:00
|
|
|
import androidx.core.view.setPadding
|
2019-10-16 16:08:07 +02:00
|
|
|
import androidx.core.view.updateLayoutParams
|
|
|
|
import com.google.android.material.card.MaterialCardView
|
2020-03-17 17:28:09 +01:00
|
|
|
import com.topjohnwu.magisk.MainDirections
|
2019-10-02 19:42:38 +02:00
|
|
|
import com.topjohnwu.magisk.R
|
2020-01-13 22:01:46 +08:00
|
|
|
import com.topjohnwu.magisk.core.Const
|
2020-02-28 21:09:52 -08:00
|
|
|
import com.topjohnwu.magisk.core.Info
|
2019-10-02 19:42:38 +02:00
|
|
|
import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding
|
2019-10-20 11:14:49 +02:00
|
|
|
import com.topjohnwu.magisk.extensions.startAnimations
|
2020-01-29 01:49:59 +08:00
|
|
|
import com.topjohnwu.magisk.ui.base.BaseUIActivity
|
2020-03-17 17:28:09 +01:00
|
|
|
import com.topjohnwu.magisk.ui.home.HomeFragmentDirections
|
2019-10-20 10:54:55 +02:00
|
|
|
import com.topjohnwu.magisk.utils.HideBottomViewOnScrollBehavior
|
2019-10-16 16:08:07 +02:00
|
|
|
import com.topjohnwu.magisk.utils.HideTopViewOnScrollBehavior
|
2020-01-04 13:30:21 +01:00
|
|
|
import com.topjohnwu.magisk.utils.HideableBehavior
|
2020-02-28 21:09:52 -08:00
|
|
|
import com.topjohnwu.magisk.view.MagiskDialog
|
2019-10-16 15:47:41 +02:00
|
|
|
import com.topjohnwu.superuser.Shell
|
2019-10-02 19:42:38 +02:00
|
|
|
import org.koin.androidx.viewmodel.ext.android.viewModel
|
|
|
|
|
2020-03-17 17:28:09 +01:00
|
|
|
open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>() {
|
2019-10-02 19:42:38 +02:00
|
|
|
|
|
|
|
override val layoutRes = R.layout.activity_main_md2
|
|
|
|
override val viewModel by viewModel<MainViewModel>()
|
2019-10-16 17:27:11 +02:00
|
|
|
override val navHost: Int = R.id.main_nav_host
|
2019-10-16 17:53:35 +02:00
|
|
|
|
2019-10-10 17:34:06 +02:00
|
|
|
//This temporarily fixes unwanted feature of BottomNavigationView - where the view applies
|
|
|
|
//padding on itself given insets are not consumed beforehand. Unfortunately the listener
|
|
|
|
//implementation doesn't favor us against the design library, so on re-create it's often given
|
|
|
|
//upper hand.
|
|
|
|
private val navObserver = ViewTreeObserver.OnGlobalLayoutListener {
|
|
|
|
binding.mainNavigation.setPadding(0)
|
|
|
|
}
|
|
|
|
|
2020-03-17 17:28:09 +01:00
|
|
|
protected var isRoot = true
|
|
|
|
private set
|
|
|
|
|
2019-10-03 18:41:04 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2020-02-28 21:09:52 -08:00
|
|
|
if (Info.env.isUnsupported) {
|
|
|
|
MagiskDialog(this)
|
|
|
|
.applyTitle(R.string.unsupport_magisk_title)
|
|
|
|
.applyMessage(R.string.unsupport_magisk_msg, Const.Version.MIN_VERSION)
|
|
|
|
.applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok }
|
|
|
|
.cancellable(true)
|
|
|
|
.reveal()
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:00:49 +08:00
|
|
|
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
|
|
|
|
2020-03-17 17:28:09 +01:00
|
|
|
navigation?.addOnDestinationChangedListener { controller, destination, arguments ->
|
|
|
|
isRoot = when (destination.id) {
|
|
|
|
R.id.homeFragment,
|
|
|
|
R.id.modulesFragment,
|
|
|
|
R.id.superuserFragment,
|
|
|
|
R.id.logFragment -> true
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
|
|
|
|
setDisplayHomeAsUpEnabled(!isRoot)
|
|
|
|
requestNavigationHidden(!isRoot)
|
|
|
|
|
|
|
|
binding.mainNavigation.menu.forEach {
|
|
|
|
if (it.itemId == destination.id) {
|
|
|
|
it.isChecked = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:41:04 +02:00
|
|
|
setSupportActionBar(binding.mainToolbar)
|
2019-10-03 19:38:57 +02:00
|
|
|
|
2019-10-16 16:08:07 +02:00
|
|
|
binding.mainToolbarWrapper.updateLayoutParams<CoordinatorLayout.LayoutParams> {
|
|
|
|
behavior = HideTopViewOnScrollBehavior<MaterialCardView>()
|
|
|
|
}
|
2019-10-20 10:54:55 +02:00
|
|
|
binding.mainBottomBar.updateLayoutParams<CoordinatorLayout.LayoutParams> {
|
|
|
|
behavior = HideBottomViewOnScrollBehavior<MaterialCardView>()
|
|
|
|
}
|
2019-10-03 19:38:57 +02:00
|
|
|
binding.mainNavigation.setOnNavigationItemSelectedListener {
|
|
|
|
when (it.itemId) {
|
2020-03-17 17:28:09 +01:00
|
|
|
R.id.homeFragment -> MainDirections.actionHomeFragment()
|
|
|
|
R.id.modulesFragment -> MainDirections.actionModuleFragment()
|
|
|
|
R.id.superuserFragment -> MainDirections.actionSuperuserFragment()
|
|
|
|
R.id.logFragment -> MainDirections.actionLogFragment()
|
2019-10-03 19:38:57 +02:00
|
|
|
else -> throw NotImplementedError("Id ${it.itemId} is not defined as selectable")
|
2020-03-17 17:28:09 +01:00
|
|
|
}.navigate()
|
2019-10-03 19:38:57 +02:00
|
|
|
true
|
|
|
|
}
|
2019-11-15 22:16:59 +01:00
|
|
|
binding.mainNavigation.setOnNavigationItemReselectedListener {
|
2020-03-17 17:28:09 +01:00
|
|
|
(currentFragment as? ReselectionTarget)?.onReselected()
|
2019-11-15 22:16:59 +01:00
|
|
|
}
|
2019-10-06 12:20:05 +02:00
|
|
|
|
2019-10-10 17:34:06 +02:00
|
|
|
binding.mainNavigation.viewTreeObserver.addOnGlobalLayoutListener(navObserver)
|
|
|
|
|
2019-10-06 12:20:05 +02:00
|
|
|
if (intent.getBooleanExtra(Const.Key.OPEN_SETTINGS, false)) {
|
2020-03-17 17:28:09 +01:00
|
|
|
HomeFragmentDirections.actionHomeFragmentToSettingsFragment().navigate()
|
2019-10-06 12:20:05 +02:00
|
|
|
}
|
2019-10-24 18:07:36 +02:00
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
2020-03-17 17:28:09 +01:00
|
|
|
if (!isRoot) {
|
2020-01-04 13:30:21 +01:00
|
|
|
requestNavigationHidden()
|
2019-11-21 14:24:14 +01:00
|
|
|
}
|
2019-10-24 18:07:36 +02:00
|
|
|
}
|
2019-10-03 18:41:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-16 15:47:41 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
binding.mainNavigation.menu.apply {
|
|
|
|
val isRoot = Shell.rootAccess()
|
|
|
|
findItem(R.id.modulesFragment)?.isEnabled = isRoot
|
|
|
|
findItem(R.id.superuserFragment)?.isEnabled = isRoot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 17:34:06 +02:00
|
|
|
override fun onDestroy() {
|
|
|
|
binding.mainNavigation.viewTreeObserver.removeOnGlobalLayoutListener(navObserver)
|
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
2019-10-18 17:04:41 +02:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
when (item.itemId) {
|
|
|
|
android.R.id.home -> onBackPressed()
|
|
|
|
else -> return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:42:47 +02:00
|
|
|
override fun peekSystemWindowInsets(insets: Insets) {
|
|
|
|
viewModel.insets.value = insets
|
|
|
|
}
|
|
|
|
|
2020-03-24 15:52:02 +01:00
|
|
|
fun setDisplayHomeAsUpEnabled(isEnabled: Boolean) {
|
2019-10-20 11:14:49 +02:00
|
|
|
binding.mainToolbar.startAnimations()
|
2019-10-03 18:41:04 +02:00
|
|
|
when {
|
|
|
|
isEnabled -> binding.mainToolbar.setNavigationIcon(R.drawable.ic_back_md2)
|
|
|
|
else -> binding.mainToolbar.navigationIcon = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:30:21 +01:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
|
|
|
internal fun requestNavigationHidden(hide: Boolean = true) {
|
|
|
|
val topView = binding.mainToolbarWrapper
|
|
|
|
val bottomView = binding.mainBottomBar
|
|
|
|
|
|
|
|
val topParams = topView.layoutParams as? CoordinatorLayout.LayoutParams
|
|
|
|
val bottomParams = bottomView.layoutParams as? CoordinatorLayout.LayoutParams
|
|
|
|
|
|
|
|
val topBehavior = topParams?.behavior as? HideableBehavior<View>
|
|
|
|
val bottomBehavior = bottomParams?.behavior as? HideableBehavior<View>
|
2019-11-21 14:24:14 +01:00
|
|
|
|
2020-01-04 13:30:21 +01:00
|
|
|
topBehavior?.setHidden(topView, hide = false, lockState = false)
|
|
|
|
bottomBehavior?.setHidden(bottomView, hide, hide)
|
2019-11-14 18:56:03 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 17:31:37 +01:00
|
|
|
fun invalidateToolbar() {
|
|
|
|
//binding.mainToolbar.startAnimations()
|
|
|
|
binding.mainToolbar.invalidate()
|
|
|
|
}
|
|
|
|
|
2020-01-05 16:05:22 +08:00
|
|
|
}
|