2020-04-03 16:40:54 +00:00
|
|
|
package com.vanced.manager.ui
|
2020-03-16 15:41:57 +00:00
|
|
|
|
2020-05-27 16:37:03 +00:00
|
|
|
import android.content.*
|
2020-03-16 15:41:57 +00:00
|
|
|
import android.os.Bundle
|
2020-04-05 18:07:26 +00:00
|
|
|
import android.view.MenuItem
|
2020-05-27 16:37:03 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-05-05 19:56:43 +00:00
|
|
|
import androidx.appcompat.widget.Toolbar
|
2020-05-27 16:37:03 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
2020-04-19 17:21:40 +00:00
|
|
|
import androidx.navigation.NavDestination
|
2020-04-14 20:54:42 +00:00
|
|
|
import androidx.navigation.findNavController
|
2020-04-22 09:41:18 +00:00
|
|
|
import androidx.navigation.ui.AppBarConfiguration
|
2020-04-14 20:54:42 +00:00
|
|
|
import androidx.navigation.ui.setupWithNavController
|
2020-05-19 20:06:05 +00:00
|
|
|
import androidx.preference.PreferenceManager
|
2020-04-03 16:40:54 +00:00
|
|
|
import com.vanced.manager.R
|
2020-04-25 17:40:24 +00:00
|
|
|
import com.vanced.manager.core.Main
|
2020-03-16 15:41:57 +00:00
|
|
|
|
2020-04-25 17:40:24 +00:00
|
|
|
class MainActivity : Main() {
|
2020-03-16 15:41:57 +00:00
|
|
|
|
2020-04-19 17:21:40 +00:00
|
|
|
private var isParent = true
|
|
|
|
|
2020-03-16 15:41:57 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
2020-05-02 13:05:19 +00:00
|
|
|
|
2020-04-10 12:45:09 +00:00
|
|
|
super.onCreate(savedInstanceState)
|
2020-03-16 15:41:57 +00:00
|
|
|
setContentView(R.layout.activity_main)
|
2020-03-18 18:10:54 +00:00
|
|
|
|
2020-05-05 19:56:43 +00:00
|
|
|
val toolbar: Toolbar = findViewById(R.id.home_toolbar)
|
2020-04-05 18:07:26 +00:00
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
|
2020-05-14 06:27:07 +00:00
|
|
|
val navHost = findNavController(R.id.bottom_nav_host)
|
2020-04-22 09:41:18 +00:00
|
|
|
val appBarConfiguration = AppBarConfiguration(navHost.graph)
|
|
|
|
toolbar.setupWithNavController(navHost, appBarConfiguration)
|
2020-04-27 21:18:19 +00:00
|
|
|
|
2020-05-19 20:39:03 +00:00
|
|
|
navHost.addOnDestinationChangedListener{_, currFrag: NavDestination, _ ->
|
|
|
|
isParent = when (currFrag.id) {
|
2020-04-27 21:18:19 +00:00
|
|
|
R.id.home_fragment -> true
|
2020-04-19 17:21:40 +00:00
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
|
|
|
|
setDisplayHomeAsUpEnabled(!isParent)
|
|
|
|
|
|
|
|
}
|
2020-05-27 17:06:24 +00:00
|
|
|
|
2020-05-27 17:43:21 +00:00
|
|
|
registerReceivers()
|
2020-05-27 16:37:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
2020-05-28 07:19:46 +00:00
|
|
|
when {
|
|
|
|
intent.action.equals(INSTALL_COMPLETED) -> launchVanced()
|
|
|
|
else -> intent.action?.let { alertBuilder(it) }
|
2020-05-27 16:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
|
|
|
|
}
|
2020-04-19 17:21:40 +00:00
|
|
|
|
2020-05-27 16:37:03 +00:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2020-05-27 17:43:21 +00:00
|
|
|
registerReceivers()
|
2020-04-05 18:07:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 17:21:40 +00:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
2020-04-24 16:10:29 +00:00
|
|
|
val navHost = findNavController(R.id.bottom_nav_host)
|
2020-05-19 20:06:05 +00:00
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
|
2020-05-19 20:39:03 +00:00
|
|
|
val devSettings = prefs.getBoolean("devSettings", false)
|
2020-04-19 17:21:40 +00:00
|
|
|
when (item.itemId) {
|
|
|
|
android.R.id.home -> {
|
2020-04-24 16:10:29 +00:00
|
|
|
onBackPressed()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.toolbar_about -> {
|
|
|
|
navHost.navigate(R.id.toAboutFragment)
|
|
|
|
return true
|
|
|
|
}
|
2020-04-27 21:18:19 +00:00
|
|
|
R.id.toolbar_settings -> {
|
|
|
|
navHost.navigate(R.id.action_settingsFragment)
|
|
|
|
return true
|
|
|
|
}
|
2020-04-24 16:10:29 +00:00
|
|
|
R.id.secret_settings -> {
|
|
|
|
navHost.navigate(R.id.toSecretSettingsFragment)
|
|
|
|
return true
|
2020-04-19 17:21:40 +00:00
|
|
|
}
|
2020-05-03 11:14:09 +00:00
|
|
|
R.id.dev_settings -> {
|
2020-05-19 20:39:03 +00:00
|
|
|
return if (devSettings) {
|
2020-05-19 20:06:05 +00:00
|
|
|
navHost.navigate(R.id.toDevSettingsFragment)
|
2020-05-19 20:39:03 +00:00
|
|
|
true
|
|
|
|
} else
|
|
|
|
false
|
2020-05-19 20:06:05 +00:00
|
|
|
|
2020-05-03 11:14:09 +00:00
|
|
|
}
|
2020-04-19 17:21:40 +00:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
2020-04-05 18:07:26 +00:00
|
|
|
}
|
2020-04-24 16:10:29 +00:00
|
|
|
return false
|
2020-04-19 17:21:40 +00:00
|
|
|
}
|
2020-05-14 06:27:07 +00:00
|
|
|
|
2020-04-19 17:21:40 +00:00
|
|
|
private fun setDisplayHomeAsUpEnabled(isNeeded: Boolean) {
|
2020-05-05 19:56:43 +00:00
|
|
|
val toolbar: Toolbar = findViewById(R.id.home_toolbar)
|
2020-04-19 17:21:40 +00:00
|
|
|
when {
|
|
|
|
isNeeded -> toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp)
|
|
|
|
else -> toolbar.navigationIcon = null
|
2020-04-05 18:07:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 16:37:03 +00:00
|
|
|
|
|
|
|
private fun alertBuilder(msg: String) {
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setTitle("Error")
|
|
|
|
.setMessage("Operation failed because $msg")
|
|
|
|
.setPositiveButton(getString(R.string.close)) { dialog, _ ->
|
|
|
|
run {
|
|
|
|
dialog.dismiss()
|
|
|
|
recreate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun launchVanced() {
|
|
|
|
val intent = Intent()
|
2020-05-28 07:19:46 +00:00
|
|
|
intent.component = ComponentName("com.vanced.android.youtube", "com.google.android.youtube.HomeActivity")
|
2020-05-27 16:37:03 +00:00
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setTitle("Success!")
|
|
|
|
.setMessage("Vanced has been successfully installed, do you want to launch it now?")
|
|
|
|
.setPositiveButton("Launch") {
|
|
|
|
_, _ -> startActivity(intent)
|
|
|
|
}
|
|
|
|
.setNegativeButton("Cancel") {
|
|
|
|
dialog, _ ->
|
|
|
|
run {
|
|
|
|
dialog.dismiss()
|
|
|
|
recreate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:43:21 +00:00
|
|
|
private fun registerReceivers() {
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_COMPLETED
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_ABORTED
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_BLOCKED
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_STORAGE
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_CONFLICT
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_FAILED
|
|
|
|
))
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(
|
|
|
|
INSTALL_INVALID
|
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:37:03 +00:00
|
|
|
companion object {
|
|
|
|
const val INSTALL_COMPLETED = "Installation completed"
|
|
|
|
const val INSTALL_ABORTED = "user aborted installation"
|
|
|
|
const val INSTALL_BLOCKED = "user blocked installation"
|
|
|
|
const val INSTALL_STORAGE = "there was an error with storage.\n Hold up how is that even possible?"
|
|
|
|
const val INSTALL_CONFLICT = "app conflicts with already installed app"
|
|
|
|
const val INSTALL_FAILED = "it just failed idk"
|
|
|
|
const val INSTALL_INVALID = "apk files are invalid"
|
|
|
|
|
|
|
|
}
|
2020-03-16 15:41:57 +00:00
|
|
|
}
|