0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-15 23:35:06 +00:00
VancedManager/app/src/main/java/com/vanced/manager/model/DataModel.kt

79 lines
3 KiB
Kotlin
Raw Normal View History

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
import com.vanced.manager.utils.InternetTools.getJsonInt
import com.vanced.manager.utils.InternetTools.getJsonString
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-26 11:50:02 +00:00
open class DataModel(
private val jsonName: String,
private val context: Context
) {
2020-08-26 17:21:37 +00:00
private val variant = getDefaultSharedPreferences(context).getString("vanced_variant", "nonroot")
2020-08-26 11:50:02 +00:00
private val appPkg =
when (jsonName) {
"vanced" -> if (variant == "root") "com.google.android.youtube" else "com.vanced.android.youtube"
"microg" -> "com.mgoogle.android.gms"
2020-08-26 17:21:37 +00:00
else -> "com.vanced.android.youtube.music"
2020-08-26 11:50:02 +00:00
}
open fun isAppInstalled(): Boolean = isPackageInstalled(appPkg, context.packageManager)
2020-08-26 17:21:37 +00:00
open fun getVersionName(): String = runBlocking { getJsonString("$jsonName.json", "version", context) }
2020-08-26 11:50:02 +00:00
2020-08-26 17:21:37 +00:00
open fun getVersionCode(): Int = runBlocking { getJsonInt("$jsonName.json", "versionCode", context) }
2020-08-26 11:50:02 +00:00
2020-08-26 17:21:37 +00:00
open fun getInstalledVersionName(): String = getPkgVersion(isAppInstalled(), appPkg)
2020-08-26 11:50:02 +00:00
2020-08-26 17:21:37 +00:00
open fun getInstalledVersionCode(): Int = runBlocking { getJsonInt("$jsonName.json", "versionCode", context) }
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-26 11:50:02 +00:00
private fun getPkgVersion(toCheck: Boolean, pkg: String): String {
return if (toCheck) {
2020-08-26 17:21:37 +00:00
context.packageManager.getPackageInfo(pkg, 0).versionName
2020-08-26 11:50:02 +00:00
} else {
context.getString(R.string.unavailable)
}
}
@Suppress("DEPRECATION")
private fun getPkgVerCode(toCheck: Boolean, pkg: String): Int {
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)
}
}
}