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

199 lines
8.1 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui.fragments
2020-03-18 18:10:54 +00:00
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-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.*
2020-06-25 14:22:33 +00:00
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.*
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-25 11:29:07 +00:00
import com.google.android.material.button.MaterialButton
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-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-03-18 18:10:54 +00:00
class HomeFragment : Home() {
2020-03-18 18:10:54 +00:00
2020-06-04 07:35:26 +00:00
private lateinit var binding: FragmentHomeBinding
2020-06-25 14:22:33 +00:00
private var isExpanded: Boolean = false
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-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
if (variantPref == "root")
2020-06-02 12:19:04 +00:00
attachRootChangelog()
else {
2020-06-02 12:19:04 +00:00
attachNonrootChangelog()
2020-06-24 19:17:21 +00:00
if (!viewModel.microgInstalled) {
2020-06-26 16:00:17 +00:00
disableVancedButton()
2020-06-24 19:17:21 +00:00
}
}
2020-06-25 14:22:33 +00:00
view.findViewById<ImageButton>(R.id.changelog_button).setOnClickListener {
cardExpandCollapse()
2020-06-25 14:22:33 +00:00
}
2020-06-25 15:58:23 +00:00
}
2020-06-25 14:22:33 +00:00
private fun cardExpandCollapse() {
2020-06-25 15:58:23 +00:00
val viewPagerContainer = view?.findViewById<ViewPager2>(R.id.viewpager)
val tabLayoutContainer = view?.findViewById<TabLayout>(R.id.tablayout)
2020-06-25 15:11:58 +00:00
val arrow = view?.findViewById<ImageButton>(R.id.changelog_button)
2020-06-25 14:22:33 +00:00
if (isExpanded) {
viewPagerContainer?.visibility = View.GONE
2020-06-25 15:58:23 +00:00
tabLayoutContainer?.visibility = View.GONE
2020-06-25 14:22:33 +00:00
isExpanded = false
2020-06-26 16:00:17 +00:00
arrow?.startAnimation(RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f))
2020-06-25 14:22:33 +00:00
} else {
viewPagerContainer?.visibility = View.VISIBLE
2020-06-25 15:58:23 +00:00
tabLayoutContainer?.visibility = View.VISIBLE
2020-06-25 14:22:33 +00:00
isExpanded = true
2020-06-26 16:00:17 +00:00
arrow?.startAnimation(RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f))
2020-06-25 14:22:33 +00:00
}
2020-03-18 18:10:54 +00:00
}
override fun onPause() {
super.onPause()
activity?.let { LocalBroadcastManager.getInstance(it).unregisterReceiver(broadcastReceiver) }
}
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
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
view?.findViewById<ProgressBar>(R.id.microg_installing)?.visibility = View.VISIBLE
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() {
2020-06-23 13:47:20 +00:00
val intentFilter = IntentFilter()
intentFilter.addAction(VANCED_DOWNLOADING)
intentFilter.addAction(MICROG_DOWNLOADING)
intentFilter.addAction(VANCED_DOWNLOADED)
intentFilter.addAction(MICROG_DOWNLOADED)
activity?.let {
2020-06-23 13:47:20 +00:00
LocalBroadcastManager.getInstance(it).registerReceiver(broadcastReceiver, intentFilter)
2020-06-19 18:21:21 +00:00
}
2020-06-19 15:48:14 +00:00
}
2020-06-02 12:19:04 +00:00
private fun attachNonrootChangelog() {
2020-06-25 15:11:58 +00:00
val sectionPageAdapter = SectionPageAdapter(this)
2020-06-02 12:19:04 +00:00
val tabLayout = view?.findViewById(R.id.tablayout) as TabLayout
2020-06-25 15:11:58 +00:00
val viewPager = view?.findViewById(R.id.viewpager) as ViewPager2
2020-06-02 12:19:04 +00:00
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() {
2020-06-25 15:11:58 +00:00
val sectionPageRootAdapter = SectionPageRootAdapter(this)
2020-06-02 12:19:04 +00:00
val tabLayout = view?.findViewById(R.id.tablayout) as TabLayout
2020-06-25 15:11:58 +00:00
val viewPager = view?.findViewById(R.id.viewpager) as ViewPager2
2020-06-02 12:19:04 +00:00
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-26 16:00:17 +00:00
private fun disableVancedButton() {
2020-06-15 16:56:56 +00:00
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?.icon = null
}
companion object {
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
}