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

163 lines
6.0 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui.fragments
2020-03-18 18:10:54 +00:00
import android.content.*
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
import android.view.*
2020-06-28 14:47:51 +00:00
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.Toast
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 com.google.android.material.tabs.TabLayoutMediator
import com.vanced.manager.R
2020-07-04 16:06:07 +00:00
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-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
private val viewModel: HomeViewModel by viewModels()
private val localBroadcastManager by lazy { LocalBroadcastManager.getInstance(requireActivity()) }
2020-03-18 18:10:54 +00:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
2020-03-18 18:10:54 +00:00
): View? {
requireActivity().title = getString(R.string.title_home)
2020-04-22 09:41:18 +00:00
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
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
}
}
binding.includeChangelogsLayout.changelogButton.setOnClickListener {
cardExpandCollapse()
2020-06-25 14:22:33 +00:00
}
binding.includeVancedLayout.vancedCard.setOnLongClickListener {
val clip = requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clip.setPrimaryClip(ClipData.newPlainText("vanced", this.viewModel.vancedInstalledVersion.value))
versionToast("Vanced")
true
}
binding.includeMicrogLayout.microgCard.setOnLongClickListener {
val clip = requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clip.setPrimaryClip(ClipData.newPlainText("microg", viewModel.microgInstalledVersion.value))
versionToast("MicroG")
true
}
2020-06-25 15:58:23 +00:00
}
2020-06-25 14:22:33 +00:00
private fun versionToast(name: String) {
2020-07-03 17:52:15 +00:00
Toast.makeText(activity, getString(R.string.version_toast, name), Toast.LENGTH_LONG).show()
}
2020-06-25 14:22:33 +00:00
private fun cardExpandCollapse() {
with(binding.includeChangelogsLayout) {
viewpager.visibility = if (isExpanded) View.GONE else View.VISIBLE
tablayout.visibility = if (isExpanded) View.GONE else View.VISIBLE
changelogButton.animate().apply {
rotation(if (isExpanded) 0F else 180F)
interpolator = AccelerateDecelerateInterpolator()
}
isExpanded = !isExpanded
2020-06-25 14:22:33 +00:00
}
2020-03-18 18:10:54 +00:00
}
override fun onPause() {
localBroadcastManager.unregisterReceiver(broadcastReceiver)
super.onPause()
}
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
2020-06-20 15:11:24 +00:00
MICROG_DOWNLOADED -> {
binding.includeMicrogLayout.microgInstalling.visibility = View.VISIBLE
2020-07-02 14:50:02 +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 -> {
binding.includeVancedLayout.vancedInstalling.visibility = View.VISIBLE
2020-06-20 15:11:24 +00:00
}
}
}
}
private fun registerReceivers() {
2020-06-23 13:47:20 +00:00
val intentFilter = IntentFilter()
intentFilter.addAction(VANCED_DOWNLOADED)
intentFilter.addAction(MICROG_DOWNLOADED)
localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter)
}
2020-06-02 12:19:04 +00:00
private fun attachNonrootChangelog() {
2020-07-04 16:06:07 +00:00
val sectionPageRootAdapter = SectionPageAdapter(this)
with(binding.includeChangelogsLayout) {
viewpager.adapter = sectionPageRootAdapter
TabLayoutMediator(tablayout, viewpager) { tab, position ->
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "MicroG"
2 -> tab.text = "Manager"
}
}.attach()
}
2020-06-02 12:19:04 +00:00
}
private fun attachRootChangelog() {
2020-06-25 15:11:58 +00:00
val sectionPageRootAdapter = SectionPageRootAdapter(this)
with(binding.includeChangelogsLayout) {
viewpager.adapter = sectionPageRootAdapter
TabLayoutMediator(tablayout, viewpager) { tab, position ->
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "Manager"
}
}.attach()
}
2020-06-02 12:19:04 +00:00
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.toolbar_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
2020-06-26 16:00:17 +00:00
private fun disableVancedButton() {
binding.includeVancedLayout.vancedInstallbtn.apply {
icon = null
isEnabled = false
backgroundTintList = ColorStateList.valueOf(Color.DKGRAY)
setTextColor(ColorStateList.valueOf(Color.GRAY))
}
2020-06-15 16:56:56 +00:00
}
companion object {
const val VANCED_DOWNLOADED = "vanced_downloaded"
const val MICROG_DOWNLOADED = "microg_downloaded"
}
2020-03-18 18:10:54 +00:00
}