From 4ecc4f74a17d4b438fc7c6d6904945bfb76c2b16 Mon Sep 17 00:00:00 2001 From: X1nto Date: Sun, 24 May 2020 22:57:11 +0400 Subject: [PATCH] Code improvements --- .../main/java/com/vanced/manager/core/Main.kt | 46 ++------ .../vanced/manager/core/base/BaseFragment.kt | 65 ++++++++++- .../com/vanced/manager/core/fragments/Home.kt | 105 ++++-------------- .../manager/core/fragments/VariantInstall.kt | 1 - .../manager/ui/fragments/HomeFragment.kt | 65 +++++++++-- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 148 insertions(+), 137 deletions(-) diff --git a/app/src/main/java/com/vanced/manager/core/Main.kt b/app/src/main/java/com/vanced/manager/core/Main.kt index 489debda..b51cfc80 100644 --- a/app/src/main/java/com/vanced/manager/core/Main.kt +++ b/app/src/main/java/com/vanced/manager/core/Main.kt @@ -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, - 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") } } diff --git a/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt b/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt index 5e8fa02e..ba72e58e 100644 --- a/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt +++ b/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt @@ -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() + } + ) + } + } \ No newline at end of file diff --git a/app/src/main/java/com/vanced/manager/core/fragments/Home.kt b/app/src/main/java/com/vanced/manager/core/fragments/Home.kt index 31389060..ae82ba6c 100644 --- a/app/src/main/java/com/vanced/manager/core/fragments/Home.kt +++ b/app/src/main/java/com/vanced/manager/core/fragments/Home.kt @@ -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(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(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, - 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() - } - ) - } - } \ No newline at end of file diff --git a/app/src/main/java/com/vanced/manager/core/fragments/VariantInstall.kt b/app/src/main/java/com/vanced/manager/core/fragments/VariantInstall.kt index 15229bcc..0136c795 100644 --- a/app/src/main/java/com/vanced/manager/core/fragments/VariantInstall.kt +++ b/app/src/main/java/com/vanced/manager/core/fragments/VariantInstall.kt @@ -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" } diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt index e00d6e49..3247a28c 100644 --- a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt @@ -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