0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-02 01:02:39 +00:00

Code improvements

This commit is contained in:
X1nto 2020-05-24 22:57:11 +04:00
parent 2b9ffd938a
commit 4ecc4f74a1
6 changed files with 148 additions and 137 deletions

View file

@ -1,12 +1,9 @@
package com.vanced.manager.core
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import com.dezlum.codelabs.getjson.GetJson
import com.vanced.manager.R
@ -22,23 +19,6 @@ open class Main: BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
),
666
)
}
}
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val firstStart = prefs.getBoolean("firstStart", true)
val isUpgrading = prefs.getBoolean("isUpgrading", false)
@ -62,22 +42,14 @@ open class Main: BaseActivity() {
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
69 -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT)
.show()
}
else
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
override fun onDestroy() {
super.onDestroy()
try {
val cacheDir: File = cacheDir
if (cacheDir.isDirectory)
cacheDir.delete()
} catch (e: Exception) {
Log.d("VMCache", "Unable to delete cacheDir")
}
}

View file

@ -1,14 +1,18 @@
package com.vanced.manager.core.base
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import com.dezlum.codelabs.getjson.GetJson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.subscribeBy
@ -48,7 +52,13 @@ open class BaseFragment : Fragment() {
else -> return
}
val task = activity?.filesDir?.path?.let { Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) }
val task = activity?.cacheDir?.path?.let {
Task(
url = url,
saveName = getFileNameFromUrl(url),
savePath = it
)
}
if (task?.file()?.exists()!!)
task.delete()
@ -77,7 +87,13 @@ open class BaseFragment : Fragment() {
fun downloadEn() {
val url = "https://x1nto.github.io/VancedFiles/Splits/Language/split_config.en.apk"
val task = activity?.filesDir?.path?.let { Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) }
val task = activity?.cacheDir?.path?.let {
Task(
url = url,
saveName = getFileNameFromUrl(url),
savePath = it
)
}
if (task?.file()?.exists()!!)
task.delete()
@ -97,4 +113,49 @@ open class BaseFragment : Fragment() {
}
fun installApk(url: String, loadBar: ProgressBar) {
val apkUrl = GetJson().AsJSONObject(url)
val dwnldUrl = apkUrl.get("url").asString
val task = activity?.filesDir?.path?.let {
Task(
url = dwnldUrl,
saveName = getFileNameFromUrl(url),
savePath = it
)
}
if (task?.file()?.exists()!!)
task.delete()
disposable = task
.download()
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = { progress ->
loadBar.visibility = View.VISIBLE
loadBar.progress = progress.percent().toInt()
},
onComplete = {
loadBar.visibility = View.GONE
val pn = activity?.packageName
val apk = task.file()
val uri =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
FileProvider.getUriForFile(requireContext(), "$pn.provider", apk)
} else
Uri.fromFile(apk)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
},
onError = { throwable ->
Toast.makeText(requireContext(), throwable.toString(), Toast.LENGTH_SHORT)
.show()
}
)
}
}

View file

@ -3,28 +3,18 @@ package com.vanced.manager.core.fragments
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.navigation.findNavController
import com.dezlum.codelabs.getjson.GetJson
import com.vanced.manager.R
import com.vanced.manager.core.base.BaseFragment
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.subscribeBy
import zlc.season.rxdownload4.download
import zlc.season.rxdownload4.file
import java.io.File
open class Home : BaseFragment() {
private var disposable: Disposable? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -52,30 +42,29 @@ open class Home : BaseFragment() {
val vancedStatus = pm?.let { isPackageInstalled("com.vanced.android.youtube", it) }
vancedinstallbtn.setOnClickListener {
try {
val cacheDir: File? = activity?.cacheDir
if (cacheDir?.isDirectory!!)
cacheDir.delete()
} catch (e: Exception) {
Log.d("VMCache", "Unable to delete cacheDir")
}
view.findNavController().navigate(R.id.toInstallVariantFragment)
}
microginstallbtn.setOnClickListener {
if (ContextCompat.checkSelfPermission(requireContext(),
android.Manifest.permission.READ_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(requireContext(),
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
420)
} else {
installApk("https://x1nto.github.io/VancedFiles/microg.json", microgProgress)
}
installApk("https://x1nto.github.io/VancedFiles/microg.json", microgProgress)
}
val microgVerText = view.findViewById<TextView>(R.id.microg_installed_version)
if (microgStatus!!) {
microginstallbtn.text = getString(R.string.installed)
microginstallbtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.outline_cloud_done_24, 0, 0, 0)
microginstallbtn.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.outline_cloud_done_24,
0,
0,
0
)
val microgVer = pm.getPackageInfo("com.mgoogle.android.gms", 0).versionName
microguninstallbtn.setOnClickListener {
@ -83,7 +72,7 @@ open class Home : BaseFragment() {
val uri = Uri.parse("package:com.mgoogle.android.gms")
val mgUninstall = Intent(Intent.ACTION_DELETE, uri)
startActivity(mgUninstall)
} catch (e: ActivityNotFoundException){
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireContext(), "App not installed", Toast.LENGTH_SHORT).show()
activity?.recreate()
}
@ -98,7 +87,7 @@ open class Home : BaseFragment() {
"org.microg.gms.ui.SettingsActivity"
)
startActivity(intent)
} catch (e: ActivityNotFoundException){
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireContext(), "App not installed", Toast.LENGTH_SHORT).show()
activity?.recreate()
}
@ -112,15 +101,13 @@ open class Home : BaseFragment() {
val vancedVerText = view.findViewById<TextView>(R.id.vanced_installed_version)
if (vancedStatus!!) {
vancedinstallbtn.text = getString(R.string.installed)
vancedinstallbtn.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.outline_cloud_done_24, 0, 0, 0)
val vancedVer = pm.getPackageInfo("com.vanced.android.youtube", 0).versionName
vanceduninstallbtn.setOnClickListener {
try {
val uri = Uri.parse("package:com.vanced.android.youtube")
val vanUninstall = Intent(Intent.ACTION_DELETE, uri)
startActivity(vanUninstall)
} catch (e: ActivityNotFoundException){
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireContext(), "App not installed", Toast.LENGTH_SHORT).show()
activity?.recreate()
}
@ -156,58 +143,4 @@ open class Home : BaseFragment() {
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
420 -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(requireContext(), "Permission Granted", Toast.LENGTH_SHORT).show()
}
else
Toast.makeText(requireContext(), "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
private fun installApk(url: String, loadBar: ProgressBar) {
val apkUrl = GetJson().AsJSONObject(url)
val dwnldUrl = apkUrl.get("url").asString
if (dwnldUrl.file().exists())
dwnldUrl.file().delete()
disposable = dwnldUrl.download()
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = { progress ->
loadBar.visibility = View.VISIBLE
loadBar.progress = progress.percent().toInt()
},
onComplete = {
loadBar.visibility = View.GONE
val pn = activity?.packageName
val apk = dwnldUrl.file()
val uri =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
FileProvider.getUriForFile(requireContext(), "$pn.provider", apk)
} else
Uri.fromFile(apk)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
},
onError = { throwable ->
Toast.makeText(requireContext(), throwable.toString(), Toast.LENGTH_SHORT).show()
}
)
}
}

View file

@ -21,7 +21,6 @@ open class VariantInstall : BaseFragment() {
when {
Build.SUPPORTED_ABIS.contains("x86") -> "x86"
Build.SUPPORTED_ABIS.contains("arm64-v8a") -> "arm64_v8a"
Build.SUPPORTED_ABIS.contains("armeabi-v7a") -> "armeabi_v7a"
else -> "armeabi_v7a"
}

View file

@ -12,7 +12,6 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.google.android.material.card.MaterialCardView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.JsonObject
import com.vanced.manager.R
import com.vanced.manager.adapter.SectionPageAdapter
import com.vanced.manager.core.fragments.Home
@ -61,25 +60,71 @@ class HomeFragment : Home() {
}
private fun checkNetwork() {
val pm = activity?.packageManager
val microgStatus = pm?.let { isPackageInstalled("com.mgoogle.android.gms", it) }
val vancedStatus = pm?.let { isPackageInstalled("com.vanced.android.youtube", it) }
val microginstallbtn = view?.findViewById<Button>(R.id.microg_installbtn)
val vancedinstallbtn = view?.findViewById<Button>(R.id.vanced_installbtn)
val vancedLatestTxt = view?.findViewById<TextView>(R.id.vanced_latest_version)
val microgLatestTxt = view?.findViewById<TextView>(R.id.microg_latest_version)
val networkErrorLayout = view?.findViewById<MaterialCardView>(R.id.home_network_wrapper)
disposable = ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { isConnectedToInternet ->
run {
val microginstallbtn = view?.findViewById<Button>(R.id.microg_installbtn)
val vancedinstallbtn = view?.findViewById<Button>(R.id.vanced_installbtn)
val vancedLatestTxt = view?.findViewById<TextView>(R.id.vanced_latest_version)
val microgLatestTxt = view?.findViewById<TextView>(R.id.microg_latest_version)
if (isConnectedToInternet) {
vancedinstallbtn?.visibility = View.VISIBLE
microginstallbtn?.visibility = View.VISIBLE
val vancedVer: JsonObject = GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json")
val microgVer: JsonObject = GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/microg.json")
vancedLatestTxt?.text = vancedVer.get("version").asString
microgLatestTxt?.text = microgVer.get("version").asString
val vancedRemoteVer: String =
GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json")
.get("version").asString
val microgRemoteVer: String =
GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/microg.json")
.get("version").asString
vancedLatestTxt?.text = vancedRemoteVer
microgLatestTxt?.text = microgRemoteVer
when {
microgStatus!! -> {
val microgVer =
pm.getPackageInfo("com.mgoogle.microg", 0).versionName
when {
microgRemoteVer > microgVer -> {
microginstallbtn?.text = getString(R.string.update)
}
microgRemoteVer == microgVer -> {
microginstallbtn?.text = getString(R.string.update)
}
}
}
vancedStatus!! -> {
val vancedVer =
pm.getPackageInfo("com.vanced.android.youtube", 0).versionName
when {
vancedRemoteVer > vancedVer -> {
vancedinstallbtn?.text = getString(R.string.update)
vancedinstallbtn?.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.ic_cloud_upload_black_24dp,
0,
0,
0
)
}
microgRemoteVer == vancedVer -> {
vancedinstallbtn?.text = getString(R.string.update)
vancedinstallbtn?.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.outline_cloud_done_24,
0,
0,
0
)
}
}
}
}
val oa2 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0f, 0.3f)
val oa3 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0.3f, -1f)

View file

@ -18,7 +18,8 @@
<string name="brave_browser">Brave Browser</string>
<string name="changelogs">Changelogs</string>
<string name="install">Install</string>
<string name="installed">Installed:</string>
<string name="version_installed">Installed:</string>
<string name="button_installed">Installed</string>
<string name="latest">Latest:</string>
<string name="loading">Loading…</string>
<string name="n_a">N/A</string>