VancedManager/app/src/main/java/com/vanced/manager/ui/MainActivity.kt

208 lines
7.3 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui
2020-03-16 15:41:57 +00:00
2021-02-04 07:55:12 +00:00
import android.content.ActivityNotFoundException
2021-02-04 08:34:40 +00:00
import android.content.ComponentName
import android.content.Context
2020-09-20 14:41:28 +00:00
import android.content.Intent
import android.content.res.Configuration
2021-02-04 07:55:12 +00:00
import android.net.Uri
2020-03-16 15:41:57 +00:00
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
2020-11-23 16:00:41 +00:00
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
2020-08-06 07:46:07 +00:00
import com.google.firebase.messaging.FirebaseMessaging
2020-12-19 08:35:07 +00:00
import com.vanced.manager.BuildConfig.VERSION_CODE
import com.vanced.manager.R
2020-06-04 07:35:26 +00:00
import com.vanced.manager.databinding.ActivityMainBinding
import com.vanced.manager.ui.dialogs.DialogContainer
2020-10-31 19:45:39 +00:00
import com.vanced.manager.ui.dialogs.ManagerUpdateDialog
2020-11-02 14:41:39 +00:00
import com.vanced.manager.ui.dialogs.URLChangeDialog
import com.vanced.manager.ui.fragments.HomeFragmentDirections
import com.vanced.manager.ui.fragments.SettingsFragmentDirections
2021-01-16 17:00:38 +00:00
import com.vanced.manager.utils.*
import com.vanced.manager.utils.AppUtils.currentLocale
2021-02-04 08:34:40 +00:00
import com.vanced.manager.utils.AppUtils.faqpkg
2021-02-03 18:24:28 +00:00
import com.vanced.manager.utils.AppUtils.log
2021-02-04 15:30:52 +00:00
import com.vanced.manager.utils.AppUtils.playStorePkg
2021-02-28 10:17:46 +00:00
import com.vanced.manager.utils.AppUtils.vancedRootPkg
2021-02-04 08:34:40 +00:00
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
2020-03-16 15:41:57 +00:00
class MainActivity : AppCompatActivity() {
2020-03-16 15:41:57 +00:00
2020-09-20 14:41:28 +00:00
lateinit var binding: ActivityMainBinding
private val navHost by lazy { findNavController(R.id.nav_host) }
2020-09-18 17:55:45 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
setFinalTheme()
super.onCreate(savedInstanceState)
2020-11-03 07:28:18 +00:00
2020-11-23 20:33:24 +00:00
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
2020-09-16 05:18:07 +00:00
with(binding) {
2020-10-31 19:45:39 +00:00
setSupportActionBar(toolbar)
2020-11-23 20:33:24 +00:00
toolbar.setupWithNavController(
this@MainActivity.navHost,
AppBarConfiguration(this@MainActivity.navHost.graph)
)
}
navHost.addOnDestinationChangedListener { _, currFrag: NavDestination, _ ->
setDisplayHomeAsUpEnabled(currFrag.id != R.id.home_fragment)
}
2020-10-31 19:45:39 +00:00
initDialogs(intent.getBooleanExtra("firstLaunch", false))
manager.observe(this) {
if (manager.value?.int("versionCode") ?: 0 > VERSION_CODE) {
2021-02-28 10:17:46 +00:00
ManagerUpdateDialog.newInstance(true).show(this)
}
}
}
override fun onBackPressed() {
if (!navHost.popBackStack())
finish()
}
private fun setDisplayHomeAsUpEnabled(isNeeded: Boolean) {
2021-04-19 15:57:14 +00:00
binding.toolbar.navigationIcon = if (isNeeded) ContextCompat.getDrawable(
this,
R.drawable.ic_keyboard_backspace_black_24dp
) else null
}
2020-05-27 16:37:03 +00:00
override fun onResume() {
setFinalTheme()
2020-05-27 16:37:03 +00:00
super.onResume()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
2021-02-03 18:24:28 +00:00
return when (item.itemId) {
2020-08-06 07:46:07 +00:00
R.id.toolbar_about -> {
navHost.navigate(HomeFragmentDirections.toAboutFragment())
2021-02-03 18:24:28 +00:00
true
2020-08-06 07:46:07 +00:00
}
R.id.toolbar_settings -> {
navHost.navigate(HomeFragmentDirections.toSettingsFragment())
2021-02-03 18:24:28 +00:00
true
}
R.id.toolbar_log -> {
navHost.navigate(HomeFragmentDirections.toLogFragment())
true
2020-08-06 07:46:07 +00:00
}
2021-02-05 10:17:15 +00:00
R.id.toolbar_guide -> {
2021-02-04 07:55:12 +00:00
try {
2021-02-04 08:34:40 +00:00
val intent = if (isPackageInstalled(faqpkg, packageManager)) {
Intent().apply {
component = ComponentName(faqpkg, "$faqpkg.ui.MainActivity")
}
} else {
2021-02-04 07:55:12 +00:00
Intent(Intent.ACTION_VIEW).apply {
2021-02-04 15:30:52 +00:00
val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", faqpkg)
.appendQueryParameter("launch", "true")
data = uriBuilder.build()
setPackage(playStorePkg)
2021-02-04 07:55:12 +00:00
}
2021-02-04 08:34:40 +00:00
}
startActivity(intent)
2021-02-04 07:55:12 +00:00
true
} catch (e: ActivityNotFoundException) {
false
}
}
2020-10-31 19:45:39 +00:00
R.id.toolbar_update_manager -> {
2021-04-19 15:57:14 +00:00
ManagerUpdateDialog.newInstance(false)
.show(supportFragmentManager, "manager_update")
2021-02-03 18:24:28 +00:00
true
2020-10-31 19:45:39 +00:00
}
2020-08-06 07:46:07 +00:00
R.id.dev_settings -> {
navHost.navigate(SettingsFragmentDirections.toDevSettingsFragment())
return true
2020-08-06 07:46:07 +00:00
}
else -> super.onOptionsItemSelected(item)
}
}
2020-05-14 06:27:07 +00:00
2020-07-06 11:38:00 +00:00
override fun attachBaseContext(newBase: Context) {
2021-10-08 11:19:15 +00:00
super.attachBaseContext(LanguageContextWrapper.wrap(newBase))
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
//update manager language when system language is changed
@Suppress("DEPRECATION")
if (newConfig.locale != currentLocale) {
recreate() //restarting activity in order to recreate viewmodels, otherwise some text won't update
return
}
when (newConfig.orientation) {
2021-04-19 15:57:14 +00:00
Configuration.ORIENTATION_PORTRAIT -> log(
"VMUI",
"screen orientation changed to portrait"
)
Configuration.ORIENTATION_LANDSCAPE -> log(
"VMUI",
"screen orientation changed to landscape"
)
else -> log("VMUI", "screen orientation changed to [REDACTED]")
}
}
override fun recreate() {
//needed for setting language smh
startActivity(Intent(this, this::class.java))
finish()
}
2020-10-31 19:45:39 +00:00
private fun initDialogs(firstLaunch: Boolean) {
2020-08-26 12:29:05 +00:00
val prefs = getDefaultSharedPreferences(this)
2021-01-24 18:17:19 +00:00
val variant = prefs.managerVariant
prefs.getBoolean("show_root_dialog", true)
2020-11-10 18:16:29 +00:00
if (intent?.data != null && intent.dataString?.startsWith("https") == true) {
2020-11-02 14:41:39 +00:00
val urldialog = URLChangeDialog()
val arg = Bundle()
arg.putString("url", intent.dataString)
urldialog.arguments = arg
urldialog.show(this)
}
2021-02-16 11:00:36 +00:00
if (firstLaunch) {
DialogContainer.showSecurityDialog(this)
with(FirebaseMessaging.getInstance()) {
subscribeToTopic("Vanced-Update")
subscribeToTopic("Music-Update")
subscribeToTopic("MicroG-Update")
}
} else {
if (isMiuiOptimizationsEnabled) {
2021-02-28 10:17:46 +00:00
DialogContainer.miuiDialog(this)
}
2021-02-16 11:00:36 +00:00
}
if (!prefs.getBoolean("statement", true)) {
DialogContainer.statementFalse(this)
}
if (variant == "root") {
2021-02-28 10:17:46 +00:00
if (PackageHelper.getPackageVersionName(vancedRootPkg, packageManager) == "14.21.54") {
2021-02-16 11:00:36 +00:00
DialogContainer.basicDialog(
getString(R.string.hold_on),
getString(R.string.magisk_vanced),
this
)
}
}
}
2020-03-16 15:41:57 +00:00
}