0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2025-01-05 15:01:01 +00:00

cleanup and optimisations

This commit is contained in:
X1nto 2020-07-02 00:34:34 +04:00
parent 5b326da2f6
commit cb0fcddfed
8 changed files with 60 additions and 92 deletions

View file

@ -1,15 +1,31 @@
package com.vanced.manager.core
import android.app.Application
import android.widget.Toast
import androidx.preference.PreferenceManager
import com.dezlum.codelabs.getjson.GetJson
import com.downloader.PRDownloader
import com.vanced.manager.R
import com.vanced.manager.utils.InternetTools
import com.vanced.manager.utils.NotificationHelper.createNotifChannel
class App: Application() {
override fun onCreate() {
super.onCreate()
checkUpdates()
PRDownloader.initialize(applicationContext)
createNotifChannel(this)
}
private fun checkUpdates() {
val checkPrefs = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("auto_check_update", true)
if (checkPrefs) {
if (GetJson().isConnected(this) && InternetTools.isUpdateAvailable()) {
Toast.makeText(this, getString(R.string.update_found), Toast.LENGTH_SHORT).show()
} else
Toast.makeText(this, getString(R.string.update_notfound), Toast.LENGTH_SHORT).show()
}
}
}

View file

@ -25,21 +25,13 @@ open class Main: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkUpdates()
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val firstStart = prefs.getBoolean("firstStart", true)
val isUpgrading = prefs.getBoolean("isUpgrading", false)
val variant = prefs.getString("vanced_variant", "nonroot")
val shouldShowRootDialog = prefs.getBoolean("show_root_dialog", true)
val falseStatement = prefs.getBoolean("statement", true)
when {
firstStart -> showSecurityDialog(this)
!falseStatement -> statementFalse(this)
isUpgrading -> prefs.edit().putBoolean("isUpgrading", false).apply()
variant == "root" && shouldShowRootDialog -> showRootDialog(this)
!firstStart && !isMiuiOptimisationsDisabled() -> secondMiuiDialog(this)
}
@ -52,13 +44,4 @@ open class Main: AppCompatActivity() {
super.onPause()
}
private fun checkUpdates() {
val checkPrefs = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("auto_check_update", true)
if (checkPrefs) {
if (GetJson().isConnected(this) && InternetTools.isUpdateAvailable()) {
UpdateCheckFragment().show(supportFragmentManager, "Update")
} else Toast.makeText(this, getString(R.string.update_notfound), Toast.LENGTH_SHORT).show()
}
}
}

View file

@ -5,7 +5,6 @@ import android.content.Context
import android.content.Intent
import android.os.IBinder
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.dezlum.codelabs.getjson.GetJson
import com.downloader.Error
import com.downloader.OnDownloadListener
import com.downloader.OnStartOrResumeListener
@ -18,9 +17,6 @@ import com.vanced.manager.utils.InternetTools.getObjectFromJson
import com.vanced.manager.utils.NotificationHelper
import com.vanced.manager.utils.NotificationHelper.cancelNotif
import com.vanced.manager.utils.NotificationHelper.createBasicNotif
import java.lang.IllegalStateException
import java.lang.RuntimeException
import java.util.concurrent.ExecutionException
class MicrogDownloadService: Service() {
@ -33,7 +29,7 @@ class MicrogDownloadService: Service() {
private fun downloadMicrog() {
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
val apkUrl = getObjectFromJson("https://vanced.app/api/v1/microg.json", "url")
val apkUrl = getObjectFromJson("https://vanced.app/api/v1/microg.json", "url", this)
val channel = 420
PRDownloader.download(apkUrl, filesDir.path, "microg.apk")

View file

@ -5,7 +5,6 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import com.dezlum.codelabs.getjson.GetJson
@ -23,9 +22,6 @@ import com.vanced.manager.utils.InternetTools.getObjectFromJson
import com.vanced.manager.utils.NotificationHelper.cancelNotif
import com.vanced.manager.utils.NotificationHelper.createBasicNotif
import com.vanced.manager.utils.NotificationHelper.displayDownloadNotif
import java.lang.IllegalStateException
import java.lang.RuntimeException
import java.util.concurrent.ExecutionException
class VancedDownloadService: Service() {
@ -40,7 +36,7 @@ class VancedDownloadService: Service() {
type: String = "arch"
) {
val baseUrl = PreferenceManager.getDefaultSharedPreferences(this).getString("install_url", baseUrl)
val vancedVer = getObjectFromJson("https://vanced.app/api/v1/vanced.json", "version")
val vancedVer = getObjectFromJson("https://vanced.app/api/v1/vanced.json", "version", this)
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
val variant = PreferenceManager.getDefaultSharedPreferences(this).getString("vanced_variant", "nonroot")

View file

@ -17,6 +17,7 @@ import com.vanced.manager.core.downloader.MicrogDownloadService
import com.vanced.manager.core.downloader.VancedDownloadService
import com.vanced.manager.ui.MainActivity
import com.vanced.manager.ui.dialogs.DialogContainer.secondMiuiDialog
import com.vanced.manager.ui.dialogs.DialogContainer.showRootDialog
import com.vanced.manager.utils.MiuiHelper
import com.vanced.manager.utils.PackageHelper.uninstallApk
@ -104,8 +105,10 @@ open class Home : BaseFragment(), View.OnClickListener {
R.id.vanced_uninstallbtn -> activity?.let { uninstallApk(vancedPkgName, it) }
R.id.nonroot_switch -> writeToVariantPref("nonroot", R.anim.slide_in_left, R.anim.slide_out_right)
R.id.root_switch ->
if (Shell.rootAccess())
if (Shell.rootAccess()) {
writeToVariantPref("root", R.anim.slide_in_right, R.anim.slide_out_left)
activity?.let { showRootDialog(it) }
}
else {
writeToVariantPref("nonroot", R.anim.slide_in_left, R.anim.slide_out_right)
Toast.makeText(activity, "Root access not granted", Toast.LENGTH_SHORT).show()

View file

@ -92,12 +92,17 @@ object DialogContainer {
}
fun installAlertBuilder(msg: String, context: Context) {
AlertDialog.Builder(context)
.setTitle(context.getString(R.string.error))
.setMessage(msg)
.setPositiveButton(context.getString(R.string.close)) { dialog, _ -> dialog.dismiss() }
.create()
.show()
AlertDialog.Builder(context).apply {
setTitle(context.getString(R.string.error))
setMessage(msg)
setPositiveButton(context.getString(R.string.close)) { dialog, _ -> dialog.dismiss() }
if (msg == context.getString(R.string.installation_signature))
setNeutralButton(context.getString(R.string.guide)) { _, _ ->
openUrl("https://lmgtfy.com/?q=andnixsh+apk+verification+disable", R.color.Twitter, context)
}
create()
show()
}
}
fun regularPackageInstalled(msg: String, activity: MainActivity) {

View file

@ -4,7 +4,6 @@ import android.app.Application
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Intent
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
@ -24,7 +23,7 @@ import com.vanced.manager.utils.PackageHelper.isPackageInstalled
class HomeViewModel(application: Application): AndroidViewModel(application) {
private val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot")
private val connected: Boolean = GetJson().isConnected(application)
//private val connected: Boolean = GetJson().isConnected(application)
private val vancedPkgName: String =
if (variant== "root") {
@ -110,24 +109,22 @@ class HomeViewModel(application: Application): AndroidViewModel(application) {
}
private fun compareInt(int1: Int, int2: Int, application: Application): String {
return if (connected)
when {
int2 == 0 -> application.getString(R.string.install)
int1 > int2 -> application.getString(R.string.update)
int2 == int1 -> application.getString(R.string.button_reinstall)
else -> application.getString(R.string.install)
} else application.getString(R.string.install)
return when {
int2 == 0 -> application.getString(R.string.install)
int1 > int2 -> application.getString(R.string.update)
int2 == int1 -> application.getString(R.string.button_reinstall)
else -> application.getString(R.string.install)
}
}
private fun compareIntDrawable(int1: Int, int2: Int, application: Application): Drawable? {
return if (connected)
when {
int2 == 0 -> application.getDrawable(R.drawable.ic_download)
int1 > int2 -> application.getDrawable(R.drawable.ic_update)
int2 == int1 -> application.getDrawable(R.drawable.ic_done)
else -> application.getDrawable(R.drawable.ic_download)
} else application.getDrawable(R.drawable.ic_download)
return when {
int2 == 0 -> application.getDrawable(R.drawable.ic_download)
int1 > int2 -> application.getDrawable(R.drawable.ic_update)
int2 == int1 -> application.getDrawable(R.drawable.ic_done)
else -> application.getDrawable(R.drawable.ic_download)
}
}
init {

View file

@ -8,9 +8,6 @@ import androidx.preference.PreferenceManager
import com.dezlum.codelabs.getjson.GetJson
import com.vanced.manager.BuildConfig
import com.vanced.manager.R
import java.lang.IllegalStateException
import java.lang.RuntimeException
import java.util.concurrent.ExecutionException
object InternetTools {
@ -25,51 +22,26 @@ object InternetTools {
fun displayJsonString(json: String, obj: String, context: Context): String {
val installUrl = PreferenceManager.getDefaultSharedPreferences(context).getString("install_url", baseUrl)
try {
return GetJson().AsJSONObject("$installUrl/$json").get(obj).asString
} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
e.printStackTrace()
} catch (e: RuntimeException) {
e.printStackTrace()
}
return context.getString(R.string.unavailable)
return if (GetJson().isConnected(context))
GetJson().AsJSONObject("$installUrl/$json").get(obj).asString
else
context.getString(R.string.unavailable)
}
fun displayJsonInt(json: String, obj: String, context: Context): Int {
val installUrl = PreferenceManager.getDefaultSharedPreferences(context).getString("install_url", baseUrl)
try {
return GetJson().AsJSONObject("$installUrl/$json").get(obj).asInt
} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
e.printStackTrace()
} catch (e: RuntimeException) {
e.printStackTrace()
}
return 0
return if (GetJson().isConnected(context))
GetJson().AsJSONObject("$installUrl/$json").get(obj).asInt
else
0
}
fun getObjectFromJson(url: String, obj: String): String {
try {
return GetJson().AsJSONObject(url).get(obj).asString
} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
e.printStackTrace()
} catch (e: RuntimeException) {
e.printStackTrace()
}
return ""
fun getObjectFromJson(url: String, obj: String, context: Context): String {
return if (GetJson().isConnected(context))
GetJson().AsJSONObject(url).get(obj).asString
else
""
}
fun isUpdateAvailable(): Boolean {