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

387 lines
17 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui.fragments
2020-03-18 18:10:54 +00:00
2020-05-10 13:07:00 +00:00
import android.animation.ObjectAnimator
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
2020-06-09 09:31:00 +00:00
import android.content.res.ColorStateList
import android.graphics.Color
2020-06-01 16:38:31 +00:00
import android.os.Build
2020-03-18 18:10:54 +00:00
import android.os.Bundle
2020-06-15 16:56:56 +00:00
import android.util.Log
import android.view.*
import android.widget.ProgressBar
2020-06-20 15:11:24 +00:00
import android.widget.TextView
2020-06-19 18:20:41 +00:00
import android.widget.Toast
2020-05-10 14:01:38 +00:00
import androidx.core.animation.addListener
2020-06-04 07:35:26 +00:00
import androidx.databinding.DataBindingUtil
2020-06-04 08:39:04 +00:00
import androidx.fragment.app.viewModels
import androidx.localbroadcastmanager.content.LocalBroadcastManager
2020-05-30 13:49:03 +00:00
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
import androidx.viewpager2.widget.ViewPager2
2020-05-20 14:33:53 +00:00
import com.dezlum.codelabs.getjson.GetJson
2020-05-24 15:06:20 +00:00
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
2020-05-25 11:29:07 +00:00
import com.google.android.material.button.MaterialButton
2020-04-22 09:41:18 +00:00
import com.google.android.material.card.MaterialCardView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.vanced.manager.R
import com.vanced.manager.adapter.SectionPageAdapter
2020-06-02 12:19:04 +00:00
import com.vanced.manager.adapter.SectionPageRootAdapter
import com.vanced.manager.core.fragments.Home
import com.vanced.manager.core.installer.RootAppUninstaller
2020-06-04 07:35:26 +00:00
import com.vanced.manager.databinding.FragmentHomeBinding
2020-06-04 08:39:04 +00:00
import com.vanced.manager.ui.viewmodels.HomeViewModel
2020-06-22 12:11:57 +00:00
import com.vanced.manager.utils.PackageHelper.installApp
2020-06-19 15:48:14 +00:00
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
2020-05-24 15:06:20 +00:00
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
2020-03-18 18:10:54 +00:00
class HomeFragment : Home() {
2020-03-18 18:10:54 +00:00
private lateinit var sectionPageAdapter: SectionPageAdapter
2020-06-02 12:19:04 +00:00
private lateinit var sectionPageRootAdapter: SectionPageRootAdapter
private lateinit var viewPager: ViewPager2
2020-06-04 07:35:26 +00:00
private lateinit var binding: FragmentHomeBinding
2020-05-24 15:06:20 +00:00
private var disposable: Disposable? = null
2020-03-18 18:10:54 +00:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2020-04-22 09:41:18 +00:00
activity?.title = getString(R.string.title_home)
setHasOptionsMenu(true)
2020-06-04 07:35:26 +00:00
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2020-04-22 18:00:00 +00:00
super.onViewCreated(view, savedInstanceState)
2020-05-24 19:21:12 +00:00
initNetworkFun()
2020-06-04 09:06:02 +00:00
val viewModel: HomeViewModel by viewModels()
binding.viewModel = viewModel
2020-05-24 15:06:20 +00:00
2020-06-20 15:11:24 +00:00
val variantPref = getDefaultSharedPreferences(activity).getString("vanced_variant", "nonroot")
registerReceivers()
2020-06-04 17:12:50 +00:00
2020-06-20 15:11:38 +00:00
if (variantPref == "root") {
2020-06-02 12:19:04 +00:00
attachRootChangelog()
} else
2020-06-02 12:19:04 +00:00
attachNonrootChangelog()
2020-03-18 18:10:54 +00:00
}
override fun onPause() {
super.onPause()
activity?.let { LocalBroadcastManager.getInstance(it).unregisterReceiver(broadcastReceiver) }
}
2020-05-24 19:21:12 +00:00
private fun initNetworkFun() {
2020-05-24 18:57:11 +00:00
val pm = activity?.packageManager
2020-06-20 15:11:24 +00:00
val variant = getDefaultSharedPreferences(activity).getString("vanced_variant", "nonroot")
2020-05-24 18:57:11 +00:00
val microgStatus = pm?.let { isPackageInstalled("com.mgoogle.android.gms", it) }
val vancedStatus =
2020-06-20 16:30:44 +00:00
if (variant == "root") {
2020-06-05 13:00:08 +00:00
pm?.let { isPackageInstalled("com.google.android.youtube", it) }
} else {
pm?.let { isPackageInstalled("com.vanced.android.youtube", it) }
}
2020-05-25 11:29:07 +00:00
val vancedinstallbtn = view?.findViewById<MaterialButton>(R.id.vanced_installbtn)
2020-05-24 15:15:17 +00:00
val networkErrorLayout = view?.findViewById<MaterialCardView>(R.id.home_network_wrapper)
2020-06-19 18:20:41 +00:00
val viewModel: HomeViewModel by viewModels()
2020-05-24 18:57:11 +00:00
2020-05-24 15:06:20 +00:00
disposable = ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { isConnectedToInternet ->
2020-05-24 15:25:11 +00:00
run {
if (isConnectedToInternet) {
2020-05-24 16:08:10 +00:00
vancedinstallbtn?.visibility = View.VISIBLE
2020-06-04 17:12:50 +00:00
val vancedRemoteCode =
GetJson().AsJSONObject("https://vanced.app/api/v1/vanced.json")
.get("versionCode").asInt
2020-06-01 16:38:31 +00:00
val microgRemoteCode =
2020-06-04 17:12:50 +00:00
GetJson().AsJSONObject("https://vanced.app/api/v1/microg.json")
2020-06-01 16:38:31 +00:00
.get("versionCode").asInt
2020-06-20 15:11:24 +00:00
if (variant == "nonroot") {
2020-05-30 17:00:45 +00:00
val microginstallbtn =
view?.findViewById<MaterialButton>(R.id.microg_installbtn)
microginstallbtn?.visibility = View.VISIBLE
if (microgStatus!!) {
2020-06-15 16:56:56 +00:00
@Suppress("DEPRECATION")
2020-06-01 16:38:31 +00:00
val microgVerCode =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
pm.getPackageInfo("com.mgoogle.android.gms", 0).longVersionCode.and(0xFFFFFFFF).toInt()
else
pm.getPackageInfo("com.mgoogle.android.gms", 0).versionCode
when {
microgRemoteCode > microgVerCode -> {
microginstallbtn?.text =
2020-06-01 17:46:20 +00:00
activity?.getString(R.string.update)
2020-06-01 16:38:31 +00:00
microginstallbtn?.icon =
2020-06-01 17:46:20 +00:00
activity?.getDrawable(R.drawable.ic_cloud_upload_black_24dp)
2020-06-01 16:38:31 +00:00
}
microgRemoteCode == microgVerCode -> {
microginstallbtn?.text =
2020-06-01 17:46:20 +00:00
activity?.getString(R.string.button_installed)
2020-06-01 16:38:31 +00:00
microginstallbtn?.icon =
2020-06-01 17:46:20 +00:00
activity?.getDrawable(R.drawable.outline_cloud_done_24)
2020-06-01 16:38:31 +00:00
}
2020-05-30 17:00:45 +00:00
}
} else {
2020-06-19 18:20:41 +00:00
disableVancedButton(getString(R.string.no_microg))
2020-05-24 18:57:11 +00:00
}
2020-05-26 13:21:30 +00:00
}
2020-05-27 09:38:46 +00:00
2020-05-26 13:21:30 +00:00
if (vancedStatus!!) {
2020-06-02 17:41:50 +00:00
val vanPkgName =
2020-06-20 15:11:24 +00:00
if (variant == "root")
2020-06-02 17:41:50 +00:00
"com.google.android.youtube"
2020-06-15 16:56:56 +00:00
else
"com.vanced.android.youtube"
2020-06-02 17:41:50 +00:00
2020-06-15 16:56:56 +00:00
@Suppress("DEPRECATION")
2020-06-02 17:41:50 +00:00
val vancedVerCode =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pm?.getPackageInfo(
2020-06-02 17:41:50 +00:00
vanPkgName,
0
)?.longVersionCode?.and(0xFFFFFFFF)?.toInt()
2020-06-01 16:38:31 +00:00
}
else {
pm?.getPackageInfo(
2020-06-02 17:41:50 +00:00
vanPkgName,
0
)?.versionCode
2020-06-01 16:38:31 +00:00
}
when {
vancedRemoteCode > vancedVerCode!! -> {
2020-06-01 16:38:31 +00:00
vancedinstallbtn?.text =
2020-06-01 17:46:20 +00:00
activity?.getString(R.string.update)
2020-06-01 16:38:31 +00:00
vancedinstallbtn?.icon =
2020-06-01 17:46:20 +00:00
activity?.getDrawable(R.drawable.ic_cloud_upload_black_24dp)
2020-06-01 16:38:31 +00:00
}
vancedRemoteCode == vancedVerCode -> {
vancedinstallbtn?.text =
2020-06-01 17:46:20 +00:00
activity?.getString(R.string.button_installed)
2020-06-01 16:38:31 +00:00
vancedinstallbtn?.icon =
2020-06-01 17:46:20 +00:00
activity?.getDrawable(R.drawable.outline_cloud_done_24)
2020-06-01 16:38:31 +00:00
}
2020-06-04 07:35:26 +00:00
2020-05-24 18:57:11 +00:00
}
2020-06-04 17:12:50 +00:00
2020-06-20 16:30:44 +00:00
}
2020-06-19 18:20:41 +00:00
2020-06-20 16:30:44 +00:00
if (variant == "root" && viewModel.signatureStatusTxt.value != getString(R.string.signature_disabled)) {
when (viewModel.signatureStatusTxt.value) {
getString(R.string.unavailable) -> disableVancedButton(getString(R.string.signature_not_checked))
getString(R.string.signature_enabled) -> disableVancedButton(getString(R.string.signature_disable))
else -> throw NotImplementedError("Error handling status")
}
2020-05-24 18:57:11 +00:00
}
2020-05-24 16:08:10 +00:00
2020-05-24 15:25:11 +00:00
val oa2 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0f, 0.3f)
val oa3 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0.3f, -1f)
oa2.start()
oa3.apply {
oa3.addListener(onEnd = {
networkErrorLayout?.visibility = View.GONE
})
start()
}
} else {
2020-06-20 15:11:24 +00:00
if (variant == "nonroot") {
2020-06-09 08:46:21 +00:00
view?.findViewById<MaterialButton>(R.id.microg_installbtn)?.visibility = View.INVISIBLE
2020-05-30 17:00:45 +00:00
}
2020-05-24 16:08:10 +00:00
2020-05-30 17:00:45 +00:00
vancedinstallbtn?.visibility = View.INVISIBLE
2020-05-24 16:08:10 +00:00
2020-05-24 15:25:11 +00:00
val oa2 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", -1f, 0.3f)
val oa3 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0.3f, 0f)
oa2.apply {
oa2.addListener(onStart = {
networkErrorLayout?.visibility = View.VISIBLE
})
start()
}
oa3.start()
2020-05-24 15:06:20 +00:00
}
2020-05-24 15:25:11 +00:00
2020-05-24 15:15:17 +00:00
2020-05-24 15:06:20 +00:00
}
}
}
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
2020-06-19 18:20:41 +00:00
val viewModel: HomeViewModel by viewModels()
when (intent.action) {
SIGNATURE_DISABLED -> {
2020-06-19 18:57:20 +00:00
activity?.runOnUiThread {
viewModel.signatureStatusTxt.value = getString(R.string.signature_disabled)
}
val mIntent = Intent(activity, RootAppUninstaller::class.java)
2020-06-09 08:46:21 +00:00
mIntent.putExtra("Data", "com.vanced.stub")
activity?.startService(mIntent)
2020-06-20 15:11:24 +00:00
activity?.recreate()
}
SIGNATURE_ENABLED -> {
2020-06-19 18:57:20 +00:00
activity?.runOnUiThread {
viewModel.signatureStatusTxt.value = getString(R.string.signature_enabled)
}
2020-06-20 15:11:24 +00:00
activity?.recreate()
2020-06-19 15:48:14 +00:00
}
MICROG_DOWNLOADING -> {
val progress = intent.getIntExtra("microgProgress", 0)
val progressbar = view?.findViewById<ProgressBar>(R.id.microg_progress)
2020-06-20 15:11:24 +00:00
val downloadTxt = intent.getStringExtra("fileName")
val txt = view?.findViewById<TextView>(R.id.microg_downloading)
txt?.visibility = View.VISIBLE
txt?.text = downloadTxt
2020-06-19 15:48:14 +00:00
progressbar?.visibility = View.VISIBLE
progressbar?.progress = progress
}
VANCED_DOWNLOADING -> {
val progress = intent.getIntExtra("vancedProgress", 0)
val progressbar = view?.findViewById<ProgressBar>(R.id.vanced_progress)
2020-06-20 15:11:24 +00:00
val downloadTxt = intent.getStringExtra("fileName")
val txt = view?.findViewById<TextView>(R.id.vanced_downloading)
txt?.visibility = View.VISIBLE
txt?.text = downloadTxt
2020-06-19 15:48:14 +00:00
progressbar?.visibility = View.VISIBLE
progressbar?.progress = progress
}
2020-06-20 15:11:24 +00:00
MICROG_DOWNLOADED -> {
view?.findViewById<TextView>(R.id.microg_downloading)?.visibility = View.GONE
view?.findViewById<ProgressBar>(R.id.microg_progress)?.visibility = View.GONE
2020-06-22 12:11:57 +00:00
activity?.let { installApp(it, it.filesDir.path + "/microg.apk", "com.mgoogle.android.gms") }
2020-06-20 15:11:24 +00:00
}
VANCED_DOWNLOADED -> {
view?.findViewById<TextView>(R.id.vanced_downloading)?.visibility = View.GONE
view?.findViewById<ProgressBar>(R.id.vanced_progress)?.visibility = View.GONE
2020-06-20 16:00:21 +00:00
view?.findViewById<ProgressBar>(R.id.vanced_installing)?.visibility = View.VISIBLE
2020-06-20 15:11:24 +00:00
}
2020-06-19 18:20:41 +00:00
DOWNLOAD_ERROR -> {
val error = intent.getStringExtra("DownloadError") as String
Toast.makeText(activity, error, Toast.LENGTH_SHORT).show()
Log.d("VMDwnld", error)
}
}
}
}
private fun registerReceivers() {
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
SIGNATURE_DISABLED
))
}
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
SIGNATURE_ENABLED
)
)
}
2020-06-19 15:48:14 +00:00
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
VANCED_DOWNLOADING
)
)
}
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
MICROG_DOWNLOADING
)
)
}
2020-06-20 15:11:24 +00:00
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
VANCED_DOWNLOADED
)
)
}
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
MICROG_DOWNLOADED
)
)
}
2020-06-19 18:21:21 +00:00
activity?.let {
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, IntentFilter(
DOWNLOAD_ERROR
)
)
}
2020-06-19 15:48:14 +00:00
}
2020-06-02 12:19:04 +00:00
private fun attachNonrootChangelog() {
sectionPageAdapter = SectionPageAdapter(this)
val tabLayout = view?.findViewById(R.id.tablayout) as TabLayout
viewPager = view?.findViewById(R.id.viewpager)!!
viewPager.adapter = sectionPageAdapter
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "MicroG"
2 -> tab.text = "Manager"
}
}.attach()
}
private fun attachRootChangelog() {
sectionPageRootAdapter = SectionPageRootAdapter(this)
val tabLayout = view?.findViewById(R.id.tablayout) as TabLayout
viewPager = view?.findViewById(R.id.viewpager)!!
viewPager.adapter = sectionPageRootAdapter
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "Manager"
}
}.attach()
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.toolbar_menu, menu)
2020-04-22 09:41:18 +00:00
super .onCreateOptionsMenu(menu, inflater)
}
2020-06-15 16:56:56 +00:00
private fun disableVancedButton(txt: String) {
val vancedinstallbtn = view?.findViewById<MaterialButton>(R.id.vanced_installbtn)
vancedinstallbtn?.isEnabled = false
vancedinstallbtn?.backgroundTintList = ColorStateList.valueOf(Color.DKGRAY)
vancedinstallbtn?.setTextColor(ColorStateList.valueOf(Color.GRAY))
vancedinstallbtn?.text = txt
vancedinstallbtn?.icon = null
}
companion object {
const val SIGNATURE_DISABLED = "Signature verification disabled"
const val SIGNATURE_ENABLED = "Signature verification enabled"
2020-06-19 15:48:14 +00:00
const val VANCED_DOWNLOADING = "Vanced downloading"
const val MICROG_DOWNLOADING = "MicroG downloading"
2020-06-20 15:11:24 +00:00
const val VANCED_DOWNLOADED = "Vanced downloaded"
const val MICROG_DOWNLOADED = "MicroG downloaded"
2020-06-19 18:20:41 +00:00
const val DOWNLOAD_ERROR = "Error occurred"
}
2020-03-18 18:10:54 +00:00
}