2020-08-26 11:50:02 +00:00
|
|
|
package com.vanced.manager.model
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.graphics.drawable.Drawable
|
2020-08-26 17:21:37 +00:00
|
|
|
import android.os.Build
|
2020-08-26 11:50:02 +00:00
|
|
|
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
|
2020-08-31 16:12:20 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.baseUrl
|
2020-08-26 11:50:02 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.getJsonInt
|
|
|
|
import com.vanced.manager.utils.InternetTools.getJsonString
|
2020-08-31 16:12:20 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.getObjectFromJson
|
2020-08-26 11:50:02 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
|
2020-08-26 17:21:37 +00:00
|
|
|
import com.vanced.manager.R
|
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-08-31 16:12:20 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.coroutineScope
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
2020-08-26 11:50:02 +00:00
|
|
|
|
|
|
|
open class DataModel(
|
2020-09-04 19:14:43 +00:00
|
|
|
private val jsonName: String,
|
|
|
|
private val variant: String = "nonroot",
|
2020-08-26 11:50:02 +00:00
|
|
|
private val context: Context
|
|
|
|
) {
|
|
|
|
|
|
|
|
private val appPkg =
|
|
|
|
when (jsonName) {
|
|
|
|
"vanced" -> if (variant == "root") "com.google.android.youtube" else "com.vanced.android.youtube"
|
|
|
|
"microg" -> "com.mgoogle.android.gms"
|
2020-09-04 19:14:43 +00:00
|
|
|
else -> "com.vanced.android.apps.youtube.music"
|
2020-08-26 11:50:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
/*
|
|
|
|
private var versionName: String = ""
|
|
|
|
private var installedVersionName: String = ""
|
|
|
|
private var changelog: String = ""
|
|
|
|
|
|
|
|
private var versionCode: Int = 0
|
|
|
|
private var installedVersionCode: Int = 0
|
|
|
|
*/
|
|
|
|
|
2020-08-26 11:50:02 +00:00
|
|
|
open fun isAppInstalled(): Boolean = isPackageInstalled(appPkg, context.packageManager)
|
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
open fun getVersionName(): String = runBlocking(Dispatchers.IO) {
|
|
|
|
getJsonString("$jsonName.json", "version", context)
|
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
open fun getVersionCode(): Int = runBlocking(Dispatchers.IO) {
|
|
|
|
getJsonInt("$jsonName.json", "versionCode", context)
|
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
open fun getInstalledVersionName(): String = runBlocking(Dispatchers.IO) {
|
|
|
|
getPkgVersionName(isAppInstalled(), appPkg)
|
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
open fun getInstalledVersionCode(): Int = runBlocking(Dispatchers.IO) {
|
|
|
|
getPkgVersionCode(isAppInstalled(), appPkg)
|
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
|
|
|
open fun getButtonTxt(): String = compareInt(getInstalledVersionCode(), getVersionCode())
|
|
|
|
|
2020-08-26 17:21:37 +00:00
|
|
|
open fun getButtonIcon(): Drawable? = compareIntDrawable(getInstalledVersionCode(), getVersionCode())
|
2020-08-31 16:12:20 +00:00
|
|
|
|
|
|
|
open fun getChangelog(): String = runBlocking(Dispatchers.IO) {
|
2020-09-04 19:14:43 +00:00
|
|
|
when (jsonName) {
|
|
|
|
"vanced" -> getObjectFromJson("$baseUrl/changelog/${getVersionName().replace('.', '_')}.json", "message")
|
|
|
|
"music" -> getJsonString("$jsonName.json", "changelog", context)
|
|
|
|
else -> getObjectFromJson("https://ytvanced.github.io/VancedBackend/$jsonName.json", "changelog")
|
|
|
|
}
|
2020-08-31 16:12:20 +00:00
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
private fun getPkgVersionName(toCheck: Boolean, pkg: String): String {
|
2020-08-26 11:50:02 +00:00
|
|
|
return if (toCheck) {
|
2020-08-31 16:12:20 +00:00
|
|
|
context.packageManager.getPackageInfo(pkg, 0).versionName.removeSuffix("-vanced")
|
2020-08-26 11:50:02 +00:00
|
|
|
} else {
|
|
|
|
context.getString(R.string.unavailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
2020-08-31 16:12:20 +00:00
|
|
|
private fun getPkgVersionCode(toCheck: Boolean, pkg: String): Int {
|
2020-08-26 11:50:02 +00:00
|
|
|
return if (toCheck) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
2020-08-26 17:21:37 +00:00
|
|
|
context.packageManager.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF).toInt()
|
2020-08-26 11:50:02 +00:00
|
|
|
else
|
2020-08-26 17:21:37 +00:00
|
|
|
context.packageManager.getPackageInfo(pkg, 0).versionCode
|
2020-08-26 11:50:02 +00:00
|
|
|
} else 0
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun compareInt(int1: Int, int2: Int): String {
|
|
|
|
return when {
|
|
|
|
int1 == 0 -> context.getString(R.string.install)
|
|
|
|
int2 > int1 -> context.getString(R.string.update)
|
|
|
|
int2 == int1 || int1 > int2 -> context.getString(R.string.button_reinstall)
|
|
|
|
else -> context.getString(R.string.install)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun compareIntDrawable(int1: Int, int2: Int): Drawable? {
|
|
|
|
return when {
|
|
|
|
int1 == 0 -> context.getDrawable(R.drawable.ic_download)
|
|
|
|
int2 > int1 -> context.getDrawable(R.drawable.ic_update)
|
|
|
|
int2 == int1 -> context.getDrawable(R.drawable.ic_done)
|
|
|
|
else -> context.getDrawable(R.drawable.ic_download)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|