2020-01-13 00:43:09 +08:00
|
|
|
package com.topjohnwu.magisk.ui
|
2019-10-02 19:42:38 +02:00
|
|
|
|
2020-08-19 05:18:15 -07:00
|
|
|
import android.content.Intent
|
2020-10-30 16:43:11 +08:00
|
|
|
import android.content.pm.ApplicationInfo
|
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
|
2020-01-12 15:00:49 +08:00
|
|
|
import android.view.WindowManager
|
2020-08-21 03:36:12 -07:00
|
|
|
import androidx.core.content.pm.ShortcutManagerCompat
|
2020-03-17 17:28:09 +01:00
|
|
|
import androidx.core.view.forEach
|
2022-01-17 18:13:25 +08:00
|
|
|
import androidx.core.view.isGone
|
2022-01-06 19:30:13 +08:00
|
|
|
import androidx.core.view.isVisible
|
2020-04-11 18:34:10 +02:00
|
|
|
import androidx.navigation.NavDirections
|
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
|
2021-11-05 04:16:58 -07:00
|
|
|
import com.topjohnwu.magisk.arch.BaseMainActivity
|
2020-08-18 06:31:15 -07:00
|
|
|
import com.topjohnwu.magisk.arch.BaseViewModel
|
2020-08-21 03:36:12 -07:00
|
|
|
import com.topjohnwu.magisk.core.*
|
2019-10-02 19:42:38 +02:00
|
|
|
import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding
|
2021-04-18 04:46:11 -07:00
|
|
|
import com.topjohnwu.magisk.di.viewModel
|
2020-07-11 05:36:31 -07:00
|
|
|
import com.topjohnwu.magisk.ktx.startAnimations
|
2020-03-17 17:28:09 +01:00
|
|
|
import com.topjohnwu.magisk.ui.home.HomeFragmentDirections
|
2020-12-01 02:59:11 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Utils
|
2020-02-28 21:09:52 -08:00
|
|
|
import com.topjohnwu.magisk.view.MagiskDialog
|
2020-08-21 03:36:12 -07:00
|
|
|
import com.topjohnwu.magisk.view.Shortcuts
|
2020-10-30 16:43:11 +08:00
|
|
|
import java.io.File
|
2019-10-02 19:42:38 +02:00
|
|
|
|
2020-07-12 03:17:50 -07:00
|
|
|
class MainViewModel : BaseViewModel()
|
|
|
|
|
2021-11-05 04:16:58 -07:00
|
|
|
class MainActivity : BaseMainActivity<MainViewModel, ActivityMainMd2Binding>() {
|
2019-10-02 19:42:38 +02:00
|
|
|
|
|
|
|
override val layoutRes = R.layout.activity_main_md2
|
|
|
|
override val viewModel by viewModel<MainViewModel>()
|
2021-03-16 04:58:02 -07:00
|
|
|
override val navHostId: Int = R.id.main_nav_host
|
2022-01-06 19:30:13 +08:00
|
|
|
override val snackbarAnchorView: View?
|
|
|
|
get() {
|
|
|
|
val fragmentAnchor = currentFragment?.snackbarAnchorView
|
|
|
|
return when {
|
|
|
|
fragmentAnchor?.isVisible == true -> fragmentAnchor
|
|
|
|
binding.mainNavigation.isVisible -> return binding.mainNavigation
|
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 17:53:35 +02:00
|
|
|
|
2020-08-19 03:27:12 -07:00
|
|
|
private var isRootFragment = true
|
2020-03-17 17:28:09 +01:00
|
|
|
|
2021-11-05 04:16:58 -07:00
|
|
|
override fun showMainUI(savedInstanceState: Bundle?) {
|
2020-09-11 02:31:41 -07:00
|
|
|
setContentView()
|
2020-08-21 03:36:12 -07:00
|
|
|
showUnsupportedMessage()
|
|
|
|
askForHomeShortcut()
|
2020-02-28 21:09:52 -08:00
|
|
|
|
2020-01-12 15:00:49 +08:00
|
|
|
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
|
|
|
|
2020-08-19 03:27:12 -07:00
|
|
|
navigation?.addOnDestinationChangedListener { _, destination, _ ->
|
|
|
|
isRootFragment = when (destination.id) {
|
2020-03-17 17:28:09 +01:00
|
|
|
R.id.homeFragment,
|
|
|
|
R.id.modulesFragment,
|
|
|
|
R.id.superuserFragment,
|
|
|
|
R.id.logFragment -> true
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
|
2020-08-19 03:27:12 -07:00
|
|
|
setDisplayHomeAsUpEnabled(!isRootFragment)
|
|
|
|
requestNavigationHidden(!isRootFragment)
|
2020-03-17 17:28:09 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-12 00:39:24 -07:00
|
|
|
binding.mainNavigation.setOnItemSelectedListener {
|
2020-08-19 03:27:12 -07:00
|
|
|
getScreen(it.itemId)?.navigate()
|
2019-10-03 19:38:57 +02:00
|
|
|
true
|
|
|
|
}
|
2021-11-05 04:16:58 -07:00
|
|
|
binding.mainNavigation.menu.apply {
|
|
|
|
findItem(R.id.superuserFragment)?.isEnabled = Utils.showSuperUser()
|
2021-11-12 15:36:29 +08:00
|
|
|
findItem(R.id.modulesFragment)?.isEnabled = Info.env.isActive
|
2021-11-05 04:16:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
val section =
|
|
|
|
if (intent.action == Intent.ACTION_APPLICATION_PREFERENCES)
|
|
|
|
Const.Nav.SETTINGS
|
|
|
|
else
|
|
|
|
intent.getStringExtra(Const.Key.OPEN_SECTION)
|
2019-10-06 12:20:05 +02:00
|
|
|
|
2020-08-19 05:18:15 -07:00
|
|
|
getScreen(section)?.navigate()
|
2020-04-11 18:34:10 +02:00
|
|
|
|
2022-01-17 18:13:25 +08:00
|
|
|
if (!isRootFragment) {
|
|
|
|
requestNavigationHidden(requiresAnimation = savedInstanceState == null)
|
2019-10-24 18:07:36 +02:00
|
|
|
}
|
2019-10-03 18:41:04 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:13:25 +08:00
|
|
|
internal fun requestNavigationHidden(hide: Boolean = true, requiresAnimation: Boolean = true) {
|
2021-09-03 18:38:02 +08:00
|
|
|
val bottomView = binding.mainNavigation
|
2022-01-17 18:13:25 +08:00
|
|
|
if (requiresAnimation) {
|
|
|
|
bottomView.isVisible = true
|
|
|
|
bottomView.isHidden = hide
|
2021-09-03 18:38:02 +08:00
|
|
|
} else {
|
2022-01-17 18:13:25 +08:00
|
|
|
bottomView.isGone = hide
|
2021-09-03 18:38:02 +08:00
|
|
|
}
|
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-04-11 18:34:10 +02:00
|
|
|
private fun getScreen(name: String?): NavDirections? {
|
|
|
|
return when (name) {
|
2021-03-16 04:58:02 -07:00
|
|
|
Const.Nav.SUPERUSER -> MainDirections.actionSuperuserFragment()
|
|
|
|
Const.Nav.MODULES -> MainDirections.actionModuleFragment()
|
2020-08-19 03:27:12 -07:00
|
|
|
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
|
2020-04-11 18:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 03:36:12 -07:00
|
|
|
private fun showUnsupportedMessage() {
|
|
|
|
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 }
|
2020-10-30 16:43:11 +08:00
|
|
|
.cancellable(false)
|
|
|
|
.reveal()
|
|
|
|
}
|
|
|
|
|
2021-02-22 03:05:49 -08:00
|
|
|
if (!Info.isEmulator && Info.env.isActive && System.getenv("PATH")
|
2020-10-30 16:43:11 +08:00
|
|
|
?.split(':')
|
|
|
|
?.filterNot { File("$it/magisk").exists() }
|
|
|
|
?.any { File("$it/su").exists() } == true) {
|
|
|
|
MagiskDialog(this)
|
2021-02-22 03:05:49 -08:00
|
|
|
.applyTitle(R.string.unsupport_general_title)
|
2020-10-30 16:43:11 +08:00
|
|
|
.applyMessage(R.string.unsupport_other_su_msg)
|
|
|
|
.applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok }
|
|
|
|
.cancellable(false)
|
|
|
|
.reveal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) {
|
|
|
|
MagiskDialog(this)
|
2021-02-22 03:05:49 -08:00
|
|
|
.applyTitle(R.string.unsupport_general_title)
|
2020-10-30 16:43:11 +08:00
|
|
|
.applyMessage(R.string.unsupport_system_app_msg)
|
|
|
|
.applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok }
|
|
|
|
.cancellable(false)
|
|
|
|
.reveal()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (applicationInfo.flags and ApplicationInfo.FLAG_EXTERNAL_STORAGE != 0) {
|
|
|
|
MagiskDialog(this)
|
2021-02-22 03:05:49 -08:00
|
|
|
.applyTitle(R.string.unsupport_general_title)
|
2020-10-30 16:43:11 +08:00
|
|
|
.applyMessage(R.string.unsupport_external_storage_msg)
|
|
|
|
.applyButton(MagiskDialog.ButtonType.POSITIVE) { titleRes = android.R.string.ok }
|
|
|
|
.cancellable(false)
|
|
|
|
.reveal()
|
|
|
|
}
|
2021-02-22 03:28:54 -08:00
|
|
|
}
|
2020-08-21 03:36:12 -07:00
|
|
|
|
|
|
|
private fun askForHomeShortcut() {
|
2020-08-22 10:51:32 -07:00
|
|
|
if (isRunningAsStub && !Config.askedHome &&
|
|
|
|
ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
|
2020-08-21 03:36:12 -07:00
|
|
|
// Ask and show dialog
|
|
|
|
Config.askedHome = true
|
|
|
|
MagiskDialog(this)
|
|
|
|
.applyTitle(R.string.add_shortcut_title)
|
|
|
|
.applyMessage(R.string.add_shortcut_msg)
|
|
|
|
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
|
2020-08-28 08:13:04 +08:00
|
|
|
titleRes = android.R.string.cancel
|
2020-08-21 03:36:12 -07:00
|
|
|
}.applyButton(MagiskDialog.ButtonType.POSITIVE) {
|
2020-08-28 08:13:04 +08:00
|
|
|
titleRes = android.R.string.ok
|
2020-08-21 03:36:12 -07:00
|
|
|
onClick {
|
|
|
|
Shortcuts.addHomeIcon(this@MainActivity)
|
|
|
|
}
|
|
|
|
}.cancellable(true)
|
|
|
|
.reveal()
|
|
|
|
}
|
|
|
|
}
|
2020-01-05 16:05:22 +08:00
|
|
|
}
|