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

258 lines
11 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
2020-06-01 16:38:31 +00:00
import android.os.Build
2020-03-18 18:10:54 +00:00
import android.os.Bundle
import android.view.*
2020-05-23 17:17:27 +00:00
import android.widget.TextView
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-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
2020-06-04 07:35:26 +00:00
import com.vanced.manager.databinding.FragmentHomeBinding
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
2020-06-01 16:38:31 +00:00
@Suppress("DEPRECATION")
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)
2020-04-22 18:00:00 +00:00
2020-06-04 07:35:26 +00:00
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-05-24 15:06:20 +00:00
2020-06-02 12:19:04 +00:00
val variantPref = getDefaultSharedPreferences(activity).getString("vanced_variant", "Nonroot")
2020-05-30 13:49:03 +00:00
2020-06-04 07:35:26 +00:00
/*val microgWrapper = view.findViewById<MaterialCardView>(R.id.home_microg_wrapper)
2020-06-02 13:26:40 +00:00
if (variantPref == "Root") {
2020-05-30 13:49:03 +00:00
activity?.runOnUiThread {
microgWrapper.visibility = View.GONE
}
} else {
activity?.runOnUiThread {
microgWrapper.visibility = View.VISIBLE
}
}
2020-06-04 07:35:26 +00:00
*/
2020-05-30 13:49:03 +00:00
2020-06-02 12:19:04 +00:00
if (variantPref == "Root")
attachRootChangelog()
else
attachNonrootChangelog()
2020-03-18 18:10:54 +00:00
}
2020-05-24 19:21:12 +00:00
private fun initNetworkFun() {
2020-05-24 18:57:11 +00:00
val pm = activity?.packageManager
val microgStatus = pm?.let { isPackageInstalled("com.mgoogle.android.gms", it) }
2020-06-04 07:35:26 +00:00
//val vancedStatus = 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 18:57:11 +00:00
val vancedLatestTxt = view?.findViewById<TextView>(R.id.vanced_latest_version)
2020-05-24 15:15:17 +00:00
val networkErrorLayout = view?.findViewById<MaterialCardView>(R.id.home_network_wrapper)
2020-06-02 17:41:50 +00:00
val variant = getDefaultSharedPreferences(activity).getString("vanced_variant", "Nonroot")
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-05-28 07:19:46 +00:00
val vancedRemoteVer =
2020-05-24 18:57:11 +00:00
GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json")
2020-05-28 17:24:21 +00:00
.get("version").asString
2020-05-28 07:19:46 +00:00
val microgRemoteVer =
2020-05-24 18:57:11 +00:00
GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/microg.json")
2020-05-28 17:24:21 +00:00
.get("version").asString
vancedLatestTxt?.text = vancedRemoteVer
2020-05-24 18:57:11 +00:00
2020-06-04 07:35:26 +00:00
//val vancedRemoteCode =
// GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json")
// .get("versionCode").asInt
2020-06-01 16:38:31 +00:00
val microgRemoteCode =
GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/microg.json")
.get("versionCode").asInt
2020-06-02 12:19:04 +00:00
if (variant == "Nonroot") {
2020-05-30 17:00:45 +00:00
val microgLatestTxt =
view?.findViewById<TextView>(R.id.microg_latest_version)
val microginstallbtn =
view?.findViewById<MaterialButton>(R.id.microg_installbtn)
microginstallbtn?.visibility = View.VISIBLE
microgLatestTxt?.text = microgRemoteVer
if (microgStatus!!) {
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
}
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-06-04 07:35:26 +00:00
/*
2020-05-26 13:21:30 +00:00
if (vancedStatus!!) {
2020-06-02 17:41:50 +00:00
val vanPkgName =
2020-06-02 17:11:53 +00:00
if (variant == "Root") {
2020-06-02 17:41:50 +00:00
"com.google.android.youtube"
} else {
"com.vanced.android.youtube"
}
val vancedVerCode =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pm.getPackageInfo(
vanPkgName,
0
).longVersionCode.and(0xFFFFFFFF).toInt()
2020-06-01 16:38:31 +00:00
}
else {
2020-06-02 17:41:50 +00:00
pm.getPackageInfo(
vanPkgName,
0
).versionCode
2020-06-01 16:38:31 +00:00
}
2020-06-04 07:35:26 +00:00
/*
2020-06-01 16:38:31 +00:00
when {
vancedRemoteCode > vancedVerCode -> {
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 07:35:26 +00:00
*/
2020-05-24 18:57:11 +00:00
}
2020-06-04 07:35:26 +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-05-30 17:00:45 +00:00
if (variant == "nonroot") {
val microgLatestTxt =
view?.findViewById<TextView>(R.id.microg_latest_version)
val microginstallbtn =
view?.findViewById<MaterialButton>(R.id.microg_installbtn)
microginstallbtn?.visibility = View.INVISIBLE
microgLatestTxt?.text = getString(R.string.unavailable)
}
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
vancedLatestTxt?.text = getString(R.string.unavailable)
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
}
}
}
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-03-18 18:10:54 +00:00
}