2019-10-02 19:42:38 +02:00
|
|
|
package com.topjohnwu.magisk.redesign
|
|
|
|
|
2019-10-03 16:42:47 +02:00
|
|
|
import android.graphics.Insets
|
2019-10-03 18:41:04 +02:00
|
|
|
import android.os.Bundle
|
2019-10-18 17:04:41 +02:00
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.ViewGroup
|
2019-10-10 17:34:06 +02:00
|
|
|
import android.view.ViewTreeObserver
|
2019-10-16 16:08:07 +02:00
|
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
2019-10-18 17:04:41 +02:00
|
|
|
import androidx.core.view.isVisible
|
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
|
2019-10-03 17:31:45 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-10-18 17:04:41 +02:00
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
2019-10-16 16:08:07 +02:00
|
|
|
import com.google.android.material.card.MaterialCardView
|
2019-10-03 18:41:04 +02:00
|
|
|
import com.ncapdevi.fragnav.FragNavController
|
2019-10-06 12:20:05 +02:00
|
|
|
import com.topjohnwu.magisk.Const
|
2019-10-02 19:42:38 +02:00
|
|
|
import com.topjohnwu.magisk.R
|
|
|
|
import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding
|
2019-10-20 11:14:49 +02:00
|
|
|
import com.topjohnwu.magisk.extensions.startAnimations
|
2019-10-03 19:38:57 +02:00
|
|
|
import com.topjohnwu.magisk.model.navigation.Navigation
|
2019-10-03 16:42:47 +02:00
|
|
|
import com.topjohnwu.magisk.redesign.compat.CompatActivity
|
2019-10-16 17:53:35 +02:00
|
|
|
import com.topjohnwu.magisk.redesign.compat.CompatNavigationDelegate
|
2019-10-03 17:31:45 +02:00
|
|
|
import com.topjohnwu.magisk.redesign.home.HomeFragment
|
2019-10-17 18:57:00 +02:00
|
|
|
import com.topjohnwu.magisk.redesign.log.LogFragment
|
|
|
|
import com.topjohnwu.magisk.redesign.module.ModuleFragment
|
|
|
|
import com.topjohnwu.magisk.redesign.settings.SettingsFragment
|
|
|
|
import com.topjohnwu.magisk.redesign.superuser.SuperuserFragment
|
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
|
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
|
2019-10-17 18:57:00 +02:00
|
|
|
import kotlin.reflect.KClass
|
2019-10-02 19:42:38 +02:00
|
|
|
|
2019-10-16 17:53:35 +02:00
|
|
|
open class MainActivity : CompatActivity<MainViewModel, ActivityMainMd2Binding>(),
|
|
|
|
FragNavController.TransactionListener {
|
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
|
|
|
|
|
|
|
override val navigation by lazy { CompatNavigationDelegate(this, this) }
|
2019-10-03 17:31:45 +02:00
|
|
|
|
2019-10-17 18:57:00 +02:00
|
|
|
override val baseFragments: List<KClass<out Fragment>> = listOf(
|
2019-10-03 19:38:57 +02:00
|
|
|
HomeFragment::class,
|
2019-10-17 18:57:00 +02:00
|
|
|
ModuleFragment::class,
|
2019-10-03 19:38:57 +02:00
|
|
|
SuperuserFragment::class,
|
|
|
|
LogFragment::class,
|
|
|
|
SettingsFragment::class
|
2019-10-03 17:31:45 +02:00
|
|
|
)
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:41:04 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
R.id.homeFragment -> Navigation.home()
|
|
|
|
R.id.modulesFragment -> Navigation.modules()
|
|
|
|
R.id.superuserFragment -> Navigation.superuser()
|
|
|
|
R.id.logFragment -> Navigation.log()
|
|
|
|
R.id.settingsFragment -> Navigation.settings()
|
|
|
|
else -> throw NotImplementedError("Id ${it.itemId} is not defined as selectable")
|
2019-10-06 12:20:05 +02:00
|
|
|
}.dispatchOnSelf()
|
2019-10-03 19:38:57 +02:00
|
|
|
true
|
|
|
|
}
|
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)) {
|
|
|
|
binding.mainNavigation.selectedItemId = R.id.settingsFragment
|
|
|
|
}
|
2019-10-24 18:07:36 +02:00
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
onTabTransaction(null, -1)
|
|
|
|
onFragmentTransaction(null, FragNavController.TransactionType.PUSH)
|
|
|
|
}
|
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
|
|
|
|
findItem(R.id.logFragment)?.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 18:41:04 +02:00
|
|
|
override fun onTabTransaction(fragment: Fragment?, index: Int) {
|
|
|
|
setDisplayHomeAsUpEnabled(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFragmentTransaction(
|
|
|
|
fragment: Fragment?,
|
|
|
|
transactionType: FragNavController.TransactionType
|
2019-10-18 17:04:41 +02:00
|
|
|
) {
|
|
|
|
setDisplayHomeAsUpEnabled(!navigation.isRoot)
|
|
|
|
|
|
|
|
val lapam = binding.mainBottomBar.layoutParams as ViewGroup.MarginLayoutParams
|
|
|
|
val height = binding.mainBottomBar.measuredHeight
|
|
|
|
val verticalMargin = lapam.let { it.topMargin + it.bottomMargin }
|
|
|
|
val maxTranslation = height + verticalMargin
|
|
|
|
val translation = if (navigation.isRoot) 0 else maxTranslation
|
|
|
|
|
|
|
|
binding.mainBottomBar.animate()
|
|
|
|
.translationY(translation.toFloat())
|
|
|
|
.setInterpolator(FastOutSlowInInterpolator())
|
2019-10-24 18:07:36 +02:00
|
|
|
.withStartAction { if (navigation.isRoot) binding.mainBottomBar.isVisible = true }
|
|
|
|
.withEndAction { if (!navigation.isRoot) binding.mainBottomBar.isVisible = false }
|
2019-10-18 17:04:41 +02:00
|
|
|
.start()
|
2019-10-03 18:41:04 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:42:47 +02:00
|
|
|
override fun peekSystemWindowInsets(insets: Insets) {
|
|
|
|
viewModel.insets.value = insets
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:14:49 +02:00
|
|
|
private fun setDisplayHomeAsUpEnabled(isEnabled: Boolean) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 19:42:38 +02:00
|
|
|
}
|